2 * acpi_button.c - ACPI Button Driver ($Revision: 30 $)
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 <acpi/acpi_bus.h>
33 #include <acpi/acpi_drivers.h>
35 #define ACPI_BUTTON_COMPONENT 0x00080000
36 #define ACPI_BUTTON_DRIVER_NAME "ACPI Button Driver"
37 #define ACPI_BUTTON_CLASS "button"
38 #define ACPI_BUTTON_FILE_INFO "info"
39 #define ACPI_BUTTON_FILE_STATE "state"
40 #define ACPI_BUTTON_TYPE_UNKNOWN 0x00
41 #define ACPI_BUTTON_NOTIFY_STATUS 0x80
43 #define ACPI_BUTTON_SUBCLASS_POWER "power"
44 #define ACPI_BUTTON_HID_POWER "PNP0C0C"
45 #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button (CM)"
46 #define ACPI_BUTTON_DEVICE_NAME_POWERF "Power Button (FF)"
47 #define ACPI_BUTTON_TYPE_POWER 0x01
48 #define ACPI_BUTTON_TYPE_POWERF 0x02
50 #define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
51 #define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
52 #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button (CM)"
53 #define ACPI_BUTTON_DEVICE_NAME_SLEEPF "Sleep Button (FF)"
54 #define ACPI_BUTTON_TYPE_SLEEP 0x03
55 #define ACPI_BUTTON_TYPE_SLEEPF 0x04
57 #define ACPI_BUTTON_SUBCLASS_LID "lid"
58 #define ACPI_BUTTON_HID_LID "PNP0C0D"
59 #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
60 #define ACPI_BUTTON_TYPE_LID 0x05
62 #define _COMPONENT ACPI_BUTTON_COMPONENT
63 ACPI_MODULE_NAME("acpi_button")
65 MODULE_AUTHOR("Paul Diefenbaugh");
66 MODULE_DESCRIPTION(ACPI_BUTTON_DRIVER_NAME
);
67 MODULE_LICENSE("GPL");
69 static int acpi_button_add(struct acpi_device
*device
);
70 static int acpi_button_remove(struct acpi_device
*device
, int type
);
71 static int acpi_button_info_open_fs(struct inode
*inode
, struct file
*file
);
72 static int acpi_button_state_open_fs(struct inode
*inode
, struct file
*file
);
74 static struct acpi_driver acpi_button_driver
= {
75 .name
= ACPI_BUTTON_DRIVER_NAME
,
76 .class = ACPI_BUTTON_CLASS
,
77 .ids
= "ACPI_FPB,ACPI_FSB,PNP0C0D,PNP0C0C,PNP0C0E",
79 .add
= acpi_button_add
,
80 .remove
= acpi_button_remove
,
86 struct acpi_device
*device
; /* Fixed button kludge */
91 static struct file_operations acpi_button_info_fops
= {
92 .open
= acpi_button_info_open_fs
,
95 .release
= single_release
,
98 static struct file_operations acpi_button_state_fops
= {
99 .open
= acpi_button_state_open_fs
,
102 .release
= single_release
,
105 /* --------------------------------------------------------------------------
107 -------------------------------------------------------------------------- */
109 static struct proc_dir_entry
*acpi_button_dir
;
111 static int acpi_button_info_seq_show(struct seq_file
*seq
, void *offset
)
113 struct acpi_button
*button
= (struct acpi_button
*)seq
->private;
116 if (!button
|| !button
->device
)
119 seq_printf(seq
, "type: %s\n",
120 acpi_device_name(button
->device
));
125 static int acpi_button_info_open_fs(struct inode
*inode
, struct file
*file
)
127 return single_open(file
, acpi_button_info_seq_show
, PDE(inode
)->data
);
130 static int acpi_button_state_seq_show(struct seq_file
*seq
, void *offset
)
132 struct acpi_button
*button
= (struct acpi_button
*)seq
->private;
137 if (!button
|| !button
->device
)
140 status
= acpi_evaluate_integer(button
->handle
, "_LID", NULL
, &state
);
141 if (ACPI_FAILURE(status
)) {
142 seq_printf(seq
, "state: unsupported\n");
144 seq_printf(seq
, "state: %s\n",
145 (state
? "open" : "closed"));
151 static int acpi_button_state_open_fs(struct inode
*inode
, struct file
*file
)
153 return single_open(file
, acpi_button_state_seq_show
, PDE(inode
)->data
);
156 static struct proc_dir_entry
*acpi_power_dir
;
157 static struct proc_dir_entry
*acpi_sleep_dir
;
158 static struct proc_dir_entry
*acpi_lid_dir
;
160 static int acpi_button_add_fs(struct acpi_device
*device
)
162 struct proc_dir_entry
*entry
= NULL
;
163 struct acpi_button
*button
= NULL
;
166 if (!device
|| !acpi_driver_data(device
))
169 button
= acpi_driver_data(device
);
171 switch (button
->type
) {
172 case ACPI_BUTTON_TYPE_POWER
:
173 case ACPI_BUTTON_TYPE_POWERF
:
175 acpi_power_dir
= proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER
,
177 entry
= acpi_power_dir
;
179 case ACPI_BUTTON_TYPE_SLEEP
:
180 case ACPI_BUTTON_TYPE_SLEEPF
:
182 acpi_sleep_dir
= proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP
,
184 entry
= acpi_sleep_dir
;
186 case ACPI_BUTTON_TYPE_LID
:
188 acpi_lid_dir
= proc_mkdir(ACPI_BUTTON_SUBCLASS_LID
,
190 entry
= acpi_lid_dir
;
196 entry
->owner
= THIS_MODULE
;
198 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
), entry
);
199 if (!acpi_device_dir(device
))
201 acpi_device_dir(device
)->owner
= THIS_MODULE
;
204 entry
= create_proc_entry(ACPI_BUTTON_FILE_INFO
,
205 S_IRUGO
, acpi_device_dir(device
));
209 entry
->proc_fops
= &acpi_button_info_fops
;
210 entry
->data
= acpi_driver_data(device
);
211 entry
->owner
= THIS_MODULE
;
214 /* show lid state [R] */
215 if (button
->type
== ACPI_BUTTON_TYPE_LID
) {
216 entry
= create_proc_entry(ACPI_BUTTON_FILE_STATE
,
217 S_IRUGO
, acpi_device_dir(device
));
221 entry
->proc_fops
= &acpi_button_state_fops
;
222 entry
->data
= acpi_driver_data(device
);
223 entry
->owner
= THIS_MODULE
;
230 static int acpi_button_remove_fs(struct acpi_device
*device
)
232 struct acpi_button
*button
= NULL
;
235 button
= acpi_driver_data(device
);
236 if (acpi_device_dir(device
)) {
237 if (button
->type
== ACPI_BUTTON_TYPE_LID
)
238 remove_proc_entry(ACPI_BUTTON_FILE_STATE
,
239 acpi_device_dir(device
));
240 remove_proc_entry(ACPI_BUTTON_FILE_INFO
,
241 acpi_device_dir(device
));
243 remove_proc_entry(acpi_device_bid(device
),
244 acpi_device_dir(device
)->parent
);
245 acpi_device_dir(device
) = NULL
;
251 /* --------------------------------------------------------------------------
253 -------------------------------------------------------------------------- */
255 static void acpi_button_notify(acpi_handle handle
, u32 event
, void *data
)
257 struct acpi_button
*button
= (struct acpi_button
*)data
;
260 if (!button
|| !button
->device
)
264 case ACPI_BUTTON_NOTIFY_STATUS
:
265 acpi_bus_generate_event(button
->device
, event
,
269 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
270 "Unsupported event [0x%x]\n", event
));
277 static acpi_status
acpi_button_notify_fixed(void *data
)
279 struct acpi_button
*button
= (struct acpi_button
*)data
;
283 return AE_BAD_PARAMETER
;
285 acpi_button_notify(button
->handle
, ACPI_BUTTON_NOTIFY_STATUS
, button
);
290 static int acpi_button_add(struct acpi_device
*device
)
293 acpi_status status
= AE_OK
;
294 struct acpi_button
*button
= NULL
;
300 button
= kmalloc(sizeof(struct acpi_button
), GFP_KERNEL
);
303 memset(button
, 0, sizeof(struct acpi_button
));
305 button
->device
= device
;
306 button
->handle
= device
->handle
;
307 acpi_driver_data(device
) = button
;
310 * Determine the button type (via hid), as fixed-feature buttons
311 * need to be handled a bit differently than generic-space.
313 if (!strcmp(acpi_device_hid(device
), ACPI_BUTTON_HID_POWER
)) {
314 button
->type
= ACPI_BUTTON_TYPE_POWER
;
315 strcpy(acpi_device_name(device
), ACPI_BUTTON_DEVICE_NAME_POWER
);
316 sprintf(acpi_device_class(device
), "%s/%s",
317 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_POWER
);
318 } else if (!strcmp(acpi_device_hid(device
), ACPI_BUTTON_HID_POWERF
)) {
319 button
->type
= ACPI_BUTTON_TYPE_POWERF
;
320 strcpy(acpi_device_name(device
),
321 ACPI_BUTTON_DEVICE_NAME_POWERF
);
322 sprintf(acpi_device_class(device
), "%s/%s",
323 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_POWER
);
324 } else if (!strcmp(acpi_device_hid(device
), ACPI_BUTTON_HID_SLEEP
)) {
325 button
->type
= ACPI_BUTTON_TYPE_SLEEP
;
326 strcpy(acpi_device_name(device
), ACPI_BUTTON_DEVICE_NAME_SLEEP
);
327 sprintf(acpi_device_class(device
), "%s/%s",
328 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_SLEEP
);
329 } else if (!strcmp(acpi_device_hid(device
), ACPI_BUTTON_HID_SLEEPF
)) {
330 button
->type
= ACPI_BUTTON_TYPE_SLEEPF
;
331 strcpy(acpi_device_name(device
),
332 ACPI_BUTTON_DEVICE_NAME_SLEEPF
);
333 sprintf(acpi_device_class(device
), "%s/%s",
334 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_SLEEP
);
335 } else if (!strcmp(acpi_device_hid(device
), ACPI_BUTTON_HID_LID
)) {
336 button
->type
= ACPI_BUTTON_TYPE_LID
;
337 strcpy(acpi_device_name(device
), ACPI_BUTTON_DEVICE_NAME_LID
);
338 sprintf(acpi_device_class(device
), "%s/%s",
339 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_LID
);
341 printk(KERN_ERR PREFIX
"Unsupported hid [%s]\n",
342 acpi_device_hid(device
));
347 result
= acpi_button_add_fs(device
);
351 switch (button
->type
) {
352 case ACPI_BUTTON_TYPE_POWERF
:
354 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
355 acpi_button_notify_fixed
,
358 case ACPI_BUTTON_TYPE_SLEEPF
:
360 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
361 acpi_button_notify_fixed
,
365 status
= acpi_install_notify_handler(button
->handle
,
372 if (ACPI_FAILURE(status
)) {
377 if (device
->wakeup
.flags
.valid
) {
378 /* Button's GPE is run-wake GPE */
379 acpi_set_gpe_type(device
->wakeup
.gpe_device
,
380 device
->wakeup
.gpe_number
,
381 ACPI_GPE_TYPE_WAKE_RUN
);
382 acpi_enable_gpe(device
->wakeup
.gpe_device
,
383 device
->wakeup
.gpe_number
, ACPI_NOT_ISR
);
384 device
->wakeup
.state
.enabled
= 1;
387 printk(KERN_INFO PREFIX
"%s [%s]\n",
388 acpi_device_name(device
), acpi_device_bid(device
));
392 acpi_button_remove_fs(device
);
399 static int acpi_button_remove(struct acpi_device
*device
, int type
)
401 acpi_status status
= 0;
402 struct acpi_button
*button
= NULL
;
405 if (!device
|| !acpi_driver_data(device
))
408 button
= acpi_driver_data(device
);
410 /* Unregister for device notifications. */
411 switch (button
->type
) {
412 case ACPI_BUTTON_TYPE_POWERF
:
414 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
415 acpi_button_notify_fixed
);
417 case ACPI_BUTTON_TYPE_SLEEPF
:
419 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
420 acpi_button_notify_fixed
);
423 status
= acpi_remove_notify_handler(button
->handle
,
429 acpi_button_remove_fs(device
);
436 static int __init
acpi_button_init(void)
441 acpi_button_dir
= proc_mkdir(ACPI_BUTTON_CLASS
, acpi_root_dir
);
442 if (!acpi_button_dir
)
444 acpi_button_dir
->owner
= THIS_MODULE
;
445 result
= acpi_bus_register_driver(&acpi_button_driver
);
447 remove_proc_entry(ACPI_BUTTON_CLASS
, acpi_root_dir
);
454 static void __exit
acpi_button_exit(void)
457 acpi_bus_unregister_driver(&acpi_button_driver
);
460 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER
, acpi_button_dir
);
462 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP
, acpi_button_dir
);
464 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID
, acpi_button_dir
);
465 remove_proc_entry(ACPI_BUTTON_CLASS
, acpi_root_dir
);
470 module_init(acpi_button_init
);
471 module_exit(acpi_button_exit
);