blob: 6e291c17e43a81be327ae1cb53457c24e4ca7e18 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/init.h>
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040025#include <linux/types.h>
26#include <linux/proc_fs.h>
27#include <linux/seq_file.h>
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050028#include <linux/input.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Lv Zheng8b484632013-12-03 08:49:16 +080030#include <linux/acpi.h>
Andy Shevchenko6270da62013-03-11 09:17:04 +000031#include <acpi/button.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Len Browna192a952009-07-28 16:45:54 -040033#define PREFIX "ACPI: "
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define ACPI_BUTTON_CLASS "button"
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040036#define ACPI_BUTTON_FILE_INFO "info"
37#define ACPI_BUTTON_FILE_STATE "state"
38#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define ACPI_BUTTON_NOTIFY_STATUS 0x80
40
41#define ACPI_BUTTON_SUBCLASS_POWER "power"
Len Brown4be44fc2005-08-05 00:44:28 -040042#define ACPI_BUTTON_HID_POWER "PNP0C0C"
Bjorn Helgaasd68b5972009-04-08 15:40:04 +000043#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_BUTTON_TYPE_POWER 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
47#define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
Bjorn Helgaasd68b5972009-04-08 15:40:04 +000048#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define ACPI_BUTTON_TYPE_SLEEP 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define ACPI_BUTTON_SUBCLASS_LID "lid"
52#define ACPI_BUTTON_HID_LID "PNP0C0D"
53#define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
54#define ACPI_BUTTON_TYPE_LID 0x05
55
56#define _COMPONENT ACPI_BUTTON_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050057ACPI_MODULE_NAME("button");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050059MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050060MODULE_DESCRIPTION("ACPI Button Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061MODULE_LICENSE("GPL");
62
Thomas Renninger1ba90e32007-07-23 14:44:41 +020063static const struct acpi_device_id button_device_ids[] = {
64 {ACPI_BUTTON_HID_LID, 0},
65 {ACPI_BUTTON_HID_SLEEP, 0},
66 {ACPI_BUTTON_HID_SLEEPF, 0},
67 {ACPI_BUTTON_HID_POWER, 0},
68 {ACPI_BUTTON_HID_POWERF, 0},
69 {"", 0},
70};
71MODULE_DEVICE_TABLE(acpi, button_device_ids);
72
Len Brown4be44fc2005-08-05 00:44:28 -040073static int acpi_button_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010074static int acpi_button_remove(struct acpi_device *device);
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000075static void acpi_button_notify(struct acpi_device *device, u32 event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Rafael J. Wysocki90692402012-08-09 23:00:02 +020077#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +020078static int acpi_button_suspend(struct device *dev);
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +020079static int acpi_button_resume(struct device *dev);
Shuah Khan2de9fd12014-02-12 20:19:07 -070080#else
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +020081#define acpi_button_suspend NULL
Shuah Khan2de9fd12014-02-12 20:19:07 -070082#define acpi_button_resume NULL
Rafael J. Wysocki90692402012-08-09 23:00:02 +020083#endif
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +020084static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume);
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +020085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static struct acpi_driver acpi_button_driver = {
Len Brownc2b67052007-02-12 23:33:40 -050087 .name = "button",
Len Brown4be44fc2005-08-05 00:44:28 -040088 .class = ACPI_BUTTON_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020089 .ids = button_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040090 .ops = {
91 .add = acpi_button_add,
92 .remove = acpi_button_remove,
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000093 .notify = acpi_button_notify,
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050094 },
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +020095 .drv.pm = &acpi_button_pm,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98struct acpi_button {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050099 unsigned int type;
100 struct input_dev *input;
101 char phys[32]; /* for input device */
Len Brown4be44fc2005-08-05 00:44:28 -0400102 unsigned long pushed;
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200103 bool suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
Jesse Barnes7e127152009-09-10 15:28:02 -0700106static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier);
107static struct acpi_device *lid_device;
108
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400109/* --------------------------------------------------------------------------
110 FS Interface (/proc)
111 -------------------------------------------------------------------------- */
112
Len Brown4be44fc2005-08-05 00:44:28 -0400113static struct proc_dir_entry *acpi_button_dir;
Zhang Rui912b7422011-03-23 10:21:40 +0800114static struct proc_dir_entry *acpi_lid_dir;
Len Brown4be44fc2005-08-05 00:44:28 -0400115
Lv Zhengee7e2262016-06-01 18:10:42 +0800116static int acpi_lid_evaluate_state(struct acpi_device *device)
117{
118 unsigned long long lid_state;
119 acpi_status status;
120
121 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &lid_state);
122 if (ACPI_FAILURE(status))
123 return -ENODEV;
124
125 return lid_state ? 1 : 0;
126}
127
128static int acpi_lid_notify_state(struct acpi_device *device, int state)
129{
130 struct acpi_button *button = acpi_driver_data(device);
131 int ret;
132
133 /* input layer checks if event is redundant */
134 input_report_switch(button->input, SW_LID, !state);
135 input_sync(button->input);
136
137 if (state)
138 pm_wakeup_event(&device->dev, 0);
139
140 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
141 if (ret == NOTIFY_DONE)
142 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
143 device);
144 if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
145 /*
146 * It is also regarded as success if the notifier_chain
147 * returns NOTIFY_OK or NOTIFY_DONE.
148 */
149 ret = 0;
150 }
151 return ret;
152}
153
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400154static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
155{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000156 struct acpi_device *device = seq->private;
Lv Zhengee7e2262016-06-01 18:10:42 +0800157 int state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400158
Lv Zhengee7e2262016-06-01 18:10:42 +0800159 state = acpi_lid_evaluate_state(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500160 seq_printf(seq, "state: %s\n",
Lv Zhengee7e2262016-06-01 18:10:42 +0800161 state < 0 ? "unsupported" : (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400162 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400163}
164
165static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
166{
Al Virod9dda782013-03-31 18:16:14 -0400167 return single_open(file, acpi_button_state_seq_show, PDE_DATA(inode));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400168}
169
Zhang Rui912b7422011-03-23 10:21:40 +0800170static const struct file_operations acpi_button_state_fops = {
171 .owner = THIS_MODULE,
172 .open = acpi_button_state_open_fs,
173 .read = seq_read,
174 .llseek = seq_lseek,
175 .release = single_release,
176};
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400177
Len Brown4be44fc2005-08-05 00:44:28 -0400178static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400179{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000180 struct acpi_button *button = acpi_driver_data(device);
Len Brown4be44fc2005-08-05 00:44:28 -0400181 struct proc_dir_entry *entry = NULL;
Zhang Rui912b7422011-03-23 10:21:40 +0800182 int ret = 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400183
Zhang Rui912b7422011-03-23 10:21:40 +0800184 /* procfs I/F for ACPI lid device only */
185 if (button->type != ACPI_BUTTON_TYPE_LID)
186 return 0;
187
188 if (acpi_button_dir || acpi_lid_dir) {
189 printk(KERN_ERR PREFIX "More than one Lid device found!\n");
190 return -EEXIST;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400191 }
192
Zhang Rui912b7422011-03-23 10:21:40 +0800193 /* create /proc/acpi/button */
194 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
195 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400196 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400197
Zhang Rui912b7422011-03-23 10:21:40 +0800198 /* create /proc/acpi/button/lid */
199 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
200 if (!acpi_lid_dir) {
201 ret = -ENODEV;
202 goto remove_button_dir;
203 }
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400204
Zhang Rui912b7422011-03-23 10:21:40 +0800205 /* create /proc/acpi/button/lid/LID/ */
206 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir);
207 if (!acpi_device_dir(device)) {
208 ret = -ENODEV;
209 goto remove_lid_dir;
210 }
211
212 /* create /proc/acpi/button/lid/LID/state */
213 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700214 S_IRUGO, acpi_device_dir(device),
Zhang Rui912b7422011-03-23 10:21:40 +0800215 &acpi_button_state_fops, device);
216 if (!entry) {
217 ret = -ENODEV;
218 goto remove_dev_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400219 }
220
Zhang Rui912b7422011-03-23 10:21:40 +0800221done:
222 return ret;
223
224remove_dev_dir:
225 remove_proc_entry(acpi_device_bid(device),
226 acpi_lid_dir);
227 acpi_device_dir(device) = NULL;
228remove_lid_dir:
229 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
230remove_button_dir:
231 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
232 goto done;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400233}
234
Len Brown4be44fc2005-08-05 00:44:28 -0400235static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400236{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500237 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400238
Zhang Rui912b7422011-03-23 10:21:40 +0800239 if (button->type != ACPI_BUTTON_TYPE_LID)
240 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400241
Zhang Rui912b7422011-03-23 10:21:40 +0800242 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
243 acpi_device_dir(device));
244 remove_proc_entry(acpi_device_bid(device),
245 acpi_lid_dir);
246 acpi_device_dir(device) = NULL;
247 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
248 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400249
Patrick Mocheld550d982006-06-27 00:41:40 -0400250 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400251}
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/* --------------------------------------------------------------------------
254 Driver Interface
255 -------------------------------------------------------------------------- */
Jesse Barnes7e127152009-09-10 15:28:02 -0700256int acpi_lid_notifier_register(struct notifier_block *nb)
257{
258 return blocking_notifier_chain_register(&acpi_lid_notifier, nb);
259}
260EXPORT_SYMBOL(acpi_lid_notifier_register);
261
262int acpi_lid_notifier_unregister(struct notifier_block *nb)
263{
264 return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb);
265}
266EXPORT_SYMBOL(acpi_lid_notifier_unregister);
267
268int acpi_lid_open(void)
269{
Jesse Barnes2c907b72009-10-07 14:39:46 -0700270 if (!lid_device)
271 return -ENODEV;
272
Lv Zhengee7e2262016-06-01 18:10:42 +0800273 return acpi_lid_evaluate_state(lid_device);
Jesse Barnes7e127152009-09-10 15:28:02 -0700274}
275EXPORT_SYMBOL(acpi_lid_open);
276
Lv Zhengee7e2262016-06-01 18:10:42 +0800277static int acpi_lid_update_state(struct acpi_device *device)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400278{
Lv Zhengee7e2262016-06-01 18:10:42 +0800279 int state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400280
Lv Zhengee7e2262016-06-01 18:10:42 +0800281 state = acpi_lid_evaluate_state(device);
282 if (state < 0)
283 return state;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000284
Lv Zhengee7e2262016-06-01 18:10:42 +0800285 return acpi_lid_notify_state(device, state);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000288static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000290 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500291 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000294 case ACPI_FIXED_HARDWARE_EVENT:
295 event = ACPI_BUTTON_NOTIFY_STATUS;
296 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500298 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500299 if (button->type == ACPI_BUTTON_TYPE_LID) {
Lv Zhengee7e2262016-06-01 18:10:42 +0800300 acpi_lid_update_state(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500301 } else {
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200302 int keycode;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500303
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200304 pm_wakeup_event(&device->dev, 0);
305 if (button->suspended)
306 break;
307
308 keycode = test_bit(KEY_SLEEP, input->keybit) ?
309 KEY_SLEEP : KEY_POWER;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500310 input_report_key(input, keycode, 1);
311 input_sync(input);
312 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300313 input_sync(input);
Rafael J. Wysocki1f835112011-01-06 23:36:01 +0100314
Lan Tianyu0bf63682014-03-15 13:37:13 -0400315 acpi_bus_generate_netlink_event(
316 device->pnp.device_class,
317 dev_name(&device->dev),
318 event, ++button->pushed);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 break;
321 default:
322 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400323 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200328#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200329static int acpi_button_suspend(struct device *dev)
330{
331 struct acpi_device *device = to_acpi_device(dev);
332 struct acpi_button *button = acpi_driver_data(device);
333
334 button->suspended = true;
335 return 0;
336}
337
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200338static int acpi_button_resume(struct device *dev)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400339{
Rafael J. Wysocki1be532d2012-06-27 23:26:51 +0200340 struct acpi_device *device = to_acpi_device(dev);
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000341 struct acpi_button *button = acpi_driver_data(device);
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000342
Rafael J. Wysockie71eeb22014-07-23 00:59:04 +0200343 button->suspended = false;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400344 return 0;
345}
Rafael J. Wysocki90692402012-08-09 23:00:02 +0200346#endif
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400347
Len Brown4be44fc2005-08-05 00:44:28 -0400348static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500350 struct acpi_button *button;
351 struct input_dev *input;
Thomas Renninger620e1122010-10-01 10:54:00 +0200352 const char *hid = acpi_device_hid(device);
353 char *name, *class;
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000354 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500356 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400358 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700360 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500362 button->input = input = input_allocate_device();
363 if (!input) {
364 error = -ENOMEM;
365 goto err_free_button;
366 }
367
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000368 name = acpi_device_name(device);
369 class = acpi_device_class(device);
370
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000371 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
372 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 button->type = ACPI_BUTTON_TYPE_POWER;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000374 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
375 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000377 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
378 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 button->type = ACPI_BUTTON_TYPE_SLEEP;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000380 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
381 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000383 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 button->type = ACPI_BUTTON_TYPE_LID;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000385 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
386 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400388 } else {
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000389 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500390 error = -ENODEV;
391 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500394 error = acpi_button_add_fs(device);
395 if (error)
396 goto err_free_input;
397
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000398 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500399
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000400 input->name = name;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500401 input->phys = button->phys;
402 input->id.bustype = BUS_HOST;
403 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800404 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500407 case ACPI_BUTTON_TYPE_POWER:
Lan Tianyu763f5272013-09-12 03:32:03 -0400408 input_set_capability(input, EV_KEY, KEY_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500410
411 case ACPI_BUTTON_TYPE_SLEEP:
Lan Tianyu763f5272013-09-12 03:32:03 -0400412 input_set_capability(input, EV_KEY, KEY_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500414
415 case ACPI_BUTTON_TYPE_LID:
Lan Tianyu763f5272013-09-12 03:32:03 -0400416 input_set_capability(input, EV_SW, SW_LID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 break;
418 }
419
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500420 error = input_register_device(input);
421 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000422 goto err_remove_fs;
Jesse Barnes7e127152009-09-10 15:28:02 -0700423 if (button->type == ACPI_BUTTON_TYPE_LID) {
Jesse Barnes7e127152009-09-10 15:28:02 -0700424 /*
425 * This assumes there's only one lid device, or if there are
426 * more we only care about the last one...
427 */
428 lid_device = device;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000431 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500434 err_remove_fs:
435 acpi_button_remove_fs(device);
436 err_free_input:
437 input_free_device(input);
438 err_free_button:
439 kfree(button);
440 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100443static int acpi_button_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000445 struct acpi_button *button = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Len Brown4be44fc2005-08-05 00:44:28 -0400447 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500448 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Mika Westerberg466e78f2012-09-07 10:31:39 +0300453module_acpi_driver(acpi_button_driver);