1 /* 2 * scan.c - support for transforming the ACPI namespace into individual objects 3 */ 4 5 #include <linux/module.h> 6 #include <linux/init.h> 7 #include <linux/acpi.h> 8 9 #include <acpi/acpi_drivers.h> 10 #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */ 11 12 13 #define _COMPONENT ACPI_BUS_COMPONENT 14 ACPI_MODULE_NAME ("scan") 15 16 #define STRUCT_TO_INT(s) (*((int*)&s)) 17 18 extern struct acpi_device *acpi_root; 19 20 21 #define ACPI_BUS_CLASS "system_bus" 22 #define ACPI_BUS_HID "ACPI_BUS" 23 #define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver" 24 #define ACPI_BUS_DEVICE_NAME "System Bus" 25 26 static LIST_HEAD(acpi_device_list); 27 DEFINE_SPINLOCK(acpi_device_lock); 28 LIST_HEAD(acpi_wakeup_device_list); 29 30 static int 31 acpi_bus_trim(struct acpi_device *start, 32 int rmdevice); 33 34 static void acpi_device_release(struct kobject * kobj) 35 { 36 struct acpi_device * dev = container_of(kobj,struct acpi_device,kobj); 37 if (dev->pnp.cid_list) 38 kfree(dev->pnp.cid_list); 39 kfree(dev); 40 } 41 42 struct acpi_device_attribute { 43 struct attribute attr; 44 ssize_t (*show)(struct acpi_device *, char *); 45 ssize_t (*store)(struct acpi_device *, const char *, size_t); 46 }; 47 48 typedef void acpi_device_sysfs_files(struct kobject *, 49 const struct attribute *); 50 51 static void setup_sys_fs_device_files(struct acpi_device *dev, 52 acpi_device_sysfs_files *func); 53 54 #define create_sysfs_device_files(dev) \ 55 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file) 56 #define remove_sysfs_device_files(dev) \ 57 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file) 58 59 60 #define to_acpi_device(n) container_of(n, struct acpi_device, kobj) 61 #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr); 62 63 static ssize_t acpi_device_attr_show(struct kobject *kobj, 64 struct attribute *attr, char *buf) 65 { 66 struct acpi_device *device = to_acpi_device(kobj); 67 struct acpi_device_attribute *attribute = to_handle_attr(attr); 68 return attribute->show ? attribute->show(device, buf) : -EIO; 69 } 70 static ssize_t acpi_device_attr_store(struct kobject *kobj, 71 struct attribute *attr, const char *buf, size_t len) 72 { 73 struct acpi_device *device = to_acpi_device(kobj); 74 struct acpi_device_attribute *attribute = to_handle_attr(attr); 75 return attribute->store ? attribute->store(device, buf, len) : -EIO; 76 } 77 78 static struct sysfs_ops acpi_device_sysfs_ops = { 79 .show = acpi_device_attr_show, 80 .store = acpi_device_attr_store, 81 }; 82 83 static struct kobj_type ktype_acpi_ns = { 84 .sysfs_ops = &acpi_device_sysfs_ops, 85 .release = acpi_device_release, 86 }; 87 88 static int namespace_hotplug(struct kset *kset, struct kobject *kobj, 89 char **envp, int num_envp, char *buffer, 90 int buffer_size) 91 { 92 struct acpi_device *dev = to_acpi_device(kobj); 93 int i = 0; 94 int len = 0; 95 96 if (!dev->driver) 97 return 0; 98 99 if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len, 100 "PHYSDEVDRIVER=%s", dev->driver->name)) 101 return -ENOMEM; 102 103 envp[i] = NULL; 104 105 return 0; 106 } 107 108 static struct kset_hotplug_ops namespace_hotplug_ops = { 109 .hotplug = &namespace_hotplug, 110 }; 111 112 static struct kset acpi_namespace_kset = { 113 .kobj = { 114 .name = "namespace", 115 }, 116 .subsys = &acpi_subsys, 117 .ktype = &ktype_acpi_ns, 118 .hotplug_ops = &namespace_hotplug_ops, 119 }; 120 121 122 static void acpi_device_register(struct acpi_device * device, struct acpi_device * parent) 123 { 124 /* 125 * Linkage 126 * ------- 127 * Link this device to its parent and siblings. 128 */ 129 INIT_LIST_HEAD(&device->children); 130 INIT_LIST_HEAD(&device->node); 131 INIT_LIST_HEAD(&device->g_list); 132 INIT_LIST_HEAD(&device->wakeup_list); 133 134 spin_lock(&acpi_device_lock); 135 if (device->parent) { 136 list_add_tail(&device->node, &device->parent->children); 137 list_add_tail(&device->g_list,&device->parent->g_list); 138 } else 139 list_add_tail(&device->g_list,&acpi_device_list); 140 if (device->wakeup.flags.valid) 141 list_add_tail(&device->wakeup_list,&acpi_wakeup_device_list); 142 spin_unlock(&acpi_device_lock); 143 144 strlcpy(device->kobj.name,device->pnp.bus_id,KOBJ_NAME_LEN); 145 if (parent) 146 device->kobj.parent = &parent->kobj; 147 device->kobj.ktype = &ktype_acpi_ns; 148 device->kobj.kset = &acpi_namespace_kset; 149 kobject_register(&device->kobj); 150 create_sysfs_device_files(device); 151 } 152 153 static int 154 acpi_device_unregister ( 155 struct acpi_device *device, 156 int type) 157 { 158 spin_lock(&acpi_device_lock); 159 if (device->parent) { 160 list_del(&device->node); 161 list_del(&device->g_list); 162 } else 163 list_del(&device->g_list); 164 165 list_del(&device->wakeup_list); 166 167 spin_unlock(&acpi_device_lock); 168 169 acpi_detach_data(device->handle, acpi_bus_data_handler); 170 remove_sysfs_device_files(device); 171 kobject_unregister(&device->kobj); 172 return 0; 173 } 174 175 void 176 acpi_bus_data_handler ( 177 acpi_handle handle, 178 u32 function, 179 void *context) 180 { 181 ACPI_FUNCTION_TRACE("acpi_bus_data_handler"); 182 183 /* TBD */ 184 185 return_VOID; 186 } 187 188 static int 189 acpi_bus_get_power_flags ( 190 struct acpi_device *device) 191 { 192 acpi_status status = 0; 193 acpi_handle handle = NULL; 194 u32 i = 0; 195 196 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags"); 197 198 /* 199 * Power Management Flags 200 */ 201 status = acpi_get_handle(device->handle, "_PSC", &handle); 202 if (ACPI_SUCCESS(status)) 203 device->power.flags.explicit_get = 1; 204 status = acpi_get_handle(device->handle, "_IRC", &handle); 205 if (ACPI_SUCCESS(status)) 206 device->power.flags.inrush_current = 1; 207 208 /* 209 * Enumerate supported power management states 210 */ 211 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) { 212 struct acpi_device_power_state *ps = &device->power.states[i]; 213 char object_name[5] = {'_','P','R','0'+i,'\0'}; 214 215 /* Evaluate "_PRx" to se if power resources are referenced */ 216 acpi_evaluate_reference(device->handle, object_name, NULL, 217 &ps->resources); 218 if (ps->resources.count) { 219 device->power.flags.power_resources = 1; 220 ps->flags.valid = 1; 221 } 222 223 /* Evaluate "_PSx" to see if we can do explicit sets */ 224 object_name[2] = 'S'; 225 status = acpi_get_handle(device->handle, object_name, &handle); 226 if (ACPI_SUCCESS(status)) { 227 ps->flags.explicit_set = 1; 228 ps->flags.valid = 1; 229 } 230 231 /* State is valid if we have some power control */ 232 if (ps->resources.count || ps->flags.explicit_set) 233 ps->flags.valid = 1; 234 235 ps->power = -1; /* Unknown - driver assigned */ 236 ps->latency = -1; /* Unknown - driver assigned */ 237 } 238 239 /* Set defaults for D0 and D3 states (always valid) */ 240 device->power.states[ACPI_STATE_D0].flags.valid = 1; 241 device->power.states[ACPI_STATE_D0].power = 100; 242 device->power.states[ACPI_STATE_D3].flags.valid = 1; 243 device->power.states[ACPI_STATE_D3].power = 0; 244 245 /* TBD: System wake support and resource requirements. */ 246 247 device->power.state = ACPI_STATE_UNKNOWN; 248 249 return_VALUE(0); 250 } 251 252 int 253 acpi_match_ids ( 254 struct acpi_device *device, 255 char *ids) 256 { 257 int error = 0; 258 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 259 260 if (device->flags.hardware_id) 261 if (strstr(ids, device->pnp.hardware_id)) 262 goto Done; 263 264 if (device->flags.compatible_ids) { 265 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list; 266 int i; 267 268 /* compare multiple _CID entries against driver ids */ 269 for (i = 0; i < cid_list->count; i++) 270 { 271 if (strstr(ids, cid_list->id[i].value)) 272 goto Done; 273 } 274 } 275 error = -ENOENT; 276 277 Done: 278 if (buffer.pointer) 279 acpi_os_free(buffer.pointer); 280 return error; 281 } 282 283 static acpi_status 284 acpi_bus_extract_wakeup_device_power_package ( 285 struct acpi_device *device, 286 union acpi_object *package) 287 { 288 int i = 0; 289 union acpi_object *element = NULL; 290 291 if (!device || !package || (package->package.count < 2)) 292 return AE_BAD_PARAMETER; 293 294 element = &(package->package.elements[0]); 295 if (!element) 296 return AE_BAD_PARAMETER; 297 if (element->type == ACPI_TYPE_PACKAGE) { 298 if ((element->package.count < 2) || 299 (element->package.elements[0].type != ACPI_TYPE_LOCAL_REFERENCE) || 300 (element->package.elements[1].type != ACPI_TYPE_INTEGER)) 301 return AE_BAD_DATA; 302 device->wakeup.gpe_device = element->package.elements[0].reference.handle; 303 device->wakeup.gpe_number = (u32)element->package.elements[1].integer.value; 304 }else if (element->type == ACPI_TYPE_INTEGER) { 305 device->wakeup.gpe_number = element->integer.value; 306 }else 307 return AE_BAD_DATA; 308 309 element = &(package->package.elements[1]); 310 if (element->type != ACPI_TYPE_INTEGER) { 311 return AE_BAD_DATA; 312 } 313 device->wakeup.sleep_state = element->integer.value; 314 315 if ((package->package.count - 2) > ACPI_MAX_HANDLES) { 316 return AE_NO_MEMORY; 317 } 318 device->wakeup.resources.count = package->package.count - 2; 319 for (i=0; i < device->wakeup.resources.count; i++) { 320 element = &(package->package.elements[i + 2]); 321 if (element->type != ACPI_TYPE_ANY ) { 322 return AE_BAD_DATA; 323 } 324 325 device->wakeup.resources.handles[i] = element->reference.handle; 326 } 327 328 return AE_OK; 329 } 330 331 static int 332 acpi_bus_get_wakeup_device_flags ( 333 struct acpi_device *device) 334 { 335 acpi_status status = 0; 336 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 337 union acpi_object *package = NULL; 338 339 ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags"); 340 341 /* _PRW */ 342 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer); 343 if (ACPI_FAILURE(status)) { 344 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n")); 345 goto end; 346 } 347 348 package = (union acpi_object *) buffer.pointer; 349 status = acpi_bus_extract_wakeup_device_power_package(device, package); 350 if (ACPI_FAILURE(status)) { 351 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error extracting _PRW package\n")); 352 goto end; 353 } 354 355 acpi_os_free(buffer.pointer); 356 357 device->wakeup.flags.valid = 1; 358 /* Power button, Lid switch always enable wakeup*/ 359 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E")) 360 device->wakeup.flags.run_wake = 1; 361 362 end: 363 if (ACPI_FAILURE(status)) 364 device->flags.wake_capable = 0; 365 return_VALUE(0); 366 } 367 368 /* -------------------------------------------------------------------------- 369 ACPI hotplug sysfs device file support 370 -------------------------------------------------------------------------- */ 371 static ssize_t acpi_eject_store(struct acpi_device *device, 372 const char *buf, size_t count); 373 374 #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \ 375 static struct acpi_device_attribute acpi_device_attr_##_name = \ 376 __ATTR(_name, _mode, _show, _store) 377 378 ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store); 379 380 /** 381 * setup_sys_fs_device_files - sets up the device files under device namespace 382 * @dev: acpi_device object 383 * @func: function pointer to create or destroy the device file 384 */ 385 static void 386 setup_sys_fs_device_files ( 387 struct acpi_device *dev, 388 acpi_device_sysfs_files *func) 389 { 390 acpi_status status; 391 acpi_handle temp = NULL; 392 393 /* 394 * If device has _EJ0, 'eject' file is created that is used to trigger 395 * hot-removal function from userland. 396 */ 397 status = acpi_get_handle(dev->handle, "_EJ0", &temp); 398 if (ACPI_SUCCESS(status)) 399 (*(func))(&dev->kobj,&acpi_device_attr_eject.attr); 400 } 401 402 static int 403 acpi_eject_operation(acpi_handle handle, int lockable) 404 { 405 struct acpi_object_list arg_list; 406 union acpi_object arg; 407 acpi_status status = AE_OK; 408 409 /* 410 * TBD: evaluate _PS3? 411 */ 412 413 if (lockable) { 414 arg_list.count = 1; 415 arg_list.pointer = &arg; 416 arg.type = ACPI_TYPE_INTEGER; 417 arg.integer.value = 0; 418 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL); 419 } 420 421 arg_list.count = 1; 422 arg_list.pointer = &arg; 423 arg.type = ACPI_TYPE_INTEGER; 424 arg.integer.value = 1; 425 426 /* 427 * TBD: _EJD support. 428 */ 429 430 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL); 431 if (ACPI_FAILURE(status)) { 432 return(-ENODEV); 433 } 434 435 return(0); 436 } 437 438 439 static ssize_t 440 acpi_eject_store(struct acpi_device *device, const char *buf, size_t count) 441 { 442 int result; 443 int ret = count; 444 int islockable; 445 acpi_status status; 446 acpi_handle handle; 447 acpi_object_type type = 0; 448 449 if ((!count) || (buf[0] != '1')) { 450 return -EINVAL; 451 } 452 453 #ifndef FORCE_EJECT 454 if (device->driver == NULL) { 455 ret = -ENODEV; 456 goto err; 457 } 458 #endif 459 status = acpi_get_type(device->handle, &type); 460 if (ACPI_FAILURE(status) || (!device->flags.ejectable) ) { 461 ret = -ENODEV; 462 goto err; 463 } 464 465 islockable = device->flags.lockable; 466 handle = device->handle; 467 468 if (type == ACPI_TYPE_PROCESSOR) 469 result = acpi_bus_trim(device, 0); 470 else 471 result = acpi_bus_trim(device, 1); 472 473 if (!result) 474 result = acpi_eject_operation(handle, islockable); 475 476 if (result) { 477 ret = -EBUSY; 478 } 479 err: 480 return ret; 481 } 482 483 484 /* -------------------------------------------------------------------------- 485 Performance Management 486 -------------------------------------------------------------------------- */ 487 488 static int 489 acpi_bus_get_perf_flags ( 490 struct acpi_device *device) 491 { 492 device->performance.state = ACPI_STATE_UNKNOWN; 493 return 0; 494 } 495 496 /* -------------------------------------------------------------------------- 497 Driver Management 498 -------------------------------------------------------------------------- */ 499 500 static LIST_HEAD(acpi_bus_drivers); 501 static DECLARE_MUTEX(acpi_bus_drivers_lock); 502 503 504 /** 505 * acpi_bus_match 506 * -------------- 507 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it 508 * matches the specified driver's criteria. 509 */ 510 static int 511 acpi_bus_match ( 512 struct acpi_device *device, 513 struct acpi_driver *driver) 514 { 515 if (driver && driver->ops.match) 516 return driver->ops.match(device, driver); 517 return acpi_match_ids(device, driver->ids); 518 } 519 520 521 /** 522 * acpi_bus_driver_init 523 * -------------------- 524 * Used to initialize a device via its device driver. Called whenever a 525 * driver is bound to a device. Invokes the driver's add() and start() ops. 526 */ 527 static int 528 acpi_bus_driver_init ( 529 struct acpi_device *device, 530 struct acpi_driver *driver) 531 { 532 int result = 0; 533 534 ACPI_FUNCTION_TRACE("acpi_bus_driver_init"); 535 536 if (!device || !driver) 537 return_VALUE(-EINVAL); 538 539 if (!driver->ops.add) 540 return_VALUE(-ENOSYS); 541 542 result = driver->ops.add(device); 543 if (result) { 544 device->driver = NULL; 545 acpi_driver_data(device) = NULL; 546 return_VALUE(result); 547 } 548 549 device->driver = driver; 550 551 /* 552 * TBD - Configuration Management: Assign resources to device based 553 * upon possible configuration and currently allocated resources. 554 */ 555 556 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Driver successfully bound to device\n")); 557 return_VALUE(0); 558 } 559 560 int 561 acpi_start_single_object ( 562 struct acpi_device *device) 563 { 564 int result = 0; 565 struct acpi_driver *driver; 566 567 ACPI_FUNCTION_TRACE("acpi_start_single_object"); 568 569 if (!(driver = device->driver)) 570 return_VALUE(0); 571 572 if (driver->ops.start) { 573 result = driver->ops.start(device); 574 if (result && driver->ops.remove) 575 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL); 576 } 577 578 return_VALUE(result); 579 } 580 581 static int acpi_driver_attach(struct acpi_driver * drv) 582 { 583 struct list_head * node, * next; 584 int count = 0; 585 586 ACPI_FUNCTION_TRACE("acpi_driver_attach"); 587 588 spin_lock(&acpi_device_lock); 589 list_for_each_safe(node, next, &acpi_device_list) { 590 struct acpi_device * dev = container_of(node, struct acpi_device, g_list); 591 592 if (dev->driver || !dev->status.present) 593 continue; 594 spin_unlock(&acpi_device_lock); 595 596 if (!acpi_bus_match(dev, drv)) { 597 if (!acpi_bus_driver_init(dev, drv)) { 598 acpi_start_single_object(dev); 599 atomic_inc(&drv->references); 600 count++; 601 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n", 602 drv->name, dev->pnp.bus_id)); 603 } 604 } 605 spin_lock(&acpi_device_lock); 606 } 607 spin_unlock(&acpi_device_lock); 608 return_VALUE(count); 609 } 610 611 static int acpi_driver_detach(struct acpi_driver * drv) 612 { 613 struct list_head * node, * next; 614 615 ACPI_FUNCTION_TRACE("acpi_driver_detach"); 616 617 spin_lock(&acpi_device_lock); 618 list_for_each_safe(node,next,&acpi_device_list) { 619 struct acpi_device * dev = container_of(node,struct acpi_device,g_list); 620 621 if (dev->driver == drv) { 622 spin_unlock(&acpi_device_lock); 623 if (drv->ops.remove) 624 drv->ops.remove(dev,ACPI_BUS_REMOVAL_NORMAL); 625 spin_lock(&acpi_device_lock); 626 dev->driver = NULL; 627 dev->driver_data = NULL; 628 atomic_dec(&drv->references); 629 } 630 } 631 spin_unlock(&acpi_device_lock); 632 return_VALUE(0); 633 } 634 635 /** 636 * acpi_bus_register_driver 637 * ------------------------ 638 * Registers a driver with the ACPI bus. Searches the namespace for all 639 * devices that match the driver's criteria and binds. Returns the 640 * number of devices that were claimed by the driver, or a negative 641 * error status for failure. 642 */ 643 int 644 acpi_bus_register_driver ( 645 struct acpi_driver *driver) 646 { 647 int count; 648 649 ACPI_FUNCTION_TRACE("acpi_bus_register_driver"); 650 651 if (acpi_disabled) 652 return_VALUE(-ENODEV); 653 654 if (!driver) 655 return_VALUE(-EINVAL); 656 657 spin_lock(&acpi_device_lock); 658 list_add_tail(&driver->node, &acpi_bus_drivers); 659 spin_unlock(&acpi_device_lock); 660 count = acpi_driver_attach(driver); 661 662 return_VALUE(count); 663 } 664 EXPORT_SYMBOL(acpi_bus_register_driver); 665 666 667 /** 668 * acpi_bus_unregister_driver 669 * -------------------------- 670 * Unregisters a driver with the ACPI bus. Searches the namespace for all 671 * devices that match the driver's criteria and unbinds. 672 */ 673 int 674 acpi_bus_unregister_driver ( 675 struct acpi_driver *driver) 676 { 677 int error = 0; 678 679 ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver"); 680 681 if (driver) { 682 acpi_driver_detach(driver); 683 684 if (!atomic_read(&driver->references)) { 685 spin_lock(&acpi_device_lock); 686 list_del_init(&driver->node); 687 spin_unlock(&acpi_device_lock); 688 } 689 } else 690 error = -EINVAL; 691 return_VALUE(error); 692 } 693 EXPORT_SYMBOL(acpi_bus_unregister_driver); 694 695 /** 696 * acpi_bus_find_driver 697 * -------------------- 698 * Parses the list of registered drivers looking for a driver applicable for 699 * the specified device. 700 */ 701 static int 702 acpi_bus_find_driver ( 703 struct acpi_device *device) 704 { 705 int result = 0; 706 struct list_head * node, *next; 707 708 ACPI_FUNCTION_TRACE("acpi_bus_find_driver"); 709 710 spin_lock(&acpi_device_lock); 711 list_for_each_safe(node,next,&acpi_bus_drivers) { 712 struct acpi_driver * driver = container_of(node,struct acpi_driver,node); 713 714 atomic_inc(&driver->references); 715 spin_unlock(&acpi_device_lock); 716 if (!acpi_bus_match(device, driver)) { 717 result = acpi_bus_driver_init(device, driver); 718 if (!result) 719 goto Done; 720 } 721 atomic_dec(&driver->references); 722 spin_lock(&acpi_device_lock); 723 } 724 spin_unlock(&acpi_device_lock); 725 726 Done: 727 return_VALUE(result); 728 } 729 730 731 /* -------------------------------------------------------------------------- 732 Device Enumeration 733 -------------------------------------------------------------------------- */ 734 735 static int 736 acpi_bus_get_flags ( 737 struct acpi_device *device) 738 { 739 acpi_status status = AE_OK; 740 acpi_handle temp = NULL; 741 742 ACPI_FUNCTION_TRACE("acpi_bus_get_flags"); 743 744 /* Presence of _STA indicates 'dynamic_status' */ 745 status = acpi_get_handle(device->handle, "_STA", &temp); 746 if (ACPI_SUCCESS(status)) 747 device->flags.dynamic_status = 1; 748 749 /* Presence of _CID indicates 'compatible_ids' */ 750 status = acpi_get_handle(device->handle, "_CID", &temp); 751 if (ACPI_SUCCESS(status)) 752 device->flags.compatible_ids = 1; 753 754 /* Presence of _RMV indicates 'removable' */ 755 status = acpi_get_handle(device->handle, "_RMV", &temp); 756 if (ACPI_SUCCESS(status)) 757 device->flags.removable = 1; 758 759 /* Presence of _EJD|_EJ0 indicates 'ejectable' */ 760 status = acpi_get_handle(device->handle, "_EJD", &temp); 761 if (ACPI_SUCCESS(status)) 762 device->flags.ejectable = 1; 763 else { 764 status = acpi_get_handle(device->handle, "_EJ0", &temp); 765 if (ACPI_SUCCESS(status)) 766 device->flags.ejectable = 1; 767 } 768 769 /* Presence of _LCK indicates 'lockable' */ 770 status = acpi_get_handle(device->handle, "_LCK", &temp); 771 if (ACPI_SUCCESS(status)) 772 device->flags.lockable = 1; 773 774 /* Presence of _PS0|_PR0 indicates 'power manageable' */ 775 status = acpi_get_handle(device->handle, "_PS0", &temp); 776 if (ACPI_FAILURE(status)) 777 status = acpi_get_handle(device->handle, "_PR0", &temp); 778 if (ACPI_SUCCESS(status)) 779 device->flags.power_manageable = 1; 780 781 /* Presence of _PRW indicates wake capable */ 782 status = acpi_get_handle(device->handle, "_PRW", &temp); 783 if (ACPI_SUCCESS(status)) 784 device->flags.wake_capable = 1; 785 786 /* TBD: Peformance management */ 787 788 return_VALUE(0); 789 } 790 791 static void acpi_device_get_busid(struct acpi_device * device, acpi_handle handle, int type) 792 { 793 char bus_id[5] = {'?',0}; 794 struct acpi_buffer buffer = {sizeof(bus_id), bus_id}; 795 int i = 0; 796 797 /* 798 * Bus ID 799 * ------ 800 * The device's Bus ID is simply the object name. 801 * TBD: Shouldn't this value be unique (within the ACPI namespace)? 802 */ 803 switch (type) { 804 case ACPI_BUS_TYPE_SYSTEM: 805 strcpy(device->pnp.bus_id, "ACPI"); 806 break; 807 case ACPI_BUS_TYPE_POWER_BUTTON: 808 strcpy(device->pnp.bus_id, "PWRF"); 809 break; 810 case ACPI_BUS_TYPE_SLEEP_BUTTON: 811 strcpy(device->pnp.bus_id, "SLPF"); 812 break; 813 default: 814 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); 815 /* Clean up trailing underscores (if any) */ 816 for (i = 3; i > 1; i--) { 817 if (bus_id[i] == '_') 818 bus_id[i] = '\0'; 819 else 820 break; 821 } 822 strcpy(device->pnp.bus_id, bus_id); 823 break; 824 } 825 } 826 827 static void acpi_device_set_id(struct acpi_device * device, struct acpi_device * parent, 828 acpi_handle handle, int type) 829 { 830 struct acpi_device_info *info; 831 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 832 char *hid = NULL; 833 char *uid = NULL; 834 struct acpi_compatible_id_list *cid_list = NULL; 835 acpi_status status; 836 837 switch (type) { 838 case ACPI_BUS_TYPE_DEVICE: 839 status = acpi_get_object_info(handle, &buffer); 840 if (ACPI_FAILURE(status)) { 841 printk("%s: Error reading device info\n",__FUNCTION__); 842 return; 843 } 844 845 info = buffer.pointer; 846 if (info->valid & ACPI_VALID_HID) 847 hid = info->hardware_id.value; 848 if (info->valid & ACPI_VALID_UID) 849 uid = info->unique_id.value; 850 if (info->valid & ACPI_VALID_CID) 851 cid_list = &info->compatibility_id; 852 if (info->valid & ACPI_VALID_ADR) { 853 device->pnp.bus_address = info->address; 854 device->flags.bus_address = 1; 855 } 856 break; 857 case ACPI_BUS_TYPE_POWER: 858 hid = ACPI_POWER_HID; 859 break; 860 case ACPI_BUS_TYPE_PROCESSOR: 861 hid = ACPI_PROCESSOR_HID; 862 break; 863 case ACPI_BUS_TYPE_SYSTEM: 864 hid = ACPI_SYSTEM_HID; 865 break; 866 case ACPI_BUS_TYPE_THERMAL: 867 hid = ACPI_THERMAL_HID; 868 break; 869 case ACPI_BUS_TYPE_POWER_BUTTON: 870 hid = ACPI_BUTTON_HID_POWERF; 871 break; 872 case ACPI_BUS_TYPE_SLEEP_BUTTON: 873 hid = ACPI_BUTTON_HID_SLEEPF; 874 break; 875 } 876 877 /* 878 * \_SB 879 * ---- 880 * Fix for the system root bus device -- the only root-level device. 881 */ 882 if ((parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) { 883 hid = ACPI_BUS_HID; 884 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME); 885 strcpy(device->pnp.device_class, ACPI_BUS_CLASS); 886 } 887 888 if (hid) { 889 strcpy(device->pnp.hardware_id, hid); 890 device->flags.hardware_id = 1; 891 } 892 if (uid) { 893 strcpy(device->pnp.unique_id, uid); 894 device->flags.unique_id = 1; 895 } 896 if (cid_list) { 897 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL); 898 if (device->pnp.cid_list) 899 memcpy(device->pnp.cid_list, cid_list, cid_list->size); 900 else 901 printk(KERN_ERR "Memory allocation error\n"); 902 } 903 904 acpi_os_free(buffer.pointer); 905 } 906 907 static int acpi_device_set_context(struct acpi_device * device, int type) 908 { 909 acpi_status status = AE_OK; 910 int result = 0; 911 /* 912 * Context 913 * ------- 914 * Attach this 'struct acpi_device' to the ACPI object. This makes 915 * resolutions from handle->device very efficient. Note that we need 916 * to be careful with fixed-feature devices as they all attach to the 917 * root object. 918 */ 919 if (type != ACPI_BUS_TYPE_POWER_BUTTON && 920 type != ACPI_BUS_TYPE_SLEEP_BUTTON) { 921 status = acpi_attach_data(device->handle, 922 acpi_bus_data_handler, device); 923 924 if (ACPI_FAILURE(status)) { 925 printk("Error attaching device data\n"); 926 result = -ENODEV; 927 } 928 } 929 return result; 930 } 931 932 static void acpi_device_get_debug_info(struct acpi_device * device, acpi_handle handle, int type) 933 { 934 #ifdef CONFIG_ACPI_DEBUG_OUTPUT 935 char *type_string = NULL; 936 char name[80] = {'?','\0'}; 937 struct acpi_buffer buffer = {sizeof(name), name}; 938 939 switch (type) { 940 case ACPI_BUS_TYPE_DEVICE: 941 type_string = "Device"; 942 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 943 break; 944 case ACPI_BUS_TYPE_POWER: 945 type_string = "Power Resource"; 946 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 947 break; 948 case ACPI_BUS_TYPE_PROCESSOR: 949 type_string = "Processor"; 950 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 951 break; 952 case ACPI_BUS_TYPE_SYSTEM: 953 type_string = "System"; 954 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 955 break; 956 case ACPI_BUS_TYPE_THERMAL: 957 type_string = "Thermal Zone"; 958 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 959 break; 960 case ACPI_BUS_TYPE_POWER_BUTTON: 961 type_string = "Power Button"; 962 sprintf(name, "PWRB"); 963 break; 964 case ACPI_BUS_TYPE_SLEEP_BUTTON: 965 type_string = "Sleep Button"; 966 sprintf(name, "SLPB"); 967 break; 968 } 969 970 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle); 971 #endif /*CONFIG_ACPI_DEBUG_OUTPUT*/ 972 } 973 974 975 static int 976 acpi_bus_remove ( 977 struct acpi_device *dev, 978 int rmdevice) 979 { 980 int result = 0; 981 struct acpi_driver *driver; 982 983 ACPI_FUNCTION_TRACE("acpi_bus_remove"); 984 985 if (!dev) 986 return_VALUE(-EINVAL); 987 988 driver = dev->driver; 989 990 if ((driver) && (driver->ops.remove)) { 991 992 if (driver->ops.stop) { 993 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT); 994 if (result) 995 return_VALUE(result); 996 } 997 998 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT); 999 if (result) { 1000 return_VALUE(result); 1001 } 1002 1003 atomic_dec(&dev->driver->references); 1004 dev->driver = NULL; 1005 acpi_driver_data(dev) = NULL; 1006 } 1007 1008 if (!rmdevice) 1009 return_VALUE(0); 1010 1011 if (dev->flags.bus_address) { 1012 if ((dev->parent) && (dev->parent->ops.unbind)) 1013 dev->parent->ops.unbind(dev); 1014 } 1015 1016 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT); 1017 1018 return_VALUE(0); 1019 } 1020 1021 1022 static int 1023 acpi_add_single_object ( 1024 struct acpi_device **child, 1025 struct acpi_device *parent, 1026 acpi_handle handle, 1027 int type) 1028 { 1029 int result = 0; 1030 struct acpi_device *device = NULL; 1031 1032 ACPI_FUNCTION_TRACE("acpi_add_single_object"); 1033 1034 if (!child) 1035 return_VALUE(-EINVAL); 1036 1037 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL); 1038 if (!device) { 1039 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n")); 1040 return_VALUE(-ENOMEM); 1041 } 1042 memset(device, 0, sizeof(struct acpi_device)); 1043 1044 device->handle = handle; 1045 device->parent = parent; 1046 1047 acpi_device_get_busid(device,handle,type); 1048 1049 /* 1050 * Flags 1051 * ----- 1052 * Get prior to calling acpi_bus_get_status() so we know whether 1053 * or not _STA is present. Note that we only look for object 1054 * handles -- cannot evaluate objects until we know the device is 1055 * present and properly initialized. 1056 */ 1057 result = acpi_bus_get_flags(device); 1058 if (result) 1059 goto end; 1060 1061 /* 1062 * Status 1063 * ------ 1064 * See if the device is present. We always assume that non-Device() 1065 * objects (e.g. thermal zones, power resources, processors, etc.) are 1066 * present, functioning, etc. (at least when parent object is present). 1067 * Note that _STA has a different meaning for some objects (e.g. 1068 * power resources) so we need to be careful how we use it. 1069 */ 1070 switch (type) { 1071 case ACPI_BUS_TYPE_DEVICE: 1072 result = acpi_bus_get_status(device); 1073 if (ACPI_FAILURE(result) || !device->status.present) { 1074 result = -ENOENT; 1075 goto end; 1076 } 1077 break; 1078 default: 1079 STRUCT_TO_INT(device->status) = 0x0F; 1080 break; 1081 } 1082 1083 /* 1084 * Initialize Device 1085 * ----------------- 1086 * TBD: Synch with Core's enumeration/initialization process. 1087 */ 1088 1089 /* 1090 * Hardware ID, Unique ID, & Bus Address 1091 * ------------------------------------- 1092 */ 1093 acpi_device_set_id(device,parent,handle,type); 1094 1095 /* 1096 * Power Management 1097 * ---------------- 1098 */ 1099 if (device->flags.power_manageable) { 1100 result = acpi_bus_get_power_flags(device); 1101 if (result) 1102 goto end; 1103 } 1104 1105 /* 1106 * Wakeup device management 1107 *----------------------- 1108 */ 1109 if (device->flags.wake_capable) { 1110 result = acpi_bus_get_wakeup_device_flags(device); 1111 if (result) 1112 goto end; 1113 } 1114 1115 /* 1116 * Performance Management 1117 * ---------------------- 1118 */ 1119 if (device->flags.performance_manageable) { 1120 result = acpi_bus_get_perf_flags(device); 1121 if (result) 1122 goto end; 1123 } 1124 1125 if ((result = acpi_device_set_context(device,type))) 1126 goto end; 1127 1128 acpi_device_get_debug_info(device,handle,type); 1129 1130 acpi_device_register(device,parent); 1131 1132 /* 1133 * Bind _ADR-Based Devices 1134 * ----------------------- 1135 * If there's a a bus address (_ADR) then we utilize the parent's 1136 * 'bind' function (if exists) to bind the ACPI- and natively- 1137 * enumerated device representations. 1138 */ 1139 if (device->flags.bus_address) { 1140 if (device->parent && device->parent->ops.bind) 1141 device->parent->ops.bind(device); 1142 } 1143 1144 /* 1145 * Locate & Attach Driver 1146 * ---------------------- 1147 * If there's a hardware id (_HID) or compatible ids (_CID) we check 1148 * to see if there's a driver installed for this kind of device. Note 1149 * that drivers can install before or after a device is enumerated. 1150 * 1151 * TBD: Assumes LDM provides driver hot-plug capability. 1152 */ 1153 result = acpi_bus_find_driver(device); 1154 1155 end: 1156 if (!result) 1157 *child = device; 1158 else { 1159 if (device->pnp.cid_list) 1160 kfree(device->pnp.cid_list); 1161 kfree(device); 1162 } 1163 1164 return_VALUE(result); 1165 } 1166 1167 1168 static int acpi_bus_scan (struct acpi_device *start, 1169 struct acpi_bus_ops *ops) 1170 { 1171 acpi_status status = AE_OK; 1172 struct acpi_device *parent = NULL; 1173 struct acpi_device *child = NULL; 1174 acpi_handle phandle = NULL; 1175 acpi_handle chandle = NULL; 1176 acpi_object_type type = 0; 1177 u32 level = 1; 1178 1179 ACPI_FUNCTION_TRACE("acpi_bus_scan"); 1180 1181 if (!start) 1182 return_VALUE(-EINVAL); 1183 1184 parent = start; 1185 phandle = start->handle; 1186 1187 /* 1188 * Parse through the ACPI namespace, identify all 'devices', and 1189 * create a new 'struct acpi_device' for each. 1190 */ 1191 while ((level > 0) && parent) { 1192 1193 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, 1194 chandle, &chandle); 1195 1196 /* 1197 * If this scope is exhausted then move our way back up. 1198 */ 1199 if (ACPI_FAILURE(status)) { 1200 level--; 1201 chandle = phandle; 1202 acpi_get_parent(phandle, &phandle); 1203 if (parent->parent) 1204 parent = parent->parent; 1205 continue; 1206 } 1207 1208 status = acpi_get_type(chandle, &type); 1209 if (ACPI_FAILURE(status)) 1210 continue; 1211 1212 /* 1213 * If this is a scope object then parse it (depth-first). 1214 */ 1215 if (type == ACPI_TYPE_LOCAL_SCOPE) { 1216 level++; 1217 phandle = chandle; 1218 chandle = NULL; 1219 continue; 1220 } 1221 1222 /* 1223 * We're only interested in objects that we consider 'devices'. 1224 */ 1225 switch (type) { 1226 case ACPI_TYPE_DEVICE: 1227 type = ACPI_BUS_TYPE_DEVICE; 1228 break; 1229 case ACPI_TYPE_PROCESSOR: 1230 type = ACPI_BUS_TYPE_PROCESSOR; 1231 break; 1232 case ACPI_TYPE_THERMAL: 1233 type = ACPI_BUS_TYPE_THERMAL; 1234 break; 1235 case ACPI_TYPE_POWER: 1236 type = ACPI_BUS_TYPE_POWER; 1237 break; 1238 default: 1239 continue; 1240 } 1241 1242 if (ops->acpi_op_add) 1243 status = acpi_add_single_object(&child, parent, 1244 chandle, type); 1245 else 1246 status = acpi_bus_get_device(chandle, &child); 1247 1248 if (ACPI_FAILURE(status)) 1249 continue; 1250 1251 if (ops->acpi_op_start) { 1252 status = acpi_start_single_object(child); 1253 if (ACPI_FAILURE(status)) 1254 continue; 1255 } 1256 1257 /* 1258 * If the device is present, enabled, and functioning then 1259 * parse its scope (depth-first). Note that we need to 1260 * represent absent devices to facilitate PnP notifications 1261 * -- but only the subtree head (not all of its children, 1262 * which will be enumerated when the parent is inserted). 1263 * 1264 * TBD: Need notifications and other detection mechanisms 1265 * in place before we can fully implement this. 1266 */ 1267 if (child->status.present) { 1268 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle, 1269 NULL, NULL); 1270 if (ACPI_SUCCESS(status)) { 1271 level++; 1272 phandle = chandle; 1273 chandle = NULL; 1274 parent = child; 1275 } 1276 } 1277 } 1278 1279 return_VALUE(0); 1280 } 1281 1282 int 1283 acpi_bus_add ( 1284 struct acpi_device **child, 1285 struct acpi_device *parent, 1286 acpi_handle handle, 1287 int type) 1288 { 1289 int result; 1290 struct acpi_bus_ops ops; 1291 1292 ACPI_FUNCTION_TRACE("acpi_bus_add"); 1293 1294 result = acpi_add_single_object(child, parent, handle, type); 1295 if (!result) { 1296 memset(&ops, 0, sizeof(ops)); 1297 ops.acpi_op_add = 1; 1298 result = acpi_bus_scan(*child, &ops); 1299 } 1300 return_VALUE(result); 1301 } 1302 EXPORT_SYMBOL(acpi_bus_add); 1303 1304 int 1305 acpi_bus_start ( 1306 struct acpi_device *device) 1307 { 1308 int result; 1309 struct acpi_bus_ops ops; 1310 1311 ACPI_FUNCTION_TRACE("acpi_bus_start"); 1312 1313 if (!device) 1314 return_VALUE(-EINVAL); 1315 1316 result = acpi_start_single_object(device); 1317 if (!result) { 1318 memset(&ops, 0, sizeof(ops)); 1319 ops.acpi_op_start = 1; 1320 result = acpi_bus_scan(device, &ops); 1321 } 1322 return_VALUE(result); 1323 } 1324 EXPORT_SYMBOL(acpi_bus_start); 1325 1326 static int 1327 acpi_bus_trim(struct acpi_device *start, 1328 int rmdevice) 1329 { 1330 acpi_status status; 1331 struct acpi_device *parent, *child; 1332 acpi_handle phandle, chandle; 1333 acpi_object_type type; 1334 u32 level = 1; 1335 int err = 0; 1336 1337 parent = start; 1338 phandle = start->handle; 1339 child = chandle = NULL; 1340 1341 while ((level > 0) && parent && (!err)) { 1342 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, 1343 chandle, &chandle); 1344 1345 /* 1346 * If this scope is exhausted then move our way back up. 1347 */ 1348 if (ACPI_FAILURE(status)) { 1349 level--; 1350 chandle = phandle; 1351 acpi_get_parent(phandle, &phandle); 1352 child = parent; 1353 parent = parent->parent; 1354 1355 if (level == 0) 1356 err = acpi_bus_remove(child, rmdevice); 1357 else 1358 err = acpi_bus_remove(child, 1); 1359 1360 continue; 1361 } 1362 1363 status = acpi_get_type(chandle, &type); 1364 if (ACPI_FAILURE(status)) { 1365 continue; 1366 } 1367 /* 1368 * If there is a device corresponding to chandle then 1369 * parse it (depth-first). 1370 */ 1371 if (acpi_bus_get_device(chandle, &child) == 0) { 1372 level++; 1373 phandle = chandle; 1374 chandle = NULL; 1375 parent = child; 1376 } 1377 continue; 1378 } 1379 return err; 1380 } 1381 1382 static int 1383 acpi_bus_scan_fixed ( 1384 struct acpi_device *root) 1385 { 1386 int result = 0; 1387 struct acpi_device *device = NULL; 1388 1389 ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed"); 1390 1391 if (!root) 1392 return_VALUE(-ENODEV); 1393 1394 /* 1395 * Enumerate all fixed-feature devices. 1396 */ 1397 if (acpi_fadt.pwr_button == 0) { 1398 result = acpi_add_single_object(&device, acpi_root, 1399 NULL, ACPI_BUS_TYPE_POWER_BUTTON); 1400 if (!result) 1401 result = acpi_start_single_object(device); 1402 } 1403 1404 if (acpi_fadt.sleep_button == 0) { 1405 result = acpi_add_single_object(&device, acpi_root, 1406 NULL, ACPI_BUS_TYPE_SLEEP_BUTTON); 1407 if (!result) 1408 result = acpi_start_single_object(device); 1409 } 1410 1411 return_VALUE(result); 1412 } 1413 1414 1415 static int __init acpi_scan_init(void) 1416 { 1417 int result; 1418 struct acpi_bus_ops ops; 1419 1420 ACPI_FUNCTION_TRACE("acpi_scan_init"); 1421 1422 if (acpi_disabled) 1423 return_VALUE(0); 1424 1425 kset_register(&acpi_namespace_kset); 1426 1427 /* 1428 * Create the root device in the bus's device tree 1429 */ 1430 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT, 1431 ACPI_BUS_TYPE_SYSTEM); 1432 if (result) 1433 goto Done; 1434 1435 result = acpi_start_single_object(acpi_root); 1436 1437 /* 1438 * Enumerate devices in the ACPI namespace. 1439 */ 1440 result = acpi_bus_scan_fixed(acpi_root); 1441 if (!result) { 1442 memset(&ops, 0, sizeof(ops)); 1443 ops.acpi_op_add = 1; 1444 ops.acpi_op_start = 1; 1445 result = acpi_bus_scan(acpi_root, &ops); 1446 } 1447 1448 if (result) 1449 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL); 1450 1451 Done: 1452 return_VALUE(result); 1453 } 1454 1455 subsys_initcall(acpi_scan_init); 1456