11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * scan.c - support for transforming the ACPI namespace into individual objects 31da177e4SLinus Torvalds */ 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds #include <linux/module.h> 61da177e4SLinus Torvalds #include <linux/init.h> 71da177e4SLinus Torvalds #include <linux/acpi.h> 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <acpi/acpi_drivers.h> 101da177e4SLinus Torvalds #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds #define _COMPONENT ACPI_BUS_COMPONENT 141da177e4SLinus Torvalds ACPI_MODULE_NAME ("scan") 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #define STRUCT_TO_INT(s) (*((int*)&s)) 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds extern struct acpi_device *acpi_root; 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #define ACPI_BUS_CLASS "system_bus" 221da177e4SLinus Torvalds #define ACPI_BUS_HID "ACPI_BUS" 231da177e4SLinus Torvalds #define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver" 241da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME "System Bus" 251da177e4SLinus Torvalds 261da177e4SLinus Torvalds static LIST_HEAD(acpi_device_list); 271da177e4SLinus Torvalds DEFINE_SPINLOCK(acpi_device_lock); 281da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list); 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds static int 311da177e4SLinus Torvalds acpi_bus_trim(struct acpi_device *start, 321da177e4SLinus Torvalds int rmdevice); 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds static void acpi_device_release(struct kobject * kobj) 351da177e4SLinus Torvalds { 361da177e4SLinus Torvalds struct acpi_device * dev = container_of(kobj,struct acpi_device,kobj); 371da177e4SLinus Torvalds if (dev->pnp.cid_list) 381da177e4SLinus Torvalds kfree(dev->pnp.cid_list); 391da177e4SLinus Torvalds kfree(dev); 401da177e4SLinus Torvalds } 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds struct acpi_device_attribute { 431da177e4SLinus Torvalds struct attribute attr; 441da177e4SLinus Torvalds ssize_t (*show)(struct acpi_device *, char *); 451da177e4SLinus Torvalds ssize_t (*store)(struct acpi_device *, const char *, size_t); 461da177e4SLinus Torvalds }; 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds typedef void acpi_device_sysfs_files(struct kobject *, 491da177e4SLinus Torvalds const struct attribute *); 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds static void setup_sys_fs_device_files(struct acpi_device *dev, 521da177e4SLinus Torvalds acpi_device_sysfs_files *func); 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #define create_sysfs_device_files(dev) \ 551da177e4SLinus Torvalds setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file) 561da177e4SLinus Torvalds #define remove_sysfs_device_files(dev) \ 571da177e4SLinus Torvalds setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file) 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds #define to_acpi_device(n) container_of(n, struct acpi_device, kobj) 611da177e4SLinus Torvalds #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr); 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds static ssize_t acpi_device_attr_show(struct kobject *kobj, 641da177e4SLinus Torvalds struct attribute *attr, char *buf) 651da177e4SLinus Torvalds { 661da177e4SLinus Torvalds struct acpi_device *device = to_acpi_device(kobj); 671da177e4SLinus Torvalds struct acpi_device_attribute *attribute = to_handle_attr(attr); 6870f2817aSDmitry Torokhov return attribute->show ? attribute->show(device, buf) : -EIO; 691da177e4SLinus Torvalds } 701da177e4SLinus Torvalds static ssize_t acpi_device_attr_store(struct kobject *kobj, 711da177e4SLinus Torvalds struct attribute *attr, const char *buf, size_t len) 721da177e4SLinus Torvalds { 731da177e4SLinus Torvalds struct acpi_device *device = to_acpi_device(kobj); 741da177e4SLinus Torvalds struct acpi_device_attribute *attribute = to_handle_attr(attr); 7570f2817aSDmitry Torokhov return attribute->store ? attribute->store(device, buf, len) : -EIO; 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds static struct sysfs_ops acpi_device_sysfs_ops = { 791da177e4SLinus Torvalds .show = acpi_device_attr_show, 801da177e4SLinus Torvalds .store = acpi_device_attr_store, 811da177e4SLinus Torvalds }; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds static struct kobj_type ktype_acpi_ns = { 841da177e4SLinus Torvalds .sysfs_ops = &acpi_device_sysfs_ops, 851da177e4SLinus Torvalds .release = acpi_device_release, 861da177e4SLinus Torvalds }; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds static int namespace_hotplug(struct kset *kset, struct kobject *kobj, 891da177e4SLinus Torvalds char **envp, int num_envp, char *buffer, 901da177e4SLinus Torvalds int buffer_size) 911da177e4SLinus Torvalds { 921da177e4SLinus Torvalds struct acpi_device *dev = to_acpi_device(kobj); 931da177e4SLinus Torvalds int i = 0; 941da177e4SLinus Torvalds int len = 0; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds if (!dev->driver) 971da177e4SLinus Torvalds return 0; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds if (add_hotplug_env_var(envp, num_envp, &i, buffer, buffer_size, &len, 1001da177e4SLinus Torvalds "PHYSDEVDRIVER=%s", dev->driver->name)) 1011da177e4SLinus Torvalds return -ENOMEM; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds envp[i] = NULL; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds return 0; 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds static struct kset_hotplug_ops namespace_hotplug_ops = { 1091da177e4SLinus Torvalds .hotplug = &namespace_hotplug, 1101da177e4SLinus Torvalds }; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds static struct kset acpi_namespace_kset = { 1131da177e4SLinus Torvalds .kobj = { 1141da177e4SLinus Torvalds .name = "namespace", 1151da177e4SLinus Torvalds }, 1161da177e4SLinus Torvalds .subsys = &acpi_subsys, 1171da177e4SLinus Torvalds .ktype = &ktype_acpi_ns, 1181da177e4SLinus Torvalds .hotplug_ops = &namespace_hotplug_ops, 1191da177e4SLinus Torvalds }; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds static void acpi_device_register(struct acpi_device * device, struct acpi_device * parent) 1231da177e4SLinus Torvalds { 1241da177e4SLinus Torvalds /* 1251da177e4SLinus Torvalds * Linkage 1261da177e4SLinus Torvalds * ------- 1271da177e4SLinus Torvalds * Link this device to its parent and siblings. 1281da177e4SLinus Torvalds */ 1291da177e4SLinus Torvalds INIT_LIST_HEAD(&device->children); 1301da177e4SLinus Torvalds INIT_LIST_HEAD(&device->node); 1311da177e4SLinus Torvalds INIT_LIST_HEAD(&device->g_list); 1321da177e4SLinus Torvalds INIT_LIST_HEAD(&device->wakeup_list); 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds spin_lock(&acpi_device_lock); 1351da177e4SLinus Torvalds if (device->parent) { 1361da177e4SLinus Torvalds list_add_tail(&device->node, &device->parent->children); 1371da177e4SLinus Torvalds list_add_tail(&device->g_list,&device->parent->g_list); 1381da177e4SLinus Torvalds } else 1391da177e4SLinus Torvalds list_add_tail(&device->g_list,&acpi_device_list); 1401da177e4SLinus Torvalds if (device->wakeup.flags.valid) 1411da177e4SLinus Torvalds list_add_tail(&device->wakeup_list,&acpi_wakeup_device_list); 1421da177e4SLinus Torvalds spin_unlock(&acpi_device_lock); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds strlcpy(device->kobj.name,device->pnp.bus_id,KOBJ_NAME_LEN); 1451da177e4SLinus Torvalds if (parent) 1461da177e4SLinus Torvalds device->kobj.parent = &parent->kobj; 1471da177e4SLinus Torvalds device->kobj.ktype = &ktype_acpi_ns; 1481da177e4SLinus Torvalds device->kobj.kset = &acpi_namespace_kset; 1491da177e4SLinus Torvalds kobject_register(&device->kobj); 1501da177e4SLinus Torvalds create_sysfs_device_files(device); 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds static int 1541da177e4SLinus Torvalds acpi_device_unregister ( 1551da177e4SLinus Torvalds struct acpi_device *device, 1561da177e4SLinus Torvalds int type) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds spin_lock(&acpi_device_lock); 1591da177e4SLinus Torvalds if (device->parent) { 1601da177e4SLinus Torvalds list_del(&device->node); 1611da177e4SLinus Torvalds list_del(&device->g_list); 1621da177e4SLinus Torvalds } else 1631da177e4SLinus Torvalds list_del(&device->g_list); 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds list_del(&device->wakeup_list); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds spin_unlock(&acpi_device_lock); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds acpi_detach_data(device->handle, acpi_bus_data_handler); 1701da177e4SLinus Torvalds remove_sysfs_device_files(device); 1711da177e4SLinus Torvalds kobject_unregister(&device->kobj); 1721da177e4SLinus Torvalds return 0; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds void 1761da177e4SLinus Torvalds acpi_bus_data_handler ( 1771da177e4SLinus Torvalds acpi_handle handle, 1781da177e4SLinus Torvalds u32 function, 1791da177e4SLinus Torvalds void *context) 1801da177e4SLinus Torvalds { 1811da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_bus_data_handler"); 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds /* TBD */ 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds return_VOID; 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds static int 1891da177e4SLinus Torvalds acpi_bus_get_power_flags ( 1901da177e4SLinus Torvalds struct acpi_device *device) 1911da177e4SLinus Torvalds { 1921da177e4SLinus Torvalds acpi_status status = 0; 1931da177e4SLinus Torvalds acpi_handle handle = NULL; 1941da177e4SLinus Torvalds u32 i = 0; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags"); 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds /* 1991da177e4SLinus Torvalds * Power Management Flags 2001da177e4SLinus Torvalds */ 2011da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_PSC", &handle); 2021da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 2031da177e4SLinus Torvalds device->power.flags.explicit_get = 1; 2041da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_IRC", &handle); 2051da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 2061da177e4SLinus Torvalds device->power.flags.inrush_current = 1; 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds /* 2091da177e4SLinus Torvalds * Enumerate supported power management states 2101da177e4SLinus Torvalds */ 2111da177e4SLinus Torvalds for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) { 2121da177e4SLinus Torvalds struct acpi_device_power_state *ps = &device->power.states[i]; 2131da177e4SLinus Torvalds char object_name[5] = {'_','P','R','0'+i,'\0'}; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds /* Evaluate "_PRx" to se if power resources are referenced */ 2161da177e4SLinus Torvalds acpi_evaluate_reference(device->handle, object_name, NULL, 2171da177e4SLinus Torvalds &ps->resources); 2181da177e4SLinus Torvalds if (ps->resources.count) { 2191da177e4SLinus Torvalds device->power.flags.power_resources = 1; 2201da177e4SLinus Torvalds ps->flags.valid = 1; 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds /* Evaluate "_PSx" to see if we can do explicit sets */ 2241da177e4SLinus Torvalds object_name[2] = 'S'; 2251da177e4SLinus Torvalds status = acpi_get_handle(device->handle, object_name, &handle); 2261da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) { 2271da177e4SLinus Torvalds ps->flags.explicit_set = 1; 2281da177e4SLinus Torvalds ps->flags.valid = 1; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /* State is valid if we have some power control */ 2321da177e4SLinus Torvalds if (ps->resources.count || ps->flags.explicit_set) 2331da177e4SLinus Torvalds ps->flags.valid = 1; 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds ps->power = -1; /* Unknown - driver assigned */ 2361da177e4SLinus Torvalds ps->latency = -1; /* Unknown - driver assigned */ 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds /* Set defaults for D0 and D3 states (always valid) */ 2401da177e4SLinus Torvalds device->power.states[ACPI_STATE_D0].flags.valid = 1; 2411da177e4SLinus Torvalds device->power.states[ACPI_STATE_D0].power = 100; 2421da177e4SLinus Torvalds device->power.states[ACPI_STATE_D3].flags.valid = 1; 2431da177e4SLinus Torvalds device->power.states[ACPI_STATE_D3].power = 0; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds /* TBD: System wake support and resource requirements. */ 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds device->power.state = ACPI_STATE_UNKNOWN; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds return_VALUE(0); 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds int 2531da177e4SLinus Torvalds acpi_match_ids ( 2541da177e4SLinus Torvalds struct acpi_device *device, 2551da177e4SLinus Torvalds char *ids) 2561da177e4SLinus Torvalds { 2571da177e4SLinus Torvalds int error = 0; 2581da177e4SLinus Torvalds struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds if (device->flags.hardware_id) 2611da177e4SLinus Torvalds if (strstr(ids, device->pnp.hardware_id)) 2621da177e4SLinus Torvalds goto Done; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds if (device->flags.compatible_ids) { 2651da177e4SLinus Torvalds struct acpi_compatible_id_list *cid_list = device->pnp.cid_list; 2661da177e4SLinus Torvalds int i; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /* compare multiple _CID entries against driver ids */ 2691da177e4SLinus Torvalds for (i = 0; i < cid_list->count; i++) 2701da177e4SLinus Torvalds { 2711da177e4SLinus Torvalds if (strstr(ids, cid_list->id[i].value)) 2721da177e4SLinus Torvalds goto Done; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds error = -ENOENT; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds Done: 2781da177e4SLinus Torvalds if (buffer.pointer) 2791da177e4SLinus Torvalds acpi_os_free(buffer.pointer); 2801da177e4SLinus Torvalds return error; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds static acpi_status 2841da177e4SLinus Torvalds acpi_bus_extract_wakeup_device_power_package ( 2851da177e4SLinus Torvalds struct acpi_device *device, 2861da177e4SLinus Torvalds union acpi_object *package) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds int i = 0; 2891da177e4SLinus Torvalds union acpi_object *element = NULL; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds if (!device || !package || (package->package.count < 2)) 2921da177e4SLinus Torvalds return AE_BAD_PARAMETER; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds element = &(package->package.elements[0]); 2951da177e4SLinus Torvalds if (!element) 2961da177e4SLinus Torvalds return AE_BAD_PARAMETER; 2971da177e4SLinus Torvalds if (element->type == ACPI_TYPE_PACKAGE) { 2981da177e4SLinus Torvalds if ((element->package.count < 2) || 2991da177e4SLinus Torvalds (element->package.elements[0].type != ACPI_TYPE_LOCAL_REFERENCE) || 3001da177e4SLinus Torvalds (element->package.elements[1].type != ACPI_TYPE_INTEGER)) 3011da177e4SLinus Torvalds return AE_BAD_DATA; 3021da177e4SLinus Torvalds device->wakeup.gpe_device = element->package.elements[0].reference.handle; 3031da177e4SLinus Torvalds device->wakeup.gpe_number = (u32)element->package.elements[1].integer.value; 3041da177e4SLinus Torvalds }else if (element->type == ACPI_TYPE_INTEGER) { 3051da177e4SLinus Torvalds device->wakeup.gpe_number = element->integer.value; 3061da177e4SLinus Torvalds }else 3071da177e4SLinus Torvalds return AE_BAD_DATA; 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds element = &(package->package.elements[1]); 3101da177e4SLinus Torvalds if (element->type != ACPI_TYPE_INTEGER) { 3111da177e4SLinus Torvalds return AE_BAD_DATA; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds device->wakeup.sleep_state = element->integer.value; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds if ((package->package.count - 2) > ACPI_MAX_HANDLES) { 3161da177e4SLinus Torvalds return AE_NO_MEMORY; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds device->wakeup.resources.count = package->package.count - 2; 3191da177e4SLinus Torvalds for (i=0; i < device->wakeup.resources.count; i++) { 3201da177e4SLinus Torvalds element = &(package->package.elements[i + 2]); 3211da177e4SLinus Torvalds if (element->type != ACPI_TYPE_ANY ) { 3221da177e4SLinus Torvalds return AE_BAD_DATA; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds device->wakeup.resources.handles[i] = element->reference.handle; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds return AE_OK; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds static int 3321da177e4SLinus Torvalds acpi_bus_get_wakeup_device_flags ( 3331da177e4SLinus Torvalds struct acpi_device *device) 3341da177e4SLinus Torvalds { 3351da177e4SLinus Torvalds acpi_status status = 0; 3361da177e4SLinus Torvalds struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 3371da177e4SLinus Torvalds union acpi_object *package = NULL; 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags"); 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds /* _PRW */ 3421da177e4SLinus Torvalds status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer); 3431da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 3441da177e4SLinus Torvalds ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n")); 3451da177e4SLinus Torvalds goto end; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds package = (union acpi_object *) buffer.pointer; 3491da177e4SLinus Torvalds status = acpi_bus_extract_wakeup_device_power_package(device, package); 3501da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 3511da177e4SLinus Torvalds ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error extracting _PRW package\n")); 3521da177e4SLinus Torvalds goto end; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds acpi_os_free(buffer.pointer); 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds device->wakeup.flags.valid = 1; 3581da177e4SLinus Torvalds /* Power button, Lid switch always enable wakeup*/ 3591da177e4SLinus Torvalds if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E")) 3601da177e4SLinus Torvalds device->wakeup.flags.run_wake = 1; 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds end: 3631da177e4SLinus Torvalds if (ACPI_FAILURE(status)) 3641da177e4SLinus Torvalds device->flags.wake_capable = 0; 3651da177e4SLinus Torvalds return_VALUE(0); 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds /* -------------------------------------------------------------------------- 3691da177e4SLinus Torvalds ACPI hotplug sysfs device file support 3701da177e4SLinus Torvalds -------------------------------------------------------------------------- */ 3711da177e4SLinus Torvalds static ssize_t acpi_eject_store(struct acpi_device *device, 3721da177e4SLinus Torvalds const char *buf, size_t count); 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \ 3751da177e4SLinus Torvalds static struct acpi_device_attribute acpi_device_attr_##_name = \ 3761da177e4SLinus Torvalds __ATTR(_name, _mode, _show, _store) 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store); 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds /** 3811da177e4SLinus Torvalds * setup_sys_fs_device_files - sets up the device files under device namespace 38267be2dd1SMartin Waitz * @dev: acpi_device object 38367be2dd1SMartin Waitz * @func: function pointer to create or destroy the device file 3841da177e4SLinus Torvalds */ 3851da177e4SLinus Torvalds static void 3861da177e4SLinus Torvalds setup_sys_fs_device_files ( 3871da177e4SLinus Torvalds struct acpi_device *dev, 3881da177e4SLinus Torvalds acpi_device_sysfs_files *func) 3891da177e4SLinus Torvalds { 3901da177e4SLinus Torvalds acpi_status status; 3911da177e4SLinus Torvalds acpi_handle temp = NULL; 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds /* 3941da177e4SLinus Torvalds * If device has _EJ0, 'eject' file is created that is used to trigger 3951da177e4SLinus Torvalds * hot-removal function from userland. 3961da177e4SLinus Torvalds */ 3971da177e4SLinus Torvalds status = acpi_get_handle(dev->handle, "_EJ0", &temp); 3981da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 3991da177e4SLinus Torvalds (*(func))(&dev->kobj,&acpi_device_attr_eject.attr); 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds static int 4031da177e4SLinus Torvalds acpi_eject_operation(acpi_handle handle, int lockable) 4041da177e4SLinus Torvalds { 4051da177e4SLinus Torvalds struct acpi_object_list arg_list; 4061da177e4SLinus Torvalds union acpi_object arg; 4071da177e4SLinus Torvalds acpi_status status = AE_OK; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds /* 4101da177e4SLinus Torvalds * TBD: evaluate _PS3? 4111da177e4SLinus Torvalds */ 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds if (lockable) { 4141da177e4SLinus Torvalds arg_list.count = 1; 4151da177e4SLinus Torvalds arg_list.pointer = &arg; 4161da177e4SLinus Torvalds arg.type = ACPI_TYPE_INTEGER; 4171da177e4SLinus Torvalds arg.integer.value = 0; 4181da177e4SLinus Torvalds acpi_evaluate_object(handle, "_LCK", &arg_list, NULL); 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds arg_list.count = 1; 4221da177e4SLinus Torvalds arg_list.pointer = &arg; 4231da177e4SLinus Torvalds arg.type = ACPI_TYPE_INTEGER; 4241da177e4SLinus Torvalds arg.integer.value = 1; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds /* 4271da177e4SLinus Torvalds * TBD: _EJD support. 4281da177e4SLinus Torvalds */ 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL); 4311da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 4321da177e4SLinus Torvalds return(-ENODEV); 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds return(0); 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds static ssize_t 4401da177e4SLinus Torvalds acpi_eject_store(struct acpi_device *device, const char *buf, size_t count) 4411da177e4SLinus Torvalds { 4421da177e4SLinus Torvalds int result; 4431da177e4SLinus Torvalds int ret = count; 4441da177e4SLinus Torvalds int islockable; 4451da177e4SLinus Torvalds acpi_status status; 4461da177e4SLinus Torvalds acpi_handle handle; 4471da177e4SLinus Torvalds acpi_object_type type = 0; 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds if ((!count) || (buf[0] != '1')) { 4501da177e4SLinus Torvalds return -EINVAL; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds #ifndef FORCE_EJECT 4541da177e4SLinus Torvalds if (device->driver == NULL) { 4551da177e4SLinus Torvalds ret = -ENODEV; 4561da177e4SLinus Torvalds goto err; 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds #endif 4591da177e4SLinus Torvalds status = acpi_get_type(device->handle, &type); 4601da177e4SLinus Torvalds if (ACPI_FAILURE(status) || (!device->flags.ejectable) ) { 4611da177e4SLinus Torvalds ret = -ENODEV; 4621da177e4SLinus Torvalds goto err; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds islockable = device->flags.lockable; 4661da177e4SLinus Torvalds handle = device->handle; 4671da177e4SLinus Torvalds 4681da177e4SLinus Torvalds if (type == ACPI_TYPE_PROCESSOR) 4691da177e4SLinus Torvalds result = acpi_bus_trim(device, 0); 4701da177e4SLinus Torvalds else 4711da177e4SLinus Torvalds result = acpi_bus_trim(device, 1); 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds if (!result) 4741da177e4SLinus Torvalds result = acpi_eject_operation(handle, islockable); 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds if (result) { 4771da177e4SLinus Torvalds ret = -EBUSY; 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds err: 4801da177e4SLinus Torvalds return ret; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds /* -------------------------------------------------------------------------- 4851da177e4SLinus Torvalds Performance Management 4861da177e4SLinus Torvalds -------------------------------------------------------------------------- */ 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds static int 4891da177e4SLinus Torvalds acpi_bus_get_perf_flags ( 4901da177e4SLinus Torvalds struct acpi_device *device) 4911da177e4SLinus Torvalds { 4921da177e4SLinus Torvalds device->performance.state = ACPI_STATE_UNKNOWN; 4931da177e4SLinus Torvalds return 0; 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds /* -------------------------------------------------------------------------- 4971da177e4SLinus Torvalds Driver Management 4981da177e4SLinus Torvalds -------------------------------------------------------------------------- */ 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds static LIST_HEAD(acpi_bus_drivers); 5011da177e4SLinus Torvalds static DECLARE_MUTEX(acpi_bus_drivers_lock); 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds /** 5051da177e4SLinus Torvalds * acpi_bus_match 5061da177e4SLinus Torvalds * -------------- 5071da177e4SLinus Torvalds * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it 5081da177e4SLinus Torvalds * matches the specified driver's criteria. 5091da177e4SLinus Torvalds */ 5101da177e4SLinus Torvalds static int 5111da177e4SLinus Torvalds acpi_bus_match ( 5121da177e4SLinus Torvalds struct acpi_device *device, 5131da177e4SLinus Torvalds struct acpi_driver *driver) 5141da177e4SLinus Torvalds { 5151da177e4SLinus Torvalds if (driver && driver->ops.match) 5161da177e4SLinus Torvalds return driver->ops.match(device, driver); 5171da177e4SLinus Torvalds return acpi_match_ids(device, driver->ids); 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds /** 5221da177e4SLinus Torvalds * acpi_bus_driver_init 5231da177e4SLinus Torvalds * -------------------- 5241da177e4SLinus Torvalds * Used to initialize a device via its device driver. Called whenever a 5251da177e4SLinus Torvalds * driver is bound to a device. Invokes the driver's add() and start() ops. 5261da177e4SLinus Torvalds */ 5271da177e4SLinus Torvalds static int 5281da177e4SLinus Torvalds acpi_bus_driver_init ( 5291da177e4SLinus Torvalds struct acpi_device *device, 5301da177e4SLinus Torvalds struct acpi_driver *driver) 5311da177e4SLinus Torvalds { 5321da177e4SLinus Torvalds int result = 0; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_bus_driver_init"); 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds if (!device || !driver) 5371da177e4SLinus Torvalds return_VALUE(-EINVAL); 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds if (!driver->ops.add) 5401da177e4SLinus Torvalds return_VALUE(-ENOSYS); 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds result = driver->ops.add(device); 5431da177e4SLinus Torvalds if (result) { 5441da177e4SLinus Torvalds device->driver = NULL; 5451da177e4SLinus Torvalds acpi_driver_data(device) = NULL; 5461da177e4SLinus Torvalds return_VALUE(result); 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds device->driver = driver; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds /* 5521da177e4SLinus Torvalds * TBD - Configuration Management: Assign resources to device based 5531da177e4SLinus Torvalds * upon possible configuration and currently allocated resources. 5541da177e4SLinus Torvalds */ 5551da177e4SLinus Torvalds 556*3fb02738SRajesh Shah ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Driver successfully bound to device\n")); 557*3fb02738SRajesh Shah return_VALUE(0); 558*3fb02738SRajesh Shah } 559*3fb02738SRajesh Shah 560*3fb02738SRajesh Shah int 561*3fb02738SRajesh Shah acpi_start_single_object ( 562*3fb02738SRajesh Shah struct acpi_device *device) 563*3fb02738SRajesh Shah { 564*3fb02738SRajesh Shah int result = 0; 565*3fb02738SRajesh Shah struct acpi_driver *driver; 566*3fb02738SRajesh Shah 567*3fb02738SRajesh Shah ACPI_FUNCTION_TRACE("acpi_start_single_object"); 568*3fb02738SRajesh Shah 569*3fb02738SRajesh Shah if (!(driver = device->driver)) 570*3fb02738SRajesh Shah return_VALUE(0); 571*3fb02738SRajesh Shah 5721da177e4SLinus Torvalds if (driver->ops.start) { 5731da177e4SLinus Torvalds result = driver->ops.start(device); 5741da177e4SLinus Torvalds if (result && driver->ops.remove) 5751da177e4SLinus Torvalds driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL); 576*3fb02738SRajesh Shah } 577*3fb02738SRajesh Shah 5781da177e4SLinus Torvalds return_VALUE(result); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds static int acpi_driver_attach(struct acpi_driver * drv) 5821da177e4SLinus Torvalds { 5831da177e4SLinus Torvalds struct list_head * node, * next; 5841da177e4SLinus Torvalds int count = 0; 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_driver_attach"); 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds spin_lock(&acpi_device_lock); 5891da177e4SLinus Torvalds list_for_each_safe(node, next, &acpi_device_list) { 5901da177e4SLinus Torvalds struct acpi_device * dev = container_of(node, struct acpi_device, g_list); 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds if (dev->driver || !dev->status.present) 5931da177e4SLinus Torvalds continue; 5941da177e4SLinus Torvalds spin_unlock(&acpi_device_lock); 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds if (!acpi_bus_match(dev, drv)) { 5971da177e4SLinus Torvalds if (!acpi_bus_driver_init(dev, drv)) { 598*3fb02738SRajesh Shah acpi_start_single_object(dev); 5991da177e4SLinus Torvalds atomic_inc(&drv->references); 6001da177e4SLinus Torvalds count++; 6011da177e4SLinus Torvalds ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n", 6021da177e4SLinus Torvalds drv->name, dev->pnp.bus_id)); 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds } 6051da177e4SLinus Torvalds spin_lock(&acpi_device_lock); 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds spin_unlock(&acpi_device_lock); 6081da177e4SLinus Torvalds return_VALUE(count); 6091da177e4SLinus Torvalds } 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds static int acpi_driver_detach(struct acpi_driver * drv) 6121da177e4SLinus Torvalds { 6131da177e4SLinus Torvalds struct list_head * node, * next; 6141da177e4SLinus Torvalds 6151da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_driver_detach"); 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds spin_lock(&acpi_device_lock); 6181da177e4SLinus Torvalds list_for_each_safe(node,next,&acpi_device_list) { 6191da177e4SLinus Torvalds struct acpi_device * dev = container_of(node,struct acpi_device,g_list); 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds if (dev->driver == drv) { 6221da177e4SLinus Torvalds spin_unlock(&acpi_device_lock); 6231da177e4SLinus Torvalds if (drv->ops.remove) 6241da177e4SLinus Torvalds drv->ops.remove(dev,ACPI_BUS_REMOVAL_NORMAL); 6251da177e4SLinus Torvalds spin_lock(&acpi_device_lock); 6261da177e4SLinus Torvalds dev->driver = NULL; 6271da177e4SLinus Torvalds dev->driver_data = NULL; 6281da177e4SLinus Torvalds atomic_dec(&drv->references); 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds spin_unlock(&acpi_device_lock); 6321da177e4SLinus Torvalds return_VALUE(0); 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds /** 6361da177e4SLinus Torvalds * acpi_bus_register_driver 6371da177e4SLinus Torvalds * ------------------------ 6381da177e4SLinus Torvalds * Registers a driver with the ACPI bus. Searches the namespace for all 6391da177e4SLinus Torvalds * devices that match the driver's criteria and binds. Returns the 6401da177e4SLinus Torvalds * number of devices that were claimed by the driver, or a negative 6411da177e4SLinus Torvalds * error status for failure. 6421da177e4SLinus Torvalds */ 6431da177e4SLinus Torvalds int 6441da177e4SLinus Torvalds acpi_bus_register_driver ( 6451da177e4SLinus Torvalds struct acpi_driver *driver) 6461da177e4SLinus Torvalds { 6471da177e4SLinus Torvalds int count; 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_bus_register_driver"); 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds if (acpi_disabled) 6521da177e4SLinus Torvalds return_VALUE(-ENODEV); 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds if (!driver) 6551da177e4SLinus Torvalds return_VALUE(-EINVAL); 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds spin_lock(&acpi_device_lock); 6581da177e4SLinus Torvalds list_add_tail(&driver->node, &acpi_bus_drivers); 6591da177e4SLinus Torvalds spin_unlock(&acpi_device_lock); 6601da177e4SLinus Torvalds count = acpi_driver_attach(driver); 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds return_VALUE(count); 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_register_driver); 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds /** 6681da177e4SLinus Torvalds * acpi_bus_unregister_driver 6691da177e4SLinus Torvalds * -------------------------- 6701da177e4SLinus Torvalds * Unregisters a driver with the ACPI bus. Searches the namespace for all 6711da177e4SLinus Torvalds * devices that match the driver's criteria and unbinds. 6721da177e4SLinus Torvalds */ 6731da177e4SLinus Torvalds int 6741da177e4SLinus Torvalds acpi_bus_unregister_driver ( 6751da177e4SLinus Torvalds struct acpi_driver *driver) 6761da177e4SLinus Torvalds { 6771da177e4SLinus Torvalds int error = 0; 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver"); 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds if (driver) { 6821da177e4SLinus Torvalds acpi_driver_detach(driver); 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds if (!atomic_read(&driver->references)) { 6851da177e4SLinus Torvalds spin_lock(&acpi_device_lock); 6861da177e4SLinus Torvalds list_del_init(&driver->node); 6871da177e4SLinus Torvalds spin_unlock(&acpi_device_lock); 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds } else 6901da177e4SLinus Torvalds error = -EINVAL; 6911da177e4SLinus Torvalds return_VALUE(error); 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver); 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds /** 6961da177e4SLinus Torvalds * acpi_bus_find_driver 6971da177e4SLinus Torvalds * -------------------- 6981da177e4SLinus Torvalds * Parses the list of registered drivers looking for a driver applicable for 6991da177e4SLinus Torvalds * the specified device. 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds static int 7021da177e4SLinus Torvalds acpi_bus_find_driver ( 7031da177e4SLinus Torvalds struct acpi_device *device) 7041da177e4SLinus Torvalds { 7051da177e4SLinus Torvalds int result = 0; 7061da177e4SLinus Torvalds struct list_head * node, *next; 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_bus_find_driver"); 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds spin_lock(&acpi_device_lock); 7111da177e4SLinus Torvalds list_for_each_safe(node,next,&acpi_bus_drivers) { 7121da177e4SLinus Torvalds struct acpi_driver * driver = container_of(node,struct acpi_driver,node); 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds atomic_inc(&driver->references); 7151da177e4SLinus Torvalds spin_unlock(&acpi_device_lock); 7161da177e4SLinus Torvalds if (!acpi_bus_match(device, driver)) { 7171da177e4SLinus Torvalds result = acpi_bus_driver_init(device, driver); 7181da177e4SLinus Torvalds if (!result) 7191da177e4SLinus Torvalds goto Done; 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds atomic_dec(&driver->references); 7221da177e4SLinus Torvalds spin_lock(&acpi_device_lock); 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds spin_unlock(&acpi_device_lock); 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds Done: 7271da177e4SLinus Torvalds return_VALUE(result); 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds /* -------------------------------------------------------------------------- 7321da177e4SLinus Torvalds Device Enumeration 7331da177e4SLinus Torvalds -------------------------------------------------------------------------- */ 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds static int 7361da177e4SLinus Torvalds acpi_bus_get_flags ( 7371da177e4SLinus Torvalds struct acpi_device *device) 7381da177e4SLinus Torvalds { 7391da177e4SLinus Torvalds acpi_status status = AE_OK; 7401da177e4SLinus Torvalds acpi_handle temp = NULL; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_bus_get_flags"); 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds /* Presence of _STA indicates 'dynamic_status' */ 7451da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_STA", &temp); 7461da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 7471da177e4SLinus Torvalds device->flags.dynamic_status = 1; 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds /* Presence of _CID indicates 'compatible_ids' */ 7501da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_CID", &temp); 7511da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 7521da177e4SLinus Torvalds device->flags.compatible_ids = 1; 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds /* Presence of _RMV indicates 'removable' */ 7551da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_RMV", &temp); 7561da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 7571da177e4SLinus Torvalds device->flags.removable = 1; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds /* Presence of _EJD|_EJ0 indicates 'ejectable' */ 7601da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_EJD", &temp); 7611da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 7621da177e4SLinus Torvalds device->flags.ejectable = 1; 7631da177e4SLinus Torvalds else { 7641da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_EJ0", &temp); 7651da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 7661da177e4SLinus Torvalds device->flags.ejectable = 1; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds /* Presence of _LCK indicates 'lockable' */ 7701da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_LCK", &temp); 7711da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 7721da177e4SLinus Torvalds device->flags.lockable = 1; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds /* Presence of _PS0|_PR0 indicates 'power manageable' */ 7751da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_PS0", &temp); 7761da177e4SLinus Torvalds if (ACPI_FAILURE(status)) 7771da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_PR0", &temp); 7781da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 7791da177e4SLinus Torvalds device->flags.power_manageable = 1; 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds /* Presence of _PRW indicates wake capable */ 7821da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_PRW", &temp); 7831da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 7841da177e4SLinus Torvalds device->flags.wake_capable = 1; 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds /* TBD: Peformance management */ 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds return_VALUE(0); 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds static void acpi_device_get_busid(struct acpi_device * device, acpi_handle handle, int type) 7921da177e4SLinus Torvalds { 7931da177e4SLinus Torvalds char bus_id[5] = {'?',0}; 7941da177e4SLinus Torvalds struct acpi_buffer buffer = {sizeof(bus_id), bus_id}; 7951da177e4SLinus Torvalds int i = 0; 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds /* 7981da177e4SLinus Torvalds * Bus ID 7991da177e4SLinus Torvalds * ------ 8001da177e4SLinus Torvalds * The device's Bus ID is simply the object name. 8011da177e4SLinus Torvalds * TBD: Shouldn't this value be unique (within the ACPI namespace)? 8021da177e4SLinus Torvalds */ 8031da177e4SLinus Torvalds switch (type) { 8041da177e4SLinus Torvalds case ACPI_BUS_TYPE_SYSTEM: 8051da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "ACPI"); 8061da177e4SLinus Torvalds break; 8071da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER_BUTTON: 8081da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "PWRF"); 8091da177e4SLinus Torvalds break; 8101da177e4SLinus Torvalds case ACPI_BUS_TYPE_SLEEP_BUTTON: 8111da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "SLPF"); 8121da177e4SLinus Torvalds break; 8131da177e4SLinus Torvalds default: 8141da177e4SLinus Torvalds acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); 8151da177e4SLinus Torvalds /* Clean up trailing underscores (if any) */ 8161da177e4SLinus Torvalds for (i = 3; i > 1; i--) { 8171da177e4SLinus Torvalds if (bus_id[i] == '_') 8181da177e4SLinus Torvalds bus_id[i] = '\0'; 8191da177e4SLinus Torvalds else 8201da177e4SLinus Torvalds break; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds strcpy(device->pnp.bus_id, bus_id); 8231da177e4SLinus Torvalds break; 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds static void acpi_device_set_id(struct acpi_device * device, struct acpi_device * parent, 8281da177e4SLinus Torvalds acpi_handle handle, int type) 8291da177e4SLinus Torvalds { 8301da177e4SLinus Torvalds struct acpi_device_info *info; 8311da177e4SLinus Torvalds struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 8321da177e4SLinus Torvalds char *hid = NULL; 8331da177e4SLinus Torvalds char *uid = NULL; 8341da177e4SLinus Torvalds struct acpi_compatible_id_list *cid_list = NULL; 8351da177e4SLinus Torvalds acpi_status status; 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds switch (type) { 8381da177e4SLinus Torvalds case ACPI_BUS_TYPE_DEVICE: 8391da177e4SLinus Torvalds status = acpi_get_object_info(handle, &buffer); 8401da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 8411da177e4SLinus Torvalds printk("%s: Error reading device info\n",__FUNCTION__); 8421da177e4SLinus Torvalds return; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds info = buffer.pointer; 8461da177e4SLinus Torvalds if (info->valid & ACPI_VALID_HID) 8471da177e4SLinus Torvalds hid = info->hardware_id.value; 8481da177e4SLinus Torvalds if (info->valid & ACPI_VALID_UID) 8491da177e4SLinus Torvalds uid = info->unique_id.value; 8501da177e4SLinus Torvalds if (info->valid & ACPI_VALID_CID) 8511da177e4SLinus Torvalds cid_list = &info->compatibility_id; 8521da177e4SLinus Torvalds if (info->valid & ACPI_VALID_ADR) { 8531da177e4SLinus Torvalds device->pnp.bus_address = info->address; 8541da177e4SLinus Torvalds device->flags.bus_address = 1; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds break; 8571da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER: 8581da177e4SLinus Torvalds hid = ACPI_POWER_HID; 8591da177e4SLinus Torvalds break; 8601da177e4SLinus Torvalds case ACPI_BUS_TYPE_PROCESSOR: 8611da177e4SLinus Torvalds hid = ACPI_PROCESSOR_HID; 8621da177e4SLinus Torvalds break; 8631da177e4SLinus Torvalds case ACPI_BUS_TYPE_SYSTEM: 8641da177e4SLinus Torvalds hid = ACPI_SYSTEM_HID; 8651da177e4SLinus Torvalds break; 8661da177e4SLinus Torvalds case ACPI_BUS_TYPE_THERMAL: 8671da177e4SLinus Torvalds hid = ACPI_THERMAL_HID; 8681da177e4SLinus Torvalds break; 8691da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER_BUTTON: 8701da177e4SLinus Torvalds hid = ACPI_BUTTON_HID_POWERF; 8711da177e4SLinus Torvalds break; 8721da177e4SLinus Torvalds case ACPI_BUS_TYPE_SLEEP_BUTTON: 8731da177e4SLinus Torvalds hid = ACPI_BUTTON_HID_SLEEPF; 8741da177e4SLinus Torvalds break; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds /* 8781da177e4SLinus Torvalds * \_SB 8791da177e4SLinus Torvalds * ---- 8801da177e4SLinus Torvalds * Fix for the system root bus device -- the only root-level device. 8811da177e4SLinus Torvalds */ 8821da177e4SLinus Torvalds if ((parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) { 8831da177e4SLinus Torvalds hid = ACPI_BUS_HID; 8841da177e4SLinus Torvalds strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME); 8851da177e4SLinus Torvalds strcpy(device->pnp.device_class, ACPI_BUS_CLASS); 8861da177e4SLinus Torvalds } 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds if (hid) { 8891da177e4SLinus Torvalds strcpy(device->pnp.hardware_id, hid); 8901da177e4SLinus Torvalds device->flags.hardware_id = 1; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds if (uid) { 8931da177e4SLinus Torvalds strcpy(device->pnp.unique_id, uid); 8941da177e4SLinus Torvalds device->flags.unique_id = 1; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds if (cid_list) { 8971da177e4SLinus Torvalds device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL); 8981da177e4SLinus Torvalds if (device->pnp.cid_list) 8991da177e4SLinus Torvalds memcpy(device->pnp.cid_list, cid_list, cid_list->size); 9001da177e4SLinus Torvalds else 9011da177e4SLinus Torvalds printk(KERN_ERR "Memory allocation error\n"); 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds acpi_os_free(buffer.pointer); 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds static int acpi_device_set_context(struct acpi_device * device, int type) 9081da177e4SLinus Torvalds { 9091da177e4SLinus Torvalds acpi_status status = AE_OK; 9101da177e4SLinus Torvalds int result = 0; 9111da177e4SLinus Torvalds /* 9121da177e4SLinus Torvalds * Context 9131da177e4SLinus Torvalds * ------- 9141da177e4SLinus Torvalds * Attach this 'struct acpi_device' to the ACPI object. This makes 9151da177e4SLinus Torvalds * resolutions from handle->device very efficient. Note that we need 9161da177e4SLinus Torvalds * to be careful with fixed-feature devices as they all attach to the 9171da177e4SLinus Torvalds * root object. 9181da177e4SLinus Torvalds */ 9191da177e4SLinus Torvalds if (type != ACPI_BUS_TYPE_POWER_BUTTON && 9201da177e4SLinus Torvalds type != ACPI_BUS_TYPE_SLEEP_BUTTON) { 9211da177e4SLinus Torvalds status = acpi_attach_data(device->handle, 9221da177e4SLinus Torvalds acpi_bus_data_handler, device); 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 9251da177e4SLinus Torvalds printk("Error attaching device data\n"); 9261da177e4SLinus Torvalds result = -ENODEV; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds return result; 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds static void acpi_device_get_debug_info(struct acpi_device * device, acpi_handle handle, int type) 9331da177e4SLinus Torvalds { 9341da177e4SLinus Torvalds #ifdef CONFIG_ACPI_DEBUG_OUTPUT 9351da177e4SLinus Torvalds char *type_string = NULL; 9361da177e4SLinus Torvalds char name[80] = {'?','\0'}; 9371da177e4SLinus Torvalds struct acpi_buffer buffer = {sizeof(name), name}; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds switch (type) { 9401da177e4SLinus Torvalds case ACPI_BUS_TYPE_DEVICE: 9411da177e4SLinus Torvalds type_string = "Device"; 9421da177e4SLinus Torvalds acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 9431da177e4SLinus Torvalds break; 9441da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER: 9451da177e4SLinus Torvalds type_string = "Power Resource"; 9461da177e4SLinus Torvalds acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 9471da177e4SLinus Torvalds break; 9481da177e4SLinus Torvalds case ACPI_BUS_TYPE_PROCESSOR: 9491da177e4SLinus Torvalds type_string = "Processor"; 9501da177e4SLinus Torvalds acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 9511da177e4SLinus Torvalds break; 9521da177e4SLinus Torvalds case ACPI_BUS_TYPE_SYSTEM: 9531da177e4SLinus Torvalds type_string = "System"; 9541da177e4SLinus Torvalds acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 9551da177e4SLinus Torvalds break; 9561da177e4SLinus Torvalds case ACPI_BUS_TYPE_THERMAL: 9571da177e4SLinus Torvalds type_string = "Thermal Zone"; 9581da177e4SLinus Torvalds acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 9591da177e4SLinus Torvalds break; 9601da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER_BUTTON: 9611da177e4SLinus Torvalds type_string = "Power Button"; 9621da177e4SLinus Torvalds sprintf(name, "PWRB"); 9631da177e4SLinus Torvalds break; 9641da177e4SLinus Torvalds case ACPI_BUS_TYPE_SLEEP_BUTTON: 9651da177e4SLinus Torvalds type_string = "Sleep Button"; 9661da177e4SLinus Torvalds sprintf(name, "SLPB"); 9671da177e4SLinus Torvalds break; 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle); 9711da177e4SLinus Torvalds #endif /*CONFIG_ACPI_DEBUG_OUTPUT*/ 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds static int 9761da177e4SLinus Torvalds acpi_bus_remove ( 9771da177e4SLinus Torvalds struct acpi_device *dev, 9781da177e4SLinus Torvalds int rmdevice) 9791da177e4SLinus Torvalds { 9801da177e4SLinus Torvalds int result = 0; 9811da177e4SLinus Torvalds struct acpi_driver *driver; 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_bus_remove"); 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds if (!dev) 9861da177e4SLinus Torvalds return_VALUE(-EINVAL); 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds driver = dev->driver; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds if ((driver) && (driver->ops.remove)) { 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds if (driver->ops.stop) { 9931da177e4SLinus Torvalds result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT); 9941da177e4SLinus Torvalds if (result) 9951da177e4SLinus Torvalds return_VALUE(result); 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT); 9991da177e4SLinus Torvalds if (result) { 10001da177e4SLinus Torvalds return_VALUE(result); 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds atomic_dec(&dev->driver->references); 10041da177e4SLinus Torvalds dev->driver = NULL; 10051da177e4SLinus Torvalds acpi_driver_data(dev) = NULL; 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds if (!rmdevice) 10091da177e4SLinus Torvalds return_VALUE(0); 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds if (dev->flags.bus_address) { 10121da177e4SLinus Torvalds if ((dev->parent) && (dev->parent->ops.unbind)) 10131da177e4SLinus Torvalds dev->parent->ops.unbind(dev); 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT); 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds return_VALUE(0); 10191da177e4SLinus Torvalds } 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds 1022*3fb02738SRajesh Shah static int 1023*3fb02738SRajesh Shah acpi_add_single_object ( 10241da177e4SLinus Torvalds struct acpi_device **child, 10251da177e4SLinus Torvalds struct acpi_device *parent, 10261da177e4SLinus Torvalds acpi_handle handle, 10271da177e4SLinus Torvalds int type) 10281da177e4SLinus Torvalds { 10291da177e4SLinus Torvalds int result = 0; 10301da177e4SLinus Torvalds struct acpi_device *device = NULL; 10311da177e4SLinus Torvalds 1032*3fb02738SRajesh Shah ACPI_FUNCTION_TRACE("acpi_add_single_object"); 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds if (!child) 10351da177e4SLinus Torvalds return_VALUE(-EINVAL); 10361da177e4SLinus Torvalds 10371da177e4SLinus Torvalds device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL); 10381da177e4SLinus Torvalds if (!device) { 10391da177e4SLinus Torvalds ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n")); 10401da177e4SLinus Torvalds return_VALUE(-ENOMEM); 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds memset(device, 0, sizeof(struct acpi_device)); 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds device->handle = handle; 10451da177e4SLinus Torvalds device->parent = parent; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds acpi_device_get_busid(device,handle,type); 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds /* 10501da177e4SLinus Torvalds * Flags 10511da177e4SLinus Torvalds * ----- 10521da177e4SLinus Torvalds * Get prior to calling acpi_bus_get_status() so we know whether 10531da177e4SLinus Torvalds * or not _STA is present. Note that we only look for object 10541da177e4SLinus Torvalds * handles -- cannot evaluate objects until we know the device is 10551da177e4SLinus Torvalds * present and properly initialized. 10561da177e4SLinus Torvalds */ 10571da177e4SLinus Torvalds result = acpi_bus_get_flags(device); 10581da177e4SLinus Torvalds if (result) 10591da177e4SLinus Torvalds goto end; 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds /* 10621da177e4SLinus Torvalds * Status 10631da177e4SLinus Torvalds * ------ 10641da177e4SLinus Torvalds * See if the device is present. We always assume that non-Device() 10651da177e4SLinus Torvalds * objects (e.g. thermal zones, power resources, processors, etc.) are 10661da177e4SLinus Torvalds * present, functioning, etc. (at least when parent object is present). 10671da177e4SLinus Torvalds * Note that _STA has a different meaning for some objects (e.g. 10681da177e4SLinus Torvalds * power resources) so we need to be careful how we use it. 10691da177e4SLinus Torvalds */ 10701da177e4SLinus Torvalds switch (type) { 10711da177e4SLinus Torvalds case ACPI_BUS_TYPE_DEVICE: 10721da177e4SLinus Torvalds result = acpi_bus_get_status(device); 10731da177e4SLinus Torvalds if (ACPI_FAILURE(result) || !device->status.present) { 10741da177e4SLinus Torvalds result = -ENOENT; 10751da177e4SLinus Torvalds goto end; 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds break; 10781da177e4SLinus Torvalds default: 10791da177e4SLinus Torvalds STRUCT_TO_INT(device->status) = 0x0F; 10801da177e4SLinus Torvalds break; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds /* 10841da177e4SLinus Torvalds * Initialize Device 10851da177e4SLinus Torvalds * ----------------- 10861da177e4SLinus Torvalds * TBD: Synch with Core's enumeration/initialization process. 10871da177e4SLinus Torvalds */ 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /* 10901da177e4SLinus Torvalds * Hardware ID, Unique ID, & Bus Address 10911da177e4SLinus Torvalds * ------------------------------------- 10921da177e4SLinus Torvalds */ 10931da177e4SLinus Torvalds acpi_device_set_id(device,parent,handle,type); 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds /* 10961da177e4SLinus Torvalds * Power Management 10971da177e4SLinus Torvalds * ---------------- 10981da177e4SLinus Torvalds */ 10991da177e4SLinus Torvalds if (device->flags.power_manageable) { 11001da177e4SLinus Torvalds result = acpi_bus_get_power_flags(device); 11011da177e4SLinus Torvalds if (result) 11021da177e4SLinus Torvalds goto end; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds /* 11061da177e4SLinus Torvalds * Wakeup device management 11071da177e4SLinus Torvalds *----------------------- 11081da177e4SLinus Torvalds */ 11091da177e4SLinus Torvalds if (device->flags.wake_capable) { 11101da177e4SLinus Torvalds result = acpi_bus_get_wakeup_device_flags(device); 11111da177e4SLinus Torvalds if (result) 11121da177e4SLinus Torvalds goto end; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds /* 11161da177e4SLinus Torvalds * Performance Management 11171da177e4SLinus Torvalds * ---------------------- 11181da177e4SLinus Torvalds */ 11191da177e4SLinus Torvalds if (device->flags.performance_manageable) { 11201da177e4SLinus Torvalds result = acpi_bus_get_perf_flags(device); 11211da177e4SLinus Torvalds if (result) 11221da177e4SLinus Torvalds goto end; 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds if ((result = acpi_device_set_context(device,type))) 11261da177e4SLinus Torvalds goto end; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds acpi_device_get_debug_info(device,handle,type); 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds acpi_device_register(device,parent); 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds /* 11331da177e4SLinus Torvalds * Bind _ADR-Based Devices 11341da177e4SLinus Torvalds * ----------------------- 11351da177e4SLinus Torvalds * If there's a a bus address (_ADR) then we utilize the parent's 11361da177e4SLinus Torvalds * 'bind' function (if exists) to bind the ACPI- and natively- 11371da177e4SLinus Torvalds * enumerated device representations. 11381da177e4SLinus Torvalds */ 11391da177e4SLinus Torvalds if (device->flags.bus_address) { 11401da177e4SLinus Torvalds if (device->parent && device->parent->ops.bind) 11411da177e4SLinus Torvalds device->parent->ops.bind(device); 11421da177e4SLinus Torvalds } 11431da177e4SLinus Torvalds 11441da177e4SLinus Torvalds /* 11451da177e4SLinus Torvalds * Locate & Attach Driver 11461da177e4SLinus Torvalds * ---------------------- 11471da177e4SLinus Torvalds * If there's a hardware id (_HID) or compatible ids (_CID) we check 11481da177e4SLinus Torvalds * to see if there's a driver installed for this kind of device. Note 11491da177e4SLinus Torvalds * that drivers can install before or after a device is enumerated. 11501da177e4SLinus Torvalds * 11511da177e4SLinus Torvalds * TBD: Assumes LDM provides driver hot-plug capability. 11521da177e4SLinus Torvalds */ 1153*3fb02738SRajesh Shah result = acpi_bus_find_driver(device); 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds end: 11561da177e4SLinus Torvalds if (!result) 11571da177e4SLinus Torvalds *child = device; 11581da177e4SLinus Torvalds else { 11591da177e4SLinus Torvalds if (device->pnp.cid_list) 11601da177e4SLinus Torvalds kfree(device->pnp.cid_list); 11611da177e4SLinus Torvalds kfree(device); 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds 11641da177e4SLinus Torvalds return_VALUE(result); 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds 1168*3fb02738SRajesh Shah static int acpi_bus_scan (struct acpi_device *start, 1169*3fb02738SRajesh Shah struct acpi_bus_ops *ops) 11701da177e4SLinus Torvalds { 11711da177e4SLinus Torvalds acpi_status status = AE_OK; 11721da177e4SLinus Torvalds struct acpi_device *parent = NULL; 11731da177e4SLinus Torvalds struct acpi_device *child = NULL; 11741da177e4SLinus Torvalds acpi_handle phandle = NULL; 11751da177e4SLinus Torvalds acpi_handle chandle = NULL; 11761da177e4SLinus Torvalds acpi_object_type type = 0; 11771da177e4SLinus Torvalds u32 level = 1; 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_bus_scan"); 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds if (!start) 11821da177e4SLinus Torvalds return_VALUE(-EINVAL); 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds parent = start; 11851da177e4SLinus Torvalds phandle = start->handle; 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds /* 11881da177e4SLinus Torvalds * Parse through the ACPI namespace, identify all 'devices', and 11891da177e4SLinus Torvalds * create a new 'struct acpi_device' for each. 11901da177e4SLinus Torvalds */ 11911da177e4SLinus Torvalds while ((level > 0) && parent) { 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, 11941da177e4SLinus Torvalds chandle, &chandle); 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds /* 11971da177e4SLinus Torvalds * If this scope is exhausted then move our way back up. 11981da177e4SLinus Torvalds */ 11991da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 12001da177e4SLinus Torvalds level--; 12011da177e4SLinus Torvalds chandle = phandle; 12021da177e4SLinus Torvalds acpi_get_parent(phandle, &phandle); 12031da177e4SLinus Torvalds if (parent->parent) 12041da177e4SLinus Torvalds parent = parent->parent; 12051da177e4SLinus Torvalds continue; 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds status = acpi_get_type(chandle, &type); 12091da177e4SLinus Torvalds if (ACPI_FAILURE(status)) 12101da177e4SLinus Torvalds continue; 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds /* 12131da177e4SLinus Torvalds * If this is a scope object then parse it (depth-first). 12141da177e4SLinus Torvalds */ 12151da177e4SLinus Torvalds if (type == ACPI_TYPE_LOCAL_SCOPE) { 12161da177e4SLinus Torvalds level++; 12171da177e4SLinus Torvalds phandle = chandle; 12181da177e4SLinus Torvalds chandle = NULL; 12191da177e4SLinus Torvalds continue; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds /* 12231da177e4SLinus Torvalds * We're only interested in objects that we consider 'devices'. 12241da177e4SLinus Torvalds */ 12251da177e4SLinus Torvalds switch (type) { 12261da177e4SLinus Torvalds case ACPI_TYPE_DEVICE: 12271da177e4SLinus Torvalds type = ACPI_BUS_TYPE_DEVICE; 12281da177e4SLinus Torvalds break; 12291da177e4SLinus Torvalds case ACPI_TYPE_PROCESSOR: 12301da177e4SLinus Torvalds type = ACPI_BUS_TYPE_PROCESSOR; 12311da177e4SLinus Torvalds break; 12321da177e4SLinus Torvalds case ACPI_TYPE_THERMAL: 12331da177e4SLinus Torvalds type = ACPI_BUS_TYPE_THERMAL; 12341da177e4SLinus Torvalds break; 12351da177e4SLinus Torvalds case ACPI_TYPE_POWER: 12361da177e4SLinus Torvalds type = ACPI_BUS_TYPE_POWER; 12371da177e4SLinus Torvalds break; 12381da177e4SLinus Torvalds default: 12391da177e4SLinus Torvalds continue; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 1242*3fb02738SRajesh Shah if (ops->acpi_op_add) 1243*3fb02738SRajesh Shah status = acpi_add_single_object(&child, parent, 1244*3fb02738SRajesh Shah chandle, type); 1245*3fb02738SRajesh Shah else 1246*3fb02738SRajesh Shah status = acpi_bus_get_device(chandle, &child); 1247*3fb02738SRajesh Shah 12481da177e4SLinus Torvalds if (ACPI_FAILURE(status)) 12491da177e4SLinus Torvalds continue; 12501da177e4SLinus Torvalds 1251*3fb02738SRajesh Shah if (ops->acpi_op_start) { 1252*3fb02738SRajesh Shah status = acpi_start_single_object(child); 1253*3fb02738SRajesh Shah if (ACPI_FAILURE(status)) 1254*3fb02738SRajesh Shah continue; 1255*3fb02738SRajesh Shah } 1256*3fb02738SRajesh Shah 12571da177e4SLinus Torvalds /* 12581da177e4SLinus Torvalds * If the device is present, enabled, and functioning then 12591da177e4SLinus Torvalds * parse its scope (depth-first). Note that we need to 12601da177e4SLinus Torvalds * represent absent devices to facilitate PnP notifications 12611da177e4SLinus Torvalds * -- but only the subtree head (not all of its children, 12621da177e4SLinus Torvalds * which will be enumerated when the parent is inserted). 12631da177e4SLinus Torvalds * 12641da177e4SLinus Torvalds * TBD: Need notifications and other detection mechanisms 12651da177e4SLinus Torvalds * in place before we can fully implement this. 12661da177e4SLinus Torvalds */ 12671da177e4SLinus Torvalds if (child->status.present) { 12681da177e4SLinus Torvalds status = acpi_get_next_object(ACPI_TYPE_ANY, chandle, 12691da177e4SLinus Torvalds NULL, NULL); 12701da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) { 12711da177e4SLinus Torvalds level++; 12721da177e4SLinus Torvalds phandle = chandle; 12731da177e4SLinus Torvalds chandle = NULL; 12741da177e4SLinus Torvalds parent = child; 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds return_VALUE(0); 12801da177e4SLinus Torvalds } 12811da177e4SLinus Torvalds 1282*3fb02738SRajesh Shah int 1283*3fb02738SRajesh Shah acpi_bus_add ( 1284*3fb02738SRajesh Shah struct acpi_device **child, 1285*3fb02738SRajesh Shah struct acpi_device *parent, 1286*3fb02738SRajesh Shah acpi_handle handle, 1287*3fb02738SRajesh Shah int type) 1288*3fb02738SRajesh Shah { 1289*3fb02738SRajesh Shah int result; 1290*3fb02738SRajesh Shah struct acpi_bus_ops ops; 1291*3fb02738SRajesh Shah 1292*3fb02738SRajesh Shah ACPI_FUNCTION_TRACE("acpi_bus_add"); 1293*3fb02738SRajesh Shah 1294*3fb02738SRajesh Shah result = acpi_add_single_object(child, parent, handle, type); 1295*3fb02738SRajesh Shah if (!result) { 1296*3fb02738SRajesh Shah memset(&ops, 0, sizeof(ops)); 1297*3fb02738SRajesh Shah ops.acpi_op_add = 1; 1298*3fb02738SRajesh Shah result = acpi_bus_scan(*child, &ops); 1299*3fb02738SRajesh Shah } 1300*3fb02738SRajesh Shah return_VALUE(result); 1301*3fb02738SRajesh Shah } 1302*3fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_add); 1303*3fb02738SRajesh Shah 1304*3fb02738SRajesh Shah int 1305*3fb02738SRajesh Shah acpi_bus_start ( 1306*3fb02738SRajesh Shah struct acpi_device *device) 1307*3fb02738SRajesh Shah { 1308*3fb02738SRajesh Shah int result; 1309*3fb02738SRajesh Shah struct acpi_bus_ops ops; 1310*3fb02738SRajesh Shah 1311*3fb02738SRajesh Shah ACPI_FUNCTION_TRACE("acpi_bus_start"); 1312*3fb02738SRajesh Shah 1313*3fb02738SRajesh Shah if (!device) 1314*3fb02738SRajesh Shah return_VALUE(-EINVAL); 1315*3fb02738SRajesh Shah 1316*3fb02738SRajesh Shah result = acpi_start_single_object(device); 1317*3fb02738SRajesh Shah if (!result) { 1318*3fb02738SRajesh Shah memset(&ops, 0, sizeof(ops)); 1319*3fb02738SRajesh Shah ops.acpi_op_start = 1; 1320*3fb02738SRajesh Shah result = acpi_bus_scan(device, &ops); 1321*3fb02738SRajesh Shah } 1322*3fb02738SRajesh Shah return_VALUE(result); 1323*3fb02738SRajesh Shah } 1324*3fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_start); 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds static int 13271da177e4SLinus Torvalds acpi_bus_trim(struct acpi_device *start, 13281da177e4SLinus Torvalds int rmdevice) 13291da177e4SLinus Torvalds { 13301da177e4SLinus Torvalds acpi_status status; 13311da177e4SLinus Torvalds struct acpi_device *parent, *child; 13321da177e4SLinus Torvalds acpi_handle phandle, chandle; 13331da177e4SLinus Torvalds acpi_object_type type; 13341da177e4SLinus Torvalds u32 level = 1; 13351da177e4SLinus Torvalds int err = 0; 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds parent = start; 13381da177e4SLinus Torvalds phandle = start->handle; 13391da177e4SLinus Torvalds child = chandle = NULL; 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds while ((level > 0) && parent && (!err)) { 13421da177e4SLinus Torvalds status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, 13431da177e4SLinus Torvalds chandle, &chandle); 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds /* 13461da177e4SLinus Torvalds * If this scope is exhausted then move our way back up. 13471da177e4SLinus Torvalds */ 13481da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 13491da177e4SLinus Torvalds level--; 13501da177e4SLinus Torvalds chandle = phandle; 13511da177e4SLinus Torvalds acpi_get_parent(phandle, &phandle); 13521da177e4SLinus Torvalds child = parent; 13531da177e4SLinus Torvalds parent = parent->parent; 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds if (level == 0) 13561da177e4SLinus Torvalds err = acpi_bus_remove(child, rmdevice); 13571da177e4SLinus Torvalds else 13581da177e4SLinus Torvalds err = acpi_bus_remove(child, 1); 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds continue; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds status = acpi_get_type(chandle, &type); 13641da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 13651da177e4SLinus Torvalds continue; 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds /* 13681da177e4SLinus Torvalds * If there is a device corresponding to chandle then 13691da177e4SLinus Torvalds * parse it (depth-first). 13701da177e4SLinus Torvalds */ 13711da177e4SLinus Torvalds if (acpi_bus_get_device(chandle, &child) == 0) { 13721da177e4SLinus Torvalds level++; 13731da177e4SLinus Torvalds phandle = chandle; 13741da177e4SLinus Torvalds chandle = NULL; 13751da177e4SLinus Torvalds parent = child; 13761da177e4SLinus Torvalds } 13771da177e4SLinus Torvalds continue; 13781da177e4SLinus Torvalds } 13791da177e4SLinus Torvalds return err; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds static int 13831da177e4SLinus Torvalds acpi_bus_scan_fixed ( 13841da177e4SLinus Torvalds struct acpi_device *root) 13851da177e4SLinus Torvalds { 13861da177e4SLinus Torvalds int result = 0; 13871da177e4SLinus Torvalds struct acpi_device *device = NULL; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed"); 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds if (!root) 13921da177e4SLinus Torvalds return_VALUE(-ENODEV); 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds /* 13951da177e4SLinus Torvalds * Enumerate all fixed-feature devices. 13961da177e4SLinus Torvalds */ 1397*3fb02738SRajesh Shah if (acpi_fadt.pwr_button == 0) { 1398*3fb02738SRajesh Shah result = acpi_add_single_object(&device, acpi_root, 13991da177e4SLinus Torvalds NULL, ACPI_BUS_TYPE_POWER_BUTTON); 1400*3fb02738SRajesh Shah if (!result) 1401*3fb02738SRajesh Shah result = acpi_start_single_object(device); 1402*3fb02738SRajesh Shah } 14031da177e4SLinus Torvalds 1404*3fb02738SRajesh Shah if (acpi_fadt.sleep_button == 0) { 1405*3fb02738SRajesh Shah result = acpi_add_single_object(&device, acpi_root, 14061da177e4SLinus Torvalds NULL, ACPI_BUS_TYPE_SLEEP_BUTTON); 1407*3fb02738SRajesh Shah if (!result) 1408*3fb02738SRajesh Shah result = acpi_start_single_object(device); 1409*3fb02738SRajesh Shah } 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds return_VALUE(result); 14121da177e4SLinus Torvalds } 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds static int __init acpi_scan_init(void) 14161da177e4SLinus Torvalds { 14171da177e4SLinus Torvalds int result; 1418*3fb02738SRajesh Shah struct acpi_bus_ops ops; 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds ACPI_FUNCTION_TRACE("acpi_scan_init"); 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds if (acpi_disabled) 14231da177e4SLinus Torvalds return_VALUE(0); 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds kset_register(&acpi_namespace_kset); 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds /* 14281da177e4SLinus Torvalds * Create the root device in the bus's device tree 14291da177e4SLinus Torvalds */ 1430*3fb02738SRajesh Shah result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT, 14311da177e4SLinus Torvalds ACPI_BUS_TYPE_SYSTEM); 14321da177e4SLinus Torvalds if (result) 14331da177e4SLinus Torvalds goto Done; 14341da177e4SLinus Torvalds 1435*3fb02738SRajesh Shah result = acpi_start_single_object(acpi_root); 1436*3fb02738SRajesh Shah 14371da177e4SLinus Torvalds /* 14381da177e4SLinus Torvalds * Enumerate devices in the ACPI namespace. 14391da177e4SLinus Torvalds */ 14401da177e4SLinus Torvalds result = acpi_bus_scan_fixed(acpi_root); 1441*3fb02738SRajesh Shah if (!result) { 1442*3fb02738SRajesh Shah memset(&ops, 0, sizeof(ops)); 1443*3fb02738SRajesh Shah ops.acpi_op_add = 1; 1444*3fb02738SRajesh Shah ops.acpi_op_start = 1; 1445*3fb02738SRajesh Shah result = acpi_bus_scan(acpi_root, &ops); 1446*3fb02738SRajesh Shah } 14471da177e4SLinus Torvalds 14481da177e4SLinus Torvalds if (result) 14491da177e4SLinus Torvalds acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL); 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds Done: 14521da177e4SLinus Torvalds return_VALUE(result); 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds 14551da177e4SLinus Torvalds subsys_initcall(acpi_scan_init); 1456