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