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