ACPI: button: remove button->device pointer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / button.c
blobc8441627f68e6906b6c37a56c80b68499edcd88d
1 /*
2 * button.c - ACPI Button Driver
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
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
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
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
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
61 #define _COMPONENT ACPI_BUTTON_COMPONENT
62 ACPI_MODULE_NAME("button");
64 MODULE_AUTHOR("Paul Diefenbaugh");
65 MODULE_DESCRIPTION("ACPI Button Driver");
66 MODULE_LICENSE("GPL");
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},
76 MODULE_DEVICE_TABLE(acpi, button_device_ids);
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);
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,
97 struct acpi_button {
98 unsigned int type;
99 struct input_dev *input;
100 char phys[32]; /* for input device */
101 unsigned long pushed;
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,
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,
120 /* --------------------------------------------------------------------------
121 FS Interface (/proc)
122 -------------------------------------------------------------------------- */
124 static struct proc_dir_entry *acpi_button_dir;
126 static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
128 struct acpi_device *device = seq->private;
130 seq_printf(seq, "type: %s\n",
131 acpi_device_name(device));
132 return 0;
135 static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
137 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
140 static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
142 struct acpi_device *device = seq->private;
143 acpi_status status;
144 unsigned long long state;
146 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
147 seq_printf(seq, "state: %s\n",
148 ACPI_FAILURE(status) ? "unsupported" :
149 (state ? "open" : "closed"));
150 return 0;
153 static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
155 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
158 static struct proc_dir_entry *acpi_power_dir;
159 static struct proc_dir_entry *acpi_sleep_dir;
160 static struct proc_dir_entry *acpi_lid_dir;
162 static int acpi_button_add_fs(struct acpi_device *device)
164 struct acpi_button *button = acpi_driver_data(device);
165 struct proc_dir_entry *entry = NULL;
167 switch (button->type) {
168 case ACPI_BUTTON_TYPE_POWER:
169 case ACPI_BUTTON_TYPE_POWERF:
170 if (!acpi_power_dir)
171 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
172 acpi_button_dir);
173 entry = acpi_power_dir;
174 break;
175 case ACPI_BUTTON_TYPE_SLEEP:
176 case ACPI_BUTTON_TYPE_SLEEPF:
177 if (!acpi_sleep_dir)
178 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
179 acpi_button_dir);
180 entry = acpi_sleep_dir;
181 break;
182 case ACPI_BUTTON_TYPE_LID:
183 if (!acpi_lid_dir)
184 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
185 acpi_button_dir);
186 entry = acpi_lid_dir;
187 break;
190 if (!entry)
191 return -ENODEV;
193 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
194 if (!acpi_device_dir(device))
195 return -ENODEV;
197 /* 'info' [R] */
198 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
199 S_IRUGO, acpi_device_dir(device),
200 &acpi_button_info_fops, device);
201 if (!entry)
202 return -ENODEV;
204 /* show lid state [R] */
205 if (button->type == ACPI_BUTTON_TYPE_LID) {
206 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
207 S_IRUGO, acpi_device_dir(device),
208 &acpi_button_state_fops, device);
209 if (!entry)
210 return -ENODEV;
213 return 0;
216 static int acpi_button_remove_fs(struct acpi_device *device)
218 struct acpi_button *button = acpi_driver_data(device);
220 if (acpi_device_dir(device)) {
221 if (button->type == ACPI_BUTTON_TYPE_LID)
222 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
223 acpi_device_dir(device));
224 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
225 acpi_device_dir(device));
227 remove_proc_entry(acpi_device_bid(device),
228 acpi_device_dir(device)->parent);
229 acpi_device_dir(device) = NULL;
232 return 0;
235 /* --------------------------------------------------------------------------
236 Driver Interface
237 -------------------------------------------------------------------------- */
238 static int acpi_lid_send_state(struct acpi_device *device)
240 struct acpi_button *button = acpi_driver_data(device);
241 unsigned long long state;
242 acpi_status status;
244 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
245 if (ACPI_FAILURE(status))
246 return -ENODEV;
248 /* input layer checks if event is redundant */
249 input_report_switch(button->input, SW_LID, !state);
250 input_sync(button->input);
251 return 0;
254 static void acpi_button_notify(struct acpi_device *device, u32 event)
256 struct acpi_button *button = acpi_driver_data(device);
257 struct input_dev *input;
259 switch (event) {
260 case ACPI_FIXED_HARDWARE_EVENT:
261 event = ACPI_BUTTON_NOTIFY_STATUS;
262 /* fall through */
263 case ACPI_BUTTON_NOTIFY_STATUS:
264 input = button->input;
265 if (button->type == ACPI_BUTTON_TYPE_LID) {
266 acpi_lid_send_state(device);
267 } else {
268 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
269 KEY_SLEEP : KEY_POWER;
271 input_report_key(input, keycode, 1);
272 input_sync(input);
273 input_report_key(input, keycode, 0);
274 input_sync(input);
277 acpi_bus_generate_proc_event(device, event, ++button->pushed);
278 break;
279 default:
280 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
281 "Unsupported event [0x%x]\n", event));
282 break;
286 static int acpi_button_resume(struct acpi_device *device)
288 struct acpi_button *button = acpi_driver_data(device);
290 if (button->type == ACPI_BUTTON_TYPE_LID)
291 return acpi_lid_send_state(device);
292 return 0;
295 static int acpi_button_add(struct acpi_device *device)
297 struct acpi_button *button;
298 struct input_dev *input;
299 char *hid, *name, *class;
300 int error;
302 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
303 if (!button)
304 return -ENOMEM;
306 device->driver_data = button;
308 button->input = input = input_allocate_device();
309 if (!input) {
310 error = -ENOMEM;
311 goto err_free_button;
314 hid = acpi_device_hid(device);
315 name = acpi_device_name(device);
316 class = acpi_device_class(device);
319 * Determine the button type (via hid), as fixed-feature buttons
320 * need to be handled a bit differently than generic-space.
322 if (!strcmp(hid, ACPI_BUTTON_HID_POWER)) {
323 button->type = ACPI_BUTTON_TYPE_POWER;
324 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
325 sprintf(class, "%s/%s",
326 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
327 } else if (!strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
328 button->type = ACPI_BUTTON_TYPE_POWERF;
329 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWERF);
330 sprintf(class, "%s/%s",
331 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
332 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP)) {
333 button->type = ACPI_BUTTON_TYPE_SLEEP;
334 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
335 sprintf(class, "%s/%s",
336 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
337 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
338 button->type = ACPI_BUTTON_TYPE_SLEEPF;
339 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEPF);
340 sprintf(class, "%s/%s",
341 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
342 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
343 button->type = ACPI_BUTTON_TYPE_LID;
344 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
345 sprintf(class, "%s/%s",
346 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
347 } else {
348 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
349 error = -ENODEV;
350 goto err_free_input;
353 error = acpi_button_add_fs(device);
354 if (error)
355 goto err_free_input;
357 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
359 input->name = name;
360 input->phys = button->phys;
361 input->id.bustype = BUS_HOST;
362 input->id.product = button->type;
363 input->dev.parent = &device->dev;
365 switch (button->type) {
366 case ACPI_BUTTON_TYPE_POWER:
367 case ACPI_BUTTON_TYPE_POWERF:
368 input->evbit[0] = BIT_MASK(EV_KEY);
369 set_bit(KEY_POWER, input->keybit);
370 break;
372 case ACPI_BUTTON_TYPE_SLEEP:
373 case ACPI_BUTTON_TYPE_SLEEPF:
374 input->evbit[0] = BIT_MASK(EV_KEY);
375 set_bit(KEY_SLEEP, input->keybit);
376 break;
378 case ACPI_BUTTON_TYPE_LID:
379 input->evbit[0] = BIT_MASK(EV_SW);
380 set_bit(SW_LID, input->swbit);
381 break;
384 error = input_register_device(input);
385 if (error)
386 goto err_remove_fs;
387 if (button->type == ACPI_BUTTON_TYPE_LID)
388 acpi_lid_send_state(device);
390 if (device->wakeup.flags.valid) {
391 /* Button's GPE is run-wake GPE */
392 acpi_set_gpe_type(device->wakeup.gpe_device,
393 device->wakeup.gpe_number,
394 ACPI_GPE_TYPE_WAKE_RUN);
395 acpi_enable_gpe(device->wakeup.gpe_device,
396 device->wakeup.gpe_number);
397 device->wakeup.state.enabled = 1;
400 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
401 return 0;
403 err_remove_fs:
404 acpi_button_remove_fs(device);
405 err_free_input:
406 input_free_device(input);
407 err_free_button:
408 kfree(button);
409 return error;
412 static int acpi_button_remove(struct acpi_device *device, int type)
414 struct acpi_button *button = acpi_driver_data(device);
416 acpi_button_remove_fs(device);
417 input_unregister_device(button->input);
418 kfree(button);
419 return 0;
422 static int __init acpi_button_init(void)
424 int result;
426 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
427 if (!acpi_button_dir)
428 return -ENODEV;
430 result = acpi_bus_register_driver(&acpi_button_driver);
431 if (result < 0) {
432 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
433 return -ENODEV;
436 return 0;
439 static void __exit acpi_button_exit(void)
441 acpi_bus_unregister_driver(&acpi_button_driver);
443 if (acpi_power_dir)
444 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
445 if (acpi_sleep_dir)
446 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
447 if (acpi_lid_dir)
448 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
449 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
452 module_init(acpi_button_init);
453 module_exit(acpi_button_exit);