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