ACPI / Button: Avoid disabling wakeup unnecessarily on remove
[pandora-kernel.git] / drivers / acpi / button.c
1 /*
2  *  button.c - ACPI Button Driver
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/input.h>
33 #include <linux/slab.h>
34 #include <acpi/acpi_bus.h>
35 #include <acpi/acpi_drivers.h>
36
37 #define PREFIX "ACPI: "
38
39 #define ACPI_BUTTON_CLASS               "button"
40 #define ACPI_BUTTON_FILE_INFO           "info"
41 #define ACPI_BUTTON_FILE_STATE          "state"
42 #define ACPI_BUTTON_TYPE_UNKNOWN        0x00
43 #define ACPI_BUTTON_NOTIFY_STATUS       0x80
44
45 #define ACPI_BUTTON_SUBCLASS_POWER      "power"
46 #define ACPI_BUTTON_HID_POWER           "PNP0C0C"
47 #define ACPI_BUTTON_DEVICE_NAME_POWER   "Power Button"
48 #define ACPI_BUTTON_TYPE_POWER          0x01
49
50 #define ACPI_BUTTON_SUBCLASS_SLEEP      "sleep"
51 #define ACPI_BUTTON_HID_SLEEP           "PNP0C0E"
52 #define ACPI_BUTTON_DEVICE_NAME_SLEEP   "Sleep Button"
53 #define ACPI_BUTTON_TYPE_SLEEP          0x03
54
55 #define ACPI_BUTTON_SUBCLASS_LID        "lid"
56 #define ACPI_BUTTON_HID_LID             "PNP0C0D"
57 #define ACPI_BUTTON_DEVICE_NAME_LID     "Lid Switch"
58 #define ACPI_BUTTON_TYPE_LID            0x05
59
60 #define _COMPONENT              ACPI_BUTTON_COMPONENT
61 ACPI_MODULE_NAME("button");
62
63 MODULE_AUTHOR("Paul Diefenbaugh");
64 MODULE_DESCRIPTION("ACPI Button Driver");
65 MODULE_LICENSE("GPL");
66
67 static const struct acpi_device_id button_device_ids[] = {
68         {ACPI_BUTTON_HID_LID,    0},
69         {ACPI_BUTTON_HID_SLEEP,  0},
70         {ACPI_BUTTON_HID_SLEEPF, 0},
71         {ACPI_BUTTON_HID_POWER,  0},
72         {ACPI_BUTTON_HID_POWERF, 0},
73         {"", 0},
74 };
75 MODULE_DEVICE_TABLE(acpi, button_device_ids);
76
77 static int acpi_button_add(struct acpi_device *device);
78 static int acpi_button_remove(struct acpi_device *device, int type);
79 static int acpi_button_resume(struct acpi_device *device);
80 static void acpi_button_notify(struct acpi_device *device, u32 event);
81 static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
82 static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
83
84 static struct acpi_driver acpi_button_driver = {
85         .name = "button",
86         .class = ACPI_BUTTON_CLASS,
87         .ids = button_device_ids,
88         .ops = {
89                 .add = acpi_button_add,
90                 .resume = acpi_button_resume,
91                 .remove = acpi_button_remove,
92                 .notify = acpi_button_notify,
93         },
94 };
95
96 struct acpi_button {
97         unsigned int type;
98         struct input_dev *input;
99         char phys[32];                  /* for input device */
100         unsigned long pushed;
101         bool wakeup_enabled;
102 };
103
104 static const struct file_operations acpi_button_info_fops = {
105         .owner = THIS_MODULE,
106         .open = acpi_button_info_open_fs,
107         .read = seq_read,
108         .llseek = seq_lseek,
109         .release = single_release,
110 };
111
112 static const struct file_operations acpi_button_state_fops = {
113         .owner = THIS_MODULE,
114         .open = acpi_button_state_open_fs,
115         .read = seq_read,
116         .llseek = seq_lseek,
117         .release = single_release,
118 };
119
120 static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
121 static struct acpi_device *lid_device;
122
123 /* --------------------------------------------------------------------------
124                               FS Interface (/proc)
125    -------------------------------------------------------------------------- */
126
127 static struct proc_dir_entry *acpi_button_dir;
128
129 static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
130 {
131         struct acpi_device *device = seq->private;
132
133         seq_printf(seq, "type:                    %s\n",
134                    acpi_device_name(device));
135         return 0;
136 }
137
138 static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
139 {
140         return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
141 }
142
143 static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
144 {
145         struct acpi_device *device = seq->private;
146         acpi_status status;
147         unsigned long long state;
148
149         status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
150         seq_printf(seq, "state:      %s\n",
151                    ACPI_FAILURE(status) ? "unsupported" :
152                         (state ? "open" : "closed"));
153         return 0;
154 }
155
156 static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
157 {
158         return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
159 }
160
161 static struct proc_dir_entry *acpi_power_dir;
162 static struct proc_dir_entry *acpi_sleep_dir;
163 static struct proc_dir_entry *acpi_lid_dir;
164
165 static int acpi_button_add_fs(struct acpi_device *device)
166 {
167         struct acpi_button *button = acpi_driver_data(device);
168         struct proc_dir_entry *entry = NULL;
169
170         switch (button->type) {
171         case ACPI_BUTTON_TYPE_POWER:
172                 if (!acpi_power_dir)
173                         acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
174                                                     acpi_button_dir);
175                 entry = acpi_power_dir;
176                 break;
177         case ACPI_BUTTON_TYPE_SLEEP:
178                 if (!acpi_sleep_dir)
179                         acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
180                                                     acpi_button_dir);
181                 entry = acpi_sleep_dir;
182                 break;
183         case ACPI_BUTTON_TYPE_LID:
184                 if (!acpi_lid_dir)
185                         acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
186                                                   acpi_button_dir);
187                 entry = acpi_lid_dir;
188                 break;
189         }
190
191         if (!entry)
192                 return -ENODEV;
193
194         acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
195         if (!acpi_device_dir(device))
196                 return -ENODEV;
197
198         /* 'info' [R] */
199         entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
200                                  S_IRUGO, acpi_device_dir(device),
201                                  &acpi_button_info_fops, device);
202         if (!entry)
203                 return -ENODEV;
204
205         /* show lid state [R] */
206         if (button->type == ACPI_BUTTON_TYPE_LID) {
207                 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
208                                          S_IRUGO, acpi_device_dir(device),
209                                          &acpi_button_state_fops, device);
210                 if (!entry)
211                         return -ENODEV;
212         }
213
214         return 0;
215 }
216
217 static int acpi_button_remove_fs(struct acpi_device *device)
218 {
219         struct acpi_button *button = acpi_driver_data(device);
220
221         if (acpi_device_dir(device)) {
222                 if (button->type == ACPI_BUTTON_TYPE_LID)
223                         remove_proc_entry(ACPI_BUTTON_FILE_STATE,
224                                           acpi_device_dir(device));
225                 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
226                                   acpi_device_dir(device));
227
228                 remove_proc_entry(acpi_device_bid(device),
229                                   acpi_device_dir(device)->parent);
230                 acpi_device_dir(device) = NULL;
231         }
232
233         return 0;
234 }
235
236 /* --------------------------------------------------------------------------
237                                 Driver Interface
238    -------------------------------------------------------------------------- */
239 int acpi_lid_notifier_register(struct notifier_block *nb)
240 {
241         return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
242 }
243 EXPORT_SYMBOL(acpi_lid_notifier_register);
244
245 int acpi_lid_notifier_unregister(struct notifier_block *nb)
246 {
247         return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
248 }
249 EXPORT_SYMBOL(acpi_lid_notifier_unregister);
250
251 int acpi_lid_open(void)
252 {
253         acpi_status status;
254         unsigned long long state;
255
256         if (!lid_device)
257                 return -ENODEV;
258
259         status = acpi_evaluate_integer(lid_device->handle, "_LID", NULL,
260                                        &state);
261         if (ACPI_FAILURE(status))
262                 return -ENODEV;
263
264         return !!state;
265 }
266 EXPORT_SYMBOL(acpi_lid_open);
267
268 static int acpi_lid_send_state(struct acpi_device *device)
269 {
270         struct acpi_button *button = acpi_driver_data(device);
271         unsigned long long state;
272         acpi_status status;
273         int ret;
274
275         status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
276         if (ACPI_FAILURE(status))
277                 return -ENODEV;
278
279         /* input layer checks if event is redundant */
280         input_report_switch(button->input, SW_LID, !state);
281         input_sync(button->input);
282
283         if (state)
284                 pm_wakeup_event(&device->dev, 0);
285
286         ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
287         if (ret == NOTIFY_DONE)
288                 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
289                                                    device);
290         if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
291                 /*
292                  * It is also regarded as success if the notifier_chain
293                  * returns NOTIFY_OK or NOTIFY_DONE.
294                  */
295                 ret = 0;
296         }
297         return ret;
298 }
299
300 static void acpi_button_notify(struct acpi_device *device, u32 event)
301 {
302         struct acpi_button *button = acpi_driver_data(device);
303         struct input_dev *input;
304
305         switch (event) {
306         case ACPI_FIXED_HARDWARE_EVENT:
307                 event = ACPI_BUTTON_NOTIFY_STATUS;
308                 /* fall through */
309         case ACPI_BUTTON_NOTIFY_STATUS:
310                 input = button->input;
311                 if (button->type == ACPI_BUTTON_TYPE_LID) {
312                         acpi_lid_send_state(device);
313                 } else {
314                         int keycode = test_bit(KEY_SLEEP, input->keybit) ?
315                                                 KEY_SLEEP : KEY_POWER;
316
317                         input_report_key(input, keycode, 1);
318                         input_sync(input);
319                         input_report_key(input, keycode, 0);
320                         input_sync(input);
321
322                         pm_wakeup_event(&device->dev, 0);
323                 }
324
325                 acpi_bus_generate_proc_event(device, event, ++button->pushed);
326                 break;
327         default:
328                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
329                                   "Unsupported event [0x%x]\n", event));
330                 break;
331         }
332 }
333
334 static int acpi_button_resume(struct acpi_device *device)
335 {
336         struct acpi_button *button = acpi_driver_data(device);
337
338         if (button->type == ACPI_BUTTON_TYPE_LID)
339                 return acpi_lid_send_state(device);
340         return 0;
341 }
342
343 static int acpi_button_add(struct acpi_device *device)
344 {
345         struct acpi_button *button;
346         struct input_dev *input;
347         const char *hid = acpi_device_hid(device);
348         char *name, *class;
349         int error;
350
351         button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
352         if (!button)
353                 return -ENOMEM;
354
355         device->driver_data = button;
356
357         button->input = input = input_allocate_device();
358         if (!input) {
359                 error = -ENOMEM;
360                 goto err_free_button;
361         }
362
363         name = acpi_device_name(device);
364         class = acpi_device_class(device);
365
366         if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
367             !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
368                 button->type = ACPI_BUTTON_TYPE_POWER;
369                 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
370                 sprintf(class, "%s/%s",
371                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
372         } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
373                    !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
374                 button->type = ACPI_BUTTON_TYPE_SLEEP;
375                 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
376                 sprintf(class, "%s/%s",
377                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
378         } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
379                 button->type = ACPI_BUTTON_TYPE_LID;
380                 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
381                 sprintf(class, "%s/%s",
382                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
383         } else {
384                 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
385                 error = -ENODEV;
386                 goto err_free_input;
387         }
388
389         error = acpi_button_add_fs(device);
390         if (error)
391                 goto err_free_input;
392
393         snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
394
395         input->name = name;
396         input->phys = button->phys;
397         input->id.bustype = BUS_HOST;
398         input->id.product = button->type;
399         input->dev.parent = &device->dev;
400
401         switch (button->type) {
402         case ACPI_BUTTON_TYPE_POWER:
403                 input->evbit[0] = BIT_MASK(EV_KEY);
404                 set_bit(KEY_POWER, input->keybit);
405                 break;
406
407         case ACPI_BUTTON_TYPE_SLEEP:
408                 input->evbit[0] = BIT_MASK(EV_KEY);
409                 set_bit(KEY_SLEEP, input->keybit);
410                 break;
411
412         case ACPI_BUTTON_TYPE_LID:
413                 input->evbit[0] = BIT_MASK(EV_SW);
414                 set_bit(SW_LID, input->swbit);
415                 break;
416         }
417
418         error = input_register_device(input);
419         if (error)
420                 goto err_remove_fs;
421         if (button->type == ACPI_BUTTON_TYPE_LID) {
422                 acpi_lid_send_state(device);
423                 /*
424                  * This assumes there's only one lid device, or if there are
425                  * more we only care about the last one...
426                  */
427                 lid_device = device;
428         }
429
430         if (device->wakeup.flags.valid) {
431                 /* Button's GPE is run-wake GPE */
432                 acpi_enable_gpe(device->wakeup.gpe_device,
433                                 device->wakeup.gpe_number);
434                 if (!device_may_wakeup(&device->dev)) {
435                         device_set_wakeup_enable(&device->dev, true);
436                         button->wakeup_enabled = true;
437                 }
438         }
439
440         printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
441         return 0;
442
443  err_remove_fs:
444         acpi_button_remove_fs(device);
445  err_free_input:
446         input_free_device(input);
447  err_free_button:
448         kfree(button);
449         return error;
450 }
451
452 static int acpi_button_remove(struct acpi_device *device, int type)
453 {
454         struct acpi_button *button = acpi_driver_data(device);
455
456         if (device->wakeup.flags.valid) {
457                 acpi_disable_gpe(device->wakeup.gpe_device,
458                                 device->wakeup.gpe_number);
459                 if (button->wakeup_enabled)
460                         device_set_wakeup_enable(&device->dev, false);
461         }
462
463         acpi_button_remove_fs(device);
464         input_unregister_device(button->input);
465         kfree(button);
466         return 0;
467 }
468
469 static int __init acpi_button_init(void)
470 {
471         int result;
472
473         acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
474         if (!acpi_button_dir)
475                 return -ENODEV;
476
477         result = acpi_bus_register_driver(&acpi_button_driver);
478         if (result < 0) {
479                 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
480                 return -ENODEV;
481         }
482
483         return 0;
484 }
485
486 static void __exit acpi_button_exit(void)
487 {
488         acpi_bus_unregister_driver(&acpi_button_driver);
489
490         if (acpi_power_dir)
491                 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
492         if (acpi_sleep_dir)
493                 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
494         if (acpi_lid_dir)
495                 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
496         remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
497 }
498
499 module_init(acpi_button_init);
500 module_exit(acpi_button_exit);