blob: c8441627f68e6906b6c37a56c80b68499edcd88d [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 {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -050098 unsigned int type;
99 struct input_dev *input;
100 char phys[32]; /* for input device */
Len Brown4be44fc2005-08-05 00:44:28 -0400101 unsigned long pushed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Arjan van de Vend7508032006-07-04 13:06:00 -0400104static const struct file_operations acpi_button_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700105 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400106 .open = acpi_button_info_open_fs,
107 .read = seq_read,
108 .llseek = seq_lseek,
109 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400110};
111
Arjan van de Vend7508032006-07-04 13:06:00 -0400112static const struct file_operations acpi_button_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700113 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400114 .open = acpi_button_state_open_fs,
115 .read = seq_read,
116 .llseek = seq_lseek,
117 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400118};
Len Brown4be44fc2005-08-05 00:44:28 -0400119
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400120/* --------------------------------------------------------------------------
121 FS Interface (/proc)
122 -------------------------------------------------------------------------- */
123
Len Brown4be44fc2005-08-05 00:44:28 -0400124static struct proc_dir_entry *acpi_button_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400125
126static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
127{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000128 struct acpi_device *device = seq->private;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400129
Len Brown4be44fc2005-08-05 00:44:28 -0400130 seq_printf(seq, "type: %s\n",
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000131 acpi_device_name(device));
Patrick Mocheld550d982006-06-27 00:41:40 -0400132 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400133}
134
135static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
136{
137 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
138}
Len Brown4be44fc2005-08-05 00:44:28 -0400139
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400140static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
141{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000142 struct acpi_device *device = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400143 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400144 unsigned long long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400145
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000146 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500147 seq_printf(seq, "state: %s\n",
148 ACPI_FAILURE(status) ? "unsupported" :
149 (state ? "open" : "closed"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400150 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400151}
152
153static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
154{
155 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
156}
157
158static struct proc_dir_entry *acpi_power_dir;
159static struct proc_dir_entry *acpi_sleep_dir;
160static struct proc_dir_entry *acpi_lid_dir;
161
Len Brown4be44fc2005-08-05 00:44:28 -0400162static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400163{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000164 struct acpi_button *button = acpi_driver_data(device);
Len Brown4be44fc2005-08-05 00:44:28 -0400165 struct proc_dir_entry *entry = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400166
167 switch (button->type) {
168 case ACPI_BUTTON_TYPE_POWER:
169 case ACPI_BUTTON_TYPE_POWERF:
170 if (!acpi_power_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400171 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
172 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400173 entry = acpi_power_dir;
174 break;
175 case ACPI_BUTTON_TYPE_SLEEP:
176 case ACPI_BUTTON_TYPE_SLEEPF:
177 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 -------------------------------------------------------------------------- */
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000238static int acpi_lid_send_state(struct acpi_device *device)
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400239{
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000240 struct acpi_button *button = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400241 unsigned long long state;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400242 acpi_status status;
243
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000244 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400245 if (ACPI_FAILURE(status))
246 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000247
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400248 /* input layer checks if event is redundant */
249 input_report_switch(button->input, SW_LID, !state);
Guillem Joverdf316e92008-10-24 00:28:33 +0300250 input_sync(button->input);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400251 return 0;
252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000254static void acpi_button_notify(struct acpi_device *device, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000256 struct acpi_button *button = acpi_driver_data(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500257 struct input_dev *input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 switch (event) {
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000260 case ACPI_FIXED_HARDWARE_EVENT:
261 event = ACPI_BUTTON_NOTIFY_STATUS;
262 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 case ACPI_BUTTON_NOTIFY_STATUS:
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500264 input = button->input;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500265 if (button->type == ACPI_BUTTON_TYPE_LID) {
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000266 acpi_lid_send_state(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500267 } else {
268 int keycode = test_bit(KEY_SLEEP, input->keybit) ?
269 KEY_SLEEP : KEY_POWER;
270
271 input_report_key(input, keycode, 1);
272 input_sync(input);
273 input_report_key(input, keycode, 0);
Guillem Joverdf316e92008-10-24 00:28:33 +0300274 input_sync(input);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500275 }
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500276
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000277 acpi_bus_generate_proc_event(device, event, ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
279 default:
280 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400281 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400286static int acpi_button_resume(struct acpi_device *device)
287{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000288 struct acpi_button *button = acpi_driver_data(device);
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000289
Bjorn Helgaase2fb9752009-04-08 15:39:43 +0000290 if (button->type == ACPI_BUTTON_TYPE_LID)
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000291 return acpi_lid_send_state(device);
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400292 return 0;
293}
294
Len Brown4be44fc2005-08-05 00:44:28 -0400295static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500297 struct acpi_button *button;
298 struct input_dev *input;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000299 char *hid, *name, *class;
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000300 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500302 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400304 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700306 device->driver_data = button;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500308 button->input = input = input_allocate_device();
309 if (!input) {
310 error = -ENOMEM;
311 goto err_free_button;
312 }
313
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000314 hid = acpi_device_hid(device);
315 name = acpi_device_name(device);
316 class = acpi_device_class(device);
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 /*
319 * Determine the button type (via hid), as fixed-feature buttons
320 * need to be handled a bit differently than generic-space.
321 */
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000322 if (!strcmp(hid, ACPI_BUTTON_HID_POWER)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 button->type = ACPI_BUTTON_TYPE_POWER;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000324 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER);
325 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000327 } else if (!strcmp(hid, ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 button->type = ACPI_BUTTON_TYPE_POWERF;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000329 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWERF);
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_SLEEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 button->type = ACPI_BUTTON_TYPE_SLEEP;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000334 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP);
335 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000337 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 button->type = ACPI_BUTTON_TYPE_SLEEPF;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000339 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEPF);
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_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 button->type = ACPI_BUTTON_TYPE_LID;
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000344 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID);
345 sprintf(class, "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400347 } else {
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000348 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500349 error = -ENODEV;
350 goto err_free_input;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500353 error = acpi_button_add_fs(device);
354 if (error)
355 goto err_free_input;
356
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000357 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500358
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000359 input->name = name;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500360 input->phys = button->phys;
361 input->id.bustype = BUS_HOST;
362 input->id.product = button->type;
Andrey Borzenkov3b34e522008-03-04 15:06:35 -0800363 input->dev.parent = &device->dev;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 switch (button->type) {
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500366 case ACPI_BUTTON_TYPE_POWER:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 case ACPI_BUTTON_TYPE_POWERF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700368 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500369 set_bit(KEY_POWER, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500371
372 case ACPI_BUTTON_TYPE_SLEEP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 case ACPI_BUTTON_TYPE_SLEEPF:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700374 input->evbit[0] = BIT_MASK(EV_KEY);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500375 set_bit(KEY_SLEEP, input->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500377
378 case ACPI_BUTTON_TYPE_LID:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700379 input->evbit[0] = BIT_MASK(EV_SW);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500380 set_bit(SW_LID, input->swbit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 break;
382 }
383
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500384 error = input_register_device(input);
385 if (error)
Bjorn Helgaas373cfc32009-03-30 17:48:18 +0000386 goto err_remove_fs;
Alexey Starikovskiy23de5d92007-10-22 14:18:18 +0400387 if (button->type == ACPI_BUTTON_TYPE_LID)
Bjorn Helgaas106c19e2009-04-08 15:39:59 +0000388 acpi_lid_send_state(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 if (device->wakeup.flags.valid) {
391 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400392 acpi_set_gpe_type(device->wakeup.gpe_device,
393 device->wakeup.gpe_number,
394 ACPI_GPE_TYPE_WAKE_RUN);
395 acpi_enable_gpe(device->wakeup.gpe_device,
Alexey Starikovskiy0b7084a2008-10-25 21:48:46 +0400396 device->wakeup.gpe_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 device->wakeup.state.enabled = 1;
398 }
399
Bjorn Helgaasbf04a772009-04-08 15:39:54 +0000400 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device));
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500403 err_remove_fs:
404 acpi_button_remove_fs(device);
405 err_free_input:
406 input_free_device(input);
407 err_free_button:
408 kfree(button);
409 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Len Brown4be44fc2005-08-05 00:44:28 -0400412static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Bjorn Helgaas1bce8112009-04-08 15:39:49 +0000414 struct acpi_button *button = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Len Brown4be44fc2005-08-05 00:44:28 -0400416 acpi_button_remove_fs(device);
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500417 input_unregister_device(button->input);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 kfree(button);
Patrick Mocheld550d982006-06-27 00:41:40 -0400419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Len Brown4be44fc2005-08-05 00:44:28 -0400422static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Dmitry Torokhovc0968f02006-11-09 00:40:13 -0500424 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400426 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
427 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400428 return -ENODEV;
Bjorn Helgaas50a4da82009-04-08 15:39:38 +0000429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 result = acpi_bus_register_driver(&acpi_button_driver);
431 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400432 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400433 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
Patrick Mocheld550d982006-06-27 00:41:40 -0400436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
Len Brown4be44fc2005-08-05 00:44:28 -0400439static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 acpi_bus_unregister_driver(&acpi_button_driver);
442
Len Brown4be44fc2005-08-05 00:44:28 -0400443 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400444 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
445 if (acpi_sleep_dir)
446 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
447 if (acpi_lid_dir)
448 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
449 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452module_init(acpi_button_init);
453module_exit(acpi_button_exit);