1/*
2 *  atlas_btns.c - Atlas Wallmount Touchscreen ACPI Extras
3 *
4 *  Copyright (C) 2006 Jaya Kumar
5 *  Based on Toshiba ACPI by John Belmonte and ASUS ACPI
6 *  This work was sponsored by CIS(M) Sdn Bhd.
7 *
8 *  This program is free software; you can redistribute it and/or modify
9 *  it under the terms of the GNU General Public License as published by
10 *  the Free Software Foundation; either version 2 of the License, or
11 *  (at your option) any later version.
12 *
13 *  This program is distributed in the hope that it will be useful,
14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *  GNU General Public License for more details.
17 *
18 *  You should have received a copy of the GNU General Public License
19 *  along with this program; if not, write to the Free Software
20 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/input.h>
28#include <linux/types.h>
29#include <asm/uaccess.h>
30#include <acpi/acpi_drivers.h>
31
32#define ACPI_ATLAS_NAME			"Atlas ACPI"
33#define ACPI_ATLAS_CLASS		"Atlas"
34#define ACPI_ATLAS_BUTTON_HID		"ASIM0000"
35
36static struct input_dev *input_dev;
37
38/* button handling code */
39static acpi_status acpi_atlas_button_setup(acpi_handle region_handle,
40		    u32 function, void *handler_context, void **return_context)
41{
42	*return_context =
43		(function != ACPI_REGION_DEACTIVATE) ? handler_context : NULL;
44
45	return AE_OK;
46}
47
48static acpi_status acpi_atlas_button_handler(u32 function,
49		      acpi_physical_address address,
50		      u32 bit_width, acpi_integer *value,
51		      void *handler_context, void *region_context)
52{
53	acpi_status status;
54	int keycode;
55
56	if (function == ACPI_WRITE) {
57		keycode = KEY_F1 + (address & 0x0F);
58		input_report_key(input_dev, keycode, !(address & 0x10));
59		input_sync(input_dev);
60		status = 0;
61	} else {
62		printk(KERN_WARNING "atlas: shrugged on unexpected function"
63			":function=%x,address=%lx,value=%x\n",
64			function, (unsigned long)address, (u32)*value);
65		status = -EINVAL;
66	}
67
68	return status;
69}
70
71static int atlas_acpi_button_add(struct acpi_device *device)
72{
73	acpi_status status;
74	int err;
75
76	input_dev = input_allocate_device();
77	if (!input_dev) {
78		printk(KERN_ERR "atlas: unable to allocate input device\n");
79		return -ENOMEM;
80	}
81
82	input_dev->name = "Atlas ACPI button driver";
83	input_dev->phys = "ASIM0000/atlas/input0";
84	input_dev->id.bustype = BUS_HOST;
85	input_dev->evbit[LONG(EV_KEY)] = BIT(EV_KEY);
86
87	set_bit(KEY_F1, input_dev->keybit);
88	set_bit(KEY_F2, input_dev->keybit);
89	set_bit(KEY_F3, input_dev->keybit);
90	set_bit(KEY_F4, input_dev->keybit);
91	set_bit(KEY_F5, input_dev->keybit);
92	set_bit(KEY_F6, input_dev->keybit);
93	set_bit(KEY_F7, input_dev->keybit);
94	set_bit(KEY_F8, input_dev->keybit);
95	set_bit(KEY_F9, input_dev->keybit);
96
97	err = input_register_device(input_dev);
98	if (err) {
99		printk(KERN_ERR "atlas: couldn't register input device\n");
100		input_free_device(input_dev);
101		return err;
102	}
103
104	/* hookup button handler */
105	status = acpi_install_address_space_handler(device->handle,
106				0x81, &acpi_atlas_button_handler,
107				&acpi_atlas_button_setup, device);
108	if (ACPI_FAILURE(status)) {
109		printk(KERN_ERR "Atlas: Error installing addr spc handler\n");
110		input_unregister_device(input_dev);
111		status = -EINVAL;
112	}
113
114	return status;
115}
116
117static int atlas_acpi_button_remove(struct acpi_device *device, int type)
118{
119	acpi_status status;
120
121	status = acpi_remove_address_space_handler(device->handle,
122				0x81, &acpi_atlas_button_handler);
123	if (ACPI_FAILURE(status)) {
124		printk(KERN_ERR "Atlas: Error removing addr spc handler\n");
125		status = -EINVAL;
126	}
127
128	input_unregister_device(input_dev);
129
130	return status;
131}
132
133static struct acpi_driver atlas_acpi_driver = {
134	.name	= ACPI_ATLAS_NAME,
135	.class	= ACPI_ATLAS_CLASS,
136	.ids	= ACPI_ATLAS_BUTTON_HID,
137	.ops	= {
138		.add	= atlas_acpi_button_add,
139		.remove	= atlas_acpi_button_remove,
140	},
141};
142
143static int __init atlas_acpi_init(void)
144{
145	int result;
146
147	if (acpi_disabled)
148		return -ENODEV;
149
150	result = acpi_bus_register_driver(&atlas_acpi_driver);
151	if (result < 0) {
152		printk(KERN_ERR "Atlas ACPI: Unable to register driver\n");
153		return -ENODEV;
154	}
155
156	return 0;
157}
158
159static void __exit atlas_acpi_exit(void)
160{
161	acpi_bus_unregister_driver(&atlas_acpi_driver);
162}
163
164module_init(atlas_acpi_init);
165module_exit(atlas_acpi_exit);
166
167MODULE_AUTHOR("Jaya Kumar");
168MODULE_LICENSE("GPL");
169MODULE_DESCRIPTION("Atlas button driver");
170