[quickstart] register the module ACPI table
[quickstart] / quickstart / quickstart.c
1 /*
2  *  quickstart.c - ACPI Direct App Launch driver
3  *
4  *
5  *  Copyright (C) 2007-2008 Angelo Arrifano <miknix@gmail.com>
6  *
7  *  Information gathered from disassebled dsdt and from here:
8  *  "http://download.microsoft.com/download/9/c/5/
9  *  9c5b2167-8017-4bae-9fde-d599bac8184a/DirAppLaunch_Vista.doc"
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  *
25  */
26
27 #define QUICKSTART_VERSION "1.02"
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <acpi/acpi_drivers.h>
34 #include <linux/platform_device.h>
35 #include <linux/input.h>
36
37 MODULE_AUTHOR("Angelo Arrifano");
38 MODULE_DESCRIPTION("ACPI Direct App Launch driver");
39 MODULE_LICENSE("GPL");
40
41 #define QUICKSTART_ACPI_DEVICE_NAME   "quickstart"
42 #define QUICKSTART_ACPI_CLASS         "quickstart"
43 #define QUICKSTART_ACPI_HID           "PNP0C32"
44
45 #define QUICKSTART_PF_DRIVER_NAME     "quickstart"
46 #define QUICKSTART_PF_DEVICE_NAME     "quickstart"
47 #define QUICKSTART_PF_DEVATTR_NAME    "pressed_button"
48
49 #define QUICKSTART_MAX_BTN_NAME_LEN   16
50
51 /* There will be two events:
52          * 0x02 - A hot button was pressed while device was off/sleeping.
53          * 0x80 - A hot button was pressed while device was up. */
54 #define QUICKSTART_EVENT_WAKE         0x02
55 #define QUICKSTART_EVENT_RUNTIME      0x80
56
57 struct quickstart_btn {
58         char *name;
59         unsigned int id;
60         struct quickstart_btn *next;
61 };
62
63 static struct quickstart_driver_data {
64         struct quickstart_btn *btn_lst;
65         struct quickstart_btn *pressed;
66 } quickstart_data;
67
68 /* ACPI driver Structs */
69 struct quickstart_acpi {
70         struct acpi_device *device;
71         struct quickstart_btn *btn;
72 };
73 static int quickstart_acpi_add(struct acpi_device *device);
74 static int quickstart_acpi_remove(struct acpi_device *device, int type);
75 static const struct acpi_device_id  quickstart_device_ids[] = {
76         {QUICKSTART_ACPI_HID, 0},
77         {"", 0},
78 };
79
80 MODULE_DEVICE_TABLE(acpi, quickstart_device_ids);
81
82 static struct acpi_driver quickstart_acpi_driver = {
83         .name = "quickstart",
84         .class = QUICKSTART_ACPI_CLASS,
85         .ids = quickstart_device_ids,
86         .ops = {
87                         .add = quickstart_acpi_add,
88                         .remove = quickstart_acpi_remove,
89                 },
90 };
91
92 /* Input device structs */
93 struct input_dev *quickstart_input;
94
95 /* Platform driver structs */
96 static ssize_t buttons_show(struct device *dev,
97                                         struct device_attribute *attr,
98                                         char *buf);
99 static ssize_t pressed_button_show(struct device *dev,
100                                         struct device_attribute *attr,
101                                         char *buf);
102 static ssize_t pressed_button_store(struct device *dev,
103                                         struct device_attribute *attr,
104                                          const char *buf,
105                                          size_t count);
106 static DEVICE_ATTR(pressed_button, 0666, pressed_button_show,
107                                          pressed_button_store);
108 static DEVICE_ATTR(buttons, 0444, buttons_show, NULL);
109 static struct platform_device *pf_device;
110 static struct platform_driver pf_driver = {
111         .driver = {
112                 .name = QUICKSTART_PF_DRIVER_NAME,
113                 .owner = THIS_MODULE,
114         }
115 };
116
117 /*
118  * Platform driver functions
119  */
120 static ssize_t buttons_show(struct device *dev,
121                                          struct device_attribute *attr,
122                                          char *buf)
123 {
124         int count = 0;
125         struct quickstart_btn *ptr = quickstart_data.btn_lst;
126
127         if (!ptr)
128                 return snprintf(buf, PAGE_SIZE, "none");
129
130         while (ptr && (count < PAGE_SIZE)) {
131                 if (ptr->name) {
132                         count += snprintf(buf + count,
133                                         PAGE_SIZE - count,
134                                         "%d\t%s\n", ptr->id, ptr->name);
135                 }
136                 ptr = ptr->next;
137         }
138
139         return count;
140 }
141
142 static ssize_t pressed_button_show(struct device *dev,
143                                         struct device_attribute *attr,
144                                         char *buf)
145 {
146         return snprintf(buf, PAGE_SIZE, "%s\n",
147                 (quickstart_data.pressed?quickstart_data.pressed->name:"none"));
148 }
149
150
151 static ssize_t pressed_button_store(struct device *dev,
152                                          struct device_attribute *attr,
153                                          const char *buf, size_t count)
154 {
155         if (count < 2)
156                 return -EINVAL;
157
158         if (strncasecmp(buf, "none", 4) != 0)
159                 return -EINVAL;
160
161         quickstart_data.pressed = NULL;
162         return count;
163 }
164
165 /* Hotstart Helper functions */
166 static int quickstart_btnlst_add(struct quickstart_btn **data)
167 {
168         struct quickstart_btn **ptr = &quickstart_data.btn_lst;
169
170         while (*ptr)
171                 ptr = &((*ptr)->next);
172
173         *ptr = kzalloc(sizeof(struct quickstart_btn), GFP_KERNEL);
174         if (!*ptr) {
175                 *data = NULL;
176                 return -ENOMEM;
177         }
178         *data = *ptr;
179
180         return 0;
181 }
182
183 static void quickstart_btnlst_del(struct quickstart_btn *data)
184 {
185         struct quickstart_btn **ptr = &quickstart_data.btn_lst;
186
187         if (!data)
188                 return;
189
190         while (*ptr) {
191                 if (*ptr == data) {
192                         *ptr = (*ptr)->next;
193                         kfree(data);
194                         return;
195                 }
196                 ptr = &((*ptr)->next);
197         }
198
199         return;
200 }
201
202 static void quickstart_btnlst_free(void)
203 {
204         struct quickstart_btn *ptr = quickstart_data.btn_lst;
205         struct quickstart_btn *lptr = NULL;
206
207         while (ptr) {
208                 lptr = ptr;
209                 ptr = ptr->next;
210                 kfree(lptr->name);
211                 kfree(lptr);
212         }
213
214         return;
215 }
216
217 /* ACPI Driver functions */
218 static void quickstart_acpi_notify(acpi_handle handle, u32 event, void *data)
219 {
220         struct quickstart_acpi *quickstart = data;
221
222         if (!quickstart)
223                 return;
224
225         if (event == QUICKSTART_EVENT_WAKE)
226                 quickstart_data.pressed = quickstart->btn;
227         else if (event == QUICKSTART_EVENT_RUNTIME) {
228                 input_report_key(quickstart_input, quickstart->btn->id, 1);
229                 input_sync(quickstart_input);
230                 input_report_key(quickstart_input, quickstart->btn->id, 0);
231                 input_sync(quickstart_input);
232         }
233         return;
234 }
235
236 static void quickstart_acpi_ghid(struct quickstart_acpi *quickstart)
237 {
238         acpi_status status;
239         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
240         unsigned int usageid = 0;
241
242         if (!quickstart)
243                 return;
244
245         /* This returns a buffer telling the button usage ID,
246          * and triggers pending notify events (The ones before booting). */
247         status = acpi_evaluate_object(quickstart->device->handle,
248                                         "GHID", NULL, &buffer);
249         if (ACPI_FAILURE(status) || !buffer.pointer) {
250                 printk(KERN_ERR "quickstart: %s GHID method failed.\n",
251                        quickstart->btn->name);
252                 return;
253         }
254
255         if (buffer.length < 8)
256                 return;
257
258         /* <<The GHID method can return a BYTE, WORD, or DWORD.
259          * The value must be encoded in little-endian byte
260          * order (least significant byte first).>> */
261         usageid = le32_to_cpu(*(unsigned int*)(buffer.pointer + (buffer.length - 8)));
262         quickstart->btn->id = usageid;
263
264         kfree(buffer.pointer);
265 }
266
267 static int quickstart_acpi_config(struct quickstart_acpi *quickstart, char *bid)
268 {
269         int len = strlen(bid);
270         int ret;
271
272         /* Add button to list */
273         ret = quickstart_btnlst_add(&quickstart->btn);
274         if (ret)
275                 return ret;
276
277         quickstart->btn->name = kzalloc(len + 1, GFP_KERNEL);
278         if (!quickstart->btn->name) {
279                 quickstart_btnlst_free();
280                 return -ENOMEM;
281         }
282         strcpy(quickstart->btn->name, bid);
283
284         return 0;
285 }
286
287 static int quickstart_acpi_add(struct acpi_device *device)
288 {
289         int ret = 0;
290         acpi_status status = AE_OK;
291         struct quickstart_acpi *quickstart = NULL;
292
293         if (!device)
294                 return -EINVAL;
295
296         quickstart = kzalloc(sizeof(struct quickstart_acpi), GFP_KERNEL);
297         if (!quickstart)
298                 return -ENOMEM;
299
300         quickstart->device = device;
301         strcpy(acpi_device_name(device), QUICKSTART_ACPI_DEVICE_NAME);
302         strcpy(acpi_device_class(device), QUICKSTART_ACPI_CLASS);
303         device->driver_data = quickstart;
304
305         /* Add button to list and initialize some stuff */
306         ret = quickstart_acpi_config(quickstart, acpi_device_bid(device));
307         if (ret)
308                 goto fail_config;
309
310         status = acpi_install_notify_handler(device->handle,
311                                                 ACPI_ALL_NOTIFY,
312                                                 quickstart_acpi_notify,
313                                                 quickstart);
314         if (ACPI_FAILURE(status)) {
315                 printk(KERN_ERR "quickstart: Notify handler install error\n");
316                 ret = -ENODEV;
317                 goto fail_installnotify;
318         }
319
320         quickstart_acpi_ghid(quickstart);
321
322         return 0;
323
324 fail_installnotify:
325         quickstart_btnlst_del(quickstart->btn);
326
327 fail_config:
328
329         kfree(quickstart);
330
331         return ret;
332 }
333
334 static int quickstart_acpi_remove(struct acpi_device *device, int type)
335 {
336         acpi_status status = 0;
337         struct quickstart_acpi *quickstart = NULL;
338
339         if (!device || !acpi_driver_data(device))
340                 return -EINVAL;
341
342         quickstart = acpi_driver_data(device);
343
344         status = acpi_remove_notify_handler(device->handle,
345                                                  ACPI_ALL_NOTIFY,
346                                             quickstart_acpi_notify);
347         if (ACPI_FAILURE(status))
348                 printk(KERN_ERR "quickstart: Error removing notify handler\n");
349
350
351         kfree(quickstart);
352
353         return 0;
354 }
355
356 /* Module functions */
357
358 static void quickstart_exit(void)
359 {
360         input_unregister_device(quickstart_input);
361         input_free_device(quickstart_input);
362
363         device_remove_file(&pf_device->dev, &dev_attr_pressed_button);
364         device_remove_file(&pf_device->dev, &dev_attr_buttons);
365
366         platform_device_unregister(pf_device);
367
368         platform_driver_unregister(&pf_driver);
369
370         acpi_bus_unregister_driver(&quickstart_acpi_driver);
371
372         quickstart_btnlst_free();
373
374         return;
375 }
376
377 static int __init quickstart_init_input(void)
378 {
379         struct quickstart_btn **ptr = &quickstart_data.btn_lst;
380         int count;
381
382         quickstart_input = input_allocate_device();
383
384         if (!quickstart_input)
385                 return -ENOMEM;
386
387         quickstart_input->name = "Quickstart ACPI Buttons";
388         quickstart_input->id.bustype = BUS_HOST;
389
390         while (*ptr) {
391                 count++;
392                 set_bit(EV_KEY, quickstart_input->evbit);
393                 set_bit((*ptr)->id, quickstart_input->keybit);
394                 ptr = &((*ptr)->next);
395         }
396
397         return input_register_device(quickstart_input);
398 }
399
400 static int __init quickstart_init(void)
401 {
402         int ret;
403         acpi_status status = 0;
404
405         /* ACPI Check */
406         if (acpi_disabled)
407                 return -ENODEV;
408
409         /* ACPI driver register */
410         status = acpi_bus_register_driver(&quickstart_acpi_driver);
411         if (status < 0)
412                 return -ENODEV;
413
414         /* If existing bus with no devices */
415         if (!quickstart_data.btn_lst) {
416                 ret = -ENODEV;
417                 goto fail_pfdrv_reg;
418         }
419
420         /* Platform driver register */
421         ret = platform_driver_register(&pf_driver);
422         if (ret)
423                 goto fail_pfdrv_reg;
424
425         /* Platform device register */
426         pf_device = platform_device_alloc(QUICKSTART_PF_DEVICE_NAME, -1);
427         if (!pf_device) {
428                 ret = -ENOMEM;
429                 goto fail_pfdev_alloc;
430         }
431         ret = platform_device_add(pf_device);
432         if (ret)
433                 goto fail_pfdev_add;
434
435         /* Create device sysfs file */
436         ret = device_create_file(&pf_device->dev, &dev_attr_pressed_button);
437         if (ret)
438                 goto fail_dev_file;
439
440         ret = device_create_file(&pf_device->dev, &dev_attr_buttons);
441         if (ret)
442                 goto fail_dev_file2;
443
444
445         /* Input device */
446         ret = quickstart_init_input();
447         if (ret)
448                 goto fail_input;
449
450         printk(KERN_INFO "quickstart: ACPI Direct App Launch ver %s\n",
451                                                 QUICKSTART_VERSION);
452
453         return 0;
454 fail_input:
455         device_remove_file(&pf_device->dev, &dev_attr_buttons);
456
457 fail_dev_file2:
458         device_remove_file(&pf_device->dev, &dev_attr_pressed_button);
459
460 fail_dev_file:
461         platform_device_del(pf_device);
462
463 fail_pfdev_add:
464         platform_device_put(pf_device);
465
466 fail_pfdev_alloc:
467         platform_driver_unregister(&pf_driver);
468
469 fail_pfdrv_reg:
470         acpi_bus_unregister_driver(&quickstart_acpi_driver);
471
472         return ret;
473 }
474
475 module_init(quickstart_init);
476 module_exit(quickstart_exit);