RS485: fix inconsistencies in the meaning of some variables
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / quickstart / quickstart.c
blobc60911c6ab3fd974fe19c187652182337687fe00
1 /*
2 * quickstart.c - ACPI Direct App Launch driver
5 * Copyright (C) 2007-2010 Angelo Arrifano <miknix@gmail.com>
7 * Information gathered from disassebled dsdt and from here:
8 * <http://www.microsoft.com/whdc/system/platform/firmware/DirAppLaunch.mspx>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define QUICKSTART_VERSION "1.03"
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <acpi/acpi_drivers.h>
33 #include <linux/platform_device.h>
34 #include <linux/input.h>
36 MODULE_AUTHOR("Angelo Arrifano");
37 MODULE_DESCRIPTION("ACPI Direct App Launch driver");
38 MODULE_LICENSE("GPL");
40 #define QUICKSTART_ACPI_DEVICE_NAME "quickstart"
41 #define QUICKSTART_ACPI_CLASS "quickstart"
42 #define QUICKSTART_ACPI_HID "PNP0C32"
44 #define QUICKSTART_PF_DRIVER_NAME "quickstart"
45 #define QUICKSTART_PF_DEVICE_NAME "quickstart"
46 #define QUICKSTART_PF_DEVATTR_NAME "pressed_button"
48 #define QUICKSTART_MAX_BTN_NAME_LEN 16
50 /* There will be two events:
51 * 0x02 - A hot button was pressed while device was off/sleeping.
52 * 0x80 - A hot button was pressed while device was up. */
53 #define QUICKSTART_EVENT_WAKE 0x02
54 #define QUICKSTART_EVENT_RUNTIME 0x80
56 struct quickstart_btn {
57 char *name;
58 unsigned int id;
59 struct quickstart_btn *next;
62 static struct quickstart_driver_data {
63 struct quickstart_btn *btn_lst;
64 struct quickstart_btn *pressed;
65 } quickstart_data;
67 /* ACPI driver Structs */
68 struct quickstart_acpi {
69 struct acpi_device *device;
70 struct quickstart_btn *btn;
72 static int quickstart_acpi_add(struct acpi_device *device);
73 static int quickstart_acpi_remove(struct acpi_device *device, int type);
74 static const struct acpi_device_id quickstart_device_ids[] = {
75 {QUICKSTART_ACPI_HID, 0},
76 {"", 0},
79 static struct acpi_driver quickstart_acpi_driver = {
80 .name = "quickstart",
81 .class = QUICKSTART_ACPI_CLASS,
82 .ids = quickstart_device_ids,
83 .ops = {
84 .add = quickstart_acpi_add,
85 .remove = quickstart_acpi_remove,
89 /* Input device structs */
90 struct input_dev *quickstart_input;
92 /* Platform driver structs */
93 static ssize_t buttons_show(struct device *dev,
94 struct device_attribute *attr,
95 char *buf);
96 static ssize_t pressed_button_show(struct device *dev,
97 struct device_attribute *attr,
98 char *buf);
99 static ssize_t pressed_button_store(struct device *dev,
100 struct device_attribute *attr,
101 const char *buf,
102 size_t count);
103 static DEVICE_ATTR(pressed_button, 0666, pressed_button_show,
104 pressed_button_store);
105 static DEVICE_ATTR(buttons, 0444, buttons_show, NULL);
106 static struct platform_device *pf_device;
107 static struct platform_driver pf_driver = {
108 .driver = {
109 .name = QUICKSTART_PF_DRIVER_NAME,
110 .owner = THIS_MODULE,
115 * Platform driver functions
117 static ssize_t buttons_show(struct device *dev,
118 struct device_attribute *attr,
119 char *buf)
121 int count = 0;
122 struct quickstart_btn *ptr = quickstart_data.btn_lst;
124 if (!ptr)
125 return snprintf(buf, PAGE_SIZE, "none");
127 while (ptr && (count < PAGE_SIZE)) {
128 if (ptr->name) {
129 count += snprintf(buf + count,
130 PAGE_SIZE - count,
131 "%d\t%s\n", ptr->id, ptr->name);
133 ptr = ptr->next;
136 return count;
139 static ssize_t pressed_button_show(struct device *dev,
140 struct device_attribute *attr,
141 char *buf)
143 return snprintf(buf, PAGE_SIZE, "%s\n",
144 (quickstart_data.pressed ?
145 quickstart_data.pressed->name : "none"));
149 static ssize_t pressed_button_store(struct device *dev,
150 struct device_attribute *attr,
151 const char *buf, size_t count)
153 if (count < 2)
154 return -EINVAL;
156 if (strncasecmp(buf, "none", 4) != 0)
157 return -EINVAL;
159 quickstart_data.pressed = NULL;
160 return count;
163 /* Hotstart Helper functions */
164 static int quickstart_btnlst_add(struct quickstart_btn **data)
166 struct quickstart_btn **ptr = &quickstart_data.btn_lst;
168 while (*ptr)
169 ptr = &((*ptr)->next);
171 *ptr = kzalloc(sizeof(struct quickstart_btn), GFP_KERNEL);
172 if (!*ptr) {
173 *data = NULL;
174 return -ENOMEM;
176 *data = *ptr;
178 return 0;
181 static void quickstart_btnlst_del(struct quickstart_btn *data)
183 struct quickstart_btn **ptr = &quickstart_data.btn_lst;
185 if (!data)
186 return;
188 while (*ptr) {
189 if (*ptr == data) {
190 *ptr = (*ptr)->next;
191 kfree(data);
192 return;
194 ptr = &((*ptr)->next);
197 return;
200 static void quickstart_btnlst_free(void)
202 struct quickstart_btn *ptr = quickstart_data.btn_lst;
203 struct quickstart_btn *lptr = NULL;
205 while (ptr) {
206 lptr = ptr;
207 ptr = ptr->next;
208 kfree(lptr->name);
209 kfree(lptr);
212 return;
215 /* ACPI Driver functions */
216 static void quickstart_acpi_notify(acpi_handle handle, u32 event, void *data)
218 struct quickstart_acpi *quickstart = data;
220 if (!quickstart)
221 return;
223 if (event == QUICKSTART_EVENT_WAKE)
224 quickstart_data.pressed = quickstart->btn;
225 else if (event == QUICKSTART_EVENT_RUNTIME) {
226 input_report_key(quickstart_input, quickstart->btn->id, 1);
227 input_sync(quickstart_input);
228 input_report_key(quickstart_input, quickstart->btn->id, 0);
229 input_sync(quickstart_input);
231 return;
234 static void quickstart_acpi_ghid(struct quickstart_acpi *quickstart)
236 acpi_status status;
237 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
238 uint32_t usageid = 0;
240 if (!quickstart)
241 return;
243 /* This returns a buffer telling the button usage ID,
244 * and triggers pending notify events (The ones before booting). */
245 status = acpi_evaluate_object(quickstart->device->handle,
246 "GHID", NULL, &buffer);
247 if (ACPI_FAILURE(status) || !buffer.pointer) {
248 printk(KERN_ERR "quickstart: %s GHID method failed.\n",
249 quickstart->btn->name);
250 return;
253 if (buffer.length < 8)
254 return;
256 /* <<The GHID method can return a BYTE, WORD, or DWORD.
257 * The value must be encoded in little-endian byte
258 * order (least significant byte first).>> */
259 usageid = *((uint32_t *)(buffer.pointer + (buffer.length - 8)));
260 quickstart->btn->id = usageid;
262 kfree(buffer.pointer);
265 static int quickstart_acpi_config(struct quickstart_acpi *quickstart, char *bid)
267 int len = strlen(bid);
268 int ret;
270 /* Add button to list */
271 ret = quickstart_btnlst_add(&quickstart->btn);
272 if (ret)
273 return ret;
275 quickstart->btn->name = kzalloc(len + 1, GFP_KERNEL);
276 if (!quickstart->btn->name) {
277 quickstart_btnlst_free();
278 return -ENOMEM;
280 strcpy(quickstart->btn->name, bid);
282 return 0;
285 static int quickstart_acpi_add(struct acpi_device *device)
287 int ret = 0;
288 acpi_status status = AE_OK;
289 struct quickstart_acpi *quickstart = NULL;
291 if (!device)
292 return -EINVAL;
294 quickstart = kzalloc(sizeof(struct quickstart_acpi), GFP_KERNEL);
295 if (!quickstart)
296 return -ENOMEM;
298 quickstart->device = device;
299 strcpy(acpi_device_name(device), QUICKSTART_ACPI_DEVICE_NAME);
300 strcpy(acpi_device_class(device), QUICKSTART_ACPI_CLASS);
301 device->driver_data = quickstart;
303 /* Add button to list and initialize some stuff */
304 ret = quickstart_acpi_config(quickstart, acpi_device_bid(device));
305 if (ret)
306 goto fail_config;
308 status = acpi_install_notify_handler(device->handle,
309 ACPI_ALL_NOTIFY,
310 quickstart_acpi_notify,
311 quickstart);
312 if (ACPI_FAILURE(status)) {
313 printk(KERN_ERR "quickstart: Notify handler install error\n");
314 ret = -ENODEV;
315 goto fail_installnotify;
318 quickstart_acpi_ghid(quickstart);
320 return 0;
322 fail_installnotify:
323 quickstart_btnlst_del(quickstart->btn);
325 fail_config:
327 kfree(quickstart);
329 return ret;
332 static int quickstart_acpi_remove(struct acpi_device *device, int type)
334 acpi_status status = 0;
335 struct quickstart_acpi *quickstart = NULL;
337 if (!device || !acpi_driver_data(device))
338 return -EINVAL;
340 quickstart = acpi_driver_data(device);
342 status = acpi_remove_notify_handler(device->handle,
343 ACPI_ALL_NOTIFY,
344 quickstart_acpi_notify);
345 if (ACPI_FAILURE(status))
346 printk(KERN_ERR "quickstart: Error removing notify handler\n");
349 kfree(quickstart);
351 return 0;
354 /* Module functions */
356 static void quickstart_exit(void)
358 input_unregister_device(quickstart_input);
360 device_remove_file(&pf_device->dev, &dev_attr_pressed_button);
361 device_remove_file(&pf_device->dev, &dev_attr_buttons);
363 platform_device_unregister(pf_device);
365 platform_driver_unregister(&pf_driver);
367 acpi_bus_unregister_driver(&quickstart_acpi_driver);
369 quickstart_btnlst_free();
371 return;
374 static int __init quickstart_init_input(void)
376 struct quickstart_btn **ptr = &quickstart_data.btn_lst;
377 int count;
378 int ret;
380 quickstart_input = input_allocate_device();
382 if (!quickstart_input)
383 return -ENOMEM;
385 quickstart_input->name = "Quickstart ACPI Buttons";
386 quickstart_input->id.bustype = BUS_HOST;
388 while (*ptr) {
389 count++;
390 set_bit(EV_KEY, quickstart_input->evbit);
391 set_bit((*ptr)->id, quickstart_input->keybit);
392 ptr = &((*ptr)->next);
395 ret = input_register_device(quickstart_input);
396 if (ret) {
397 input_free_device(quickstart_input);
398 return ret;
401 return 0;
404 static int __init quickstart_init(void)
406 int ret;
408 /* ACPI Check */
409 if (acpi_disabled)
410 return -ENODEV;
412 /* ACPI driver register */
413 ret = acpi_bus_register_driver(&quickstart_acpi_driver);
414 if (ret)
415 return ret;
417 /* If existing bus with no devices */
418 if (!quickstart_data.btn_lst) {
419 ret = -ENODEV;
420 goto fail_pfdrv_reg;
423 /* Platform driver register */
424 ret = platform_driver_register(&pf_driver);
425 if (ret)
426 goto fail_pfdrv_reg;
428 /* Platform device register */
429 pf_device = platform_device_alloc(QUICKSTART_PF_DEVICE_NAME, -1);
430 if (!pf_device) {
431 ret = -ENOMEM;
432 goto fail_pfdev_alloc;
434 ret = platform_device_add(pf_device);
435 if (ret)
436 goto fail_pfdev_add;
438 /* Create device sysfs file */
439 ret = device_create_file(&pf_device->dev, &dev_attr_pressed_button);
440 if (ret)
441 goto fail_dev_file;
443 ret = device_create_file(&pf_device->dev, &dev_attr_buttons);
444 if (ret)
445 goto fail_dev_file2;
448 /* Input device */
449 ret = quickstart_init_input();
450 if (ret)
451 goto fail_input;
453 printk(KERN_INFO "quickstart: ACPI Direct App Launch ver %s\n",
454 QUICKSTART_VERSION);
456 return 0;
457 fail_input:
458 device_remove_file(&pf_device->dev, &dev_attr_buttons);
460 fail_dev_file2:
461 device_remove_file(&pf_device->dev, &dev_attr_pressed_button);
463 fail_dev_file:
464 platform_device_del(pf_device);
466 fail_pfdev_add:
467 platform_device_put(pf_device);
469 fail_pfdev_alloc:
470 platform_driver_unregister(&pf_driver);
472 fail_pfdrv_reg:
473 acpi_bus_unregister_driver(&quickstart_acpi_driver);
475 return ret;
478 module_init(quickstart_init);
479 module_exit(quickstart_exit);