blob: 79d4c22f7a6d82c21e786b591f02f8bfe28b0f43 [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);
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000079static void acpi_button_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +020081static int acpi_button_resume(struct device *dev);
82static SIMPLE_DEV_PM_OPS(acpi_button_pm, NULL, acpi_button_resume);
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static 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,
90 .remove = acpi_button_remove,
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000091 .notify = acpi_button_notify,
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050092 },
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +020093 .drv.pm = &acpi_button_pm,
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
Jesse Barnes7e127152009-09-10 15:28:02 -0700104static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
105static struct acpi_device *lid_device;
106
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400107/* --------------------------------------------------------------------------
108 FS Interface (/proc)
109 -------------------------------------------------------------------------- */
110
Len Brown4be44fc2005-08-05 00:44:28 -0400111static struct proc_dir_entry *acpi_button_dir;
Zhang Rui912b7422011-03-23 10:21:40 +0800112static struct proc_dir_entry *acpi_lid_dir;
Len Brown4be44fc2005-08-05 00:44:28 -0400113
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400114static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
115{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000116 struct acpi_device *device = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400117 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400118 unsigned long long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400119
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000120 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500121 seq_printf(seq, "state: %s\n",
122 ACPI_FAILURE(status) ? "unsupported" :
123 (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400124 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400125}
126
127static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
128{
129 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
130}
131
Zhang Rui912b7422011-03-23 10:21:40 +0800132static const struct file_operations acpi_button_state_fops = {
133 .owner = THIS_MODULE,
134 .open = acpi_button_state_open_fs,
135 .read = seq_read,
136 .llseek = seq_lseek,
137 .release = single_release,
138};
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400139
Len Brown4be44fc2005-08-05 00:44:28 -0400140static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400141{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000142 struct acpi_button *button = acpi_driver_data(device);
Len Brown4be44fc2005-08-05 00:44:28 -0400143 struct proc_dir_entry *entry = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800144 int ret = 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400145
Zhang Rui912b7422011-03-23 10:21:40 +0800146 /* procfs I/F for ACPI lid device only */
147 if (button->type != ACPI_BUTTON_TYPE_LID)
148 return 0;
149
150 if (acpi_button_dir || acpi_lid_dir) {
151 printk(KERN_ERR PREFIX "More than one Lid device found!\n");
152 return -EEXIST;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400153 }
154
Zhang Rui912b7422011-03-23 10:21:40 +0800155 /* create /proc/acpi/button */
156 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
157 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400158 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400159
Zhang Rui912b7422011-03-23 10:21:40 +0800160 /* create /proc/acpi/button/lid */
161 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
162 if (!acpi_lid_dir) {
163 ret = -ENODEV;
164 goto remove_button_dir;
165 }
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400166
Zhang Rui912b7422011-03-23 10:21:40 +0800167 /* create /proc/acpi/button/lid/LID/ */
168 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
169 if (!acpi_device_dir(device)) {
170 ret = -ENODEV;
171 goto remove_lid_dir;
172 }
173
174 /* create /proc/acpi/button/lid/LID/state */
175 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700176 S_IRUGO, acpi_device_dir(device),
Zhang Rui912b7422011-03-23 10:21:40 +0800177 &acpi_button_state_fops, device);
178 if (!entry) {
179 ret = -ENODEV;
180 goto remove_dev_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400181 }
182
Zhang Rui912b7422011-03-23 10:21:40 +0800183done:
184 return ret;
185
186remove_dev_dir:
187 remove_proc_entry(acpi_device_bid(device),
188 acpi_lid_dir);
189 acpi_device_dir(device) = NULL;
190remove_lid_dir:
191 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
192remove_button_dir:
193 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
194 goto done;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400195}
196
Len Brown4be44fc2005-08-05 00:44:28 -0400197static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400198{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500199 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400200
Zhang Rui912b7422011-03-23 10:21:40 +0800201 if (button->type != ACPI_BUTTON_TYPE_LID)
202 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400203
Zhang Rui912b7422011-03-23 10:21:40 +0800204 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
205 acpi_device_dir(device));
206 remove_proc_entry(acpi_device_bid(device),
207 acpi_lid_dir);
208 acpi_device_dir(device) = NULL;
209 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
210 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400211
Patrick Mocheld550d982006-06-27 00:41:40 -0400212 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/* --------------------------------------------------------------------------
216 Driver Interface
217 -------------------------------------------------------------------------- */
Jesse Barnes7e127152009-09-10 15:28:02 -0700218int acpi_lid_notifier_register(struct notifier_block *nb)
219{
220 return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
221}
222EXPORT_SYMBOL(acpi_lid_notifier_register);
223
224int acpi_lid_notifier_unregister(struct notifier_block *nb)
225{
226 return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
227}
228EXPORT_SYMBOL(acpi_lid_notifier_unregister);
229
230int acpi_lid_open(void)
231{
232 acpi_status status;
233 unsigned long long state;
234
Jesse Barnes2c907b72009-10-07 14:39:46 -0700235 if (!lid_device)
236 return -ENODEV;
237
Jesse Barnes7e127152009-09-10 15:28:02 -0700238 status = acpi_evaluate_integer(lid_device->handle, "_LID", NULL,
239 &state);
240 if (ACPI_FAILURE(status))
241 return -ENODEV;
242
243 return !!state;
244}
245EXPORT_SYMBOL(acpi_lid_open);
246
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000247static int acpi_lid_send_state(struct acpi_device *device)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400248{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000249 struct acpi_button *button = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400250 unsigned long long state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400251 acpi_status status;
Jesse Barnes7e127152009-09-10 15:28:02 -0700252 int ret;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400253
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000254 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400255 if (ACPI_FAILURE(status))
256 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000257
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400258 /* input layer checks if event is redundant */
259 input_report_switch(button->input, SW_LID, !state);
Guillem Joverdf316e92008-10-24 00:28:33 +0300260 input_sync(button->input);
Jesse Barnes7e127152009-09-10 15:28:02 -0700261
Rafael J. Wysocki1f835112011-01-06 23:36:01 +0100262 if (state)
263 pm_wakeup_event(&device->dev, 0);
264
Jesse Barnes7e127152009-09-10 15:28:02 -0700265 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
266 if (ret == NOTIFY_DONE)
267 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
268 device);
Zhao Yakui13c199c2009-12-15 22:01:57 +0800269 if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
270 /*
271 * It is also regarded as success if the notifier_chain
272 * returns NOTIFY_OK or NOTIFY_DONE.
273 */
274 ret = 0;
275 }
Jesse Barnes7e127152009-09-10 15:28:02 -0700276 return ret;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000279static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000281 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500282 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000285 case ACPI_FIXED_HARDWARE_EVENT:
286 event = ACPI_BUTTON_NOTIFY_STATUS;
287 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500289 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500290 if (button->type == ACPI_BUTTON_TYPE_LID) {
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000291 acpi_lid_send_state(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500292 } else {
293 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
294 KEY_SLEEP : KEY_POWER;
295
296 input_report_key(input, keycode, 1);
297 input_sync(input);
298 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300299 input_sync(input);
Rafael J. Wysocki1f835112011-01-06 23:36:01 +0100300
301 pm_wakeup_event(&device->dev, 0);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500302 }
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500303
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000304 acpi_bus_generate_proc_event(device, event, ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 break;
306 default:
307 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400308 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 break;
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200313static int acpi_button_resume(struct device *dev)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400314{
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200315 struct acpi_device *device = to_acpi_device(dev);
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000316 struct acpi_button *button = acpi_driver_data(device);
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000317
Bjorn Helgaase2fb9752009-04-08 15:39:43 +0000318 if (button->type == ACPI_BUTTON_TYPE_LID)
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000319 return acpi_lid_send_state(device);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400320 return 0;
321}
322
Len Brown4be44fc2005-08-05 00:44:28 -0400323static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500325 struct acpi_button *button;
326 struct input_dev *input;
Thomas Renninger620e1122010-10-01 10:54:00 +0200327 const char *hid = acpi_device_hid(device);
328 char *name, *class;
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000329 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500331 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400333 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700335 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500337 button->input = input = input_allocate_device();
338 if (!input) {
339 error = -ENOMEM;
340 goto err_free_button;
341 }
342
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000343 name = acpi_device_name(device);
344 class = acpi_device_class(device);
345
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000346 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
347 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 button->type = ACPI_BUTTON_TYPE_POWER;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000349 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
350 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000352 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
353 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 button->type = ACPI_BUTTON_TYPE_SLEEP;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000355 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
356 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000358 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 button->type = ACPI_BUTTON_TYPE_LID;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000360 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
361 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400363 } else {
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000364 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500365 error = -ENODEV;
366 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500369 error = acpi_button_add_fs(device);
370 if (error)
371 goto err_free_input;
372
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000373 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500374
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000375 input->name = name;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500376 input->phys = button->phys;
377 input->id.bustype = BUS_HOST;
378 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800379 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500382 case ACPI_BUTTON_TYPE_POWER:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700383 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500384 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500386
387 case ACPI_BUTTON_TYPE_SLEEP:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700388 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500389 set_bit(KEY_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500391
392 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700393 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500394 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 break;
396 }
397
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500398 error = input_register_device(input);
399 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000400 goto err_remove_fs;
Jesse Barnes7e127152009-09-10 15:28:02 -0700401 if (button->type == ACPI_BUTTON_TYPE_LID) {
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000402 acpi_lid_send_state(device);
Jesse Barnes7e127152009-09-10 15:28:02 -0700403 /*
404 * This assumes there's only one lid device, or if there are
405 * more we only care about the last one...
406 */
407 lid_device = device;
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (device->wakeup.flags.valid) {
411 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400412 acpi_enable_gpe(device->wakeup.gpe_device,
Rafael J. Wysockia44061a2010-07-01 10:11:45 +0800413 device->wakeup.gpe_number);
Rafael J. Wysockic19f9a82011-02-08 23:41:13 +0100414 if (!device_may_wakeup(&device->dev)) {
415 device_set_wakeup_enable(&device->dev, true);
416 button->wakeup_enabled = true;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000420 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500421 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500423 err_remove_fs:
424 acpi_button_remove_fs(device);
425 err_free_input:
426 input_free_device(input);
427 err_free_button:
428 kfree(button);
429 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Len Brown4be44fc2005-08-05 00:44:28 -0400432static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000434 struct acpi_button *button = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +0100436 if (device->wakeup.flags.valid) {
437 acpi_disable_gpe(device->wakeup.gpe_device,
Rafael J. Wysockia44061a2010-07-01 10:11:45 +0800438 device->wakeup.gpe_number);
Rafael J. Wysockic19f9a82011-02-08 23:41:13 +0100439 if (button->wakeup_enabled)
440 device_set_wakeup_enable(&device->dev, false);
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +0100441 }
442
Len Brown4be44fc2005-08-05 00:44:28 -0400443 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500444 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kfree(button);
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 int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Zhang Rui912b7422011-03-23 10:21:40 +0800451 return acpi_bus_register_driver(&acpi_button_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Len Brown4be44fc2005-08-05 00:44:28 -0400454static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 acpi_bus_unregister_driver(&acpi_button_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459module_init(acpi_button_init);
460module_exit(acpi_button_exit);