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.04"
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/types.h>
34 #include <linux/acpi.h>
35 #include <linux/platform_device.h>
36 #include <linux/input.h>
38 MODULE_AUTHOR("Angelo Arrifano");
39 MODULE_DESCRIPTION("ACPI Direct App Launch driver");
40 MODULE_LICENSE("GPL");
42 #define QUICKSTART_ACPI_DEVICE_NAME "quickstart"
43 #define QUICKSTART_ACPI_CLASS "quickstart"
44 #define QUICKSTART_ACPI_HID "PNP0C32"
46 #define QUICKSTART_PF_DRIVER_NAME "quickstart"
47 #define QUICKSTART_PF_DEVICE_NAME "quickstart"
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.
54 #define QUICKSTART_EVENT_WAKE 0x02
55 #define QUICKSTART_EVENT_RUNTIME 0x80
57 struct quickstart_button
{
60 struct list_head list
;
63 struct quickstart_acpi
{
64 struct acpi_device
*device
;
65 struct quickstart_button
*button
;
68 static LIST_HEAD(buttons
);
69 static struct quickstart_button
*pressed
;
71 static struct input_dev
*quickstart_input
;
73 /* Platform driver functions */
74 static ssize_t
buttons_show(struct device
*dev
, struct device_attribute
*attr
,
78 struct quickstart_button
*b
;
80 if (list_empty(&buttons
))
81 return snprintf(buf
, PAGE_SIZE
, "none");
83 list_for_each_entry(b
, &buttons
, list
) {
84 count
+= snprintf(buf
+ count
, PAGE_SIZE
- count
, "%u\t%s\n",
87 if (count
>= PAGE_SIZE
) {
96 static ssize_t
pressed_button_show(struct device
*dev
,
97 struct device_attribute
*attr
, char *buf
)
99 return scnprintf(buf
, PAGE_SIZE
, "%s\n",
100 (pressed
? pressed
->name
: "none"));
104 static ssize_t
pressed_button_store(struct device
*dev
,
105 struct device_attribute
*attr
,
106 const char *buf
, size_t count
)
111 if (strncasecmp(buf
, "none", 4) != 0)
118 /* Helper functions */
119 static struct quickstart_button
*quickstart_buttons_add(void)
121 struct quickstart_button
*b
;
123 b
= kzalloc(sizeof(*b
), GFP_KERNEL
);
127 list_add_tail(&b
->list
, &buttons
);
132 static void quickstart_button_del(struct quickstart_button
*data
)
137 list_del(&data
->list
);
142 static void quickstart_buttons_free(void)
144 struct quickstart_button
*b
, *n
;
146 list_for_each_entry_safe(b
, n
, &buttons
, list
)
147 quickstart_button_del(b
);
150 /* ACPI Driver functions */
151 static void quickstart_acpi_notify(acpi_handle handle
, u32 event
, void *data
)
153 struct quickstart_acpi
*quickstart
= data
;
159 case QUICKSTART_EVENT_WAKE
:
160 pressed
= quickstart
->button
;
162 case QUICKSTART_EVENT_RUNTIME
:
163 input_report_key(quickstart_input
, quickstart
->button
->id
, 1);
164 input_sync(quickstart_input
);
165 input_report_key(quickstart_input
, quickstart
->button
->id
, 0);
166 input_sync(quickstart_input
);
169 pr_err("Unexpected ACPI event notify (%u)\n", event
);
174 static int quickstart_acpi_ghid(struct quickstart_acpi
*quickstart
)
177 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
181 * This returns a buffer telling the button usage ID,
182 * and triggers pending notify events (The ones before booting).
184 status
= acpi_evaluate_object(quickstart
->device
->handle
, "GHID", NULL
,
186 if (ACPI_FAILURE(status
)) {
187 pr_err("%s GHID method failed\n", quickstart
->button
->name
);
192 * <<The GHID method can return a BYTE, WORD, or DWORD.
193 * The value must be encoded in little-endian byte
194 * order (least significant byte first).>>
196 switch (buffer
.length
) {
198 quickstart
->button
->id
= *(uint8_t *)buffer
.pointer
;
201 quickstart
->button
->id
= *(uint16_t *)buffer
.pointer
;
204 quickstart
->button
->id
= *(uint32_t *)buffer
.pointer
;
207 quickstart
->button
->id
= *(uint64_t *)buffer
.pointer
;
210 pr_err("%s GHID method returned buffer of unexpected length %lu\n",
211 quickstart
->button
->name
,
212 (unsigned long)buffer
.length
);
217 kfree(buffer
.pointer
);
222 static int quickstart_acpi_config(struct quickstart_acpi
*quickstart
)
224 char *bid
= acpi_device_bid(quickstart
->device
);
227 name
= kmalloc(strlen(bid
) + 1, GFP_KERNEL
);
231 /* Add new button to list */
232 quickstart
->button
= quickstart_buttons_add();
233 if (!quickstart
->button
) {
238 quickstart
->button
->name
= name
;
239 strcpy(quickstart
->button
->name
, bid
);
244 static int quickstart_acpi_add(struct acpi_device
*device
)
248 struct quickstart_acpi
*quickstart
;
253 quickstart
= kzalloc(sizeof(*quickstart
), GFP_KERNEL
);
257 quickstart
->device
= device
;
259 strcpy(acpi_device_name(device
), QUICKSTART_ACPI_DEVICE_NAME
);
260 strcpy(acpi_device_class(device
), QUICKSTART_ACPI_CLASS
);
261 device
->driver_data
= quickstart
;
263 /* Add button to list and initialize some stuff */
264 ret
= quickstart_acpi_config(quickstart
);
268 status
= acpi_install_notify_handler(device
->handle
, ACPI_ALL_NOTIFY
,
269 quickstart_acpi_notify
,
271 if (ACPI_FAILURE(status
)) {
272 pr_err("Notify handler install error\n");
274 goto fail_installnotify
;
277 ret
= quickstart_acpi_ghid(quickstart
);
284 acpi_remove_notify_handler(device
->handle
, ACPI_ALL_NOTIFY
,
285 quickstart_acpi_notify
);
288 quickstart_button_del(quickstart
->button
);
297 static int quickstart_acpi_remove(struct acpi_device
*device
)
300 struct quickstart_acpi
*quickstart
;
305 quickstart
= acpi_driver_data(device
);
309 status
= acpi_remove_notify_handler(device
->handle
, ACPI_ALL_NOTIFY
,
310 quickstart_acpi_notify
);
311 if (ACPI_FAILURE(status
))
312 pr_err("Error removing notify handler\n");
319 /* Platform driver structs */
320 static DEVICE_ATTR_RW(pressed_button
);
321 static DEVICE_ATTR_RO(buttons
);
322 static struct platform_device
*pf_device
;
323 static struct platform_driver pf_driver
= {
325 .name
= QUICKSTART_PF_DRIVER_NAME
,
326 .owner
= THIS_MODULE
,
330 static const struct acpi_device_id quickstart_device_ids
[] = {
331 {QUICKSTART_ACPI_HID
, 0},
335 static struct acpi_driver quickstart_acpi_driver
= {
336 .name
= "quickstart",
337 .class = QUICKSTART_ACPI_CLASS
,
338 .ids
= quickstart_device_ids
,
340 .add
= quickstart_acpi_add
,
341 .remove
= quickstart_acpi_remove
,
345 /* Module functions */
346 static void quickstart_exit(void)
348 input_unregister_device(quickstart_input
);
350 device_remove_file(&pf_device
->dev
, &dev_attr_pressed_button
);
351 device_remove_file(&pf_device
->dev
, &dev_attr_buttons
);
353 platform_device_unregister(pf_device
);
355 platform_driver_unregister(&pf_driver
);
357 acpi_bus_unregister_driver(&quickstart_acpi_driver
);
359 quickstart_buttons_free();
362 static int __init
quickstart_init_input(void)
364 struct quickstart_button
*b
;
367 quickstart_input
= input_allocate_device();
369 if (!quickstart_input
)
372 quickstart_input
->name
= "Quickstart ACPI Buttons";
373 quickstart_input
->id
.bustype
= BUS_HOST
;
375 list_for_each_entry(b
, &buttons
, list
) {
376 set_bit(EV_KEY
, quickstart_input
->evbit
);
377 set_bit(b
->id
, quickstart_input
->keybit
);
380 ret
= input_register_device(quickstart_input
);
382 input_free_device(quickstart_input
);
389 static int __init
quickstart_init(void)
393 /* ACPI driver register */
394 ret
= acpi_bus_register_driver(&quickstart_acpi_driver
);
398 /* If existing bus with no devices */
399 if (list_empty(&buttons
)) {
404 /* Platform driver register */
405 ret
= platform_driver_register(&pf_driver
);
409 /* Platform device register */
410 pf_device
= platform_device_alloc(QUICKSTART_PF_DEVICE_NAME
, -1);
413 goto fail_pfdev_alloc
;
415 ret
= platform_device_add(pf_device
);
419 /* Create device sysfs file */
420 ret
= device_create_file(&pf_device
->dev
, &dev_attr_pressed_button
);
424 ret
= device_create_file(&pf_device
->dev
, &dev_attr_buttons
);
429 ret
= quickstart_init_input();
433 pr_info("ACPI Direct App Launch ver %s\n", QUICKSTART_VERSION
);
437 device_remove_file(&pf_device
->dev
, &dev_attr_buttons
);
440 device_remove_file(&pf_device
->dev
, &dev_attr_pressed_button
);
443 platform_device_del(pf_device
);
446 platform_device_put(pf_device
);
449 platform_driver_unregister(&pf_driver
);
452 acpi_bus_unregister_driver(&quickstart_acpi_driver
);
457 module_init(quickstart_init
);
458 module_exit(quickstart_exit
);