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