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 } 475*d43e167dSRafael J. Wysocki kfree(device->pnp.unique_id); 4767f47fa6cSBjorn Helgaas } 4777f47fa6cSBjorn Helgaas 4781890a97aSPatrick Mochel static void acpi_device_release(struct device *dev) 4791890a97aSPatrick Mochel { 4801890a97aSPatrick Mochel struct acpi_device *acpi_dev = to_acpi_device(dev); 4811890a97aSPatrick Mochel 4827f47fa6cSBjorn Helgaas acpi_free_ids(acpi_dev); 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 626*d43e167dSRafael J. Wysocki if (device->handle) { 627*d43e167dSRafael J. Wysocki acpi_status status; 628*d43e167dSRafael J. Wysocki 629*d43e167dSRafael J. Wysocki status = acpi_attach_data(device->handle, acpi_bus_data_handler, 630*d43e167dSRafael J. Wysocki device); 631*d43e167dSRafael J. Wysocki if (ACPI_FAILURE(status)) { 632*d43e167dSRafael J. Wysocki acpi_handle_err(device->handle, 633*d43e167dSRafael J. Wysocki "Unable to attach device data\n"); 634*d43e167dSRafael J. Wysocki return -ENODEV; 635*d43e167dSRafael J. Wysocki } 636*d43e167dSRafael J. Wysocki } 637*d43e167dSRafael J. Wysocki 6381da177e4SLinus Torvalds /* 6391da177e4SLinus Torvalds * Linkage 6401da177e4SLinus Torvalds * ------- 6411da177e4SLinus Torvalds * Link this device to its parent and siblings. 6421da177e4SLinus Torvalds */ 6431da177e4SLinus Torvalds INIT_LIST_HEAD(&device->children); 6441da177e4SLinus Torvalds INIT_LIST_HEAD(&device->node); 6451da177e4SLinus Torvalds INIT_LIST_HEAD(&device->wakeup_list); 6461033f904SLan Tianyu INIT_LIST_HEAD(&device->physical_node_list); 6471033f904SLan Tianyu mutex_init(&device->physical_node_lock); 648bc9b6407SRafael J. Wysocki INIT_LIST_HEAD(&device->power_dependent); 6491da177e4SLinus Torvalds 650e49bd2ddSZhang Rui new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL); 651e49bd2ddSZhang Rui if (!new_bus_id) { 652*d43e167dSRafael J. Wysocki pr_err(PREFIX "Memory allocation error\n"); 653*d43e167dSRafael J. Wysocki result = -ENOMEM; 654*d43e167dSRafael J. Wysocki goto err_detach; 655e49bd2ddSZhang Rui } 656e49bd2ddSZhang Rui 6579090589dSShaohua Li mutex_lock(&acpi_device_lock); 658e49bd2ddSZhang Rui /* 659e49bd2ddSZhang Rui * Find suitable bus_id and instance number in acpi_bus_id_list 660e49bd2ddSZhang Rui * If failed, create one and link it into acpi_bus_id_list 661e49bd2ddSZhang Rui */ 662e49bd2ddSZhang Rui list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { 6631131b938SBjorn Helgaas if (!strcmp(acpi_device_bus_id->bus_id, 6641131b938SBjorn Helgaas acpi_device_hid(device))) { 665e49bd2ddSZhang Rui acpi_device_bus_id->instance_no++; 666e49bd2ddSZhang Rui found = 1; 667e49bd2ddSZhang Rui kfree(new_bus_id); 668e49bd2ddSZhang Rui break; 669e49bd2ddSZhang Rui } 670e49bd2ddSZhang Rui } 671e49bd2ddSZhang Rui if (!found) { 672e49bd2ddSZhang Rui acpi_device_bus_id = new_bus_id; 6731131b938SBjorn Helgaas strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device)); 674e49bd2ddSZhang Rui acpi_device_bus_id->instance_no = 0; 675e49bd2ddSZhang Rui list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); 676e49bd2ddSZhang Rui } 6770794469dSKay Sievers dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no); 678e49bd2ddSZhang Rui 67933b57150SLen Brown if (device->parent) 6801da177e4SLinus Torvalds list_add_tail(&device->node, &device->parent->children); 68133b57150SLen Brown 6821da177e4SLinus Torvalds if (device->wakeup.flags.valid) 6831da177e4SLinus Torvalds list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); 6849090589dSShaohua Li mutex_unlock(&acpi_device_lock); 6851da177e4SLinus Torvalds 6861890a97aSPatrick Mochel if (device->parent) 68766b7ed40SBjorn Helgaas device->dev.parent = &device->parent->dev; 6881890a97aSPatrick Mochel device->dev.bus = &acpi_bus_type; 6891890a97aSPatrick Mochel device->dev.release = &acpi_device_release; 6908b12b922SAlex Chiang result = device_register(&device->dev); 691e49bd2ddSZhang Rui if (result) { 6928b12b922SAlex Chiang dev_err(&device->dev, "Error registering device\n"); 693*d43e167dSRafael J. Wysocki goto err; 694e49bd2ddSZhang Rui } 695f883d9dbSPatrick Mochel 696e49bd2ddSZhang Rui result = acpi_device_setup_files(device); 697e49bd2ddSZhang Rui if (result) 6980794469dSKay Sievers printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n", 6990794469dSKay Sievers dev_name(&device->dev)); 700e49bd2ddSZhang Rui 70196333578SLi Shaohua device->removal_type = ACPI_BUS_REMOVAL_NORMAL; 702e49bd2ddSZhang Rui return 0; 703*d43e167dSRafael J. Wysocki 704*d43e167dSRafael J. Wysocki err: 7059090589dSShaohua Li mutex_lock(&acpi_device_lock); 70633b57150SLen Brown if (device->parent) 707e49bd2ddSZhang Rui list_del(&device->node); 708e49bd2ddSZhang Rui list_del(&device->wakeup_list); 7099090589dSShaohua Li mutex_unlock(&acpi_device_lock); 710*d43e167dSRafael J. Wysocki 711*d43e167dSRafael J. Wysocki err_detach: 712*d43e167dSRafael J. Wysocki acpi_detach_data(device->handle, acpi_bus_data_handler); 713e49bd2ddSZhang Rui return result; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 716b17b537aSRafael J. Wysocki static void acpi_device_unregister(struct acpi_device *device) 7171da177e4SLinus Torvalds { 7189090589dSShaohua Li mutex_lock(&acpi_device_lock); 71933b57150SLen Brown if (device->parent) 7201da177e4SLinus Torvalds list_del(&device->node); 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds list_del(&device->wakeup_list); 7239090589dSShaohua Li mutex_unlock(&acpi_device_lock); 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds acpi_detach_data(device->handle, acpi_bus_data_handler); 7261890a97aSPatrick Mochel 727bc9b6407SRafael J. Wysocki acpi_power_add_remove_device(device, false); 728f883d9dbSPatrick Mochel acpi_device_remove_files(device); 7291890a97aSPatrick Mochel device_unregister(&device->dev); 730bc9b6407SRafael J. Wysocki /* 731bc9b6407SRafael J. Wysocki * Drop the reference counts of all power resources the device depends 732bc9b6407SRafael J. Wysocki * on and turn off the ones that have no more references. 733bc9b6407SRafael J. Wysocki */ 734bc9b6407SRafael J. Wysocki acpi_power_transition(device, ACPI_STATE_D3_COLD); 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 7379e89dde2SZhang Rui /* -------------------------------------------------------------------------- 7389e89dde2SZhang Rui Driver Management 7399e89dde2SZhang Rui -------------------------------------------------------------------------- */ 7401da177e4SLinus Torvalds /** 741d758a8faSRandy Dunlap * acpi_bus_driver_init - add a device to a driver 742d758a8faSRandy Dunlap * @device: the device to add and initialize 743d758a8faSRandy Dunlap * @driver: driver for the device 744d758a8faSRandy Dunlap * 7451da177e4SLinus Torvalds * Used to initialize a device via its device driver. Called whenever a 7461890a97aSPatrick Mochel * driver is bound to a device. Invokes the driver's add() ops. 7471da177e4SLinus Torvalds */ 7481da177e4SLinus Torvalds static int 7494be44fcdSLen Brown acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver) 7501da177e4SLinus Torvalds { 7511da177e4SLinus Torvalds int result = 0; 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds if (!device || !driver) 754d550d98dSPatrick Mochel return -EINVAL; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds if (!driver->ops.add) 757d550d98dSPatrick Mochel return -ENOSYS; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds result = driver->ops.add(device); 7601da177e4SLinus Torvalds if (result) { 7611da177e4SLinus Torvalds device->driver = NULL; 762db89b4f0SPavel Machek device->driver_data = NULL; 763d550d98dSPatrick Mochel return result; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds device->driver = driver; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds /* 7691da177e4SLinus Torvalds * TBD - Configuration Management: Assign resources to device based 7701da177e4SLinus Torvalds * upon possible configuration and currently allocated resources. 7711da177e4SLinus Torvalds */ 7721da177e4SLinus Torvalds 7734be44fcdSLen Brown ACPI_DEBUG_PRINT((ACPI_DB_INFO, 7744be44fcdSLen Brown "Driver successfully bound to device\n")); 775d550d98dSPatrick Mochel return 0; 7763fb02738SRajesh Shah } 7773fb02738SRajesh Shah 7781da177e4SLinus Torvalds /** 779d758a8faSRandy Dunlap * acpi_bus_register_driver - register a driver with the ACPI bus 780d758a8faSRandy Dunlap * @driver: driver being registered 781d758a8faSRandy Dunlap * 7821da177e4SLinus Torvalds * Registers a driver with the ACPI bus. Searches the namespace for all 7839d9f749bSBjorn Helgaas * devices that match the driver's criteria and binds. Returns zero for 7849d9f749bSBjorn Helgaas * success or a negative error status for failure. 7851da177e4SLinus Torvalds */ 7864be44fcdSLen Brown int acpi_bus_register_driver(struct acpi_driver *driver) 7871da177e4SLinus Torvalds { 7881890a97aSPatrick Mochel int ret; 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds if (acpi_disabled) 791d550d98dSPatrick Mochel return -ENODEV; 7921890a97aSPatrick Mochel driver->drv.name = driver->name; 7931890a97aSPatrick Mochel driver->drv.bus = &acpi_bus_type; 7941890a97aSPatrick Mochel driver->drv.owner = driver->owner; 7951da177e4SLinus Torvalds 7961890a97aSPatrick Mochel ret = driver_register(&driver->drv); 7971890a97aSPatrick Mochel return ret; 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds 8004be44fcdSLen Brown EXPORT_SYMBOL(acpi_bus_register_driver); 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds /** 803d758a8faSRandy Dunlap * acpi_bus_unregister_driver - unregisters a driver with the APIC bus 804d758a8faSRandy Dunlap * @driver: driver to unregister 805d758a8faSRandy Dunlap * 8061da177e4SLinus Torvalds * Unregisters a driver with the ACPI bus. Searches the namespace for all 8071da177e4SLinus Torvalds * devices that match the driver's criteria and unbinds. 8081da177e4SLinus Torvalds */ 80906ea8e08SBjorn Helgaas void acpi_bus_unregister_driver(struct acpi_driver *driver) 8101da177e4SLinus Torvalds { 8111890a97aSPatrick Mochel driver_unregister(&driver->drv); 8121da177e4SLinus Torvalds } 8134be44fcdSLen Brown 8141da177e4SLinus Torvalds EXPORT_SYMBOL(acpi_bus_unregister_driver); 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds /* -------------------------------------------------------------------------- 8171da177e4SLinus Torvalds Device Enumeration 8181da177e4SLinus Torvalds -------------------------------------------------------------------------- */ 8195c478f49SBjorn Helgaas static struct acpi_device *acpi_bus_get_parent(acpi_handle handle) 8205c478f49SBjorn Helgaas { 8215c478f49SBjorn Helgaas acpi_status status; 8225c478f49SBjorn Helgaas int ret; 8235c478f49SBjorn Helgaas struct acpi_device *device; 8245c478f49SBjorn Helgaas 8255c478f49SBjorn Helgaas /* 8265c478f49SBjorn Helgaas * Fixed hardware devices do not appear in the namespace and do not 8275c478f49SBjorn Helgaas * have handles, but we fabricate acpi_devices for them, so we have 8285c478f49SBjorn Helgaas * to deal with them specially. 8295c478f49SBjorn Helgaas */ 8305c478f49SBjorn Helgaas if (handle == NULL) 8315c478f49SBjorn Helgaas return acpi_root; 8325c478f49SBjorn Helgaas 8335c478f49SBjorn Helgaas do { 8345c478f49SBjorn Helgaas status = acpi_get_parent(handle, &handle); 8355c478f49SBjorn Helgaas if (status == AE_NULL_ENTRY) 8365c478f49SBjorn Helgaas return NULL; 8375c478f49SBjorn Helgaas if (ACPI_FAILURE(status)) 8385c478f49SBjorn Helgaas return acpi_root; 8395c478f49SBjorn Helgaas 8405c478f49SBjorn Helgaas ret = acpi_bus_get_device(handle, &device); 8415c478f49SBjorn Helgaas if (ret == 0) 8425c478f49SBjorn Helgaas return device; 8435c478f49SBjorn Helgaas } while (1); 8445c478f49SBjorn Helgaas } 8455c478f49SBjorn Helgaas 846c8f7a62cSLen Brown acpi_status 847c8f7a62cSLen Brown acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd) 848c8f7a62cSLen Brown { 849c8f7a62cSLen Brown acpi_status status; 850c8f7a62cSLen Brown acpi_handle tmp; 851c8f7a62cSLen Brown struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; 852c8f7a62cSLen Brown union acpi_object *obj; 853c8f7a62cSLen Brown 854c8f7a62cSLen Brown status = acpi_get_handle(handle, "_EJD", &tmp); 855c8f7a62cSLen Brown if (ACPI_FAILURE(status)) 856c8f7a62cSLen Brown return status; 857c8f7a62cSLen Brown 858c8f7a62cSLen Brown status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer); 859c8f7a62cSLen Brown if (ACPI_SUCCESS(status)) { 860c8f7a62cSLen Brown obj = buffer.pointer; 8613b5fee59SHolger Macht status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer, 8623b5fee59SHolger Macht ejd); 863c8f7a62cSLen Brown kfree(buffer.pointer); 864c8f7a62cSLen Brown } 865c8f7a62cSLen Brown return status; 866c8f7a62cSLen Brown } 867c8f7a62cSLen Brown EXPORT_SYMBOL_GPL(acpi_bus_get_ejd); 868c8f7a62cSLen Brown 8698e4319c4SBob Moore void acpi_bus_data_handler(acpi_handle handle, void *context) 8701da177e4SLinus Torvalds { 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds /* TBD */ 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds return; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds static acpi_status 878b581a7f9SRafael J. Wysocki acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, 879b581a7f9SRafael J. Wysocki struct acpi_device_wakeup *wakeup) 8801da177e4SLinus Torvalds { 881b581a7f9SRafael J. Wysocki struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 882b581a7f9SRafael J. Wysocki union acpi_object *package = NULL; 8831da177e4SLinus Torvalds union acpi_object *element = NULL; 884b581a7f9SRafael J. Wysocki acpi_status status; 885b581a7f9SRafael J. Wysocki int i = 0; 8861da177e4SLinus Torvalds 887b581a7f9SRafael J. Wysocki if (!wakeup) 8881da177e4SLinus Torvalds return AE_BAD_PARAMETER; 8891da177e4SLinus Torvalds 890b581a7f9SRafael J. Wysocki /* _PRW */ 891b581a7f9SRafael J. Wysocki status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer); 892b581a7f9SRafael J. Wysocki if (ACPI_FAILURE(status)) { 893b581a7f9SRafael J. Wysocki ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW")); 894b581a7f9SRafael J. Wysocki return status; 895b581a7f9SRafael J. Wysocki } 896b581a7f9SRafael J. Wysocki 897b581a7f9SRafael J. Wysocki package = (union acpi_object *)buffer.pointer; 898b581a7f9SRafael J. Wysocki 899b581a7f9SRafael J. Wysocki if (!package || (package->package.count < 2)) { 900b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 901b581a7f9SRafael J. Wysocki goto out; 902b581a7f9SRafael J. Wysocki } 903b581a7f9SRafael J. Wysocki 9041da177e4SLinus Torvalds element = &(package->package.elements[0]); 905b581a7f9SRafael J. Wysocki if (!element) { 906b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 907b581a7f9SRafael J. Wysocki goto out; 908b581a7f9SRafael J. Wysocki } 9091da177e4SLinus Torvalds if (element->type == ACPI_TYPE_PACKAGE) { 9101da177e4SLinus Torvalds if ((element->package.count < 2) || 9111da177e4SLinus Torvalds (element->package.elements[0].type != 9121da177e4SLinus Torvalds ACPI_TYPE_LOCAL_REFERENCE) 913b581a7f9SRafael J. Wysocki || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) { 914b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 915b581a7f9SRafael J. Wysocki goto out; 916b581a7f9SRafael J. Wysocki } 917b581a7f9SRafael J. Wysocki wakeup->gpe_device = 9181da177e4SLinus Torvalds element->package.elements[0].reference.handle; 919b581a7f9SRafael J. Wysocki wakeup->gpe_number = 9201da177e4SLinus Torvalds (u32) element->package.elements[1].integer.value; 9211da177e4SLinus Torvalds } else if (element->type == ACPI_TYPE_INTEGER) { 922b581a7f9SRafael J. Wysocki wakeup->gpe_device = NULL; 923b581a7f9SRafael J. Wysocki wakeup->gpe_number = element->integer.value; 924b581a7f9SRafael J. Wysocki } else { 925b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 926b581a7f9SRafael J. Wysocki goto out; 927b581a7f9SRafael J. Wysocki } 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds element = &(package->package.elements[1]); 9301da177e4SLinus Torvalds if (element->type != ACPI_TYPE_INTEGER) { 931b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 932b581a7f9SRafael J. Wysocki goto out; 9331da177e4SLinus Torvalds } 934b581a7f9SRafael J. Wysocki wakeup->sleep_state = element->integer.value; 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds if ((package->package.count - 2) > ACPI_MAX_HANDLES) { 937b581a7f9SRafael J. Wysocki status = AE_NO_MEMORY; 938b581a7f9SRafael J. Wysocki goto out; 9391da177e4SLinus Torvalds } 940b581a7f9SRafael J. Wysocki wakeup->resources.count = package->package.count - 2; 941b581a7f9SRafael J. Wysocki for (i = 0; i < wakeup->resources.count; i++) { 9421da177e4SLinus Torvalds element = &(package->package.elements[i + 2]); 943b581a7f9SRafael J. Wysocki if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { 944b581a7f9SRafael J. Wysocki status = AE_BAD_DATA; 945b581a7f9SRafael J. Wysocki goto out; 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds 948b581a7f9SRafael J. Wysocki wakeup->resources.handles[i] = element->reference.handle; 949b581a7f9SRafael J. Wysocki } 9509874647bSRafael J. Wysocki 951bba63a29SLin Ming acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number); 952b581a7f9SRafael J. Wysocki 953b581a7f9SRafael J. Wysocki out: 954b581a7f9SRafael J. Wysocki kfree(buffer.pointer); 955b581a7f9SRafael J. Wysocki 956b581a7f9SRafael J. Wysocki return status; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 959f517709dSRafael J. Wysocki static void acpi_bus_set_run_wake_flags(struct acpi_device *device) 9601da177e4SLinus Torvalds { 96129b71a1cSThomas Renninger struct acpi_device_id button_device_ids[] = { 96229b71a1cSThomas Renninger {"PNP0C0C", 0}, 963b7e38304SZhang Rui {"PNP0C0D", 0}, 96429b71a1cSThomas Renninger {"PNP0C0E", 0}, 96529b71a1cSThomas Renninger {"", 0}, 96629b71a1cSThomas Renninger }; 967f517709dSRafael J. Wysocki acpi_status status; 968f517709dSRafael J. Wysocki acpi_event_status event_status; 969f517709dSRafael J. Wysocki 970b67ea761SRafael J. Wysocki device->wakeup.flags.notifier_present = 0; 971f517709dSRafael J. Wysocki 972f517709dSRafael J. Wysocki /* Power button, Lid switch always enable wakeup */ 973f517709dSRafael J. Wysocki if (!acpi_match_device_ids(device, button_device_ids)) { 974f517709dSRafael J. Wysocki device->wakeup.flags.run_wake = 1; 975b7e38304SZhang Rui if (!acpi_match_device_ids(device, &button_device_ids[1])) { 976b7e38304SZhang Rui /* Do not use Lid/sleep button for S5 wakeup */ 977b7e38304SZhang Rui if (device->wakeup.sleep_state == ACPI_STATE_S5) 978b7e38304SZhang Rui device->wakeup.sleep_state = ACPI_STATE_S4; 979b7e38304SZhang Rui } 980f2b56bc8SRafael J. Wysocki device_set_wakeup_capable(&device->dev, true); 981f517709dSRafael J. Wysocki return; 982f517709dSRafael J. Wysocki } 983f517709dSRafael J. Wysocki 984e8e18c95SRafael J. Wysocki status = acpi_get_gpe_status(device->wakeup.gpe_device, 985e8e18c95SRafael J. Wysocki device->wakeup.gpe_number, 9860f849d2cSLin Ming &event_status); 987f517709dSRafael J. Wysocki if (status == AE_OK) 988f517709dSRafael J. Wysocki device->wakeup.flags.run_wake = 989f517709dSRafael J. Wysocki !!(event_status & ACPI_EVENT_FLAG_HANDLE); 990f517709dSRafael J. Wysocki } 991f517709dSRafael J. Wysocki 992d57d09a4SRafael J. Wysocki static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) 993f517709dSRafael J. Wysocki { 994d57d09a4SRafael J. Wysocki acpi_handle temp; 995f517709dSRafael J. Wysocki acpi_status status = 0; 996f517709dSRafael J. Wysocki int psw_error; 99729b71a1cSThomas Renninger 998d57d09a4SRafael J. Wysocki /* Presence of _PRW indicates wake capable */ 999d57d09a4SRafael J. Wysocki status = acpi_get_handle(device->handle, "_PRW", &temp); 1000d57d09a4SRafael J. Wysocki if (ACPI_FAILURE(status)) 1001d57d09a4SRafael J. Wysocki return; 1002d57d09a4SRafael J. Wysocki 1003b581a7f9SRafael J. Wysocki status = acpi_bus_extract_wakeup_device_power_package(device->handle, 1004b581a7f9SRafael J. Wysocki &device->wakeup); 10051da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 10061da177e4SLinus Torvalds ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package")); 1007d57d09a4SRafael J. Wysocki return; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds device->wakeup.flags.valid = 1; 10119b83ccd2SRafael J. Wysocki device->wakeup.prepare_count = 0; 1012f517709dSRafael J. Wysocki acpi_bus_set_run_wake_flags(device); 1013729b2bdbSZhao Yakui /* Call _PSW/_DSW object to disable its ability to wake the sleeping 1014729b2bdbSZhao Yakui * system for the ACPI device with the _PRW object. 1015729b2bdbSZhao Yakui * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW. 1016729b2bdbSZhao Yakui * So it is necessary to call _DSW object first. Only when it is not 1017729b2bdbSZhao Yakui * present will the _PSW object used. 1018729b2bdbSZhao Yakui */ 101977e76609SRafael J. Wysocki psw_error = acpi_device_sleep_wake(device, 0, 0, 0); 102077e76609SRafael J. Wysocki if (psw_error) 102177e76609SRafael J. Wysocki ACPI_DEBUG_PRINT((ACPI_DB_INFO, 102277e76609SRafael J. Wysocki "error in _DSW or _PSW evaluation\n")); 10231da177e4SLinus Torvalds } 10241da177e4SLinus Torvalds 1025bf325f95SRafael J. Wysocki static void acpi_bus_add_power_resource(acpi_handle handle); 1026bf325f95SRafael J. Wysocki 1027*d43e167dSRafael J. Wysocki static void acpi_bus_get_power_flags(struct acpi_device *device) 10281da177e4SLinus Torvalds { 10299e89dde2SZhang Rui acpi_status status = 0; 10309e89dde2SZhang Rui acpi_handle handle = NULL; 10319e89dde2SZhang Rui u32 i = 0; 10329e89dde2SZhang Rui 1033*d43e167dSRafael J. Wysocki /* Power resources cannot be power manageable. */ 1034*d43e167dSRafael J. Wysocki if (device->device_type == ACPI_BUS_TYPE_POWER) 1035*d43e167dSRafael J. Wysocki return; 1036*d43e167dSRafael J. Wysocki 1037*d43e167dSRafael J. Wysocki /* Presence of _PS0|_PR0 indicates 'power manageable' */ 1038*d43e167dSRafael J. Wysocki status = acpi_get_handle(device->handle, "_PS0", &handle); 1039*d43e167dSRafael J. Wysocki if (ACPI_FAILURE(status)) { 1040*d43e167dSRafael J. Wysocki status = acpi_get_handle(device->handle, "_PR0", &handle); 1041*d43e167dSRafael J. Wysocki if (ACPI_FAILURE(status)) 1042*d43e167dSRafael J. Wysocki return; 1043*d43e167dSRafael J. Wysocki } 1044*d43e167dSRafael J. Wysocki 1045*d43e167dSRafael J. Wysocki device->flags.power_manageable = 1; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds /* 10489e89dde2SZhang Rui * Power Management Flags 10491da177e4SLinus Torvalds */ 10509e89dde2SZhang Rui status = acpi_get_handle(device->handle, "_PSC", &handle); 10511da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 10529e89dde2SZhang Rui device->power.flags.explicit_get = 1; 10539e89dde2SZhang Rui status = acpi_get_handle(device->handle, "_IRC", &handle); 10549e89dde2SZhang Rui if (ACPI_SUCCESS(status)) 10559e89dde2SZhang Rui device->power.flags.inrush_current = 1; 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds /* 10589e89dde2SZhang Rui * Enumerate supported power management states 10591da177e4SLinus Torvalds */ 10601cc0c998SLin Ming for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { 10619e89dde2SZhang Rui struct acpi_device_power_state *ps = &device->power.states[i]; 10629e89dde2SZhang Rui char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' }; 10631da177e4SLinus Torvalds 10649e89dde2SZhang Rui /* Evaluate "_PRx" to se if power resources are referenced */ 10659e89dde2SZhang Rui acpi_evaluate_reference(device->handle, object_name, NULL, 10669e89dde2SZhang Rui &ps->resources); 10679e89dde2SZhang Rui if (ps->resources.count) { 1068bf325f95SRafael J. Wysocki int j; 1069bf325f95SRafael J. Wysocki 10709e89dde2SZhang Rui device->power.flags.power_resources = 1; 1071bf325f95SRafael J. Wysocki for (j = 0; j < ps->resources.count; j++) 1072bf325f95SRafael J. Wysocki acpi_bus_add_power_resource(ps->resources.handles[j]); 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 10759e89dde2SZhang Rui /* Evaluate "_PSx" to see if we can do explicit sets */ 10769e89dde2SZhang Rui object_name[2] = 'S'; 10779e89dde2SZhang Rui status = acpi_get_handle(device->handle, object_name, &handle); 107837239978SAlex He if (ACPI_SUCCESS(status)) 10799e89dde2SZhang Rui ps->flags.explicit_set = 1; 1080c8f7a62cSLen Brown 10811cc0c998SLin Ming /* 10821cc0c998SLin Ming * State is valid if there are means to put the device into it. 10831cc0c998SLin Ming * D3hot is only valid if _PR3 present. 10841cc0c998SLin Ming */ 10851cc0c998SLin Ming if (ps->resources.count || 10861399dfcdSAaron Lu (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) { 10879e89dde2SZhang Rui ps->flags.valid = 1; 10881399dfcdSAaron Lu ps->flags.os_accessible = 1; 10891399dfcdSAaron Lu } 10909e89dde2SZhang Rui 10919e89dde2SZhang Rui ps->power = -1; /* Unknown - driver assigned */ 10929e89dde2SZhang Rui ps->latency = -1; /* Unknown - driver assigned */ 10939e89dde2SZhang Rui } 10949e89dde2SZhang Rui 10959e89dde2SZhang Rui /* Set defaults for D0 and D3 states (always valid) */ 10969e89dde2SZhang Rui device->power.states[ACPI_STATE_D0].flags.valid = 1; 10979e89dde2SZhang Rui device->power.states[ACPI_STATE_D0].power = 100; 10989e89dde2SZhang Rui device->power.states[ACPI_STATE_D3].flags.valid = 1; 10999e89dde2SZhang Rui device->power.states[ACPI_STATE_D3].power = 0; 11009e89dde2SZhang Rui 11015c7dd710SRafael J. Wysocki /* Set D3cold's explicit_set flag if _PS3 exists. */ 11025c7dd710SRafael J. Wysocki if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set) 11035c7dd710SRafael J. Wysocki device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1; 11045c7dd710SRafael J. Wysocki 11051399dfcdSAaron Lu /* Presence of _PS3 or _PRx means we can put the device into D3 cold */ 11061399dfcdSAaron Lu if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set || 11071399dfcdSAaron Lu device->power.flags.power_resources) 11081399dfcdSAaron Lu device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1; 11091399dfcdSAaron Lu 1110ade3e7feSRafael J. Wysocki acpi_bus_init_power(device); 11119e89dde2SZhang Rui } 1112c8f7a62cSLen Brown 1113*d43e167dSRafael J. Wysocki static void acpi_bus_get_flags(struct acpi_device *device) 11141da177e4SLinus Torvalds { 11151da177e4SLinus Torvalds acpi_status status = AE_OK; 11161da177e4SLinus Torvalds acpi_handle temp = NULL; 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds /* Presence of _STA indicates 'dynamic_status' */ 11191da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_STA", &temp); 11201da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 11211da177e4SLinus Torvalds device->flags.dynamic_status = 1; 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds /* Presence of _RMV indicates 'removable' */ 11241da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_RMV", &temp); 11251da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 11261da177e4SLinus Torvalds device->flags.removable = 1; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds /* Presence of _EJD|_EJ0 indicates 'ejectable' */ 11291da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_EJD", &temp); 11301da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 11311da177e4SLinus Torvalds device->flags.ejectable = 1; 11321da177e4SLinus Torvalds else { 11331da177e4SLinus Torvalds status = acpi_get_handle(device->handle, "_EJ0", &temp); 11341da177e4SLinus Torvalds if (ACPI_SUCCESS(status)) 11351da177e4SLinus Torvalds device->flags.ejectable = 1; 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 1139c7bcb4e9SBjorn Helgaas static void acpi_device_get_busid(struct acpi_device *device) 11401da177e4SLinus Torvalds { 11411da177e4SLinus Torvalds char bus_id[5] = { '?', 0 }; 11421da177e4SLinus Torvalds struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; 11431da177e4SLinus Torvalds int i = 0; 11441da177e4SLinus Torvalds 11451da177e4SLinus Torvalds /* 11461da177e4SLinus Torvalds * Bus ID 11471da177e4SLinus Torvalds * ------ 11481da177e4SLinus Torvalds * The device's Bus ID is simply the object name. 11491da177e4SLinus Torvalds * TBD: Shouldn't this value be unique (within the ACPI namespace)? 11501da177e4SLinus Torvalds */ 1151859ac9a4SBjorn Helgaas if (ACPI_IS_ROOT_DEVICE(device)) { 11521da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "ACPI"); 1153859ac9a4SBjorn Helgaas return; 1154859ac9a4SBjorn Helgaas } 1155859ac9a4SBjorn Helgaas 1156859ac9a4SBjorn Helgaas switch (device->device_type) { 11571da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER_BUTTON: 11581da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "PWRF"); 11591da177e4SLinus Torvalds break; 11601da177e4SLinus Torvalds case ACPI_BUS_TYPE_SLEEP_BUTTON: 11611da177e4SLinus Torvalds strcpy(device->pnp.bus_id, "SLPF"); 11621da177e4SLinus Torvalds break; 11631da177e4SLinus Torvalds default: 116466b7ed40SBjorn Helgaas acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer); 11651da177e4SLinus Torvalds /* Clean up trailing underscores (if any) */ 11661da177e4SLinus Torvalds for (i = 3; i > 1; i--) { 11671da177e4SLinus Torvalds if (bus_id[i] == '_') 11681da177e4SLinus Torvalds bus_id[i] = '\0'; 11691da177e4SLinus Torvalds else 11701da177e4SLinus Torvalds break; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds strcpy(device->pnp.bus_id, bus_id); 11731da177e4SLinus Torvalds break; 11741da177e4SLinus Torvalds } 11751da177e4SLinus Torvalds } 11761da177e4SLinus Torvalds 117754735266SZhang Rui /* 117854735266SZhang Rui * acpi_bay_match - see if a device is an ejectable driver bay 117954735266SZhang Rui * 118054735266SZhang Rui * If an acpi object is ejectable and has one of the ACPI ATA methods defined, 118154735266SZhang Rui * then we can safely call it an ejectable drive bay 118254735266SZhang Rui */ 118354735266SZhang Rui static int acpi_bay_match(struct acpi_device *device){ 118454735266SZhang Rui acpi_status status; 118554735266SZhang Rui acpi_handle handle; 118654735266SZhang Rui acpi_handle tmp; 118754735266SZhang Rui acpi_handle phandle; 118854735266SZhang Rui 118954735266SZhang Rui handle = device->handle; 119054735266SZhang Rui 119154735266SZhang Rui status = acpi_get_handle(handle, "_EJ0", &tmp); 119254735266SZhang Rui if (ACPI_FAILURE(status)) 119354735266SZhang Rui return -ENODEV; 119454735266SZhang Rui 119554735266SZhang Rui if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) || 119654735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) || 119754735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) || 119854735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp)))) 119954735266SZhang Rui return 0; 120054735266SZhang Rui 120154735266SZhang Rui if (acpi_get_parent(handle, &phandle)) 120254735266SZhang Rui return -ENODEV; 120354735266SZhang Rui 120454735266SZhang Rui if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) || 120554735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) || 120654735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) || 120754735266SZhang Rui (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp)))) 120854735266SZhang Rui return 0; 120954735266SZhang Rui 121054735266SZhang Rui return -ENODEV; 121154735266SZhang Rui } 121254735266SZhang Rui 12133620f2f2SFrank Seidel /* 12143620f2f2SFrank Seidel * acpi_dock_match - see if a device has a _DCK method 12153620f2f2SFrank Seidel */ 12163620f2f2SFrank Seidel static int acpi_dock_match(struct acpi_device *device) 12173620f2f2SFrank Seidel { 12183620f2f2SFrank Seidel acpi_handle tmp; 12193620f2f2SFrank Seidel return acpi_get_handle(device->handle, "_DCK", &tmp); 12203620f2f2SFrank Seidel } 12213620f2f2SFrank Seidel 1222620e112cSThomas Renninger const char *acpi_device_hid(struct acpi_device *device) 122315b8dd53SBob Moore { 12247f47fa6cSBjorn Helgaas struct acpi_hardware_id *hid; 122515b8dd53SBob Moore 12262b2ae7c7SThomas Renninger if (list_empty(&device->pnp.ids)) 12272b2ae7c7SThomas Renninger return dummy_hid; 12282b2ae7c7SThomas Renninger 12297f47fa6cSBjorn Helgaas hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list); 12307f47fa6cSBjorn Helgaas return hid->id; 12317f47fa6cSBjorn Helgaas } 12327f47fa6cSBjorn Helgaas EXPORT_SYMBOL(acpi_device_hid); 123315b8dd53SBob Moore 12347f47fa6cSBjorn Helgaas static void acpi_add_id(struct acpi_device *device, const char *dev_id) 12357f47fa6cSBjorn Helgaas { 12367f47fa6cSBjorn Helgaas struct acpi_hardware_id *id; 123715b8dd53SBob Moore 12387f47fa6cSBjorn Helgaas id = kmalloc(sizeof(*id), GFP_KERNEL); 12397f47fa6cSBjorn Helgaas if (!id) 12407f47fa6cSBjorn Helgaas return; 124115b8dd53SBob Moore 1242581de59eSThomas Meyer id->id = kstrdup(dev_id, GFP_KERNEL); 12437f47fa6cSBjorn Helgaas if (!id->id) { 12447f47fa6cSBjorn Helgaas kfree(id); 12457f47fa6cSBjorn Helgaas return; 124615b8dd53SBob Moore } 124715b8dd53SBob Moore 12487f47fa6cSBjorn Helgaas list_add_tail(&id->list, &device->pnp.ids); 124915b8dd53SBob Moore } 125015b8dd53SBob Moore 1251222e82acSDarrick J. Wong /* 1252222e82acSDarrick J. Wong * Old IBM workstations have a DSDT bug wherein the SMBus object 1253222e82acSDarrick J. Wong * lacks the SMBUS01 HID and the methods do not have the necessary "_" 1254222e82acSDarrick J. Wong * prefix. Work around this. 1255222e82acSDarrick J. Wong */ 1256222e82acSDarrick J. Wong static int acpi_ibm_smbus_match(struct acpi_device *device) 1257222e82acSDarrick J. Wong { 1258222e82acSDarrick J. Wong acpi_handle h_dummy; 1259222e82acSDarrick J. Wong struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL}; 1260222e82acSDarrick J. Wong int result; 1261222e82acSDarrick J. Wong 1262222e82acSDarrick J. Wong if (!dmi_name_in_vendors("IBM")) 1263222e82acSDarrick J. Wong return -ENODEV; 1264222e82acSDarrick J. Wong 1265222e82acSDarrick J. Wong /* Look for SMBS object */ 1266222e82acSDarrick J. Wong result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path); 1267222e82acSDarrick J. Wong if (result) 1268222e82acSDarrick J. Wong return result; 1269222e82acSDarrick J. Wong 1270222e82acSDarrick J. Wong if (strcmp("SMBS", path.pointer)) { 1271222e82acSDarrick J. Wong result = -ENODEV; 1272222e82acSDarrick J. Wong goto out; 1273222e82acSDarrick J. Wong } 1274222e82acSDarrick J. Wong 1275222e82acSDarrick J. Wong /* Does it have the necessary (but misnamed) methods? */ 1276222e82acSDarrick J. Wong result = -ENODEV; 1277222e82acSDarrick J. Wong if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) && 1278222e82acSDarrick J. Wong ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) && 1279222e82acSDarrick J. Wong ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy))) 1280222e82acSDarrick J. Wong result = 0; 1281222e82acSDarrick J. Wong out: 1282222e82acSDarrick J. Wong kfree(path.pointer); 1283222e82acSDarrick J. Wong return result; 1284222e82acSDarrick J. Wong } 1285222e82acSDarrick J. Wong 1286c7bcb4e9SBjorn Helgaas static void acpi_device_set_id(struct acpi_device *device) 12871da177e4SLinus Torvalds { 12881da177e4SLinus Torvalds acpi_status status; 128957f3674fSBjorn Helgaas struct acpi_device_info *info; 129078e25fefSLv Zheng struct acpi_pnp_device_id_list *cid_list; 12917f47fa6cSBjorn Helgaas int i; 12921da177e4SLinus Torvalds 1293c7bcb4e9SBjorn Helgaas switch (device->device_type) { 12941da177e4SLinus Torvalds case ACPI_BUS_TYPE_DEVICE: 1295859ac9a4SBjorn Helgaas if (ACPI_IS_ROOT_DEVICE(device)) { 129657f3674fSBjorn Helgaas acpi_add_id(device, ACPI_SYSTEM_HID); 1297859ac9a4SBjorn Helgaas break; 1298859ac9a4SBjorn Helgaas } 1299859ac9a4SBjorn Helgaas 130066b7ed40SBjorn Helgaas status = acpi_get_object_info(device->handle, &info); 13011da177e4SLinus Torvalds if (ACPI_FAILURE(status)) { 130296b2dd1fSHarvey Harrison printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__); 13031da177e4SLinus Torvalds return; 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds if (info->valid & ACPI_VALID_HID) 130757f3674fSBjorn Helgaas acpi_add_id(device, info->hardware_id.string); 130857f3674fSBjorn Helgaas if (info->valid & ACPI_VALID_CID) { 130915b8dd53SBob Moore cid_list = &info->compatible_id_list; 131057f3674fSBjorn Helgaas for (i = 0; i < cid_list->count; i++) 131157f3674fSBjorn Helgaas acpi_add_id(device, cid_list->ids[i].string); 131257f3674fSBjorn Helgaas } 13131da177e4SLinus Torvalds if (info->valid & ACPI_VALID_ADR) { 13141da177e4SLinus Torvalds device->pnp.bus_address = info->address; 13151da177e4SLinus Torvalds device->flags.bus_address = 1; 13161da177e4SLinus Torvalds } 1317ccf78040SLv Zheng if (info->valid & ACPI_VALID_UID) 1318ccf78040SLv Zheng device->pnp.unique_id = kstrdup(info->unique_id.string, 1319ccf78040SLv Zheng GFP_KERNEL); 1320ae843332SZhang Rui 1321a83893aeSBjorn Helgaas kfree(info); 1322a83893aeSBjorn Helgaas 132357f3674fSBjorn Helgaas /* 132457f3674fSBjorn Helgaas * Some devices don't reliably have _HIDs & _CIDs, so add 132557f3674fSBjorn Helgaas * synthetic HIDs to make sure drivers can find them. 13263620f2f2SFrank Seidel */ 1327c3d6de69SThomas Renninger if (acpi_is_video_device(device)) 132857f3674fSBjorn Helgaas acpi_add_id(device, ACPI_VIDEO_HID); 13293620f2f2SFrank Seidel else if (ACPI_SUCCESS(acpi_bay_match(device))) 133057f3674fSBjorn Helgaas acpi_add_id(device, ACPI_BAY_HID); 13313620f2f2SFrank Seidel else if (ACPI_SUCCESS(acpi_dock_match(device))) 133257f3674fSBjorn Helgaas acpi_add_id(device, ACPI_DOCK_HID); 1333222e82acSDarrick J. Wong else if (!acpi_ibm_smbus_match(device)) 1334222e82acSDarrick J. Wong acpi_add_id(device, ACPI_SMBUS_IBM_HID); 13354f5f64cfSRafael J. Wysocki else if (list_empty(&device->pnp.ids) && 1336b7b30de5SBjorn Helgaas ACPI_IS_ROOT_DEVICE(device->parent)) { 1337b7b30de5SBjorn Helgaas acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */ 1338b7b30de5SBjorn Helgaas strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME); 1339b7b30de5SBjorn Helgaas strcpy(device->pnp.device_class, ACPI_BUS_CLASS); 1340b7b30de5SBjorn Helgaas } 134154735266SZhang Rui 13421da177e4SLinus Torvalds break; 13431da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER: 134457f3674fSBjorn Helgaas acpi_add_id(device, ACPI_POWER_HID); 13451da177e4SLinus Torvalds break; 13461da177e4SLinus Torvalds case ACPI_BUS_TYPE_PROCESSOR: 134757f3674fSBjorn Helgaas acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID); 13481da177e4SLinus Torvalds break; 13491da177e4SLinus Torvalds case ACPI_BUS_TYPE_THERMAL: 135057f3674fSBjorn Helgaas acpi_add_id(device, ACPI_THERMAL_HID); 13511da177e4SLinus Torvalds break; 13521da177e4SLinus Torvalds case ACPI_BUS_TYPE_POWER_BUTTON: 135357f3674fSBjorn Helgaas acpi_add_id(device, ACPI_BUTTON_HID_POWERF); 13541da177e4SLinus Torvalds break; 13551da177e4SLinus Torvalds case ACPI_BUS_TYPE_SLEEP_BUTTON: 135657f3674fSBjorn Helgaas acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF); 13571da177e4SLinus Torvalds break; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 1361*d43e167dSRafael J. Wysocki static void acpi_init_device_object(struct acpi_device *device, 1362*d43e167dSRafael J. Wysocki acpi_handle handle, 1363*d43e167dSRafael J. Wysocki int type, unsigned long long sta) 13641da177e4SLinus Torvalds { 1365*d43e167dSRafael J. Wysocki INIT_LIST_HEAD(&device->pnp.ids); 1366*d43e167dSRafael J. Wysocki device->device_type = type; 1367*d43e167dSRafael J. Wysocki device->handle = handle; 1368*d43e167dSRafael J. Wysocki device->parent = acpi_bus_get_parent(handle); 1369*d43e167dSRafael J. Wysocki STRUCT_TO_INT(device->status) = sta; 1370*d43e167dSRafael J. Wysocki acpi_device_get_busid(device); 1371*d43e167dSRafael J. Wysocki acpi_device_set_id(device); 1372*d43e167dSRafael J. Wysocki acpi_bus_get_flags(device); 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds 13755c478f49SBjorn Helgaas static int acpi_add_single_object(struct acpi_device **child, 13765c478f49SBjorn Helgaas acpi_handle handle, int type, 1377209d3b17SRafael J. Wysocki unsigned long long sta, bool match_driver) 13781da177e4SLinus Torvalds { 137977c24888SBjorn Helgaas int result; 138077c24888SBjorn Helgaas struct acpi_device *device; 138129aaefa6SBjorn Helgaas struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 13821da177e4SLinus Torvalds 138336bcbec7SBurman Yan device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); 13841da177e4SLinus Torvalds if (!device) { 13856468463aSLen Brown printk(KERN_ERR PREFIX "Memory allocation error\n"); 1386d550d98dSPatrick Mochel return -ENOMEM; 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds 1389*d43e167dSRafael J. Wysocki acpi_init_device_object(device, handle, type, sta); 1390*d43e167dSRafael J. Wysocki acpi_bus_get_power_flags(device); 1391d57d09a4SRafael J. Wysocki acpi_bus_get_wakeup_device_flags(device); 13921da177e4SLinus Torvalds 1393209d3b17SRafael J. Wysocki device->flags.match_driver = match_driver; 139466b7ed40SBjorn Helgaas result = acpi_device_register(device); 1395*d43e167dSRafael J. Wysocki if (result) { 1396*d43e167dSRafael J. Wysocki acpi_device_release(&device->dev); 1397*d43e167dSRafael J. Wysocki return result; 1398*d43e167dSRafael J. Wysocki } 13991da177e4SLinus Torvalds 1400bc9b6407SRafael J. Wysocki acpi_power_add_remove_device(device, true); 140129aaefa6SBjorn Helgaas acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); 1402*d43e167dSRafael J. Wysocki ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n", 1403*d43e167dSRafael J. Wysocki dev_name(&device->dev), (char *) buffer.pointer, 1404*d43e167dSRafael J. Wysocki device->parent ? dev_name(&device->parent->dev) : "(null)")); 140529aaefa6SBjorn Helgaas kfree(buffer.pointer); 14061da177e4SLinus Torvalds *child = device; 1407*d43e167dSRafael J. Wysocki return 0; 14081da177e4SLinus Torvalds } 14091da177e4SLinus Torvalds 1410778cbc1dSBjorn Helgaas #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \ 1411778cbc1dSBjorn Helgaas ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING) 1412778cbc1dSBjorn Helgaas 1413bf325f95SRafael J. Wysocki static void acpi_bus_add_power_resource(acpi_handle handle) 1414bf325f95SRafael J. Wysocki { 1415bf325f95SRafael J. Wysocki struct acpi_device *device = NULL; 1416bf325f95SRafael J. Wysocki 1417bf325f95SRafael J. Wysocki acpi_bus_get_device(handle, &device); 1418bf325f95SRafael J. Wysocki if (!device) 1419bf325f95SRafael J. Wysocki acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER, 1420209d3b17SRafael J. Wysocki ACPI_STA_DEFAULT, true); 1421bf325f95SRafael J. Wysocki } 1422bf325f95SRafael J. Wysocki 1423778cbc1dSBjorn Helgaas static int acpi_bus_type_and_status(acpi_handle handle, int *type, 1424778cbc1dSBjorn Helgaas unsigned long long *sta) 1425778cbc1dSBjorn Helgaas { 1426778cbc1dSBjorn Helgaas acpi_status status; 1427778cbc1dSBjorn Helgaas acpi_object_type acpi_type; 1428778cbc1dSBjorn Helgaas 1429778cbc1dSBjorn Helgaas status = acpi_get_type(handle, &acpi_type); 1430778cbc1dSBjorn Helgaas if (ACPI_FAILURE(status)) 1431778cbc1dSBjorn Helgaas return -ENODEV; 1432778cbc1dSBjorn Helgaas 1433778cbc1dSBjorn Helgaas switch (acpi_type) { 1434778cbc1dSBjorn Helgaas case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */ 1435778cbc1dSBjorn Helgaas case ACPI_TYPE_DEVICE: 1436778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_DEVICE; 1437778cbc1dSBjorn Helgaas status = acpi_bus_get_status_handle(handle, sta); 1438778cbc1dSBjorn Helgaas if (ACPI_FAILURE(status)) 1439778cbc1dSBjorn Helgaas return -ENODEV; 1440778cbc1dSBjorn Helgaas break; 1441778cbc1dSBjorn Helgaas case ACPI_TYPE_PROCESSOR: 1442778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_PROCESSOR; 1443778cbc1dSBjorn Helgaas status = acpi_bus_get_status_handle(handle, sta); 1444778cbc1dSBjorn Helgaas if (ACPI_FAILURE(status)) 1445778cbc1dSBjorn Helgaas return -ENODEV; 1446778cbc1dSBjorn Helgaas break; 1447778cbc1dSBjorn Helgaas case ACPI_TYPE_THERMAL: 1448778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_THERMAL; 1449778cbc1dSBjorn Helgaas *sta = ACPI_STA_DEFAULT; 1450778cbc1dSBjorn Helgaas break; 1451778cbc1dSBjorn Helgaas case ACPI_TYPE_POWER: 1452778cbc1dSBjorn Helgaas *type = ACPI_BUS_TYPE_POWER; 1453778cbc1dSBjorn Helgaas *sta = ACPI_STA_DEFAULT; 1454778cbc1dSBjorn Helgaas break; 1455778cbc1dSBjorn Helgaas default: 1456778cbc1dSBjorn Helgaas return -ENODEV; 1457778cbc1dSBjorn Helgaas } 1458778cbc1dSBjorn Helgaas 1459778cbc1dSBjorn Helgaas return 0; 1460778cbc1dSBjorn Helgaas } 1461778cbc1dSBjorn Helgaas 1462e3863094SRafael J. Wysocki static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used, 1463e3863094SRafael J. Wysocki void *not_used, void **return_value) 14641da177e4SLinus Torvalds { 1465805d410fSRafael J. Wysocki struct acpi_device *device = NULL; 1466778cbc1dSBjorn Helgaas int type; 1467778cbc1dSBjorn Helgaas unsigned long long sta; 1468e3b87f8aSBjorn Helgaas acpi_status status; 1469778cbc1dSBjorn Helgaas int result; 14701da177e4SLinus Torvalds 14714002bf38SRafael J. Wysocki acpi_bus_get_device(handle, &device); 14724002bf38SRafael J. Wysocki if (device) 14734002bf38SRafael J. Wysocki goto out; 14744002bf38SRafael J. Wysocki 1475778cbc1dSBjorn Helgaas result = acpi_bus_type_and_status(handle, &type, &sta); 1476778cbc1dSBjorn Helgaas if (result) 147751a85fafSBjorn Helgaas return AE_OK; 14781da177e4SLinus Torvalds 1479778cbc1dSBjorn Helgaas if (!(sta & ACPI_STA_DEVICE_PRESENT) && 1480b581a7f9SRafael J. Wysocki !(sta & ACPI_STA_DEVICE_FUNCTIONING)) { 148186e4e20eSRafael J. Wysocki struct acpi_device_wakeup wakeup; 148286e4e20eSRafael J. Wysocki acpi_handle temp; 148386e4e20eSRafael J. Wysocki 148486e4e20eSRafael J. Wysocki status = acpi_get_handle(handle, "_PRW", &temp); 148586e4e20eSRafael J. Wysocki if (ACPI_SUCCESS(status)) 148686e4e20eSRafael J. Wysocki acpi_bus_extract_wakeup_device_power_package(handle, 148786e4e20eSRafael J. Wysocki &wakeup); 1488778cbc1dSBjorn Helgaas return AE_CTRL_DEPTH; 1489b581a7f9SRafael J. Wysocki } 14901da177e4SLinus Torvalds 1491f95988deSRafael J. Wysocki acpi_add_single_object(&device, handle, type, sta, 1492f95988deSRafael J. Wysocki type == ACPI_BUS_TYPE_POWER); 1493e3b87f8aSBjorn Helgaas if (!device) 149451a85fafSBjorn Helgaas return AE_CTRL_DEPTH; 14951da177e4SLinus Torvalds 1496209d3b17SRafael J. Wysocki device->flags.match_driver = true; 14973fb02738SRajesh Shah 14984002bf38SRafael J. Wysocki out: 149951a85fafSBjorn Helgaas if (!*return_value) 150051a85fafSBjorn Helgaas *return_value = device; 1501805d410fSRafael J. Wysocki 150251a85fafSBjorn Helgaas return AE_OK; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 15050fc300b0SRafael J. Wysocki static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used, 15060fc300b0SRafael J. Wysocki void *not_used, void **ret_not_used) 150751a85fafSBjorn Helgaas { 1508805d410fSRafael J. Wysocki acpi_status status = AE_OK; 1509805d410fSRafael J. Wysocki struct acpi_device *device; 1510805d410fSRafael J. Wysocki unsigned long long sta_not_used; 1511805d410fSRafael J. Wysocki int type_not_used; 15121da177e4SLinus Torvalds 15137779688fSThomas Renninger /* 1514805d410fSRafael J. Wysocki * Ignore errors ignored by acpi_bus_check_add() to avoid terminating 1515805d410fSRafael J. Wysocki * namespace walks prematurely. 15167779688fSThomas Renninger */ 1517805d410fSRafael J. Wysocki if (acpi_bus_type_and_status(handle, &type_not_used, &sta_not_used)) 1518805d410fSRafael J. Wysocki return AE_OK; 15197779688fSThomas Renninger 1520805d410fSRafael J. Wysocki if (acpi_bus_get_device(handle, &device)) 1521805d410fSRafael J. Wysocki return AE_CTRL_DEPTH; 1522805d410fSRafael J. Wysocki 1523805d410fSRafael J. Wysocki if (!acpi_match_device_ids(device, acpi_platform_device_ids)) { 1524805d410fSRafael J. Wysocki /* This is a known good platform device. */ 1525805d410fSRafael J. Wysocki acpi_create_platform_device(device); 1526abe99210SMika Westerberg } else if (device_attach(&device->dev) < 0) { 1527805d410fSRafael J. Wysocki status = AE_CTRL_DEPTH; 1528805d410fSRafael J. Wysocki } 1529805d410fSRafael J. Wysocki return status; 1530805d410fSRafael J. Wysocki } 1531805d410fSRafael J. Wysocki 15320cd6ac52SRafael J. Wysocki static int acpi_bus_scan(acpi_handle handle) 15333fb02738SRajesh Shah { 15341da177e4SLinus Torvalds void *device = NULL; 15353fb02738SRajesh Shah 15360cd6ac52SRafael J. Wysocki if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device))) 15371da177e4SLinus Torvalds acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 1538e3863094SRafael J. Wysocki acpi_bus_check_add, NULL, NULL, &device); 1539c4168bffSLi Shaohua 1540805d410fSRafael J. Wysocki if (!device) 15410cd6ac52SRafael J. Wysocki return -ENODEV; 1542805d410fSRafael J. Wysocki 15430cd6ac52SRafael J. Wysocki if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL))) 1544805d410fSRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, 15450fc300b0SRafael J. Wysocki acpi_bus_device_attach, NULL, NULL, NULL); 1546805d410fSRafael J. Wysocki 15470cd6ac52SRafael J. Wysocki return 0; 15483fb02738SRajesh Shah } 15493fb02738SRajesh Shah 1550636458deSRafael J. Wysocki /** 1551636458deSRafael J. Wysocki * acpi_bus_add - Add ACPI device node objects in a given namespace scope. 1552636458deSRafael J. Wysocki * @handle: Root of the namespace scope to scan. 15531da177e4SLinus Torvalds * 1554636458deSRafael J. Wysocki * Scan a given ACPI tree (probably recently hot-plugged) and create and add 1555636458deSRafael J. Wysocki * found devices. 15563fb02738SRajesh Shah * 1557636458deSRafael J. Wysocki * If no devices were found, -ENODEV is returned, but it does not mean that 1558636458deSRafael J. Wysocki * there has been a real error. There just have been no suitable ACPI objects 1559636458deSRafael J. Wysocki * in the table trunk from which the kernel could create a device and add an 1560636458deSRafael J. Wysocki * appropriate driver. 15613fb02738SRajesh Shah */ 15620cd6ac52SRafael J. Wysocki int acpi_bus_add(acpi_handle handle) 15633fb02738SRajesh Shah { 156402f57c67SRafael J. Wysocki int err; 15653fb02738SRajesh Shah 15660cd6ac52SRafael J. Wysocki err = acpi_bus_scan(handle); 156702f57c67SRafael J. Wysocki if (err) 156802f57c67SRafael J. Wysocki return err; 1569a2100801SRafael J. Wysocki 15703a37898dSLin Ming acpi_update_all_gpes(); 15710fc300b0SRafael J. Wysocki return 0; 15723fb02738SRajesh Shah } 15733fb02738SRajesh Shah EXPORT_SYMBOL(acpi_bus_add); 15743fb02738SRajesh Shah 157505404d8fSRafael J. Wysocki static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used, 157605404d8fSRafael J. Wysocki void *not_used, void **ret_not_used) 15774be44fcdSLen Brown { 157805404d8fSRafael J. Wysocki struct acpi_device *device = NULL; 15793fb02738SRajesh Shah 158005404d8fSRafael J. Wysocki if (!acpi_bus_get_device(handle, &device)) { 158105404d8fSRafael J. Wysocki device->removal_type = ACPI_BUS_REMOVAL_EJECT; 158205404d8fSRafael J. Wysocki device_release_driver(&device->dev); 15833fb02738SRajesh Shah } 158405404d8fSRafael J. Wysocki return AE_OK; 158505404d8fSRafael J. Wysocki } 15861da177e4SLinus Torvalds 158705404d8fSRafael J. Wysocki static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used, 158805404d8fSRafael J. Wysocki void *not_used, void **ret_not_used) 15891da177e4SLinus Torvalds { 159005404d8fSRafael J. Wysocki struct acpi_device *device = NULL; 15911da177e4SLinus Torvalds 159205404d8fSRafael J. Wysocki if (!acpi_bus_get_device(handle, &device)) 159305404d8fSRafael J. Wysocki acpi_device_unregister(device); 15941da177e4SLinus Torvalds 159505404d8fSRafael J. Wysocki return AE_OK; 159605404d8fSRafael J. Wysocki } 15971da177e4SLinus Torvalds 1598ae281795SRafael J. Wysocki int acpi_bus_trim(struct acpi_device *start) 15991da177e4SLinus Torvalds { 16001da177e4SLinus Torvalds /* 160105404d8fSRafael J. Wysocki * Execute acpi_bus_device_detach() as a post-order callback to detach 160205404d8fSRafael J. Wysocki * all ACPI drivers from the device nodes being removed. 16031da177e4SLinus Torvalds */ 160405404d8fSRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL, 160505404d8fSRafael J. Wysocki acpi_bus_device_detach, NULL, NULL); 160605404d8fSRafael J. Wysocki acpi_bus_device_detach(start->handle, 0, NULL, NULL); 16071da177e4SLinus Torvalds /* 1608cecdb193SRafael J. Wysocki * Execute acpi_bus_remove() as a post-order callback to remove device 1609cecdb193SRafael J. Wysocki * nodes in the given namespace scope. 16101da177e4SLinus Torvalds */ 1611cecdb193SRafael J. Wysocki acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL, 1612cecdb193SRafael J. Wysocki acpi_bus_remove, NULL, NULL); 1613cecdb193SRafael J. Wysocki acpi_bus_remove(start->handle, 0, NULL, NULL); 1614cecdb193SRafael J. Wysocki return 0; 16151da177e4SLinus Torvalds } 1616ceaba663SKristen Accardi EXPORT_SYMBOL_GPL(acpi_bus_trim); 1617ceaba663SKristen Accardi 1618e8b945c9SBjorn Helgaas static int acpi_bus_scan_fixed(void) 16191da177e4SLinus Torvalds { 16201da177e4SLinus Torvalds int result = 0; 16211da177e4SLinus Torvalds struct acpi_device *device = NULL; 1622c4168bffSLi Shaohua 16231da177e4SLinus Torvalds /* 16241da177e4SLinus Torvalds * Enumerate all fixed-feature devices. 16251da177e4SLinus Torvalds */ 1626cee324b1SAlexey Starikovskiy if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) { 16275c478f49SBjorn Helgaas result = acpi_add_single_object(&device, NULL, 1628c4168bffSLi Shaohua ACPI_BUS_TYPE_POWER_BUTTON, 1629209d3b17SRafael J. Wysocki ACPI_STA_DEFAULT, true); 1630c10d7a13SDaniel Drake device_init_wakeup(&device->dev, true); 16313fb02738SRajesh Shah } 16321da177e4SLinus Torvalds 1633cee324b1SAlexey Starikovskiy if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 16345c478f49SBjorn Helgaas result = acpi_add_single_object(&device, NULL, 1635c4168bffSLi Shaohua ACPI_BUS_TYPE_SLEEP_BUTTON, 1636209d3b17SRafael J. Wysocki ACPI_STA_DEFAULT, true); 16373fb02738SRajesh Shah } 16381da177e4SLinus Torvalds 1639d550d98dSPatrick Mochel return result; 16401da177e4SLinus Torvalds } 16411da177e4SLinus Torvalds 1642e747f274SBjorn Helgaas int __init acpi_scan_init(void) 16431da177e4SLinus Torvalds { 16441da177e4SLinus Torvalds int result; 16451da177e4SLinus Torvalds 16465b327265SPatrick Mochel result = bus_register(&acpi_bus_type); 16475b327265SPatrick Mochel if (result) { 16485b327265SPatrick Mochel /* We don't want to quit even if we failed to add suspend/resume */ 16495b327265SPatrick Mochel printk(KERN_ERR PREFIX "Could not register bus type\n"); 16505b327265SPatrick Mochel } 16515b327265SPatrick Mochel 165297d9a9e9SRafael J. Wysocki acpi_power_init(); 165392ef2a25SRafael J. Wysocki acpi_pci_root_init(); 165497d9a9e9SRafael J. Wysocki 16551da177e4SLinus Torvalds /* 16561da177e4SLinus Torvalds * Enumerate devices in the ACPI namespace. 16571da177e4SLinus Torvalds */ 16580cd6ac52SRafael J. Wysocki result = acpi_bus_scan(ACPI_ROOT_OBJECT); 16590cd6ac52SRafael J. Wysocki if (result) 16600cd6ac52SRafael J. Wysocki return result; 1661c04209a7SAlexey Starikovskiy 16620cd6ac52SRafael J. Wysocki result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root); 1663c4168bffSLi Shaohua if (!result) 1664adc08e20SBjorn Helgaas result = acpi_bus_scan_fixed(); 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds if (result) 1667b17b537aSRafael J. Wysocki acpi_device_unregister(acpi_root); 1668a2100801SRafael J. Wysocki else 16693a37898dSLin Ming acpi_update_all_gpes(); 16701da177e4SLinus Torvalds 1671d550d98dSPatrick Mochel return result; 16721da177e4SLinus Torvalds } 1673