blob: c4632366226f4c5b9bb309584729ce27bc2d459e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bjorn Helgaas50a4da82009-04-08 15:39:38 +00002 * button.c - ACPI Button Driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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);
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000081static void acpi_button_notify(struct acpi_device *device, u32 event);
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,
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000093 .notify = acpi_button_notify,
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050094 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
97struct acpi_button {
Len Brown4be44fc2005-08-05 00:44:28 -040098 struct acpi_device *device; /* Fixed button kludge */
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050099 unsigned int type;
100 struct input_dev *input;
101 char phys[32]; /* for input device */
Len Brown4be44fc2005-08-05 00:44:28 -0400102 unsigned long pushed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
Arjan van de Vend7508032006-07-04 13:06:00 -0400105static const struct file_operations acpi_button_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700106 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400107 .open = acpi_button_info_open_fs,
108 .read = seq_read,
109 .llseek = seq_lseek,
110 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400111};
112
Arjan van de Vend7508032006-07-04 13:06:00 -0400113static const struct file_operations acpi_button_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700114 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400115 .open = acpi_button_state_open_fs,
116 .read = seq_read,
117 .llseek = seq_lseek,
118 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400119};
Len Brown4be44fc2005-08-05 00:44:28 -0400120
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400121/* --------------------------------------------------------------------------
122 FS Interface (/proc)
123 -------------------------------------------------------------------------- */
124
Len Brown4be44fc2005-08-05 00:44:28 -0400125static struct proc_dir_entry *acpi_button_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400126
127static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
128{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500129 struct acpi_button *button = seq->private;
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));
Patrick Mocheld550d982006-06-27 00:41:40 -0400133 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400134}
135
136static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
137{
138 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
139}
Len Brown4be44fc2005-08-05 00:44:28 -0400140
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400141static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
142{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500143 struct acpi_button *button = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400144 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400145 unsigned long long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400146
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400147 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500148 seq_printf(seq, "state: %s\n",
149 ACPI_FAILURE(status) ? "unsupported" :
150 (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400151 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400152}
153
154static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
155{
156 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
157}
158
159static struct proc_dir_entry *acpi_power_dir;
160static struct proc_dir_entry *acpi_sleep_dir;
161static struct proc_dir_entry *acpi_lid_dir;
162
Len Brown4be44fc2005-08-05 00:44:28 -0400163static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400164{
Len Brown4be44fc2005-08-05 00:44:28 -0400165 struct proc_dir_entry *entry = NULL;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500166 struct acpi_button *button;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400167
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400168 button = acpi_driver_data(device);
169
170 switch (button->type) {
171 case ACPI_BUTTON_TYPE_POWER:
172 case ACPI_BUTTON_TYPE_POWERF:
173 if (!acpi_power_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400174 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
175 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400176 entry = acpi_power_dir;
177 break;
178 case ACPI_BUTTON_TYPE_SLEEP:
179 case ACPI_BUTTON_TYPE_SLEEPF:
180 if (!acpi_sleep_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400181 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
182 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400183 entry = acpi_sleep_dir;
184 break;
185 case ACPI_BUTTON_TYPE_LID:
186 if (!acpi_lid_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400187 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
188 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400189 entry = acpi_lid_dir;
190 break;
191 }
192
193 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400194 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400195
196 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
197 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400198 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400199
200 /* 'info' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700201 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
202 S_IRUGO, acpi_device_dir(device),
203 &acpi_button_info_fops,
204 acpi_driver_data(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400205 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400206 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400207
208 /* show lid state [R] */
209 if (button->type == ACPI_BUTTON_TYPE_LID) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700210 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
211 S_IRUGO, acpi_device_dir(device),
212 &acpi_button_state_fops,
213 acpi_driver_data(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400214 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400215 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400216 }
217
Patrick Mocheld550d982006-06-27 00:41:40 -0400218 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400219}
220
Len Brown4be44fc2005-08-05 00:44:28 -0400221static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400222{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500223 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400224
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400225 if (acpi_device_dir(device)) {
226 if (button->type == ACPI_BUTTON_TYPE_LID)
227 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400228 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400229 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400230 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400231
232 remove_proc_entry(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400233 acpi_device_dir(device)->parent);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400234 acpi_device_dir(device) = NULL;
235 }
236
Patrick Mocheld550d982006-06-27 00:41:40 -0400237 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/* --------------------------------------------------------------------------
241 Driver Interface
242 -------------------------------------------------------------------------- */
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400243static int acpi_lid_send_state(struct acpi_button *button)
244{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400245 unsigned long long state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400246 acpi_status status;
247
248 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
249 &state);
250 if (ACPI_FAILURE(status))
251 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000252
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400253 /* input layer checks if event is redundant */
254 input_report_switch(button->input, SW_LID, !state);
Guillem Joverdf316e92008-10-24 00:28:33 +0300255 input_sync(button->input);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400256 return 0;
257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000259static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000261 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500262 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000265 case ACPI_FIXED_HARDWARE_EVENT:
266 event = ACPI_BUTTON_NOTIFY_STATUS;
267 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500269 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500270 if (button->type == ACPI_BUTTON_TYPE_LID) {
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400271 acpi_lid_send_state(button);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500272 } else {
273 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
274 KEY_SLEEP : KEY_POWER;
275
276 input_report_key(input, keycode, 1);
277 input_sync(input);
278 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300279 input_sync(input);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500280 }
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500281
Len Brown14e04fb2007-08-23 15:20:26 -0400282 acpi_bus_generate_proc_event(button->device, event,
Len Brown4be44fc2005-08-05 00:44:28 -0400283 ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285 default:
286 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400287 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 break;
289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400292static int acpi_button_resume(struct acpi_device *device)
293{
294 struct acpi_button *button;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000295
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400296 button = acpi_driver_data(device);
Bjorn Helgaase2fb9752009-04-08 15:39:43 +0000297 if (button->type == ACPI_BUTTON_TYPE_LID)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400298 return acpi_lid_send_state(button);
299 return 0;
300}
301
Len Brown4be44fc2005-08-05 00:44:28 -0400302static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500304 int error;
305 struct acpi_button *button;
306 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500308 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400310 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 button->device = device;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700313 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500315 button->input = input = input_allocate_device();
316 if (!input) {
317 error = -ENOMEM;
318 goto err_free_button;
319 }
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /*
322 * Determine the button type (via hid), as fixed-feature buttons
323 * need to be handled a bit differently than generic-space.
324 */
325 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
326 button->type = ACPI_BUTTON_TYPE_POWER;
Len Brown4be44fc2005-08-05 00:44:28 -0400327 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
328 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400330 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 button->type = ACPI_BUTTON_TYPE_POWERF;
332 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400333 ACPI_BUTTON_DEVICE_NAME_POWERF);
334 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400336 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 button->type = ACPI_BUTTON_TYPE_SLEEP;
Len Brown4be44fc2005-08-05 00:44:28 -0400338 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
339 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400341 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 button->type = ACPI_BUTTON_TYPE_SLEEPF;
343 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400344 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
345 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400347 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 button->type = ACPI_BUTTON_TYPE_LID;
Len Brown4be44fc2005-08-05 00:44:28 -0400349 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
350 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400352 } else {
Len Brown64684632006-06-26 23:41:38 -0400353 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
354 acpi_device_hid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500355 error = -ENODEV;
356 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500359 error = acpi_button_add_fs(device);
360 if (error)
361 goto err_free_input;
362
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500363 snprintf(button->phys, sizeof(button->phys),
364 "%s/button/input0", acpi_device_hid(device));
365
366 input->name = acpi_device_name(device);
367 input->phys = button->phys;
368 input->id.bustype = BUS_HOST;
369 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800370 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500373 case ACPI_BUTTON_TYPE_POWER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 case ACPI_BUTTON_TYPE_POWERF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700375 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500376 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500378
379 case ACPI_BUTTON_TYPE_SLEEP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 case ACPI_BUTTON_TYPE_SLEEPF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700381 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500382 set_bit(KEY_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500384
385 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700386 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500387 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 break;
389 }
390
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500391 error = input_register_device(input);
392 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000393 goto err_remove_fs;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400394 if (button->type == ACPI_BUTTON_TYPE_LID)
395 acpi_lid_send_state(button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if (device->wakeup.flags.valid) {
398 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400399 acpi_set_gpe_type(device->wakeup.gpe_device,
400 device->wakeup.gpe_number,
401 ACPI_GPE_TYPE_WAKE_RUN);
402 acpi_enable_gpe(device->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400403 device->wakeup.gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 device->wakeup.state.enabled = 1;
405 }
406
Len Brown4be44fc2005-08-05 00:44:28 -0400407 printk(KERN_INFO PREFIX "%s [%s]\n",
408 acpi_device_name(device), acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500411 err_remove_fs:
412 acpi_button_remove_fs(device);
413 err_free_input:
414 input_free_device(input);
415 err_free_button:
416 kfree(button);
417 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Len Brown4be44fc2005-08-05 00:44:28 -0400420static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500422 struct acpi_button *button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 button = acpi_driver_data(device);
425
Len Brown4be44fc2005-08-05 00:44:28 -0400426 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500427 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Len Brown4be44fc2005-08-05 00:44:28 -0400432static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500434 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400436 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
437 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400438 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 result = acpi_bus_register_driver(&acpi_button_driver);
441 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400442 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400443 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Patrick Mocheld550d982006-06-27 00:41:40 -0400446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Len Brown4be44fc2005-08-05 00:44:28 -0400449static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 acpi_bus_unregister_driver(&acpi_button_driver);
452
Len Brown4be44fc2005-08-05 00:44:28 -0400453 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400454 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
455 if (acpi_sleep_dir)
456 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
457 if (acpi_lid_dir)
458 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
459 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462module_init(acpi_button_init);
463module_exit(acpi_button_exit);