2 * quickstart.c - ACPI Direct App Launch driver
5 * Copyright (C) 2007-2010 Angelo Arrifano <miknix@gmail.com>
7 * Information gathered from disassembled 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"
48 * There will be two events:
49 * 0x02 - A hot button was pressed while device was off/sleeping.
50 * 0x80 - A hot button was pressed while device was up.
52 #define QUICKSTART_EVENT_WAKE 0x02
53 #define QUICKSTART_EVENT_RUNTIME 0x80
55 struct quickstart_btn {
58 struct quickstart_btn *next;
61 struct quickstart_acpi {
62 struct acpi_device *device;
63 struct quickstart_btn *btn;
66 static struct quickstart_driver_data {
67 struct quickstart_btn *btn_lst;
68 struct quickstart_btn *pressed;
71 static struct input_dev *quickstart_input;
73 /* Platform driver functions */
74 static ssize_t quickstart_buttons_show(struct device *dev,
75 struct device_attribute *attr,
79 struct quickstart_btn *ptr = quickstart_data.btn_lst;
82 return snprintf(buf, PAGE_SIZE, "none");
84 while (ptr && (count < PAGE_SIZE)) {
86 count += snprintf(buf + count,
88 "%d\t%s\n", ptr->id, ptr->name);
96 static ssize_t quickstart_pressed_button_show(struct device *dev,
97 struct device_attribute *attr,
100 return snprintf(buf, PAGE_SIZE, "%s\n",
101 (quickstart_data.pressed ?
102 quickstart_data.pressed->name : "none"));
106 static ssize_t quickstart_pressed_button_store(struct device *dev,
107 struct device_attribute *attr,
108 const char *buf, size_t count)
113 if (strncasecmp(buf, "none", 4) != 0)
116 quickstart_data.pressed = NULL;
120 /* Helper functions */
121 static int quickstart_btnlst_add(struct quickstart_btn **data)
123 struct quickstart_btn **ptr = &quickstart_data.btn_lst;
126 ptr = &((*ptr)->next);
128 *ptr = kzalloc(sizeof(struct quickstart_btn), GFP_KERNEL);
138 static void quickstart_btnlst_del(struct quickstart_btn *data)
140 struct quickstart_btn **ptr = &quickstart_data.btn_lst;
151 ptr = &((*ptr)->next);
157 static void quickstart_btnlst_free(void)
159 struct quickstart_btn *ptr = quickstart_data.btn_lst;
160 struct quickstart_btn *lptr = NULL;
172 /* ACPI Driver functions */
173 static void quickstart_acpi_notify(acpi_handle handle, u32 event, void *data)
175 struct quickstart_acpi *quickstart = data;
181 case QUICKSTART_EVENT_WAKE:
182 quickstart_data.pressed = quickstart->btn;
184 case QUICKSTART_EVENT_RUNTIME:
185 input_report_key(quickstart_input, quickstart->btn->id, 1);
186 input_sync(quickstart_input);
187 input_report_key(quickstart_input, quickstart->btn->id, 0);
188 input_sync(quickstart_input);
195 static int quickstart_acpi_ghid(struct quickstart_acpi *quickstart)
198 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
202 * This returns a buffer telling the button usage ID,
203 * and triggers pending notify events (The ones before booting).
205 status = acpi_evaluate_object(quickstart->device->handle, "GHID", NULL,
207 if (ACPI_FAILURE(status)) {
208 printk(KERN_ERR "quickstart: %s GHID method failed.\n",
209 quickstart->btn->name);
214 * <<The GHID method can return a BYTE, WORD, or DWORD.
215 * The value must be encoded in little-endian byte
216 * order (least significant byte first).>>
218 switch (buffer.length) {
220 quickstart->btn->id = *(uint8_t *)buffer.pointer;
223 quickstart->btn->id = *(uint16_t *)buffer.pointer;
226 quickstart->btn->id = *(uint32_t *)buffer.pointer;
229 quickstart->btn->id = *(uint64_t *)buffer.pointer;
232 printk(KERN_ERR "quickstart: %s GHID method returned buffer "
233 "of unexpected length %u\n",
234 quickstart->btn->name, buffer.length);
239 kfree(buffer.pointer);
244 static int quickstart_acpi_config(struct quickstart_acpi *quickstart)
246 char *bid = acpi_device_bid(quickstart->device);
250 name = kmalloc(strlen(bid) + 1, GFP_KERNEL);
254 /* Add button to list */
255 ret = quickstart_btnlst_add(&quickstart->btn);
261 quickstart->btn->name = name;
262 strcpy(quickstart->btn->name, bid);
267 static int quickstart_acpi_add(struct acpi_device *device)
271 struct quickstart_acpi *quickstart;
276 quickstart = kzalloc(sizeof(*quickstart), GFP_KERNEL);
280 quickstart->device = device;
282 strcpy(acpi_device_name(device), QUICKSTART_ACPI_DEVICE_NAME);
283 strcpy(acpi_device_class(device), QUICKSTART_ACPI_CLASS);
284 device->driver_data = quickstart;
286 /* Add button to list and initialize some stuff */
287 ret = quickstart_acpi_config(quickstart);
291 status = acpi_install_notify_handler(device->handle, ACPI_ALL_NOTIFY,
292 quickstart_acpi_notify,
294 if (ACPI_FAILURE(status)) {
295 printk(KERN_ERR "quickstart: Notify handler install error\n");
297 goto fail_installnotify;
300 ret = quickstart_acpi_ghid(quickstart);
307 acpi_remove_notify_handler(device->handle, ACPI_ALL_NOTIFY,
308 quickstart_acpi_notify);
311 quickstart_btnlst_del(quickstart->btn);
320 static int quickstart_acpi_remove(struct acpi_device *device, int type)
323 struct quickstart_acpi *quickstart;
328 quickstart = acpi_driver_data(device);
332 status = acpi_remove_notify_handler(device->handle, ACPI_ALL_NOTIFY,
333 quickstart_acpi_notify);
334 if (ACPI_FAILURE(status))
335 printk(KERN_ERR "quickstart: Error removing notify handler\n");
342 /* Platform driver structs */
343 static DEVICE_ATTR(pressed_button, 0666, quickstart_pressed_button_show,
344 quickstart_pressed_button_store);
345 static DEVICE_ATTR(buttons, 0444, quickstart_buttons_show, NULL);
346 static struct platform_device *pf_device;
347 static struct platform_driver pf_driver = {
349 .name = QUICKSTART_PF_DRIVER_NAME,
350 .owner = THIS_MODULE,
354 static const struct acpi_device_id quickstart_device_ids[] = {
355 {QUICKSTART_ACPI_HID, 0},
359 static struct acpi_driver quickstart_acpi_driver = {
360 .name = "quickstart",
361 .class = QUICKSTART_ACPI_CLASS,
362 .ids = quickstart_device_ids,
364 .add = quickstart_acpi_add,
365 .remove = quickstart_acpi_remove,
369 /* Module functions */
370 static void quickstart_exit(void)
372 input_unregister_device(quickstart_input);
374 device_remove_file(&pf_device->dev, &dev_attr_pressed_button);
375 device_remove_file(&pf_device->dev, &dev_attr_buttons);
377 platform_device_unregister(pf_device);
379 platform_driver_unregister(&pf_driver);
381 acpi_bus_unregister_driver(&quickstart_acpi_driver);
383 quickstart_btnlst_free();
386 static int __init quickstart_init_input(void)
388 struct quickstart_btn **ptr = &quickstart_data.btn_lst;
392 quickstart_input = input_allocate_device();
394 if (!quickstart_input)
397 quickstart_input->name = "Quickstart ACPI Buttons";
398 quickstart_input->id.bustype = BUS_HOST;
402 set_bit(EV_KEY, quickstart_input->evbit);
403 set_bit((*ptr)->id, quickstart_input->keybit);
404 ptr = &((*ptr)->next);
407 ret = input_register_device(quickstart_input);
409 input_free_device(quickstart_input);
416 static int __init quickstart_init(void)
424 /* ACPI driver register */
425 ret = acpi_bus_register_driver(&quickstart_acpi_driver);
429 /* If existing bus with no devices */
430 if (!quickstart_data.btn_lst) {
435 /* Platform driver register */
436 ret = platform_driver_register(&pf_driver);
440 /* Platform device register */
441 pf_device = platform_device_alloc(QUICKSTART_PF_DEVICE_NAME, -1);
444 goto fail_pfdev_alloc;
446 ret = platform_device_add(pf_device);
450 /* Create device sysfs file */
451 ret = device_create_file(&pf_device->dev, &dev_attr_pressed_button);
455 ret = device_create_file(&pf_device->dev, &dev_attr_buttons);
460 ret = quickstart_init_input();
464 printk(KERN_INFO "quickstart: ACPI Direct App Launch ver %s\n",
469 device_remove_file(&pf_device->dev, &dev_attr_buttons);
472 device_remove_file(&pf_device->dev, &dev_attr_pressed_button);
475 platform_device_del(pf_device);
478 platform_device_put(pf_device);
481 platform_driver_unregister(&pf_driver);
484 acpi_bus_unregister_driver(&quickstart_acpi_driver);
489 module_init(quickstart_init);
490 module_exit(quickstart_exit);