blob: 76bbb78a5ad957525c066b90ec2457ae533934e6 [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 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,
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;
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
Jesse Barnes7e127152009-09-10 15:28:02 -0700119static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
120static struct acpi_device *lid_device;
121
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400122/* --------------------------------------------------------------------------
123 FS Interface (/proc)
124 -------------------------------------------------------------------------- */
125
Len Brown4be44fc2005-08-05 00:44:28 -0400126static struct proc_dir_entry *acpi_button_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400127
128static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
129{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000130 struct acpi_device *device = seq->private;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400131
Len Brown4be44fc2005-08-05 00:44:28 -0400132 seq_printf(seq, "type: %s\n",
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000133 acpi_device_name(device));
Patrick Mocheld550d982006-06-27 00:41:40 -0400134 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400135}
136
137static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
138{
139 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
140}
Len Brown4be44fc2005-08-05 00:44:28 -0400141
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400142static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
143{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000144 struct acpi_device *device = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400145 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400146 unsigned long long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400147
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000148 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500149 seq_printf(seq, "state: %s\n",
150 ACPI_FAILURE(status) ? "unsupported" :
151 (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400152 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400153}
154
155static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
156{
157 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
158}
159
160static struct proc_dir_entry *acpi_power_dir;
161static struct proc_dir_entry *acpi_sleep_dir;
162static struct proc_dir_entry *acpi_lid_dir;
163
Len Brown4be44fc2005-08-05 00:44:28 -0400164static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400165{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000166 struct acpi_button *button = acpi_driver_data(device);
Len Brown4be44fc2005-08-05 00:44:28 -0400167 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400168
169 switch (button->type) {
170 case ACPI_BUTTON_TYPE_POWER:
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400171 if (!acpi_power_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400172 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
173 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400174 entry = acpi_power_dir;
175 break;
176 case ACPI_BUTTON_TYPE_SLEEP:
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400177 if (!acpi_sleep_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400178 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
179 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400180 entry = acpi_sleep_dir;
181 break;
182 case ACPI_BUTTON_TYPE_LID:
183 if (!acpi_lid_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400184 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
185 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400186 entry = acpi_lid_dir;
187 break;
188 }
189
190 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400191 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400192
193 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
194 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400195 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400196
197 /* 'info' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700198 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
199 S_IRUGO, acpi_device_dir(device),
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000200 &acpi_button_info_fops, device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400201 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400202 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400203
204 /* show lid state [R] */
205 if (button->type == ACPI_BUTTON_TYPE_LID) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700206 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
207 S_IRUGO, acpi_device_dir(device),
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000208 &acpi_button_state_fops, device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400209 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400210 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400211 }
212
Patrick Mocheld550d982006-06-27 00:41:40 -0400213 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400214}
215
Len Brown4be44fc2005-08-05 00:44:28 -0400216static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400217{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500218 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400219
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400220 if (acpi_device_dir(device)) {
221 if (button->type == ACPI_BUTTON_TYPE_LID)
222 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400223 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400224 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400225 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400226
227 remove_proc_entry(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400228 acpi_device_dir(device)->parent);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400229 acpi_device_dir(device) = NULL;
230 }
231
Patrick Mocheld550d982006-06-27 00:41:40 -0400232 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/* --------------------------------------------------------------------------
236 Driver Interface
237 -------------------------------------------------------------------------- */
Jesse Barnes7e127152009-09-10 15:28:02 -0700238int acpi_lid_notifier_register(struct notifier_block *nb)
239{
240 return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
241}
242EXPORT_SYMBOL(acpi_lid_notifier_register);
243
244int acpi_lid_notifier_unregister(struct notifier_block *nb)
245{
246 return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
247}
248EXPORT_SYMBOL(acpi_lid_notifier_unregister);
249
250int acpi_lid_open(void)
251{
252 acpi_status status;
253 unsigned long long state;
254
Jesse Barnes2c907b72009-10-07 14:39:46 -0700255 if (!lid_device)
256 return -ENODEV;
257
Jesse Barnes7e127152009-09-10 15:28:02 -0700258 status = acpi_evaluate_integer(lid_device->handle, "_LID", NULL,
259 &state);
260 if (ACPI_FAILURE(status))
261 return -ENODEV;
262
263 return !!state;
264}
265EXPORT_SYMBOL(acpi_lid_open);
266
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000267static int acpi_lid_send_state(struct acpi_device *device)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400268{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000269 struct acpi_button *button = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400270 unsigned long long state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400271 acpi_status status;
Jesse Barnes7e127152009-09-10 15:28:02 -0700272 int ret;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400273
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000274 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400275 if (ACPI_FAILURE(status))
276 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000277
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400278 /* input layer checks if event is redundant */
279 input_report_switch(button->input, SW_LID, !state);
Guillem Joverdf316e92008-10-24 00:28:33 +0300280 input_sync(button->input);
Jesse Barnes7e127152009-09-10 15:28:02 -0700281
Rafael J. Wysocki1f835112011-01-06 23:36:01 +0100282 if (state)
283 pm_wakeup_event(&device->dev, 0);
284
Jesse Barnes7e127152009-09-10 15:28:02 -0700285 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
286 if (ret == NOTIFY_DONE)
287 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
288 device);
Zhao Yakui13c199c2009-12-15 22:01:57 +0800289 if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
290 /*
291 * It is also regarded as success if the notifier_chain
292 * returns NOTIFY_OK or NOTIFY_DONE.
293 */
294 ret = 0;
295 }
Jesse Barnes7e127152009-09-10 15:28:02 -0700296 return ret;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400297}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000299static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000301 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500302 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000305 case ACPI_FIXED_HARDWARE_EVENT:
306 event = ACPI_BUTTON_NOTIFY_STATUS;
307 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500309 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500310 if (button->type == ACPI_BUTTON_TYPE_LID) {
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000311 acpi_lid_send_state(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500312 } else {
313 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
314 KEY_SLEEP : KEY_POWER;
315
316 input_report_key(input, keycode, 1);
317 input_sync(input);
318 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300319 input_sync(input);
Rafael J. Wysocki1f835112011-01-06 23:36:01 +0100320
321 pm_wakeup_event(&device->dev, 0);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500322 }
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500323
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000324 acpi_bus_generate_proc_event(device, event, ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 break;
326 default:
327 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400328 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400333static int acpi_button_resume(struct acpi_device *device)
334{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000335 struct acpi_button *button = acpi_driver_data(device);
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000336
Bjorn Helgaase2fb9752009-04-08 15:39:43 +0000337 if (button->type == ACPI_BUTTON_TYPE_LID)
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000338 return acpi_lid_send_state(device);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400339 return 0;
340}
341
Len Brown4be44fc2005-08-05 00:44:28 -0400342static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500344 struct acpi_button *button;
345 struct input_dev *input;
Thomas Renninger620e1122010-10-01 10:54:00 +0200346 const char *hid = acpi_device_hid(device);
347 char *name, *class;
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000348 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500350 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400352 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700354 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500356 button->input = input = input_allocate_device();
357 if (!input) {
358 error = -ENOMEM;
359 goto err_free_button;
360 }
361
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000362 name = acpi_device_name(device);
363 class = acpi_device_class(device);
364
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000365 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
366 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 button->type = ACPI_BUTTON_TYPE_POWER;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000368 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
369 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000371 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
372 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 button->type = ACPI_BUTTON_TYPE_SLEEP;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000374 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
375 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000377 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 button->type = ACPI_BUTTON_TYPE_LID;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000379 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
380 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400382 } else {
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000383 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500384 error = -ENODEV;
385 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500388 error = acpi_button_add_fs(device);
389 if (error)
390 goto err_free_input;
391
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000392 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500393
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000394 input->name = name;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500395 input->phys = button->phys;
396 input->id.bustype = BUS_HOST;
397 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800398 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500401 case ACPI_BUTTON_TYPE_POWER:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700402 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500403 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500405
406 case ACPI_BUTTON_TYPE_SLEEP:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700407 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500408 set_bit(KEY_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500410
411 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700412 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500413 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 break;
415 }
416
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500417 error = input_register_device(input);
418 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000419 goto err_remove_fs;
Jesse Barnes7e127152009-09-10 15:28:02 -0700420 if (button->type == ACPI_BUTTON_TYPE_LID) {
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000421 acpi_lid_send_state(device);
Jesse Barnes7e127152009-09-10 15:28:02 -0700422 /*
423 * This assumes there's only one lid device, or if there are
424 * more we only care about the last one...
425 */
426 lid_device = device;
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if (device->wakeup.flags.valid) {
430 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400431 acpi_enable_gpe(device->wakeup.gpe_device,
Rafael J. Wysockia44061a2010-07-01 10:11:45 +0800432 device->wakeup.gpe_number);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100433 device->wakeup.run_wake_count++;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100434 device_set_wakeup_enable(&device->dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000437 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500440 err_remove_fs:
441 acpi_button_remove_fs(device);
442 err_free_input:
443 input_free_device(input);
444 err_free_button:
445 kfree(button);
446 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Len Brown4be44fc2005-08-05 00:44:28 -0400449static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000451 struct acpi_button *button = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +0100453 if (device->wakeup.flags.valid) {
454 acpi_disable_gpe(device->wakeup.gpe_device,
Rafael J. Wysockia44061a2010-07-01 10:11:45 +0800455 device->wakeup.gpe_number);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100456 device->wakeup.run_wake_count--;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100457 device_set_wakeup_enable(&device->dev, false);
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +0100458 }
459
Len Brown4be44fc2005-08-05 00:44:28 -0400460 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500461 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Len Brown4be44fc2005-08-05 00:44:28 -0400466static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500468 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400470 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
471 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400472 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 result = acpi_bus_register_driver(&acpi_button_driver);
475 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400476 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400477 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479
Patrick Mocheld550d982006-06-27 00:41:40 -0400480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Len Brown4be44fc2005-08-05 00:44:28 -0400483static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 acpi_bus_unregister_driver(&acpi_button_driver);
486
Len Brown4be44fc2005-08-05 00:44:28 -0400487 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400488 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
489 if (acpi_sleep_dir)
490 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
491 if (acpi_lid_dir)
492 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
493 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496module_init(acpi_button_init);
497module_exit(acpi_button_exit);