Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 29 | #include <linux/types.h> |
| 30 | #include <linux/proc_fs.h> |
| 31 | #include <linux/seq_file.h> |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 32 | #include <linux/input.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <acpi/acpi_bus.h> |
| 34 | #include <acpi/acpi_drivers.h> |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #define ACPI_BUTTON_COMPONENT 0x00080000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #define ACPI_BUTTON_CLASS "button" |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 38 | #define ACPI_BUTTON_FILE_INFO "info" |
| 39 | #define ACPI_BUTTON_FILE_STATE "state" |
| 40 | #define ACPI_BUTTON_TYPE_UNKNOWN 0x00 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #define ACPI_BUTTON_NOTIFY_STATUS 0x80 |
| 42 | |
| 43 | #define ACPI_BUTTON_SUBCLASS_POWER "power" |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 44 | #define ACPI_BUTTON_HID_POWER "PNP0C0C" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #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 Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 63 | ACPI_MODULE_NAME("button"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 65 | MODULE_AUTHOR("Paul Diefenbaugh"); |
Len Brown | 7cda93e | 2007-02-12 23:50:02 -0500 | [diff] [blame] | 66 | MODULE_DESCRIPTION("ACPI Button Driver"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | MODULE_LICENSE("GPL"); |
| 68 | |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 69 | static const struct acpi_device_id button_device_ids[] = { |
| 70 | {ACPI_BUTTON_HID_LID, 0}, |
| 71 | {ACPI_BUTTON_HID_SLEEP, 0}, |
| 72 | {ACPI_BUTTON_HID_SLEEPF, 0}, |
| 73 | {ACPI_BUTTON_HID_POWER, 0}, |
| 74 | {ACPI_BUTTON_HID_POWERF, 0}, |
| 75 | {"", 0}, |
| 76 | }; |
| 77 | MODULE_DEVICE_TABLE(acpi, button_device_ids); |
| 78 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 79 | static int acpi_button_add(struct acpi_device *device); |
| 80 | static int acpi_button_remove(struct acpi_device *device, int type); |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 81 | static int acpi_button_info_open_fs(struct inode *inode, struct file *file); |
| 82 | static int acpi_button_state_open_fs(struct inode *inode, struct file *file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | static struct acpi_driver acpi_button_driver = { |
Len Brown | c2b6705 | 2007-02-12 23:33:40 -0500 | [diff] [blame] | 85 | .name = "button", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 86 | .class = ACPI_BUTTON_CLASS, |
Thomas Renninger | 1ba90e3 | 2007-07-23 14:44:41 +0200 | [diff] [blame] | 87 | .ids = button_device_ids, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 88 | .ops = { |
| 89 | .add = acpi_button_add, |
| 90 | .remove = acpi_button_remove, |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 91 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | struct acpi_button { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 95 | struct acpi_device *device; /* Fixed button kludge */ |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 96 | unsigned int type; |
| 97 | struct input_dev *input; |
| 98 | char phys[32]; /* for input device */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 99 | unsigned long pushed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 102 | static const struct file_operations acpi_button_info_fops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 103 | .open = acpi_button_info_open_fs, |
| 104 | .read = seq_read, |
| 105 | .llseek = seq_lseek, |
| 106 | .release = single_release, |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 107 | }; |
| 108 | |
Arjan van de Ven | d750803 | 2006-07-04 13:06:00 -0400 | [diff] [blame] | 109 | static const struct file_operations acpi_button_state_fops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 110 | .open = acpi_button_state_open_fs, |
| 111 | .read = seq_read, |
| 112 | .llseek = seq_lseek, |
| 113 | .release = single_release, |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 114 | }; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 115 | |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 116 | /* -------------------------------------------------------------------------- |
| 117 | FS Interface (/proc) |
| 118 | -------------------------------------------------------------------------- */ |
| 119 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 120 | static struct proc_dir_entry *acpi_button_dir; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 121 | |
| 122 | static int acpi_button_info_seq_show(struct seq_file *seq, void *offset) |
| 123 | { |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 124 | struct acpi_button *button = seq->private; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 125 | |
| 126 | if (!button || !button->device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 127 | return 0; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 128 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 129 | seq_printf(seq, "type: %s\n", |
| 130 | acpi_device_name(button->device)); |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 131 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 132 | return 0; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static 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 Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 139 | |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 140 | static int acpi_button_state_seq_show(struct seq_file *seq, void *offset) |
| 141 | { |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 142 | struct acpi_button *button = seq->private; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 143 | acpi_status status; |
| 144 | unsigned long state; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 145 | |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 146 | if (!button || !button->device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 147 | return 0; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 148 | |
Patrick Mochel | 27b1d3e | 2006-05-19 16:54:42 -0400 | [diff] [blame] | 149 | status = acpi_evaluate_integer(button->device->handle, "_LID", NULL, &state); |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 150 | seq_printf(seq, "state: %s\n", |
| 151 | ACPI_FAILURE(status) ? "unsupported" : |
| 152 | (state ? "open" : "closed")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 153 | return 0; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static int acpi_button_state_open_fs(struct inode *inode, struct file *file) |
| 157 | { |
| 158 | return single_open(file, acpi_button_state_seq_show, PDE(inode)->data); |
| 159 | } |
| 160 | |
| 161 | static struct proc_dir_entry *acpi_power_dir; |
| 162 | static struct proc_dir_entry *acpi_sleep_dir; |
| 163 | static struct proc_dir_entry *acpi_lid_dir; |
| 164 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 165 | static int acpi_button_add_fs(struct acpi_device *device) |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 166 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 167 | struct proc_dir_entry *entry = NULL; |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 168 | struct acpi_button *button; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 169 | |
| 170 | if (!device || !acpi_driver_data(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 171 | return -EINVAL; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 172 | |
| 173 | button = acpi_driver_data(device); |
| 174 | |
| 175 | switch (button->type) { |
| 176 | case ACPI_BUTTON_TYPE_POWER: |
| 177 | case ACPI_BUTTON_TYPE_POWERF: |
| 178 | if (!acpi_power_dir) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 179 | acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER, |
| 180 | acpi_button_dir); |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 181 | entry = acpi_power_dir; |
| 182 | break; |
| 183 | case ACPI_BUTTON_TYPE_SLEEP: |
| 184 | case ACPI_BUTTON_TYPE_SLEEPF: |
| 185 | if (!acpi_sleep_dir) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 186 | acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP, |
| 187 | acpi_button_dir); |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 188 | entry = acpi_sleep_dir; |
| 189 | break; |
| 190 | case ACPI_BUTTON_TYPE_LID: |
| 191 | if (!acpi_lid_dir) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 192 | acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, |
| 193 | acpi_button_dir); |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 194 | entry = acpi_lid_dir; |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | if (!entry) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 199 | return -ENODEV; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 200 | entry->owner = THIS_MODULE; |
| 201 | |
| 202 | acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry); |
| 203 | if (!acpi_device_dir(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 204 | return -ENODEV; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 205 | acpi_device_dir(device)->owner = THIS_MODULE; |
| 206 | |
| 207 | /* 'info' [R] */ |
| 208 | entry = create_proc_entry(ACPI_BUTTON_FILE_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 209 | S_IRUGO, acpi_device_dir(device)); |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 210 | if (!entry) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 211 | return -ENODEV; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 212 | else { |
| 213 | entry->proc_fops = &acpi_button_info_fops; |
| 214 | entry->data = acpi_driver_data(device); |
| 215 | entry->owner = THIS_MODULE; |
| 216 | } |
| 217 | |
| 218 | /* show lid state [R] */ |
| 219 | if (button->type == ACPI_BUTTON_TYPE_LID) { |
| 220 | entry = create_proc_entry(ACPI_BUTTON_FILE_STATE, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 221 | S_IRUGO, acpi_device_dir(device)); |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 222 | if (!entry) |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 223 | return -ENODEV; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 224 | else { |
| 225 | entry->proc_fops = &acpi_button_state_fops; |
| 226 | entry->data = acpi_driver_data(device); |
| 227 | entry->owner = THIS_MODULE; |
| 228 | } |
| 229 | } |
| 230 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 231 | return 0; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 234 | static int acpi_button_remove_fs(struct acpi_device *device) |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 235 | { |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 236 | struct acpi_button *button = acpi_driver_data(device); |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 237 | |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 238 | if (acpi_device_dir(device)) { |
| 239 | if (button->type == ACPI_BUTTON_TYPE_LID) |
| 240 | remove_proc_entry(ACPI_BUTTON_FILE_STATE, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 241 | acpi_device_dir(device)); |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 242 | remove_proc_entry(ACPI_BUTTON_FILE_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 243 | acpi_device_dir(device)); |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 244 | |
| 245 | remove_proc_entry(acpi_device_bid(device), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 246 | acpi_device_dir(device)->parent); |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 247 | acpi_device_dir(device) = NULL; |
| 248 | } |
| 249 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 250 | return 0; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 251 | } |
| 252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | /* -------------------------------------------------------------------------- |
| 254 | Driver Interface |
| 255 | -------------------------------------------------------------------------- */ |
| 256 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 257 | static void acpi_button_notify(acpi_handle handle, u32 event, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 259 | struct acpi_button *button = data; |
| 260 | struct input_dev *input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
| 262 | if (!button || !button->device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 263 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | switch (event) { |
| 266 | case ACPI_BUTTON_NOTIFY_STATUS: |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 267 | input = button->input; |
| 268 | |
| 269 | if (button->type == ACPI_BUTTON_TYPE_LID) { |
| 270 | struct acpi_handle *handle = button->device->handle; |
| 271 | unsigned long state; |
| 272 | |
| 273 | if (!ACPI_FAILURE(acpi_evaluate_integer(handle, "_LID", |
| 274 | NULL, &state))) |
| 275 | input_report_switch(input, SW_LID, !state); |
| 276 | |
| 277 | } else { |
| 278 | int keycode = test_bit(KEY_SLEEP, input->keybit) ? |
| 279 | KEY_SLEEP : KEY_POWER; |
| 280 | |
| 281 | input_report_key(input, keycode, 1); |
| 282 | input_sync(input); |
| 283 | input_report_key(input, keycode, 0); |
| 284 | } |
| 285 | input_sync(input); |
| 286 | |
Len Brown | 14e04fb | 2007-08-23 15:20:26 -0400 | [diff] [blame] | 287 | acpi_bus_generate_proc_event(button->device, event, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 288 | ++button->pushed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | break; |
| 290 | default: |
| 291 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 292 | "Unsupported event [0x%x]\n", event)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | break; |
| 294 | } |
| 295 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 296 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 299 | static acpi_status acpi_button_notify_fixed(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 301 | struct acpi_button *button = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 303 | if (!button) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 304 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Patrick Mochel | 27b1d3e | 2006-05-19 16:54:42 -0400 | [diff] [blame] | 306 | acpi_button_notify(button->device->handle, ACPI_BUTTON_NOTIFY_STATUS, button); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 308 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 311 | static int acpi_button_install_notify_handlers(struct acpi_button *button) |
| 312 | { |
| 313 | acpi_status status; |
| 314 | |
| 315 | switch (button->type) { |
| 316 | case ACPI_BUTTON_TYPE_POWERF: |
| 317 | status = |
| 318 | acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, |
| 319 | acpi_button_notify_fixed, |
| 320 | button); |
| 321 | break; |
| 322 | case ACPI_BUTTON_TYPE_SLEEPF: |
| 323 | status = |
| 324 | acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, |
| 325 | acpi_button_notify_fixed, |
| 326 | button); |
| 327 | break; |
| 328 | default: |
| 329 | status = acpi_install_notify_handler(button->device->handle, |
| 330 | ACPI_DEVICE_NOTIFY, |
| 331 | acpi_button_notify, |
| 332 | button); |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | return ACPI_FAILURE(status) ? -ENODEV : 0; |
| 337 | } |
| 338 | |
| 339 | static void acpi_button_remove_notify_handlers(struct acpi_button *button) |
| 340 | { |
| 341 | switch (button->type) { |
| 342 | case ACPI_BUTTON_TYPE_POWERF: |
| 343 | acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, |
| 344 | acpi_button_notify_fixed); |
| 345 | break; |
| 346 | case ACPI_BUTTON_TYPE_SLEEPF: |
| 347 | acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, |
| 348 | acpi_button_notify_fixed); |
| 349 | break; |
| 350 | default: |
| 351 | acpi_remove_notify_handler(button->device->handle, |
| 352 | ACPI_DEVICE_NOTIFY, |
| 353 | acpi_button_notify); |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 358 | static int acpi_button_add(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | { |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 360 | int error; |
| 361 | struct acpi_button *button; |
| 362 | struct input_dev *input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
| 364 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 365 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 367 | button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | if (!button) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 369 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | button->device = device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | acpi_driver_data(device) = button; |
| 373 | |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 374 | button->input = input = input_allocate_device(); |
| 375 | if (!input) { |
| 376 | error = -ENOMEM; |
| 377 | goto err_free_button; |
| 378 | } |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* |
| 381 | * Determine the button type (via hid), as fixed-feature buttons |
| 382 | * need to be handled a bit differently than generic-space. |
| 383 | */ |
| 384 | if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWER)) { |
| 385 | button->type = ACPI_BUTTON_TYPE_POWER; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 386 | strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_POWER); |
| 387 | sprintf(acpi_device_class(device), "%s/%s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 389 | } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_POWERF)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | button->type = ACPI_BUTTON_TYPE_POWERF; |
| 391 | strcpy(acpi_device_name(device), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 392 | ACPI_BUTTON_DEVICE_NAME_POWERF); |
| 393 | sprintf(acpi_device_class(device), "%s/%s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 395 | } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEP)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | button->type = ACPI_BUTTON_TYPE_SLEEP; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 397 | strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_SLEEP); |
| 398 | sprintf(acpi_device_class(device), "%s/%s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 400 | } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_SLEEPF)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | button->type = ACPI_BUTTON_TYPE_SLEEPF; |
| 402 | strcpy(acpi_device_name(device), |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 403 | ACPI_BUTTON_DEVICE_NAME_SLEEPF); |
| 404 | sprintf(acpi_device_class(device), "%s/%s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 406 | } else if (!strcmp(acpi_device_hid(device), ACPI_BUTTON_HID_LID)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | button->type = ACPI_BUTTON_TYPE_LID; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 408 | strcpy(acpi_device_name(device), ACPI_BUTTON_DEVICE_NAME_LID); |
| 409 | sprintf(acpi_device_class(device), "%s/%s", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 411 | } else { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 412 | printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", |
| 413 | acpi_device_hid(device)); |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 414 | error = -ENODEV; |
| 415 | goto err_free_input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 418 | error = acpi_button_add_fs(device); |
| 419 | if (error) |
| 420 | goto err_free_input; |
| 421 | |
| 422 | error = acpi_button_install_notify_handlers(button); |
| 423 | if (error) |
| 424 | goto err_remove_fs; |
| 425 | |
| 426 | snprintf(button->phys, sizeof(button->phys), |
| 427 | "%s/button/input0", acpi_device_hid(device)); |
| 428 | |
| 429 | input->name = acpi_device_name(device); |
| 430 | input->phys = button->phys; |
| 431 | input->id.bustype = BUS_HOST; |
| 432 | input->id.product = button->type; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | switch (button->type) { |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 435 | case ACPI_BUTTON_TYPE_POWER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | case ACPI_BUTTON_TYPE_POWERF: |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 437 | input->evbit[0] = BIT(EV_KEY); |
| 438 | set_bit(KEY_POWER, input->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | break; |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 440 | |
| 441 | case ACPI_BUTTON_TYPE_SLEEP: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | case ACPI_BUTTON_TYPE_SLEEPF: |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 443 | input->evbit[0] = BIT(EV_KEY); |
| 444 | set_bit(KEY_SLEEP, input->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | break; |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 446 | |
| 447 | case ACPI_BUTTON_TYPE_LID: |
| 448 | input->evbit[0] = BIT(EV_SW); |
| 449 | set_bit(SW_LID, input->swbit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | break; |
| 451 | } |
| 452 | |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 453 | error = input_register_device(input); |
| 454 | if (error) |
| 455 | goto err_remove_handlers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
| 457 | if (device->wakeup.flags.valid) { |
| 458 | /* Button's GPE is run-wake GPE */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 459 | acpi_set_gpe_type(device->wakeup.gpe_device, |
| 460 | device->wakeup.gpe_number, |
| 461 | ACPI_GPE_TYPE_WAKE_RUN); |
| 462 | acpi_enable_gpe(device->wakeup.gpe_device, |
| 463 | device->wakeup.gpe_number, ACPI_NOT_ISR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | device->wakeup.state.enabled = 1; |
| 465 | } |
| 466 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 467 | printk(KERN_INFO PREFIX "%s [%s]\n", |
| 468 | acpi_device_name(device), acpi_device_bid(device)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 470 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 472 | err_remove_handlers: |
| 473 | acpi_button_remove_notify_handlers(button); |
| 474 | err_remove_fs: |
| 475 | acpi_button_remove_fs(device); |
| 476 | err_free_input: |
| 477 | input_free_device(input); |
| 478 | err_free_button: |
| 479 | kfree(button); |
| 480 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 483 | static int acpi_button_remove(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | { |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 485 | struct acpi_button *button; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
| 487 | if (!device || !acpi_driver_data(device)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 488 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
| 490 | button = acpi_driver_data(device); |
| 491 | |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 492 | acpi_button_remove_notify_handlers(button); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 493 | acpi_button_remove_fs(device); |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 494 | input_unregister_device(button->input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | kfree(button); |
| 496 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 497 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 500 | static int __init acpi_button_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
Dmitry Torokhov | c0968f0 | 2006-11-09 00:40:13 -0500 | [diff] [blame] | 502 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 504 | acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir); |
| 505 | if (!acpi_button_dir) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 506 | return -ENODEV; |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 507 | acpi_button_dir->owner = THIS_MODULE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | result = acpi_bus_register_driver(&acpi_button_driver); |
| 509 | if (result < 0) { |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 510 | remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 511 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 514 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 517 | static void __exit acpi_button_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | acpi_bus_unregister_driver(&acpi_button_driver); |
| 520 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 521 | if (acpi_power_dir) |
Alexey Starikovskiy | b34a803 | 2005-08-03 17:55:21 -0400 | [diff] [blame] | 522 | remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir); |
| 523 | if (acpi_sleep_dir) |
| 524 | remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir); |
| 525 | if (acpi_lid_dir) |
| 526 | remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir); |
| 527 | remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | module_init(acpi_button_init); |
| 531 | module_exit(acpi_button_exit); |