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