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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/types.h>
26 #include <linux/proc_fs.h>
27 #include <linux/seq_file.h>
28 #include <linux/input.h>
29 #include <linux/slab.h>
30 #include <linux/acpi.h>
31 #include <acpi/button.h>
33 #define PREFIX "ACPI: "
35 #define ACPI_BUTTON_CLASS "button"
36 #define ACPI_BUTTON_FILE_INFO "info"
37 #define ACPI_BUTTON_FILE_STATE "state"
38 #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
39 #define ACPI_BUTTON_NOTIFY_STATUS 0x80
41 #define ACPI_BUTTON_SUBCLASS_POWER "power"
42 #define ACPI_BUTTON_HID_POWER "PNP0C0C"
43 #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
44 #define ACPI_BUTTON_TYPE_POWER 0x01
46 #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
47 #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
48 #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
49 #define ACPI_BUTTON_TYPE_SLEEP 0x03
51 #define ACPI_BUTTON_SUBCLASS_LID "lid"
52 #define ACPI_BUTTON_HID_LID "PNP0C0D"
53 #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
54 #define ACPI_BUTTON_TYPE_LID 0x05
56 #define ACPI_BUTTON_LID_INIT_IGNORE 0x00
57 #define ACPI_BUTTON_LID_INIT_OPEN 0x01
58 #define ACPI_BUTTON_LID_INIT_METHOD 0x02
60 #define _COMPONENT ACPI_BUTTON_COMPONENT
61 ACPI_MODULE_NAME("button");
63 MODULE_AUTHOR("Paul Diefenbaugh");
64 MODULE_DESCRIPTION("ACPI Button Driver");
65 MODULE_LICENSE("GPL");
67 static const struct acpi_device_id button_device_ids
[] = {
68 {ACPI_BUTTON_HID_LID
, 0},
69 {ACPI_BUTTON_HID_SLEEP
, 0},
70 {ACPI_BUTTON_HID_SLEEPF
, 0},
71 {ACPI_BUTTON_HID_POWER
, 0},
72 {ACPI_BUTTON_HID_POWERF
, 0},
75 MODULE_DEVICE_TABLE(acpi
, button_device_ids
);
77 static int acpi_button_add(struct acpi_device
*device
);
78 static int acpi_button_remove(struct acpi_device
*device
);
79 static void acpi_button_notify(struct acpi_device
*device
, u32 event
);
81 #ifdef CONFIG_PM_SLEEP
82 static int acpi_button_suspend(struct device
*dev
);
83 static int acpi_button_resume(struct device
*dev
);
85 #define acpi_button_suspend NULL
86 #define acpi_button_resume NULL
88 static SIMPLE_DEV_PM_OPS(acpi_button_pm
, acpi_button_suspend
, acpi_button_resume
);
90 static struct acpi_driver acpi_button_driver
= {
92 .class = ACPI_BUTTON_CLASS
,
93 .ids
= button_device_ids
,
95 .add
= acpi_button_add
,
96 .remove
= acpi_button_remove
,
97 .notify
= acpi_button_notify
,
99 .drv
.pm
= &acpi_button_pm
,
104 struct input_dev
*input
;
105 char phys
[32]; /* for input device */
106 unsigned long pushed
;
110 static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier
);
111 static struct acpi_device
*lid_device
;
112 static u8 lid_init_state
= ACPI_BUTTON_LID_INIT_METHOD
;
114 /* --------------------------------------------------------------------------
116 -------------------------------------------------------------------------- */
118 static struct proc_dir_entry
*acpi_button_dir
;
119 static struct proc_dir_entry
*acpi_lid_dir
;
121 static int acpi_lid_evaluate_state(struct acpi_device
*device
)
123 unsigned long long lid_state
;
126 status
= acpi_evaluate_integer(device
->handle
, "_LID", NULL
, &lid_state
);
127 if (ACPI_FAILURE(status
))
130 return lid_state
? 1 : 0;
133 static int acpi_lid_notify_state(struct acpi_device
*device
, int state
)
135 struct acpi_button
*button
= acpi_driver_data(device
);
138 /* input layer checks if event is redundant */
139 input_report_switch(button
->input
, SW_LID
, !state
);
140 input_sync(button
->input
);
143 pm_wakeup_event(&device
->dev
, 0);
145 ret
= blocking_notifier_call_chain(&acpi_lid_notifier
, state
, device
);
146 if (ret
== NOTIFY_DONE
)
147 ret
= blocking_notifier_call_chain(&acpi_lid_notifier
, state
,
149 if (ret
== NOTIFY_DONE
|| ret
== NOTIFY_OK
) {
151 * It is also regarded as success if the notifier_chain
152 * returns NOTIFY_OK or NOTIFY_DONE.
159 static int acpi_button_state_seq_show(struct seq_file
*seq
, void *offset
)
161 struct acpi_device
*device
= seq
->private;
164 state
= acpi_lid_evaluate_state(device
);
165 seq_printf(seq
, "state: %s\n",
166 state
< 0 ? "unsupported" : (state
? "open" : "closed"));
170 static int acpi_button_state_open_fs(struct inode
*inode
, struct file
*file
)
172 return single_open(file
, acpi_button_state_seq_show
, PDE_DATA(inode
));
175 static const struct file_operations acpi_button_state_fops
= {
176 .owner
= THIS_MODULE
,
177 .open
= acpi_button_state_open_fs
,
180 .release
= single_release
,
183 static int acpi_button_add_fs(struct acpi_device
*device
)
185 struct acpi_button
*button
= acpi_driver_data(device
);
186 struct proc_dir_entry
*entry
= NULL
;
189 /* procfs I/F for ACPI lid device only */
190 if (button
->type
!= ACPI_BUTTON_TYPE_LID
)
193 if (acpi_button_dir
|| acpi_lid_dir
) {
194 printk(KERN_ERR PREFIX
"More than one Lid device found!\n");
198 /* create /proc/acpi/button */
199 acpi_button_dir
= proc_mkdir(ACPI_BUTTON_CLASS
, acpi_root_dir
);
200 if (!acpi_button_dir
)
203 /* create /proc/acpi/button/lid */
204 acpi_lid_dir
= proc_mkdir(ACPI_BUTTON_SUBCLASS_LID
, acpi_button_dir
);
207 goto remove_button_dir
;
210 /* create /proc/acpi/button/lid/LID/ */
211 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
), acpi_lid_dir
);
212 if (!acpi_device_dir(device
)) {
217 /* create /proc/acpi/button/lid/LID/state */
218 entry
= proc_create_data(ACPI_BUTTON_FILE_STATE
,
219 S_IRUGO
, acpi_device_dir(device
),
220 &acpi_button_state_fops
, device
);
230 remove_proc_entry(acpi_device_bid(device
),
232 acpi_device_dir(device
) = NULL
;
234 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID
, acpi_button_dir
);
237 remove_proc_entry(ACPI_BUTTON_CLASS
, acpi_root_dir
);
238 acpi_button_dir
= NULL
;
242 static int acpi_button_remove_fs(struct acpi_device
*device
)
244 struct acpi_button
*button
= acpi_driver_data(device
);
246 if (button
->type
!= ACPI_BUTTON_TYPE_LID
)
249 remove_proc_entry(ACPI_BUTTON_FILE_STATE
,
250 acpi_device_dir(device
));
251 remove_proc_entry(acpi_device_bid(device
),
253 acpi_device_dir(device
) = NULL
;
254 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID
, acpi_button_dir
);
256 remove_proc_entry(ACPI_BUTTON_CLASS
, acpi_root_dir
);
257 acpi_button_dir
= NULL
;
262 /* --------------------------------------------------------------------------
264 -------------------------------------------------------------------------- */
265 int acpi_lid_notifier_register(struct notifier_block
*nb
)
267 return blocking_notifier_chain_register(&acpi_lid_notifier
, nb
);
269 EXPORT_SYMBOL(acpi_lid_notifier_register
);
271 int acpi_lid_notifier_unregister(struct notifier_block
*nb
)
273 return blocking_notifier_chain_unregister(&acpi_lid_notifier
, nb
);
275 EXPORT_SYMBOL(acpi_lid_notifier_unregister
);
277 int acpi_lid_open(void)
282 return acpi_lid_evaluate_state(lid_device
);
284 EXPORT_SYMBOL(acpi_lid_open
);
286 static int acpi_lid_update_state(struct acpi_device
*device
)
290 state
= acpi_lid_evaluate_state(device
);
294 return acpi_lid_notify_state(device
, state
);
297 static void acpi_lid_initialize_state(struct acpi_device
*device
)
299 switch (lid_init_state
) {
300 case ACPI_BUTTON_LID_INIT_OPEN
:
301 (void)acpi_lid_notify_state(device
, 1);
303 case ACPI_BUTTON_LID_INIT_METHOD
:
304 (void)acpi_lid_update_state(device
);
306 case ACPI_BUTTON_LID_INIT_IGNORE
:
312 static void acpi_button_notify(struct acpi_device
*device
, u32 event
)
314 struct acpi_button
*button
= acpi_driver_data(device
);
315 struct input_dev
*input
;
318 case ACPI_FIXED_HARDWARE_EVENT
:
319 event
= ACPI_BUTTON_NOTIFY_STATUS
;
321 case ACPI_BUTTON_NOTIFY_STATUS
:
322 input
= button
->input
;
323 if (button
->type
== ACPI_BUTTON_TYPE_LID
) {
324 acpi_lid_update_state(device
);
328 pm_wakeup_event(&device
->dev
, 0);
329 if (button
->suspended
)
332 keycode
= test_bit(KEY_SLEEP
, input
->keybit
) ?
333 KEY_SLEEP
: KEY_POWER
;
334 input_report_key(input
, keycode
, 1);
336 input_report_key(input
, keycode
, 0);
339 acpi_bus_generate_netlink_event(
340 device
->pnp
.device_class
,
341 dev_name(&device
->dev
),
342 event
, ++button
->pushed
);
346 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
347 "Unsupported event [0x%x]\n", event
));
352 #ifdef CONFIG_PM_SLEEP
353 static int acpi_button_suspend(struct device
*dev
)
355 struct acpi_device
*device
= to_acpi_device(dev
);
356 struct acpi_button
*button
= acpi_driver_data(device
);
358 button
->suspended
= true;
362 static int acpi_button_resume(struct device
*dev
)
364 struct acpi_device
*device
= to_acpi_device(dev
);
365 struct acpi_button
*button
= acpi_driver_data(device
);
367 button
->suspended
= false;
368 if (button
->type
== ACPI_BUTTON_TYPE_LID
)
369 acpi_lid_initialize_state(device
);
374 static int acpi_button_add(struct acpi_device
*device
)
376 struct acpi_button
*button
;
377 struct input_dev
*input
;
378 const char *hid
= acpi_device_hid(device
);
382 button
= kzalloc(sizeof(struct acpi_button
), GFP_KERNEL
);
386 device
->driver_data
= button
;
388 button
->input
= input
= input_allocate_device();
391 goto err_free_button
;
394 name
= acpi_device_name(device
);
395 class = acpi_device_class(device
);
397 if (!strcmp(hid
, ACPI_BUTTON_HID_POWER
) ||
398 !strcmp(hid
, ACPI_BUTTON_HID_POWERF
)) {
399 button
->type
= ACPI_BUTTON_TYPE_POWER
;
400 strcpy(name
, ACPI_BUTTON_DEVICE_NAME_POWER
);
401 sprintf(class, "%s/%s",
402 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_POWER
);
403 } else if (!strcmp(hid
, ACPI_BUTTON_HID_SLEEP
) ||
404 !strcmp(hid
, ACPI_BUTTON_HID_SLEEPF
)) {
405 button
->type
= ACPI_BUTTON_TYPE_SLEEP
;
406 strcpy(name
, ACPI_BUTTON_DEVICE_NAME_SLEEP
);
407 sprintf(class, "%s/%s",
408 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_SLEEP
);
409 } else if (!strcmp(hid
, ACPI_BUTTON_HID_LID
)) {
410 button
->type
= ACPI_BUTTON_TYPE_LID
;
411 strcpy(name
, ACPI_BUTTON_DEVICE_NAME_LID
);
412 sprintf(class, "%s/%s",
413 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_LID
);
415 printk(KERN_ERR PREFIX
"Unsupported hid [%s]\n", hid
);
420 error
= acpi_button_add_fs(device
);
424 snprintf(button
->phys
, sizeof(button
->phys
), "%s/button/input0", hid
);
427 input
->phys
= button
->phys
;
428 input
->id
.bustype
= BUS_HOST
;
429 input
->id
.product
= button
->type
;
430 input
->dev
.parent
= &device
->dev
;
432 switch (button
->type
) {
433 case ACPI_BUTTON_TYPE_POWER
:
434 input_set_capability(input
, EV_KEY
, KEY_POWER
);
437 case ACPI_BUTTON_TYPE_SLEEP
:
438 input_set_capability(input
, EV_KEY
, KEY_SLEEP
);
441 case ACPI_BUTTON_TYPE_LID
:
442 input_set_capability(input
, EV_SW
, SW_LID
);
446 error
= input_register_device(input
);
449 if (button
->type
== ACPI_BUTTON_TYPE_LID
) {
450 acpi_lid_initialize_state(device
);
452 * This assumes there's only one lid device, or if there are
453 * more we only care about the last one...
458 printk(KERN_INFO PREFIX
"%s [%s]\n", name
, acpi_device_bid(device
));
462 acpi_button_remove_fs(device
);
464 input_free_device(input
);
470 static int acpi_button_remove(struct acpi_device
*device
)
472 struct acpi_button
*button
= acpi_driver_data(device
);
474 acpi_button_remove_fs(device
);
475 input_unregister_device(button
->input
);
480 static int param_set_lid_init_state(const char *val
, struct kernel_param
*kp
)
484 if (!strncmp(val
, "open", sizeof("open") - 1)) {
485 lid_init_state
= ACPI_BUTTON_LID_INIT_OPEN
;
486 pr_info("Notify initial lid state as open\n");
487 } else if (!strncmp(val
, "method", sizeof("method") - 1)) {
488 lid_init_state
= ACPI_BUTTON_LID_INIT_METHOD
;
489 pr_info("Notify initial lid state with _LID return value\n");
490 } else if (!strncmp(val
, "ignore", sizeof("ignore") - 1)) {
491 lid_init_state
= ACPI_BUTTON_LID_INIT_IGNORE
;
492 pr_info("Do not notify initial lid state\n");
498 static int param_get_lid_init_state(char *buffer
, struct kernel_param
*kp
)
500 switch (lid_init_state
) {
501 case ACPI_BUTTON_LID_INIT_OPEN
:
502 return sprintf(buffer
, "open");
503 case ACPI_BUTTON_LID_INIT_METHOD
:
504 return sprintf(buffer
, "method");
505 case ACPI_BUTTON_LID_INIT_IGNORE
:
506 return sprintf(buffer
, "ignore");
508 return sprintf(buffer
, "invalid");
513 module_param_call(lid_init_state
,
514 param_set_lid_init_state
, param_get_lid_init_state
,
516 MODULE_PARM_DESC(lid_init_state
, "Behavior for reporting LID initial state");
518 module_acpi_driver(acpi_button_driver
);