Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[linux-2.6] / drivers / acpi / button.c
1 /*
2  *  acpi_button.c - ACPI Button Driver ($Revision: 30 $)
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 <acpi/acpi_bus.h>
34 #include <acpi/acpi_drivers.h>
35
36 #define ACPI_BUTTON_COMPONENT           0x00080000
37 #define ACPI_BUTTON_CLASS               "button"
38 #define ACPI_BUTTON_FILE_INFO           "info"
39 #define ACPI_BUTTON_FILE_STATE          "state"
40 #define ACPI_BUTTON_TYPE_UNKNOWN        0x00
41 #define ACPI_BUTTON_NOTIFY_STATUS       0x80
42
43 #define ACPI_BUTTON_SUBCLASS_POWER      "power"
44 #define ACPI_BUTTON_HID_POWER           "PNP0C0C"
45 #define ACPI_BUTTON_DEVICE_NAME_POWER   "Power Button (CM)"
46 #define ACPI_BUTTON_DEVICE_NAME_POWERF  "Power Button (FF)"
47 #define ACPI_BUTTON_TYPE_POWER          0x01
48 #define ACPI_BUTTON_TYPE_POWERF         0x02
49
50 #define ACPI_BUTTON_SUBCLASS_SLEEP      "sleep"
51 #define ACPI_BUTTON_HID_SLEEP           "PNP0C0E"
52 #define ACPI_BUTTON_DEVICE_NAME_SLEEP   "Sleep Button (CM)"
53 #define ACPI_BUTTON_DEVICE_NAME_SLEEPF  "Sleep Button (FF)"
54 #define ACPI_BUTTON_TYPE_SLEEP          0x03
55 #define ACPI_BUTTON_TYPE_SLEEPF         0x04
56
57 #define ACPI_BUTTON_SUBCLASS_LID        "lid"
58 #define ACPI_BUTTON_HID_LID             "PNP0C0D"
59 #define ACPI_BUTTON_DEVICE_NAME_LID     "Lid Switch"
60 #define ACPI_BUTTON_TYPE_LID            0x05
61
62 #define _COMPONENT              ACPI_BUTTON_COMPONENT
63 ACPI_MODULE_NAME("button");
64
65 MODULE_AUTHOR("Paul Diefenbaugh");
66 MODULE_DESCRIPTION("ACPI Button Driver");
67 MODULE_LICENSE("GPL");
68
69 static const struct acpi_device_id button_device_ids[] = {
70         {ACPI_BUTTON_HID_LID,    0},
71         {ACPI_BUTTON_HID_SLEEP,  0},
72         {ACPI_BUTTON_HID_SLEEPF, 0},
73         {ACPI_BUTTON_HID_POWER,  0},
74         {ACPI_BUTTON_HID_POWERF, 0},
75         {"", 0},
76 };
77 MODULE_DEVICE_TABLE(acpi, button_device_ids);
78
79 static int acpi_button_add(struct acpi_device *device);
80 static int acpi_button_remove(struct acpi_device *device, int type);
81 static int acpi_button_resume(struct acpi_device *device);
82 static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
83 static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
84
85 static struct acpi_driver acpi_button_driver = {
86         .name = "button",
87         .class = ACPI_BUTTON_CLASS,
88         .ids = button_device_ids,
89         .ops = {
90                 .add = acpi_button_add,
91                 .resume = acpi_button_resume,
92                 .remove = acpi_button_remove,
93         },
94 };
95
96 struct acpi_button {
97         struct acpi_device *device;     /* Fixed button kludge */
98         unsigned int type;
99         struct input_dev *input;
100         char phys[32];                  /* for input device */
101         unsigned long pushed;
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 /* --------------------------------------------------------------------------
121                               FS Interface (/proc)
122    -------------------------------------------------------------------------- */
123
124 static struct proc_dir_entry *acpi_button_dir;
125
126 static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
127 {
128         struct acpi_button *button = seq->private;
129
130         if (!button || !button->device)
131                 return 0;
132
133         seq_printf(seq, "type:                    %s\n",
134                    acpi_device_name(button->device));
135
136         return 0;
137 }
138
139 static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
140 {
141         return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
142 }
143
144 static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
145 {
146         struct acpi_button *button = seq->private;
147         acpi_status status;
148         unsigned long state;
149
150         if (!button || !button->device)
151                 return 0;
152
153         status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
154         seq_printf(seq, "state:      %s\n",
155                    ACPI_FAILURE(status) ? "unsupported" :
156                         (state ? "open" : "closed"));
157         return 0;
158 }
159
160 static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
161 {
162         return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
163 }
164
165 static struct proc_dir_entry *acpi_power_dir;
166 static struct proc_dir_entry *acpi_sleep_dir;
167 static struct proc_dir_entry *acpi_lid_dir;
168
169 static int acpi_button_add_fs(struct acpi_device *device)
170 {
171         struct proc_dir_entry *entry = NULL;
172         struct acpi_button *button;
173
174         if (!device || !acpi_driver_data(device))
175                 return -EINVAL;
176
177         button = acpi_driver_data(device);
178
179         switch (button->type) {
180         case ACPI_BUTTON_TYPE_POWER:
181         case ACPI_BUTTON_TYPE_POWERF:
182                 if (!acpi_power_dir)
183                         acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
184                                                     acpi_button_dir);
185                 entry = acpi_power_dir;
186                 break;
187         case ACPI_BUTTON_TYPE_SLEEP:
188         case ACPI_BUTTON_TYPE_SLEEPF:
189                 if (!acpi_sleep_dir)
190                         acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
191                                                     acpi_button_dir);
192                 entry = acpi_sleep_dir;
193                 break;
194         case ACPI_BUTTON_TYPE_LID:
195                 if (!acpi_lid_dir)
196                         acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
197                                                   acpi_button_dir);
198                 entry = acpi_lid_dir;
199                 break;
200         }
201
202         if (!entry)
203                 return -ENODEV;
204         entry->owner = THIS_MODULE;
205
206         acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
207         if (!acpi_device_dir(device))
208                 return -ENODEV;
209         acpi_device_dir(device)->owner = THIS_MODULE;
210
211         /* 'info' [R] */
212         entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
213                                  S_IRUGO, acpi_device_dir(device),
214                                  &acpi_button_info_fops,
215                                  acpi_driver_data(device));
216         if (!entry)
217                 return -ENODEV;
218
219         /* show lid state [R] */
220         if (button->type == ACPI_BUTTON_TYPE_LID) {
221                 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
222                                          S_IRUGO, acpi_device_dir(device),
223                                          &acpi_button_state_fops,
224                                          acpi_driver_data(device));
225                 if (!entry)
226                         return -ENODEV;
227         }
228
229         return 0;
230 }
231
232 static int acpi_button_remove_fs(struct acpi_device *device)
233 {
234         struct acpi_button *button = acpi_driver_data(device);
235
236         if (acpi_device_dir(device)) {
237                 if (button->type == ACPI_BUTTON_TYPE_LID)
238                         remove_proc_entry(ACPI_BUTTON_FILE_STATE,
239                                           acpi_device_dir(device));
240                 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
241                                   acpi_device_dir(device));
242
243                 remove_proc_entry(acpi_device_bid(device),
244                                   acpi_device_dir(device)->parent);
245                 acpi_device_dir(device) = NULL;
246         }
247
248         return 0;
249 }
250
251 /* --------------------------------------------------------------------------
252                                 Driver Interface
253    -------------------------------------------------------------------------- */
254 static int acpi_lid_send_state(struct acpi_button *button)
255 {
256         unsigned long state;
257         acpi_status status;
258
259         status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
260                                         &state);
261         if (ACPI_FAILURE(status))
262                 return -ENODEV;
263         /* input layer checks if event is redundant */
264         input_report_switch(button->input, SW_LID, !state);
265         return 0;
266 }
267
268 static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
269 {
270         struct acpi_button *button = data;
271         struct input_dev *input;
272
273         if (!button || !button->device)
274                 return;
275
276         switch (event) {
277         case ACPI_BUTTON_NOTIFY_STATUS:
278                 input = button->input;
279                 if (button->type == ACPI_BUTTON_TYPE_LID) {
280                         acpi_lid_send_state(button);
281                 } else {
282                         int keycode = test_bit(KEY_SLEEP, input->keybit) ?
283                                                 KEY_SLEEP : KEY_POWER;
284
285                         input_report_key(input, keycode, 1);
286                         input_sync(input);
287                         input_report_key(input, keycode, 0);
288                 }
289                 input_sync(input);
290
291                 acpi_bus_generate_proc_event(button->device, event,
292                                         ++button->pushed);
293                 break;
294         default:
295                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
296                                   "Unsupported event [0x%x]\n", event));
297                 break;
298         }
299
300         return;
301 }
302
303 static acpi_status acpi_button_notify_fixed(void *data)
304 {
305         struct acpi_button *button = data;
306
307         if (!button)
308                 return AE_BAD_PARAMETER;
309
310         acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
311
312         return AE_OK;
313 }
314
315 static int acpi_button_install_notify_handlers(struct acpi_button *button)
316 {
317         acpi_status status;
318
319         switch (button->type) {
320         case ACPI_BUTTON_TYPE_POWERF:
321                 status =
322                     acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
323                                                      acpi_button_notify_fixed,
324                                                      button);
325                 break;
326         case ACPI_BUTTON_TYPE_SLEEPF:
327                 status =
328                     acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
329                                                      acpi_button_notify_fixed,
330                                                      button);
331                 break;
332         default:
333                 status = acpi_install_notify_handler(button->device->handle,
334                                                      ACPI_DEVICE_NOTIFY,
335                                                      acpi_button_notify,
336                                                      button);
337                 break;
338         }
339
340         return ACPI_FAILURE(status) ? -ENODEV : 0;
341 }
342
343 static int acpi_button_resume(struct acpi_device *device)
344 {
345         struct acpi_button *button;
346         if (!device)
347                 return -EINVAL;
348         button = acpi_driver_data(device);
349         if (button && button->type == ACPI_BUTTON_TYPE_LID)
350                 return acpi_lid_send_state(button);
351         return 0;
352 }
353
354 static void acpi_button_remove_notify_handlers(struct acpi_button *button)
355 {
356         switch (button->type) {
357         case ACPI_BUTTON_TYPE_POWERF:
358                 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
359                                                 acpi_button_notify_fixed);
360                 break;
361         case ACPI_BUTTON_TYPE_SLEEPF:
362                 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
363                                                 acpi_button_notify_fixed);
364                 break;
365         default:
366                 acpi_remove_notify_handler(button->device->handle,
367                                            ACPI_DEVICE_NOTIFY,
368                                            acpi_button_notify);
369                 break;
370         }
371 }
372
373 static int acpi_button_add(struct acpi_device *device)
374 {
375         int error;
376         struct acpi_button *button;
377         struct input_dev *input;
378
379         if (!device)
380                 return -EINVAL;
381
382         button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
383         if (!button)
384                 return -ENOMEM;
385
386         button->device = device;
387         acpi_driver_data(device) = button;
388
389         button->input = input = input_allocate_device();
390         if (!input) {
391                 error = -ENOMEM;
392                 goto err_free_button;
393         }
394
395         /*
396          * Determine the button type (via hid), as fixed-feature buttons
397          * need to be handled a bit differently than generic-space.
398          */
399         if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
400                 button->type = ACPI_BUTTON_TYPE_POWER;
401                 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
402                 sprintf(acpi_device_class(device), "%s/%s",
403                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
404         } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
405                 button->type = ACPI_BUTTON_TYPE_POWERF;
406                 strcpy(acpi_device_name(device),
407                        ACPI_BUTTON_DEVICE_NAME_POWERF);
408                 sprintf(acpi_device_class(device), "%s/%s",
409                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
410         } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
411                 button->type = ACPI_BUTTON_TYPE_SLEEP;
412                 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
413                 sprintf(acpi_device_class(device), "%s/%s",
414                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
415         } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
416                 button->type = ACPI_BUTTON_TYPE_SLEEPF;
417                 strcpy(acpi_device_name(device),
418                        ACPI_BUTTON_DEVICE_NAME_SLEEPF);
419                 sprintf(acpi_device_class(device), "%s/%s",
420                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
421         } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
422                 button->type = ACPI_BUTTON_TYPE_LID;
423                 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
424                 sprintf(acpi_device_class(device), "%s/%s",
425                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
426         } else {
427                 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
428                             acpi_device_hid(device));
429                 error = -ENODEV;
430                 goto err_free_input;
431         }
432
433         error = acpi_button_add_fs(device);
434         if (error)
435                 goto err_free_input;
436
437         error = acpi_button_install_notify_handlers(button);
438         if (error)
439                 goto err_remove_fs;
440
441         snprintf(button->phys, sizeof(button->phys),
442                  "%s/button/input0", acpi_device_hid(device));
443
444         input->name = acpi_device_name(device);
445         input->phys = button->phys;
446         input->id.bustype = BUS_HOST;
447         input->id.product = button->type;
448         input->dev.parent = &device->dev;
449
450         switch (button->type) {
451         case ACPI_BUTTON_TYPE_POWER:
452         case ACPI_BUTTON_TYPE_POWERF:
453                 input->evbit[0] = BIT_MASK(EV_KEY);
454                 set_bit(KEY_POWER, input->keybit);
455                 break;
456
457         case ACPI_BUTTON_TYPE_SLEEP:
458         case ACPI_BUTTON_TYPE_SLEEPF:
459                 input->evbit[0] = BIT_MASK(EV_KEY);
460                 set_bit(KEY_SLEEP, input->keybit);
461                 break;
462
463         case ACPI_BUTTON_TYPE_LID:
464                 input->evbit[0] = BIT_MASK(EV_SW);
465                 set_bit(SW_LID, input->swbit);
466                 break;
467         }
468
469         error = input_register_device(input);
470         if (error)
471                 goto err_remove_handlers;
472         if (button->type == ACPI_BUTTON_TYPE_LID)
473                 acpi_lid_send_state(button);
474
475         if (device->wakeup.flags.valid) {
476                 /* Button's GPE is run-wake GPE */
477                 acpi_set_gpe_type(device->wakeup.gpe_device,
478                                   device->wakeup.gpe_number,
479                                   ACPI_GPE_TYPE_WAKE_RUN);
480                 acpi_enable_gpe(device->wakeup.gpe_device,
481                                 device->wakeup.gpe_number, ACPI_NOT_ISR);
482                 device->wakeup.state.enabled = 1;
483         }
484
485         printk(KERN_INFO PREFIX "%s [%s]\n",
486                acpi_device_name(device), acpi_device_bid(device));
487
488         return 0;
489
490  err_remove_handlers:
491         acpi_button_remove_notify_handlers(button);
492  err_remove_fs:
493         acpi_button_remove_fs(device);
494  err_free_input:
495         input_free_device(input);
496  err_free_button:
497         kfree(button);
498         return error;
499 }
500
501 static int acpi_button_remove(struct acpi_device *device, int type)
502 {
503         struct acpi_button *button;
504
505         if (!device || !acpi_driver_data(device))
506                 return -EINVAL;
507
508         button = acpi_driver_data(device);
509
510         acpi_button_remove_notify_handlers(button);
511         acpi_button_remove_fs(device);
512         input_unregister_device(button->input);
513         kfree(button);
514
515         return 0;
516 }
517
518 static int __init acpi_button_init(void)
519 {
520         int result;
521
522         acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
523         if (!acpi_button_dir)
524                 return -ENODEV;
525         acpi_button_dir->owner = THIS_MODULE;
526         result = acpi_bus_register_driver(&acpi_button_driver);
527         if (result < 0) {
528                 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
529                 return -ENODEV;
530         }
531
532         return 0;
533 }
534
535 static void __exit acpi_button_exit(void)
536 {
537         acpi_bus_unregister_driver(&acpi_button_driver);
538
539         if (acpi_power_dir)
540                 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
541         if (acpi_sleep_dir)
542                 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
543         if (acpi_lid_dir)
544                 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
545         remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
546 }
547
548 module_init(acpi_button_init);
549 module_exit(acpi_button_exit);