blob: eefb9e4df5806face2ac0cb6bf1077f54a483118 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_button.c - ACPI Button Driver ($Revision: 30 $)
3 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <acpi/acpi_bus.h>
33#include <acpi/acpi_drivers.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#define ACPI_BUTTON_COMPONENT 0x00080000
36#define ACPI_BUTTON_DRIVER_NAME "ACPI Button Driver"
37#define ACPI_BUTTON_CLASS "button"
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040038#define ACPI_BUTTON_FILE_INFO "info"
39#define ACPI_BUTTON_FILE_STATE "state"
40#define ACPI_BUTTON_TYPE_UNKNOWN 0x00
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define ACPI_BUTTON_NOTIFY_STATUS 0x80
42
43#define ACPI_BUTTON_SUBCLASS_POWER "power"
Len Brown4be44fc2005-08-05 00:44:28 -040044#define ACPI_BUTTON_HID_POWER "PNP0C0C"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button (CM)"
46#define ACPI_BUTTON_DEVICE_NAME_POWERF "Power Button (FF)"
47#define ACPI_BUTTON_TYPE_POWER 0x01
48#define ACPI_BUTTON_TYPE_POWERF 0x02
49
50#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep"
51#define ACPI_BUTTON_HID_SLEEP "PNP0C0E"
52#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button (CM)"
53#define ACPI_BUTTON_DEVICE_NAME_SLEEPF "Sleep Button (FF)"
54#define ACPI_BUTTON_TYPE_SLEEP 0x03
55#define ACPI_BUTTON_TYPE_SLEEPF 0x04
56
57#define ACPI_BUTTON_SUBCLASS_LID "lid"
58#define ACPI_BUTTON_HID_LID "PNP0C0D"
59#define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch"
60#define ACPI_BUTTON_TYPE_LID 0x05
61
62#define _COMPONENT ACPI_BUTTON_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040063ACPI_MODULE_NAME("acpi_button")
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Len Brown4be44fc2005-08-05 00:44:28 -040065 MODULE_AUTHOR("Paul Diefenbaugh");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066MODULE_DESCRIPTION(ACPI_BUTTON_DRIVER_NAME);
67MODULE_LICENSE("GPL");
68
Len Brown4be44fc2005-08-05 00:44:28 -040069static int acpi_button_add(struct acpi_device *device);
70static int acpi_button_remove(struct acpi_device *device, int type);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040071static int acpi_button_info_open_fs(struct inode *inode, struct file *file);
72static int acpi_button_state_open_fs(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static struct acpi_driver acpi_button_driver = {
Len Brown4be44fc2005-08-05 00:44:28 -040075 .name = ACPI_BUTTON_DRIVER_NAME,
76 .class = ACPI_BUTTON_CLASS,
77 .ids = "ACPI_FPB,ACPI_FSB,PNP0C0D,PNP0C0C,PNP0C0E",
78 .ops = {
79 .add = acpi_button_add,
80 .remove = acpi_button_remove,
81 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
84struct acpi_button {
Len Brown4be44fc2005-08-05 00:44:28 -040085 acpi_handle handle;
86 struct acpi_device *device; /* Fixed button kludge */
87 u8 type;
88 unsigned long pushed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
90
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040091static struct file_operations acpi_button_info_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -040092 .open = acpi_button_info_open_fs,
93 .read = seq_read,
94 .llseek = seq_lseek,
95 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040096};
97
98static struct file_operations acpi_button_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -040099 .open = acpi_button_state_open_fs,
100 .read = seq_read,
101 .llseek = seq_lseek,
102 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400103};
Len Brown4be44fc2005-08-05 00:44:28 -0400104
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400105/* --------------------------------------------------------------------------
106 FS Interface (/proc)
107 -------------------------------------------------------------------------- */
108
Len Brown4be44fc2005-08-05 00:44:28 -0400109static struct proc_dir_entry *acpi_button_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400110
111static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
112{
Len Brown4be44fc2005-08-05 00:44:28 -0400113 struct acpi_button *button = (struct acpi_button *)seq->private;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400114
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400115
116 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400117 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400118
Len Brown4be44fc2005-08-05 00:44:28 -0400119 seq_printf(seq, "type: %s\n",
120 acpi_device_name(button->device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400121
Patrick Mocheld550d982006-06-27 00:41:40 -0400122 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400123}
124
125static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
126{
127 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
128}
Len Brown4be44fc2005-08-05 00:44:28 -0400129
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400130static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
131{
Len Brown4be44fc2005-08-05 00:44:28 -0400132 struct acpi_button *button = (struct acpi_button *)seq->private;
133 acpi_status status;
134 unsigned long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400135
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400136
137 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400138 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400139
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400140 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400141 if (ACPI_FAILURE(status)) {
142 seq_printf(seq, "state: unsupported\n");
Len Brown4be44fc2005-08-05 00:44:28 -0400143 } else {
144 seq_printf(seq, "state: %s\n",
145 (state ? "open" : "closed"));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400146 }
147
Patrick Mocheld550d982006-06-27 00:41:40 -0400148 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400149}
150
151static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
152{
153 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
154}
155
156static struct proc_dir_entry *acpi_power_dir;
157static struct proc_dir_entry *acpi_sleep_dir;
158static struct proc_dir_entry *acpi_lid_dir;
159
Len Brown4be44fc2005-08-05 00:44:28 -0400160static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400161{
Len Brown4be44fc2005-08-05 00:44:28 -0400162 struct proc_dir_entry *entry = NULL;
163 struct acpi_button *button = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400164
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400165
166 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400167 return -EINVAL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400168
169 button = acpi_driver_data(device);
170
171 switch (button->type) {
172 case ACPI_BUTTON_TYPE_POWER:
173 case ACPI_BUTTON_TYPE_POWERF:
174 if (!acpi_power_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400175 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
176 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400177 entry = acpi_power_dir;
178 break;
179 case ACPI_BUTTON_TYPE_SLEEP:
180 case ACPI_BUTTON_TYPE_SLEEPF:
181 if (!acpi_sleep_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400182 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
183 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400184 entry = acpi_sleep_dir;
185 break;
186 case ACPI_BUTTON_TYPE_LID:
187 if (!acpi_lid_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400188 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
189 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400190 entry = acpi_lid_dir;
191 break;
192 }
193
194 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400195 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400196 entry->owner = THIS_MODULE;
197
198 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
199 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400200 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400201 acpi_device_dir(device)->owner = THIS_MODULE;
202
203 /* 'info' [R] */
204 entry = create_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400205 S_IRUGO, acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400206 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400207 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400208 else {
209 entry->proc_fops = &acpi_button_info_fops;
210 entry->data = acpi_driver_data(device);
211 entry->owner = THIS_MODULE;
212 }
213
214 /* show lid state [R] */
215 if (button->type == ACPI_BUTTON_TYPE_LID) {
216 entry = create_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400217 S_IRUGO, acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400218 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400219 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400220 else {
221 entry->proc_fops = &acpi_button_state_fops;
222 entry->data = acpi_driver_data(device);
223 entry->owner = THIS_MODULE;
224 }
225 }
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{
Len Brown4be44fc2005-08-05 00:44:28 -0400232 struct acpi_button *button = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400233
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400234
235 button = acpi_driver_data(device);
236 if (acpi_device_dir(device)) {
237 if (button->type == ACPI_BUTTON_TYPE_LID)
238 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400239 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400240 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400241 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400242
243 remove_proc_entry(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400244 acpi_device_dir(device)->parent);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400245 acpi_device_dir(device) = NULL;
246 }
247
Patrick Mocheld550d982006-06-27 00:41:40 -0400248 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400249}
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/* --------------------------------------------------------------------------
252 Driver Interface
253 -------------------------------------------------------------------------- */
254
Len Brown4be44fc2005-08-05 00:44:28 -0400255static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Len Brown4be44fc2005-08-05 00:44:28 -0400257 struct acpi_button *button = (struct acpi_button *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400261 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 switch (event) {
264 case ACPI_BUTTON_NOTIFY_STATUS:
Len Brown4be44fc2005-08-05 00:44:28 -0400265 acpi_bus_generate_event(button->device, event,
266 ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 break;
268 default:
269 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400270 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 break;
272 }
273
Patrick Mocheld550d982006-06-27 00:41:40 -0400274 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Len Brown4be44fc2005-08-05 00:44:28 -0400277static acpi_status acpi_button_notify_fixed(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Len Brown4be44fc2005-08-05 00:44:28 -0400279 struct acpi_button *button = (struct acpi_button *)data;
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400282 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400283 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400285 acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Patrick Mocheld550d982006-06-27 00:41:40 -0400287 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Len Brown4be44fc2005-08-05 00:44:28 -0400290static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Len Brown4be44fc2005-08-05 00:44:28 -0400292 int result = 0;
293 acpi_status status = AE_OK;
294 struct acpi_button *button = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400298 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 button = kmalloc(sizeof(struct acpi_button), GFP_KERNEL);
301 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400302 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 memset(button, 0, sizeof(struct acpi_button));
304
305 button->device = device;
306 button->handle = device->handle;
307 acpi_driver_data(device) = button;
308
309 /*
310 * Determine the button type (via hid), as fixed-feature buttons
311 * need to be handled a bit differently than generic-space.
312 */
313 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
314 button->type = ACPI_BUTTON_TYPE_POWER;
Len Brown4be44fc2005-08-05 00:44:28 -0400315 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
316 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400318 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 button->type = ACPI_BUTTON_TYPE_POWERF;
320 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400321 ACPI_BUTTON_DEVICE_NAME_POWERF);
322 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400324 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 button->type = ACPI_BUTTON_TYPE_SLEEP;
Len Brown4be44fc2005-08-05 00:44:28 -0400326 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
327 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400329 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 button->type = ACPI_BUTTON_TYPE_SLEEPF;
331 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400332 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
333 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400335 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 button->type = ACPI_BUTTON_TYPE_LID;
Len Brown4be44fc2005-08-05 00:44:28 -0400337 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
338 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400340 } else {
Len Brown64684632006-06-26 23:41:38 -0400341 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
342 acpi_device_hid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 result = -ENODEV;
344 goto end;
345 }
346
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400347 result = acpi_button_add_fs(device);
348 if (result)
349 goto end;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 switch (button->type) {
352 case ACPI_BUTTON_TYPE_POWERF:
Len Brown4be44fc2005-08-05 00:44:28 -0400353 status =
354 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
355 acpi_button_notify_fixed,
356 button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
358 case ACPI_BUTTON_TYPE_SLEEPF:
Len Brown4be44fc2005-08-05 00:44:28 -0400359 status =
360 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
361 acpi_button_notify_fixed,
362 button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 default:
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400365 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400366 ACPI_DEVICE_NOTIFY,
367 acpi_button_notify,
368 button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 break;
370 }
371
372 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 result = -ENODEV;
374 goto end;
375 }
376
377 if (device->wakeup.flags.valid) {
378 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400379 acpi_set_gpe_type(device->wakeup.gpe_device,
380 device->wakeup.gpe_number,
381 ACPI_GPE_TYPE_WAKE_RUN);
382 acpi_enable_gpe(device->wakeup.gpe_device,
383 device->wakeup.gpe_number, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 device->wakeup.state.enabled = 1;
385 }
386
Len Brown4be44fc2005-08-05 00:44:28 -0400387 printk(KERN_INFO PREFIX "%s [%s]\n",
388 acpi_device_name(device), acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Len Brown4be44fc2005-08-05 00:44:28 -0400390 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (result) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400392 acpi_button_remove_fs(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 kfree(button);
394 }
395
Patrick Mocheld550d982006-06-27 00:41:40 -0400396 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Len Brown4be44fc2005-08-05 00:44:28 -0400399static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Len Brown4be44fc2005-08-05 00:44:28 -0400401 acpi_status status = 0;
402 struct acpi_button *button = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400406 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 button = acpi_driver_data(device);
409
410 /* Unregister for device notifications. */
411 switch (button->type) {
412 case ACPI_BUTTON_TYPE_POWERF:
Len Brown4be44fc2005-08-05 00:44:28 -0400413 status =
414 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
415 acpi_button_notify_fixed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 break;
417 case ACPI_BUTTON_TYPE_SLEEPF:
Len Brown4be44fc2005-08-05 00:44:28 -0400418 status =
419 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
420 acpi_button_notify_fixed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
422 default:
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400423 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400424 ACPI_DEVICE_NOTIFY,
425 acpi_button_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 break;
427 }
428
Len Brown4be44fc2005-08-05 00:44:28 -0400429 acpi_button_remove_fs(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 kfree(button);
432
Patrick Mocheld550d982006-06-27 00:41:40 -0400433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Len Brown4be44fc2005-08-05 00:44:28 -0400436static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Len Brown4be44fc2005-08-05 00:44:28 -0400438 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400441 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
442 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400443 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400444 acpi_button_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 result = acpi_bus_register_driver(&acpi_button_driver);
446 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400447 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400448 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
Patrick Mocheld550d982006-06-27 00:41:40 -0400451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Len Brown4be44fc2005-08-05 00:44:28 -0400454static void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 acpi_bus_unregister_driver(&acpi_button_driver);
458
Len Brown4be44fc2005-08-05 00:44:28 -0400459 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400460 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
461 if (acpi_sleep_dir)
462 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
463 if (acpi_lid_dir)
464 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
465 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
466
Patrick Mocheld550d982006-06-27 00:41:40 -0400467 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470module_init(acpi_button_init);
471module_exit(acpi_button_exit);