ACPI: button: use Linux style for getting driver_data
[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 (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         seq_printf(seq, "type:                    %s\n",
132                    acpi_device_name(button->device));
133         return 0;
134 }
135
136 static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
137 {
138         return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
139 }
140
141 static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
142 {
143         struct acpi_button *button = seq->private;
144         acpi_status status;
145         unsigned long long state;
146
147         status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
148         seq_printf(seq, "state:      %s\n",
149                    ACPI_FAILURE(status) ? "unsupported" :
150                         (state ? "open" : "closed"));
151         return 0;
152 }
153
154 static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
155 {
156         return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
157 }
158
159 static struct proc_dir_entry *acpi_power_dir;
160 static struct proc_dir_entry *acpi_sleep_dir;
161 static struct proc_dir_entry *acpi_lid_dir;
162
163 static int acpi_button_add_fs(struct acpi_device *device)
164 {
165         struct acpi_button *button = acpi_driver_data(device);
166         struct proc_dir_entry *entry = NULL;
167
168         switch (button->type) {
169         case ACPI_BUTTON_TYPE_POWER:
170         case ACPI_BUTTON_TYPE_POWERF:
171                 if (!acpi_power_dir)
172                         acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
173                                                     acpi_button_dir);
174                 entry = acpi_power_dir;
175                 break;
176         case ACPI_BUTTON_TYPE_SLEEP:
177         case ACPI_BUTTON_TYPE_SLEEPF:
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,
202                                  acpi_driver_data(device));
203         if (!entry)
204                 return -ENODEV;
205
206         /* show lid state [R] */
207         if (button->type == ACPI_BUTTON_TYPE_LID) {
208                 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
209                                          S_IRUGO, acpi_device_dir(device),
210                                          &acpi_button_state_fops,
211                                          acpi_driver_data(device));
212                 if (!entry)
213                         return -ENODEV;
214         }
215
216         return 0;
217 }
218
219 static int acpi_button_remove_fs(struct acpi_device *device)
220 {
221         struct acpi_button *button = acpi_driver_data(device);
222
223         if (acpi_device_dir(device)) {
224                 if (button->type == ACPI_BUTTON_TYPE_LID)
225                         remove_proc_entry(ACPI_BUTTON_FILE_STATE,
226                                           acpi_device_dir(device));
227                 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
228                                   acpi_device_dir(device));
229
230                 remove_proc_entry(acpi_device_bid(device),
231                                   acpi_device_dir(device)->parent);
232                 acpi_device_dir(device) = NULL;
233         }
234
235         return 0;
236 }
237
238 /* --------------------------------------------------------------------------
239                                 Driver Interface
240    -------------------------------------------------------------------------- */
241 static int acpi_lid_send_state(struct acpi_button *button)
242 {
243         unsigned long long state;
244         acpi_status status;
245
246         status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
247                                         &state);
248         if (ACPI_FAILURE(status))
249                 return -ENODEV;
250
251         /* input layer checks if event is redundant */
252         input_report_switch(button->input, SW_LID, !state);
253         input_sync(button->input);
254         return 0;
255 }
256
257 static void acpi_button_notify(struct acpi_device *device, u32 event)
258 {
259         struct acpi_button *button = acpi_driver_data(device);
260         struct input_dev *input;
261
262         switch (event) {
263         case ACPI_FIXED_HARDWARE_EVENT:
264                 event = ACPI_BUTTON_NOTIFY_STATUS;
265                 /* fall through */
266         case ACPI_BUTTON_NOTIFY_STATUS:
267                 input = button->input;
268                 if (button->type == ACPI_BUTTON_TYPE_LID) {
269                         acpi_lid_send_state(button);
270                 } else {
271                         int keycode = test_bit(KEY_SLEEP, input->keybit) ?
272                                                 KEY_SLEEP : KEY_POWER;
273
274                         input_report_key(input, keycode, 1);
275                         input_sync(input);
276                         input_report_key(input, keycode, 0);
277                         input_sync(input);
278                 }
279
280                 acpi_bus_generate_proc_event(button->device, event,
281                                         ++button->pushed);
282                 break;
283         default:
284                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
285                                   "Unsupported event [0x%x]\n", event));
286                 break;
287         }
288 }
289
290 static int acpi_button_resume(struct acpi_device *device)
291 {
292         struct acpi_button *button = acpi_driver_data(device);
293
294         if (button->type == ACPI_BUTTON_TYPE_LID)
295                 return acpi_lid_send_state(button);
296         return 0;
297 }
298
299 static int acpi_button_add(struct acpi_device *device)
300 {
301         struct acpi_button *button;
302         struct input_dev *input;
303         int error;
304
305         button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
306         if (!button)
307                 return -ENOMEM;
308
309         button->device = device;
310         device->driver_data = button;
311
312         button->input = input = input_allocate_device();
313         if (!input) {
314                 error = -ENOMEM;
315                 goto err_free_button;
316         }
317
318         /*
319          * Determine the button type (via hid), as fixed-feature buttons
320          * need to be handled a bit differently than generic-space.
321          */
322         if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
323                 button->type = ACPI_BUTTON_TYPE_POWER;
324                 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
325                 sprintf(acpi_device_class(device), "%s/%s",
326                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
327         } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
328                 button->type = ACPI_BUTTON_TYPE_POWERF;
329                 strcpy(acpi_device_name(device),
330                        ACPI_BUTTON_DEVICE_NAME_POWERF);
331                 sprintf(acpi_device_class(device), "%s/%s",
332                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
333         } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
334                 button->type = ACPI_BUTTON_TYPE_SLEEP;
335                 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
336                 sprintf(acpi_device_class(device), "%s/%s",
337                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
338         } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
339                 button->type = ACPI_BUTTON_TYPE_SLEEPF;
340                 strcpy(acpi_device_name(device),
341                        ACPI_BUTTON_DEVICE_NAME_SLEEPF);
342                 sprintf(acpi_device_class(device), "%s/%s",
343                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
344         } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
345                 button->type = ACPI_BUTTON_TYPE_LID;
346                 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
347                 sprintf(acpi_device_class(device), "%s/%s",
348                         ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
349         } else {
350                 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
351                             acpi_device_hid(device));
352                 error = -ENODEV;
353                 goto err_free_input;
354         }
355
356         error = acpi_button_add_fs(device);
357         if (error)
358                 goto err_free_input;
359
360         snprintf(button->phys, sizeof(button->phys),
361                  "%s/button/input0", acpi_device_hid(device));
362
363         input->name = acpi_device_name(device);
364         input->phys = button->phys;
365         input->id.bustype = BUS_HOST;
366         input->id.product = button->type;
367         input->dev.parent = &device->dev;
368
369         switch (button->type) {
370         case ACPI_BUTTON_TYPE_POWER:
371         case ACPI_BUTTON_TYPE_POWERF:
372                 input->evbit[0] = BIT_MASK(EV_KEY);
373                 set_bit(KEY_POWER, input->keybit);
374                 break;
375
376         case ACPI_BUTTON_TYPE_SLEEP:
377         case ACPI_BUTTON_TYPE_SLEEPF:
378                 input->evbit[0] = BIT_MASK(EV_KEY);
379                 set_bit(KEY_SLEEP, input->keybit);
380                 break;
381
382         case ACPI_BUTTON_TYPE_LID:
383                 input->evbit[0] = BIT_MASK(EV_SW);
384                 set_bit(SW_LID, input->swbit);
385                 break;
386         }
387
388         error = input_register_device(input);
389         if (error)
390                 goto err_remove_fs;
391         if (button->type == ACPI_BUTTON_TYPE_LID)
392                 acpi_lid_send_state(button);
393
394         if (device->wakeup.flags.valid) {
395                 /* Button's GPE is run-wake GPE */
396                 acpi_set_gpe_type(device->wakeup.gpe_device,
397                                   device->wakeup.gpe_number,
398                                   ACPI_GPE_TYPE_WAKE_RUN);
399                 acpi_enable_gpe(device->wakeup.gpe_device,
400                                 device->wakeup.gpe_number);
401                 device->wakeup.state.enabled = 1;
402         }
403
404         printk(KERN_INFO PREFIX "%s [%s]\n",
405                acpi_device_name(device), acpi_device_bid(device));
406         return 0;
407
408  err_remove_fs:
409         acpi_button_remove_fs(device);
410  err_free_input:
411         input_free_device(input);
412  err_free_button:
413         kfree(button);
414         return error;
415 }
416
417 static int acpi_button_remove(struct acpi_device *device, int type)
418 {
419         struct acpi_button *button = acpi_driver_data(device);
420
421         acpi_button_remove_fs(device);
422         input_unregister_device(button->input);
423         kfree(button);
424         return 0;
425 }
426
427 static int __init acpi_button_init(void)
428 {
429         int result;
430
431         acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
432         if (!acpi_button_dir)
433                 return -ENODEV;
434
435         result = acpi_bus_register_driver(&acpi_button_driver);
436         if (result < 0) {
437                 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
438                 return -ENODEV;
439         }
440
441         return 0;
442 }
443
444 static void __exit acpi_button_exit(void)
445 {
446         acpi_bus_unregister_driver(&acpi_button_driver);
447
448         if (acpi_power_dir)
449                 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
450         if (acpi_sleep_dir)
451                 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
452         if (acpi_lid_dir)
453                 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
454         remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
455 }
456
457 module_init(acpi_button_init);
458 module_exit(acpi_button_exit);