blob: d295bdccc09ccc4c8c459dfeb2a10be64ec2f019 [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
Len Browna192a952009-07-28 16:45:54 -040036#define PREFIX "ACPI: "
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define ACPI_BUTTON_CLASS "button"
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040039#define ACPI_BUTTON_FILE_INFO "info"
40#define ACPI_BUTTON_FILE_STATE "state"
41#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define ACPI_BUTTON_NOTIFY_STATUS 0x80
43
44#define ACPI_BUTTON_SUBCLASS_POWER "power"
Len Brown4be44fc2005-08-05 00:44:28 -040045#define ACPI_BUTTON_HID_POWER "PNP0C0C"
Bjorn Helgaasd68b5972009-04-08 15:40:04 +000046#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define ACPI_BUTTON_TYPE_POWER 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
50#define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
Bjorn Helgaasd68b5972009-04-08 15:40:04 +000051#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define ACPI_BUTTON_TYPE_SLEEP 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#define ACPI_BUTTON_SUBCLASS_LID "lid"
55#define ACPI_BUTTON_HID_LID "PNP0C0D"
56#define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
57#define ACPI_BUTTON_TYPE_LID 0x05
58
59#define _COMPONENT ACPI_BUTTON_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050060ACPI_MODULE_NAME("button");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050062MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050063MODULE_DESCRIPTION("ACPI Button Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070064MODULE_LICENSE("GPL");
65
Thomas Renninger1ba90e32007-07-23 14:44:41 +020066static const struct acpi_device_id button_device_ids[] = {
67 {ACPI_BUTTON_HID_LID, 0},
68 {ACPI_BUTTON_HID_SLEEP, 0},
69 {ACPI_BUTTON_HID_SLEEPF, 0},
70 {ACPI_BUTTON_HID_POWER, 0},
71 {ACPI_BUTTON_HID_POWERF, 0},
72 {"", 0},
73};
74MODULE_DEVICE_TABLE(acpi, button_device_ids);
75
Len Brown4be44fc2005-08-05 00:44:28 -040076static int acpi_button_add(struct acpi_device *device);
77static int acpi_button_remove(struct acpi_device *device, int type);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +040078static int acpi_button_resume(struct acpi_device *device);
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000079static void acpi_button_notify(struct acpi_device *device, u32 event);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040080static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
81static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static struct acpi_driver acpi_button_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050084 .name = "button",
Len Brown4be44fc2005-08-05 00:44:28 -040085 .class = ACPI_BUTTON_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020086 .ids = button_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040087 .ops = {
88 .add = acpi_button_add,
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +040089 .resume = acpi_button_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040090 .remove = acpi_button_remove,
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000091 .notify = acpi_button_notify,
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050092 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95struct acpi_button {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050096 unsigned int type;
97 struct input_dev *input;
98 char phys[32]; /* for input device */
Len Brown4be44fc2005-08-05 00:44:28 -040099 unsigned long pushed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Arjan van de Vend7508032006-07-04 13:06:00 -0400102static const struct file_operations acpi_button_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700103 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400104 .open = acpi_button_info_open_fs,
105 .read = seq_read,
106 .llseek = seq_lseek,
107 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400108};
109
Arjan van de Vend7508032006-07-04 13:06:00 -0400110static const struct file_operations acpi_button_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700111 .owner = THIS_MODULE,
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{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000126 struct acpi_device *device = seq->private;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400127
Len Brown4be44fc2005-08-05 00:44:28 -0400128 seq_printf(seq, "type: %s\n",
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000129 acpi_device_name(device));
Patrick Mocheld550d982006-06-27 00:41:40 -0400130 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400131}
132
133static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
134{
135 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
136}
Len Brown4be44fc2005-08-05 00:44:28 -0400137
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400138static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
139{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000140 struct acpi_device *device = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400141 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400142 unsigned long long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400143
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000144 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500145 seq_printf(seq, "state: %s\n",
146 ACPI_FAILURE(status) ? "unsupported" :
147 (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400148 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400149}
150
151static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
152{
153 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
154}
155
156static struct proc_dir_entry *acpi_power_dir;
157static struct proc_dir_entry *acpi_sleep_dir;
158static struct proc_dir_entry *acpi_lid_dir;
159
Len Brown4be44fc2005-08-05 00:44:28 -0400160static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400161{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000162 struct acpi_button *button = acpi_driver_data(device);
Len Brown4be44fc2005-08-05 00:44:28 -0400163 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400164
165 switch (button->type) {
166 case ACPI_BUTTON_TYPE_POWER:
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400167 if (!acpi_power_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400168 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
169 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400170 entry = acpi_power_dir;
171 break;
172 case ACPI_BUTTON_TYPE_SLEEP:
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400173 if (!acpi_sleep_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400174 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
175 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400176 entry = acpi_sleep_dir;
177 break;
178 case ACPI_BUTTON_TYPE_LID:
179 if (!acpi_lid_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400180 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
181 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400182 entry = acpi_lid_dir;
183 break;
184 }
185
186 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400187 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400188
189 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
190 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400191 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400192
193 /* 'info' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700194 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
195 S_IRUGO, acpi_device_dir(device),
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000196 &acpi_button_info_fops, device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400197 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400198 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400199
200 /* show lid state [R] */
201 if (button->type == ACPI_BUTTON_TYPE_LID) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700202 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
203 S_IRUGO, acpi_device_dir(device),
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000204 &acpi_button_state_fops, device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400205 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400206 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400207 }
208
Patrick Mocheld550d982006-06-27 00:41:40 -0400209 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400210}
211
Len Brown4be44fc2005-08-05 00:44:28 -0400212static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400213{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500214 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400215
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400216 if (acpi_device_dir(device)) {
217 if (button->type == ACPI_BUTTON_TYPE_LID)
218 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400219 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400220 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400221 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400222
223 remove_proc_entry(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400224 acpi_device_dir(device)->parent);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400225 acpi_device_dir(device) = NULL;
226 }
227
Patrick Mocheld550d982006-06-27 00:41:40 -0400228 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/* --------------------------------------------------------------------------
232 Driver Interface
233 -------------------------------------------------------------------------- */
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000234static int acpi_lid_send_state(struct acpi_device *device)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400235{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000236 struct acpi_button *button = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400237 unsigned long long state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400238 acpi_status status;
239
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000240 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400241 if (ACPI_FAILURE(status))
242 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000243
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400244 /* input layer checks if event is redundant */
245 input_report_switch(button->input, SW_LID, !state);
Guillem Joverdf316e92008-10-24 00:28:33 +0300246 input_sync(button->input);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400247 return 0;
248}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000250static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000252 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500253 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000256 case ACPI_FIXED_HARDWARE_EVENT:
257 event = ACPI_BUTTON_NOTIFY_STATUS;
258 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500260 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500261 if (button->type == ACPI_BUTTON_TYPE_LID) {
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000262 acpi_lid_send_state(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500263 } else {
264 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
265 KEY_SLEEP : KEY_POWER;
266
267 input_report_key(input, keycode, 1);
268 input_sync(input);
269 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300270 input_sync(input);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500271 }
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500272
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000273 acpi_bus_generate_proc_event(device, event, ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
275 default:
276 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400277 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400282static int acpi_button_resume(struct acpi_device *device)
283{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000284 struct acpi_button *button = acpi_driver_data(device);
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000285
Bjorn Helgaase2fb9752009-04-08 15:39:43 +0000286 if (button->type == ACPI_BUTTON_TYPE_LID)
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000287 return acpi_lid_send_state(device);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400288 return 0;
289}
290
Len Brown4be44fc2005-08-05 00:44:28 -0400291static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500293 struct acpi_button *button;
294 struct input_dev *input;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000295 char *hid, *name, *class;
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000296 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500298 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400300 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700302 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500304 button->input = input = input_allocate_device();
305 if (!input) {
306 error = -ENOMEM;
307 goto err_free_button;
308 }
309
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000310 hid = acpi_device_hid(device);
311 name = acpi_device_name(device);
312 class = acpi_device_class(device);
313
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000314 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
315 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 button->type = ACPI_BUTTON_TYPE_POWER;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000317 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
318 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000320 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
321 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 button->type = ACPI_BUTTON_TYPE_SLEEP;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000323 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
324 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000326 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 button->type = ACPI_BUTTON_TYPE_LID;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000328 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
329 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400331 } else {
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000332 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500333 error = -ENODEV;
334 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500337 error = acpi_button_add_fs(device);
338 if (error)
339 goto err_free_input;
340
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000341 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500342
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000343 input->name = name;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500344 input->phys = button->phys;
345 input->id.bustype = BUS_HOST;
346 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800347 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500350 case ACPI_BUTTON_TYPE_POWER:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700351 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500352 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500354
355 case ACPI_BUTTON_TYPE_SLEEP:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700356 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500357 set_bit(KEY_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500359
360 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700361 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500362 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 }
365
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500366 error = input_register_device(input);
367 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000368 goto err_remove_fs;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400369 if (button->type == ACPI_BUTTON_TYPE_LID)
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000370 acpi_lid_send_state(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 if (device->wakeup.flags.valid) {
373 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400374 acpi_set_gpe_type(device->wakeup.gpe_device,
375 device->wakeup.gpe_number,
376 ACPI_GPE_TYPE_WAKE_RUN);
377 acpi_enable_gpe(device->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400378 device->wakeup.gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 device->wakeup.state.enabled = 1;
380 }
381
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000382 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500385 err_remove_fs:
386 acpi_button_remove_fs(device);
387 err_free_input:
388 input_free_device(input);
389 err_free_button:
390 kfree(button);
391 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Len Brown4be44fc2005-08-05 00:44:28 -0400394static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000396 struct acpi_button *button = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Len Brown4be44fc2005-08-05 00:44:28 -0400398 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500399 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Len Brown4be44fc2005-08-05 00:44:28 -0400404static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500406 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400408 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
409 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400410 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 result = acpi_bus_register_driver(&acpi_button_driver);
413 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400414 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400415 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
Patrick Mocheld550d982006-06-27 00:41:40 -0400418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Len Brown4be44fc2005-08-05 00:44:28 -0400421static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 acpi_bus_unregister_driver(&acpi_button_driver);
424
Len Brown4be44fc2005-08-05 00:44:28 -0400425 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400426 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
427 if (acpi_sleep_dir)
428 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
429 if (acpi_lid_dir)
430 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
431 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434module_init(acpi_button_init);
435module_exit(acpi_button_exit);