OSDN Git Service

Staging: quickstart: Use %u for printing button id
[android-x86/kernel.git] / drivers / staging / quickstart / quickstart.c
1 /*
2  *  quickstart.c - ACPI Direct App Launch driver
3  *
4  *
5  *  Copyright (C) 2007-2010 Angelo Arrifano <miknix@gmail.com>
6  *
7  *  Information gathered from disassembled dsdt and from here:
8  *  <http://www.microsoft.com/whdc/system/platform/firmware/DirAppLaunch.mspx>
9  *
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.
14  *
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.
19  *
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
23  *
24  */
25
26 #define QUICKSTART_VERSION "1.03"
27
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>
35
36 MODULE_AUTHOR("Angelo Arrifano");
37 MODULE_DESCRIPTION("ACPI Direct App Launch driver");
38 MODULE_LICENSE("GPL");
39
40 #define QUICKSTART_ACPI_DEVICE_NAME     "quickstart"
41 #define QUICKSTART_ACPI_CLASS           "quickstart"
42 #define QUICKSTART_ACPI_HID             "PNP0C32"
43
44 #define QUICKSTART_PF_DRIVER_NAME       "quickstart"
45 #define QUICKSTART_PF_DEVICE_NAME       "quickstart"
46
47 /*
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.
51  */
52 #define QUICKSTART_EVENT_WAKE           0x02
53 #define QUICKSTART_EVENT_RUNTIME        0x80
54
55 struct quickstart_btn {
56         char *name;
57         unsigned int id;
58         struct list_head list;
59 };
60
61 struct quickstart_acpi {
62         struct acpi_device *device;
63         struct quickstart_btn *btn;
64 };
65
66 static LIST_HEAD(buttons);
67 static struct quickstart_btn *pressed;
68
69 static struct input_dev *quickstart_input;
70
71 /* Platform driver functions */
72 static ssize_t quickstart_buttons_show(struct device *dev,
73                                         struct device_attribute *attr,
74                                         char *buf)
75 {
76         int count = 0;
77         struct quickstart_btn *b;
78
79         if (list_empty(&buttons))
80                 return snprintf(buf, PAGE_SIZE, "none");
81
82         list_for_each_entry(b, &buttons, list) {
83                 count += snprintf(buf + count, PAGE_SIZE - count, "%u\t%s\n",
84                                                         b->id, b->name);
85
86                 if (count >= PAGE_SIZE) {
87                         count = PAGE_SIZE;
88                         break;
89                 }
90         }
91
92         return count;
93 }
94
95 static ssize_t quickstart_pressed_button_show(struct device *dev,
96                                                 struct device_attribute *attr,
97                                                 char *buf)
98 {
99         return snprintf(buf, PAGE_SIZE, "%s\n",
100                                         (pressed ? pressed->name : "none"));
101 }
102
103
104 static ssize_t quickstart_pressed_button_store(struct device *dev,
105                                                 struct device_attribute *attr,
106                                                 const char *buf, size_t count)
107 {
108         if (count < 2)
109                 return -EINVAL;
110
111         if (strncasecmp(buf, "none", 4) != 0)
112                 return -EINVAL;
113
114         pressed = NULL;
115         return count;
116 }
117
118 /* Helper functions */
119 static struct quickstart_btn *quickstart_buttons_add(void)
120 {
121         struct quickstart_btn *b;
122
123         b = kzalloc(sizeof(*b), GFP_KERNEL);
124         if (!b)
125                 return NULL;
126
127         list_add_tail(&b->list, &buttons);
128
129         return b;
130 }
131
132 static void quickstart_button_del(struct quickstart_btn *data)
133 {
134         if (!data)
135                 return;
136
137         list_del(&data->list);
138         kfree(data->name);
139         kfree(data);
140 }
141
142 static void quickstart_buttons_free(void)
143 {
144         struct quickstart_btn *b, *n;
145
146         list_for_each_entry_safe(b, n, &buttons, list)
147                 quickstart_button_del(b);
148 }
149
150 /* ACPI Driver functions */
151 static void quickstart_acpi_notify(acpi_handle handle, u32 event, void *data)
152 {
153         struct quickstart_acpi *quickstart = data;
154
155         if (!quickstart)
156                 return;
157
158         switch (event) {
159         case QUICKSTART_EVENT_WAKE:
160                 pressed = quickstart->btn;
161                 break;
162         case QUICKSTART_EVENT_RUNTIME:
163                 input_report_key(quickstart_input, quickstart->btn->id, 1);
164                 input_sync(quickstart_input);
165                 input_report_key(quickstart_input, quickstart->btn->id, 0);
166                 input_sync(quickstart_input);
167                 break;
168         default:
169                 break;
170         }
171 }
172
173 static int quickstart_acpi_ghid(struct quickstart_acpi *quickstart)
174 {
175         acpi_status status;
176         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
177         int ret = 0;
178
179         /*
180          * This returns a buffer telling the button usage ID,
181          * and triggers pending notify events (The ones before booting).
182          */
183         status = acpi_evaluate_object(quickstart->device->handle, "GHID", NULL,
184                                                                 &buffer);
185         if (ACPI_FAILURE(status)) {
186                 printk(KERN_ERR "quickstart: %s GHID method failed.\n",
187                                                 quickstart->btn->name);
188                 return -EINVAL;
189         }
190
191         /*
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).>>
195          */
196         switch (buffer.length) {
197         case 1:
198                 quickstart->btn->id = *(uint8_t *)buffer.pointer;
199                 break;
200         case 2:
201                 quickstart->btn->id = *(uint16_t *)buffer.pointer;
202                 break;
203         case 4:
204                 quickstart->btn->id = *(uint32_t *)buffer.pointer;
205                 break;
206         case 8:
207                 quickstart->btn->id = *(uint64_t *)buffer.pointer;
208                 break;
209         default:
210                 printk(KERN_ERR "quickstart: %s GHID method returned buffer "
211                                 "of unexpected length %u\n",
212                                 quickstart->btn->name, buffer.length);
213                 ret = -EINVAL;
214                 break;
215         }
216
217         kfree(buffer.pointer);
218
219         return ret;
220 }
221
222 static int quickstart_acpi_config(struct quickstart_acpi *quickstart)
223 {
224         char *bid = acpi_device_bid(quickstart->device);
225         char *name;
226
227         name = kmalloc(strlen(bid) + 1, GFP_KERNEL);
228         if (!name)
229                 return -ENOMEM;
230
231         /* Add new button to list */
232         quickstart->btn = quickstart_buttons_add();
233         if (!quickstart->btn) {
234                 kfree(name);
235                 return -ENOMEM;
236         }
237
238         quickstart->btn->name = name;
239         strcpy(quickstart->btn->name, bid);
240
241         return 0;
242 }
243
244 static int quickstart_acpi_add(struct acpi_device *device)
245 {
246         int ret;
247         acpi_status status;
248         struct quickstart_acpi *quickstart;
249
250         if (!device)
251                 return -EINVAL;
252
253         quickstart = kzalloc(sizeof(*quickstart), GFP_KERNEL);
254         if (!quickstart)
255                 return -ENOMEM;
256
257         quickstart->device = device;
258
259         strcpy(acpi_device_name(device), QUICKSTART_ACPI_DEVICE_NAME);
260         strcpy(acpi_device_class(device), QUICKSTART_ACPI_CLASS);
261         device->driver_data = quickstart;
262
263         /* Add button to list and initialize some stuff */
264         ret = quickstart_acpi_config(quickstart);
265         if (ret < 0)
266                 goto fail_config;
267
268         status = acpi_install_notify_handler(device->handle, ACPI_ALL_NOTIFY,
269                                                 quickstart_acpi_notify,
270                                                 quickstart);
271         if (ACPI_FAILURE(status)) {
272                 printk(KERN_ERR "quickstart: Notify handler install error\n");
273                 ret = -ENODEV;
274                 goto fail_installnotify;
275         }
276
277         ret = quickstart_acpi_ghid(quickstart);
278         if (ret < 0)
279                 goto fail_ghid;
280
281         return 0;
282
283 fail_ghid:
284         acpi_remove_notify_handler(device->handle, ACPI_ALL_NOTIFY,
285                                                 quickstart_acpi_notify);
286
287 fail_installnotify:
288         quickstart_button_del(quickstart->btn);
289
290 fail_config:
291
292         kfree(quickstart);
293
294         return ret;
295 }
296
297 static int quickstart_acpi_remove(struct acpi_device *device, int type)
298 {
299         acpi_status status;
300         struct quickstart_acpi *quickstart;
301
302         if (!device)
303                 return -EINVAL;
304
305         quickstart = acpi_driver_data(device);
306         if (!quickstart)
307                 return -EINVAL;
308
309         status = acpi_remove_notify_handler(device->handle, ACPI_ALL_NOTIFY,
310                                                 quickstart_acpi_notify);
311         if (ACPI_FAILURE(status))
312                 printk(KERN_ERR "quickstart: Error removing notify handler\n");
313
314         kfree(quickstart);
315
316         return 0;
317 }
318
319 /* Platform driver structs */
320 static DEVICE_ATTR(pressed_button, 0666, quickstart_pressed_button_show,
321                                          quickstart_pressed_button_store);
322 static DEVICE_ATTR(buttons, 0444, quickstart_buttons_show, NULL);
323 static struct platform_device *pf_device;
324 static struct platform_driver pf_driver = {
325         .driver = {
326                 .name = QUICKSTART_PF_DRIVER_NAME,
327                 .owner = THIS_MODULE,
328         }
329 };
330
331 static const struct acpi_device_id quickstart_device_ids[] = {
332         {QUICKSTART_ACPI_HID, 0},
333         {"", 0},
334 };
335
336 static struct acpi_driver quickstart_acpi_driver = {
337         .name = "quickstart",
338         .class = QUICKSTART_ACPI_CLASS,
339         .ids = quickstart_device_ids,
340         .ops = {
341                         .add = quickstart_acpi_add,
342                         .remove = quickstart_acpi_remove,
343                 },
344 };
345
346 /* Module functions */
347 static void quickstart_exit(void)
348 {
349         input_unregister_device(quickstart_input);
350
351         device_remove_file(&pf_device->dev, &dev_attr_pressed_button);
352         device_remove_file(&pf_device->dev, &dev_attr_buttons);
353
354         platform_device_unregister(pf_device);
355
356         platform_driver_unregister(&pf_driver);
357
358         acpi_bus_unregister_driver(&quickstart_acpi_driver);
359
360         quickstart_buttons_free();
361 }
362
363 static int __init quickstart_init_input(void)
364 {
365         struct quickstart_btn *b;
366         int ret;
367
368         quickstart_input = input_allocate_device();
369
370         if (!quickstart_input)
371                 return -ENOMEM;
372
373         quickstart_input->name = "Quickstart ACPI Buttons";
374         quickstart_input->id.bustype = BUS_HOST;
375
376         list_for_each_entry(b, &buttons, list) {
377                 set_bit(EV_KEY, quickstart_input->evbit);
378                 set_bit(b->id, quickstart_input->keybit);
379         }
380
381         ret = input_register_device(quickstart_input);
382         if (ret) {
383                 input_free_device(quickstart_input);
384                 return ret;
385         }
386
387         return 0;
388 }
389
390 static int __init quickstart_init(void)
391 {
392         int ret;
393
394         /* ACPI Check */
395         if (acpi_disabled)
396                 return -ENODEV;
397
398         /* ACPI driver register */
399         ret = acpi_bus_register_driver(&quickstart_acpi_driver);
400         if (ret)
401                 return ret;
402
403         /* If existing bus with no devices */
404         if (list_empty(&buttons)) {
405                 ret = -ENODEV;
406                 goto fail_pfdrv_reg;
407         }
408
409         /* Platform driver register */
410         ret = platform_driver_register(&pf_driver);
411         if (ret)
412                 goto fail_pfdrv_reg;
413
414         /* Platform device register */
415         pf_device = platform_device_alloc(QUICKSTART_PF_DEVICE_NAME, -1);
416         if (!pf_device) {
417                 ret = -ENOMEM;
418                 goto fail_pfdev_alloc;
419         }
420         ret = platform_device_add(pf_device);
421         if (ret)
422                 goto fail_pfdev_add;
423
424         /* Create device sysfs file */
425         ret = device_create_file(&pf_device->dev, &dev_attr_pressed_button);
426         if (ret)
427                 goto fail_dev_file;
428
429         ret = device_create_file(&pf_device->dev, &dev_attr_buttons);
430         if (ret)
431                 goto fail_dev_file2;
432
433         /* Input device */
434         ret = quickstart_init_input();
435         if (ret)
436                 goto fail_input;
437
438         printk(KERN_INFO "quickstart: ACPI Direct App Launch ver %s\n",
439                                                         QUICKSTART_VERSION);
440
441         return 0;
442 fail_input:
443         device_remove_file(&pf_device->dev, &dev_attr_buttons);
444
445 fail_dev_file2:
446         device_remove_file(&pf_device->dev, &dev_attr_pressed_button);
447
448 fail_dev_file:
449         platform_device_del(pf_device);
450
451 fail_pfdev_add:
452         platform_device_put(pf_device);
453
454 fail_pfdev_alloc:
455         platform_driver_unregister(&pf_driver);
456
457 fail_pfdrv_reg:
458         acpi_bus_unregister_driver(&quickstart_acpi_driver);
459
460         return ret;
461 }
462
463 module_init(quickstart_init);
464 module_exit(quickstart_exit);