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> 75a0e3ad6STejun Heo #include <linux/slab.h> 89b6d97b6SRandy Dunlap #include <linux/kernel.h> 91da177e4SLinus Torvalds #include <linux/acpi.h> 1074523c90SAlok N Kataria #include <linux/signal.h> 1174523c90SAlok N Kataria #include <linux/kthread.h> 12222e82acSDarrick J. Wong #include <linux/dmi.h> 13d1efe3c3SLance Ortiz #include <linux/nls.h> 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #include <acpi/acpi_drivers.h> 161da177e4SLinus Torvalds 17e60cc7a6SBjorn Helgaas #include "internal.h" 18e60cc7a6SBjorn Helgaas 191da177e4SLinus Torvalds #define _COMPONENT ACPI_BUS_COMPONENT 20f52fd66dSLen Brown ACPI_MODULE_NAME("scan"); 211da177e4SLinus Torvalds #define STRUCT_TO_INT(s) (*((int*)&s)) 221da177e4SLinus Torvalds extern struct acpi_device *acpi_root; 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #define ACPI_BUS_CLASS "system_bus" 2529b71a1cSThomas Renninger #define ACPI_BUS_HID "LNXSYBUS" 261da177e4SLinus Torvalds #define ACPI_BUS_DEVICE_NAME "System Bus" 271da177e4SLinus Torvalds 28859ac9a4SBjorn Helgaas #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent) 29859ac9a4SBjorn Helgaas 30620e112cSThomas Renninger static const char *dummy_hid = "device"; 312b2ae7c7SThomas Renninger 3291e56878SMika Westerberg /* 3391e56878SMika Westerberg * The following ACPI IDs are known to be suitable for representing as 3491e56878SMika Westerberg * platform devices. 3591e56878SMika Westerberg */ 3691e56878SMika Westerberg static const struct acpi_device_id acpi_platform_device_ids[] = { 3791e56878SMika Westerberg 38142b007bSAdrian Hunter { "PNP0D40" }, 39142b007bSAdrian Hunter 405e7779f0SMika Westerberg /* Haswell LPSS devices */ 415e7779f0SMika Westerberg { "INT33C0", 0 }, 425e7779f0SMika Westerberg { "INT33C1", 0 }, 435e7779f0SMika Westerberg { "INT33C2", 0 }, 445e7779f0SMika Westerberg { "INT33C3", 0 }, 455e7779f0SMika Westerberg { "INT33C4", 0 }, 465e7779f0SMika Westerberg { "INT33C5", 0 }, 475e7779f0SMika Westerberg { "INT33C6", 0 }, 485e7779f0SMika Westerberg { "INT33C7", 0 }, 495e7779f0SMika Westerberg 5091e56878SMika Westerberg { } 5191e56878SMika Westerberg }; 5291e56878SMika Westerberg 531da177e4SLinus Torvalds static LIST_HEAD(acpi_device_list); 54e49bd2ddSZhang Rui static LIST_HEAD(acpi_bus_id_list); 559090589dSShaohua Li DEFINE_MUTEX(acpi_device_lock); 561da177e4SLinus Torvalds LIST_HEAD(acpi_wakeup_device_list); 571da177e4SLinus Torvalds 58e49bd2ddSZhang Rui struct acpi_device_bus_id{ 59bb095854SZhang Rui char bus_id[15]; 60e49bd2ddSZhang Rui unsigned int instance_no; 61e49bd2ddSZhang Rui struct list_head node; 62e49bd2ddSZhang Rui }; 6329b71a1cSThomas Renninger 6429b71a1cSThomas Renninger /* 6529b71a1cSThomas Renninger * Creates hid/cid(s) string needed for modalias and uevent 6629b71a1cSThomas Renninger * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get: 6729b71a1cSThomas Renninger * char *modalias: "acpi:IBM0001:ACPI0001" 6829b71a1cSThomas Renninger */ 69b3e572d2SAdrian Bunk static int create_modalias(struct acpi_device *acpi_dev, char *modalias, 70b3e572d2SAdrian Bunk int size) 71b3e572d2SAdrian Bunk { 7229b71a1cSThomas Renninger int len; 735c9fcb5dSZhang Rui int count; 747f47fa6cSBjorn Helgaas struct acpi_hardware_id *id; 7529b71a1cSThomas Renninger 762b2ae7c7SThomas Renninger if (list_empty(&acpi_dev->pnp.ids)) 772b2ae7c7SThomas Renninger return 0; 782b2ae7c7SThomas Renninger 795c9fcb5dSZhang Rui len = snprintf(modalias, size, "acpi:"); 8029b71a1cSThomas Renninger size -= len; 8129b71a1cSThomas Renninger 827f47fa6cSBjorn Helgaas list_for_each_entry(id, &acpi_dev->pnp.ids, list) { 837f47fa6cSBjorn Helgaas count = snprintf(&modalias[len], size, "%s:", id->id); 845c9fcb5dSZhang Rui if (count < 0 || count >= size) 855c9fcb5dSZhang Rui return -EINVAL; 865c9fcb5dSZhang Rui len += count; 875c9fcb5dSZhang Rui size -= count; 885c9fcb5dSZhang Rui } 895c9fcb5dSZhang Rui 9029b71a1cSThomas Renninger modalias[len] = '\0'; 9129b71a1cSThomas Renninger return len; 9229b71a1cSThomas Renninger } 9329b71a1cSThomas Renninger 9429b71a1cSThomas Renninger static ssize_t 9529b71a1cSThomas Renninger acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) { 9629b71a1cSThomas Renninger struct acpi_device *acpi_dev = to_acpi_device(dev); 9729b71a1cSThomas Renninger int len; 9829b71a1cSThomas Renninger 9929b71a1cSThomas Renninger /* Device has no HID and no CID or string is >1024 */ 10029b71a1cSThomas Renninger len = create_modalias(acpi_dev, buf, 1024); 10129b71a1cSThomas Renninger if (len <= 0) 10229b71a1cSThomas Renninger return 0; 10329b71a1cSThomas Renninger buf[len++] = '\n'; 10429b71a1cSThomas Renninger return len; 10529b71a1cSThomas Renninger } 10629b71a1cSThomas Renninger static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); 10729b71a1cSThomas Renninger 108c4753e57SToshi Kani /** 109c4753e57SToshi Kani * acpi_bus_hot_remove_device: hot-remove a device and its children 110c4753e57SToshi Kani * @context: struct acpi_eject_event pointer (freed in this func) 111c4753e57SToshi Kani * 112c4753e57SToshi Kani * Hot-remove a device and its children. This function frees up the 113c4753e57SToshi Kani * memory space passed by arg context, so that the caller may call 114c4753e57SToshi Kani * this function asynchronously through acpi_os_hotplug_execute(). 115c4753e57SToshi Kani */ 116c4753e57SToshi Kani void acpi_bus_hot_remove_device(void *context) 1171da177e4SLinus Torvalds { 118c4753e57SToshi Kani struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context; 1195993c467SYinghai Lu struct acpi_device *device = ej_event->device; 1205993c467SYinghai Lu acpi_handle handle = device->handle; 121b3c450c3SToshi Kani acpi_handle temp; 1221da177e4SLinus Torvalds struct acpi_object_list arg_list; 1231da177e4SLinus Torvalds union acpi_object arg; 1241da177e4SLinus Torvalds acpi_status status = AE_OK; 125c4753e57SToshi Kani u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */ 1261da177e4SLinus Torvalds 12726d46867SZhang Rui ACPI_DEBUG_PRINT((ACPI_DB_INFO, 1280794469dSKay Sievers "Hot-removing device %s...\n", dev_name(&device->dev))); 12926d46867SZhang Rui 130ae281795SRafael J. Wysocki if (acpi_bus_trim(device)) { 13155ac9a01SLin Ming printk(KERN_ERR PREFIX 13255ac9a01SLin Ming "Removing device failed\n"); 133c4753e57SToshi Kani goto err_out; 13426d46867SZhang Rui } 13526d46867SZhang Rui 136b3c450c3SToshi Kani /* device has been freed */ 137b3c450c3SToshi Kani device = NULL; 138b3c450c3SToshi Kani 13926d46867SZhang Rui /* power off device */ 14026d46867SZhang Rui status = acpi_evaluate_object(handle, "_PS3", NULL, NULL); 14126d46867SZhang Rui if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) 14255ac9a01SLin Ming printk(KERN_WARNING PREFIX 14355ac9a01SLin Ming "Power-off device failed\n"); 14426d46867SZhang Rui 145b3c450c3SToshi Kani if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) { 1461da177e4SLinus Torvalds arg_list.count = 1; 1471da177e4SLinus Torvalds arg_list.pointer = &arg; 1481da177e4SLinus Torvalds arg.type = ACPI_TYPE_INTEGER; 1491da177e4SLinus Torvalds arg.integer.value = 0; 1501da177e4SLinus Torvalds acpi_evaluate_object(handle, "_LCK", &arg_list, NULL); 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds arg_list.count = 1; 1541da177e4SLinus Torvalds arg_list.pointer = &arg; 1551da177e4SLinus Torvalds arg.type = ACPI_TYPE_INTEGER; 1561da177e4SLinus Torvalds arg.integer.value = 1; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* 1591da177e4SLinus Torvalds * TBD: _EJD support. 1601da177e4SLinus Torvalds */ 1611da177e4SLinus Torvalds status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL); 162c4753e57SToshi Kani if (ACPI_FAILURE(status)) { 163c4753e57SToshi Kani if (status != AE_NOT_FOUND) 164c8d72a5eSZhang Rui printk(KERN_WARNING PREFIX 165c8d72a5eSZhang Rui "Eject device failed\n"); 166c4753e57SToshi Kani goto err_out; 167c4753e57SToshi Kani } 1681da177e4SLinus Torvalds 169c4753e57SToshi Kani kfree(context); 170c4753e57SToshi Kani return; 171c4753e57SToshi Kani 172c4753e57SToshi Kani err_out: 173c4753e57SToshi Kani /* Inform firmware the hot-remove operation has completed w/ error */ 174c4753e57SToshi Kani (void) acpi_evaluate_hotplug_ost(handle, 175c4753e57SToshi Kani ej_event->event, ost_code, NULL); 176c4753e57SToshi Kani kfree(context); 177c8d72a5eSZhang Rui return; 1781da177e4SLinus Torvalds } 17961622accSToshi Kani EXPORT_SYMBOL(acpi_bus_hot_remove_device); 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds static ssize_t 182f883d9dbSPatrick Mochel acpi_eject_store(struct device *d, struct device_attribute *attr, 183f883d9dbSPatrick Mochel const char *buf, size_t count) 1841da177e4SLinus Torvalds { 1851da177e4SLinus Torvalds int ret = count; 1861da177e4SLinus Torvalds acpi_status status; 1871da177e4SLinus Torvalds acpi_object_type type = 0; 188f883d9dbSPatrick Mochel struct acpi_device *acpi_device = to_acpi_device(d); 189c4753e57SToshi Kani struct acpi_eject_event *ej_event; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds if ((!count) || (buf[0] != '1')) { 1921da177e4SLinus Torvalds return -EINVAL; 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds #ifndef FORCE_EJECT 195f883d9dbSPatrick Mochel if (acpi_device->driver == NULL) { 1961da177e4SLinus Torvalds ret = -ENODEV; 1971da177e4SLinus Torvalds goto err; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds #endif 200f883d9dbSPatrick Mochel status = acpi_get_type(acpi_device->handle, &type); 201f883d9dbSPatrick Mochel if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) { 2021da177e4SLinus Torvalds ret = -ENODEV; 2031da177e4SLinus Torvalds goto err; 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 206c4753e57SToshi Kani ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL); 207c4753e57SToshi Kani if (!ej_event) { 208c4753e57SToshi Kani ret = -ENOMEM; 209c4753e57SToshi Kani goto err; 210c4753e57SToshi Kani } 211c4753e57SToshi Kani 2125993c467SYinghai Lu ej_event->device = acpi_device; 213c4753e57SToshi Kani if (acpi_device->flags.eject_pending) { 214c4753e57SToshi Kani /* event originated from ACPI eject notification */ 215c4753e57SToshi Kani ej_event->event = ACPI_NOTIFY_EJECT_REQUEST; 216c4753e57SToshi Kani acpi_device->flags.eject_pending = 0; 217c4753e57SToshi Kani } else { 218c4753e57SToshi Kani /* event originated from user */ 219c4753e57SToshi Kani ej_event->event = ACPI_OST_EC_OSPM_EJECT; 2205993c467SYinghai Lu (void) acpi_evaluate_hotplug_ost(acpi_device->handle, 221c4753e57SToshi Kani ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); 222c4753e57SToshi Kani } 223c4753e57SToshi Kani 224c4753e57SToshi Kani acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event); 2251da177e4SLinus Torvalds err: 2261da177e4SLinus Torvalds return ret; 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 229f883d9dbSPatrick Mochel static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store); 230f883d9dbSPatrick Mochel 231e49bd2ddSZhang Rui static ssize_t 232e49bd2ddSZhang Rui acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) { 233e49bd2ddSZhang Rui struct acpi_device *acpi_dev = to_acpi_device(dev); 234e49bd2ddSZhang Rui 235ea8d82fdSBjorn Helgaas return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev)); 236e49bd2ddSZhang Rui } 237e49bd2ddSZhang Rui static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL); 238e49bd2ddSZhang Rui 239923d4a45SLv Zheng static ssize_t acpi_device_uid_show(struct device *dev, 240923d4a45SLv Zheng struct device_attribute *attr, char *buf) 241923d4a45SLv Zheng { 242923d4a45SLv Zheng struct acpi_device *acpi_dev = to_acpi_device(dev); 243923d4a45SLv Zheng 244923d4a45SLv Zheng return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id); 245923d4a45SLv Zheng } 246923d4a45SLv Zheng static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL); 247923d4a45SLv Zheng 248923d4a45SLv Zheng static ssize_t acpi_device_adr_show(struct device *dev, 249923d4a45SLv Zheng struct device_attribute *attr, char *buf) 250923d4a45SLv Zheng { 251923d4a45SLv Zheng struct acpi_device *acpi_dev = to_acpi_device(dev); 252923d4a45SLv Zheng 253923d4a45SLv Zheng return sprintf(buf, "0x%08x\n", 254923d4a45SLv Zheng (unsigned int)(acpi_dev->pnp.bus_address)); 255923d4a45SLv Zheng } 256923d4a45SLv Zheng static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL); 257923d4a45SLv Zheng 258e49bd2ddSZhang Rui static ssize_t 259e49bd2ddSZhang Rui acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) { 260e49bd2ddSZhang Rui struct acpi_device *acpi_dev = to_acpi_device(dev); 261e49bd2ddSZhang Rui struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL}; 262e49bd2ddSZhang Rui int result; 263e49bd2ddSZhang Rui 264e49bd2ddSZhang Rui result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path); 265e49bd2ddSZhang Rui if (result) 266e49bd2ddSZhang Rui goto end; 267e49bd2ddSZhang Rui 268e49bd2ddSZhang Rui result = sprintf(buf, "%s\n", (char*)path.pointer); 269e49bd2ddSZhang Rui kfree(path.pointer); 270e49bd2ddSZhang Rui end: 271e49bd2ddSZhang Rui return result; 272e49bd2ddSZhang Rui } 273e49bd2ddSZhang Rui static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL); 274e49bd2ddSZhang Rui 275d1efe3c3SLance Ortiz /* sysfs file that shows description text from the ACPI _STR method */ 276d1efe3c3SLance Ortiz static ssize_t description_show(struct device *dev, 277d1efe3c3SLance Ortiz struct device_attribute *attr, 278d1efe3c3SLance Ortiz char *buf) { 279d1efe3c3SLance Ortiz struct acpi_device *acpi_dev = to_acpi_device(dev); 280d1efe3c3SLance Ortiz int result; 281d1efe3c3SLance Ortiz 282d1efe3c3SLance Ortiz if (acpi_dev->pnp.str_obj == NULL) 283d1efe3c3SLance Ortiz return 0; 284d1efe3c3SLance Ortiz 285d1efe3c3SLance Ortiz /* 286d1efe3c3SLance Ortiz * The _STR object contains a Unicode identifier for a device. 287d1efe3c3SLance Ortiz * We need to convert to utf-8 so it can be displayed. 288d1efe3c3SLance Ortiz */ 289d1efe3c3SLance Ortiz result = utf16s_to_utf8s( 290d1efe3c3SLance Ortiz (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer, 291d1efe3c3SLance Ortiz acpi_dev->pnp.str_obj->buffer.length, 292d1efe3c3SLance Ortiz UTF16_LITTLE_ENDIAN, buf, 293d1efe3c3SLance Ortiz PAGE_SIZE); 294d1efe3c3SLance Ortiz 295d1efe3c3SLance Ortiz buf[result++] = '\n'; 296d1efe3c3SLance Ortiz 297d1efe3c3SLance Ortiz return result; 298d1efe3c3SLance Ortiz } 299d1efe3c3SLance Ortiz static DEVICE_ATTR(description, 0444, description_show, NULL); 300d1efe3c3SLance Ortiz 301bb74ac23SYasuaki Ishimatsu static ssize_t 302bb74ac23SYasuaki Ishimatsu acpi_device_sun_show(struct device *dev, struct device_attribute *attr, 303bb74ac23SYasuaki Ishimatsu char *buf) { 304bb74ac23SYasuaki Ishimatsu struct acpi_device *acpi_dev = to_acpi_device(dev); 305bb74ac23SYasuaki Ishimatsu 306bb74ac23SYasuaki Ishimatsu return sprintf(buf, "%lu\n", acpi_dev->pnp.sun); 307bb74ac23SYasuaki Ishimatsu } 308bb74ac23SYasuaki Ishimatsu static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL); 309bb74ac23SYasuaki Ishimatsu 310e49bd2ddSZhang Rui static int acpi_device_setup_files(struct acpi_device *dev) 311f883d9dbSPatrick Mochel { 312d1efe3c3SLance Ortiz struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 313f883d9dbSPatrick Mochel acpi_status status; 314f883d9dbSPatrick Mochel acpi_handle temp; 315bb74ac23SYasuaki Ishimatsu unsigned long long sun; 316e49bd2ddSZhang Rui int result = 0; 317e49bd2ddSZhang Rui 318e49bd2ddSZhang Rui /* 319e49bd2ddSZhang Rui * Devices gotten from FADT don't have a "path" attribute 320e49bd2ddSZhang Rui */ 321e49bd2ddSZhang Rui if (dev->handle) { 322e49bd2ddSZhang Rui result = device_create_file(&dev->dev, &dev_attr_path); 323e49bd2ddSZhang Rui if (result) 324e49bd2ddSZhang Rui goto end; 325e49bd2ddSZhang Rui } 326e49bd2ddSZhang Rui 3272b2ae7c7SThomas Renninger if (!list_empty(&dev->pnp.ids)) { 328e49bd2ddSZhang Rui result = device_create_file(&dev->dev, &dev_attr_hid); 329e49bd2ddSZhang Rui if (result) 330e49bd2ddSZhang Rui goto end; 331f883d9dbSPatrick Mochel 33229b71a1cSThomas Renninger result = device_create_file(&dev->dev, &dev_attr_modalias); 33329b71a1cSThomas Renninger if (result) 33429b71a1cSThomas Renninger goto end; 3352b2ae7c7SThomas Renninger } 33629b71a1cSThomas Renninger 337f883d9dbSPatrick Mochel /* 338d1efe3c3SLance Ortiz * If device has _STR, 'description' file is created 339d1efe3c3SLance Ortiz */ 340d1efe3c3SLance Ortiz status = acpi_get_handle(dev->handle, "_STR", &temp); 341d1efe3c3SLance Ortiz if (ACPI_SUCCESS(status)) { 342d1efe3c3SLance Ortiz status = acpi_evaluate_object(dev->handle, "_STR", 343d1efe3c3SLance Ortiz NULL, &buffer); 344d1efe3c3SLance Ortiz if (ACPI_FAILURE(status)) 345d1efe3c3SLance Ortiz buffer.pointer = NULL; 346d1efe3c3SLance Ortiz dev->pnp.str_obj = buffer.pointer; 347d1efe3c3SLance Ortiz result = device_create_file(&dev->dev, &dev_attr_description); 348d1efe3c3SLance Ortiz if (result) 349d1efe3c3SLance Ortiz goto end; 350d1efe3c3SLance Ortiz } 351d1efe3c3SLance Ortiz 352923d4a45SLv Zheng if (dev->flags.bus_address) 353923d4a45SLv Zheng result = device_create_file(&dev->dev, &dev_attr_adr); 354923d4a45SLv Zheng if (dev->pnp.unique_id) 355923d4a45SLv Zheng result = device_create_file(&dev->dev, &dev_attr_uid); 356923d4a45SLv Zheng 357bb74ac23SYasuaki Ishimatsu status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun); 358bb74ac23SYasuaki Ishimatsu if (ACPI_SUCCESS(status)) { 359bb74ac23SYasuaki Ishimatsu dev->pnp.sun = (unsigned long)sun; 360bb74ac23SYasuaki Ishimatsu result = device_create_file(&dev->dev, &dev_attr_sun); 361bb74ac23SYasuaki Ishimatsu if (result) 362bb74ac23SYasuaki Ishimatsu goto end; 363bb74ac23SYasuaki Ishimatsu } else { 364bb74ac23SYasuaki Ishimatsu dev->pnp.sun = (unsigned long)-1; 365bb74ac23SYasuaki Ishimatsu } 366bb74ac23SYasuaki Ishimatsu 367d1efe3c3SLance Ortiz /* 368f883d9dbSPatrick Mochel * If device has _EJ0, 'eject' file is created that is used to trigger 369f883d9dbSPatrick Mochel * hot-removal function from userland. 370f883d9dbSPatrick Mochel */ 371f883d9dbSPatrick Mochel status = acpi_get_handle(dev->handle, "_EJ0", &temp); 372f883d9dbSPatrick Mochel if (ACPI_SUCCESS(status)) 373e49bd2ddSZhang Rui result = device_create_file(&dev->dev, &dev_attr_eject); 374e49bd2ddSZhang Rui end: 375e49bd2ddSZhang Rui return result; 376f883d9dbSPatrick Mochel } 377f883d9dbSPatrick Mochel 378f883d9dbSPatrick Mochel static void acpi_device_remove_files(struct acpi_device *dev) 379f883d9dbSPatrick Mochel { 380f883d9dbSPatrick Mochel acpi_status status; 381f883d9dbSPatrick Mochel acpi_handle temp; 382f883d9dbSPatrick Mochel 383f883d9dbSPatrick Mochel /* 384d1efe3c3SLance Ortiz * If device has _STR, remove 'description' file 385d1efe3c3SLance Ortiz */ 386d1efe3c3SLance Ortiz status = acpi_get_handle(dev->handle, "_STR", &temp); 387d1efe3c3SLance Ortiz if (ACPI_SUCCESS(status)) { 388d1efe3c3SLance Ortiz kfree(dev->pnp.str_obj); 389d1efe3c3SLance Ortiz device_remove_file(&dev->dev, &dev_attr_description); 390d1efe3c3SLance Ortiz } 391d1efe3c3SLance Ortiz /* 392d1efe3c3SLance Ortiz * If device has _EJ0, remove 'eject' file. 393f883d9dbSPatrick Mochel */ 394f883d9dbSPatrick Mochel status = acpi_get_handle(dev->handle, "_EJ0", &temp); 395f883d9dbSPatrick Mochel if (ACPI_SUCCESS(status)) 396f883d9dbSPatrick Mochel device_remove_file(&dev->dev, &dev_attr_eject); 397e49bd2ddSZhang Rui 398bb74ac23SYasuaki Ishimatsu status = acpi_get_handle(dev->handle, "_SUN", &temp); 399bb74ac23SYasuaki Ishimatsu if (ACPI_SUCCESS(status)) 400bb74ac23SYasuaki Ishimatsu device_remove_file(&dev->dev, &dev_attr_sun); 401bb74ac23SYasuaki Ishimatsu 402923d4a45SLv Zheng if (dev->pnp.unique_id) 403923d4a45SLv Zheng device_remove_file(&dev->dev, &dev_attr_uid); 404923d4a45SLv Zheng if (dev->flags.bus_address) 405923d4a45SLv Zheng device_remove_file(&dev->dev, &dev_attr_adr); 40629b71a1cSThomas Renninger device_remove_file(&dev->dev, &dev_attr_modalias); 407e49bd2ddSZhang Rui device_remove_file(&dev->dev, &dev_attr_hid); 408e49bd2ddSZhang Rui if (dev->handle) 409e49bd2ddSZhang Rui device_remove_file(&dev->dev, &dev_attr_path); 410f883d9dbSPatrick Mochel } 4111da177e4SLinus Torvalds /* -------------------------------------------------------------------------- 4129e89dde2SZhang Rui ACPI Bus operations 4131da177e4SLinus Torvalds -------------------------------------------------------------------------- */ 41429b71a1cSThomas Renninger 415cf761af9SMika Westerberg static const struct acpi_device_id *__acpi_match_device( 416cf761af9SMika Westerberg struct acpi_device *device, const struct acpi_device_id *ids) 41729b71a1cSThomas Renninger { 41829b71a1cSThomas Renninger const struct acpi_device_id *id; 4197f47fa6cSBjorn Helgaas struct acpi_hardware_id *hwid; 42029b71a1cSThomas Renninger 42139a0ad87SZhao Yakui /* 42239a0ad87SZhao Yakui * If the device is not present, it is unnecessary to load device 42339a0ad87SZhao Yakui * driver for it. 42439a0ad87SZhao Yakui */ 42539a0ad87SZhao Yakui if (!device->status.present) 426cf761af9SMika Westerberg return NULL; 42739a0ad87SZhao Yakui 4287f47fa6cSBjorn Helgaas for (id = ids; id->id[0]; id++) 4297f47fa6cSBjorn Helgaas list_for_each_entry(hwid, &device->pnp.ids, list) 4307f47fa6cSBjorn Helgaas if (!strcmp((char *) id->id, hwid->id)) 431cf761af9SMika Westerberg return id; 43229b71a1cSThomas Renninger 433cf761af9SMika Westerberg return NULL; 434cf761af9SMika Westerberg } 435cf761af9SMika Westerberg 436cf761af9SMika Westerberg /** 437cf761af9SMika Westerberg * acpi_match_device - Match a struct device against a given list of ACPI IDs 438cf761af9SMika Westerberg * @ids: Array of struct acpi_device_id object to match against. 439cf761af9SMika Westerberg * @dev: The device structure to match. 440cf761af9SMika Westerberg * 441cf761af9SMika Westerberg * Check if @dev has a valid ACPI handle and if there is a struct acpi_device 442cf761af9SMika Westerberg * object for that handle and use that object to match against a given list of 443cf761af9SMika Westerberg * device IDs. 444cf761af9SMika Westerberg * 445cf761af9SMika Westerberg * Return a pointer to the first matching ID on success or %NULL on failure. 446cf761af9SMika Westerberg */ 447cf761af9SMika Westerberg const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids, 448cf761af9SMika Westerberg const struct device *dev) 449cf761af9SMika Westerberg { 450cf761af9SMika Westerberg struct acpi_device *adev; 451cf761af9SMika Westerberg 45295f8a082SRafael J. Wysocki if (!ids || !ACPI_HANDLE(dev) 45395f8a082SRafael J. Wysocki || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev))) 454cf761af9SMika Westerberg return NULL; 455cf761af9SMika Westerberg 456cf761af9SMika Westerberg return __acpi_match_device(adev, ids); 457cf761af9SMika Westerberg } 458cf761af9SMika Westerberg EXPORT_SYMBOL_GPL(acpi_match_device); 459cf761af9SMika Westerberg 460cf761af9SMika Westerberg int acpi_match_device_ids(struct acpi_device *device, 461cf761af9SMika Westerberg const struct acpi_device_id *ids) 462cf761af9SMika Westerberg { 463cf761af9SMika Westerberg return __acpi_match_device(device, ids) ? 0 : -ENOENT; 46429b71a1cSThomas Renninger } 46529b71a1cSThomas Renninger EXPORT_SYMBOL(acpi_match_device_ids); 46629b71a1cSThomas Renninger 4677f47fa6cSBjorn Helgaas static void acpi_free_ids(struct acpi_device *device) 4687f47fa6cSBjorn Helgaas { 4697f47fa6cSBjorn Helgaas struct acpi_hardware_id *id, *tmp; 4707f47fa6cSBjorn Helgaas 4717f47fa6cSBjorn Helgaas list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) { 4727f47fa6cSBjorn Helgaas kfree(id->id); 4737f47fa6cSBjorn Helgaas kfree(id); 4747f47fa6cSBjorn Helgaas } 4757f47fa6cSBjorn Helgaas } 4767f47fa6cSBjorn Helgaas 4771890a97aSPatrick Mochel static void acpi_device_release(struct device *dev) 4781890a97aSPatrick Mochel { 4791890a97aSPatrick Mochel struct acpi_device *acpi_dev = to_acpi_device(dev); 4801890a97aSPatrick Mochel 4817f47fa6cSBjorn Helgaas acpi_free_ids(acpi_dev); 482ccf78040SLv Zheng kfree(acpi_dev->pnp.unique_id); 4831890a97aSPatrick Mochel kfree(acpi_dev); 4841890a97aSPatrick Mochel } 4851890a97aSPatrick Mochel 4865d9464a4SPatrick Mochel static int acpi_bus_match(struct device *dev, struct device_driver *drv) 4871da177e4SLinus Torvalds { 4885d9464a4SPatrick Mochel struct acpi_device *acpi_dev = to_acpi_device(dev); 4895d9464a4SPatrick Mochel struct acpi_driver *acpi_drv = to_acpi_driver(drv); 4905d9464a4SPatrick Mochel 491209d3b17SRafael J. Wysocki return acpi_dev->flags.match_driver 492805d410fSRafael J. Wysocki && !acpi_match_device_ids(acpi_dev, acpi_drv->ids); 4935d9464a4SPatrick Mochel } 4945d9464a4SPatrick Mochel 4957eff2e7aSKay Sievers static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env) 4965d9464a4SPatrick Mochel { 4975d9464a4SPatrick Mochel struct acpi_device *acpi_dev = to_acpi_device(dev); 4987eff2e7aSKay Sievers int len; 4995d9464a4SPatrick Mochel 5002b2ae7c7SThomas Renninger if (list_empty(&acpi_dev->pnp.ids)) 5012b2ae7c7SThomas Renninger return 0; 5022b2ae7c7SThomas Renninger 5037eff2e7aSKay Sievers if (add_uevent_var(env, "MODALIAS=")) 5047eff2e7aSKay Sievers return -ENOMEM; 5057eff2e7aSKay Sievers len = create_modalias(acpi_dev, &env->buf[env->buflen - 1], 5067eff2e7aSKay Sievers sizeof(env->buf) - env->buflen); 5077eff2e7aSKay Sievers if (len >= (sizeof(env->buf) - env->buflen)) 5087eff2e7aSKay Sievers return -ENOMEM; 5097eff2e7aSKay Sievers env->buflen += len; 5101da177e4SLinus Torvalds return 0; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 51346ec8598SBjorn Helgaas static void acpi_device_notify(acpi_handle handle, u32 event, void *data) 51446ec8598SBjorn Helgaas { 51546ec8598SBjorn Helgaas struct acpi_device *device = data; 51646ec8598SBjorn Helgaas 51746ec8598SBjorn Helgaas device->driver->ops.notify(device, event); 51846ec8598SBjorn Helgaas } 51946ec8598SBjorn Helgaas 52046ec8598SBjorn Helgaas static acpi_status acpi_device_notify_fixed(void *data) 52146ec8598SBjorn Helgaas { 52246ec8598SBjorn Helgaas struct acpi_device *device = data; 52346ec8598SBjorn Helgaas 52453de5356SBjorn Helgaas /* Fixed hardware devices have no handles */ 52553de5356SBjorn Helgaas acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device); 52646ec8598SBjorn Helgaas return AE_OK; 52746ec8598SBjorn Helgaas } 52846ec8598SBjorn Helgaas 52946ec8598SBjorn Helgaas static int acpi_device_install_notify_handler(struct acpi_device *device) 53046ec8598SBjorn Helgaas { 53146ec8598SBjorn Helgaas acpi_status status; 53246ec8598SBjorn Helgaas 533ccba2a36SBjorn Helgaas if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON) 53446ec8598SBjorn Helgaas status = 53546ec8598SBjorn Helgaas acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, 53646ec8598SBjorn Helgaas acpi_device_notify_fixed, 53746ec8598SBjorn Helgaas device); 538ccba2a36SBjorn Helgaas else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) 53946ec8598SBjorn Helgaas status = 54046ec8598SBjorn Helgaas acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, 54146ec8598SBjorn Helgaas acpi_device_notify_fixed, 54246ec8598SBjorn Helgaas device); 54346ec8598SBjorn Helgaas else 54446ec8598SBjorn Helgaas status = acpi_install_notify_handler(device->handle, 54546ec8598SBjorn Helgaas ACPI_DEVICE_NOTIFY, 54646ec8598SBjorn Helgaas acpi_device_notify, 54746ec8598SBjorn Helgaas device); 54846ec8598SBjorn Helgaas 54946ec8598SBjorn Helgaas if (ACPI_FAILURE(status)) 55046ec8598SBjorn Helgaas return -EINVAL; 55146ec8598SBjorn Helgaas return 0; 55246ec8598SBjorn Helgaas } 55346ec8598SBjorn Helgaas 55446ec8598SBjorn Helgaas static void acpi_device_remove_notify_handler(struct acpi_device *device) 55546ec8598SBjorn Helgaas { 556ccba2a36SBjorn Helgaas if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON) 55746ec8598SBjorn Helgaas acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, 55846ec8598SBjorn Helgaas acpi_device_notify_fixed); 559ccba2a36SBjorn Helgaas else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) 56046ec8598SBjorn Helgaas acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, 56146ec8598SBjorn Helgaas acpi_device_notify_fixed); 56246ec8598SBjorn Helgaas else 56346ec8598SBjorn Helgaas acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY, 56446ec8598SBjorn Helgaas acpi_device_notify); 56546ec8598SBjorn Helgaas } 56646ec8598SBjorn Helgaas 5675d9464a4SPatrick Mochel static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *); 5685d9464a4SPatrick Mochel static int acpi_device_probe(struct device * dev) 5695d9464a4SPatrick Mochel { 5705d9464a4SPatrick Mochel struct acpi_device *acpi_dev = to_acpi_device(dev); 5715d9464a4SPatrick Mochel struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver); 5725d9464a4SPatrick Mochel int ret; 5735d9464a4SPatrick Mochel 5745d9464a4SPatrick Mochel ret = acpi_bus_driver_init(acpi_dev, acpi_drv); 5755d9464a4SPatrick Mochel if (!ret) { 57646ec8598SBjorn Helgaas if (acpi_drv->ops.notify) { 57746ec8598SBjorn Helgaas ret = acpi_device_install_notify_handler(acpi_dev); 57846ec8598SBjorn Helgaas if (ret) { 57946ec8598SBjorn Helgaas if (acpi_drv->ops.remove) 58046ec8598SBjorn Helgaas acpi_drv->ops.remove(acpi_dev, 58146ec8598SBjorn Helgaas acpi_dev->removal_type); 58246ec8598SBjorn Helgaas return ret; 58346ec8598SBjorn Helgaas } 58446ec8598SBjorn Helgaas } 58546ec8598SBjorn Helgaas 5865d9464a4SPatrick Mochel ACPI_DEBUG_PRINT((ACPI_DB_INFO, 5875d9464a4SPatrick Mochel "Found driver [%s] for device [%s]\n", 5885d9464a4SPatrick Mochel acpi_drv->name, acpi_dev->pnp.bus_id)); 5895d9464a4SPatrick Mochel get_device(dev); 5905d9464a4SPatrick Mochel } 5915d9464a4SPatrick Mochel return ret; 5925d9464a4SPatrick Mochel } 5935d9464a4SPatrick Mochel 5945d9464a4SPatrick Mochel static int acpi_device_remove(struct device * dev) 5955d9464a4SPatrick Mochel { 5965d9464a4SPatrick Mochel struct acpi_device *acpi_dev = to_acpi_device(dev); 5975d9464a4SPatrick Mochel struct acpi_driver *acpi_drv = acpi_dev->driver; 5985d9464a4SPatrick Mochel 5995d9464a4SPatrick Mochel if (acpi_drv) { 60046ec8598SBjorn Helgaas if (acpi_drv->ops.notify) 60146ec8598SBjorn Helgaas acpi_device_remove_notify_handler(acpi_dev); 6025d9464a4SPatrick Mochel if (acpi_drv->ops.remove) 60396333578SLi Shaohua acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type); 6045d9464a4SPatrick Mochel } 6055d9464a4SPatrick Mochel acpi_dev->driver = NULL; 606db89b4f0SPavel Machek acpi_dev->driver_data = NULL; 6075d9464a4SPatrick Mochel 6085d9464a4SPatrick Mochel put_device(dev); 6095d9464a4SPatrick Mochel return 0; 6105d9464a4SPatrick Mochel } 6115d9464a4SPatrick Mochel 61255955aadSDavid Brownell struct bus_type acpi_bus_type = { 6139e89dde2SZhang Rui .name = "acpi", 6145d9464a4SPatrick Mochel .match = acpi_bus_match, 6155d9464a4SPatrick Mochel .probe = acpi_device_probe, 6165d9464a4SPatrick Mochel .remove = acpi_device_remove, 6175d9464a4SPatrick Mochel .uevent = acpi_device_uevent, 6181da177e4SLinus Torvalds }; 6191da177e4SLinus Torvalds 62066b7ed40SBjorn Helgaas static int acpi_device_register(struct acpi_device *device) 6211da177e4SLinus Torvalds { 622e49bd2ddSZhang Rui int result; 623e49bd2ddSZhang Rui struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id; 624e49bd2ddSZhang Rui int found = 0; 62566b7ed40SBjorn Helgaas 6261da177e4SLinus Torvalds /* 6271da177e4SLinus Torvalds * Linkage 6281da177e4SLinus Torvalds * ------- 6291da177e4SLinus Torvalds * Link this device to its parent and siblings. 6301da177e4SLinus Torvalds */ 6311da177e4SLinus Torvalds INIT_LIST_HEAD(&device->children); 6321da177e4SLinus Torvalds INIT_LIST_HEAD(&device->node); 6331da177e4SLinus Torvalds INIT_LIST_HEAD(&device->wakeup_list); 6341033f904SLan Tianyu INIT_LIST_HEAD(&device->physical_node_list); 6351033f904SLan Tianyu mutex_init(&device->physical_node_lock); 6361da177e4SLinus Torvalds 637e49bd2ddSZhang Rui new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL); 638e49bd2ddSZhang Rui if (!new_bus_id) { 639e49bd2ddSZhang Rui printk(KERN_ERR PREFIX "Memory allocation error\n"); 640e49bd2ddSZhang Rui return -ENOMEM; 641e49bd2ddSZhang Rui } 642e49bd2ddSZhang Rui 6439090589dSShaohua Li mutex_lock(&acpi_device_lock); 644e49bd2ddSZhang Rui /* 645e49bd2ddSZhang Rui * Find suitable bus_id and instance number in acpi_bus_id_list 646e49bd2ddSZhang Rui * If failed, create one and link it into acpi_bus_id_list 647e49bd2ddSZhang Rui */ 648e49bd2ddSZhang Rui list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { 6491131b938SBjorn Helgaas if (!strcmp(acpi_device_bus_id->bus_id, 6501131b938SBjorn Helgaas acpi_device_hid(device))) { 651e49bd2ddSZhang Rui acpi_device_bus_id->instance_no++; 652e49bd2ddSZhang Rui found = 1; 653e49bd2ddSZhang Rui kfree(new_bus_id); 654e49bd2ddSZhang Rui break; 655e49bd2ddSZhang Rui } 656e49bd2ddSZhang Rui } 657e49bd2ddSZhang Rui if (!found) { 658e49bd2ddSZhang Rui acpi_device_bus_id = new_bus_id; 6591131b938SBjorn Helgaas strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device)); 660e49bd2ddSZhang Rui acpi_device_bus_id->instance_no = 0; 661e49bd2ddSZhang Rui list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); 662e49bd2ddSZhang Rui } 6630794469dSKay Sievers dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no); 664e49bd2ddSZhang Rui 66533b57150SLen Brown if (device->parent) 6661da177e4SLinus Torvalds list_add_tail(&device->node, &device->parent->children); 66733b57150SLen Brown 6681da177e4SLinus Torvalds if (device->wakeup.flags.valid) 6691da177e4SLinus Torvalds list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); 6709090589dSShaohua Li mutex_unlock(&acpi_device_lock); 6711da177e4SLinus Torvalds 6721890a97aSPatrick Mochel if (device->parent) 67366b7ed40SBjorn Helgaas device->dev.parent = &device->parent->dev; 6741890a97aSPatrick Mochel device->dev.bus = &acpi_bus_type; 6751890a97aSPatrick Mochel device->dev.release = &acpi_device_release; 6768b12b922SAlex Chiang result = device_register(&device->dev); 677e49bd2ddSZhang Rui if (result) { 6788b12b922SAlex Chiang dev_err(&device->dev, "Error registering device\n"); 679e49bd2ddSZhang Rui goto end; 680e49bd2ddSZhang Rui } 681f883d9dbSPatrick Mochel 682e49bd2ddSZhang Rui result = acpi_device_setup_files(device); 683e49bd2ddSZhang Rui if (result) 6840794469dSKay Sievers printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n", 6850794469dSKay Sievers dev_name(&device->dev)); 686e49bd2ddSZhang Rui 68796333578SLi Shaohua device->removal_type = ACPI_BUS_REMOVAL_NORMAL; 688e49bd2ddSZhang Rui return 0; 689e49bd2ddSZhang Rui end: 6909090589dSShaohua Li mutex_lock(&acpi_device_lock); 69133b57150SLen Brown if (device->parent) 692e49bd2ddSZhang Rui list_del(&device->node); 693e49bd2ddSZhang Rui list_del(&device->wakeup_list); 6949090589dSShaohua Li mutex_unlock(&acpi_device_lock); 695e49bd2ddSZhang Rui return result; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds 698b17b537aSRafael J. Wysocki static void acpi_device_unregister(struct acpi_device *device) 6991da177e4SLinus Torvalds { 7009090589dSShaohua Li mutex_lock(&acpi_device_lock); 70133b57150SLen Brown if (device->parent) 7021da177e4SLinus Torvalds list_del(&device->node); 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds list_del(&device->wakeup_list); 7059090589dSShaohua Li mutex_unlock(&acpi_device_lock); 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds acpi_detach_data(device->handle, acpi_bus_data_handler); 7081890a97aSPatrick Mochel 709f883d9dbSPatrick Mochel acpi_device_remove_files(device); 7101890a97aSPatrick Mochel device_unregister(&device->dev); 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 7139e89dde2SZhang Rui /* -------------------------------------------------------------------------- 7149e89dde2SZhang Rui Driver Management 7159e89dde2SZhang Rui -------------------------------------------------------------------------- */ 7161da177e4SLinus Torvalds /** 717d758a8faSRandy Dunlap * acpi_bus_driver_init - add a device to a driver 718d758a8faSRandy Dunlap * @device: the device to add and initialize 719d758a8faSRandy Dunlap * @driver: driver for the device 720d758a8faSRandy Dunlap * 7211da177e4SLinus Torvalds * Used to initialize a device via its device driver. Called whenever a 7221890a97aSPatrick Mochel * driver is bound to a device. Invokes the driver's add() ops. 7231da177e4SLinus Torvalds */ 7241da177e4SLinus Torvalds static int 7254be44fcdSLen Brown acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver) 7261da177e4SLinus Torvalds { 7271da177e4SLinus Torvalds int result = 0; 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds if (!device || !driver) 730d550d98dSPatrick Mochel return -EINVAL; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds if (!driver->ops.add) 733d550d98dSPatrick Mochel return -ENOSYS; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds result = driver->ops.add(device); 7361da177e4SLinus Torvalds if (result) { 7371da177e4SLinus Torvalds device->driver = NULL; 738db89b4f0SPavel Machek device->driver_data = NULL; 739d550d98dSPatrick Mochel return result; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds device->driver = driver; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds /* 7451da177e4SLinus Torvalds * TBD - Configuration Management: Assign resources to device based 7461da177e4SLinus Torvalds * upon possible configuration and currently allocated resources. 7471da177e4SLinus Torvalds */ 7481da177e4SLinus Torvalds 7494be44fcdSLen Brown ACPI_DEBUG_PRINT((ACPI_DB_INFO, 7504be44fcdSLen Brown "Driver successfully bound to device\n")); 751d550d98dSPatrick Mochel return 0; 7523fb02738SRajesh Shah } 7533fb02738SRajesh Shah 7541da177e4SLinus Torvalds /** 755d758a8faSRandy Dunlap * acpi_bus_register_driver - register a driver with the ACPI bus 756d758a8faSRandy Dunlap * @driver: driver being registered 757d758a8faSRandy Dunlap * 7581da177e4SLinus Torvalds * Registers a driver with the ACPI bus. Searches the namespace for all 7599d9f749bSBjorn Helgaas * devices that match the driver's criteria and binds. Returns zero for 7609d9f749bSBjorn Helgaas * success or a negative error status for failure. 7611da177e4SLinus Torvalds */ 7624be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver) 7631da177e4SLinus Torvalds { 7641890a97aSPatrick Mochel int ret; 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds if (acpi_disabled) 767d550d98dSPatrick Mochel return -ENODEV; 7681890a97aSPatrick Mochel driver->drv.name = driver->name; 7691890a97aSPatrick Mochel driver->drv.bus = &acpi_bus_type; 7701890a97aSPatrick Mochel driver->drv.owner = driver->owner; 7711da177e4SLinus Torvalds 7721890a97aSPatrick Mochel ret = driver_register(&driver->drv); 7731890a97aSPatrick Mochel return ret; 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds 7764be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver); 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds /** 779d758a8faSRandy Dunlap * acpi_bus_unregister_driver - unregisters a driver with the APIC bus 780d758a8faSRandy Dunlap * @driver: driver to unregister 781d758a8faSRandy Dunlap * 7821da177e4SLinus Torvalds * Unregisters a driver with the ACPI bus. Searches the namespace for all 7831da177e4SLinus Torvalds * devices that match the driver's criteria and unbinds. 7841da177e4SLinus Torvalds */ 78506ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver) 7861da177e4SLinus Torvalds { 7871890a97aSPatrick Mochel driver_unregister(&driver->drv); 7881da177e4SLinus Torvalds } 7894be44fcdSLen Brown 7901da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver); 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds /* -------------------------------------------------------------------------- 7931da177e4SLinus Torvalds Device Enumeration 7941da177e4SLinus Torvalds -------------------------------------------------------------------------- */ 7955c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle) 7965c478f49SBjorn Helgaas { 7975c478f49SBjorn Helgaas acpi_status status; 7985c478f49SBjorn Helgaas int ret; 7995c478f49SBjorn Helgaas struct acpi_device *device; 8005c478f49SBjorn Helgaas 8015c478f49SBjorn Helgaas /* 8025c478f49SBjorn Helgaas * Fixed hardware devices do not appear in the namespace and do not 8035c478f49SBjorn Helgaas * have handles, but we fabricate acpi_devices for them, so we have 8045c478f49SBjorn Helgaas * to deal with them specially. 8055c478f49SBjorn Helgaas */ 8065c478f49SBjorn Helgaas if (handle == NULL) 8075c478f49SBjorn Helgaas return acpi_root; 8085c478f49SBjorn Helgaas 8095c478f49SBjorn Helgaas do { 8105c478f49SBjorn Helgaas status = acpi_get_parent(handle, &handle); 8115c478f49SBjorn Helgaas if (status == AE_NULL_ENTRY) 8125c478f49SBjorn Helgaas return NULL; 8135c478f49SBjorn Helgaas if (ACPI_FAILURE(status)) 8145c478f49SBjorn Helgaas return acpi_root; 8155c478f49SBjorn Helgaas 8165c478f49SBjorn Helgaas ret = acpi_bus_get_device(handle, &device); 8175c478f49SBjorn Helgaas if (ret == 0) 8185c478f49SBjorn Helgaas return device; 8195c478f49SBjorn Helgaas } while (1); 8205c478f49SBjorn Helgaas } 8215c478f49SBjorn Helgaas 822c8f7a62cSLen Brown acpi_status 823c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd) 824c8f7a62cSLen Brown { 825c8f7a62cSLen Brown acpi_status status; 826c8f7a62cSLen Brown acpi_handle tmp; 827c8f7a62cSLen Brown struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 828c8f7a62cSLen Brown union acpi_object *obj; 829c8f7a62cSLen Brown 830c8f7a62cSLen Brown status = acpi_get_handle(handle, "_EJD", &tmp); 831c8f7a62cSLen Brown if (ACPI_FAILURE(status)) 832c8f7a62cSLen Brown return status; 833c8f7a62cSLen Brown 834c8f7a62cSLen Brown status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer); 835c8f7a62cSLen Brown if (ACPI_SUCCESS(status)) { 836c8f7a62cSLen Brown obj = buffer.pointer; 8373b5fee59SHolger Macht status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer, 8383b5fee59SHolger Macht ejd); 839c8f7a62cSLen Brown kfree(buffer.pointer); 840c8f7a62cSLen Brown } 841c8f7a62cSLen Brown return status; 842c8f7a62cSLen Brown } 843c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd); 844c8f7a62cSLen Brown 8458e4319c4SBob Moore void acpi_bus_data_handler(acpi_handle handle, void *context) 8461da177e4SLinus Torvalds { 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds /* TBD */ 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds return; 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds 8539e89dde2SZhang Rui static int acpi_bus_get_perf_flags(struct acpi_device *device) 8549e89dde2SZhang Rui { 8559e89dde2SZhang Rui device->performance.state = ACPI_STATE_UNKNOWN; 8569e89dde2SZhang Rui return 0; 8579e89dde2SZhang Rui } 8589e89dde2SZhang Rui 8591da177e4SLinus Torvalds static acpi_status 860b581a7f9SRafael J. Wysocki acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, 861b581a7f9SRafael J. Wysocki struct acpi_device_wakeup *wakeup) 8621da177e4SLinus Torvalds { 863b581a7f9SRafael J. Wysocki struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 864b581a7f9SRafael J. Wysocki union acpi_object *package = NULL; 8651da177e4SLinus Torvalds union acpi_object *element = NULL; 866b581a7f9SRafael J. Wysocki acpi_status status; 867b581a7f9SRafael J. Wysocki int i = 0; 8681da177e4SLinus Torvalds 869b581a7f9SRafael J. Wysocki if (!wakeup) 8701da177e4SLinus Torvalds return AE_BAD_PARAMETER; 8711da177e4SLinus Torvalds 872b581a7f9SRafael J. Wysocki /* _PRW */ 873b581a7f9SRafael J. Wysocki status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer); 874b581a7f9SRafael J. Wysocki if (ACPI_FAILURE(status)) { 875b581a7f9SRafael J. Wysocki ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW")); 876b581a7f9SRafael J. Wysocki return status; 877b581a7f9SRafael J. Wysocki } 878b581a7f9SRafael J. Wysocki 879b581a7f9SRafael J. Wysocki package = (union acpi_object *)buffer.pointer; 880b581a7f9SRafael J. Wysocki 881b581a7f9SRafael J. Wysocki if (!package || (package->package.count < 2)) { 882b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 883b581a7f9SRafael J. Wysocki goto out; 884b581a7f9SRafael J. Wysocki } 885b581a7f9SRafael J. Wysocki 8861da177e4SLinus Torvalds element = &(package->package.elements[0]); 887b581a7f9SRafael J. Wysocki if (!element) { 888b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 889b581a7f9SRafael J. Wysocki goto out; 890b581a7f9SRafael J. Wysocki } 8911da177e4SLinus Torvalds if (element->type == ACPI_TYPE_PACKAGE) { 8921da177e4SLinus Torvalds if ((element->package.count < 2) || 8931da177e4SLinus Torvalds (element->package.elements[0].type != 8941da177e4SLinus Torvalds ACPI_TYPE_LOCAL_REFERENCE) 895b581a7f9SRafael J. Wysocki || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) { 896b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 897b581a7f9SRafael J. Wysocki goto out; 898b581a7f9SRafael J. Wysocki } 899b581a7f9SRafael J. Wysocki wakeup->gpe_device = 9001da177e4SLinus Torvalds element->package.elements[0].reference.handle; 901b581a7f9SRafael J. Wysocki wakeup->gpe_number = 9021da177e4SLinus Torvalds (u32) element->package.elements[1].integer.value; 9031da177e4SLinus Torvalds } else if (element->type == ACPI_TYPE_INTEGER) { 904b581a7f9SRafael J. Wysocki wakeup->gpe_device = NULL; 905b581a7f9SRafael J. Wysocki wakeup->gpe_number = element->integer.value; 906b581a7f9SRafael J. Wysocki } else { 907b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 908b581a7f9SRafael J. Wysocki goto out; 909b581a7f9SRafael J. Wysocki } 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds element = &(package->package.elements[1]); 9121da177e4SLinus Torvalds if (element->type != ACPI_TYPE_INTEGER) { 913b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 914b581a7f9SRafael J. Wysocki goto out; 9151da177e4SLinus Torvalds } 916b581a7f9SRafael J. Wysocki wakeup->sleep_state = element->integer.value; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds if ((package->package.count - 2) > ACPI_MAX_HANDLES) { 919b581a7f9SRafael J. Wysocki status = AE_NO_MEMORY; 920b581a7f9SRafael J. Wysocki goto out; 9211da177e4SLinus Torvalds } 922b581a7f9SRafael J. Wysocki wakeup->resources.count = package->package.count - 2; 923b581a7f9SRafael J. Wysocki for (i = 0; i < wakeup->resources.count; i++) { 9241da177e4SLinus Torvalds element = &(package->package.elements[i + 2]); 925b581a7f9SRafael J. Wysocki if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { 926b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 927b581a7f9SRafael J. Wysocki goto out; 9281da177e4SLinus Torvalds } 9291da177e4SLinus Torvalds 930b581a7f9SRafael J. Wysocki wakeup->resources.handles[i] = element->reference.handle; 931b581a7f9SRafael J. Wysocki } 9329874647bSRafael J. Wysocki 933bba63a29SLin Ming acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number); 934b581a7f9SRafael J. Wysocki 935b581a7f9SRafael J. Wysocki out: 936b581a7f9SRafael J. Wysocki kfree(buffer.pointer); 937b581a7f9SRafael J. Wysocki 938b581a7f9SRafael J. Wysocki return status; 9391da177e4SLinus Torvalds } 9401da177e4SLinus Torvalds 941f517709dSRafael J. Wysocki static void acpi_bus_set_run_wake_flags(struct acpi_device *device) 9421da177e4SLinus Torvalds { 94329b71a1cSThomas Renninger struct acpi_device_id button_device_ids[] = { 94429b71a1cSThomas Renninger {"PNP0C0C", 0}, 945b7e38304SZhang Rui {"PNP0C0D", 0}, 94629b71a1cSThomas Renninger {"PNP0C0E", 0}, 94729b71a1cSThomas Renninger {"", 0}, 94829b71a1cSThomas Renninger }; 949f517709dSRafael J. Wysocki acpi_status status; 950f517709dSRafael J. Wysocki acpi_event_status event_status; 951f517709dSRafael J. Wysocki 952b67ea761SRafael J. Wysocki device->wakeup.flags.notifier_present = 0; 953f517709dSRafael J. Wysocki 954f517709dSRafael J. Wysocki /* Power button, Lid switch always enable wakeup */ 955f517709dSRafael J. Wysocki if (!acpi_match_device_ids(device, button_device_ids)) { 956f517709dSRafael J. Wysocki device->wakeup.flags.run_wake = 1; 957b7e38304SZhang Rui if (!acpi_match_device_ids(device, &button_device_ids[1])) { 958b7e38304SZhang Rui /* Do not use Lid/sleep button for S5 wakeup */ 959b7e38304SZhang Rui if (device->wakeup.sleep_state == ACPI_STATE_S5) 960b7e38304SZhang Rui device->wakeup.sleep_state = ACPI_STATE_S4; 961b7e38304SZhang Rui } 962f2b56bc8SRafael J. Wysocki device_set_wakeup_capable(&device->dev, true); 963f517709dSRafael J. Wysocki return; 964f517709dSRafael J. Wysocki } 965f517709dSRafael J. Wysocki 966e8e18c95SRafael J. Wysocki status = acpi_get_gpe_status(device->wakeup.gpe_device, 967e8e18c95SRafael J. Wysocki device->wakeup.gpe_number, 9680f849d2cSLin Ming &event_status); 969f517709dSRafael J. Wysocki if (status == AE_OK) 970f517709dSRafael J. Wysocki device->wakeup.flags.run_wake = 971f517709dSRafael J. Wysocki !!(event_status & ACPI_EVENT_FLAG_HANDLE); 972f517709dSRafael J. Wysocki } 973f517709dSRafael J. Wysocki 974d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) 975f517709dSRafael J. Wysocki { 976d57d09a4SRafael J. Wysocki acpi_handle temp; 977f517709dSRafael J. Wysocki acpi_status status = 0; 978f517709dSRafael J. Wysocki int psw_error; 97929b71a1cSThomas Renninger 980d57d09a4SRafael J. Wysocki /* Presence of _PRW indicates wake capable */ 981d57d09a4SRafael J. Wysocki status = acpi_get_handle(device->handle, "_PRW", &temp); 982d57d09a4SRafael J. Wysocki if (ACPI_FAILURE(status)) 983d57d09a4SRafael J. Wysocki return; 984d57d09a4SRafael J. Wysocki 985b581a7f9SRafael J. Wysocki status = acpi_bus_extract_wakeup_device_power_package(device->handle, 986b581a7f9SRafael J. Wysocki &device->wakeup); 9871da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 9881da177e4SLinus Torvalds ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package")); 989d57d09a4SRafael J. Wysocki return; 9901da177e4SLinus Torvalds } 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds device->wakeup.flags.valid = 1; 9939b83ccd2SRafael J. Wysocki device->wakeup.prepare_count = 0; 994f517709dSRafael J. Wysocki acpi_bus_set_run_wake_flags(device); 995729b2bdbSZhao Yakui /* Call _PSW/_DSW object to disable its ability to wake the sleeping 996729b2bdbSZhao Yakui * system for the ACPI device with the _PRW object. 997729b2bdbSZhao Yakui * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW. 998729b2bdbSZhao Yakui * So it is necessary to call _DSW object first. Only when it is not 999729b2bdbSZhao Yakui * present will the _PSW object used. 1000729b2bdbSZhao Yakui */ 100177e76609SRafael J. Wysocki psw_error = acpi_device_sleep_wake(device, 0, 0, 0); 100277e76609SRafael J. Wysocki if (psw_error) 100377e76609SRafael J. Wysocki ACPI_DEBUG_PRINT((ACPI_DB_INFO, 100477e76609SRafael J. Wysocki "error in _DSW or _PSW evaluation\n")); 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 1007bf325f95SRafael J. Wysocki static void acpi_bus_add_power_resource(acpi_handle handle); 1008bf325f95SRafael J. Wysocki 10099e89dde2SZhang Rui static int acpi_bus_get_power_flags(struct acpi_device *device) 10101da177e4SLinus Torvalds { 10119e89dde2SZhang Rui acpi_status status = 0; 10129e89dde2SZhang Rui acpi_handle handle = NULL; 10139e89dde2SZhang Rui u32 i = 0; 10149e89dde2SZhang Rui 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds /* 10179e89dde2SZhang Rui * Power Management Flags 10181da177e4SLinus Torvalds */ 10199e89dde2SZhang Rui status = acpi_get_handle(device->handle, "_PSC", &handle); 10201da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 10219e89dde2SZhang Rui device->power.flags.explicit_get = 1; 10229e89dde2SZhang Rui status = acpi_get_handle(device->handle, "_IRC", &handle); 10239e89dde2SZhang Rui if (ACPI_SUCCESS(status)) 10249e89dde2SZhang Rui device->power.flags.inrush_current = 1; 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds /* 10279e89dde2SZhang Rui * Enumerate supported power management states 10281da177e4SLinus Torvalds */ 10291cc0c998SLin Ming for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { 10309e89dde2SZhang Rui struct acpi_device_power_state *ps = &device->power.states[i]; 10319e89dde2SZhang Rui char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' }; 10321da177e4SLinus Torvalds 10339e89dde2SZhang Rui /* Evaluate "_PRx" to se if power resources are referenced */ 10349e89dde2SZhang Rui acpi_evaluate_reference(device->handle, object_name, NULL, 10359e89dde2SZhang Rui &ps->resources); 10369e89dde2SZhang Rui if (ps->resources.count) { 1037bf325f95SRafael J. Wysocki int j; 1038bf325f95SRafael J. Wysocki 10399e89dde2SZhang Rui device->power.flags.power_resources = 1; 1040bf325f95SRafael J. Wysocki for (j = 0; j < ps->resources.count; j++) 1041bf325f95SRafael J. Wysocki acpi_bus_add_power_resource(ps->resources.handles[j]); 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10449e89dde2SZhang Rui /* Evaluate "_PSx" to see if we can do explicit sets */ 10459e89dde2SZhang Rui object_name[2] = 'S'; 10469e89dde2SZhang Rui status = acpi_get_handle(device->handle, object_name, &handle); 104737239978SAlex He if (ACPI_SUCCESS(status)) 10489e89dde2SZhang Rui ps->flags.explicit_set = 1; 1049c8f7a62cSLen Brown 10501cc0c998SLin Ming /* 10511cc0c998SLin Ming * State is valid if there are means to put the device into it. 10521cc0c998SLin Ming * D3hot is only valid if _PR3 present. 10531cc0c998SLin Ming */ 10541cc0c998SLin Ming if (ps->resources.count || 10551399dfcdSAaron Lu (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) { 10569e89dde2SZhang Rui ps->flags.valid = 1; 10571399dfcdSAaron Lu ps->flags.os_accessible = 1; 10581399dfcdSAaron Lu } 10599e89dde2SZhang Rui 10609e89dde2SZhang Rui ps->power = -1; /* Unknown - driver assigned */ 10619e89dde2SZhang Rui ps->latency = -1; /* Unknown - driver assigned */ 10629e89dde2SZhang Rui } 10639e89dde2SZhang Rui 10649e89dde2SZhang Rui /* Set defaults for D0 and D3 states (always valid) */ 10659e89dde2SZhang Rui device->power.states[ACPI_STATE_D0].flags.valid = 1; 10669e89dde2SZhang Rui device->power.states[ACPI_STATE_D0].power = 100; 10679e89dde2SZhang Rui device->power.states[ACPI_STATE_D3].flags.valid = 1; 10689e89dde2SZhang Rui device->power.states[ACPI_STATE_D3].power = 0; 10699e89dde2SZhang Rui 10705c7dd710SRafael J. Wysocki /* Set D3cold's explicit_set flag if _PS3 exists. */ 10715c7dd710SRafael J. Wysocki if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set) 10725c7dd710SRafael J. Wysocki device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1; 10735c7dd710SRafael J. Wysocki 10741399dfcdSAaron Lu /* Presence of _PS3 or _PRx means we can put the device into D3 cold */ 10751399dfcdSAaron Lu if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set || 10761399dfcdSAaron Lu device->power.flags.power_resources) 10771399dfcdSAaron Lu device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1; 10781399dfcdSAaron Lu 1079ade3e7feSRafael J. Wysocki acpi_bus_init_power(device); 10809e89dde2SZhang Rui 10819e89dde2SZhang Rui return 0; 10829e89dde2SZhang Rui } 1083c8f7a62cSLen Brown 10844be44fcdSLen Brown static int acpi_bus_get_flags(struct acpi_device *device) 10851da177e4SLinus Torvalds { 10861da177e4SLinus Torvalds acpi_status status = AE_OK; 10871da177e4SLinus Torvalds acpi_handle temp = NULL; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds /* Presence of _STA indicates 'dynamic_status' */ 10911da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_STA", &temp); 10921da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 10931da177e4SLinus Torvalds device->flags.dynamic_status = 1; 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds /* Presence of _RMV indicates 'removable' */ 10961da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_RMV", &temp); 10971da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 10981da177e4SLinus Torvalds device->flags.removable = 1; 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds /* Presence of _EJD|_EJ0 indicates 'ejectable' */ 11011da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_EJD", &temp); 11021da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 11031da177e4SLinus Torvalds device->flags.ejectable = 1; 11041da177e4SLinus Torvalds else { 11051da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_EJ0", &temp); 11061da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 11071da177e4SLinus Torvalds device->flags.ejectable = 1; 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds 11107bed50c5SRafael J. Wysocki /* Power resources cannot be power manageable. */ 11117bed50c5SRafael J. Wysocki if (device->device_type == ACPI_BUS_TYPE_POWER) 11127bed50c5SRafael J. Wysocki return 0; 11137bed50c5SRafael J. Wysocki 11141da177e4SLinus Torvalds /* Presence of _PS0|_PR0 indicates 'power manageable' */ 11151da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_PS0", &temp); 11161da177e4SLinus Torvalds if (ACPI_FAILURE(status)) 11171da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_PR0", &temp); 11181da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 11191da177e4SLinus Torvalds device->flags.power_manageable = 1; 11201da177e4SLinus Torvalds 11213c5f9be4SJoe Perches /* TBD: Performance management */ 11221da177e4SLinus Torvalds 1123d550d98dSPatrick Mochel return 0; 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 1126c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device) 11271da177e4SLinus Torvalds { 11281da177e4SLinus Torvalds char bus_id[5] = { '?', 0 }; 11291da177e4SLinus Torvalds struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; 11301da177e4SLinus Torvalds int i = 0; 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds /* 11331da177e4SLinus Torvalds * Bus ID 11341da177e4SLinus Torvalds * ------ 11351da177e4SLinus Torvalds * The device's Bus ID is simply the object name. 11361da177e4SLinus Torvalds * TBD: Shouldn't this value be unique (within the ACPI namespace)? 11371da177e4SLinus Torvalds */ 1138859ac9a4SBjorn Helgaas if (ACPI_IS_ROOT_DEVICE(device)) { 11391da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "ACPI"); 1140859ac9a4SBjorn Helgaas return; 1141859ac9a4SBjorn Helgaas } 1142859ac9a4SBjorn Helgaas 1143859ac9a4SBjorn Helgaas switch (device->device_type) { 11441da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER_BUTTON: 11451da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "PWRF"); 11461da177e4SLinus Torvalds break; 11471da177e4SLinus Torvalds case ACPI_BUS_TYPE_SLEEP_BUTTON: 11481da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "SLPF"); 11491da177e4SLinus Torvalds break; 11501da177e4SLinus Torvalds default: 115166b7ed40SBjorn Helgaas acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer); 11521da177e4SLinus Torvalds /* Clean up trailing underscores (if any) */ 11531da177e4SLinus Torvalds for (i = 3; i > 1; i--) { 11541da177e4SLinus Torvalds if (bus_id[i] == '_') 11551da177e4SLinus Torvalds bus_id[i] = '\0'; 11561da177e4SLinus Torvalds else 11571da177e4SLinus Torvalds break; 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds strcpy(device->pnp.bus_id, bus_id); 11601da177e4SLinus Torvalds break; 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds } 11631da177e4SLinus Torvalds 116454735266SZhang Rui /* 116554735266SZhang Rui * acpi_bay_match - see if a device is an ejectable driver bay 116654735266SZhang Rui * 116754735266SZhang Rui * If an acpi object is ejectable and has one of the ACPI ATA methods defined, 116854735266SZhang Rui * then we can safely call it an ejectable drive bay 116954735266SZhang Rui */ 117054735266SZhang Rui static int acpi_bay_match(struct acpi_device *device){ 117154735266SZhang Rui acpi_status status; 117254735266SZhang Rui acpi_handle handle; 117354735266SZhang Rui acpi_handle tmp; 117454735266SZhang Rui acpi_handle phandle; 117554735266SZhang Rui 117654735266SZhang Rui handle = device->handle; 117754735266SZhang Rui 117854735266SZhang Rui status = acpi_get_handle(handle, "_EJ0", &tmp); 117954735266SZhang Rui if (ACPI_FAILURE(status)) 118054735266SZhang Rui return -ENODEV; 118154735266SZhang Rui 118254735266SZhang Rui if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) || 118354735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) || 118454735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) || 118554735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp)))) 118654735266SZhang Rui return 0; 118754735266SZhang Rui 118854735266SZhang Rui if (acpi_get_parent(handle, &phandle)) 118954735266SZhang Rui return -ENODEV; 119054735266SZhang Rui 119154735266SZhang Rui if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) || 119254735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) || 119354735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) || 119454735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp)))) 119554735266SZhang Rui return 0; 119654735266SZhang Rui 119754735266SZhang Rui return -ENODEV; 119854735266SZhang Rui } 119954735266SZhang Rui 12003620f2f2SFrank Seidel /* 12013620f2f2SFrank Seidel * acpi_dock_match - see if a device has a _DCK method 12023620f2f2SFrank Seidel */ 12033620f2f2SFrank Seidel static int acpi_dock_match(struct acpi_device *device) 12043620f2f2SFrank Seidel { 12053620f2f2SFrank Seidel acpi_handle tmp; 12063620f2f2SFrank Seidel return acpi_get_handle(device->handle, "_DCK", &tmp); 12073620f2f2SFrank Seidel } 12083620f2f2SFrank Seidel 1209620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device) 121015b8dd53SBob Moore { 12117f47fa6cSBjorn Helgaas struct acpi_hardware_id *hid; 121215b8dd53SBob Moore 12132b2ae7c7SThomas Renninger if (list_empty(&device->pnp.ids)) 12142b2ae7c7SThomas Renninger return dummy_hid; 12152b2ae7c7SThomas Renninger 12167f47fa6cSBjorn Helgaas hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list); 12177f47fa6cSBjorn Helgaas return hid->id; 12187f47fa6cSBjorn Helgaas } 12197f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid); 122015b8dd53SBob Moore 12217f47fa6cSBjorn Helgaas static void acpi_add_id(struct acpi_device *device, const char *dev_id) 12227f47fa6cSBjorn Helgaas { 12237f47fa6cSBjorn Helgaas struct acpi_hardware_id *id; 122415b8dd53SBob Moore 12257f47fa6cSBjorn Helgaas id = kmalloc(sizeof(*id), GFP_KERNEL); 12267f47fa6cSBjorn Helgaas if (!id) 12277f47fa6cSBjorn Helgaas return; 122815b8dd53SBob Moore 1229581de59eSThomas Meyer id->id = kstrdup(dev_id, GFP_KERNEL); 12307f47fa6cSBjorn Helgaas if (!id->id) { 12317f47fa6cSBjorn Helgaas kfree(id); 12327f47fa6cSBjorn Helgaas return; 123315b8dd53SBob Moore } 123415b8dd53SBob Moore 12357f47fa6cSBjorn Helgaas list_add_tail(&id->list, &device->pnp.ids); 123615b8dd53SBob Moore } 123715b8dd53SBob Moore 1238222e82acSDarrick J. Wong /* 1239222e82acSDarrick J. Wong * Old IBM workstations have a DSDT bug wherein the SMBus object 1240222e82acSDarrick J. Wong * lacks the SMBUS01 HID and the methods do not have the necessary "_" 1241222e82acSDarrick J. Wong * prefix. Work around this. 1242222e82acSDarrick J. Wong */ 1243222e82acSDarrick J. Wong static int acpi_ibm_smbus_match(struct acpi_device *device) 1244222e82acSDarrick J. Wong { 1245222e82acSDarrick J. Wong acpi_handle h_dummy; 1246222e82acSDarrick J. Wong struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL}; 1247222e82acSDarrick J. Wong int result; 1248222e82acSDarrick J. Wong 1249222e82acSDarrick J. Wong if (!dmi_name_in_vendors("IBM")) 1250222e82acSDarrick J. Wong return -ENODEV; 1251222e82acSDarrick J. Wong 1252222e82acSDarrick J. Wong /* Look for SMBS object */ 1253222e82acSDarrick J. Wong result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path); 1254222e82acSDarrick J. Wong if (result) 1255222e82acSDarrick J. Wong return result; 1256222e82acSDarrick J. Wong 1257222e82acSDarrick J. Wong if (strcmp("SMBS", path.pointer)) { 1258222e82acSDarrick J. Wong result = -ENODEV; 1259222e82acSDarrick J. Wong goto out; 1260222e82acSDarrick J. Wong } 1261222e82acSDarrick J. Wong 1262222e82acSDarrick J. Wong /* Does it have the necessary (but misnamed) methods? */ 1263222e82acSDarrick J. Wong result = -ENODEV; 1264222e82acSDarrick J. Wong if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) && 1265222e82acSDarrick J. Wong ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) && 1266222e82acSDarrick J. Wong ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy))) 1267222e82acSDarrick J. Wong result = 0; 1268222e82acSDarrick J. Wong out: 1269222e82acSDarrick J. Wong kfree(path.pointer); 1270222e82acSDarrick J. Wong return result; 1271222e82acSDarrick J. Wong } 1272222e82acSDarrick J. Wong 1273c7bcb4e9SBjorn Helgaas static void acpi_device_set_id(struct acpi_device *device) 12741da177e4SLinus Torvalds { 12751da177e4SLinus Torvalds acpi_status status; 127657f3674fSBjorn Helgaas struct acpi_device_info *info; 127778e25fefSLv Zheng struct acpi_pnp_device_id_list *cid_list; 12787f47fa6cSBjorn Helgaas int i; 12791da177e4SLinus Torvalds 1280c7bcb4e9SBjorn Helgaas switch (device->device_type) { 12811da177e4SLinus Torvalds case ACPI_BUS_TYPE_DEVICE: 1282859ac9a4SBjorn Helgaas if (ACPI_IS_ROOT_DEVICE(device)) { 128357f3674fSBjorn Helgaas acpi_add_id(device, ACPI_SYSTEM_HID); 1284859ac9a4SBjorn Helgaas break; 1285859ac9a4SBjorn Helgaas } 1286859ac9a4SBjorn Helgaas 128766b7ed40SBjorn Helgaas status = acpi_get_object_info(device->handle, &info); 12881da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 128996b2dd1fSHarvey Harrison printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__); 12901da177e4SLinus Torvalds return; 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds if (info->valid & ACPI_VALID_HID) 129457f3674fSBjorn Helgaas acpi_add_id(device, info->hardware_id.string); 129557f3674fSBjorn Helgaas if (info->valid & ACPI_VALID_CID) { 129615b8dd53SBob Moore cid_list = &info->compatible_id_list; 129757f3674fSBjorn Helgaas for (i = 0; i < cid_list->count; i++) 129857f3674fSBjorn Helgaas acpi_add_id(device, cid_list->ids[i].string); 129957f3674fSBjorn Helgaas } 13001da177e4SLinus Torvalds if (info->valid & ACPI_VALID_ADR) { 13011da177e4SLinus Torvalds device->pnp.bus_address = info->address; 13021da177e4SLinus Torvalds device->flags.bus_address = 1; 13031da177e4SLinus Torvalds } 1304ccf78040SLv Zheng if (info->valid & ACPI_VALID_UID) 1305ccf78040SLv Zheng device->pnp.unique_id = kstrdup(info->unique_id.string, 1306ccf78040SLv Zheng GFP_KERNEL); 1307ae843332SZhang Rui 1308a83893aeSBjorn Helgaas kfree(info); 1309a83893aeSBjorn Helgaas 131057f3674fSBjorn Helgaas /* 131157f3674fSBjorn Helgaas * Some devices don't reliably have _HIDs & _CIDs, so add 131257f3674fSBjorn Helgaas * synthetic HIDs to make sure drivers can find them. 13133620f2f2SFrank Seidel */ 1314c3d6de69SThomas Renninger if (acpi_is_video_device(device)) 131557f3674fSBjorn Helgaas acpi_add_id(device, ACPI_VIDEO_HID); 13163620f2f2SFrank Seidel else if (ACPI_SUCCESS(acpi_bay_match(device))) 131757f3674fSBjorn Helgaas acpi_add_id(device, ACPI_BAY_HID); 13183620f2f2SFrank Seidel else if (ACPI_SUCCESS(acpi_dock_match(device))) 131957f3674fSBjorn Helgaas acpi_add_id(device, ACPI_DOCK_HID); 1320222e82acSDarrick J. Wong else if (!acpi_ibm_smbus_match(device)) 1321222e82acSDarrick J. Wong acpi_add_id(device, ACPI_SMBUS_IBM_HID); 1322b7b30de5SBjorn Helgaas else if (!acpi_device_hid(device) && 1323b7b30de5SBjorn Helgaas ACPI_IS_ROOT_DEVICE(device->parent)) { 1324b7b30de5SBjorn Helgaas acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */ 1325b7b30de5SBjorn Helgaas strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME); 1326b7b30de5SBjorn Helgaas strcpy(device->pnp.device_class, ACPI_BUS_CLASS); 1327b7b30de5SBjorn Helgaas } 132854735266SZhang Rui 13291da177e4SLinus Torvalds break; 13301da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER: 133157f3674fSBjorn Helgaas acpi_add_id(device, ACPI_POWER_HID); 13321da177e4SLinus Torvalds break; 13331da177e4SLinus Torvalds case ACPI_BUS_TYPE_PROCESSOR: 133457f3674fSBjorn Helgaas acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID); 13351da177e4SLinus Torvalds break; 13361da177e4SLinus Torvalds case ACPI_BUS_TYPE_THERMAL: 133757f3674fSBjorn Helgaas acpi_add_id(device, ACPI_THERMAL_HID); 13381da177e4SLinus Torvalds break; 13391da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER_BUTTON: 134057f3674fSBjorn Helgaas acpi_add_id(device, ACPI_BUTTON_HID_POWERF); 13411da177e4SLinus Torvalds break; 13421da177e4SLinus Torvalds case ACPI_BUS_TYPE_SLEEP_BUTTON: 134357f3674fSBjorn Helgaas acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF); 13441da177e4SLinus Torvalds break; 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 1348bc3b0772SBjorn Helgaas static int acpi_device_set_context(struct acpi_device *device) 13491da177e4SLinus Torvalds { 1350bc3b0772SBjorn Helgaas acpi_status status; 1351bc3b0772SBjorn Helgaas 13521da177e4SLinus Torvalds /* 13531da177e4SLinus Torvalds * Context 13541da177e4SLinus Torvalds * ------- 13551da177e4SLinus Torvalds * Attach this 'struct acpi_device' to the ACPI object. This makes 1356bc3b0772SBjorn Helgaas * resolutions from handle->device very efficient. Fixed hardware 1357bc3b0772SBjorn Helgaas * devices have no handles, so we skip them. 13581da177e4SLinus Torvalds */ 1359bc3b0772SBjorn Helgaas if (!device->handle) 1360bc3b0772SBjorn Helgaas return 0; 1361bc3b0772SBjorn Helgaas 13621da177e4SLinus Torvalds status = acpi_attach_data(device->handle, 13631da177e4SLinus Torvalds acpi_bus_data_handler, device); 1364bc3b0772SBjorn Helgaas if (ACPI_SUCCESS(status)) 1365bc3b0772SBjorn Helgaas return 0; 13661da177e4SLinus Torvalds 136787ecd5cdSLen Brown printk(KERN_ERR PREFIX "Error attaching device data\n"); 1368bc3b0772SBjorn Helgaas return -ENODEV; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 13715c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child, 13725c478f49SBjorn Helgaas acpi_handle handle, int type, 1373209d3b17SRafael J. Wysocki unsigned long long sta, bool match_driver) 13741da177e4SLinus Torvalds { 137577c24888SBjorn Helgaas int result; 137677c24888SBjorn Helgaas struct acpi_device *device; 137729aaefa6SBjorn Helgaas struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 13781da177e4SLinus Torvalds 137936bcbec7SBurman Yan device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); 13801da177e4SLinus Torvalds if (!device) { 13816468463aSLen Brown printk(KERN_ERR PREFIX "Memory allocation error\n"); 1382d550d98dSPatrick Mochel return -ENOMEM; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 13857f47fa6cSBjorn Helgaas INIT_LIST_HEAD(&device->pnp.ids); 1386caaa6efbSBjorn Helgaas device->device_type = type; 13871da177e4SLinus Torvalds device->handle = handle; 13885c478f49SBjorn Helgaas device->parent = acpi_bus_get_parent(handle); 1389778cbc1dSBjorn Helgaas STRUCT_TO_INT(device->status) = sta; 1390c4168bffSLi Shaohua 1391c7bcb4e9SBjorn Helgaas acpi_device_get_busid(device); 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds /* 13941da177e4SLinus Torvalds * Flags 13951da177e4SLinus Torvalds * ----- 1396778cbc1dSBjorn Helgaas * Note that we only look for object handles -- cannot evaluate objects 1397778cbc1dSBjorn Helgaas * until we know the device is present and properly initialized. 13981da177e4SLinus Torvalds */ 13991da177e4SLinus Torvalds result = acpi_bus_get_flags(device); 14001da177e4SLinus Torvalds if (result) 14011da177e4SLinus Torvalds goto end; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds /* 14041da177e4SLinus Torvalds * Initialize Device 14051da177e4SLinus Torvalds * ----------------- 14061da177e4SLinus Torvalds * TBD: Synch with Core's enumeration/initialization process. 14071da177e4SLinus Torvalds */ 1408c7bcb4e9SBjorn Helgaas acpi_device_set_id(device); 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds /* 14111da177e4SLinus Torvalds * Power Management 14121da177e4SLinus Torvalds * ---------------- 14131da177e4SLinus Torvalds */ 14141da177e4SLinus Torvalds if (device->flags.power_manageable) { 14151da177e4SLinus Torvalds result = acpi_bus_get_power_flags(device); 14161da177e4SLinus Torvalds if (result) 14171da177e4SLinus Torvalds goto end; 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds /* 14211da177e4SLinus Torvalds * Wakeup device management 14221da177e4SLinus Torvalds *----------------------- 14231da177e4SLinus Torvalds */ 1424d57d09a4SRafael J. Wysocki acpi_bus_get_wakeup_device_flags(device); 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds /* 14271da177e4SLinus Torvalds * Performance Management 14281da177e4SLinus Torvalds * ---------------------- 14291da177e4SLinus Torvalds */ 14301da177e4SLinus Torvalds if (device->flags.performance_manageable) { 14311da177e4SLinus Torvalds result = acpi_bus_get_perf_flags(device); 14321da177e4SLinus Torvalds if (result) 14331da177e4SLinus Torvalds goto end; 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds 1436bc3b0772SBjorn Helgaas if ((result = acpi_device_set_context(device))) 1437f61f9258SLen Brown goto end; 14381da177e4SLinus Torvalds 1439209d3b17SRafael J. Wysocki device->flags.match_driver = match_driver; 144066b7ed40SBjorn Helgaas result = acpi_device_register(device); 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds end: 144329aaefa6SBjorn Helgaas if (!result) { 144429aaefa6SBjorn Helgaas acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 144529aaefa6SBjorn Helgaas ACPI_DEBUG_PRINT((ACPI_DB_INFO, 144629aaefa6SBjorn Helgaas "Adding %s [%s] parent %s\n", dev_name(&device->dev), 144729aaefa6SBjorn Helgaas (char *) buffer.pointer, 144829aaefa6SBjorn Helgaas device->parent ? dev_name(&device->parent->dev) : 144929aaefa6SBjorn Helgaas "(null)")); 145029aaefa6SBjorn Helgaas kfree(buffer.pointer); 14511da177e4SLinus Torvalds *child = device; 145229aaefa6SBjorn Helgaas } else 1453718fb0deSHugh Dickins acpi_device_release(&device->dev); 14541da177e4SLinus Torvalds 1455d550d98dSPatrick Mochel return result; 14561da177e4SLinus Torvalds } 14571da177e4SLinus Torvalds 1458778cbc1dSBjorn Helgaas #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \ 1459778cbc1dSBjorn Helgaas ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING) 1460778cbc1dSBjorn Helgaas 1461bf325f95SRafael J. Wysocki static void acpi_bus_add_power_resource(acpi_handle handle) 1462bf325f95SRafael J. Wysocki { 1463bf325f95SRafael J. Wysocki struct acpi_device *device = NULL; 1464bf325f95SRafael J. Wysocki 1465bf325f95SRafael J. Wysocki acpi_bus_get_device(handle, &device); 1466bf325f95SRafael J. Wysocki if (!device) 1467bf325f95SRafael J. Wysocki acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER, 1468209d3b17SRafael J. Wysocki ACPI_STA_DEFAULT, true); 1469bf325f95SRafael J. Wysocki } 1470bf325f95SRafael J. Wysocki 1471778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type, 1472778cbc1dSBjorn Helgaas unsigned long long *sta) 1473778cbc1dSBjorn Helgaas { 1474778cbc1dSBjorn Helgaas acpi_status status; 1475778cbc1dSBjorn Helgaas acpi_object_type acpi_type; 1476778cbc1dSBjorn Helgaas 1477778cbc1dSBjorn Helgaas status = acpi_get_type(handle, &acpi_type); 1478778cbc1dSBjorn Helgaas if (ACPI_FAILURE(status)) 1479778cbc1dSBjorn Helgaas return -ENODEV; 1480778cbc1dSBjorn Helgaas 1481778cbc1dSBjorn Helgaas switch (acpi_type) { 1482778cbc1dSBjorn Helgaas case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */ 1483778cbc1dSBjorn Helgaas case ACPI_TYPE_DEVICE: 1484778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_DEVICE; 1485778cbc1dSBjorn Helgaas status = acpi_bus_get_status_handle(handle, sta); 1486778cbc1dSBjorn Helgaas if (ACPI_FAILURE(status)) 1487778cbc1dSBjorn Helgaas return -ENODEV; 1488778cbc1dSBjorn Helgaas break; 1489778cbc1dSBjorn Helgaas case ACPI_TYPE_PROCESSOR: 1490778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_PROCESSOR; 1491778cbc1dSBjorn Helgaas status = acpi_bus_get_status_handle(handle, sta); 1492778cbc1dSBjorn Helgaas if (ACPI_FAILURE(status)) 1493778cbc1dSBjorn Helgaas return -ENODEV; 1494778cbc1dSBjorn Helgaas break; 1495778cbc1dSBjorn Helgaas case ACPI_TYPE_THERMAL: 1496778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_THERMAL; 1497778cbc1dSBjorn Helgaas *sta = ACPI_STA_DEFAULT; 1498778cbc1dSBjorn Helgaas break; 1499778cbc1dSBjorn Helgaas case ACPI_TYPE_POWER: 1500778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_POWER; 1501778cbc1dSBjorn Helgaas *sta = ACPI_STA_DEFAULT; 1502778cbc1dSBjorn Helgaas break; 1503778cbc1dSBjorn Helgaas default: 1504778cbc1dSBjorn Helgaas return -ENODEV; 1505778cbc1dSBjorn Helgaas } 1506778cbc1dSBjorn Helgaas 1507778cbc1dSBjorn Helgaas return 0; 1508778cbc1dSBjorn Helgaas } 1509778cbc1dSBjorn Helgaas 1510e3863094SRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used, 1511e3863094SRafael J. Wysocki void *not_used, void **return_value) 15121da177e4SLinus Torvalds { 1513805d410fSRafael J. Wysocki struct acpi_device *device = NULL; 1514778cbc1dSBjorn Helgaas int type; 1515778cbc1dSBjorn Helgaas unsigned long long sta; 1516e3b87f8aSBjorn Helgaas acpi_status status; 1517778cbc1dSBjorn Helgaas int result; 15181da177e4SLinus Torvalds 15194002bf38SRafael J. Wysocki acpi_bus_get_device(handle, &device); 15204002bf38SRafael J. Wysocki if (device) 15214002bf38SRafael J. Wysocki goto out; 15224002bf38SRafael J. Wysocki 1523778cbc1dSBjorn Helgaas result = acpi_bus_type_and_status(handle, &type, &sta); 1524778cbc1dSBjorn Helgaas if (result) 152551a85fafSBjorn Helgaas return AE_OK; 15261da177e4SLinus Torvalds 1527778cbc1dSBjorn Helgaas if (!(sta & ACPI_STA_DEVICE_PRESENT) && 1528b581a7f9SRafael J. Wysocki !(sta & ACPI_STA_DEVICE_FUNCTIONING)) { 152986e4e20eSRafael J. Wysocki struct acpi_device_wakeup wakeup; 153086e4e20eSRafael J. Wysocki acpi_handle temp; 153186e4e20eSRafael J. Wysocki 153286e4e20eSRafael J. Wysocki status = acpi_get_handle(handle, "_PRW", &temp); 153386e4e20eSRafael J. Wysocki if (ACPI_SUCCESS(status)) 153486e4e20eSRafael J. Wysocki acpi_bus_extract_wakeup_device_power_package(handle, 153586e4e20eSRafael J. Wysocki &wakeup); 1536778cbc1dSBjorn Helgaas return AE_CTRL_DEPTH; 1537b581a7f9SRafael J. Wysocki } 15381da177e4SLinus Torvalds 1539f95988deSRafael J. Wysocki acpi_add_single_object(&device, handle, type, sta, 1540f95988deSRafael J. Wysocki type == ACPI_BUS_TYPE_POWER); 1541e3b87f8aSBjorn Helgaas if (!device) 154251a85fafSBjorn Helgaas return AE_CTRL_DEPTH; 15431da177e4SLinus Torvalds 1544209d3b17SRafael J. Wysocki device->flags.match_driver = true; 15453fb02738SRajesh Shah 15464002bf38SRafael J. Wysocki out: 154751a85fafSBjorn Helgaas if (!*return_value) 154851a85fafSBjorn Helgaas *return_value = device; 1549805d410fSRafael J. Wysocki 155051a85fafSBjorn Helgaas return AE_OK; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds 15530fc300b0SRafael J. Wysocki static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used, 15540fc300b0SRafael J. Wysocki void *not_used, void **ret_not_used) 1555805d410fSRafael J. Wysocki { 1556805d410fSRafael J. Wysocki acpi_status status = AE_OK; 1557805d410fSRafael J. Wysocki struct acpi_device *device; 1558805d410fSRafael J. Wysocki unsigned long long sta_not_used; 1559805d410fSRafael J. Wysocki int type_not_used; 1560805d410fSRafael J. Wysocki 1561805d410fSRafael J. Wysocki /* 1562805d410fSRafael J. Wysocki * Ignore errors ignored by acpi_bus_check_add() to avoid terminating 1563805d410fSRafael J. Wysocki * namespace walks prematurely. 1564805d410fSRafael J. Wysocki */ 1565805d410fSRafael J. Wysocki if (acpi_bus_type_and_status(handle, &type_not_used, &sta_not_used)) 1566805d410fSRafael J. Wysocki return AE_OK; 1567805d410fSRafael J. Wysocki 1568805d410fSRafael J. Wysocki if (acpi_bus_get_device(handle, &device)) 1569805d410fSRafael J. Wysocki return AE_CTRL_DEPTH; 1570805d410fSRafael J. Wysocki 1571805d410fSRafael J. Wysocki if (!acpi_match_device_ids(device, acpi_platform_device_ids)) { 1572805d410fSRafael J. Wysocki /* This is a known good platform device. */ 1573805d410fSRafael J. Wysocki acpi_create_platform_device(device); 1574abe99210SMika Westerberg } else if (device_attach(&device->dev) < 0) { 1575805d410fSRafael J. Wysocki status = AE_CTRL_DEPTH; 1576805d410fSRafael J. Wysocki } 1577805d410fSRafael J. Wysocki return status; 1578805d410fSRafael J. Wysocki } 1579805d410fSRafael J. Wysocki 1580*b8bd759aSRafael J. Wysocki /** 1581*b8bd759aSRafael J. Wysocki * acpi_bus_scan - Add ACPI device node objects in a given namespace scope. 1582*b8bd759aSRafael J. Wysocki * @handle: Root of the namespace scope to scan. 1583*b8bd759aSRafael J. Wysocki * 1584*b8bd759aSRafael J. Wysocki * Scan a given ACPI tree (probably recently hot-plugged) and create and add 1585*b8bd759aSRafael J. Wysocki * found devices. 1586*b8bd759aSRafael J. Wysocki * 1587*b8bd759aSRafael J. Wysocki * If no devices were found, -ENODEV is returned, but it does not mean that 1588*b8bd759aSRafael J. Wysocki * there has been a real error. There just have been no suitable ACPI objects 1589*b8bd759aSRafael J. Wysocki * in the table trunk from which the kernel could create a device and add an 1590*b8bd759aSRafael J. Wysocki * appropriate driver. 1591*b8bd759aSRafael J. Wysocki */ 1592*b8bd759aSRafael J. Wysocki int acpi_bus_scan(acpi_handle handle) 159351a85fafSBjorn Helgaas { 159451a85fafSBjorn Helgaas void *device = NULL; 159551a85fafSBjorn Helgaas 15960cd6ac52SRafael J. Wysocki if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device))) 159751a85fafSBjorn Helgaas acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 1598e3863094SRafael J. Wysocki acpi_bus_check_add, NULL, NULL, &device); 159951a85fafSBjorn Helgaas 1600805d410fSRafael J. Wysocki if (!device) 16010cd6ac52SRafael J. Wysocki return -ENODEV; 1602805d410fSRafael J. Wysocki 16030cd6ac52SRafael J. Wysocki if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL))) 1604805d410fSRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 16050fc300b0SRafael J. Wysocki acpi_bus_device_attach, NULL, NULL, NULL); 1606805d410fSRafael J. Wysocki 16070cd6ac52SRafael J. Wysocki return 0; 16081da177e4SLinus Torvalds } 1609*b8bd759aSRafael J. Wysocki EXPORT_SYMBOL(acpi_bus_scan); 16101da177e4SLinus Torvalds 161105404d8fSRafael J. Wysocki static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used, 161205404d8fSRafael J. Wysocki void *not_used, void **ret_not_used) 161305404d8fSRafael J. Wysocki { 161405404d8fSRafael J. Wysocki struct acpi_device *device = NULL; 161505404d8fSRafael J. Wysocki 161605404d8fSRafael J. Wysocki if (!acpi_bus_get_device(handle, &device)) { 161705404d8fSRafael J. Wysocki device->removal_type = ACPI_BUS_REMOVAL_EJECT; 161805404d8fSRafael J. Wysocki device_release_driver(&device->dev); 161905404d8fSRafael J. Wysocki } 162005404d8fSRafael J. Wysocki return AE_OK; 162105404d8fSRafael J. Wysocki } 162205404d8fSRafael J. Wysocki 162305404d8fSRafael J. Wysocki static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used, 162405404d8fSRafael J. Wysocki void *not_used, void **ret_not_used) 162505404d8fSRafael J. Wysocki { 162605404d8fSRafael J. Wysocki struct acpi_device *device = NULL; 162705404d8fSRafael J. Wysocki 162805404d8fSRafael J. Wysocki if (!acpi_bus_get_device(handle, &device)) 162905404d8fSRafael J. Wysocki acpi_device_unregister(device); 163005404d8fSRafael J. Wysocki 163105404d8fSRafael J. Wysocki return AE_OK; 163205404d8fSRafael J. Wysocki } 163305404d8fSRafael J. Wysocki 1634ae281795SRafael J. Wysocki int acpi_bus_trim(struct acpi_device *start) 16351da177e4SLinus Torvalds { 16361da177e4SLinus Torvalds /* 163705404d8fSRafael J. Wysocki * Execute acpi_bus_device_detach() as a post-order callback to detach 163805404d8fSRafael J. Wysocki * all ACPI drivers from the device nodes being removed. 163905404d8fSRafael J. Wysocki */ 164005404d8fSRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL, 164105404d8fSRafael J. Wysocki acpi_bus_device_detach, NULL, NULL); 164205404d8fSRafael J. Wysocki acpi_bus_device_detach(start->handle, 0, NULL, NULL); 164305404d8fSRafael J. Wysocki /* 1644cecdb193SRafael J. Wysocki * Execute acpi_bus_remove() as a post-order callback to remove device 1645cecdb193SRafael J. Wysocki * nodes in the given namespace scope. 16461da177e4SLinus Torvalds */ 1647cecdb193SRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL, 1648cecdb193SRafael J. Wysocki acpi_bus_remove, NULL, NULL); 1649cecdb193SRafael J. Wysocki acpi_bus_remove(start->handle, 0, NULL, NULL); 1650cecdb193SRafael J. Wysocki return 0; 16511da177e4SLinus Torvalds } 1652ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim); 1653ceaba663SKristen Accardi 1654e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void) 16551da177e4SLinus Torvalds { 16561da177e4SLinus Torvalds int result = 0; 16571da177e4SLinus Torvalds struct acpi_device *device = NULL; 1658c4168bffSLi Shaohua 16591da177e4SLinus Torvalds /* 16601da177e4SLinus Torvalds * Enumerate all fixed-feature devices. 16611da177e4SLinus Torvalds */ 1662cee324b1SAlexey Starikovskiy if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) { 16635c478f49SBjorn Helgaas result = acpi_add_single_object(&device, NULL, 1664c4168bffSLi Shaohua ACPI_BUS_TYPE_POWER_BUTTON, 1665209d3b17SRafael J. Wysocki ACPI_STA_DEFAULT, true); 1666c10d7a13SDaniel Drake device_init_wakeup(&device->dev, true); 16673fb02738SRajesh Shah } 16681da177e4SLinus Torvalds 1669cee324b1SAlexey Starikovskiy if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 16705c478f49SBjorn Helgaas result = acpi_add_single_object(&device, NULL, 1671c4168bffSLi Shaohua ACPI_BUS_TYPE_SLEEP_BUTTON, 1672209d3b17SRafael J. Wysocki ACPI_STA_DEFAULT, true); 16733fb02738SRajesh Shah } 16741da177e4SLinus Torvalds 1675d550d98dSPatrick Mochel return result; 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds 1678e747f274SBjorn Helgaas int __init acpi_scan_init(void) 16791da177e4SLinus Torvalds { 16801da177e4SLinus Torvalds int result; 16811da177e4SLinus Torvalds 16825b327265SPatrick Mochel result = bus_register(&acpi_bus_type); 16835b327265SPatrick Mochel if (result) { 16845b327265SPatrick Mochel /* We don't want to quit even if we failed to add suspend/resume */ 16855b327265SPatrick Mochel printk(KERN_ERR PREFIX "Could not register bus type\n"); 16865b327265SPatrick Mochel } 16875b327265SPatrick Mochel 168897d9a9e9SRafael J. Wysocki acpi_power_init(); 168992ef2a25SRafael J. Wysocki acpi_pci_root_init(); 169097d9a9e9SRafael J. Wysocki 16911da177e4SLinus Torvalds /* 16921da177e4SLinus Torvalds * Enumerate devices in the ACPI namespace. 16931da177e4SLinus Torvalds */ 16940cd6ac52SRafael J. Wysocki result = acpi_bus_scan(ACPI_ROOT_OBJECT); 16950cd6ac52SRafael J. Wysocki if (result) 16960cd6ac52SRafael J. Wysocki return result; 1697c04209a7SAlexey Starikovskiy 16980cd6ac52SRafael J. Wysocki result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root); 16991da177e4SLinus Torvalds if (result) 1700d550d98dSPatrick Mochel return result; 1701*b8bd759aSRafael J. Wysocki 1702*b8bd759aSRafael J. Wysocki result = acpi_bus_scan_fixed(); 1703*b8bd759aSRafael J. Wysocki if (result) { 1704*b8bd759aSRafael J. Wysocki acpi_device_unregister(acpi_root); 1705*b8bd759aSRafael J. Wysocki return result; 1706*b8bd759aSRafael J. Wysocki } 1707*b8bd759aSRafael J. Wysocki 1708*b8bd759aSRafael J. Wysocki acpi_update_all_gpes(); 1709*b8bd759aSRafael J. Wysocki return 0; 17101da177e4SLinus Torvalds } 1711