blob: 52eb06ea4f80cfac8115e0b11df624ccd6e86c91 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <acpi/acpi_bus.h>
34#include <acpi/acpi_drivers.h>
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define ACPI_BUTTON_CLASS "button"
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040037#define ACPI_BUTTON_FILE_INFO "info"
38#define ACPI_BUTTON_FILE_STATE "state"
39#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define ACPI_BUTTON_NOTIFY_STATUS 0x80
41
42#define ACPI_BUTTON_SUBCLASS_POWER "power"
Len Brown4be44fc2005-08-05 00:44:28 -040043#define ACPI_BUTTON_HID_POWER "PNP0C0C"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button (CM)"
45#define ACPI_BUTTON_DEVICE_NAME_POWERF "Power Button (FF)"
46#define ACPI_BUTTON_TYPE_POWER 0x01
47#define ACPI_BUTTON_TYPE_POWERF 0x02
48
49#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
50#define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
51#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button (CM)"
52#define ACPI_BUTTON_DEVICE_NAME_SLEEPF "Sleep Button (FF)"
53#define ACPI_BUTTON_TYPE_SLEEP 0x03
54#define ACPI_BUTTON_TYPE_SLEEPF 0x04
55
56#define ACPI_BUTTON_SUBCLASS_LID "lid"
57#define ACPI_BUTTON_HID_LID "PNP0C0D"
58#define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
59#define ACPI_BUTTON_TYPE_LID 0x05
60
61#define _COMPONENT ACPI_BUTTON_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050062ACPI_MODULE_NAME("button");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050064MODULE_AUTHOR("Paul Diefenbaugh");
Len Brown7cda93e2007-02-12 23:50:02 -050065MODULE_DESCRIPTION("ACPI Button Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066MODULE_LICENSE("GPL");
67
Thomas Renninger1ba90e32007-07-23 14:44:41 +020068static const struct acpi_device_id button_device_ids[] = {
69 {ACPI_BUTTON_HID_LID, 0},
70 {ACPI_BUTTON_HID_SLEEP, 0},
71 {ACPI_BUTTON_HID_SLEEPF, 0},
72 {ACPI_BUTTON_HID_POWER, 0},
73 {ACPI_BUTTON_HID_POWERF, 0},
74 {"", 0},
75};
76MODULE_DEVICE_TABLE(acpi, button_device_ids);
77
Len Brown4be44fc2005-08-05 00:44:28 -040078static int acpi_button_add(struct acpi_device *device);
79static int acpi_button_remove(struct acpi_device *device, int type);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +040080static int acpi_button_resume(struct acpi_device *device);
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000081static void acpi_button_notify(struct acpi_device *device, u32 event);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040082static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
83static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85static struct acpi_driver acpi_button_driver = {
Len Brownc2b6705b2007-02-12 23:33:40 -050086 .name = "button",
Len Brown4be44fc2005-08-05 00:44:28 -040087 .class = ACPI_BUTTON_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020088 .ids = button_device_ids,
Len Brown4be44fc2005-08-05 00:44:28 -040089 .ops = {
90 .add = acpi_button_add,
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +040091 .resume = acpi_button_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040092 .remove = acpi_button_remove,
Bjorn Helgaas373cfc32009-03-30 17:48:18 +000093 .notify = acpi_button_notify,
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050094 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
97struct acpi_button {
Len Brown4be44fc2005-08-05 00:44:28 -040098 struct acpi_device *device; /* Fixed button kludge */
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
Arjan van de Vend7508032006-07-04 13:06:00 -0400105static const struct file_operations acpi_button_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700106 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400107 .open = acpi_button_info_open_fs,
108 .read = seq_read,
109 .llseek = seq_lseek,
110 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400111};
112
Arjan van de Vend7508032006-07-04 13:06:00 -0400113static const struct file_operations acpi_button_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700114 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400115 .open = acpi_button_state_open_fs,
116 .read = seq_read,
117 .llseek = seq_lseek,
118 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400119};
Len Brown4be44fc2005-08-05 00:44:28 -0400120
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400121/* --------------------------------------------------------------------------
122 FS Interface (/proc)
123 -------------------------------------------------------------------------- */
124
Len Brown4be44fc2005-08-05 00:44:28 -0400125static struct proc_dir_entry *acpi_button_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400126
127static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
128{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500129 struct acpi_button *button = seq->private;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400130
131 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400132 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400133
Len Brown4be44fc2005-08-05 00:44:28 -0400134 seq_printf(seq, "type: %s\n",
135 acpi_device_name(button->device));
Patrick Mocheld550d982006-06-27 00:41:40 -0400136 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400137}
138
139static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
140{
141 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
142}
Len Brown4be44fc2005-08-05 00:44:28 -0400143
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400144static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
145{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500146 struct acpi_button *button = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400147 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400148 unsigned long long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400149
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400150 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400151 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400152
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400153 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500154 seq_printf(seq, "state: %s\n",
155 ACPI_FAILURE(status) ? "unsupported" :
156 (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400157 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400158}
159
160static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
161{
162 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
163}
164
165static struct proc_dir_entry *acpi_power_dir;
166static struct proc_dir_entry *acpi_sleep_dir;
167static struct proc_dir_entry *acpi_lid_dir;
168
Len Brown4be44fc2005-08-05 00:44:28 -0400169static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400170{
Len Brown4be44fc2005-08-05 00:44:28 -0400171 struct proc_dir_entry *entry = NULL;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500172 struct acpi_button *button;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400173
174 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400175 return -EINVAL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400176
177 button = acpi_driver_data(device);
178
179 switch (button->type) {
180 case ACPI_BUTTON_TYPE_POWER:
181 case ACPI_BUTTON_TYPE_POWERF:
182 if (!acpi_power_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400183 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
184 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400185 entry = acpi_power_dir;
186 break;
187 case ACPI_BUTTON_TYPE_SLEEP:
188 case ACPI_BUTTON_TYPE_SLEEPF:
189 if (!acpi_sleep_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400190 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
191 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400192 entry = acpi_sleep_dir;
193 break;
194 case ACPI_BUTTON_TYPE_LID:
195 if (!acpi_lid_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400196 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
197 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400198 entry = acpi_lid_dir;
199 break;
200 }
201
202 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400203 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400204
205 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
206 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400207 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400208
209 /* 'info' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700210 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
211 S_IRUGO, acpi_device_dir(device),
212 &acpi_button_info_fops,
213 acpi_driver_data(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400214 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400215 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400216
217 /* show lid state [R] */
218 if (button->type == ACPI_BUTTON_TYPE_LID) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700219 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
220 S_IRUGO, acpi_device_dir(device),
221 &acpi_button_state_fops,
222 acpi_driver_data(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400223 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400224 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400225 }
226
Patrick Mocheld550d982006-06-27 00:41:40 -0400227 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400228}
229
Len Brown4be44fc2005-08-05 00:44:28 -0400230static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400231{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500232 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400233
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400234 if (acpi_device_dir(device)) {
235 if (button->type == ACPI_BUTTON_TYPE_LID)
236 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400237 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400238 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400239 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400240
241 remove_proc_entry(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400242 acpi_device_dir(device)->parent);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400243 acpi_device_dir(device) = NULL;
244 }
245
Patrick Mocheld550d982006-06-27 00:41:40 -0400246 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/* --------------------------------------------------------------------------
250 Driver Interface
251 -------------------------------------------------------------------------- */
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400252static int acpi_lid_send_state(struct acpi_button *button)
253{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400254 unsigned long long state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400255 acpi_status status;
256
257 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
258 &state);
259 if (ACPI_FAILURE(status))
260 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000261
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400262 /* input layer checks if event is redundant */
263 input_report_switch(button->input, SW_LID, !state);
Guillem Joverdf316e92008-10-24 00:28:33 +0300264 input_sync(button->input);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400265 return 0;
266}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000268static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000270 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500271 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400274 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000277 case ACPI_FIXED_HARDWARE_EVENT:
278 event = ACPI_BUTTON_NOTIFY_STATUS;
279 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500281 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500282 if (button->type == ACPI_BUTTON_TYPE_LID) {
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400283 acpi_lid_send_state(button);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500284 } else {
285 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
286 KEY_SLEEP : KEY_POWER;
287
288 input_report_key(input, keycode, 1);
289 input_sync(input);
290 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300291 input_sync(input);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500292 }
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500293
Len Brown14e04fb2007-08-23 15:20:26 -0400294 acpi_bus_generate_proc_event(button->device, event,
Len Brown4be44fc2005-08-05 00:44:28 -0400295 ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 default:
298 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400299 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 break;
301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400304static int acpi_button_resume(struct acpi_device *device)
305{
306 struct acpi_button *button;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000307
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400308 if (!device)
309 return -EINVAL;
310 button = acpi_driver_data(device);
311 if (button && button->type == ACPI_BUTTON_TYPE_LID)
312 return acpi_lid_send_state(button);
313 return 0;
314}
315
Len Brown4be44fc2005-08-05 00:44:28 -0400316static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500318 int error;
319 struct acpi_button *button;
320 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400323 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500325 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400327 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 button->device = device;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700330 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500332 button->input = input = input_allocate_device();
333 if (!input) {
334 error = -ENOMEM;
335 goto err_free_button;
336 }
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 /*
339 * Determine the button type (via hid), as fixed-feature buttons
340 * need to be handled a bit differently than generic-space.
341 */
342 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
343 button->type = ACPI_BUTTON_TYPE_POWER;
Len Brown4be44fc2005-08-05 00:44:28 -0400344 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
345 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400347 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 button->type = ACPI_BUTTON_TYPE_POWERF;
349 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400350 ACPI_BUTTON_DEVICE_NAME_POWERF);
351 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400353 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 button->type = ACPI_BUTTON_TYPE_SLEEP;
Len Brown4be44fc2005-08-05 00:44:28 -0400355 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
356 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400358 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 button->type = ACPI_BUTTON_TYPE_SLEEPF;
360 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400361 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
362 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400364 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 button->type = ACPI_BUTTON_TYPE_LID;
Len Brown4be44fc2005-08-05 00:44:28 -0400366 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
367 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400369 } else {
Len Brown64684632006-06-26 23:41:38 -0400370 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
371 acpi_device_hid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500372 error = -ENODEV;
373 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500376 error = acpi_button_add_fs(device);
377 if (error)
378 goto err_free_input;
379
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500380 snprintf(button->phys, sizeof(button->phys),
381 "%s/button/input0", acpi_device_hid(device));
382
383 input->name = acpi_device_name(device);
384 input->phys = button->phys;
385 input->id.bustype = BUS_HOST;
386 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800387 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500390 case ACPI_BUTTON_TYPE_POWER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 case ACPI_BUTTON_TYPE_POWERF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700392 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500393 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500395
396 case ACPI_BUTTON_TYPE_SLEEP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 case ACPI_BUTTON_TYPE_SLEEPF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700398 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500399 set_bit(KEY_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500401
402 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700403 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500404 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 break;
406 }
407
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500408 error = input_register_device(input);
409 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000410 goto err_remove_fs;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400411 if (button->type == ACPI_BUTTON_TYPE_LID)
412 acpi_lid_send_state(button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (device->wakeup.flags.valid) {
415 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400416 acpi_set_gpe_type(device->wakeup.gpe_device,
417 device->wakeup.gpe_number,
418 ACPI_GPE_TYPE_WAKE_RUN);
419 acpi_enable_gpe(device->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400420 device->wakeup.gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 device->wakeup.state.enabled = 1;
422 }
423
Len Brown4be44fc2005-08-05 00:44:28 -0400424 printk(KERN_INFO PREFIX "%s [%s]\n",
425 acpi_device_name(device), acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500428 err_remove_fs:
429 acpi_button_remove_fs(device);
430 err_free_input:
431 input_free_device(input);
432 err_free_button:
433 kfree(button);
434 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Len Brown4be44fc2005-08-05 00:44:28 -0400437static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500439 struct acpi_button *button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400442 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 button = acpi_driver_data(device);
445
Len Brown4be44fc2005-08-05 00:44:28 -0400446 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500447 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Len Brown4be44fc2005-08-05 00:44:28 -0400452static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500454 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400456 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
457 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400458 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 result = acpi_bus_register_driver(&acpi_button_driver);
461 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400462 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400463 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
Patrick Mocheld550d982006-06-27 00:41:40 -0400466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Len Brown4be44fc2005-08-05 00:44:28 -0400469static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 acpi_bus_unregister_driver(&acpi_button_driver);
472
Len Brown4be44fc2005-08-05 00:44:28 -0400473 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400474 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
475 if (acpi_sleep_dir)
476 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
477 if (acpi_lid_dir)
478 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
479 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482module_init(acpi_button_init);
483module_exit(acpi_button_exit);