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