Blame view
drivers/acpi/button.c
12 KB
1da177e4c
|
1 |
/* |
50a4da890
|
2 |
* button.c - ACPI Button Driver |
1da177e4c
|
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
* * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> |
b34a8030e
|
29 30 31 |
#include <linux/types.h> #include <linux/proc_fs.h> #include <linux/seq_file.h> |
c0968f0ea
|
32 |
#include <linux/input.h> |
5a0e3ad6a
|
33 |
#include <linux/slab.h> |
1da177e4c
|
34 35 |
#include <acpi/acpi_bus.h> #include <acpi/acpi_drivers.h> |
6270da6f4
|
36 |
#include <acpi/button.h> |
1da177e4c
|
37 |
|
a192a9580
|
38 |
#define PREFIX "ACPI: " |
1da177e4c
|
39 |
#define ACPI_BUTTON_CLASS "button" |
b34a8030e
|
40 41 42 |
#define ACPI_BUTTON_FILE_INFO "info" #define ACPI_BUTTON_FILE_STATE "state" #define ACPI_BUTTON_TYPE_UNKNOWN 0x00 |
1da177e4c
|
43 44 45 |
#define ACPI_BUTTON_NOTIFY_STATUS 0x80 #define ACPI_BUTTON_SUBCLASS_POWER "power" |
4be44fcd3
|
46 |
#define ACPI_BUTTON_HID_POWER "PNP0C0C" |
d68b597c8
|
47 |
#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button" |
1da177e4c
|
48 |
#define ACPI_BUTTON_TYPE_POWER 0x01 |
1da177e4c
|
49 50 51 |
#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep" #define ACPI_BUTTON_HID_SLEEP "PNP0C0E" |
d68b597c8
|
52 |
#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button" |
1da177e4c
|
53 |
#define ACPI_BUTTON_TYPE_SLEEP 0x03 |
1da177e4c
|
54 55 56 57 58 59 60 |
#define ACPI_BUTTON_SUBCLASS_LID "lid" #define ACPI_BUTTON_HID_LID "PNP0C0D" #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch" #define ACPI_BUTTON_TYPE_LID 0x05 #define _COMPONENT ACPI_BUTTON_COMPONENT |
f52fd66d2
|
61 |
ACPI_MODULE_NAME("button"); |
1da177e4c
|
62 |
|
c0968f0ea
|
63 |
MODULE_AUTHOR("Paul Diefenbaugh"); |
7cda93e00
|
64 |
MODULE_DESCRIPTION("ACPI Button Driver"); |
1da177e4c
|
65 |
MODULE_LICENSE("GPL"); |
1ba90e3a8
|
66 67 68 69 70 71 72 73 74 |
static const struct acpi_device_id button_device_ids[] = { {ACPI_BUTTON_HID_LID, 0}, {ACPI_BUTTON_HID_SLEEP, 0}, {ACPI_BUTTON_HID_SLEEPF, 0}, {ACPI_BUTTON_HID_POWER, 0}, {ACPI_BUTTON_HID_POWERF, 0}, {"", 0}, }; MODULE_DEVICE_TABLE(acpi, button_device_ids); |
4be44fcd3
|
75 |
static int acpi_button_add(struct acpi_device *device); |
51fac8388
|
76 |
static int acpi_button_remove(struct acpi_device *device); |
373cfc360
|
77 |
static void acpi_button_notify(struct acpi_device *device, u32 event); |
1da177e4c
|
78 |
|
906924048
|
79 |
#ifdef CONFIG_PM_SLEEP |
1be532de8
|
80 |
static int acpi_button_resume(struct device *dev); |
906924048
|
81 |
#endif |
1be532de8
|
82 |
static SIMPLE_DEV_PM_OPS(acpi_button_pm, NULL, acpi_button_resume); |
1da177e4c
|
83 |
static struct acpi_driver acpi_button_driver = { |
c2b6705b7
|
84 |
.name = "button", |
4be44fcd3
|
85 |
.class = ACPI_BUTTON_CLASS, |
1ba90e3a8
|
86 |
.ids = button_device_ids, |
4be44fcd3
|
87 88 89 |
.ops = { .add = acpi_button_add, .remove = acpi_button_remove, |
373cfc360
|
90 |
.notify = acpi_button_notify, |
c0968f0ea
|
91 |
}, |
1be532de8
|
92 |
.drv.pm = &acpi_button_pm, |
1da177e4c
|
93 94 95 |
}; struct acpi_button { |
c0968f0ea
|
96 97 98 |
unsigned int type; struct input_dev *input; char phys[32]; /* for input device */ |
4be44fcd3
|
99 |
unsigned long pushed; |
c19f9a84e
|
100 |
bool wakeup_enabled; |
1da177e4c
|
101 |
}; |
7e12715ec
|
102 103 |
static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier); static struct acpi_device *lid_device; |
b34a8030e
|
104 105 106 |
/* -------------------------------------------------------------------------- FS Interface (/proc) -------------------------------------------------------------------------- */ |
4be44fcd3
|
107 |
static struct proc_dir_entry *acpi_button_dir; |
912b7427f
|
108 |
static struct proc_dir_entry *acpi_lid_dir; |
4be44fcd3
|
109 |
|
b34a8030e
|
110 111 |
static int acpi_button_state_seq_show(struct seq_file *seq, void *offset) { |
106c19e7b
|
112 |
struct acpi_device *device = seq->private; |
4be44fcd3
|
113 |
acpi_status status; |
27663c585
|
114 |
unsigned long long state; |
b34a8030e
|
115 |
|
106c19e7b
|
116 |
status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state); |
c0968f0ea
|
117 118 119 120 |
seq_printf(seq, "state: %s ", ACPI_FAILURE(status) ? "unsupported" : (state ? "open" : "closed")); |
d550d98d3
|
121 |
return 0; |
b34a8030e
|
122 123 124 125 |
} static int acpi_button_state_open_fs(struct inode *inode, struct file *file) { |
d9dda78ba
|
126 |
return single_open(file, acpi_button_state_seq_show, PDE_DATA(inode)); |
b34a8030e
|
127 |
} |
912b7427f
|
128 129 130 131 132 133 134 |
static const struct file_operations acpi_button_state_fops = { .owner = THIS_MODULE, .open = acpi_button_state_open_fs, .read = seq_read, .llseek = seq_lseek, .release = single_release, }; |
b34a8030e
|
135 |
|
4be44fcd3
|
136 |
static int acpi_button_add_fs(struct acpi_device *device) |
b34a8030e
|
137 |
{ |
1bce81131
|
138 |
struct acpi_button *button = acpi_driver_data(device); |
4be44fcd3
|
139 |
struct proc_dir_entry *entry = NULL; |
912b7427f
|
140 |
int ret = 0; |
b34a8030e
|
141 |
|
912b7427f
|
142 143 144 145 146 147 148 149 |
/* procfs I/F for ACPI lid device only */ if (button->type != ACPI_BUTTON_TYPE_LID) return 0; if (acpi_button_dir || acpi_lid_dir) { printk(KERN_ERR PREFIX "More than one Lid device found! "); return -EEXIST; |
b34a8030e
|
150 |
} |
912b7427f
|
151 152 153 |
/* create /proc/acpi/button */ acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir); if (!acpi_button_dir) |
d550d98d3
|
154 |
return -ENODEV; |
b34a8030e
|
155 |
|
912b7427f
|
156 157 158 159 160 161 |
/* create /proc/acpi/button/lid */ acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir); if (!acpi_lid_dir) { ret = -ENODEV; goto remove_button_dir; } |
b34a8030e
|
162 |
|
912b7427f
|
163 164 165 166 167 168 |
/* create /proc/acpi/button/lid/LID/ */ acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir); if (!acpi_device_dir(device)) { ret = -ENODEV; goto remove_lid_dir; } |
b34a8030e
|
169 |
|
912b7427f
|
170 171 172 173 174 175 176 |
/* create /proc/acpi/button/lid/LID/state */ entry = proc_create_data(ACPI_BUTTON_FILE_STATE, S_IRUGO, acpi_device_dir(device), &acpi_button_state_fops, device); if (!entry) { ret = -ENODEV; goto remove_dev_dir; |
b34a8030e
|
177 |
} |
912b7427f
|
178 179 180 181 182 183 184 185 186 187 188 189 |
done: return ret; remove_dev_dir: remove_proc_entry(acpi_device_bid(device), acpi_lid_dir); acpi_device_dir(device) = NULL; remove_lid_dir: remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir); remove_button_dir: remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir); goto done; |
b34a8030e
|
190 |
} |
4be44fcd3
|
191 |
static int acpi_button_remove_fs(struct acpi_device *device) |
b34a8030e
|
192 |
{ |
c0968f0ea
|
193 |
struct acpi_button *button = acpi_driver_data(device); |
b34a8030e
|
194 |
|
912b7427f
|
195 196 |
if (button->type != ACPI_BUTTON_TYPE_LID) return 0; |
b34a8030e
|
197 |
|
912b7427f
|
198 199 200 201 202 203 204 |
remove_proc_entry(ACPI_BUTTON_FILE_STATE, acpi_device_dir(device)); remove_proc_entry(acpi_device_bid(device), acpi_lid_dir); acpi_device_dir(device) = NULL; remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir); remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir); |
b34a8030e
|
205 |
|
d550d98d3
|
206 |
return 0; |
b34a8030e
|
207 |
} |
1da177e4c
|
208 209 210 |
/* -------------------------------------------------------------------------- Driver Interface -------------------------------------------------------------------------- */ |
7e12715ec
|
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
int acpi_lid_notifier_register(struct notifier_block *nb) { return blocking_notifier_chain_register(&acpi_lid_notifier, nb); } EXPORT_SYMBOL(acpi_lid_notifier_register); int acpi_lid_notifier_unregister(struct notifier_block *nb) { return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb); } EXPORT_SYMBOL(acpi_lid_notifier_unregister); int acpi_lid_open(void) { acpi_status status; unsigned long long state; |
2c907b72d
|
227 228 |
if (!lid_device) return -ENODEV; |
7e12715ec
|
229 230 231 232 233 234 235 236 |
status = acpi_evaluate_integer(lid_device->handle, "_LID", NULL, &state); if (ACPI_FAILURE(status)) return -ENODEV; return !!state; } EXPORT_SYMBOL(acpi_lid_open); |
106c19e7b
|
237 |
static int acpi_lid_send_state(struct acpi_device *device) |
23de5d9ef
|
238 |
{ |
106c19e7b
|
239 |
struct acpi_button *button = acpi_driver_data(device); |
27663c585
|
240 |
unsigned long long state; |
23de5d9ef
|
241 |
acpi_status status; |
7e12715ec
|
242 |
int ret; |
23de5d9ef
|
243 |
|
106c19e7b
|
244 |
status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state); |
23de5d9ef
|
245 246 |
if (ACPI_FAILURE(status)) return -ENODEV; |
50a4da890
|
247 |
|
23de5d9ef
|
248 249 |
/* input layer checks if event is redundant */ input_report_switch(button->input, SW_LID, !state); |
df316e939
|
250 |
input_sync(button->input); |
7e12715ec
|
251 |
|
1f83511bd
|
252 253 |
if (state) pm_wakeup_event(&device->dev, 0); |
7e12715ec
|
254 255 256 257 |
ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device); if (ret == NOTIFY_DONE) ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device); |
13c199c0d
|
258 259 260 261 262 263 264 |
if (ret == NOTIFY_DONE || ret == NOTIFY_OK) { /* * It is also regarded as success if the notifier_chain * returns NOTIFY_OK or NOTIFY_DONE. */ ret = 0; } |
7e12715ec
|
265 |
return ret; |
23de5d9ef
|
266 |
} |
1da177e4c
|
267 |
|
373cfc360
|
268 |
static void acpi_button_notify(struct acpi_device *device, u32 event) |
1da177e4c
|
269 |
{ |
373cfc360
|
270 |
struct acpi_button *button = acpi_driver_data(device); |
c0968f0ea
|
271 |
struct input_dev *input; |
1da177e4c
|
272 |
|
1da177e4c
|
273 |
switch (event) { |
373cfc360
|
274 275 276 |
case ACPI_FIXED_HARDWARE_EVENT: event = ACPI_BUTTON_NOTIFY_STATUS; /* fall through */ |
1da177e4c
|
277 |
case ACPI_BUTTON_NOTIFY_STATUS: |
c0968f0ea
|
278 |
input = button->input; |
c0968f0ea
|
279 |
if (button->type == ACPI_BUTTON_TYPE_LID) { |
106c19e7b
|
280 |
acpi_lid_send_state(device); |
c0968f0ea
|
281 282 283 284 285 286 287 |
} else { int keycode = test_bit(KEY_SLEEP, input->keybit) ? KEY_SLEEP : KEY_POWER; input_report_key(input, keycode, 1); input_sync(input); input_report_key(input, keycode, 0); |
df316e939
|
288 |
input_sync(input); |
1f83511bd
|
289 290 |
pm_wakeup_event(&device->dev, 0); |
c0968f0ea
|
291 |
} |
1da177e4c
|
292 293 294 |
break; default: ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
4be44fcd3
|
295 296 |
"Unsupported event [0x%x] ", event)); |
1da177e4c
|
297 298 |
break; } |
1da177e4c
|
299 |
} |
906924048
|
300 |
#ifdef CONFIG_PM_SLEEP |
1be532de8
|
301 |
static int acpi_button_resume(struct device *dev) |
23de5d9ef
|
302 |
{ |
1be532de8
|
303 |
struct acpi_device *device = to_acpi_device(dev); |
1bce81131
|
304 |
struct acpi_button *button = acpi_driver_data(device); |
50a4da890
|
305 |
|
e2fb9754d
|
306 |
if (button->type == ACPI_BUTTON_TYPE_LID) |
106c19e7b
|
307 |
return acpi_lid_send_state(device); |
23de5d9ef
|
308 309 |
return 0; } |
906924048
|
310 |
#endif |
23de5d9ef
|
311 |
|
c0968f0ea
|
312 313 |
static int acpi_button_add(struct acpi_device *device) { |
c0968f0ea
|
314 315 |
struct acpi_button *button; struct input_dev *input; |
620e112cf
|
316 317 |
const char *hid = acpi_device_hid(device); char *name, *class; |
1bce81131
|
318 |
int error; |
1da177e4c
|
319 |
|
c0968f0ea
|
320 |
button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL); |
1da177e4c
|
321 |
if (!button) |
d550d98d3
|
322 |
return -ENOMEM; |
1da177e4c
|
323 |
|
db89b4f0d
|
324 |
device->driver_data = button; |
1da177e4c
|
325 |
|
c0968f0ea
|
326 327 328 329 330 |
button->input = input = input_allocate_device(); if (!input) { error = -ENOMEM; goto err_free_button; } |
bf04a7722
|
331 332 |
name = acpi_device_name(device); class = acpi_device_class(device); |
d68b597c8
|
333 334 |
if (!strcmp(hid, ACPI_BUTTON_HID_POWER) || !strcmp(hid, ACPI_BUTTON_HID_POWERF)) { |
1da177e4c
|
335 |
button->type = ACPI_BUTTON_TYPE_POWER; |
bf04a7722
|
336 337 |
strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER); sprintf(class, "%s/%s", |
1da177e4c
|
338 |
ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER); |
d68b597c8
|
339 340 |
} else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) || !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) { |
1da177e4c
|
341 |
button->type = ACPI_BUTTON_TYPE_SLEEP; |
bf04a7722
|
342 343 |
strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP); sprintf(class, "%s/%s", |
1da177e4c
|
344 |
ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP); |
bf04a7722
|
345 |
} else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) { |
1da177e4c
|
346 |
button->type = ACPI_BUTTON_TYPE_LID; |
bf04a7722
|
347 348 |
strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID); sprintf(class, "%s/%s", |
1da177e4c
|
349 |
ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID); |
4be44fcd3
|
350 |
} else { |
bf04a7722
|
351 352 |
printk(KERN_ERR PREFIX "Unsupported hid [%s] ", hid); |
c0968f0ea
|
353 354 |
error = -ENODEV; goto err_free_input; |
1da177e4c
|
355 |
} |
c0968f0ea
|
356 357 358 |
error = acpi_button_add_fs(device); if (error) goto err_free_input; |
bf04a7722
|
359 |
snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid); |
c0968f0ea
|
360 |
|
bf04a7722
|
361 |
input->name = name; |
c0968f0ea
|
362 363 364 |
input->phys = button->phys; input->id.bustype = BUS_HOST; input->id.product = button->type; |
3b34e5232
|
365 |
input->dev.parent = &device->dev; |
b34a8030e
|
366 |
|
1da177e4c
|
367 |
switch (button->type) { |
c0968f0ea
|
368 |
case ACPI_BUTTON_TYPE_POWER: |
763f527b6
|
369 |
input_set_capability(input, EV_KEY, KEY_POWER); |
1da177e4c
|
370 |
break; |
c0968f0ea
|
371 372 |
case ACPI_BUTTON_TYPE_SLEEP: |
763f527b6
|
373 |
input_set_capability(input, EV_KEY, KEY_SLEEP); |
1da177e4c
|
374 |
break; |
c0968f0ea
|
375 376 |
case ACPI_BUTTON_TYPE_LID: |
763f527b6
|
377 |
input_set_capability(input, EV_SW, SW_LID); |
1da177e4c
|
378 379 |
break; } |
c0968f0ea
|
380 381 |
error = input_register_device(input); if (error) |
373cfc360
|
382 |
goto err_remove_fs; |
7e12715ec
|
383 |
if (button->type == ACPI_BUTTON_TYPE_LID) { |
106c19e7b
|
384 |
acpi_lid_send_state(device); |
7e12715ec
|
385 386 387 388 389 390 |
/* * This assumes there's only one lid device, or if there are * more we only care about the last one... */ lid_device = device; } |
1da177e4c
|
391 392 393 |
if (device->wakeup.flags.valid) { /* Button's GPE is run-wake GPE */ |
4be44fcd3
|
394 |
acpi_enable_gpe(device->wakeup.gpe_device, |
a44061aa8
|
395 |
device->wakeup.gpe_number); |
c19f9a84e
|
396 397 398 399 |
if (!device_may_wakeup(&device->dev)) { device_set_wakeup_enable(&device->dev, true); button->wakeup_enabled = true; } |
1da177e4c
|
400 |
} |
bf04a7722
|
401 402 |
printk(KERN_INFO PREFIX "%s [%s] ", name, acpi_device_bid(device)); |
c0968f0ea
|
403 |
return 0; |
1da177e4c
|
404 |
|
c0968f0ea
|
405 406 407 408 409 410 411 |
err_remove_fs: acpi_button_remove_fs(device); err_free_input: input_free_device(input); err_free_button: kfree(button); return error; |
1da177e4c
|
412 |
} |
51fac8388
|
413 |
static int acpi_button_remove(struct acpi_device *device) |
1da177e4c
|
414 |
{ |
1bce81131
|
415 |
struct acpi_button *button = acpi_driver_data(device); |
1da177e4c
|
416 |
|
9630bdd9b
|
417 418 |
if (device->wakeup.flags.valid) { acpi_disable_gpe(device->wakeup.gpe_device, |
a44061aa8
|
419 |
device->wakeup.gpe_number); |
c19f9a84e
|
420 421 |
if (button->wakeup_enabled) device_set_wakeup_enable(&device->dev, false); |
9630bdd9b
|
422 |
} |
4be44fcd3
|
423 |
acpi_button_remove_fs(device); |
c0968f0ea
|
424 |
input_unregister_device(button->input); |
1da177e4c
|
425 |
kfree(button); |
d550d98d3
|
426 |
return 0; |
1da177e4c
|
427 |
} |
466e78f77
|
428 |
module_acpi_driver(acpi_button_driver); |