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