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},
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
= {
87 .class = ACPI_BUTTON_CLASS
,
88 .ids
= button_device_ids
,
90 .add
= acpi_button_add
,
91 .resume
= acpi_button_resume
,
92 .remove
= acpi_button_remove
,
93 .notify
= acpi_button_notify
,
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
,
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
,
117 .release
= single_release
,
120 /* --------------------------------------------------------------------------
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
));
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;
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"));
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
:
171 acpi_power_dir
= proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER
,
173 entry
= acpi_power_dir
;
175 case ACPI_BUTTON_TYPE_SLEEP
:
176 case ACPI_BUTTON_TYPE_SLEEPF
:
178 acpi_sleep_dir
= proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP
,
180 entry
= acpi_sleep_dir
;
182 case ACPI_BUTTON_TYPE_LID
:
184 acpi_lid_dir
= proc_mkdir(ACPI_BUTTON_SUBCLASS_LID
,
186 entry
= acpi_lid_dir
;
193 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
), entry
);
194 if (!acpi_device_dir(device
))
198 entry
= proc_create_data(ACPI_BUTTON_FILE_INFO
,
199 S_IRUGO
, acpi_device_dir(device
),
200 &acpi_button_info_fops
, device
);
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
);
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
;
235 /* --------------------------------------------------------------------------
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
;
244 status
= acpi_evaluate_integer(device
->handle
, "_LID", NULL
, &state
);
245 if (ACPI_FAILURE(status
))
248 /* input layer checks if event is redundant */
249 input_report_switch(button
->input
, SW_LID
, !state
);
250 input_sync(button
->input
);
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
;
260 case ACPI_FIXED_HARDWARE_EVENT
:
261 event
= ACPI_BUTTON_NOTIFY_STATUS
;
263 case ACPI_BUTTON_NOTIFY_STATUS
:
264 input
= button
->input
;
265 if (button
->type
== ACPI_BUTTON_TYPE_LID
) {
266 acpi_lid_send_state(device
);
268 int keycode
= test_bit(KEY_SLEEP
, input
->keybit
) ?
269 KEY_SLEEP
: KEY_POWER
;
271 input_report_key(input
, keycode
, 1);
273 input_report_key(input
, keycode
, 0);
277 acpi_bus_generate_proc_event(device
, event
, ++button
->pushed
);
280 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
281 "Unsupported event [0x%x]\n", event
));
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
);
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;
302 button
= kzalloc(sizeof(struct acpi_button
), GFP_KERNEL
);
306 device
->driver_data
= button
;
308 button
->input
= input
= input_allocate_device();
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
);
348 printk(KERN_ERR PREFIX
"Unsupported hid [%s]\n", hid
);
353 error
= acpi_button_add_fs(device
);
357 snprintf(button
->phys
, sizeof(button
->phys
), "%s/button/input0", hid
);
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
);
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
);
378 case ACPI_BUTTON_TYPE_LID
:
379 input
->evbit
[0] = BIT_MASK(EV_SW
);
380 set_bit(SW_LID
, input
->swbit
);
384 error
= input_register_device(input
);
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
));
404 acpi_button_remove_fs(device
);
406 input_free_device(input
);
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
);
422 static int __init
acpi_button_init(void)
426 acpi_button_dir
= proc_mkdir(ACPI_BUTTON_CLASS
, acpi_root_dir
);
427 if (!acpi_button_dir
)
430 result
= acpi_bus_register_driver(&acpi_button_driver
);
432 remove_proc_entry(ACPI_BUTTON_CLASS
, acpi_root_dir
);
439 static void __exit
acpi_button_exit(void)
441 acpi_bus_unregister_driver(&acpi_button_driver
);
444 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER
, acpi_button_dir
);
446 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP
, acpi_button_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
);