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