blob: c2f06069dcd40d614a20c0a5e3a2883d47535a9a [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_CLASS "button"
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040037#define ACPI_BUTTON_FILE_INFO "info"
38#define ACPI_BUTTON_FILE_STATE "state"
39#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define ACPI_BUTTON_NOTIFY_STATUS 0x80
41
42#define ACPI_BUTTON_SUBCLASS_POWER "power"
Len Brown4be44fc2005-08-05 00:44:28 -040043#define ACPI_BUTTON_HID_POWER "PNP0C0C"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button (CM)"
45#define ACPI_BUTTON_DEVICE_NAME_POWERF "Power Button (FF)"
46#define ACPI_BUTTON_TYPE_POWER 0x01
47#define ACPI_BUTTON_TYPE_POWERF 0x02
48
49#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
50#define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
51#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button (CM)"
52#define ACPI_BUTTON_DEVICE_NAME_SLEEPF "Sleep Button (FF)"
53#define ACPI_BUTTON_TYPE_SLEEP 0x03
54#define ACPI_BUTTON_TYPE_SLEEPF 0x04
55
56#define ACPI_BUTTON_SUBCLASS_LID "lid"
57#define ACPI_BUTTON_HID_LID "PNP0C0D"
58#define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
59#define ACPI_BUTTON_TYPE_LID 0x05
60
61#define _COMPONENT ACPI_BUTTON_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050062ACPI_MODULE_NAME("button");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050064MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050065MODULE_DESCRIPTION("ACPI Button Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066MODULE_LICENSE("GPL");
67
Thomas Renninger1ba90e32007-07-23 14:44:41 +020068static const struct acpi_device_id button_device_ids[] = {
69 {ACPI_BUTTON_HID_LID, 0},
70 {ACPI_BUTTON_HID_SLEEP, 0},
71 {ACPI_BUTTON_HID_SLEEPF, 0},
72 {ACPI_BUTTON_HID_POWER, 0},
73 {ACPI_BUTTON_HID_POWERF, 0},
74 {"", 0},
75};
76MODULE_DEVICE_TABLE(acpi, button_device_ids);
77
Len Brown4be44fc2005-08-05 00:44:28 -040078static int acpi_button_add(struct acpi_device *device);
79static int acpi_button_remove(struct acpi_device *device, int type);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +040080static int acpi_button_resume(struct acpi_device *device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040081static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
82static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84static struct acpi_driver acpi_button_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050085 .name = "button",
Len Brown4be44fc2005-08-05 00:44:28 -040086 .class = ACPI_BUTTON_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020087 .ids = button_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040088 .ops = {
89 .add = acpi_button_add,
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +040090 .resume = acpi_button_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040091 .remove = acpi_button_remove,
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050092 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95struct acpi_button {
Len Brown4be44fc2005-08-05 00:44:28 -040096 struct acpi_device *device; /* Fixed button kludge */
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050097 unsigned int type;
98 struct input_dev *input;
99 char phys[32]; /* for input device */
Len Brown4be44fc2005-08-05 00:44:28 -0400100 unsigned long pushed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Arjan van de Vend7508032006-07-04 13:06:00 -0400103static const struct file_operations acpi_button_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700104 .owner = THIS_MODULE,
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 = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700112 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400113 .open = acpi_button_state_open_fs,
114 .read = seq_read,
115 .llseek = seq_lseek,
116 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400117};
Len Brown4be44fc2005-08-05 00:44:28 -0400118
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400119/* --------------------------------------------------------------------------
120 FS Interface (/proc)
121 -------------------------------------------------------------------------- */
122
Len Brown4be44fc2005-08-05 00:44:28 -0400123static struct proc_dir_entry *acpi_button_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400124
125static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
126{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500127 struct acpi_button *button = seq->private;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400128
129 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400130 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400131
Len Brown4be44fc2005-08-05 00:44:28 -0400132 seq_printf(seq, "type: %s\n",
133 acpi_device_name(button->device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400134
Patrick Mocheld550d982006-06-27 00:41:40 -0400135 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400136}
137
138static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
139{
140 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
141}
Len Brown4be44fc2005-08-05 00:44:28 -0400142
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400143static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
144{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500145 struct acpi_button *button = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400146 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400147 unsigned long long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400148
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400149 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400150 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400151
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400152 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500153 seq_printf(seq, "state: %s\n",
154 ACPI_FAILURE(status) ? "unsupported" :
155 (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400156 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400157}
158
159static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
160{
161 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
162}
163
164static struct proc_dir_entry *acpi_power_dir;
165static struct proc_dir_entry *acpi_sleep_dir;
166static struct proc_dir_entry *acpi_lid_dir;
167
Len Brown4be44fc2005-08-05 00:44:28 -0400168static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400169{
Len Brown4be44fc2005-08-05 00:44:28 -0400170 struct proc_dir_entry *entry = NULL;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500171 struct acpi_button *button;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400172
173 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400174 return -EINVAL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400175
176 button = acpi_driver_data(device);
177
178 switch (button->type) {
179 case ACPI_BUTTON_TYPE_POWER:
180 case ACPI_BUTTON_TYPE_POWERF:
181 if (!acpi_power_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400182 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
183 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400184 entry = acpi_power_dir;
185 break;
186 case ACPI_BUTTON_TYPE_SLEEP:
187 case ACPI_BUTTON_TYPE_SLEEPF:
188 if (!acpi_sleep_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400189 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
190 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400191 entry = acpi_sleep_dir;
192 break;
193 case ACPI_BUTTON_TYPE_LID:
194 if (!acpi_lid_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400195 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
196 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400197 entry = acpi_lid_dir;
198 break;
199 }
200
201 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400202 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400203
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
208 /* 'info' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700209 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
210 S_IRUGO, acpi_device_dir(device),
211 &acpi_button_info_fops,
212 acpi_driver_data(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400213 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400214 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400215
216 /* show lid state [R] */
217 if (button->type == ACPI_BUTTON_TYPE_LID) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700218 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
219 S_IRUGO, acpi_device_dir(device),
220 &acpi_button_state_fops,
221 acpi_driver_data(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400222 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400223 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400224 }
225
Patrick Mocheld550d982006-06-27 00:41:40 -0400226 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400227}
228
Len Brown4be44fc2005-08-05 00:44:28 -0400229static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400230{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500231 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400232
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400233 if (acpi_device_dir(device)) {
234 if (button->type == ACPI_BUTTON_TYPE_LID)
235 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400236 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400237 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400238 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400239
240 remove_proc_entry(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400241 acpi_device_dir(device)->parent);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400242 acpi_device_dir(device) = NULL;
243 }
244
Patrick Mocheld550d982006-06-27 00:41:40 -0400245 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/* --------------------------------------------------------------------------
249 Driver Interface
250 -------------------------------------------------------------------------- */
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400251static int acpi_lid_send_state(struct acpi_button *button)
252{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400253 unsigned long long state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400254 acpi_status status;
255
256 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
257 &state);
258 if (ACPI_FAILURE(status))
259 return -ENODEV;
260 /* input layer checks if event is redundant */
261 input_report_switch(button->input, SW_LID, !state);
Guillem Joverdf316e92008-10-24 00:28:33 +0300262 input_sync(button->input);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400263 return 0;
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Len Brown4be44fc2005-08-05 00:44:28 -0400266static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500268 struct acpi_button *button = data;
269 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400272 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 switch (event) {
275 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500276 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500277 if (button->type == ACPI_BUTTON_TYPE_LID) {
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400278 acpi_lid_send_state(button);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500279 } else {
280 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
281 KEY_SLEEP : KEY_POWER;
282
283 input_report_key(input, keycode, 1);
284 input_sync(input);
285 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300286 input_sync(input);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500287 }
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500288
Len Brown14e04fb2007-08-23 15:20:26 -0400289 acpi_bus_generate_proc_event(button->device, event,
Len Brown4be44fc2005-08-05 00:44:28 -0400290 ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
292 default:
293 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400294 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 break;
296 }
297
Patrick Mocheld550d982006-06-27 00:41:40 -0400298 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Len Brown4be44fc2005-08-05 00:44:28 -0400301static acpi_status acpi_button_notify_fixed(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500303 struct acpi_button *button = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400305 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400306 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400308 acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Patrick Mocheld550d982006-06-27 00:41:40 -0400310 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500313static int acpi_button_install_notify_handlers(struct acpi_button *button)
314{
315 acpi_status status;
316
317 switch (button->type) {
318 case ACPI_BUTTON_TYPE_POWERF:
319 status =
320 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
321 acpi_button_notify_fixed,
322 button);
323 break;
324 case ACPI_BUTTON_TYPE_SLEEPF:
325 status =
326 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
327 acpi_button_notify_fixed,
328 button);
329 break;
330 default:
331 status = acpi_install_notify_handler(button->device->handle,
332 ACPI_DEVICE_NOTIFY,
333 acpi_button_notify,
334 button);
335 break;
336 }
337
338 return ACPI_FAILURE(status) ? -ENODEV : 0;
339}
340
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400341static int acpi_button_resume(struct acpi_device *device)
342{
343 struct acpi_button *button;
344 if (!device)
345 return -EINVAL;
346 button = acpi_driver_data(device);
347 if (button && button->type == ACPI_BUTTON_TYPE_LID)
348 return acpi_lid_send_state(button);
349 return 0;
350}
351
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500352static void acpi_button_remove_notify_handlers(struct acpi_button *button)
353{
354 switch (button->type) {
355 case ACPI_BUTTON_TYPE_POWERF:
356 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
357 acpi_button_notify_fixed);
358 break;
359 case ACPI_BUTTON_TYPE_SLEEPF:
360 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
361 acpi_button_notify_fixed);
362 break;
363 default:
364 acpi_remove_notify_handler(button->device->handle,
365 ACPI_DEVICE_NOTIFY,
366 acpi_button_notify);
367 break;
368 }
369}
370
Len Brown4be44fc2005-08-05 00:44:28 -0400371static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500373 int error;
374 struct acpi_button *button;
375 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500380 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400382 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 button->device = device;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700385 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500387 button->input = input = input_allocate_device();
388 if (!input) {
389 error = -ENOMEM;
390 goto err_free_button;
391 }
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /*
394 * Determine the button type (via hid), as fixed-feature buttons
395 * need to be handled a bit differently than generic-space.
396 */
397 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
398 button->type = ACPI_BUTTON_TYPE_POWER;
Len Brown4be44fc2005-08-05 00:44:28 -0400399 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
400 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400402 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 button->type = ACPI_BUTTON_TYPE_POWERF;
404 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400405 ACPI_BUTTON_DEVICE_NAME_POWERF);
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_SLEEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 button->type = ACPI_BUTTON_TYPE_SLEEP;
Len Brown4be44fc2005-08-05 00:44:28 -0400410 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
411 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400413 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 button->type = ACPI_BUTTON_TYPE_SLEEPF;
415 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400416 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
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_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 button->type = ACPI_BUTTON_TYPE_LID;
Len Brown4be44fc2005-08-05 00:44:28 -0400421 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
422 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400424 } else {
Len Brown64684632006-06-26 23:41:38 -0400425 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
426 acpi_device_hid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500427 error = -ENODEV;
428 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500431 error = acpi_button_add_fs(device);
432 if (error)
433 goto err_free_input;
434
435 error = acpi_button_install_notify_handlers(button);
436 if (error)
437 goto err_remove_fs;
438
439 snprintf(button->phys, sizeof(button->phys),
440 "%s/button/input0", acpi_device_hid(device));
441
442 input->name = acpi_device_name(device);
443 input->phys = button->phys;
444 input->id.bustype = BUS_HOST;
445 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800446 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500449 case ACPI_BUTTON_TYPE_POWER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 case ACPI_BUTTON_TYPE_POWERF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700451 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500452 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500454
455 case ACPI_BUTTON_TYPE_SLEEP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 case ACPI_BUTTON_TYPE_SLEEPF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700457 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500458 set_bit(KEY_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500460
461 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700462 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500463 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 break;
465 }
466
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500467 error = input_register_device(input);
468 if (error)
469 goto err_remove_handlers;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400470 if (button->type == ACPI_BUTTON_TYPE_LID)
471 acpi_lid_send_state(button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if (device->wakeup.flags.valid) {
474 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400475 acpi_set_gpe_type(device->wakeup.gpe_device,
476 device->wakeup.gpe_number,
477 ACPI_GPE_TYPE_WAKE_RUN);
478 acpi_enable_gpe(device->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400479 device->wakeup.gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 device->wakeup.state.enabled = 1;
481 }
482
Len Brown4be44fc2005-08-05 00:44:28 -0400483 printk(KERN_INFO PREFIX "%s [%s]\n",
484 acpi_device_name(device), acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500488 err_remove_handlers:
489 acpi_button_remove_notify_handlers(button);
490 err_remove_fs:
491 acpi_button_remove_fs(device);
492 err_free_input:
493 input_free_device(input);
494 err_free_button:
495 kfree(button);
496 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Len Brown4be44fc2005-08-05 00:44:28 -0400499static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500501 struct acpi_button *button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400504 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 button = acpi_driver_data(device);
507
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500508 acpi_button_remove_notify_handlers(button);
Len Brown4be44fc2005-08-05 00:44:28 -0400509 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500510 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 kfree(button);
512
Patrick Mocheld550d982006-06-27 00:41:40 -0400513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Len Brown4be44fc2005-08-05 00:44:28 -0400516static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500518 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400520 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
521 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400522 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 result = acpi_bus_register_driver(&acpi_button_driver);
524 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400525 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400526 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
Patrick Mocheld550d982006-06-27 00:41:40 -0400529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Len Brown4be44fc2005-08-05 00:44:28 -0400532static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 acpi_bus_unregister_driver(&acpi_button_driver);
535
Len Brown4be44fc2005-08-05 00:44:28 -0400536 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400537 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
538 if (acpi_sleep_dir)
539 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
540 if (acpi_lid_dir)
541 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
542 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545module_init(acpi_button_init);
546module_exit(acpi_button_exit);