blob: 71ef9cd0735f2fadd8c76d67cee4029bc72b1ce1 [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;
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
282 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device);
283 if (ret == NOTIFY_DONE)
284 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state,
285 device);
Zhao Yakui13c199c2009-12-15 22:01:57 +0800286 if (ret == NOTIFY_DONE || ret == NOTIFY_OK) {
287 /*
288 * It is also regarded as success if the notifier_chain
289 * returns NOTIFY_OK or NOTIFY_DONE.
290 */
291 ret = 0;
292 }
Jesse Barnes7e127152009-09-10 15:28:02 -0700293 return ret;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400294}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000296static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000298 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500299 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000302 case ACPI_FIXED_HARDWARE_EVENT:
303 event = ACPI_BUTTON_NOTIFY_STATUS;
304 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500306 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500307 if (button->type == ACPI_BUTTON_TYPE_LID) {
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000308 acpi_lid_send_state(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500309 } else {
310 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
311 KEY_SLEEP : KEY_POWER;
312
313 input_report_key(input, keycode, 1);
314 input_sync(input);
315 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300316 input_sync(input);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500317 }
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500318
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000319 acpi_bus_generate_proc_event(device, event, ++button->pushed);
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
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400328static int acpi_button_resume(struct acpi_device *device)
329{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000330 struct acpi_button *button = acpi_driver_data(device);
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000331
Bjorn Helgaase2fb9752009-04-08 15:39:43 +0000332 if (button->type == ACPI_BUTTON_TYPE_LID)
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000333 return acpi_lid_send_state(device);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400334 return 0;
335}
336
Len Brown4be44fc2005-08-05 00:44:28 -0400337static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500339 struct acpi_button *button;
340 struct input_dev *input;
Thomas Renninger620e1122010-10-01 10:54:00 +0200341 const char *hid = acpi_device_hid(device);
342 char *name, *class;
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000343 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500345 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400347 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700349 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500351 button->input = input = input_allocate_device();
352 if (!input) {
353 error = -ENOMEM;
354 goto err_free_button;
355 }
356
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000357 name = acpi_device_name(device);
358 class = acpi_device_class(device);
359
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000360 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) ||
361 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 button->type = ACPI_BUTTON_TYPE_POWER;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000363 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
364 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasd68b5972009-04-08 15:40:04 +0000366 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) ||
367 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 button->type = ACPI_BUTTON_TYPE_SLEEP;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000369 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
370 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000372 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 button->type = ACPI_BUTTON_TYPE_LID;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000374 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
375 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400377 } else {
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000378 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500379 error = -ENODEV;
380 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500383 error = acpi_button_add_fs(device);
384 if (error)
385 goto err_free_input;
386
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000387 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500388
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000389 input->name = name;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500390 input->phys = button->phys;
391 input->id.bustype = BUS_HOST;
392 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800393 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500396 case ACPI_BUTTON_TYPE_POWER:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700397 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500398 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500400
401 case ACPI_BUTTON_TYPE_SLEEP:
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_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500405
406 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700407 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500408 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 break;
410 }
411
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500412 error = input_register_device(input);
413 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000414 goto err_remove_fs;
Jesse Barnes7e127152009-09-10 15:28:02 -0700415 if (button->type == ACPI_BUTTON_TYPE_LID) {
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000416 acpi_lid_send_state(device);
Jesse Barnes7e127152009-09-10 15:28:02 -0700417 /*
418 * This assumes there's only one lid device, or if there are
419 * more we only care about the last one...
420 */
421 lid_device = device;
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 if (device->wakeup.flags.valid) {
425 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400426 acpi_enable_gpe(device->wakeup.gpe_device,
Rafael J. Wysockia44061a2010-07-01 10:11:45 +0800427 device->wakeup.gpe_number);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100428 device->wakeup.run_wake_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 device->wakeup.state.enabled = 1;
430 }
431
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000432 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500435 err_remove_fs:
436 acpi_button_remove_fs(device);
437 err_free_input:
438 input_free_device(input);
439 err_free_button:
440 kfree(button);
441 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Len Brown4be44fc2005-08-05 00:44:28 -0400444static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000446 struct acpi_button *button = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +0100448 if (device->wakeup.flags.valid) {
449 acpi_disable_gpe(device->wakeup.gpe_device,
Rafael J. Wysockia44061a2010-07-01 10:11:45 +0800450 device->wakeup.gpe_number);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100451 device->wakeup.run_wake_count--;
Rafael J. Wysocki9630bdd2010-02-17 23:41:07 +0100452 device->wakeup.state.enabled = 0;
453 }
454
Len Brown4be44fc2005-08-05 00:44:28 -0400455 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500456 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Len Brown4be44fc2005-08-05 00:44:28 -0400461static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500463 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400465 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
466 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400467 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 result = acpi_bus_register_driver(&acpi_button_driver);
470 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400471 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400472 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Patrick Mocheld550d982006-06-27 00:41:40 -0400475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Len Brown4be44fc2005-08-05 00:44:28 -0400478static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 acpi_bus_unregister_driver(&acpi_button_driver);
481
Len Brown4be44fc2005-08-05 00:44:28 -0400482 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400483 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
484 if (acpi_sleep_dir)
485 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
486 if (acpi_lid_dir)
487 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
488 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491module_init(acpi_button_init);
492module_exit(acpi_button_exit);