blob: eb6bf3025f97cebff32efc570d525ea5f289618d [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 = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700105 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400106 .open = acpi_button_info_open_fs,
107 .read = seq_read,
108 .llseek = seq_lseek,
109 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400110};
111
Arjan van de Vend7508032006-07-04 13:06:00 -0400112static const struct file_operations acpi_button_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700113 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400114 .open = acpi_button_state_open_fs,
115 .read = seq_read,
116 .llseek = seq_lseek,
117 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400118};
Len Brown4be44fc2005-08-05 00:44:28 -0400119
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400120/* --------------------------------------------------------------------------
121 FS Interface (/proc)
122 -------------------------------------------------------------------------- */
123
Len Brown4be44fc2005-08-05 00:44:28 -0400124static struct proc_dir_entry *acpi_button_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400125
126static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
127{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500128 struct acpi_button *button = seq->private;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400129
130 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400131 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400132
Len Brown4be44fc2005-08-05 00:44:28 -0400133 seq_printf(seq, "type: %s\n",
134 acpi_device_name(button->device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400135
Patrick Mocheld550d982006-06-27 00:41:40 -0400136 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400137}
138
139static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
140{
141 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
142}
Len Brown4be44fc2005-08-05 00:44:28 -0400143
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400144static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
145{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500146 struct acpi_button *button = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400147 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400148 unsigned long long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400149
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400150 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400151 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400152
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400153 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500154 seq_printf(seq, "state: %s\n",
155 ACPI_FAILURE(status) ? "unsupported" :
156 (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400157 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400158}
159
160static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
161{
162 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
163}
164
165static struct proc_dir_entry *acpi_power_dir;
166static struct proc_dir_entry *acpi_sleep_dir;
167static struct proc_dir_entry *acpi_lid_dir;
168
Len Brown4be44fc2005-08-05 00:44:28 -0400169static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400170{
Len Brown4be44fc2005-08-05 00:44:28 -0400171 struct proc_dir_entry *entry = NULL;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500172 struct acpi_button *button;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400173
174 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400175 return -EINVAL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400176
177 button = acpi_driver_data(device);
178
179 switch (button->type) {
180 case ACPI_BUTTON_TYPE_POWER:
181 case ACPI_BUTTON_TYPE_POWERF:
182 if (!acpi_power_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400183 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
184 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400185 entry = acpi_power_dir;
186 break;
187 case ACPI_BUTTON_TYPE_SLEEP:
188 case ACPI_BUTTON_TYPE_SLEEPF:
189 if (!acpi_sleep_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400190 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
191 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400192 entry = acpi_sleep_dir;
193 break;
194 case ACPI_BUTTON_TYPE_LID:
195 if (!acpi_lid_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400196 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
197 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400198 entry = acpi_lid_dir;
199 break;
200 }
201
202 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400203 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400204 entry->owner = THIS_MODULE;
205
206 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
207 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400208 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400209 acpi_device_dir(device)->owner = THIS_MODULE;
210
211 /* 'info' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700212 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
213 S_IRUGO, acpi_device_dir(device),
214 &acpi_button_info_fops,
215 acpi_driver_data(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400216 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400217 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400218
219 /* show lid state [R] */
220 if (button->type == ACPI_BUTTON_TYPE_LID) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700221 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
222 S_IRUGO, acpi_device_dir(device),
223 &acpi_button_state_fops,
224 acpi_driver_data(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400225 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400226 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400227 }
228
Patrick Mocheld550d982006-06-27 00:41:40 -0400229 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400230}
231
Len Brown4be44fc2005-08-05 00:44:28 -0400232static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400233{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500234 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400235
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400236 if (acpi_device_dir(device)) {
237 if (button->type == ACPI_BUTTON_TYPE_LID)
238 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400239 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400240 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400241 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400242
243 remove_proc_entry(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400244 acpi_device_dir(device)->parent);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400245 acpi_device_dir(device) = NULL;
246 }
247
Patrick Mocheld550d982006-06-27 00:41:40 -0400248 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/* --------------------------------------------------------------------------
252 Driver Interface
253 -------------------------------------------------------------------------- */
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400254static int acpi_lid_send_state(struct acpi_button *button)
255{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400256 unsigned long long state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400257 acpi_status status;
258
259 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
260 &state);
261 if (ACPI_FAILURE(status))
262 return -ENODEV;
263 /* input layer checks if event is redundant */
264 input_report_switch(button->input, SW_LID, !state);
Guillem Joverdf316e92008-10-24 00:28:33 +0300265 input_sync(button->input);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400266 return 0;
267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Len Brown4be44fc2005-08-05 00:44:28 -0400269static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500271 struct acpi_button *button = data;
272 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400275 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 switch (event) {
278 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500279 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500280 if (button->type == ACPI_BUTTON_TYPE_LID) {
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400281 acpi_lid_send_state(button);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500282 } else {
283 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
284 KEY_SLEEP : KEY_POWER;
285
286 input_report_key(input, keycode, 1);
287 input_sync(input);
288 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300289 input_sync(input);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500290 }
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500291
Len Brown14e04fb32007-08-23 15:20:26 -0400292 acpi_bus_generate_proc_event(button->device, event,
Len Brown4be44fc2005-08-05 00:44:28 -0400293 ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 break;
295 default:
296 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400297 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 break;
299 }
300
Patrick Mocheld550d982006-06-27 00:41:40 -0400301 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Len Brown4be44fc2005-08-05 00:44:28 -0400304static acpi_status acpi_button_notify_fixed(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500306 struct acpi_button *button = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400308 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400309 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400311 acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Patrick Mocheld550d982006-06-27 00:41:40 -0400313 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500316static int acpi_button_install_notify_handlers(struct acpi_button *button)
317{
318 acpi_status status;
319
320 switch (button->type) {
321 case ACPI_BUTTON_TYPE_POWERF:
322 status =
323 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
324 acpi_button_notify_fixed,
325 button);
326 break;
327 case ACPI_BUTTON_TYPE_SLEEPF:
328 status =
329 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
330 acpi_button_notify_fixed,
331 button);
332 break;
333 default:
334 status = acpi_install_notify_handler(button->device->handle,
335 ACPI_DEVICE_NOTIFY,
336 acpi_button_notify,
337 button);
338 break;
339 }
340
341 return ACPI_FAILURE(status) ? -ENODEV : 0;
342}
343
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400344static int acpi_button_resume(struct acpi_device *device)
345{
346 struct acpi_button *button;
347 if (!device)
348 return -EINVAL;
349 button = acpi_driver_data(device);
350 if (button && button->type == ACPI_BUTTON_TYPE_LID)
351 return acpi_lid_send_state(button);
352 return 0;
353}
354
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500355static void acpi_button_remove_notify_handlers(struct acpi_button *button)
356{
357 switch (button->type) {
358 case ACPI_BUTTON_TYPE_POWERF:
359 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
360 acpi_button_notify_fixed);
361 break;
362 case ACPI_BUTTON_TYPE_SLEEPF:
363 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
364 acpi_button_notify_fixed);
365 break;
366 default:
367 acpi_remove_notify_handler(button->device->handle,
368 ACPI_DEVICE_NOTIFY,
369 acpi_button_notify);
370 break;
371 }
372}
373
Len Brown4be44fc2005-08-05 00:44:28 -0400374static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500376 int error;
377 struct acpi_button *button;
378 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400381 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500383 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400385 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 button->device = device;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700388 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500390 button->input = input = input_allocate_device();
391 if (!input) {
392 error = -ENOMEM;
393 goto err_free_button;
394 }
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /*
397 * Determine the button type (via hid), as fixed-feature buttons
398 * need to be handled a bit differently than generic-space.
399 */
400 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
401 button->type = ACPI_BUTTON_TYPE_POWER;
Len Brown4be44fc2005-08-05 00:44:28 -0400402 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
403 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400405 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 button->type = ACPI_BUTTON_TYPE_POWERF;
407 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400408 ACPI_BUTTON_DEVICE_NAME_POWERF);
409 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400411 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 button->type = ACPI_BUTTON_TYPE_SLEEP;
Len Brown4be44fc2005-08-05 00:44:28 -0400413 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
414 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400416 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 button->type = ACPI_BUTTON_TYPE_SLEEPF;
418 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400419 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
420 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400422 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 button->type = ACPI_BUTTON_TYPE_LID;
Len Brown4be44fc2005-08-05 00:44:28 -0400424 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
425 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400427 } else {
Len Brown64684632006-06-26 23:41:38 -0400428 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
429 acpi_device_hid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500430 error = -ENODEV;
431 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500434 error = acpi_button_add_fs(device);
435 if (error)
436 goto err_free_input;
437
438 error = acpi_button_install_notify_handlers(button);
439 if (error)
440 goto err_remove_fs;
441
442 snprintf(button->phys, sizeof(button->phys),
443 "%s/button/input0", acpi_device_hid(device));
444
445 input->name = acpi_device_name(device);
446 input->phys = button->phys;
447 input->id.bustype = BUS_HOST;
448 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800449 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500452 case ACPI_BUTTON_TYPE_POWER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 case ACPI_BUTTON_TYPE_POWERF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700454 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500455 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500457
458 case ACPI_BUTTON_TYPE_SLEEP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 case ACPI_BUTTON_TYPE_SLEEPF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700460 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500461 set_bit(KEY_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500463
464 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700465 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500466 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 break;
468 }
469
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500470 error = input_register_device(input);
471 if (error)
472 goto err_remove_handlers;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400473 if (button->type == ACPI_BUTTON_TYPE_LID)
474 acpi_lid_send_state(button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if (device->wakeup.flags.valid) {
477 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400478 acpi_set_gpe_type(device->wakeup.gpe_device,
479 device->wakeup.gpe_number,
480 ACPI_GPE_TYPE_WAKE_RUN);
481 acpi_enable_gpe(device->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400482 device->wakeup.gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 device->wakeup.state.enabled = 1;
484 }
485
Len Brown4be44fc2005-08-05 00:44:28 -0400486 printk(KERN_INFO PREFIX "%s [%s]\n",
487 acpi_device_name(device), acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500489 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500491 err_remove_handlers:
492 acpi_button_remove_notify_handlers(button);
493 err_remove_fs:
494 acpi_button_remove_fs(device);
495 err_free_input:
496 input_free_device(input);
497 err_free_button:
498 kfree(button);
499 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Len Brown4be44fc2005-08-05 00:44:28 -0400502static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500504 struct acpi_button *button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400507 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 button = acpi_driver_data(device);
510
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500511 acpi_button_remove_notify_handlers(button);
Len Brown4be44fc2005-08-05 00:44:28 -0400512 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500513 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 kfree(button);
515
Patrick Mocheld550d982006-06-27 00:41:40 -0400516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
Len Brown4be44fc2005-08-05 00:44:28 -0400519static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500521 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400523 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
524 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400525 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400526 acpi_button_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 result = acpi_bus_register_driver(&acpi_button_driver);
528 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400529 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400530 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
Patrick Mocheld550d982006-06-27 00:41:40 -0400533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Len Brown4be44fc2005-08-05 00:44:28 -0400536static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 acpi_bus_unregister_driver(&acpi_button_driver);
539
Len Brown4be44fc2005-08-05 00:44:28 -0400540 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400541 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
542 if (acpi_sleep_dir)
543 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
544 if (acpi_lid_dir)
545 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
546 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549module_init(acpi_button_init);
550module_exit(acpi_button_exit);