Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * scan.c - support for transforming the ACPI namespace into individual objects |
| 3 | */ |
| 4 | |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Randy Dunlap | 9b6d97b | 2006-07-12 02:08:00 -0400 | [diff] [blame] | 8 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/acpi.h> |
Alok N Kataria | 74523c9 | 2008-06-13 12:54:24 -0400 | [diff] [blame] | 10 | #include <linux/signal.h> |
| 11 | #include <linux/kthread.h> |
Darrick J. Wong | 222e82a | 2010-03-24 14:38:37 +0100 | [diff] [blame] | 12 | #include <linux/dmi.h> |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 13 | #include <linux/nls.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | #include <acpi/acpi_drivers.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Bjorn Helgaas | e60cc7a | 2009-03-13 12:08:26 -0600 | [diff] [blame] | 17 | #include "internal.h" |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #define _COMPONENT ACPI_BUS_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 20 | ACPI_MODULE_NAME("scan"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #define STRUCT_TO_INT(s) (*((int*)&s)) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 22 | extern struct acpi_device *acpi_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #define ACPI_BUS_CLASS "system_bus" |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 25 | #define ACPI_BUS_HID "LNXSYBUS" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #define ACPI_BUS_DEVICE_NAME "System Bus" |
| 27 | |
Bjorn Helgaas | 859ac9a4 | 2009-09-21 19:29:50 +0000 | [diff] [blame] | 28 | #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent) |
| 29 | |
Thomas Renninger | 620e112 | 2010-10-01 10:54:00 +0200 | [diff] [blame] | 30 | static const char *dummy_hid = "device"; |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static LIST_HEAD(acpi_device_list); |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 33 | static LIST_HEAD(acpi_bus_id_list); |
Rafael J. Wysocki | c511cc1 | 2013-01-27 21:17:29 +0100 | [diff] [blame] | 34 | static DEFINE_MUTEX(acpi_scan_lock); |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 35 | static LIST_HEAD(acpi_scan_handlers_list); |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 36 | DEFINE_MUTEX(acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | LIST_HEAD(acpi_wakeup_device_list); |
| 38 | |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 39 | struct acpi_device_bus_id{ |
Zhang Rui | bb09585 | 2007-01-04 15:03:18 +0800 | [diff] [blame] | 40 | char bus_id[15]; |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 41 | unsigned int instance_no; |
| 42 | struct list_head node; |
| 43 | }; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 44 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 45 | void acpi_scan_lock_acquire(void) |
| 46 | { |
| 47 | mutex_lock(&acpi_scan_lock); |
| 48 | } |
| 49 | EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire); |
| 50 | |
| 51 | void acpi_scan_lock_release(void) |
| 52 | { |
| 53 | mutex_unlock(&acpi_scan_lock); |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(acpi_scan_lock_release); |
| 56 | |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 57 | int acpi_scan_add_handler(struct acpi_scan_handler *handler) |
| 58 | { |
| 59 | if (!handler || !handler->attach) |
| 60 | return -EINVAL; |
| 61 | |
| 62 | list_add_tail(&handler->list_node, &acpi_scan_handlers_list); |
| 63 | return 0; |
| 64 | } |
| 65 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 66 | /* |
| 67 | * Creates hid/cid(s) string needed for modalias and uevent |
| 68 | * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get: |
| 69 | * char *modalias: "acpi:IBM0001:ACPI0001" |
| 70 | */ |
Adrian Bunk | b3e572d | 2007-08-14 23:22:35 +0200 | [diff] [blame] | 71 | static int create_modalias(struct acpi_device *acpi_dev, char *modalias, |
| 72 | int size) |
| 73 | { |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 74 | int len; |
Zhang Rui | 5c9fcb5 | 2008-03-20 16:40:32 +0800 | [diff] [blame] | 75 | int count; |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 76 | struct acpi_hardware_id *id; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 77 | |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 78 | if (list_empty(&acpi_dev->pnp.ids)) |
| 79 | return 0; |
| 80 | |
Zhang Rui | 5c9fcb5 | 2008-03-20 16:40:32 +0800 | [diff] [blame] | 81 | len = snprintf(modalias, size, "acpi:"); |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 82 | size -= len; |
| 83 | |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 84 | list_for_each_entry(id, &acpi_dev->pnp.ids, list) { |
| 85 | count = snprintf(&modalias[len], size, "%s:", id->id); |
Zhang Rui | 5c9fcb5 | 2008-03-20 16:40:32 +0800 | [diff] [blame] | 86 | if (count < 0 || count >= size) |
| 87 | return -EINVAL; |
| 88 | len += count; |
| 89 | size -= count; |
| 90 | } |
| 91 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 92 | modalias[len] = '\0'; |
| 93 | return len; |
| 94 | } |
| 95 | |
| 96 | static ssize_t |
| 97 | acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) { |
| 98 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 99 | int len; |
| 100 | |
| 101 | /* Device has no HID and no CID or string is >1024 */ |
| 102 | len = create_modalias(acpi_dev, buf, 1024); |
| 103 | if (len <= 0) |
| 104 | return 0; |
| 105 | buf[len++] = '\n'; |
| 106 | return len; |
| 107 | } |
| 108 | static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); |
| 109 | |
Toshi Kani | c4753e5 | 2012-05-23 20:25:20 -0600 | [diff] [blame] | 110 | /** |
| 111 | * acpi_bus_hot_remove_device: hot-remove a device and its children |
| 112 | * @context: struct acpi_eject_event pointer (freed in this func) |
| 113 | * |
| 114 | * Hot-remove a device and its children. This function frees up the |
| 115 | * memory space passed by arg context, so that the caller may call |
| 116 | * this function asynchronously through acpi_os_hotplug_execute(). |
| 117 | */ |
| 118 | void acpi_bus_hot_remove_device(void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 120 | struct acpi_eject_event *ej_event = context; |
Yinghai Lu | 5993c46 | 2013-01-11 22:40:41 +0000 | [diff] [blame] | 121 | struct acpi_device *device = ej_event->device; |
| 122 | acpi_handle handle = device->handle; |
Toshi Kani | b3c450c | 2012-10-26 13:38:57 +0200 | [diff] [blame] | 123 | acpi_handle temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | struct acpi_object_list arg_list; |
| 125 | union acpi_object arg; |
| 126 | acpi_status status = AE_OK; |
Toshi Kani | c4753e5 | 2012-05-23 20:25:20 -0600 | [diff] [blame] | 127 | u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Rafael J. Wysocki | f058cdf | 2013-02-09 15:29:11 +0100 | [diff] [blame] | 129 | mutex_lock(&acpi_scan_lock); |
| 130 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 131 | /* If there is no handle, the device node has been unregistered. */ |
| 132 | if (!device->handle) { |
| 133 | dev_dbg(&device->dev, "ACPI handle missing\n"); |
| 134 | put_device(&device->dev); |
| 135 | goto out; |
| 136 | } |
| 137 | |
Zhang Rui | 26d4686 | 2008-04-29 02:35:48 -0400 | [diff] [blame] | 138 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Kay Sievers | 0794469 | 2008-10-30 01:18:59 +0100 | [diff] [blame] | 139 | "Hot-removing device %s...\n", dev_name(&device->dev))); |
Zhang Rui | 26d4686 | 2008-04-29 02:35:48 -0400 | [diff] [blame] | 140 | |
Rafael J. Wysocki | bfee26d | 2013-01-26 00:27:44 +0100 | [diff] [blame] | 141 | acpi_bus_trim(device); |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 142 | /* Device node has been unregistered. */ |
| 143 | put_device(&device->dev); |
Toshi Kani | b3c450c | 2012-10-26 13:38:57 +0200 | [diff] [blame] | 144 | device = NULL; |
| 145 | |
Toshi Kani | b3c450c | 2012-10-26 13:38:57 +0200 | [diff] [blame] | 146 | if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | arg_list.count = 1; |
| 148 | arg_list.pointer = &arg; |
| 149 | arg.type = ACPI_TYPE_INTEGER; |
| 150 | arg.integer.value = 0; |
| 151 | acpi_evaluate_object(handle, "_LCK", &arg_list, NULL); |
| 152 | } |
| 153 | |
| 154 | arg_list.count = 1; |
| 155 | arg_list.pointer = &arg; |
| 156 | arg.type = ACPI_TYPE_INTEGER; |
| 157 | arg.integer.value = 1; |
| 158 | |
| 159 | /* |
| 160 | * TBD: _EJD support. |
| 161 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL); |
Toshi Kani | c4753e5 | 2012-05-23 20:25:20 -0600 | [diff] [blame] | 163 | if (ACPI_FAILURE(status)) { |
| 164 | if (status != AE_NOT_FOUND) |
Rafael J. Wysocki | f058cdf | 2013-02-09 15:29:11 +0100 | [diff] [blame] | 165 | acpi_handle_warn(handle, "Eject failed\n"); |
| 166 | |
| 167 | /* Tell the firmware the hot-remove operation has failed. */ |
| 168 | acpi_evaluate_hotplug_ost(handle, ej_event->event, |
| 169 | ost_code, NULL); |
Toshi Kani | c4753e5 | 2012-05-23 20:25:20 -0600 | [diff] [blame] | 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 172 | out: |
Rafael J. Wysocki | f058cdf | 2013-02-09 15:29:11 +0100 | [diff] [blame] | 173 | mutex_unlock(&acpi_scan_lock); |
Toshi Kani | c4753e5 | 2012-05-23 20:25:20 -0600 | [diff] [blame] | 174 | kfree(context); |
Zhang Rui | c8d72a5 | 2009-06-22 11:31:16 +0800 | [diff] [blame] | 175 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Toshi Kani | 61622ac | 2012-11-01 14:42:12 +0000 | [diff] [blame] | 177 | EXPORT_SYMBOL(acpi_bus_hot_remove_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Rafael J. Wysocki | 836aedb | 2013-01-24 12:49:59 +0100 | [diff] [blame] | 179 | static ssize_t real_power_state_show(struct device *dev, |
| 180 | struct device_attribute *attr, char *buf) |
| 181 | { |
| 182 | struct acpi_device *adev = to_acpi_device(dev); |
| 183 | int state; |
| 184 | int ret; |
| 185 | |
| 186 | ret = acpi_device_get_power(adev, &state); |
| 187 | if (ret) |
| 188 | return ret; |
| 189 | |
| 190 | return sprintf(buf, "%s\n", acpi_power_state_string(state)); |
| 191 | } |
| 192 | |
| 193 | static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL); |
| 194 | |
| 195 | static ssize_t power_state_show(struct device *dev, |
| 196 | struct device_attribute *attr, char *buf) |
| 197 | { |
| 198 | struct acpi_device *adev = to_acpi_device(dev); |
| 199 | |
| 200 | return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state)); |
| 201 | } |
| 202 | |
| 203 | static DEVICE_ATTR(power_state, 0444, power_state_show, NULL); |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | static ssize_t |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 206 | acpi_eject_store(struct device *d, struct device_attribute *attr, |
| 207 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 209 | int ret = count; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 210 | acpi_status status; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 211 | acpi_object_type type = 0; |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 212 | struct acpi_device *acpi_device = to_acpi_device(d); |
Toshi Kani | c4753e5 | 2012-05-23 20:25:20 -0600 | [diff] [blame] | 213 | struct acpi_eject_event *ej_event; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | if ((!count) || (buf[0] != '1')) { |
| 216 | return -EINVAL; |
| 217 | } |
Toshi Kani | ce7685a | 2013-02-07 12:50:53 +0100 | [diff] [blame] | 218 | if (!acpi_device->driver && !acpi_device->handler) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | ret = -ENODEV; |
| 220 | goto err; |
| 221 | } |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 222 | status = acpi_get_type(acpi_device->handle, &type); |
| 223 | if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | ret = -ENODEV; |
| 225 | goto err; |
| 226 | } |
| 227 | |
Toshi Kani | c4753e5 | 2012-05-23 20:25:20 -0600 | [diff] [blame] | 228 | ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL); |
| 229 | if (!ej_event) { |
| 230 | ret = -ENOMEM; |
| 231 | goto err; |
| 232 | } |
| 233 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 234 | get_device(&acpi_device->dev); |
Yinghai Lu | 5993c46 | 2013-01-11 22:40:41 +0000 | [diff] [blame] | 235 | ej_event->device = acpi_device; |
Toshi Kani | c4753e5 | 2012-05-23 20:25:20 -0600 | [diff] [blame] | 236 | if (acpi_device->flags.eject_pending) { |
| 237 | /* event originated from ACPI eject notification */ |
| 238 | ej_event->event = ACPI_NOTIFY_EJECT_REQUEST; |
| 239 | acpi_device->flags.eject_pending = 0; |
| 240 | } else { |
| 241 | /* event originated from user */ |
| 242 | ej_event->event = ACPI_OST_EC_OSPM_EJECT; |
Yinghai Lu | 5993c46 | 2013-01-11 22:40:41 +0000 | [diff] [blame] | 243 | (void) acpi_evaluate_hotplug_ost(acpi_device->handle, |
Toshi Kani | c4753e5 | 2012-05-23 20:25:20 -0600 | [diff] [blame] | 244 | ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL); |
| 245 | } |
| 246 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 247 | status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event); |
| 248 | if (ACPI_FAILURE(status)) { |
| 249 | put_device(&acpi_device->dev); |
| 250 | kfree(ej_event); |
| 251 | } |
Alok N Kataria | 74523c9 | 2008-06-13 12:54:24 -0400 | [diff] [blame] | 252 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 256 | static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 258 | static ssize_t |
| 259 | acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) { |
| 260 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Bjorn Helgaas | ea8d82f | 2009-09-21 13:35:09 -0600 | [diff] [blame] | 262 | return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev)); |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 263 | } |
| 264 | static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL); |
| 265 | |
Lv Zheng | 923d4a4 | 2012-10-30 14:43:26 +0100 | [diff] [blame] | 266 | static ssize_t acpi_device_uid_show(struct device *dev, |
| 267 | struct device_attribute *attr, char *buf) |
| 268 | { |
| 269 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 270 | |
| 271 | return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id); |
| 272 | } |
| 273 | static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL); |
| 274 | |
| 275 | static ssize_t acpi_device_adr_show(struct device *dev, |
| 276 | struct device_attribute *attr, char *buf) |
| 277 | { |
| 278 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 279 | |
| 280 | return sprintf(buf, "0x%08x\n", |
| 281 | (unsigned int)(acpi_dev->pnp.bus_address)); |
| 282 | } |
| 283 | static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL); |
| 284 | |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 285 | static ssize_t |
| 286 | acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) { |
| 287 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 288 | struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 289 | int result; |
| 290 | |
| 291 | result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 292 | if (result) |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 293 | goto end; |
| 294 | |
| 295 | result = sprintf(buf, "%s\n", (char*)path.pointer); |
| 296 | kfree(path.pointer); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 297 | end: |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 298 | return result; |
| 299 | } |
| 300 | static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL); |
| 301 | |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 302 | /* sysfs file that shows description text from the ACPI _STR method */ |
| 303 | static ssize_t description_show(struct device *dev, |
| 304 | struct device_attribute *attr, |
| 305 | char *buf) { |
| 306 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 307 | int result; |
| 308 | |
| 309 | if (acpi_dev->pnp.str_obj == NULL) |
| 310 | return 0; |
| 311 | |
| 312 | /* |
| 313 | * The _STR object contains a Unicode identifier for a device. |
| 314 | * We need to convert to utf-8 so it can be displayed. |
| 315 | */ |
| 316 | result = utf16s_to_utf8s( |
| 317 | (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer, |
| 318 | acpi_dev->pnp.str_obj->buffer.length, |
| 319 | UTF16_LITTLE_ENDIAN, buf, |
| 320 | PAGE_SIZE); |
| 321 | |
| 322 | buf[result++] = '\n'; |
| 323 | |
| 324 | return result; |
| 325 | } |
| 326 | static DEVICE_ATTR(description, 0444, description_show, NULL); |
| 327 | |
Yasuaki Ishimatsu | bb74ac2 | 2012-11-16 02:56:59 +0100 | [diff] [blame] | 328 | static ssize_t |
| 329 | acpi_device_sun_show(struct device *dev, struct device_attribute *attr, |
| 330 | char *buf) { |
| 331 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 332 | |
| 333 | return sprintf(buf, "%lu\n", acpi_dev->pnp.sun); |
| 334 | } |
| 335 | static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL); |
| 336 | |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 337 | static int acpi_device_setup_files(struct acpi_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 339 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 340 | acpi_status status; |
| 341 | acpi_handle temp; |
Yasuaki Ishimatsu | bb74ac2 | 2012-11-16 02:56:59 +0100 | [diff] [blame] | 342 | unsigned long long sun; |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 343 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 345 | /* |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 346 | * Devices gotten from FADT don't have a "path" attribute |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 347 | */ |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 348 | if (dev->handle) { |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 349 | result = device_create_file(&dev->dev, &dev_attr_path); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 350 | if (result) |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 351 | goto end; |
| 352 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 354 | if (!list_empty(&dev->pnp.ids)) { |
| 355 | result = device_create_file(&dev->dev, &dev_attr_hid); |
| 356 | if (result) |
| 357 | goto end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 359 | result = device_create_file(&dev->dev, &dev_attr_modalias); |
| 360 | if (result) |
| 361 | goto end; |
| 362 | } |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 363 | |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 364 | /* |
| 365 | * If device has _STR, 'description' file is created |
| 366 | */ |
| 367 | status = acpi_get_handle(dev->handle, "_STR", &temp); |
| 368 | if (ACPI_SUCCESS(status)) { |
| 369 | status = acpi_evaluate_object(dev->handle, "_STR", |
| 370 | NULL, &buffer); |
| 371 | if (ACPI_FAILURE(status)) |
| 372 | buffer.pointer = NULL; |
| 373 | dev->pnp.str_obj = buffer.pointer; |
| 374 | result = device_create_file(&dev->dev, &dev_attr_description); |
| 375 | if (result) |
| 376 | goto end; |
| 377 | } |
| 378 | |
Lv Zheng | 923d4a4 | 2012-10-30 14:43:26 +0100 | [diff] [blame] | 379 | if (dev->flags.bus_address) |
| 380 | result = device_create_file(&dev->dev, &dev_attr_adr); |
| 381 | if (dev->pnp.unique_id) |
| 382 | result = device_create_file(&dev->dev, &dev_attr_uid); |
| 383 | |
Yasuaki Ishimatsu | bb74ac2 | 2012-11-16 02:56:59 +0100 | [diff] [blame] | 384 | status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun); |
| 385 | if (ACPI_SUCCESS(status)) { |
| 386 | dev->pnp.sun = (unsigned long)sun; |
| 387 | result = device_create_file(&dev->dev, &dev_attr_sun); |
| 388 | if (result) |
| 389 | goto end; |
| 390 | } else { |
| 391 | dev->pnp.sun = (unsigned long)-1; |
| 392 | } |
| 393 | |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 394 | /* |
| 395 | * If device has _EJ0, 'eject' file is created that is used to trigger |
| 396 | * hot-removal function from userland. |
| 397 | */ |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 398 | status = acpi_get_handle(dev->handle, "_EJ0", &temp); |
Rafael J. Wysocki | 836aedb | 2013-01-24 12:49:59 +0100 | [diff] [blame] | 399 | if (ACPI_SUCCESS(status)) { |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 400 | result = device_create_file(&dev->dev, &dev_attr_eject); |
Rafael J. Wysocki | 836aedb | 2013-01-24 12:49:59 +0100 | [diff] [blame] | 401 | if (result) |
| 402 | return result; |
| 403 | } |
| 404 | |
| 405 | if (dev->flags.power_manageable) { |
| 406 | result = device_create_file(&dev->dev, &dev_attr_power_state); |
| 407 | if (result) |
| 408 | return result; |
| 409 | |
| 410 | if (dev->power.flags.power_resources) |
| 411 | result = device_create_file(&dev->dev, |
| 412 | &dev_attr_real_power_state); |
| 413 | } |
| 414 | |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 415 | end: |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 416 | return result; |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 417 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 419 | static void acpi_device_remove_files(struct acpi_device *dev) |
| 420 | { |
| 421 | acpi_status status; |
| 422 | acpi_handle temp; |
| 423 | |
Rafael J. Wysocki | 836aedb | 2013-01-24 12:49:59 +0100 | [diff] [blame] | 424 | if (dev->flags.power_manageable) { |
| 425 | device_remove_file(&dev->dev, &dev_attr_power_state); |
| 426 | if (dev->power.flags.power_resources) |
| 427 | device_remove_file(&dev->dev, |
| 428 | &dev_attr_real_power_state); |
| 429 | } |
| 430 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 431 | /* |
Lance Ortiz | d1efe3c | 2012-10-02 12:43:23 -0600 | [diff] [blame] | 432 | * If device has _STR, remove 'description' file |
| 433 | */ |
| 434 | status = acpi_get_handle(dev->handle, "_STR", &temp); |
| 435 | if (ACPI_SUCCESS(status)) { |
| 436 | kfree(dev->pnp.str_obj); |
| 437 | device_remove_file(&dev->dev, &dev_attr_description); |
| 438 | } |
| 439 | /* |
| 440 | * If device has _EJ0, remove 'eject' file. |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 441 | */ |
| 442 | status = acpi_get_handle(dev->handle, "_EJ0", &temp); |
| 443 | if (ACPI_SUCCESS(status)) |
| 444 | device_remove_file(&dev->dev, &dev_attr_eject); |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 445 | |
Yasuaki Ishimatsu | bb74ac2 | 2012-11-16 02:56:59 +0100 | [diff] [blame] | 446 | status = acpi_get_handle(dev->handle, "_SUN", &temp); |
| 447 | if (ACPI_SUCCESS(status)) |
| 448 | device_remove_file(&dev->dev, &dev_attr_sun); |
| 449 | |
Lv Zheng | 923d4a4 | 2012-10-30 14:43:26 +0100 | [diff] [blame] | 450 | if (dev->pnp.unique_id) |
| 451 | device_remove_file(&dev->dev, &dev_attr_uid); |
| 452 | if (dev->flags.bus_address) |
| 453 | device_remove_file(&dev->dev, &dev_attr_adr); |
Bjorn Helgaas | 1131b93 | 2009-09-21 13:35:29 -0600 | [diff] [blame] | 454 | device_remove_file(&dev->dev, &dev_attr_modalias); |
| 455 | device_remove_file(&dev->dev, &dev_attr_hid); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 456 | if (dev->handle) |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 457 | device_remove_file(&dev->dev, &dev_attr_path); |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 458 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | /* -------------------------------------------------------------------------- |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 460 | ACPI Bus operations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | -------------------------------------------------------------------------- */ |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 462 | |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 463 | static const struct acpi_device_id *__acpi_match_device( |
| 464 | struct acpi_device *device, const struct acpi_device_id *ids) |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 465 | { |
| 466 | const struct acpi_device_id *id; |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 467 | struct acpi_hardware_id *hwid; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 468 | |
Zhao Yakui | 39a0ad8 | 2008-08-11 13:40:22 +0800 | [diff] [blame] | 469 | /* |
| 470 | * If the device is not present, it is unnecessary to load device |
| 471 | * driver for it. |
| 472 | */ |
| 473 | if (!device->status.present) |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 474 | return NULL; |
Zhao Yakui | 39a0ad8 | 2008-08-11 13:40:22 +0800 | [diff] [blame] | 475 | |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 476 | for (id = ids; id->id[0]; id++) |
| 477 | list_for_each_entry(hwid, &device->pnp.ids, list) |
| 478 | if (!strcmp((char *) id->id, hwid->id)) |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 479 | return id; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 480 | |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 481 | return NULL; |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * acpi_match_device - Match a struct device against a given list of ACPI IDs |
| 486 | * @ids: Array of struct acpi_device_id object to match against. |
| 487 | * @dev: The device structure to match. |
| 488 | * |
| 489 | * Check if @dev has a valid ACPI handle and if there is a struct acpi_device |
| 490 | * object for that handle and use that object to match against a given list of |
| 491 | * device IDs. |
| 492 | * |
| 493 | * Return a pointer to the first matching ID on success or %NULL on failure. |
| 494 | */ |
| 495 | const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids, |
| 496 | const struct device *dev) |
| 497 | { |
| 498 | struct acpi_device *adev; |
Rafael J. Wysocki | 0613e1f | 2013-01-31 20:54:05 +0100 | [diff] [blame] | 499 | acpi_handle handle = ACPI_HANDLE(dev); |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 500 | |
Rafael J. Wysocki | 0613e1f | 2013-01-31 20:54:05 +0100 | [diff] [blame] | 501 | if (!ids || !handle || acpi_bus_get_device(handle, &adev)) |
Mika Westerberg | cf761af | 2012-10-31 22:44:41 +0100 | [diff] [blame] | 502 | return NULL; |
| 503 | |
| 504 | return __acpi_match_device(adev, ids); |
| 505 | } |
| 506 | EXPORT_SYMBOL_GPL(acpi_match_device); |
| 507 | |
| 508 | int acpi_match_device_ids(struct acpi_device *device, |
| 509 | const struct acpi_device_id *ids) |
| 510 | { |
| 511 | return __acpi_match_device(device, ids) ? 0 : -ENOENT; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 512 | } |
| 513 | EXPORT_SYMBOL(acpi_match_device_ids); |
| 514 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 515 | void acpi_free_ids(struct acpi_device *device) |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 516 | { |
| 517 | struct acpi_hardware_id *id, *tmp; |
| 518 | |
| 519 | list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) { |
| 520 | kfree(id->id); |
| 521 | kfree(id); |
| 522 | } |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 523 | kfree(device->pnp.unique_id); |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 524 | } |
| 525 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 526 | static void acpi_free_power_resources_lists(struct acpi_device *device) |
| 527 | { |
| 528 | int i; |
| 529 | |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 530 | if (device->wakeup.flags.valid) |
| 531 | acpi_power_resources_list_free(&device->wakeup.resources); |
| 532 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 533 | if (!device->flags.power_manageable) |
| 534 | return; |
| 535 | |
| 536 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) { |
| 537 | struct acpi_device_power_state *ps = &device->power.states[i]; |
| 538 | acpi_power_resources_list_free(&ps->resources); |
| 539 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 542 | static void acpi_device_release(struct device *dev) |
| 543 | { |
| 544 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 545 | |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 546 | acpi_free_ids(acpi_dev); |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 547 | acpi_free_power_resources_lists(acpi_dev); |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 548 | kfree(acpi_dev); |
| 549 | } |
| 550 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 551 | static int acpi_bus_match(struct device *dev, struct device_driver *drv) |
| 552 | { |
| 553 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 554 | struct acpi_driver *acpi_drv = to_acpi_driver(drv); |
| 555 | |
Rafael J. Wysocki | 209d3b1 | 2012-12-21 00:36:48 +0100 | [diff] [blame] | 556 | return acpi_dev->flags.match_driver |
Rafael J. Wysocki | 805d410f | 2012-12-21 00:36:39 +0100 | [diff] [blame] | 557 | && !acpi_match_device_ids(acpi_dev, acpi_drv->ids); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 558 | } |
| 559 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 560 | static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 561 | { |
| 562 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 563 | int len; |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 564 | |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 565 | if (list_empty(&acpi_dev->pnp.ids)) |
| 566 | return 0; |
| 567 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 568 | if (add_uevent_var(env, "MODALIAS=")) |
| 569 | return -ENOMEM; |
| 570 | len = create_modalias(acpi_dev, &env->buf[env->buflen - 1], |
| 571 | sizeof(env->buf) - env->buflen); |
| 572 | if (len >= (sizeof(env->buf) - env->buflen)) |
| 573 | return -ENOMEM; |
| 574 | env->buflen += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | return 0; |
| 576 | } |
| 577 | |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 578 | static void acpi_device_notify(acpi_handle handle, u32 event, void *data) |
| 579 | { |
| 580 | struct acpi_device *device = data; |
| 581 | |
| 582 | device->driver->ops.notify(device, event); |
| 583 | } |
| 584 | |
| 585 | static acpi_status acpi_device_notify_fixed(void *data) |
| 586 | { |
| 587 | struct acpi_device *device = data; |
| 588 | |
Bjorn Helgaas | 53de535 | 2009-08-31 22:32:20 +0000 | [diff] [blame] | 589 | /* Fixed hardware devices have no handles */ |
| 590 | acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device); |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 591 | return AE_OK; |
| 592 | } |
| 593 | |
| 594 | static int acpi_device_install_notify_handler(struct acpi_device *device) |
| 595 | { |
| 596 | acpi_status status; |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 597 | |
Bjorn Helgaas | ccba2a3 | 2009-09-21 19:29:15 +0000 | [diff] [blame] | 598 | if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON) |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 599 | status = |
| 600 | acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, |
| 601 | acpi_device_notify_fixed, |
| 602 | device); |
Bjorn Helgaas | ccba2a3 | 2009-09-21 19:29:15 +0000 | [diff] [blame] | 603 | else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 604 | status = |
| 605 | acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, |
| 606 | acpi_device_notify_fixed, |
| 607 | device); |
| 608 | else |
| 609 | status = acpi_install_notify_handler(device->handle, |
| 610 | ACPI_DEVICE_NOTIFY, |
| 611 | acpi_device_notify, |
| 612 | device); |
| 613 | |
| 614 | if (ACPI_FAILURE(status)) |
| 615 | return -EINVAL; |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static void acpi_device_remove_notify_handler(struct acpi_device *device) |
| 620 | { |
Bjorn Helgaas | ccba2a3 | 2009-09-21 19:29:15 +0000 | [diff] [blame] | 621 | if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON) |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 622 | acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, |
| 623 | acpi_device_notify_fixed); |
Bjorn Helgaas | ccba2a3 | 2009-09-21 19:29:15 +0000 | [diff] [blame] | 624 | else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 625 | acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, |
| 626 | acpi_device_notify_fixed); |
| 627 | else |
| 628 | acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY, |
| 629 | acpi_device_notify); |
| 630 | } |
| 631 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 632 | static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 633 | static int acpi_device_probe(struct device * dev) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 634 | { |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 635 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 636 | struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver); |
| 637 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 639 | ret = acpi_bus_driver_init(acpi_dev, acpi_drv); |
| 640 | if (!ret) { |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 641 | if (acpi_drv->ops.notify) { |
| 642 | ret = acpi_device_install_notify_handler(acpi_dev); |
| 643 | if (ret) { |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 644 | if (acpi_drv->ops.remove) |
Rafael J. Wysocki | 51fac83 | 2013-01-24 00:24:48 +0100 | [diff] [blame] | 645 | acpi_drv->ops.remove(acpi_dev); |
Toshi Kani | 5f27ee8 | 2013-02-07 21:19:13 +0000 | [diff] [blame] | 646 | acpi_dev->driver = NULL; |
| 647 | acpi_dev->driver_data = NULL; |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 648 | return ret; |
| 649 | } |
| 650 | } |
| 651 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 652 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 653 | "Found driver [%s] for device [%s]\n", |
| 654 | acpi_drv->name, acpi_dev->pnp.bus_id)); |
| 655 | get_device(dev); |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 656 | } |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | static int acpi_device_remove(struct device * dev) |
| 661 | { |
| 662 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 663 | struct acpi_driver *acpi_drv = acpi_dev->driver; |
| 664 | |
| 665 | if (acpi_drv) { |
Bjorn Helgaas | 46ec859 | 2009-03-30 17:48:13 +0000 | [diff] [blame] | 666 | if (acpi_drv->ops.notify) |
| 667 | acpi_device_remove_notify_handler(acpi_dev); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 668 | if (acpi_drv->ops.remove) |
Rafael J. Wysocki | 51fac83 | 2013-01-24 00:24:48 +0100 | [diff] [blame] | 669 | acpi_drv->ops.remove(acpi_dev); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 670 | } |
| 671 | acpi_dev->driver = NULL; |
Pavel Machek | db89b4f | 2008-09-22 14:37:34 -0700 | [diff] [blame] | 672 | acpi_dev->driver_data = NULL; |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 673 | |
| 674 | put_device(dev); |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 675 | return 0; |
| 676 | } |
| 677 | |
David Brownell | 55955aa | 2007-05-08 00:28:35 -0700 | [diff] [blame] | 678 | struct bus_type acpi_bus_type = { |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 679 | .name = "acpi", |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 680 | .match = acpi_bus_match, |
| 681 | .probe = acpi_device_probe, |
| 682 | .remove = acpi_device_remove, |
| 683 | .uevent = acpi_device_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | }; |
| 685 | |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 686 | int acpi_device_add(struct acpi_device *device, |
| 687 | void (*release)(struct device *)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | { |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 689 | int result; |
| 690 | struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id; |
| 691 | int found = 0; |
Bjorn Helgaas | 66b7ed4 | 2009-09-21 19:29:05 +0000 | [diff] [blame] | 692 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 693 | if (device->handle) { |
| 694 | acpi_status status; |
| 695 | |
| 696 | status = acpi_attach_data(device->handle, acpi_bus_data_handler, |
| 697 | device); |
| 698 | if (ACPI_FAILURE(status)) { |
| 699 | acpi_handle_err(device->handle, |
| 700 | "Unable to attach device data\n"); |
| 701 | return -ENODEV; |
| 702 | } |
| 703 | } |
| 704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | /* |
| 706 | * Linkage |
| 707 | * ------- |
| 708 | * Link this device to its parent and siblings. |
| 709 | */ |
| 710 | INIT_LIST_HEAD(&device->children); |
| 711 | INIT_LIST_HEAD(&device->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | INIT_LIST_HEAD(&device->wakeup_list); |
Lan Tianyu | 1033f90 | 2012-08-17 14:44:09 +0800 | [diff] [blame] | 713 | INIT_LIST_HEAD(&device->physical_node_list); |
| 714 | mutex_init(&device->physical_node_lock); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 715 | INIT_LIST_HEAD(&device->power_dependent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 717 | new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL); |
| 718 | if (!new_bus_id) { |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 719 | pr_err(PREFIX "Memory allocation error\n"); |
| 720 | result = -ENOMEM; |
| 721 | goto err_detach; |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 722 | } |
| 723 | |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 724 | mutex_lock(&acpi_device_lock); |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 725 | /* |
| 726 | * Find suitable bus_id and instance number in acpi_bus_id_list |
| 727 | * If failed, create one and link it into acpi_bus_id_list |
| 728 | */ |
| 729 | list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { |
Bjorn Helgaas | 1131b93 | 2009-09-21 13:35:29 -0600 | [diff] [blame] | 730 | if (!strcmp(acpi_device_bus_id->bus_id, |
| 731 | acpi_device_hid(device))) { |
| 732 | acpi_device_bus_id->instance_no++; |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 733 | found = 1; |
| 734 | kfree(new_bus_id); |
| 735 | break; |
| 736 | } |
| 737 | } |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 738 | if (!found) { |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 739 | acpi_device_bus_id = new_bus_id; |
Bjorn Helgaas | 1131b93 | 2009-09-21 13:35:29 -0600 | [diff] [blame] | 740 | strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device)); |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 741 | acpi_device_bus_id->instance_no = 0; |
| 742 | list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); |
| 743 | } |
Kay Sievers | 0794469 | 2008-10-30 01:18:59 +0100 | [diff] [blame] | 744 | dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no); |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 745 | |
Len Brown | 33b5715 | 2008-12-15 22:09:26 -0500 | [diff] [blame] | 746 | if (device->parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | list_add_tail(&device->node, &device->parent->children); |
Len Brown | 33b5715 | 2008-12-15 22:09:26 -0500 | [diff] [blame] | 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | if (device->wakeup.flags.valid) |
| 750 | list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 751 | mutex_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 753 | if (device->parent) |
Bjorn Helgaas | 66b7ed4 | 2009-09-21 19:29:05 +0000 | [diff] [blame] | 754 | device->dev.parent = &device->parent->dev; |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 755 | device->dev.bus = &acpi_bus_type; |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 756 | device->dev.release = release; |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 757 | result = device_add(&device->dev); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 758 | if (result) { |
Alex Chiang | 8b12b92 | 2009-05-14 08:31:32 -0600 | [diff] [blame] | 759 | dev_err(&device->dev, "Error registering device\n"); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 760 | goto err; |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 761 | } |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 762 | |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 763 | result = acpi_device_setup_files(device); |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 764 | if (result) |
Kay Sievers | 0794469 | 2008-10-30 01:18:59 +0100 | [diff] [blame] | 765 | printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n", |
| 766 | dev_name(&device->dev)); |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 767 | |
Li Shaohua | 9633357 | 2006-12-07 20:56:46 +0800 | [diff] [blame] | 768 | device->removal_type = ACPI_BUS_REMOVAL_NORMAL; |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 769 | return 0; |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 770 | |
| 771 | err: |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 772 | mutex_lock(&acpi_device_lock); |
Len Brown | 33b5715 | 2008-12-15 22:09:26 -0500 | [diff] [blame] | 773 | if (device->parent) |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 774 | list_del(&device->node); |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 775 | list_del(&device->wakeup_list); |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 776 | mutex_unlock(&acpi_device_lock); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 777 | |
| 778 | err_detach: |
| 779 | acpi_detach_data(device->handle, acpi_bus_data_handler); |
Zhang Rui | e49bd2dd | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 780 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Rafael J. Wysocki | b17b537 | 2013-01-15 13:23:44 +0100 | [diff] [blame] | 783 | static void acpi_device_unregister(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | { |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 785 | mutex_lock(&acpi_device_lock); |
Len Brown | 33b5715 | 2008-12-15 22:09:26 -0500 | [diff] [blame] | 786 | if (device->parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | list_del(&device->node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | |
| 789 | list_del(&device->wakeup_list); |
Shaohua Li | 9090589 | 2009-04-07 10:24:29 +0800 | [diff] [blame] | 790 | mutex_unlock(&acpi_device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
| 792 | acpi_detach_data(device->handle, acpi_bus_data_handler); |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 793 | |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 794 | acpi_power_add_remove_device(device, false); |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 795 | acpi_device_remove_files(device); |
Rafael J. Wysocki | b1c0f99 | 2013-01-24 12:50:09 +0100 | [diff] [blame] | 796 | if (device->remove) |
| 797 | device->remove(device); |
| 798 | |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 799 | device_del(&device->dev); |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 800 | /* |
Rafael J. Wysocki | 0aa120a | 2013-02-09 15:29:20 +0100 | [diff] [blame] | 801 | * Transition the device to D3cold to drop the reference counts of all |
| 802 | * power resources the device depends on and turn off the ones that have |
| 803 | * no more references. |
Rafael J. Wysocki | bc9b640 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 804 | */ |
Rafael J. Wysocki | 0aa120a | 2013-02-09 15:29:20 +0100 | [diff] [blame] | 805 | acpi_device_set_power(device, ACPI_STATE_D3_COLD); |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 806 | device->handle = NULL; |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 807 | put_device(&device->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 810 | /* -------------------------------------------------------------------------- |
| 811 | Driver Management |
| 812 | -------------------------------------------------------------------------- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 814 | * acpi_bus_driver_init - add a device to a driver |
| 815 | * @device: the device to add and initialize |
| 816 | * @driver: driver for the device |
| 817 | * |
Alex Chiang | 0c526d9 | 2009-05-14 08:31:37 -0600 | [diff] [blame] | 818 | * Used to initialize a device via its device driver. Called whenever a |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 819 | * driver is bound to a device. Invokes the driver's add() ops. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | */ |
| 821 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 822 | acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 824 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | if (!device || !driver) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 827 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
| 829 | if (!driver->ops.add) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 830 | return -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
| 832 | result = driver->ops.add(device); |
| 833 | if (result) { |
| 834 | device->driver = NULL; |
Pavel Machek | db89b4f | 2008-09-22 14:37:34 -0700 | [diff] [blame] | 835 | device->driver_data = NULL; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 836 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | device->driver = driver; |
| 840 | |
| 841 | /* |
| 842 | * TBD - Configuration Management: Assign resources to device based |
| 843 | * upon possible configuration and currently allocated resources. |
| 844 | */ |
| 845 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 846 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 847 | "Driver successfully bound to device\n")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 848 | return 0; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 852 | * acpi_bus_register_driver - register a driver with the ACPI bus |
| 853 | * @driver: driver being registered |
| 854 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | * Registers a driver with the ACPI bus. Searches the namespace for all |
Bjorn Helgaas | 9d9f749 | 2006-03-28 17:04:00 -0500 | [diff] [blame] | 856 | * devices that match the driver's criteria and binds. Returns zero for |
| 857 | * success or a negative error status for failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 859 | int acpi_bus_register_driver(struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 861 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
| 863 | if (acpi_disabled) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 864 | return -ENODEV; |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 865 | driver->drv.name = driver->name; |
| 866 | driver->drv.bus = &acpi_bus_type; |
| 867 | driver->drv.owner = driver->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 869 | ret = driver_register(&driver->drv); |
| 870 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 873 | EXPORT_SYMBOL(acpi_bus_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
| 875 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 876 | * acpi_bus_unregister_driver - unregisters a driver with the APIC bus |
| 877 | * @driver: driver to unregister |
| 878 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | * Unregisters a driver with the ACPI bus. Searches the namespace for all |
| 880 | * devices that match the driver's criteria and unbinds. |
| 881 | */ |
Bjorn Helgaas | 06ea8e08 | 2006-04-27 05:25:00 -0400 | [diff] [blame] | 882 | void acpi_bus_unregister_driver(struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | { |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 884 | driver_unregister(&driver->drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | EXPORT_SYMBOL(acpi_bus_unregister_driver); |
| 888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | /* -------------------------------------------------------------------------- |
| 890 | Device Enumeration |
| 891 | -------------------------------------------------------------------------- */ |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 892 | static struct acpi_device *acpi_bus_get_parent(acpi_handle handle) |
| 893 | { |
Rafael J. Wysocki | 456de89 | 2013-01-31 20:57:40 +0100 | [diff] [blame] | 894 | struct acpi_device *device = NULL; |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 895 | acpi_status status; |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 896 | |
| 897 | /* |
| 898 | * Fixed hardware devices do not appear in the namespace and do not |
| 899 | * have handles, but we fabricate acpi_devices for them, so we have |
| 900 | * to deal with them specially. |
| 901 | */ |
Rafael J. Wysocki | 456de89 | 2013-01-31 20:57:40 +0100 | [diff] [blame] | 902 | if (!handle) |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 903 | return acpi_root; |
| 904 | |
| 905 | do { |
| 906 | status = acpi_get_parent(handle, &handle); |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 907 | if (ACPI_FAILURE(status)) |
Rafael J. Wysocki | 456de89 | 2013-01-31 20:57:40 +0100 | [diff] [blame] | 908 | return status == AE_NULL_ENTRY ? NULL : acpi_root; |
| 909 | } while (acpi_bus_get_device(handle, &device)); |
| 910 | return device; |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 913 | acpi_status |
| 914 | acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd) |
| 915 | { |
| 916 | acpi_status status; |
| 917 | acpi_handle tmp; |
| 918 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 919 | union acpi_object *obj; |
| 920 | |
| 921 | status = acpi_get_handle(handle, "_EJD", &tmp); |
| 922 | if (ACPI_FAILURE(status)) |
| 923 | return status; |
| 924 | |
| 925 | status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer); |
| 926 | if (ACPI_SUCCESS(status)) { |
| 927 | obj = buffer.pointer; |
Holger Macht | 3b5fee5 | 2008-02-14 13:40:34 +0100 | [diff] [blame] | 928 | status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer, |
| 929 | ejd); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 930 | kfree(buffer.pointer); |
| 931 | } |
| 932 | return status; |
| 933 | } |
| 934 | EXPORT_SYMBOL_GPL(acpi_bus_get_ejd); |
| 935 | |
Bob Moore | 8e4319c | 2009-06-29 13:43:27 +0800 | [diff] [blame] | 936 | void acpi_bus_data_handler(acpi_handle handle, void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | { |
| 938 | |
| 939 | /* TBD */ |
| 940 | |
| 941 | return; |
| 942 | } |
| 943 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 944 | static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle, |
| 945 | struct acpi_device_wakeup *wakeup) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | { |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 947 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 948 | union acpi_object *package = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | union acpi_object *element = NULL; |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 950 | acpi_status status; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 951 | int err = -ENODATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 953 | if (!wakeup) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 954 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 956 | INIT_LIST_HEAD(&wakeup->resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 958 | /* _PRW */ |
| 959 | status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer); |
| 960 | if (ACPI_FAILURE(status)) { |
| 961 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW")); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 962 | return err; |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | package = (union acpi_object *)buffer.pointer; |
| 966 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 967 | if (!package || package->package.count < 2) |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 968 | goto out; |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | element = &(package->package.elements[0]); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 971 | if (!element) |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 972 | goto out; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | if (element->type == ACPI_TYPE_PACKAGE) { |
| 975 | if ((element->package.count < 2) || |
| 976 | (element->package.elements[0].type != |
| 977 | ACPI_TYPE_LOCAL_REFERENCE) |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 978 | || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 979 | goto out; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 980 | |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 981 | wakeup->gpe_device = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | element->package.elements[0].reference.handle; |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 983 | wakeup->gpe_number = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | (u32) element->package.elements[1].integer.value; |
| 985 | } else if (element->type == ACPI_TYPE_INTEGER) { |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 986 | wakeup->gpe_device = NULL; |
| 987 | wakeup->gpe_number = element->integer.value; |
| 988 | } else { |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 989 | goto out; |
| 990 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
| 992 | element = &(package->package.elements[1]); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 993 | if (element->type != ACPI_TYPE_INTEGER) |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 994 | goto out; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 995 | |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 996 | wakeup->sleep_state = element->integer.value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 998 | err = acpi_extract_power_resources(package, 2, &wakeup->resources); |
| 999 | if (err) |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1000 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
Rafael J. Wysocki | 0596a52 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1002 | if (!list_empty(&wakeup->resources)) { |
| 1003 | int sleep_state; |
| 1004 | |
Rafael J. Wysocki | b5d667e | 2013-02-23 23:15:21 +0100 | [diff] [blame] | 1005 | err = acpi_power_wakeup_list_init(&wakeup->resources, |
| 1006 | &sleep_state); |
| 1007 | if (err) { |
| 1008 | acpi_handle_warn(handle, "Retrieving current states " |
| 1009 | "of wakeup power resources failed\n"); |
| 1010 | acpi_power_resources_list_free(&wakeup->resources); |
| 1011 | goto out; |
| 1012 | } |
Rafael J. Wysocki | 0596a52 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1013 | if (sleep_state < wakeup->sleep_state) { |
| 1014 | acpi_handle_warn(handle, "Overriding _PRW sleep state " |
| 1015 | "(S%d) by S%d from power resources\n", |
| 1016 | (int)wakeup->sleep_state, sleep_state); |
| 1017 | wakeup->sleep_state = sleep_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | } |
Lin Ming | bba63a2 | 2010-12-13 13:39:17 +0800 | [diff] [blame] | 1020 | acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number); |
Rafael J. Wysocki | 9874647 | 2010-07-08 00:43:36 +0200 | [diff] [blame] | 1021 | |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1022 | out: |
| 1023 | kfree(buffer.pointer); |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1024 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1027 | static void acpi_bus_set_run_wake_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | { |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 1029 | struct acpi_device_id button_device_ids[] = { |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 1030 | {"PNP0C0C", 0}, |
Zhang Rui | b7e3830 | 2012-12-04 23:23:16 +0100 | [diff] [blame] | 1031 | {"PNP0C0D", 0}, |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 1032 | {"PNP0C0E", 0}, |
| 1033 | {"", 0}, |
| 1034 | }; |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1035 | acpi_status status; |
| 1036 | acpi_event_status event_status; |
| 1037 | |
Rafael J. Wysocki | b67ea76 | 2010-02-17 23:44:09 +0100 | [diff] [blame] | 1038 | device->wakeup.flags.notifier_present = 0; |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1039 | |
| 1040 | /* Power button, Lid switch always enable wakeup */ |
| 1041 | if (!acpi_match_device_ids(device, button_device_ids)) { |
| 1042 | device->wakeup.flags.run_wake = 1; |
Zhang Rui | b7e3830 | 2012-12-04 23:23:16 +0100 | [diff] [blame] | 1043 | if (!acpi_match_device_ids(device, &button_device_ids[1])) { |
| 1044 | /* Do not use Lid/sleep button for S5 wakeup */ |
| 1045 | if (device->wakeup.sleep_state == ACPI_STATE_S5) |
| 1046 | device->wakeup.sleep_state = ACPI_STATE_S4; |
| 1047 | } |
Rafael J. Wysocki | f2b56bc | 2011-01-06 23:34:22 +0100 | [diff] [blame] | 1048 | device_set_wakeup_capable(&device->dev, true); |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1049 | return; |
| 1050 | } |
| 1051 | |
Rafael J. Wysocki | e8e18c9 | 2010-07-08 00:42:51 +0200 | [diff] [blame] | 1052 | status = acpi_get_gpe_status(device->wakeup.gpe_device, |
| 1053 | device->wakeup.gpe_number, |
| 1054 | &event_status); |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1055 | if (status == AE_OK) |
| 1056 | device->wakeup.flags.run_wake = |
| 1057 | !!(event_status & ACPI_EVENT_FLAG_HANDLE); |
| 1058 | } |
| 1059 | |
Rafael J. Wysocki | d57d09a | 2011-01-06 23:41:27 +0100 | [diff] [blame] | 1060 | static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device) |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1061 | { |
Rafael J. Wysocki | d57d09a | 2011-01-06 23:41:27 +0100 | [diff] [blame] | 1062 | acpi_handle temp; |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1063 | acpi_status status = 0; |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1064 | int err; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 1065 | |
Rafael J. Wysocki | d57d09a | 2011-01-06 23:41:27 +0100 | [diff] [blame] | 1066 | /* Presence of _PRW indicates wake capable */ |
| 1067 | status = acpi_get_handle(device->handle, "_PRW", &temp); |
| 1068 | if (ACPI_FAILURE(status)) |
| 1069 | return; |
| 1070 | |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1071 | err = acpi_bus_extract_wakeup_device_power_package(device->handle, |
| 1072 | &device->wakeup); |
| 1073 | if (err) { |
| 1074 | dev_err(&device->dev, "_PRW evaluation error: %d\n", err); |
Rafael J. Wysocki | d57d09a | 2011-01-06 23:41:27 +0100 | [diff] [blame] | 1075 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | device->wakeup.flags.valid = 1; |
Rafael J. Wysocki | 9b83ccd | 2009-09-08 23:15:31 +0200 | [diff] [blame] | 1079 | device->wakeup.prepare_count = 0; |
Rafael J. Wysocki | f517709 | 2010-02-17 23:41:49 +0100 | [diff] [blame] | 1080 | acpi_bus_set_run_wake_flags(device); |
Zhao Yakui | 729b2bd | 2008-03-19 13:26:54 +0800 | [diff] [blame] | 1081 | /* Call _PSW/_DSW object to disable its ability to wake the sleeping |
| 1082 | * system for the ACPI device with the _PRW object. |
| 1083 | * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW. |
| 1084 | * So it is necessary to call _DSW object first. Only when it is not |
| 1085 | * present will the _PSW object used. |
| 1086 | */ |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1087 | err = acpi_device_sleep_wake(device, 0, 0, 0); |
| 1088 | if (err) |
Rafael J. Wysocki | 77e7660 | 2008-07-07 03:33:34 +0200 | [diff] [blame] | 1089 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 1090 | "error in _DSW or _PSW evaluation\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1093 | static void acpi_bus_init_power_state(struct acpi_device *device, int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | { |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1095 | struct acpi_device_power_state *ps = &device->power.states[state]; |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1096 | char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' }; |
| 1097 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1098 | acpi_handle handle; |
| 1099 | acpi_status status; |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1100 | |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1101 | INIT_LIST_HEAD(&ps->resources); |
| 1102 | |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1103 | /* Evaluate "_PRx" to get referenced power resources */ |
| 1104 | status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer); |
| 1105 | if (ACPI_SUCCESS(status)) { |
| 1106 | union acpi_object *package = buffer.pointer; |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1107 | |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1108 | if (buffer.length && package |
| 1109 | && package->type == ACPI_TYPE_PACKAGE |
| 1110 | && package->package.count) { |
Rafael J. Wysocki | e88c9c6 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1111 | int err = acpi_extract_power_resources(package, 0, |
| 1112 | &ps->resources); |
| 1113 | if (!err) |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1114 | device->power.flags.power_resources = 1; |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1115 | } |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1116 | ACPI_FREE(buffer.pointer); |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | /* Evaluate "_PSx" to see if we can do explicit sets */ |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1120 | pathname[2] = 'S'; |
| 1121 | status = acpi_get_handle(device->handle, pathname, &handle); |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1122 | if (ACPI_SUCCESS(status)) |
| 1123 | ps->flags.explicit_set = 1; |
| 1124 | |
| 1125 | /* |
| 1126 | * State is valid if there are means to put the device into it. |
| 1127 | * D3hot is only valid if _PR3 present. |
| 1128 | */ |
Rafael J. Wysocki | ef85bdb | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1129 | if (!list_empty(&ps->resources) |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1130 | || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) { |
| 1131 | ps->flags.valid = 1; |
| 1132 | ps->flags.os_accessible = 1; |
| 1133 | } |
| 1134 | |
| 1135 | ps->power = -1; /* Unknown - driver assigned */ |
| 1136 | ps->latency = -1; /* Unknown - driver assigned */ |
| 1137 | } |
| 1138 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1139 | static void acpi_bus_get_power_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | { |
Rafael J. Wysocki | 8bc5053 | 2013-01-17 14:11:07 +0100 | [diff] [blame] | 1141 | acpi_status status; |
| 1142 | acpi_handle handle; |
| 1143 | u32 i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1145 | /* Presence of _PS0|_PR0 indicates 'power manageable' */ |
| 1146 | status = acpi_get_handle(device->handle, "_PS0", &handle); |
| 1147 | if (ACPI_FAILURE(status)) { |
| 1148 | status = acpi_get_handle(device->handle, "_PR0", &handle); |
| 1149 | if (ACPI_FAILURE(status)) |
| 1150 | return; |
| 1151 | } |
| 1152 | |
| 1153 | device->flags.power_manageable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
| 1155 | /* |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1156 | * Power Management Flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | */ |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1158 | status = acpi_get_handle(device->handle, "_PSC", &handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | if (ACPI_SUCCESS(status)) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1160 | device->power.flags.explicit_get = 1; |
| 1161 | status = acpi_get_handle(device->handle, "_IRC", &handle); |
| 1162 | if (ACPI_SUCCESS(status)) |
| 1163 | device->power.flags.inrush_current = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | |
| 1165 | /* |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1166 | * Enumerate supported power management states |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | */ |
Rafael J. Wysocki | f33ce56 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1168 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) |
| 1169 | acpi_bus_init_power_state(device, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | |
Rafael J. Wysocki | 0b22452 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1171 | INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 1173 | /* Set defaults for D0 and D3 states (always valid) */ |
| 1174 | device->power.states[ACPI_STATE_D0].flags.valid = 1; |
| 1175 | device->power.states[ACPI_STATE_D0].power = 100; |
| 1176 | device->power.states[ACPI_STATE_D3].flags.valid = 1; |
| 1177 | device->power.states[ACPI_STATE_D3].power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | |
Rafael J. Wysocki | 5c7dd71 | 2012-05-18 00:39:35 +0200 | [diff] [blame] | 1179 | /* Set D3cold's explicit_set flag if _PS3 exists. */ |
| 1180 | if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set) |
| 1181 | device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1; |
| 1182 | |
Aaron Lu | 1399dfc | 2012-11-21 23:33:40 +0100 | [diff] [blame] | 1183 | /* Presence of _PS3 or _PRx means we can put the device into D3 cold */ |
| 1184 | if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set || |
| 1185 | device->power.flags.power_resources) |
| 1186 | device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1; |
| 1187 | |
Rafael J. Wysocki | b378549 | 2013-02-01 23:43:02 +0100 | [diff] [blame] | 1188 | if (acpi_bus_init_power(device)) { |
| 1189 | acpi_free_power_resources_lists(device); |
| 1190 | device->flags.power_manageable = 0; |
| 1191 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1194 | static void acpi_bus_get_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1196 | acpi_status status = AE_OK; |
| 1197 | acpi_handle temp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | /* Presence of _STA indicates 'dynamic_status' */ |
| 1200 | status = acpi_get_handle(device->handle, "_STA", &temp); |
| 1201 | if (ACPI_SUCCESS(status)) |
| 1202 | device->flags.dynamic_status = 1; |
| 1203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | /* Presence of _RMV indicates 'removable' */ |
| 1205 | status = acpi_get_handle(device->handle, "_RMV", &temp); |
| 1206 | if (ACPI_SUCCESS(status)) |
| 1207 | device->flags.removable = 1; |
| 1208 | |
| 1209 | /* Presence of _EJD|_EJ0 indicates 'ejectable' */ |
| 1210 | status = acpi_get_handle(device->handle, "_EJD", &temp); |
| 1211 | if (ACPI_SUCCESS(status)) |
| 1212 | device->flags.ejectable = 1; |
| 1213 | else { |
| 1214 | status = acpi_get_handle(device->handle, "_EJ0", &temp); |
| 1215 | if (ACPI_SUCCESS(status)) |
| 1216 | device->flags.ejectable = 1; |
| 1217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Bjorn Helgaas | c7bcb4e | 2009-09-21 19:29:25 +0000 | [diff] [blame] | 1220 | static void acpi_device_get_busid(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1222 | char bus_id[5] = { '?', 0 }; |
| 1223 | struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; |
| 1224 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | |
| 1226 | /* |
| 1227 | * Bus ID |
| 1228 | * ------ |
| 1229 | * The device's Bus ID is simply the object name. |
| 1230 | * TBD: Shouldn't this value be unique (within the ACPI namespace)? |
| 1231 | */ |
Bjorn Helgaas | 859ac9a4 | 2009-09-21 19:29:50 +0000 | [diff] [blame] | 1232 | if (ACPI_IS_ROOT_DEVICE(device)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | strcpy(device->pnp.bus_id, "ACPI"); |
Bjorn Helgaas | 859ac9a4 | 2009-09-21 19:29:50 +0000 | [diff] [blame] | 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | switch (device->device_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | case ACPI_BUS_TYPE_POWER_BUTTON: |
| 1239 | strcpy(device->pnp.bus_id, "PWRF"); |
| 1240 | break; |
| 1241 | case ACPI_BUS_TYPE_SLEEP_BUTTON: |
| 1242 | strcpy(device->pnp.bus_id, "SLPF"); |
| 1243 | break; |
| 1244 | default: |
Bjorn Helgaas | 66b7ed4 | 2009-09-21 19:29:05 +0000 | [diff] [blame] | 1245 | acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | /* Clean up trailing underscores (if any) */ |
| 1247 | for (i = 3; i > 1; i--) { |
| 1248 | if (bus_id[i] == '_') |
| 1249 | bus_id[i] = '\0'; |
| 1250 | else |
| 1251 | break; |
| 1252 | } |
| 1253 | strcpy(device->pnp.bus_id, bus_id); |
| 1254 | break; |
| 1255 | } |
| 1256 | } |
| 1257 | |
Zhang Rui | 5473526 | 2007-01-11 02:09:09 -0500 | [diff] [blame] | 1258 | /* |
| 1259 | * acpi_bay_match - see if a device is an ejectable driver bay |
| 1260 | * |
| 1261 | * If an acpi object is ejectable and has one of the ACPI ATA methods defined, |
| 1262 | * then we can safely call it an ejectable drive bay |
| 1263 | */ |
| 1264 | static int acpi_bay_match(struct acpi_device *device){ |
| 1265 | acpi_status status; |
| 1266 | acpi_handle handle; |
| 1267 | acpi_handle tmp; |
| 1268 | acpi_handle phandle; |
| 1269 | |
| 1270 | handle = device->handle; |
| 1271 | |
| 1272 | status = acpi_get_handle(handle, "_EJ0", &tmp); |
| 1273 | if (ACPI_FAILURE(status)) |
| 1274 | return -ENODEV; |
| 1275 | |
| 1276 | if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) || |
| 1277 | (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) || |
| 1278 | (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) || |
| 1279 | (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp)))) |
| 1280 | return 0; |
| 1281 | |
| 1282 | if (acpi_get_parent(handle, &phandle)) |
| 1283 | return -ENODEV; |
| 1284 | |
| 1285 | if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) || |
| 1286 | (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) || |
| 1287 | (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) || |
| 1288 | (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp)))) |
| 1289 | return 0; |
| 1290 | |
| 1291 | return -ENODEV; |
| 1292 | } |
| 1293 | |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1294 | /* |
| 1295 | * acpi_dock_match - see if a device has a _DCK method |
| 1296 | */ |
| 1297 | static int acpi_dock_match(struct acpi_device *device) |
| 1298 | { |
| 1299 | acpi_handle tmp; |
| 1300 | return acpi_get_handle(device->handle, "_DCK", &tmp); |
| 1301 | } |
| 1302 | |
Thomas Renninger | 620e112 | 2010-10-01 10:54:00 +0200 | [diff] [blame] | 1303 | const char *acpi_device_hid(struct acpi_device *device) |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1304 | { |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1305 | struct acpi_hardware_id *hid; |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1306 | |
Thomas Renninger | 2b2ae7c | 2010-10-01 10:53:59 +0200 | [diff] [blame] | 1307 | if (list_empty(&device->pnp.ids)) |
| 1308 | return dummy_hid; |
| 1309 | |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1310 | hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list); |
| 1311 | return hid->id; |
| 1312 | } |
| 1313 | EXPORT_SYMBOL(acpi_device_hid); |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1314 | |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1315 | static void acpi_add_id(struct acpi_device *device, const char *dev_id) |
| 1316 | { |
| 1317 | struct acpi_hardware_id *id; |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1318 | |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1319 | id = kmalloc(sizeof(*id), GFP_KERNEL); |
| 1320 | if (!id) |
| 1321 | return; |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1322 | |
Thomas Meyer | 581de59 | 2011-08-06 11:32:56 +0200 | [diff] [blame] | 1323 | id->id = kstrdup(dev_id, GFP_KERNEL); |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1324 | if (!id->id) { |
| 1325 | kfree(id); |
| 1326 | return; |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1327 | } |
| 1328 | |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1329 | list_add_tail(&id->list, &device->pnp.ids); |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1330 | } |
| 1331 | |
Darrick J. Wong | 222e82a | 2010-03-24 14:38:37 +0100 | [diff] [blame] | 1332 | /* |
| 1333 | * Old IBM workstations have a DSDT bug wherein the SMBus object |
| 1334 | * lacks the SMBUS01 HID and the methods do not have the necessary "_" |
| 1335 | * prefix. Work around this. |
| 1336 | */ |
| 1337 | static int acpi_ibm_smbus_match(struct acpi_device *device) |
| 1338 | { |
| 1339 | acpi_handle h_dummy; |
| 1340 | struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 1341 | int result; |
| 1342 | |
| 1343 | if (!dmi_name_in_vendors("IBM")) |
| 1344 | return -ENODEV; |
| 1345 | |
| 1346 | /* Look for SMBS object */ |
| 1347 | result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path); |
| 1348 | if (result) |
| 1349 | return result; |
| 1350 | |
| 1351 | if (strcmp("SMBS", path.pointer)) { |
| 1352 | result = -ENODEV; |
| 1353 | goto out; |
| 1354 | } |
| 1355 | |
| 1356 | /* Does it have the necessary (but misnamed) methods? */ |
| 1357 | result = -ENODEV; |
| 1358 | if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) && |
| 1359 | ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) && |
| 1360 | ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy))) |
| 1361 | result = 0; |
| 1362 | out: |
| 1363 | kfree(path.pointer); |
| 1364 | return result; |
| 1365 | } |
| 1366 | |
Bjorn Helgaas | c7bcb4e | 2009-09-21 19:29:25 +0000 | [diff] [blame] | 1367 | static void acpi_device_set_id(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1369 | acpi_status status; |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1370 | struct acpi_device_info *info; |
Lv Zheng | 78e25fe | 2012-10-31 02:25:24 +0000 | [diff] [blame] | 1371 | struct acpi_pnp_device_id_list *cid_list; |
Bjorn Helgaas | 7f47fa6 | 2009-09-21 13:35:19 -0600 | [diff] [blame] | 1372 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
Bjorn Helgaas | c7bcb4e | 2009-09-21 19:29:25 +0000 | [diff] [blame] | 1374 | switch (device->device_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | case ACPI_BUS_TYPE_DEVICE: |
Bjorn Helgaas | 859ac9a4 | 2009-09-21 19:29:50 +0000 | [diff] [blame] | 1376 | if (ACPI_IS_ROOT_DEVICE(device)) { |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1377 | acpi_add_id(device, ACPI_SYSTEM_HID); |
Bjorn Helgaas | 859ac9a4 | 2009-09-21 19:29:50 +0000 | [diff] [blame] | 1378 | break; |
| 1379 | } |
| 1380 | |
Bjorn Helgaas | 66b7ed4 | 2009-09-21 19:29:05 +0000 | [diff] [blame] | 1381 | status = acpi_get_object_info(device->handle, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 96b2dd1 | 2008-03-05 18:24:51 -0800 | [diff] [blame] | 1383 | printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | return; |
| 1385 | } |
| 1386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | if (info->valid & ACPI_VALID_HID) |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1388 | acpi_add_id(device, info->hardware_id.string); |
| 1389 | if (info->valid & ACPI_VALID_CID) { |
Bob Moore | 15b8dd5 | 2009-06-29 13:39:29 +0800 | [diff] [blame] | 1390 | cid_list = &info->compatible_id_list; |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1391 | for (i = 0; i < cid_list->count; i++) |
| 1392 | acpi_add_id(device, cid_list->ids[i].string); |
| 1393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | if (info->valid & ACPI_VALID_ADR) { |
| 1395 | device->pnp.bus_address = info->address; |
| 1396 | device->flags.bus_address = 1; |
| 1397 | } |
Lv Zheng | ccf7804 | 2012-10-30 14:41:07 +0100 | [diff] [blame] | 1398 | if (info->valid & ACPI_VALID_UID) |
| 1399 | device->pnp.unique_id = kstrdup(info->unique_id.string, |
| 1400 | GFP_KERNEL); |
Zhang Rui | ae84333 | 2006-12-07 20:57:10 +0800 | [diff] [blame] | 1401 | |
Bjorn Helgaas | a83893ae | 2009-10-02 11:03:12 -0400 | [diff] [blame] | 1402 | kfree(info); |
| 1403 | |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1404 | /* |
| 1405 | * Some devices don't reliably have _HIDs & _CIDs, so add |
| 1406 | * synthetic HIDs to make sure drivers can find them. |
| 1407 | */ |
Thomas Renninger | c3d6de6 | 2008-08-01 17:37:55 +0200 | [diff] [blame] | 1408 | if (acpi_is_video_device(device)) |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1409 | acpi_add_id(device, ACPI_VIDEO_HID); |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1410 | else if (ACPI_SUCCESS(acpi_bay_match(device))) |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1411 | acpi_add_id(device, ACPI_BAY_HID); |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1412 | else if (ACPI_SUCCESS(acpi_dock_match(device))) |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1413 | acpi_add_id(device, ACPI_DOCK_HID); |
Darrick J. Wong | 222e82a | 2010-03-24 14:38:37 +0100 | [diff] [blame] | 1414 | else if (!acpi_ibm_smbus_match(device)) |
| 1415 | acpi_add_id(device, ACPI_SMBUS_IBM_HID); |
Rafael J. Wysocki | 4f5f64c | 2013-01-04 23:00:54 +0100 | [diff] [blame] | 1416 | else if (list_empty(&device->pnp.ids) && |
Bjorn Helgaas | b7b30de | 2010-03-24 10:44:33 -0600 | [diff] [blame] | 1417 | ACPI_IS_ROOT_DEVICE(device->parent)) { |
| 1418 | acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */ |
| 1419 | strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME); |
| 1420 | strcpy(device->pnp.device_class, ACPI_BUS_CLASS); |
| 1421 | } |
Zhang Rui | 5473526 | 2007-01-11 02:09:09 -0500 | [diff] [blame] | 1422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | break; |
| 1424 | case ACPI_BUS_TYPE_POWER: |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1425 | acpi_add_id(device, ACPI_POWER_HID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | break; |
| 1427 | case ACPI_BUS_TYPE_PROCESSOR: |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1428 | acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | case ACPI_BUS_TYPE_THERMAL: |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1431 | acpi_add_id(device, ACPI_THERMAL_HID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | break; |
| 1433 | case ACPI_BUS_TYPE_POWER_BUTTON: |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1434 | acpi_add_id(device, ACPI_BUTTON_HID_POWERF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | break; |
| 1436 | case ACPI_BUS_TYPE_SLEEP_BUTTON: |
Bjorn Helgaas | 57f3674 | 2009-09-21 13:35:40 -0600 | [diff] [blame] | 1437 | acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | break; |
| 1439 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1442 | void acpi_init_device_object(struct acpi_device *device, acpi_handle handle, |
| 1443 | int type, unsigned long long sta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | { |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1445 | INIT_LIST_HEAD(&device->pnp.ids); |
| 1446 | device->device_type = type; |
| 1447 | device->handle = handle; |
| 1448 | device->parent = acpi_bus_get_parent(handle); |
| 1449 | STRUCT_TO_INT(device->status) = sta; |
| 1450 | acpi_device_get_busid(device); |
| 1451 | acpi_device_set_id(device); |
| 1452 | acpi_bus_get_flags(device); |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 1453 | device->flags.match_driver = false; |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 1454 | device_initialize(&device->dev); |
| 1455 | dev_set_uevent_suppress(&device->dev, true); |
| 1456 | } |
Bjorn Helgaas | bc3b077 | 2009-09-21 19:29:20 +0000 | [diff] [blame] | 1457 | |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 1458 | void acpi_device_add_finalize(struct acpi_device *device) |
| 1459 | { |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 1460 | device->flags.match_driver = true; |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 1461 | dev_set_uevent_suppress(&device->dev, false); |
| 1462 | kobject_uevent(&device->dev.kobj, KOBJ_ADD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | } |
| 1464 | |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 1465 | static int acpi_add_single_object(struct acpi_device **child, |
| 1466 | acpi_handle handle, int type, |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 1467 | unsigned long long sta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | { |
Bjorn Helgaas | 77c2488 | 2009-09-21 19:29:30 +0000 | [diff] [blame] | 1469 | int result; |
| 1470 | struct acpi_device *device; |
Bjorn Helgaas | 29aaefa | 2009-09-21 19:28:54 +0000 | [diff] [blame] | 1471 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 1473 | device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | if (!device) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 1475 | printk(KERN_ERR PREFIX "Memory allocation error\n"); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1476 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1479 | acpi_init_device_object(device, handle, type, sta); |
| 1480 | acpi_bus_get_power_flags(device); |
Rafael J. Wysocki | d57d09a | 2011-01-06 23:41:27 +0100 | [diff] [blame] | 1481 | acpi_bus_get_wakeup_device_flags(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 1483 | result = acpi_device_add(device, acpi_device_release); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1484 | if (result) { |
Hugh Dickins | 718fb0d | 2009-08-06 23:18:12 +0000 | [diff] [blame] | 1485 | acpi_device_release(&device->dev); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1486 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1489 | acpi_power_add_remove_device(device, true); |
Rafael J. Wysocki | cf860be | 2013-01-24 12:49:49 +0100 | [diff] [blame] | 1490 | acpi_device_add_finalize(device); |
Rafael J. Wysocki | d43e167 | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1491 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 1492 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n", |
| 1493 | dev_name(&device->dev), (char *) buffer.pointer, |
| 1494 | device->parent ? dev_name(&device->parent->dev) : "(null)")); |
| 1495 | kfree(buffer.pointer); |
| 1496 | *child = device; |
| 1497 | return 0; |
Rafael J. Wysocki | bf325f9 | 2010-11-25 00:10:44 +0100 | [diff] [blame] | 1498 | } |
| 1499 | |
Bjorn Helgaas | 778cbc1 | 2009-09-21 19:30:06 +0000 | [diff] [blame] | 1500 | static int acpi_bus_type_and_status(acpi_handle handle, int *type, |
| 1501 | unsigned long long *sta) |
| 1502 | { |
| 1503 | acpi_status status; |
| 1504 | acpi_object_type acpi_type; |
| 1505 | |
| 1506 | status = acpi_get_type(handle, &acpi_type); |
| 1507 | if (ACPI_FAILURE(status)) |
| 1508 | return -ENODEV; |
| 1509 | |
| 1510 | switch (acpi_type) { |
| 1511 | case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */ |
| 1512 | case ACPI_TYPE_DEVICE: |
| 1513 | *type = ACPI_BUS_TYPE_DEVICE; |
| 1514 | status = acpi_bus_get_status_handle(handle, sta); |
| 1515 | if (ACPI_FAILURE(status)) |
| 1516 | return -ENODEV; |
| 1517 | break; |
| 1518 | case ACPI_TYPE_PROCESSOR: |
| 1519 | *type = ACPI_BUS_TYPE_PROCESSOR; |
| 1520 | status = acpi_bus_get_status_handle(handle, sta); |
| 1521 | if (ACPI_FAILURE(status)) |
| 1522 | return -ENODEV; |
| 1523 | break; |
| 1524 | case ACPI_TYPE_THERMAL: |
| 1525 | *type = ACPI_BUS_TYPE_THERMAL; |
| 1526 | *sta = ACPI_STA_DEFAULT; |
| 1527 | break; |
| 1528 | case ACPI_TYPE_POWER: |
| 1529 | *type = ACPI_BUS_TYPE_POWER; |
| 1530 | *sta = ACPI_STA_DEFAULT; |
| 1531 | break; |
| 1532 | default: |
| 1533 | return -ENODEV; |
| 1534 | } |
| 1535 | |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
Rafael J. Wysocki | e386309 | 2012-12-21 00:36:47 +0100 | [diff] [blame] | 1539 | static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used, |
| 1540 | void *not_used, void **return_value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | { |
Rafael J. Wysocki | 805d410f | 2012-12-21 00:36:39 +0100 | [diff] [blame] | 1542 | struct acpi_device *device = NULL; |
Bjorn Helgaas | 778cbc1 | 2009-09-21 19:30:06 +0000 | [diff] [blame] | 1543 | int type; |
| 1544 | unsigned long long sta; |
Bjorn Helgaas | e3b87f8 | 2009-09-21 19:30:11 +0000 | [diff] [blame] | 1545 | acpi_status status; |
Bjorn Helgaas | 778cbc1 | 2009-09-21 19:30:06 +0000 | [diff] [blame] | 1546 | int result; |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 1547 | |
Rafael J. Wysocki | 4002bf3 | 2012-12-21 00:36:44 +0100 | [diff] [blame] | 1548 | acpi_bus_get_device(handle, &device); |
| 1549 | if (device) |
| 1550 | goto out; |
| 1551 | |
Bjorn Helgaas | 778cbc1 | 2009-09-21 19:30:06 +0000 | [diff] [blame] | 1552 | result = acpi_bus_type_and_status(handle, &type, &sta); |
| 1553 | if (result) |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 1554 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
Rafael J. Wysocki | 82c7d5e | 2013-01-17 14:11:05 +0100 | [diff] [blame] | 1556 | if (type == ACPI_BUS_TYPE_POWER) { |
| 1557 | acpi_add_power_resource(handle); |
| 1558 | return AE_OK; |
| 1559 | } |
| 1560 | |
Bjorn Helgaas | 778cbc1 | 2009-09-21 19:30:06 +0000 | [diff] [blame] | 1561 | if (!(sta & ACPI_STA_DEVICE_PRESENT) && |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1562 | !(sta & ACPI_STA_DEVICE_FUNCTIONING)) { |
Rafael J. Wysocki | 86e4e20 | 2011-01-06 23:40:00 +0100 | [diff] [blame] | 1563 | struct acpi_device_wakeup wakeup; |
| 1564 | acpi_handle temp; |
| 1565 | |
| 1566 | status = acpi_get_handle(handle, "_PRW", &temp); |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1567 | if (ACPI_SUCCESS(status)) { |
Rafael J. Wysocki | 86e4e20 | 2011-01-06 23:40:00 +0100 | [diff] [blame] | 1568 | acpi_bus_extract_wakeup_device_power_package(handle, |
| 1569 | &wakeup); |
Rafael J. Wysocki | 993cbe5 | 2013-01-17 14:11:06 +0100 | [diff] [blame] | 1570 | acpi_power_resources_list_free(&wakeup.resources); |
| 1571 | } |
Bjorn Helgaas | 778cbc1 | 2009-09-21 19:30:06 +0000 | [diff] [blame] | 1572 | return AE_CTRL_DEPTH; |
Rafael J. Wysocki | b581a7f | 2010-12-17 22:34:01 +0100 | [diff] [blame] | 1573 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 1575 | acpi_add_single_object(&device, handle, type, sta); |
Bjorn Helgaas | e3b87f8 | 2009-09-21 19:30:11 +0000 | [diff] [blame] | 1576 | if (!device) |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 1577 | return AE_CTRL_DEPTH; |
| 1578 | |
Rafael J. Wysocki | 4002bf3 | 2012-12-21 00:36:44 +0100 | [diff] [blame] | 1579 | out: |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 1580 | if (!*return_value) |
| 1581 | *return_value = device; |
Rafael J. Wysocki | 805d410f | 2012-12-21 00:36:39 +0100 | [diff] [blame] | 1582 | |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 1583 | return AE_OK; |
| 1584 | } |
| 1585 | |
Rafael J. Wysocki | 87b85b3 | 2013-02-06 13:05:22 +0100 | [diff] [blame] | 1586 | static int acpi_scan_do_attach_handler(struct acpi_device *device, char *id) |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 1587 | { |
| 1588 | struct acpi_scan_handler *handler; |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 1589 | |
| 1590 | list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) { |
Rafael J. Wysocki | 87b85b3 | 2013-02-06 13:05:22 +0100 | [diff] [blame] | 1591 | const struct acpi_device_id *devid; |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 1592 | |
Rafael J. Wysocki | 87b85b3 | 2013-02-06 13:05:22 +0100 | [diff] [blame] | 1593 | for (devid = handler->ids; devid->id[0]; devid++) { |
| 1594 | int ret; |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 1595 | |
Rafael J. Wysocki | 87b85b3 | 2013-02-06 13:05:22 +0100 | [diff] [blame] | 1596 | if (strcmp((char *)devid->id, id)) |
| 1597 | continue; |
| 1598 | |
| 1599 | ret = handler->attach(device, devid); |
| 1600 | if (ret > 0) { |
| 1601 | device->handler = handler; |
| 1602 | return ret; |
| 1603 | } else if (ret < 0) { |
| 1604 | return ret; |
| 1605 | } |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 1606 | } |
| 1607 | } |
Rafael J. Wysocki | 87b85b3 | 2013-02-06 13:05:22 +0100 | [diff] [blame] | 1608 | return 0; |
| 1609 | } |
| 1610 | |
| 1611 | static int acpi_scan_attach_handler(struct acpi_device *device) |
| 1612 | { |
| 1613 | struct acpi_hardware_id *hwid; |
| 1614 | int ret = 0; |
| 1615 | |
| 1616 | list_for_each_entry(hwid, &device->pnp.ids, list) { |
| 1617 | ret = acpi_scan_do_attach_handler(device, hwid->id); |
| 1618 | if (ret) |
| 1619 | break; |
| 1620 | |
| 1621 | } |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 1622 | return ret; |
| 1623 | } |
| 1624 | |
Rafael J. Wysocki | 0fc300b | 2012-12-21 00:36:41 +0100 | [diff] [blame] | 1625 | static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used, |
| 1626 | void *not_used, void **ret_not_used) |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 1627 | { |
Rafael J. Wysocki | 805d410f | 2012-12-21 00:36:39 +0100 | [diff] [blame] | 1628 | struct acpi_device *device; |
| 1629 | unsigned long long sta_not_used; |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 1630 | int ret; |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 1631 | |
Rafael J. Wysocki | 805d410f | 2012-12-21 00:36:39 +0100 | [diff] [blame] | 1632 | /* |
| 1633 | * Ignore errors ignored by acpi_bus_check_add() to avoid terminating |
| 1634 | * namespace walks prematurely. |
| 1635 | */ |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 1636 | if (acpi_bus_type_and_status(handle, &ret, &sta_not_used)) |
Rafael J. Wysocki | 805d410f | 2012-12-21 00:36:39 +0100 | [diff] [blame] | 1637 | return AE_OK; |
Bjorn Helgaas | 51a85fa | 2009-09-21 19:29:56 +0000 | [diff] [blame] | 1638 | |
Rafael J. Wysocki | 805d410f | 2012-12-21 00:36:39 +0100 | [diff] [blame] | 1639 | if (acpi_bus_get_device(handle, &device)) |
| 1640 | return AE_CTRL_DEPTH; |
Thomas Renninger | 7779688 | 2010-01-29 17:48:52 +0100 | [diff] [blame] | 1641 | |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 1642 | ret = acpi_scan_attach_handler(device); |
| 1643 | if (ret) |
| 1644 | return ret > 0 ? AE_OK : AE_CTRL_DEPTH; |
| 1645 | |
| 1646 | ret = device_attach(&device->dev); |
| 1647 | return ret >= 0 ? AE_OK : AE_CTRL_DEPTH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 1650 | /** |
| 1651 | * acpi_bus_scan - Add ACPI device node objects in a given namespace scope. |
| 1652 | * @handle: Root of the namespace scope to scan. |
Thomas Renninger | 7779688 | 2010-01-29 17:48:52 +0100 | [diff] [blame] | 1653 | * |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 1654 | * Scan a given ACPI tree (probably recently hot-plugged) and create and add |
| 1655 | * found devices. |
Thomas Renninger | 7779688 | 2010-01-29 17:48:52 +0100 | [diff] [blame] | 1656 | * |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 1657 | * If no devices were found, -ENODEV is returned, but it does not mean that |
| 1658 | * there has been a real error. There just have been no suitable ACPI objects |
| 1659 | * in the table trunk from which the kernel could create a device and add an |
| 1660 | * appropriate driver. |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1661 | * |
| 1662 | * Must be called under acpi_scan_lock. |
Thomas Renninger | 7779688 | 2010-01-29 17:48:52 +0100 | [diff] [blame] | 1663 | */ |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 1664 | int acpi_bus_scan(acpi_handle handle) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1665 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | void *device = NULL; |
Rafael J. Wysocki | c511cc1 | 2013-01-27 21:17:29 +0100 | [diff] [blame] | 1667 | int error = 0; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1668 | |
Rafael J. Wysocki | 0cd6ac5 | 2012-12-21 00:36:49 +0100 | [diff] [blame] | 1669 | if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, |
Rafael J. Wysocki | e386309 | 2012-12-21 00:36:47 +0100 | [diff] [blame] | 1671 | acpi_bus_check_add, NULL, NULL, &device); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1672 | |
Thomas Renninger | d2f6650 | 2010-01-29 17:48:51 +0100 | [diff] [blame] | 1673 | if (!device) |
Rafael J. Wysocki | c511cc1 | 2013-01-27 21:17:29 +0100 | [diff] [blame] | 1674 | error = -ENODEV; |
| 1675 | else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL))) |
Rafael J. Wysocki | 805d410f | 2012-12-21 00:36:39 +0100 | [diff] [blame] | 1676 | acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX, |
Rafael J. Wysocki | 0fc300b | 2012-12-21 00:36:41 +0100 | [diff] [blame] | 1677 | acpi_bus_device_attach, NULL, NULL, NULL); |
Thomas Renninger | d2f6650 | 2010-01-29 17:48:51 +0100 | [diff] [blame] | 1678 | |
Rafael J. Wysocki | c511cc1 | 2013-01-27 21:17:29 +0100 | [diff] [blame] | 1679 | return error; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1680 | } |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 1681 | EXPORT_SYMBOL(acpi_bus_scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | |
Rafael J. Wysocki | 05404d8 | 2013-01-15 13:24:13 +0100 | [diff] [blame] | 1683 | static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used, |
| 1684 | void *not_used, void **ret_not_used) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | { |
Rafael J. Wysocki | 05404d8 | 2013-01-15 13:24:13 +0100 | [diff] [blame] | 1686 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | |
Rafael J. Wysocki | 05404d8 | 2013-01-15 13:24:13 +0100 | [diff] [blame] | 1688 | if (!acpi_bus_get_device(handle, &device)) { |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 1689 | struct acpi_scan_handler *dev_handler = device->handler; |
| 1690 | |
Rafael J. Wysocki | 05404d8 | 2013-01-15 13:24:13 +0100 | [diff] [blame] | 1691 | device->removal_type = ACPI_BUS_REMOVAL_EJECT; |
Rafael J. Wysocki | ca589f9 | 2013-01-30 14:27:29 +0100 | [diff] [blame] | 1692 | if (dev_handler) { |
| 1693 | if (dev_handler->detach) |
| 1694 | dev_handler->detach(device); |
| 1695 | |
| 1696 | device->handler = NULL; |
| 1697 | } else { |
| 1698 | device_release_driver(&device->dev); |
| 1699 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | } |
Rafael J. Wysocki | 05404d8 | 2013-01-15 13:24:13 +0100 | [diff] [blame] | 1701 | return AE_OK; |
| 1702 | } |
| 1703 | |
| 1704 | static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used, |
| 1705 | void *not_used, void **ret_not_used) |
| 1706 | { |
| 1707 | struct acpi_device *device = NULL; |
| 1708 | |
| 1709 | if (!acpi_bus_get_device(handle, &device)) |
| 1710 | acpi_device_unregister(device); |
| 1711 | |
| 1712 | return AE_OK; |
| 1713 | } |
| 1714 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1715 | /** |
| 1716 | * acpi_bus_trim - Remove ACPI device node and all of its descendants |
| 1717 | * @start: Root of the ACPI device nodes subtree to remove. |
| 1718 | * |
| 1719 | * Must be called under acpi_scan_lock. |
| 1720 | */ |
Rafael J. Wysocki | bfee26d | 2013-01-26 00:27:44 +0100 | [diff] [blame] | 1721 | void acpi_bus_trim(struct acpi_device *start) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | { |
Rafael J. Wysocki | cecdb19 | 2013-01-15 13:24:02 +0100 | [diff] [blame] | 1723 | /* |
Rafael J. Wysocki | 05404d8 | 2013-01-15 13:24:13 +0100 | [diff] [blame] | 1724 | * Execute acpi_bus_device_detach() as a post-order callback to detach |
| 1725 | * all ACPI drivers from the device nodes being removed. |
| 1726 | */ |
| 1727 | acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL, |
| 1728 | acpi_bus_device_detach, NULL, NULL); |
| 1729 | acpi_bus_device_detach(start->handle, 0, NULL, NULL); |
| 1730 | /* |
Rafael J. Wysocki | cecdb19 | 2013-01-15 13:24:02 +0100 | [diff] [blame] | 1731 | * Execute acpi_bus_remove() as a post-order callback to remove device |
| 1732 | * nodes in the given namespace scope. |
| 1733 | */ |
| 1734 | acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL, |
| 1735 | acpi_bus_remove, NULL, NULL); |
| 1736 | acpi_bus_remove(start->handle, 0, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | } |
Kristen Accardi | ceaba66 | 2006-02-23 17:56:01 -0800 | [diff] [blame] | 1738 | EXPORT_SYMBOL_GPL(acpi_bus_trim); |
| 1739 | |
Bjorn Helgaas | e8b945c | 2009-09-21 19:28:59 +0000 | [diff] [blame] | 1740 | static int acpi_bus_scan_fixed(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1742 | int result = 0; |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | /* |
| 1745 | * Enumerate all fixed-feature devices. |
| 1746 | */ |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 1747 | if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) { |
| 1748 | struct acpi_device *device = NULL; |
| 1749 | |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 1750 | result = acpi_add_single_object(&device, NULL, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1751 | ACPI_BUS_TYPE_POWER_BUTTON, |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 1752 | ACPI_STA_DEFAULT); |
| 1753 | if (result) |
| 1754 | return result; |
| 1755 | |
| 1756 | result = device_attach(&device->dev); |
| 1757 | if (result < 0) |
| 1758 | return result; |
| 1759 | |
Daniel Drake | c10d7a1 | 2012-05-10 00:08:43 +0100 | [diff] [blame] | 1760 | device_init_wakeup(&device->dev, true); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1761 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 1763 | if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) { |
| 1764 | struct acpi_device *device = NULL; |
| 1765 | |
Bjorn Helgaas | 5c478f4 | 2009-09-21 19:29:35 +0000 | [diff] [blame] | 1766 | result = acpi_add_single_object(&device, NULL, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1767 | ACPI_BUS_TYPE_SLEEP_BUTTON, |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 1768 | ACPI_STA_DEFAULT); |
| 1769 | if (result) |
| 1770 | return result; |
| 1771 | |
| 1772 | result = device_attach(&device->dev); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1773 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | |
Rafael J. Wysocki | 2c0d4fe | 2013-01-29 13:57:20 +0100 | [diff] [blame] | 1775 | return result < 0 ? result : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | } |
| 1777 | |
Bjorn Helgaas | e747f27 | 2009-03-24 16:49:43 -0600 | [diff] [blame] | 1778 | int __init acpi_scan_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | { |
| 1780 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | |
Patrick Mochel | 5b32726 | 2006-05-10 10:33:00 -0400 | [diff] [blame] | 1782 | result = bus_register(&acpi_bus_type); |
| 1783 | if (result) { |
| 1784 | /* We don't want to quit even if we failed to add suspend/resume */ |
| 1785 | printk(KERN_ERR PREFIX "Could not register bus type\n"); |
| 1786 | } |
| 1787 | |
Rafael J. Wysocki | 92ef2a2 | 2012-12-21 00:36:40 +0100 | [diff] [blame] | 1788 | acpi_pci_root_init(); |
Rafael J. Wysocki | 4daeaf6 | 2013-01-30 14:27:37 +0100 | [diff] [blame] | 1789 | acpi_pci_link_init(); |
Rafael J. Wysocki | 141a297 | 2013-01-30 14:27:40 +0100 | [diff] [blame] | 1790 | acpi_platform_init(); |
Mika Westerberg | 13176bb | 2013-01-17 09:59:33 +0000 | [diff] [blame] | 1791 | acpi_csrt_init(); |
Rafael J. Wysocki | 737f1a9 | 2013-02-08 23:52:39 +0100 | [diff] [blame] | 1792 | acpi_container_init(); |
Rafael J. Wysocki | 97d9a9e | 2010-11-25 00:10:02 +0100 | [diff] [blame] | 1793 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1794 | mutex_lock(&acpi_scan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | * Enumerate devices in the ACPI namespace. |
| 1797 | */ |
Rafael J. Wysocki | 0cd6ac5 | 2012-12-21 00:36:49 +0100 | [diff] [blame] | 1798 | result = acpi_bus_scan(ACPI_ROOT_OBJECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | if (result) |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1800 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | |
Rafael J. Wysocki | 0cd6ac5 | 2012-12-21 00:36:49 +0100 | [diff] [blame] | 1802 | result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | if (result) |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1804 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 1806 | result = acpi_bus_scan_fixed(); |
| 1807 | if (result) { |
| 1808 | acpi_device_unregister(acpi_root); |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1809 | goto out; |
Rafael J. Wysocki | b8bd759 | 2013-01-19 01:27:35 +0100 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | acpi_update_all_gpes(); |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1813 | |
Yinghai Lu | 668192b | 2013-01-21 13:20:48 -0800 | [diff] [blame] | 1814 | acpi_pci_root_hp_init(); |
| 1815 | |
Rafael J. Wysocki | 3757b94 | 2013-02-13 14:36:47 +0100 | [diff] [blame] | 1816 | out: |
| 1817 | mutex_unlock(&acpi_scan_lock); |
| 1818 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | } |