blob: 5ef885e82c78a94b7352307307ac4b5e35e0b08f [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 struct acpi_device *device; /* Fixed button kludge */
86 u8 type;
87 unsigned long pushed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Arjan van de Vend7508032006-07-04 13:06:00 -040090static const struct file_operations acpi_button_info_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -040091 .open = acpi_button_info_open_fs,
92 .read = seq_read,
93 .llseek = seq_lseek,
94 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -040095};
96
Arjan van de Vend7508032006-07-04 13:06:00 -040097static const struct file_operations acpi_button_state_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -040098 .open = acpi_button_state_open_fs,
99 .read = seq_read,
100 .llseek = seq_lseek,
101 .release = single_release,
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400102};
Len Brown4be44fc2005-08-05 00:44:28 -0400103
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400104/* --------------------------------------------------------------------------
105 FS Interface (/proc)
106 -------------------------------------------------------------------------- */
107
Len Brown4be44fc2005-08-05 00:44:28 -0400108static struct proc_dir_entry *acpi_button_dir;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400109
110static int acpi_button_info_seq_show(struct seq_file *seq, void *offset)
111{
Len Brown4be44fc2005-08-05 00:44:28 -0400112 struct acpi_button *button = (struct acpi_button *)seq->private;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400113
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400114
115 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400116 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400117
Len Brown4be44fc2005-08-05 00:44:28 -0400118 seq_printf(seq, "type: %s\n",
119 acpi_device_name(button->device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400120
Patrick Mocheld550d982006-06-27 00:41:40 -0400121 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400122}
123
124static int acpi_button_info_open_fs(struct inode *inode, struct file *file)
125{
126 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data);
127}
Len Brown4be44fc2005-08-05 00:44:28 -0400128
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400129static int acpi_button_state_seq_show(struct seq_file *seq, void *offset)
130{
Len Brown4be44fc2005-08-05 00:44:28 -0400131 struct acpi_button *button = (struct acpi_button *)seq->private;
132 acpi_status status;
133 unsigned long state;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400134
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400135
136 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400137 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400138
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400139 status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400140 if (ACPI_FAILURE(status)) {
141 seq_printf(seq, "state: unsupported\n");
Len Brown4be44fc2005-08-05 00:44:28 -0400142 } else {
143 seq_printf(seq, "state: %s\n",
144 (state ? "open" : "closed"));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400145 }
146
Patrick Mocheld550d982006-06-27 00:41:40 -0400147 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400148}
149
150static int acpi_button_state_open_fs(struct inode *inode, struct file *file)
151{
152 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data);
153}
154
155static struct proc_dir_entry *acpi_power_dir;
156static struct proc_dir_entry *acpi_sleep_dir;
157static struct proc_dir_entry *acpi_lid_dir;
158
Len Brown4be44fc2005-08-05 00:44:28 -0400159static int acpi_button_add_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400160{
Len Brown4be44fc2005-08-05 00:44:28 -0400161 struct proc_dir_entry *entry = NULL;
162 struct acpi_button *button = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400163
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400164
165 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400166 return -EINVAL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400167
168 button = acpi_driver_data(device);
169
170 switch (button->type) {
171 case ACPI_BUTTON_TYPE_POWER:
172 case ACPI_BUTTON_TYPE_POWERF:
173 if (!acpi_power_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400174 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER,
175 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400176 entry = acpi_power_dir;
177 break;
178 case ACPI_BUTTON_TYPE_SLEEP:
179 case ACPI_BUTTON_TYPE_SLEEPF:
180 if (!acpi_sleep_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400181 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP,
182 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400183 entry = acpi_sleep_dir;
184 break;
185 case ACPI_BUTTON_TYPE_LID:
186 if (!acpi_lid_dir)
Len Brown4be44fc2005-08-05 00:44:28 -0400187 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID,
188 acpi_button_dir);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400189 entry = acpi_lid_dir;
190 break;
191 }
192
193 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400194 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400195 entry->owner = THIS_MODULE;
196
197 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry);
198 if (!acpi_device_dir(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400199 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400200 acpi_device_dir(device)->owner = THIS_MODULE;
201
202 /* 'info' [R] */
203 entry = create_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400204 S_IRUGO, acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400205 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400206 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400207 else {
208 entry->proc_fops = &acpi_button_info_fops;
209 entry->data = acpi_driver_data(device);
210 entry->owner = THIS_MODULE;
211 }
212
213 /* show lid state [R] */
214 if (button->type == ACPI_BUTTON_TYPE_LID) {
215 entry = create_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400216 S_IRUGO, acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400217 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -0400218 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400219 else {
220 entry->proc_fops = &acpi_button_state_fops;
221 entry->data = acpi_driver_data(device);
222 entry->owner = THIS_MODULE;
223 }
224 }
225
Patrick Mocheld550d982006-06-27 00:41:40 -0400226 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400227}
228
Len Brown4be44fc2005-08-05 00:44:28 -0400229static int acpi_button_remove_fs(struct acpi_device *device)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400230{
Len Brown4be44fc2005-08-05 00:44:28 -0400231 struct acpi_button *button = NULL;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400232
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400233
234 button = acpi_driver_data(device);
235 if (acpi_device_dir(device)) {
236 if (button->type == ACPI_BUTTON_TYPE_LID)
237 remove_proc_entry(ACPI_BUTTON_FILE_STATE,
Len Brown4be44fc2005-08-05 00:44:28 -0400238 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400239 remove_proc_entry(ACPI_BUTTON_FILE_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400240 acpi_device_dir(device));
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400241
242 remove_proc_entry(acpi_device_bid(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400243 acpi_device_dir(device)->parent);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400244 acpi_device_dir(device) = NULL;
245 }
246
Patrick Mocheld550d982006-06-27 00:41:40 -0400247 return 0;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/* --------------------------------------------------------------------------
251 Driver Interface
252 -------------------------------------------------------------------------- */
253
Len Brown4be44fc2005-08-05 00:44:28 -0400254static void acpi_button_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Len Brown4be44fc2005-08-05 00:44:28 -0400256 struct acpi_button *button = (struct acpi_button *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 if (!button || !button->device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400260 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 switch (event) {
263 case ACPI_BUTTON_NOTIFY_STATUS:
Len Brown4be44fc2005-08-05 00:44:28 -0400264 acpi_bus_generate_event(button->device, event,
265 ++button->pushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break;
267 default:
268 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400269 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 break;
271 }
272
Patrick Mocheld550d982006-06-27 00:41:40 -0400273 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Len Brown4be44fc2005-08-05 00:44:28 -0400276static acpi_status acpi_button_notify_fixed(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
Len Brown4be44fc2005-08-05 00:44:28 -0400278 struct acpi_button *button = (struct acpi_button *)data;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400281 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400282 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400284 acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Patrick Mocheld550d982006-06-27 00:41:40 -0400286 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
Len Brown4be44fc2005-08-05 00:44:28 -0400289static int acpi_button_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Len Brown4be44fc2005-08-05 00:44:28 -0400291 int result = 0;
292 acpi_status status = AE_OK;
293 struct acpi_button *button = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400297 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 button = kmalloc(sizeof(struct acpi_button), GFP_KERNEL);
300 if (!button)
Patrick Mocheld550d982006-06-27 00:41:40 -0400301 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 memset(button, 0, sizeof(struct acpi_button));
303
304 button->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 acpi_driver_data(device) = button;
306
307 /*
308 * Determine the button type (via hid), as fixed-feature buttons
309 * need to be handled a bit differently than generic-space.
310 */
311 if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) {
312 button->type = ACPI_BUTTON_TYPE_POWER;
Len Brown4be44fc2005-08-05 00:44:28 -0400313 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER);
314 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400316 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 button->type = ACPI_BUTTON_TYPE_POWERF;
318 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400319 ACPI_BUTTON_DEVICE_NAME_POWERF);
320 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER);
Len Brown4be44fc2005-08-05 00:44:28 -0400322 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 button->type = ACPI_BUTTON_TYPE_SLEEP;
Len Brown4be44fc2005-08-05 00:44:28 -0400324 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP);
325 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400327 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 button->type = ACPI_BUTTON_TYPE_SLEEPF;
329 strcpy(acpi_device_name(device),
Len Brown4be44fc2005-08-05 00:44:28 -0400330 ACPI_BUTTON_DEVICE_NAME_SLEEPF);
331 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP);
Len Brown4be44fc2005-08-05 00:44:28 -0400333 } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 button->type = ACPI_BUTTON_TYPE_LID;
Len Brown4be44fc2005-08-05 00:44:28 -0400335 strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID);
336 sprintf(acpi_device_class(device), "%s/%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID);
Len Brown4be44fc2005-08-05 00:44:28 -0400338 } else {
Len Brown64684632006-06-26 23:41:38 -0400339 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n",
340 acpi_device_hid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 result = -ENODEV;
342 goto end;
343 }
344
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400345 result = acpi_button_add_fs(device);
346 if (result)
347 goto end;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 switch (button->type) {
350 case ACPI_BUTTON_TYPE_POWERF:
Len Brown4be44fc2005-08-05 00:44:28 -0400351 status =
352 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
353 acpi_button_notify_fixed,
354 button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 break;
356 case ACPI_BUTTON_TYPE_SLEEPF:
Len Brown4be44fc2005-08-05 00:44:28 -0400357 status =
358 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
359 acpi_button_notify_fixed,
360 button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 break;
362 default:
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400363 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400364 ACPI_DEVICE_NOTIFY,
365 acpi_button_notify,
366 button);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
368 }
369
370 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 result = -ENODEV;
372 goto end;
373 }
374
375 if (device->wakeup.flags.valid) {
376 /* Button's GPE is run-wake GPE */
Len Brown4be44fc2005-08-05 00:44:28 -0400377 acpi_set_gpe_type(device->wakeup.gpe_device,
378 device->wakeup.gpe_number,
379 ACPI_GPE_TYPE_WAKE_RUN);
380 acpi_enable_gpe(device->wakeup.gpe_device,
381 device->wakeup.gpe_number, ACPI_NOT_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 device->wakeup.state.enabled = 1;
383 }
384
Len Brown4be44fc2005-08-05 00:44:28 -0400385 printk(KERN_INFO PREFIX "%s [%s]\n",
386 acpi_device_name(device), acpi_device_bid(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Len Brown4be44fc2005-08-05 00:44:28 -0400388 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (result) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400390 acpi_button_remove_fs(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 kfree(button);
392 }
393
Patrick Mocheld550d982006-06-27 00:41:40 -0400394 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Len Brown4be44fc2005-08-05 00:44:28 -0400397static int acpi_button_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Len Brown4be44fc2005-08-05 00:44:28 -0400399 acpi_status status = 0;
400 struct acpi_button *button = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400404 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 button = acpi_driver_data(device);
407
408 /* Unregister for device notifications. */
409 switch (button->type) {
410 case ACPI_BUTTON_TYPE_POWERF:
Len Brown4be44fc2005-08-05 00:44:28 -0400411 status =
412 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
413 acpi_button_notify_fixed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 break;
415 case ACPI_BUTTON_TYPE_SLEEPF:
Len Brown4be44fc2005-08-05 00:44:28 -0400416 status =
417 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
418 acpi_button_notify_fixed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
420 default:
Patrick Mochel27b1d3e2006-05-19 16:54:42 -0400421 status = acpi_remove_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400422 ACPI_DEVICE_NOTIFY,
423 acpi_button_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 break;
425 }
426
Len Brown4be44fc2005-08-05 00:44:28 -0400427 acpi_button_remove_fs(device);
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 kfree(button);
430
Patrick Mocheld550d982006-06-27 00:41:40 -0400431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Len Brown4be44fc2005-08-05 00:44:28 -0400434static int __init acpi_button_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Len Brown4be44fc2005-08-05 00:44:28 -0400436 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400439 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir);
440 if (!acpi_button_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -0400441 return -ENODEV;
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400442 acpi_button_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 result = acpi_bus_register_driver(&acpi_button_driver);
444 if (result < 0) {
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400445 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -0400446 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
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 void __exit acpi_button_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 acpi_bus_unregister_driver(&acpi_button_driver);
456
Len Brown4be44fc2005-08-05 00:44:28 -0400457 if (acpi_power_dir)
Alexey Starikovskiyb34a8032005-08-03 17:55:21 -0400458 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir);
459 if (acpi_sleep_dir)
460 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir);
461 if (acpi_lid_dir)
462 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir);
463 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir);
464
Patrick Mocheld550d982006-06-27 00:41:40 -0400465 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468module_init(acpi_button_init);
469module_exit(acpi_button_exit);