at v2.6.38 14 kB view raw
1/* 2 * button.c - ACPI Button Driver 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> 29#include <linux/types.h> 30#include <linux/proc_fs.h> 31#include <linux/seq_file.h> 32#include <linux/input.h> 33#include <linux/slab.h> 34#include <acpi/acpi_bus.h> 35#include <acpi/acpi_drivers.h> 36 37#define PREFIX "ACPI: " 38 39#define ACPI_BUTTON_CLASS "button" 40#define ACPI_BUTTON_FILE_INFO "info" 41#define ACPI_BUTTON_FILE_STATE "state" 42#define ACPI_BUTTON_TYPE_UNKNOWN 0x00 43#define ACPI_BUTTON_NOTIFY_STATUS 0x80 44 45#define ACPI_BUTTON_SUBCLASS_POWER "power" 46#define ACPI_BUTTON_HID_POWER "PNP0C0C" 47#define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button" 48#define ACPI_BUTTON_TYPE_POWER 0x01 49 50#define ACPI_BUTTON_SUBCLASS_SLEEP "sleep" 51#define ACPI_BUTTON_HID_SLEEP "PNP0C0E" 52#define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button" 53#define ACPI_BUTTON_TYPE_SLEEP 0x03 54 55#define ACPI_BUTTON_SUBCLASS_LID "lid" 56#define ACPI_BUTTON_HID_LID "PNP0C0D" 57#define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch" 58#define ACPI_BUTTON_TYPE_LID 0x05 59 60#define _COMPONENT ACPI_BUTTON_COMPONENT 61ACPI_MODULE_NAME("button"); 62 63MODULE_AUTHOR("Paul Diefenbaugh"); 64MODULE_DESCRIPTION("ACPI Button Driver"); 65MODULE_LICENSE("GPL"); 66 67static const struct acpi_device_id button_device_ids[] = { 68 {ACPI_BUTTON_HID_LID, 0}, 69 {ACPI_BUTTON_HID_SLEEP, 0}, 70 {ACPI_BUTTON_HID_SLEEPF, 0}, 71 {ACPI_BUTTON_HID_POWER, 0}, 72 {ACPI_BUTTON_HID_POWERF, 0}, 73 {"", 0}, 74}; 75MODULE_DEVICE_TABLE(acpi, button_device_ids); 76 77static int acpi_button_add(struct acpi_device *device); 78static int acpi_button_remove(struct acpi_device *device, int type); 79static int acpi_button_resume(struct acpi_device *device); 80static void acpi_button_notify(struct acpi_device *device, u32 event); 81static int acpi_button_info_open_fs(struct inode *inode, struct file *file); 82static int acpi_button_state_open_fs(struct inode *inode, struct file *file); 83 84static struct acpi_driver acpi_button_driver = { 85 .name = "button", 86 .class = ACPI_BUTTON_CLASS, 87 .ids = button_device_ids, 88 .ops = { 89 .add = acpi_button_add, 90 .resume = acpi_button_resume, 91 .remove = acpi_button_remove, 92 .notify = acpi_button_notify, 93 }, 94}; 95 96struct acpi_button { 97 unsigned int type; 98 struct input_dev *input; 99 char phys[32]; /* for input device */ 100 unsigned long pushed; 101}; 102 103static const struct file_operations acpi_button_info_fops = { 104 .owner = THIS_MODULE, 105 .open = acpi_button_info_open_fs, 106 .read = seq_read, 107 .llseek = seq_lseek, 108 .release = single_release, 109}; 110 111static const struct file_operations acpi_button_state_fops = { 112 .owner = THIS_MODULE, 113 .open = acpi_button_state_open_fs, 114 .read = seq_read, 115 .llseek = seq_lseek, 116 .release = single_release, 117}; 118 119static BLOCKING_NOTIFIER_HEAD(acpi_lid_notifier); 120static struct acpi_device *lid_device; 121 122/* -------------------------------------------------------------------------- 123 FS Interface (/proc) 124 -------------------------------------------------------------------------- */ 125 126static struct proc_dir_entry *acpi_button_dir; 127 128static int acpi_button_info_seq_show(struct seq_file *seq, void *offset) 129{ 130 struct acpi_device *device = seq->private; 131 132 seq_printf(seq, "type: %s\n", 133 acpi_device_name(device)); 134 return 0; 135} 136 137static int acpi_button_info_open_fs(struct inode *inode, struct file *file) 138{ 139 return single_open(file, acpi_button_info_seq_show, PDE(inode)->data); 140} 141 142static int acpi_button_state_seq_show(struct seq_file *seq, void *offset) 143{ 144 struct acpi_device *device = seq->private; 145 acpi_status status; 146 unsigned long long state; 147 148 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state); 149 seq_printf(seq, "state: %s\n", 150 ACPI_FAILURE(status) ? "unsupported" : 151 (state ? "open" : "closed")); 152 return 0; 153} 154 155static int acpi_button_state_open_fs(struct inode *inode, struct file *file) 156{ 157 return single_open(file, acpi_button_state_seq_show, PDE(inode)->data); 158} 159 160static struct proc_dir_entry *acpi_power_dir; 161static struct proc_dir_entry *acpi_sleep_dir; 162static struct proc_dir_entry *acpi_lid_dir; 163 164static int acpi_button_add_fs(struct acpi_device *device) 165{ 166 struct acpi_button *button = acpi_driver_data(device); 167 struct proc_dir_entry *entry = NULL; 168 169 switch (button->type) { 170 case ACPI_BUTTON_TYPE_POWER: 171 if (!acpi_power_dir) 172 acpi_power_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_POWER, 173 acpi_button_dir); 174 entry = acpi_power_dir; 175 break; 176 case ACPI_BUTTON_TYPE_SLEEP: 177 if (!acpi_sleep_dir) 178 acpi_sleep_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_SLEEP, 179 acpi_button_dir); 180 entry = acpi_sleep_dir; 181 break; 182 case ACPI_BUTTON_TYPE_LID: 183 if (!acpi_lid_dir) 184 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, 185 acpi_button_dir); 186 entry = acpi_lid_dir; 187 break; 188 } 189 190 if (!entry) 191 return -ENODEV; 192 193 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), entry); 194 if (!acpi_device_dir(device)) 195 return -ENODEV; 196 197 /* 'info' [R] */ 198 entry = proc_create_data(ACPI_BUTTON_FILE_INFO, 199 S_IRUGO, acpi_device_dir(device), 200 &acpi_button_info_fops, device); 201 if (!entry) 202 return -ENODEV; 203 204 /* show lid state [R] */ 205 if (button->type == ACPI_BUTTON_TYPE_LID) { 206 entry = proc_create_data(ACPI_BUTTON_FILE_STATE, 207 S_IRUGO, acpi_device_dir(device), 208 &acpi_button_state_fops, device); 209 if (!entry) 210 return -ENODEV; 211 } 212 213 return 0; 214} 215 216static int acpi_button_remove_fs(struct acpi_device *device) 217{ 218 struct acpi_button *button = acpi_driver_data(device); 219 220 if (acpi_device_dir(device)) { 221 if (button->type == ACPI_BUTTON_TYPE_LID) 222 remove_proc_entry(ACPI_BUTTON_FILE_STATE, 223 acpi_device_dir(device)); 224 remove_proc_entry(ACPI_BUTTON_FILE_INFO, 225 acpi_device_dir(device)); 226 227 remove_proc_entry(acpi_device_bid(device), 228 acpi_device_dir(device)->parent); 229 acpi_device_dir(device) = NULL; 230 } 231 232 return 0; 233} 234 235/* -------------------------------------------------------------------------- 236 Driver Interface 237 -------------------------------------------------------------------------- */ 238int acpi_lid_notifier_register(struct notifier_block *nb) 239{ 240 return blocking_notifier_chain_register(&acpi_lid_notifier, nb); 241} 242EXPORT_SYMBOL(acpi_lid_notifier_register); 243 244int acpi_lid_notifier_unregister(struct notifier_block *nb) 245{ 246 return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb); 247} 248EXPORT_SYMBOL(acpi_lid_notifier_unregister); 249 250int acpi_lid_open(void) 251{ 252 acpi_status status; 253 unsigned long long state; 254 255 if (!lid_device) 256 return -ENODEV; 257 258 status = acpi_evaluate_integer(lid_device->handle, "_LID", NULL, 259 &state); 260 if (ACPI_FAILURE(status)) 261 return -ENODEV; 262 263 return !!state; 264} 265EXPORT_SYMBOL(acpi_lid_open); 266 267static int acpi_lid_send_state(struct acpi_device *device) 268{ 269 struct acpi_button *button = acpi_driver_data(device); 270 unsigned long long state; 271 acpi_status status; 272 int ret; 273 274 status = acpi_evaluate_integer(device->handle, "_LID", NULL, &state); 275 if (ACPI_FAILURE(status)) 276 return -ENODEV; 277 278 /* input layer checks if event is redundant */ 279 input_report_switch(button->input, SW_LID, !state); 280 input_sync(button->input); 281 282 if (state) 283 pm_wakeup_event(&device->dev, 0); 284 285 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, device); 286 if (ret == NOTIFY_DONE) 287 ret = blocking_notifier_call_chain(&acpi_lid_notifier, state, 288 device); 289 if (ret == NOTIFY_DONE || ret == NOTIFY_OK) { 290 /* 291 * It is also regarded as success if the notifier_chain 292 * returns NOTIFY_OK or NOTIFY_DONE. 293 */ 294 ret = 0; 295 } 296 return ret; 297} 298 299static void acpi_button_notify(struct acpi_device *device, u32 event) 300{ 301 struct acpi_button *button = acpi_driver_data(device); 302 struct input_dev *input; 303 304 switch (event) { 305 case ACPI_FIXED_HARDWARE_EVENT: 306 event = ACPI_BUTTON_NOTIFY_STATUS; 307 /* fall through */ 308 case ACPI_BUTTON_NOTIFY_STATUS: 309 input = button->input; 310 if (button->type == ACPI_BUTTON_TYPE_LID) { 311 acpi_lid_send_state(device); 312 } else { 313 int keycode = test_bit(KEY_SLEEP, input->keybit) ? 314 KEY_SLEEP : KEY_POWER; 315 316 input_report_key(input, keycode, 1); 317 input_sync(input); 318 input_report_key(input, keycode, 0); 319 input_sync(input); 320 321 pm_wakeup_event(&device->dev, 0); 322 } 323 324 acpi_bus_generate_proc_event(device, event, ++button->pushed); 325 break; 326 default: 327 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 328 "Unsupported event [0x%x]\n", event)); 329 break; 330 } 331} 332 333static int acpi_button_resume(struct acpi_device *device) 334{ 335 struct acpi_button *button = acpi_driver_data(device); 336 337 if (button->type == ACPI_BUTTON_TYPE_LID) 338 return acpi_lid_send_state(device); 339 return 0; 340} 341 342static int acpi_button_add(struct acpi_device *device) 343{ 344 struct acpi_button *button; 345 struct input_dev *input; 346 const char *hid = acpi_device_hid(device); 347 char *name, *class; 348 int error; 349 350 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL); 351 if (!button) 352 return -ENOMEM; 353 354 device->driver_data = button; 355 356 button->input = input = input_allocate_device(); 357 if (!input) { 358 error = -ENOMEM; 359 goto err_free_button; 360 } 361 362 name = acpi_device_name(device); 363 class = acpi_device_class(device); 364 365 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) || 366 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) { 367 button->type = ACPI_BUTTON_TYPE_POWER; 368 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER); 369 sprintf(class, "%s/%s", 370 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER); 371 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) || 372 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) { 373 button->type = ACPI_BUTTON_TYPE_SLEEP; 374 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP); 375 sprintf(class, "%s/%s", 376 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP); 377 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) { 378 button->type = ACPI_BUTTON_TYPE_LID; 379 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID); 380 sprintf(class, "%s/%s", 381 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID); 382 } else { 383 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid); 384 error = -ENODEV; 385 goto err_free_input; 386 } 387 388 error = acpi_button_add_fs(device); 389 if (error) 390 goto err_free_input; 391 392 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid); 393 394 input->name = name; 395 input->phys = button->phys; 396 input->id.bustype = BUS_HOST; 397 input->id.product = button->type; 398 input->dev.parent = &device->dev; 399 400 switch (button->type) { 401 case ACPI_BUTTON_TYPE_POWER: 402 input->evbit[0] = BIT_MASK(EV_KEY); 403 set_bit(KEY_POWER, input->keybit); 404 break; 405 406 case ACPI_BUTTON_TYPE_SLEEP: 407 input->evbit[0] = BIT_MASK(EV_KEY); 408 set_bit(KEY_SLEEP, input->keybit); 409 break; 410 411 case ACPI_BUTTON_TYPE_LID: 412 input->evbit[0] = BIT_MASK(EV_SW); 413 set_bit(SW_LID, input->swbit); 414 break; 415 } 416 417 error = input_register_device(input); 418 if (error) 419 goto err_remove_fs; 420 if (button->type == ACPI_BUTTON_TYPE_LID) { 421 acpi_lid_send_state(device); 422 /* 423 * This assumes there's only one lid device, or if there are 424 * more we only care about the last one... 425 */ 426 lid_device = device; 427 } 428 429 if (device->wakeup.flags.valid) { 430 /* Button's GPE is run-wake GPE */ 431 acpi_enable_gpe(device->wakeup.gpe_device, 432 device->wakeup.gpe_number); 433 device->wakeup.run_wake_count++; 434 device_set_wakeup_enable(&device->dev, true); 435 } 436 437 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device)); 438 return 0; 439 440 err_remove_fs: 441 acpi_button_remove_fs(device); 442 err_free_input: 443 input_free_device(input); 444 err_free_button: 445 kfree(button); 446 return error; 447} 448 449static int acpi_button_remove(struct acpi_device *device, int type) 450{ 451 struct acpi_button *button = acpi_driver_data(device); 452 453 if (device->wakeup.flags.valid) { 454 acpi_disable_gpe(device->wakeup.gpe_device, 455 device->wakeup.gpe_number); 456 device->wakeup.run_wake_count--; 457 device_set_wakeup_enable(&device->dev, false); 458 } 459 460 acpi_button_remove_fs(device); 461 input_unregister_device(button->input); 462 kfree(button); 463 return 0; 464} 465 466static int __init acpi_button_init(void) 467{ 468 int result; 469 470 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir); 471 if (!acpi_button_dir) 472 return -ENODEV; 473 474 result = acpi_bus_register_driver(&acpi_button_driver); 475 if (result < 0) { 476 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir); 477 return -ENODEV; 478 } 479 480 return 0; 481} 482 483static void __exit acpi_button_exit(void) 484{ 485 acpi_bus_unregister_driver(&acpi_button_driver); 486 487 if (acpi_power_dir) 488 remove_proc_entry(ACPI_BUTTON_SUBCLASS_POWER, acpi_button_dir); 489 if (acpi_sleep_dir) 490 remove_proc_entry(ACPI_BUTTON_SUBCLASS_SLEEP, acpi_button_dir); 491 if (acpi_lid_dir) 492 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir); 493 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir); 494} 495 496module_init(acpi_button_init); 497module_exit(acpi_button_exit);