blob: 9f6d2e6844a75fdc887d59b0c47aaac83c6e4b37 [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 Brownc2b67052007-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
Len Brown4be44fc2005-08-05 00:44:28 -0400131 seq_printf(seq, "type: %s\n",
132 acpi_device_name(button->device));
Patrick Mocheld550d982006-06-27 00:41:40 -0400133 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400134}
135
136static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
137{
138 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
139}
Len Brown4be44fc2005-08-05 00:44:28 -0400140
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400141static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
142{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500143 struct acpi_button *button = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400144 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400145 unsigned long long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400146
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400147 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500148 seq_printf(seq, "state: %s\n",
149 ACPI_FAILURE(status) ? "unsupported" :
150 (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400151 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400152}
153
154static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
155{
156 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
157}
158
159static struct proc_dir_entry *acpi_power_dir;
160static struct proc_dir_entry *acpi_sleep_dir;
161static struct proc_dir_entry *acpi_lid_dir;
162
Len Brown4be44fc2005-08-05 00:44:28 -0400163static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400164{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000165 struct acpi_button *button = acpi_driver_data(device);
Len Brown4be44fc2005-08-05 00:44:28 -0400166 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400167
168 switch (button->type) {
169 case ACPI_BUTTON_TYPE_POWER:
170 case ACPI_BUTTON_TYPE_POWERF:
171 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:
177 case ACPI_BUTTON_TYPE_SLEEPF:
178 if (!acpi_sleep_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400179 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
180 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400181 entry = acpi_sleep_dir;
182 break;
183 case ACPI_BUTTON_TYPE_LID:
184 if (!acpi_lid_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400185 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
186 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400187 entry = acpi_lid_dir;
188 break;
189 }
190
191 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400192 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400193
194 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
195 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400196 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400197
198 /* 'info' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700199 entry = proc_create_data(ACPI_BUTTON_FILE_INFO,
200 S_IRUGO, acpi_device_dir(device),
201 &acpi_button_info_fops,
202 acpi_driver_data(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400203 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400204 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400205
206 /* show lid state [R] */
207 if (button->type == ACPI_BUTTON_TYPE_LID) {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700208 entry = proc_create_data(ACPI_BUTTON_FILE_STATE,
209 S_IRUGO, acpi_device_dir(device),
210 &acpi_button_state_fops,
211 acpi_driver_data(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400212 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400213 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400214 }
215
Patrick Mocheld550d982006-06-27 00:41:40 -0400216 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400217}
218
Len Brown4be44fc2005-08-05 00:44:28 -0400219static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400220{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500221 struct acpi_button *button = acpi_driver_data(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400222
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400223 if (acpi_device_dir(device)) {
224 if (button->type == ACPI_BUTTON_TYPE_LID)
225 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400226 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400227 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400228 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400229
230 remove_proc_entry(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400231 acpi_device_dir(device)->parent);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400232 acpi_device_dir(device) = NULL;
233 }
234
Patrick Mocheld550d982006-06-27 00:41:40 -0400235 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/* --------------------------------------------------------------------------
239 Driver Interface
240 -------------------------------------------------------------------------- */
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400241static int acpi_lid_send_state(struct acpi_button *button)
242{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400243 unsigned long long state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400244 acpi_status status;
245
246 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL,
247 &state);
248 if (ACPI_FAILURE(status))
249 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000250
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400251 /* input layer checks if event is redundant */
252 input_report_switch(button->input, SW_LID, !state);
Guillem Joverdf316e92008-10-24 00:28:33 +0300253 input_sync(button->input);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400254 return 0;
255}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000257static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000259 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500260 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000263 case ACPI_FIXED_HARDWARE_EVENT:
264 event = ACPI_BUTTON_NOTIFY_STATUS;
265 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500267 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500268 if (button->type == ACPI_BUTTON_TYPE_LID) {
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400269 acpi_lid_send_state(button);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500270 } else {
271 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
272 KEY_SLEEP : KEY_POWER;
273
274 input_report_key(input, keycode, 1);
275 input_sync(input);
276 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300277 input_sync(input);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500278 }
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500279
Len Brown14e04fb2007-08-23 15:20:26 -0400280 acpi_bus_generate_proc_event(button->device, event,
Len Brown4be44fc2005-08-05 00:44:28 -0400281 ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 default:
284 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400285 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 break;
287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400290static int acpi_button_resume(struct acpi_device *device)
291{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000292 struct acpi_button *button = acpi_driver_data(device);
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000293
Bjorn Helgaase2fb9752009-04-08 15:39:43 +0000294 if (button->type == ACPI_BUTTON_TYPE_LID)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400295 return acpi_lid_send_state(button);
296 return 0;
297}
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500301 struct acpi_button *button;
302 struct input_dev *input;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000303 char *hid, *name, *class;
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000304 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500306 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400308 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 button->device = device;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700311 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500313 button->input = input = input_allocate_device();
314 if (!input) {
315 error = -ENOMEM;
316 goto err_free_button;
317 }
318
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000319 hid = acpi_device_hid(device);
320 name = acpi_device_name(device);
321 class = acpi_device_class(device);
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /*
324 * Determine the button type (via hid), as fixed-feature buttons
325 * need to be handled a bit differently than generic-space.
326 */
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000327 if (!strcmp(hid, ACPI_BUTTON_HID_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 button->type = ACPI_BUTTON_TYPE_POWER;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000329 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
330 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000332 } else if (!strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 button->type = ACPI_BUTTON_TYPE_POWERF;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000334 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWERF);
335 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000337 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 button->type = ACPI_BUTTON_TYPE_SLEEP;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000339 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
340 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000342 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 button->type = ACPI_BUTTON_TYPE_SLEEPF;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000344 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEPF);
345 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000347 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 button->type = ACPI_BUTTON_TYPE_LID;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000349 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
350 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400352 } else {
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000353 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500354 error = -ENODEV;
355 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500358 error = acpi_button_add_fs(device);
359 if (error)
360 goto err_free_input;
361
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000362 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500363
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000364 input->name = name;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500365 input->phys = button->phys;
366 input->id.bustype = BUS_HOST;
367 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800368 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500371 case ACPI_BUTTON_TYPE_POWER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 case ACPI_BUTTON_TYPE_POWERF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700373 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500374 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500376
377 case ACPI_BUTTON_TYPE_SLEEP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 case ACPI_BUTTON_TYPE_SLEEPF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700379 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500380 set_bit(KEY_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500382
383 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700384 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500385 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 break;
387 }
388
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500389 error = input_register_device(input);
390 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000391 goto err_remove_fs;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400392 if (button->type == ACPI_BUTTON_TYPE_LID)
393 acpi_lid_send_state(button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (device->wakeup.flags.valid) {
396 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400397 acpi_set_gpe_type(device->wakeup.gpe_device,
398 device->wakeup.gpe_number,
399 ACPI_GPE_TYPE_WAKE_RUN);
400 acpi_enable_gpe(device->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400401 device->wakeup.gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 device->wakeup.state.enabled = 1;
403 }
404
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000405 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500406 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500408 err_remove_fs:
409 acpi_button_remove_fs(device);
410 err_free_input:
411 input_free_device(input);
412 err_free_button:
413 kfree(button);
414 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Len Brown4be44fc2005-08-05 00:44:28 -0400417static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000419 struct acpi_button *button = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Len Brown4be44fc2005-08-05 00:44:28 -0400421 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500422 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Len Brown4be44fc2005-08-05 00:44:28 -0400427static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500429 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400431 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
432 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400433 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 result = acpi_bus_register_driver(&acpi_button_driver);
436 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400437 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400438 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
Patrick Mocheld550d982006-06-27 00:41:40 -0400441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Len Brown4be44fc2005-08-05 00:44:28 -0400444static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 acpi_bus_unregister_driver(&acpi_button_driver);
447
Len Brown4be44fc2005-08-05 00:44:28 -0400448 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400449 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
450 if (acpi_sleep_dir)
451 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
452 if (acpi_lid_dir)
453 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
454 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457module_init(acpi_button_init);
458module_exit(acpi_button_exit);