blob: 12c28f4adb671aff43f9e86c42f7fe89263e73c3 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <acpi/acpi_bus.h>
35#include <acpi/acpi_drivers.h>
36
Len Browna192a952009-07-28 16:45:54 -040037#define PREFIX "ACPI: "
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define ACPI_BUTTON_CLASS "button"
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040040#define ACPI_BUTTON_FILE_INFO "info"
41#define ACPI_BUTTON_FILE_STATE "state"
42#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define ACPI_BUTTON_NOTIFY_STATUS 0x80
44
45#define ACPI_BUTTON_SUBCLASS_POWER "power"
Len Brown4be44fc2005-08-05 00:44:28 -040046#define ACPI_BUTTON_HID_POWER "PNP0C0C"
Bjorn Helgaasd68b5972009-04-08 15:40:04 +000047#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#define ACPI_BUTTON_TYPE_POWER 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
51#define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
Bjorn Helgaasd68b5972009-04-08 15:40:04 +000052#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_BUTTON_TYPE_SLEEP 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define ACPI_BUTTON_SUBCLASS_LID "lid"
56#define ACPI_BUTTON_HID_LID "PNP0C0D"
57#define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
58#define ACPI_BUTTON_TYPE_LID 0x05
59
60#define _COMPONENT ACPI_BUTTON_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050061ACPI_MODULE_NAME("button");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050063MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050064MODULE_DESCRIPTION("ACPI Button Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065MODULE_LICENSE("GPL");
66
Thomas Renninger1ba90e32007-07-23 14:44:41 +020067static const struct acpi_device_id button_device_ids[] = {
68 {ACPI_BUTTON_HID_LID, 0},
69 {ACPI_BUTTON_HID_SLEEP, 0},
70 {ACPI_BUTTON_HID_SLEEPF, 0},
71 {ACPI_BUTTON_HID_POWER, 0},
72 {ACPI_BUTTON_HID_POWERF, 0},
73 {"", 0},
74};
75MODULE_DEVICE_TABLE(acpi, button_device_ids);
76
Len Brown4be44fc2005-08-05 00:44:28 -040077static int acpi_button_add(struct acpi_device *device);
78static int acpi_button_remove(struct acpi_device *device, int type);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +040079static int acpi_button_resume(struct acpi_device *device);
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000080static void acpi_button_notify(struct acpi_device *device, u32 event);
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 Brownc2b67052007-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,
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000092 .notify = acpi_button_notify,
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050093 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
95
96struct acpi_button {
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;
Rafael J. Wysockic19f9a82011-02-08 23:41:13 +0100101 bool wakeup_enabled;
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
Jesse Barnes7e127152009-09-10 15:28:02 -0700120static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
121static struct acpi_device *lid_device;
122
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400123/* --------------------------------------------------------------------------
124 FS Interface (/proc)
125 -------------------------------------------------------------------------- */
126
Len Brown4be44fc2005-08-05 00:44:28 -0400127static struct proc_dir_entry *acpi_button_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400128
129static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
130{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000131 struct acpi_device *device = seq->private;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400132
Len Brown4be44fc2005-08-05 00:44:28 -0400133 seq_printf(seq, "type: %s\n",
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000134 acpi_device_name(device));
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{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000145 struct acpi_device *device = 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
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000149 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500150 seq_printf(seq, "state: %s\n",
151 ACPI_FAILURE(status) ? "unsupported" :
152 (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400153 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400154}
155
156static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
157{
158 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
159}
160
161static struct proc_dir_entry *acpi_power_dir;
162static struct proc_dir_entry *acpi_sleep_dir;
163static struct proc_dir_entry *acpi_lid_dir;
164
Len Brown4be44fc2005-08-05 00:44:28 -0400165static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400166{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000167 struct acpi_button *button = acpi_driver_data(device);
Len Brown4be44fc2005-08-05 00:44:28 -0400168 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400169
170 switch (button->type) {
171 case ACPI_BUTTON_TYPE_POWER:
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400172 if (!acpi_power_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400173 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
174 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400175 entry = acpi_power_dir;
176 break;
177 case ACPI_BUTTON_TYPE_SLEEP:
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400178 if (!acpi_sleep_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400179 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
180 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400181 entry = acpi_sleep_dir;
182 break;
183 case ACPI_BUTTON_TYPE_LID:
184 if (!acpi_lid_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400185 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
186 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400187 entry = acpi_lid_dir;
188 break;
189 }
190
191 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400192 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400193
194 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
195 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400196 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400197
198 /* 'info' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700199 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
200 S_IRUGO, acpi_device_dir(device),
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000201 &acpi_button_info_fops, device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400202 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400203 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400204
205 /* show lid state [R] */
206 if (button->type == ACPI_BUTTON_TYPE_LID) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700207 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
208 S_IRUGO, acpi_device_dir(device),
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000209 &acpi_button_state_fops, device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400210 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400211 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400212 }
213
Patrick Mocheld550d982006-06-27 00:41:40 -0400214 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400215}
216
Len Brown4be44fc2005-08-05 00:44:28 -0400217static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400218{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500219 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400220
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400221 if (acpi_device_dir(device)) {
222 if (button->type == ACPI_BUTTON_TYPE_LID)
223 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400224 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400225 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400226 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400227
228 remove_proc_entry(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400229 acpi_device_dir(device)->parent);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400230 acpi_device_dir(device) = NULL;
231 }
232
Patrick Mocheld550d982006-06-27 00:41:40 -0400233 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/* --------------------------------------------------------------------------
237 Driver Interface
238 -------------------------------------------------------------------------- */
Jesse Barnes7e127152009-09-10 15:28:02 -0700239int acpi_lid_notifier_register(struct notifier_block *nb)
240{
241 return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
242}
243EXPORT_SYMBOL(acpi_lid_notifier_register);
244
245int acpi_lid_notifier_unregister(struct notifier_block *nb)
246{
247 return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
248}
249EXPORT_SYMBOL(acpi_lid_notifier_unregister);
250
251int acpi_lid_open(void)
252{
253 acpi_status status;
254 unsigned long long state;
255
Jesse Barnes2c907b72009-10-07 14:39:46 -0700256 if (!lid_device)
257 return -ENODEV;
258
Jesse Barnes7e127152009-09-10 15:28:02 -0700259 status = acpi_evaluate_integer(lid_device->handle, "_LID", NULL,
260 &state);
261 if (ACPI_FAILURE(status))
262 return -ENODEV;
263
264 return !!state;
265}
266EXPORT_SYMBOL(acpi_lid_open);
267
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000268static int acpi_lid_send_state(struct acpi_device *device)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400269{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000270 struct acpi_button *button = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400271 unsigned long long state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400272 acpi_status status;
Jesse Barnes7e127152009-09-10 15:28:02 -0700273 int ret;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400274
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000275 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400276 if (ACPI_FAILURE(status))
277 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000278
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400279 /* input layer checks if event is redundant */
280 input_report_switch(button->input, SW_LID, !state);
Guillem Joverdf316e92008-10-24 00:28:33 +0300281 input_sync(button->input);
Jesse Barnes7e127152009-09-10 15:28:02 -0700282
Rafael J. Wysocki1f835112011-01-06 23:36:01 +0100283 if (state)
284 pm_wakeup_event(&device->dev, 0);
285
Jesse Barnes7e127152009-09-10 15:28:02 -0700286 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
287 if (ret == NOTIFY_DONE)
288 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
289 device);
Zhao Yakui13c199c2009-12-15 22:01:57 +0800290 if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
291 /*
292 * It is also regarded as success if the notifier_chain
293 * returns NOTIFY_OK or NOTIFY_DONE.
294 */
295 ret = 0;
296 }
Jesse Barnes7e127152009-09-10 15:28:02 -0700297 return ret;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400298}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000300static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000302 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500303 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000306 case ACPI_FIXED_HARDWARE_EVENT:
307 event = ACPI_BUTTON_NOTIFY_STATUS;
308 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500310 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500311 if (button->type == ACPI_BUTTON_TYPE_LID) {
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000312 acpi_lid_send_state(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500313 } else {
314 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
315 KEY_SLEEP : KEY_POWER;
316
317 input_report_key(input, keycode, 1);
318 input_sync(input);
319 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300320 input_sync(input);
Rafael J. Wysocki1f835112011-01-06 23:36:01 +0100321
322 pm_wakeup_event(&device->dev, 0);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500323 }
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500324
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000325 acpi_bus_generate_proc_event(device, event, ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 break;
327 default:
328 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400329 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 break;
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400334static int acpi_button_resume(struct acpi_device *device)
335{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000336 struct acpi_button *button = acpi_driver_data(device);
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000337
Bjorn Helgaase2fb9752009-04-08 15:39:43 +0000338 if (button->type == ACPI_BUTTON_TYPE_LID)
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000339 return acpi_lid_send_state(device);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400340 return 0;
341}
342
Len Brown4be44fc2005-08-05 00:44:28 -0400343static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500345 struct acpi_button *button;
346 struct input_dev *input;
Thomas Renninger620e1122010-10-01 10:54:00 +0200347 const char *hid = acpi_device_hid(device);
348 char *name, *class;
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000349 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500351 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400353 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700355 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500357 button->input = input = input_allocate_device();
358 if (!input) {
359 error = -ENOMEM;
360 goto err_free_button;
361 }
362
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000363 name = acpi_device_name(device);
364 class = acpi_device_class(device);
365
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000366 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
367 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 button->type = ACPI_BUTTON_TYPE_POWER;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000369 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
370 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000372 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
373 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 button->type = ACPI_BUTTON_TYPE_SLEEP;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000375 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
376 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000378 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 button->type = ACPI_BUTTON_TYPE_LID;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000380 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
381 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400383 } else {
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000384 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500385 error = -ENODEV;
386 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500389 error = acpi_button_add_fs(device);
390 if (error)
391 goto err_free_input;
392
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000393 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500394
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000395 input->name = name;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500396 input->phys = button->phys;
397 input->id.bustype = BUS_HOST;
398 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800399 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500402 case ACPI_BUTTON_TYPE_POWER:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700403 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500404 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500406
407 case ACPI_BUTTON_TYPE_SLEEP:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700408 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500409 set_bit(KEY_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500411
412 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700413 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500414 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
416 }
417
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500418 error = input_register_device(input);
419 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000420 goto err_remove_fs;
Jesse Barnes7e127152009-09-10 15:28:02 -0700421 if (button->type == ACPI_BUTTON_TYPE_LID) {
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000422 acpi_lid_send_state(device);
Jesse Barnes7e127152009-09-10 15:28:02 -0700423 /*
424 * This assumes there's only one lid device, or if there are
425 * more we only care about the last one...
426 */
427 lid_device = device;
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (device->wakeup.flags.valid) {
431 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400432 acpi_enable_gpe(device->wakeup.gpe_device,
Rafael J. Wysockia44061a2010-07-01 10:11:45 +0800433 device->wakeup.gpe_number);
Rafael J. Wysockic19f9a82011-02-08 23:41:13 +0100434 if (!device_may_wakeup(&device->dev)) {
435 device_set_wakeup_enable(&device->dev, true);
436 button->wakeup_enabled = true;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000440 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500443 err_remove_fs:
444 acpi_button_remove_fs(device);
445 err_free_input:
446 input_free_device(input);
447 err_free_button:
448 kfree(button);
449 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Len Brown4be44fc2005-08-05 00:44:28 -0400452static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000454 struct acpi_button *button = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +0100456 if (device->wakeup.flags.valid) {
457 acpi_disable_gpe(device->wakeup.gpe_device,
Rafael J. Wysockia44061a2010-07-01 10:11:45 +0800458 device->wakeup.gpe_number);
Rafael J. Wysockic19f9a82011-02-08 23:41:13 +0100459 if (button->wakeup_enabled)
460 device_set_wakeup_enable(&device->dev, false);
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +0100461 }
462
Len Brown4be44fc2005-08-05 00:44:28 -0400463 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500464 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Len Brown4be44fc2005-08-05 00:44:28 -0400469static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500471 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400473 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
474 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400475 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 result = acpi_bus_register_driver(&acpi_button_driver);
478 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400479 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400480 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482
Patrick Mocheld550d982006-06-27 00:41:40 -0400483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Len Brown4be44fc2005-08-05 00:44:28 -0400486static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 acpi_bus_unregister_driver(&acpi_button_driver);
489
Len Brown4be44fc2005-08-05 00:44:28 -0400490 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400491 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
492 if (acpi_sleep_dir)
493 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
494 if (acpi_lid_dir)
495 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
496 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499module_init(acpi_button_init);
500module_exit(acpi_button_exit);