blob: 24a7865a57cb2515c740fbee5d9b038ca3927630 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_button.c - ACPI Button Driver ($Revision: 30 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
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.
13 *
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.
18 *
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.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/init.h>
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040029#include <linux/types.h>
30#include <linux/proc_fs.h>
31#include <linux/seq_file.h>
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050032#include <linux/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <acpi/acpi_bus.h>
34#include <acpi/acpi_drivers.h>
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define ACPI_BUTTON_COMPONENT 0x00080000
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define ACPI_BUTTON_CLASS "button"
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040038#define ACPI_BUTTON_FILE_INFO "info"
39#define ACPI_BUTTON_FILE_STATE "state"
40#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define ACPI_BUTTON_NOTIFY_STATUS 0x80
42
43#define ACPI_BUTTON_SUBCLASS_POWER "power"
Len Brown4be44fc2005-08-05 00:44:28 -040044#define ACPI_BUTTON_HID_POWER "PNP0C0C"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#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
49
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
56
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
61
62#define _COMPONENT ACPI_BUTTON_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050063ACPI_MODULE_NAME("button");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050065MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050066MODULE_DESCRIPTION("ACPI Button Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067MODULE_LICENSE("GPL");
68
Thomas Renninger1ba90e32007-07-23 14:44:41 +020069static const struct acpi_device_id button_device_ids[] = {
70 {ACPI_BUTTON_HID_LID, 0},
71 {ACPI_BUTTON_HID_SLEEP, 0},
72 {ACPI_BUTTON_HID_SLEEPF, 0},
73 {ACPI_BUTTON_HID_POWER, 0},
74 {ACPI_BUTTON_HID_POWERF, 0},
75 {"", 0},
76};
77MODULE_DEVICE_TABLE(acpi, button_device_ids);
78
Len Brown4be44fc2005-08-05 00:44:28 -040079static int acpi_button_add(struct acpi_device *device);
80static int acpi_button_remove(struct acpi_device *device, int type);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +040081static int acpi_button_resume(struct acpi_device *device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040082static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
83static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static struct acpi_driver acpi_button_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050086 .name = "button",
Len Brown4be44fc2005-08-05 00:44:28 -040087 .class = ACPI_BUTTON_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020088 .ids = button_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040089 .ops = {
90 .add = acpi_button_add,
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +040091 .resume = acpi_button_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040092 .remove = acpi_button_remove,
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050093 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
95
96struct acpi_button {
Len Brown4be44fc2005-08-05 00:44:28 -040097 struct acpi_device *device; /* Fixed button kludge */
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050098 unsigned int type;
99 struct input_dev *input;
100 char phys[32]; /* for input device */
Len Brown4be44fc2005-08-05 00:44:28 -0400101 unsigned long pushed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Arjan van de Vend7508032006-07-04 13:06:00 -0400104static const struct file_operations acpi_button_info_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400105 .open = acpi_button_info_open_fs,
106 .read = seq_read,
107 .llseek = seq_lseek,
108 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400109};
110
Arjan van de Vend7508032006-07-04 13:06:00 -0400111static const struct file_operations acpi_button_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -0400112 .open = acpi_button_state_open_fs,
113 .read = seq_read,
114 .llseek = seq_lseek,
115 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400116};
Len Brown4be44fc2005-08-05 00:44:28 -0400117
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400118/* --------------------------------------------------------------------------
119 FS Interface (/proc)
120 -------------------------------------------------------------------------- */
121
Len Brown4be44fc2005-08-05 00:44:28 -0400122static struct proc_dir_entry *acpi_button_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400123
124static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
125{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500126 struct acpi_button *button = seq->private;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400127
128 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400129 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400130
Len Brown4be44fc2005-08-05 00:44:28 -0400131 seq_printf(seq, "type: %s\n",
132 acpi_device_name(button->device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400133
Patrick Mocheld550d982006-06-27 00:41:40 -0400134 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400135}
136
137static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
138{
139 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
140}
Len Brown4be44fc2005-08-05 00:44:28 -0400141
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400142static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
143{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500144 struct acpi_button *button = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 acpi_status status;
146 unsigned long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400147
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400148 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400149 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400150
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400151 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500152 seq_printf(seq, "state: %s\n",
153 ACPI_FAILURE(status) ? "unsupported" :
154 (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400155 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400156}
157
158static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
159{
160 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
161}
162
163static struct proc_dir_entry *acpi_power_dir;
164static struct proc_dir_entry *acpi_sleep_dir;
165static struct proc_dir_entry *acpi_lid_dir;
166
Len Brown4be44fc2005-08-05 00:44:28 -0400167static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400168{
Len Brown4be44fc2005-08-05 00:44:28 -0400169 struct proc_dir_entry *entry = NULL;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500170 struct acpi_button *button;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400171
172 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400173 return -EINVAL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400174
175 button = acpi_driver_data(device);
176
177 switch (button->type) {
178 case ACPI_BUTTON_TYPE_POWER:
179 case ACPI_BUTTON_TYPE_POWERF:
180 if (!acpi_power_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400181 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
182 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400183 entry = acpi_power_dir;
184 break;
185 case ACPI_BUTTON_TYPE_SLEEP:
186 case ACPI_BUTTON_TYPE_SLEEPF:
187 if (!acpi_sleep_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400188 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
189 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400190 entry = acpi_sleep_dir;
191 break;
192 case ACPI_BUTTON_TYPE_LID:
193 if (!acpi_lid_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400194 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
195 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400196 entry = acpi_lid_dir;
197 break;
198 }
199
200 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400201 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400202 entry->owner = THIS_MODULE;
203
204 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
205 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400206 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400207 acpi_device_dir(device)->owner = THIS_MODULE;
208
209 /* 'info' [R] */
210 entry = create_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400211 S_IRUGO, acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400212 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400213 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400214 else {
215 entry->proc_fops = &acpi_button_info_fops;
216 entry->data = acpi_driver_data(device);
217 entry->owner = THIS_MODULE;
218 }
219
220 /* show lid state [R] */
221 if (button->type == ACPI_BUTTON_TYPE_LID) {
222 entry = create_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400223 S_IRUGO, acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400224 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400225 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400226 else {
227 entry->proc_fops = &acpi_button_state_fops;
228 entry->data = acpi_driver_data(device);
229 entry->owner = THIS_MODULE;
230 }
231 }
232
Patrick Mocheld550d982006-06-27 00:41:40 -0400233 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400234}
235
Len Brown4be44fc2005-08-05 00:44:28 -0400236static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400237{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500238 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400239
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400240 if (acpi_device_dir(device)) {
241 if (button->type == ACPI_BUTTON_TYPE_LID)
242 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400243 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400244 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400245 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400246
247 remove_proc_entry(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400248 acpi_device_dir(device)->parent);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400249 acpi_device_dir(device) = NULL;
250 }
251
Patrick Mocheld550d982006-06-27 00:41:40 -0400252 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/* --------------------------------------------------------------------------
256 Driver Interface
257 -------------------------------------------------------------------------- */
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400258static int acpi_lid_send_state(struct acpi_button *button)
259{
260 unsigned long state;
261 acpi_status status;
262
263 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
264 &state);
265 if (ACPI_FAILURE(status))
266 return -ENODEV;
267 /* input layer checks if event is redundant */
268 input_report_switch(button->input, SW_LID, !state);
269 return 0;
270}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Len Brown4be44fc2005-08-05 00:44:28 -0400272static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500274 struct acpi_button *button = data;
275 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400278 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 switch (event) {
281 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500282 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500283 if (button->type == ACPI_BUTTON_TYPE_LID) {
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400284 acpi_lid_send_state(button);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500285 } else {
286 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
287 KEY_SLEEP : KEY_POWER;
288
289 input_report_key(input, keycode, 1);
290 input_sync(input);
291 input_report_key(input, keycode, 0);
292 }
293 input_sync(input);
294
Len Brown14e04fb2007-08-23 15:20:26 -0400295 acpi_bus_generate_proc_event(button->device, event,
Len Brown4be44fc2005-08-05 00:44:28 -0400296 ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 break;
298 default:
299 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400300 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
302 }
303
Patrick Mocheld550d982006-06-27 00:41:40 -0400304 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Len Brown4be44fc2005-08-05 00:44:28 -0400307static acpi_status acpi_button_notify_fixed(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500309 struct acpi_button *button = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400311 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400312 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400314 acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Patrick Mocheld550d982006-06-27 00:41:40 -0400316 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500319static int acpi_button_install_notify_handlers(struct acpi_button *button)
320{
321 acpi_status status;
322
323 switch (button->type) {
324 case ACPI_BUTTON_TYPE_POWERF:
325 status =
326 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
327 acpi_button_notify_fixed,
328 button);
329 break;
330 case ACPI_BUTTON_TYPE_SLEEPF:
331 status =
332 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
333 acpi_button_notify_fixed,
334 button);
335 break;
336 default:
337 status = acpi_install_notify_handler(button->device->handle,
338 ACPI_DEVICE_NOTIFY,
339 acpi_button_notify,
340 button);
341 break;
342 }
343
344 return ACPI_FAILURE(status) ? -ENODEV : 0;
345}
346
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400347static int acpi_button_resume(struct acpi_device *device)
348{
349 struct acpi_button *button;
350 if (!device)
351 return -EINVAL;
352 button = acpi_driver_data(device);
353 if (button && button->type == ACPI_BUTTON_TYPE_LID)
354 return acpi_lid_send_state(button);
355 return 0;
356}
357
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500358static void acpi_button_remove_notify_handlers(struct acpi_button *button)
359{
360 switch (button->type) {
361 case ACPI_BUTTON_TYPE_POWERF:
362 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
363 acpi_button_notify_fixed);
364 break;
365 case ACPI_BUTTON_TYPE_SLEEPF:
366 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
367 acpi_button_notify_fixed);
368 break;
369 default:
370 acpi_remove_notify_handler(button->device->handle,
371 ACPI_DEVICE_NOTIFY,
372 acpi_button_notify);
373 break;
374 }
375}
376
Len Brown4be44fc2005-08-05 00:44:28 -0400377static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500379 int error;
380 struct acpi_button *button;
381 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400384 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500386 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400388 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 button->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 acpi_driver_data(device) = button;
392
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500393 button->input = input = input_allocate_device();
394 if (!input) {
395 error = -ENOMEM;
396 goto err_free_button;
397 }
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /*
400 * Determine the button type (via hid), as fixed-feature buttons
401 * need to be handled a bit differently than generic-space.
402 */
403 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
404 button->type = ACPI_BUTTON_TYPE_POWER;
Len Brown4be44fc2005-08-05 00:44:28 -0400405 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
406 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400408 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 button->type = ACPI_BUTTON_TYPE_POWERF;
410 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400411 ACPI_BUTTON_DEVICE_NAME_POWERF);
412 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400414 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 button->type = ACPI_BUTTON_TYPE_SLEEP;
Len Brown4be44fc2005-08-05 00:44:28 -0400416 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
417 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400419 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 button->type = ACPI_BUTTON_TYPE_SLEEPF;
421 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400422 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
423 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400425 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 button->type = ACPI_BUTTON_TYPE_LID;
Len Brown4be44fc2005-08-05 00:44:28 -0400427 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
428 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400430 } else {
Len Brown64684632006-06-26 23:41:38 -0400431 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
432 acpi_device_hid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500433 error = -ENODEV;
434 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500437 error = acpi_button_add_fs(device);
438 if (error)
439 goto err_free_input;
440
441 error = acpi_button_install_notify_handlers(button);
442 if (error)
443 goto err_remove_fs;
444
445 snprintf(button->phys, sizeof(button->phys),
446 "%s/button/input0", acpi_device_hid(device));
447
448 input->name = acpi_device_name(device);
449 input->phys = button->phys;
450 input->id.bustype = BUS_HOST;
451 input->id.product = button->type;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500454 case ACPI_BUTTON_TYPE_POWER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 case ACPI_BUTTON_TYPE_POWERF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700456 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500457 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500459
460 case ACPI_BUTTON_TYPE_SLEEP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 case ACPI_BUTTON_TYPE_SLEEPF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700462 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500463 set_bit(KEY_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500465
466 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700467 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500468 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
470 }
471
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500472 error = input_register_device(input);
473 if (error)
474 goto err_remove_handlers;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400475 if (button->type == ACPI_BUTTON_TYPE_LID)
476 acpi_lid_send_state(button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 if (device->wakeup.flags.valid) {
479 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400480 acpi_set_gpe_type(device->wakeup.gpe_device,
481 device->wakeup.gpe_number,
482 ACPI_GPE_TYPE_WAKE_RUN);
483 acpi_enable_gpe(device->wakeup.gpe_device,
484 device->wakeup.gpe_number, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 device->wakeup.state.enabled = 1;
486 }
487
Len Brown4be44fc2005-08-05 00:44:28 -0400488 printk(KERN_INFO PREFIX "%s [%s]\n",
489 acpi_device_name(device), acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500493 err_remove_handlers:
494 acpi_button_remove_notify_handlers(button);
495 err_remove_fs:
496 acpi_button_remove_fs(device);
497 err_free_input:
498 input_free_device(input);
499 err_free_button:
500 kfree(button);
501 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Len Brown4be44fc2005-08-05 00:44:28 -0400504static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500506 struct acpi_button *button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400509 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 button = acpi_driver_data(device);
512
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500513 acpi_button_remove_notify_handlers(button);
Len Brown4be44fc2005-08-05 00:44:28 -0400514 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500515 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 kfree(button);
517
Patrick Mocheld550d982006-06-27 00:41:40 -0400518 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Len Brown4be44fc2005-08-05 00:44:28 -0400521static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500523 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400525 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
526 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400527 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400528 acpi_button_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 result = acpi_bus_register_driver(&acpi_button_driver);
530 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400531 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400532 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
Patrick Mocheld550d982006-06-27 00:41:40 -0400535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Len Brown4be44fc2005-08-05 00:44:28 -0400538static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 acpi_bus_unregister_driver(&acpi_button_driver);
541
Len Brown4be44fc2005-08-05 00:44:28 -0400542 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400543 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
544 if (acpi_sleep_dir)
545 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
546 if (acpi_lid_dir)
547 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
548 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551module_init(acpi_button_init);
552module_exit(acpi_button_exit);