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;
115 ACPI_FUNCTION_TRACE("acpi_button_info_seq_show");
117 if (!button
|| !button
->device
)
120 seq_printf(seq
, "type: %s\n",
121 acpi_device_name(button
->device
));
126 static int acpi_button_info_open_fs(struct inode
*inode
, struct file
*file
)
128 return single_open(file
, acpi_button_info_seq_show
, PDE(inode
)->data
);
131 static int acpi_button_state_seq_show(struct seq_file
*seq
, void *offset
)
133 struct acpi_button
*button
= (struct acpi_button
*)seq
->private;
137 ACPI_FUNCTION_TRACE("acpi_button_state_seq_show");
139 if (!button
|| !button
->device
)
142 status
= acpi_evaluate_integer(button
->handle
, "_LID", NULL
, &state
);
143 if (ACPI_FAILURE(status
)) {
144 seq_printf(seq
, "state: unsupported\n");
146 seq_printf(seq
, "state: %s\n",
147 (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 proc_dir_entry
*entry
= NULL
;
165 struct acpi_button
*button
= NULL
;
167 ACPI_FUNCTION_TRACE("acpi_button_add_fs");
169 if (!device
|| !acpi_driver_data(device
))
170 return_VALUE(-EINVAL
);
172 button
= acpi_driver_data(device
);
174 switch (button
->type
) {
175 case ACPI_BUTTON_TYPE_POWER
:
176 case ACPI_BUTTON_TYPE_POWERF
:
178 acpi_power_dir
= proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER
,
180 entry
= acpi_power_dir
;
182 case ACPI_BUTTON_TYPE_SLEEP
:
183 case ACPI_BUTTON_TYPE_SLEEPF
:
185 acpi_sleep_dir
= proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP
,
187 entry
= acpi_sleep_dir
;
189 case ACPI_BUTTON_TYPE_LID
:
191 acpi_lid_dir
= proc_mkdir(ACPI_BUTTON_SUBCLASS_LID
,
193 entry
= acpi_lid_dir
;
198 return_VALUE(-ENODEV
);
199 entry
->owner
= THIS_MODULE
;
201 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
), entry
);
202 if (!acpi_device_dir(device
))
203 return_VALUE(-ENODEV
);
204 acpi_device_dir(device
)->owner
= THIS_MODULE
;
207 entry
= create_proc_entry(ACPI_BUTTON_FILE_INFO
,
208 S_IRUGO
, acpi_device_dir(device
));
210 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
211 "Unable to create '%s' fs entry\n",
212 ACPI_BUTTON_FILE_INFO
));
214 entry
->proc_fops
= &acpi_button_info_fops
;
215 entry
->data
= acpi_driver_data(device
);
216 entry
->owner
= THIS_MODULE
;
219 /* show lid state [R] */
220 if (button
->type
== ACPI_BUTTON_TYPE_LID
) {
221 entry
= create_proc_entry(ACPI_BUTTON_FILE_STATE
,
222 S_IRUGO
, acpi_device_dir(device
));
224 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
225 "Unable to create '%s' fs entry\n",
226 ACPI_BUTTON_FILE_INFO
));
228 entry
->proc_fops
= &acpi_button_state_fops
;
229 entry
->data
= acpi_driver_data(device
);
230 entry
->owner
= THIS_MODULE
;
237 static int acpi_button_remove_fs(struct acpi_device
*device
)
239 struct acpi_button
*button
= NULL
;
241 ACPI_FUNCTION_TRACE("acpi_button_remove_fs");
243 button
= acpi_driver_data(device
);
244 if (acpi_device_dir(device
)) {
245 if (button
->type
== ACPI_BUTTON_TYPE_LID
)
246 remove_proc_entry(ACPI_BUTTON_FILE_STATE
,
247 acpi_device_dir(device
));
248 remove_proc_entry(ACPI_BUTTON_FILE_INFO
,
249 acpi_device_dir(device
));
251 remove_proc_entry(acpi_device_bid(device
),
252 acpi_device_dir(device
)->parent
);
253 acpi_device_dir(device
) = NULL
;
259 /* --------------------------------------------------------------------------
261 -------------------------------------------------------------------------- */
263 static void acpi_button_notify(acpi_handle handle
, u32 event
, void *data
)
265 struct acpi_button
*button
= (struct acpi_button
*)data
;
267 ACPI_FUNCTION_TRACE("acpi_button_notify");
269 if (!button
|| !button
->device
)
273 case ACPI_BUTTON_NOTIFY_STATUS
:
274 acpi_bus_generate_event(button
->device
, event
,
278 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
279 "Unsupported event [0x%x]\n", event
));
286 static acpi_status
acpi_button_notify_fixed(void *data
)
288 struct acpi_button
*button
= (struct acpi_button
*)data
;
290 ACPI_FUNCTION_TRACE("acpi_button_notify_fixed");
293 return_ACPI_STATUS(AE_BAD_PARAMETER
);
295 acpi_button_notify(button
->handle
, ACPI_BUTTON_NOTIFY_STATUS
, button
);
297 return_ACPI_STATUS(AE_OK
);
300 static int acpi_button_add(struct acpi_device
*device
)
303 acpi_status status
= AE_OK
;
304 struct acpi_button
*button
= NULL
;
306 ACPI_FUNCTION_TRACE("acpi_button_add");
309 return_VALUE(-EINVAL
);
311 button
= kmalloc(sizeof(struct acpi_button
), GFP_KERNEL
);
313 return_VALUE(-ENOMEM
);
314 memset(button
, 0, sizeof(struct acpi_button
));
316 button
->device
= device
;
317 button
->handle
= device
->handle
;
318 acpi_driver_data(device
) = button
;
321 * Determine the button type (via hid), as fixed-feature buttons
322 * need to be handled a bit differently than generic-space.
324 if (!strcmp(acpi_device_hid(device
), ACPI_BUTTON_HID_POWER
)) {
325 button
->type
= ACPI_BUTTON_TYPE_POWER
;
326 strcpy(acpi_device_name(device
), ACPI_BUTTON_DEVICE_NAME_POWER
);
327 sprintf(acpi_device_class(device
), "%s/%s",
328 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_POWER
);
329 } else if (!strcmp(acpi_device_hid(device
), ACPI_BUTTON_HID_POWERF
)) {
330 button
->type
= ACPI_BUTTON_TYPE_POWERF
;
331 strcpy(acpi_device_name(device
),
332 ACPI_BUTTON_DEVICE_NAME_POWERF
);
333 sprintf(acpi_device_class(device
), "%s/%s",
334 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_POWER
);
335 } else if (!strcmp(acpi_device_hid(device
), ACPI_BUTTON_HID_SLEEP
)) {
336 button
->type
= ACPI_BUTTON_TYPE_SLEEP
;
337 strcpy(acpi_device_name(device
), ACPI_BUTTON_DEVICE_NAME_SLEEP
);
338 sprintf(acpi_device_class(device
), "%s/%s",
339 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_SLEEP
);
340 } else if (!strcmp(acpi_device_hid(device
), ACPI_BUTTON_HID_SLEEPF
)) {
341 button
->type
= ACPI_BUTTON_TYPE_SLEEPF
;
342 strcpy(acpi_device_name(device
),
343 ACPI_BUTTON_DEVICE_NAME_SLEEPF
);
344 sprintf(acpi_device_class(device
), "%s/%s",
345 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_SLEEP
);
346 } else if (!strcmp(acpi_device_hid(device
), ACPI_BUTTON_HID_LID
)) {
347 button
->type
= ACPI_BUTTON_TYPE_LID
;
348 strcpy(acpi_device_name(device
), ACPI_BUTTON_DEVICE_NAME_LID
);
349 sprintf(acpi_device_class(device
), "%s/%s",
350 ACPI_BUTTON_CLASS
, ACPI_BUTTON_SUBCLASS_LID
);
352 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Unsupported hid [%s]\n",
353 acpi_device_hid(device
)));
358 result
= acpi_button_add_fs(device
);
362 switch (button
->type
) {
363 case ACPI_BUTTON_TYPE_POWERF
:
365 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
366 acpi_button_notify_fixed
,
369 case ACPI_BUTTON_TYPE_SLEEPF
:
371 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
372 acpi_button_notify_fixed
,
376 status
= acpi_install_notify_handler(button
->handle
,
383 if (ACPI_FAILURE(status
)) {
384 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
385 "Error installing notify handler\n"));
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
, ACPI_NOT_ISR
);
397 device
->wakeup
.state
.enabled
= 1;
400 printk(KERN_INFO PREFIX
"%s [%s]\n",
401 acpi_device_name(device
), acpi_device_bid(device
));
405 acpi_button_remove_fs(device
);
409 return_VALUE(result
);
412 static int acpi_button_remove(struct acpi_device
*device
, int type
)
414 acpi_status status
= 0;
415 struct acpi_button
*button
= NULL
;
417 ACPI_FUNCTION_TRACE("acpi_button_remove");
419 if (!device
|| !acpi_driver_data(device
))
420 return_VALUE(-EINVAL
);
422 button
= acpi_driver_data(device
);
424 /* Unregister for device notifications. */
425 switch (button
->type
) {
426 case ACPI_BUTTON_TYPE_POWERF
:
428 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON
,
429 acpi_button_notify_fixed
);
431 case ACPI_BUTTON_TYPE_SLEEPF
:
433 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON
,
434 acpi_button_notify_fixed
);
437 status
= acpi_remove_notify_handler(button
->handle
,
443 if (ACPI_FAILURE(status
))
444 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
445 "Error removing notify handler\n"));
447 acpi_button_remove_fs(device
);
454 static int __init
acpi_button_init(void)
458 ACPI_FUNCTION_TRACE("acpi_button_init");
460 acpi_button_dir
= proc_mkdir(ACPI_BUTTON_CLASS
, acpi_root_dir
);
461 if (!acpi_button_dir
)
462 return_VALUE(-ENODEV
);
463 acpi_button_dir
->owner
= THIS_MODULE
;
464 result
= acpi_bus_register_driver(&acpi_button_driver
);
466 remove_proc_entry(ACPI_BUTTON_CLASS
, acpi_root_dir
);
467 return_VALUE(-ENODEV
);
473 static void __exit
acpi_button_exit(void)
475 ACPI_FUNCTION_TRACE("acpi_button_exit");
477 acpi_bus_unregister_driver(&acpi_button_driver
);
480 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER
, acpi_button_dir
);
482 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP
, acpi_button_dir
);
484 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID
, acpi_button_dir
);
485 remove_proc_entry(ACPI_BUTTON_CLASS
, acpi_root_dir
);
490 module_init(acpi_button_init
);
491 module_exit(acpi_button_exit
);