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