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 remove_button_dir: 236 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir); 237 goto done; 238 } 239 240 static int acpi_button_remove_fs(struct acpi_device *device) 241 { 242 struct acpi_button *button = acpi_driver_data(device); 243 244 if (button->type != ACPI_BUTTON_TYPE_LID) 245 return 0; 246 247 remove_proc_entry(ACPI_BUTTON_FILE_STATE, 248 acpi_device_dir(device)); 249 remove_proc_entry(acpi_device_bid(device), 250 acpi_lid_dir); 251 acpi_device_dir(device) = NULL; 252 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir); 253 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir); 254 255 return 0; 256 } 257 258 /* -------------------------------------------------------------------------- 259 Driver Interface 260 -------------------------------------------------------------------------- */ 261 int acpi_lid_notifier_register(struct notifier_block *nb) 262 { 263 return blocking_notifier_chain_register(&acpi_lid_notifier, nb); 264 } 265 EXPORT_SYMBOL(acpi_lid_notifier_register); 266 267 int acpi_lid_notifier_unregister(struct notifier_block *nb) 268 { 269 return blocking_notifier_chain_unregister(&acpi_lid_notifier, nb); 270 } 271 EXPORT_SYMBOL(acpi_lid_notifier_unregister); 272 273 int acpi_lid_open(void) 274 { 275 if (!lid_device) 276 return -ENODEV; 277 278 return acpi_lid_evaluate_state(lid_device); 279 } 280 EXPORT_SYMBOL(acpi_lid_open); 281 282 static int acpi_lid_update_state(struct acpi_device *device) 283 { 284 int state; 285 286 state = acpi_lid_evaluate_state(device); 287 if (state < 0) 288 return state; 289 290 return acpi_lid_notify_state(device, state); 291 } 292 293 static void acpi_lid_initialize_state(struct acpi_device *device) 294 { 295 switch (lid_init_state) { 296 case ACPI_BUTTON_LID_INIT_OPEN: 297 (void)acpi_lid_notify_state(device, 1); 298 break; 299 case ACPI_BUTTON_LID_INIT_METHOD: 300 (void)acpi_lid_update_state(device); 301 break; 302 case ACPI_BUTTON_LID_INIT_IGNORE: 303 default: 304 break; 305 } 306 } 307 308 static void acpi_button_notify(struct acpi_device *device, u32 event) 309 { 310 struct acpi_button *button = acpi_driver_data(device); 311 struct input_dev *input; 312 313 switch (event) { 314 case ACPI_FIXED_HARDWARE_EVENT: 315 event = ACPI_BUTTON_NOTIFY_STATUS; 316 /* fall through */ 317 case ACPI_BUTTON_NOTIFY_STATUS: 318 input = button->input; 319 if (button->type == ACPI_BUTTON_TYPE_LID) { 320 acpi_lid_update_state(device); 321 } else { 322 int keycode; 323 324 pm_wakeup_event(&device->dev, 0); 325 if (button->suspended) 326 break; 327 328 keycode = test_bit(KEY_SLEEP, input->keybit) ? 329 KEY_SLEEP : KEY_POWER; 330 input_report_key(input, keycode, 1); 331 input_sync(input); 332 input_report_key(input, keycode, 0); 333 input_sync(input); 334 335 acpi_bus_generate_netlink_event( 336 device->pnp.device_class, 337 dev_name(&device->dev), 338 event, ++button->pushed); 339 } 340 break; 341 default: 342 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 343 "Unsupported event [0x%x]\n", event)); 344 break; 345 } 346 } 347 348 #ifdef CONFIG_PM_SLEEP 349 static int acpi_button_suspend(struct device *dev) 350 { 351 struct acpi_device *device = to_acpi_device(dev); 352 struct acpi_button *button = acpi_driver_data(device); 353 354 button->suspended = true; 355 return 0; 356 } 357 358 static int acpi_button_resume(struct device *dev) 359 { 360 struct acpi_device *device = to_acpi_device(dev); 361 struct acpi_button *button = acpi_driver_data(device); 362 363 button->suspended = false; 364 if (button->type == ACPI_BUTTON_TYPE_LID) 365 acpi_lid_initialize_state(device); 366 return 0; 367 } 368 #endif 369 370 static int acpi_button_add(struct acpi_device *device) 371 { 372 struct acpi_button *button; 373 struct input_dev *input; 374 const char *hid = acpi_device_hid(device); 375 char *name, *class; 376 int error; 377 378 button = kzalloc(sizeof(struct acpi_button), GFP_KERNEL); 379 if (!button) 380 return -ENOMEM; 381 382 device->driver_data = button; 383 384 button->input = input = input_allocate_device(); 385 if (!input) { 386 error = -ENOMEM; 387 goto err_free_button; 388 } 389 390 name = acpi_device_name(device); 391 class = acpi_device_class(device); 392 393 if (!strcmp(hid, ACPI_BUTTON_HID_POWER) || 394 !strcmp(hid, ACPI_BUTTON_HID_POWERF)) { 395 button->type = ACPI_BUTTON_TYPE_POWER; 396 strcpy(name, ACPI_BUTTON_DEVICE_NAME_POWER); 397 sprintf(class, "%s/%s", 398 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_POWER); 399 } else if (!strcmp(hid, ACPI_BUTTON_HID_SLEEP) || 400 !strcmp(hid, ACPI_BUTTON_HID_SLEEPF)) { 401 button->type = ACPI_BUTTON_TYPE_SLEEP; 402 strcpy(name, ACPI_BUTTON_DEVICE_NAME_SLEEP); 403 sprintf(class, "%s/%s", 404 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_SLEEP); 405 } else if (!strcmp(hid, ACPI_BUTTON_HID_LID)) { 406 button->type = ACPI_BUTTON_TYPE_LID; 407 strcpy(name, ACPI_BUTTON_DEVICE_NAME_LID); 408 sprintf(class, "%s/%s", 409 ACPI_BUTTON_CLASS, ACPI_BUTTON_SUBCLASS_LID); 410 } else { 411 printk(KERN_ERR PREFIX "Unsupported hid [%s]\n", hid); 412 error = -ENODEV; 413 goto err_free_input; 414 } 415 416 error = acpi_button_add_fs(device); 417 if (error) 418 goto err_free_input; 419 420 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", hid); 421 422 input->name = name; 423 input->phys = button->phys; 424 input->id.bustype = BUS_HOST; 425 input->id.product = button->type; 426 input->dev.parent = &device->dev; 427 428 switch (button->type) { 429 case ACPI_BUTTON_TYPE_POWER: 430 input_set_capability(input, EV_KEY, KEY_POWER); 431 break; 432 433 case ACPI_BUTTON_TYPE_SLEEP: 434 input_set_capability(input, EV_KEY, KEY_SLEEP); 435 break; 436 437 case ACPI_BUTTON_TYPE_LID: 438 input_set_capability(input, EV_SW, SW_LID); 439 break; 440 } 441 442 error = input_register_device(input); 443 if (error) 444 goto err_remove_fs; 445 if (button->type == ACPI_BUTTON_TYPE_LID) { 446 acpi_lid_initialize_state(device); 447 /* 448 * This assumes there's only one lid device, or if there are 449 * more we only care about the last one... 450 */ 451 lid_device = device; 452 } 453 454 printk(KERN_INFO PREFIX "%s [%s]\n", name, acpi_device_bid(device)); 455 return 0; 456 457 err_remove_fs: 458 acpi_button_remove_fs(device); 459 err_free_input: 460 input_free_device(input); 461 err_free_button: 462 kfree(button); 463 return error; 464 } 465 466 static int acpi_button_remove(struct acpi_device *device) 467 { 468 struct acpi_button *button = acpi_driver_data(device); 469 470 acpi_button_remove_fs(device); 471 input_unregister_device(button->input); 472 kfree(button); 473 return 0; 474 } 475 476 static int param_set_lid_init_state(const char *val, struct kernel_param *kp) 477 { 478 int result = 0; 479 480 if (!strncmp(val, "open", sizeof("open") - 1)) { 481 lid_init_state = ACPI_BUTTON_LID_INIT_OPEN; 482 pr_info("Notify initial lid state as open\n"); 483 } else if (!strncmp(val, "method", sizeof("method") - 1)) { 484 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD; 485 pr_info("Notify initial lid state with _LID return value\n"); 486 } else if (!strncmp(val, "ignore", sizeof("ignore") - 1)) { 487 lid_init_state = ACPI_BUTTON_LID_INIT_IGNORE; 488 pr_info("Do not notify initial lid state\n"); 489 } else 490 result = -EINVAL; 491 return result; 492 } 493 494 static int param_get_lid_init_state(char *buffer, struct kernel_param *kp) 495 { 496 switch (lid_init_state) { 497 case ACPI_BUTTON_LID_INIT_OPEN: 498 return sprintf(buffer, "open"); 499 case ACPI_BUTTON_LID_INIT_METHOD: 500 return sprintf(buffer, "method"); 501 case ACPI_BUTTON_LID_INIT_IGNORE: 502 return sprintf(buffer, "ignore"); 503 default: 504 return sprintf(buffer, "invalid"); 505 } 506 return 0; 507 } 508 509 module_param_call(lid_init_state, 510 param_set_lid_init_state, param_get_lid_init_state, 511 NULL, 0644); 512 MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state"); 513 514 module_acpi_driver(acpi_button_driver); 515