blob: 680bb5647701146dd7759d45dd59f0a1c21df440 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040022extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020025#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUS_DEVICE_NAME "System Bus"
27
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020030/*
31 * If set, devices will be hot-removed even if they cannot be put offline
32 * gracefully (from the kernel's standpoint).
33 */
34bool acpi_force_hot_remove;
35
Thomas Renninger620e1122010-10-01 10:54:00 +020036static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020037
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080038static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010039static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010040static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080041DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042LIST_HEAD(acpi_wakeup_device_list);
43
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080044struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080045 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080046 unsigned int instance_no;
47 struct list_head node;
48};
Thomas Renninger29b71a12007-07-23 14:43:51 +020049
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010050void acpi_scan_lock_acquire(void)
51{
52 mutex_lock(&acpi_scan_lock);
53}
54EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
55
56void acpi_scan_lock_release(void)
57{
58 mutex_unlock(&acpi_scan_lock);
59}
60EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
61
Rafael J. Wysockica589f92013-01-30 14:27:29 +010062int acpi_scan_add_handler(struct acpi_scan_handler *handler)
63{
64 if (!handler || !handler->attach)
65 return -EINVAL;
66
67 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
68 return 0;
69}
70
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010071int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
72 const char *hotplug_profile_name)
73{
74 int error;
75
76 error = acpi_scan_add_handler(handler);
77 if (error)
78 return error;
79
80 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
81 return 0;
82}
83
Thomas Renninger29b71a12007-07-23 14:43:51 +020084/*
85 * Creates hid/cid(s) string needed for modalias and uevent
86 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
87 * char *modalias: "acpi:IBM0001:ACPI0001"
Zhang Rui3d8e0092014-01-14 16:46:35 +080088 * Return: 0: no _HID and no _CID
89 * -EINVAL: output error
90 * -ENOMEM: output is truncated
Thomas Renninger29b71a12007-07-23 14:43:51 +020091*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020092static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
93 int size)
94{
Thomas Renninger29b71a12007-07-23 14:43:51 +020095 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080096 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060097 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020098
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020099 if (list_empty(&acpi_dev->pnp.ids))
100 return 0;
101
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800102 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +0200103 size -= len;
104
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600105 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
106 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui3d8e0092014-01-14 16:46:35 +0800107 if (count < 0)
108 return EINVAL;
109 if (count >= size)
110 return -ENOMEM;
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800111 len += count;
112 size -= count;
113 }
114
Thomas Renninger29b71a12007-07-23 14:43:51 +0200115 modalias[len] = '\0';
116 return len;
117}
118
Zhang Rui6eb24512014-01-14 16:46:36 +0800119/*
120 * Creates uevent modalias field for ACPI enumerated devices.
121 * Because the other buses does not support ACPI HIDs & CIDs.
122 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
123 * "acpi:IBM0001:ACPI0001"
124 */
125int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
126{
127 struct acpi_device *acpi_dev;
128 int len;
129
130 acpi_dev = ACPI_COMPANION(dev);
131 if (!acpi_dev)
132 return -ENODEV;
133
134 /* Fall back to bus specific way of modalias exporting */
135 if (list_empty(&acpi_dev->pnp.ids))
136 return -ENODEV;
137
138 if (add_uevent_var(env, "MODALIAS="))
139 return -ENOMEM;
140 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
141 sizeof(env->buf) - env->buflen);
142 if (len <= 0)
143 return len;
144 env->buflen += len;
145 return 0;
146}
147EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
148
149/*
150 * Creates modalias sysfs attribute for ACPI enumerated devices.
151 * Because the other buses does not support ACPI HIDs & CIDs.
152 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
153 * "acpi:IBM0001:ACPI0001"
154 */
155int acpi_device_modalias(struct device *dev, char *buf, int size)
156{
157 struct acpi_device *acpi_dev;
158 int len;
159
160 acpi_dev = ACPI_COMPANION(dev);
161 if (!acpi_dev)
162 return -ENODEV;
163
164 /* Fall back to bus specific way of modalias exporting */
165 if (list_empty(&acpi_dev->pnp.ids))
166 return -ENODEV;
167
168 len = create_modalias(acpi_dev, buf, size -1);
169 if (len <= 0)
170 return len;
171 buf[len++] = '\n';
172 return len;
173}
174EXPORT_SYMBOL_GPL(acpi_device_modalias);
175
Thomas Renninger29b71a12007-07-23 14:43:51 +0200176static ssize_t
177acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
178 struct acpi_device *acpi_dev = to_acpi_device(dev);
179 int len;
180
Thomas Renninger29b71a12007-07-23 14:43:51 +0200181 len = create_modalias(acpi_dev, buf, 1024);
182 if (len <= 0)
Zhang Rui3d8e0092014-01-14 16:46:35 +0800183 return len;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200184 buf[len++] = '\n';
185 return len;
186}
187static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
188
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100189static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
190 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200191{
192 struct acpi_device *device = NULL;
193 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200194 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200195 acpi_status status = AE_OK;
196
197 if (acpi_bus_get_device(handle, &device))
198 return AE_OK;
199
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100200 if (device->handler && !device->handler->hotplug.enabled) {
201 *ret_p = &device->dev;
202 return AE_SUPPORT;
203 }
204
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200205 mutex_lock(&device->physical_node_lock);
206
207 list_for_each_entry(pn, &device->physical_node_list, node) {
208 int ret;
209
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200210 if (second_pass) {
211 /* Skip devices offlined by the first pass. */
212 if (pn->put_online)
213 continue;
214 } else {
215 pn->put_online = false;
216 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200217 ret = device_offline(pn->dev);
218 if (acpi_force_hot_remove)
219 continue;
220
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200221 if (ret >= 0) {
222 pn->put_online = !ret;
223 } else {
224 *ret_p = pn->dev;
225 if (second_pass) {
226 status = AE_ERROR;
227 break;
228 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200229 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200230 }
231
232 mutex_unlock(&device->physical_node_lock);
233
234 return status;
235}
236
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100237static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
238 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200239{
240 struct acpi_device *device = NULL;
241 struct acpi_device_physical_node *pn;
242
243 if (acpi_bus_get_device(handle, &device))
244 return AE_OK;
245
246 mutex_lock(&device->physical_node_lock);
247
248 list_for_each_entry(pn, &device->physical_node_list, node)
249 if (pn->put_online) {
250 device_online(pn->dev);
251 pn->put_online = false;
252 }
253
254 mutex_unlock(&device->physical_node_lock);
255
256 return AE_OK;
257}
258
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100259static int acpi_scan_hot_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Yinghai Lu5993c462013-01-11 22:40:41 +0000261 acpi_handle handle = device->handle;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200262 struct device *errdev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100263 acpi_status status;
Toshi Kani882fd122013-03-13 19:29:26 +0000264 unsigned long long sta;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100265
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100266 /* If there is no handle, the device node has been unregistered. */
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100267 if (!handle) {
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100268 dev_dbg(&device->dev, "ACPI handle missing\n");
269 put_device(&device->dev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100270 return -EINVAL;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100271 }
272
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200273 /*
274 * Carry out two passes here and ignore errors in the first pass,
275 * because if the devices in question are memory blocks and
276 * CONFIG_MEMCG is set, one of the blocks may hold data structures
277 * that the other blocks depend on, but it is not known in advance which
278 * block holds them.
279 *
280 * If the first pass is successful, the second one isn't needed, though.
281 */
282 errdev = NULL;
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100283 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
284 NULL, acpi_bus_offline, (void *)false,
285 (void **)&errdev);
286 if (status == AE_SUPPORT) {
287 dev_warn(errdev, "Offline disabled.\n");
288 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
289 acpi_bus_online, NULL, NULL, NULL);
290 put_device(&device->dev);
291 return -EPERM;
292 }
293 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200294 if (errdev) {
295 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200296 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100297 NULL, acpi_bus_offline, (void *)true,
298 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200299 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100300 acpi_bus_offline(handle, 0, (void *)true,
301 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200302
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200303 if (errdev && !acpi_force_hot_remove) {
304 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100305 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200306 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100307 ACPI_UINT32_MAX, acpi_bus_online,
308 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200309 put_device(&device->dev);
310 return -EBUSY;
311 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200312 }
313
Zhang Rui26d46862008-04-29 02:35:48 -0400314 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100315 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400316
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100317 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200318
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100319 /* Device node has been unregistered. */
320 put_device(&device->dev);
Toshi Kanib3c450c2012-10-26 13:38:57 +0200321 device = NULL;
322
Jiang Liu7d2421f2013-06-29 00:24:40 +0800323 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /*
325 * TBD: _EJD support.
326 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800327 status = acpi_evaluate_ej0(handle);
328 if (status == AE_NOT_FOUND)
329 return -ENODEV;
330 else if (ACPI_FAILURE(status))
331 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100332
Toshi Kani882fd122013-03-13 19:29:26 +0000333 /*
334 * Verify if eject was indeed successful. If not, log an error
335 * message. No need to call _OST since _EJ0 call was made OK.
336 */
337 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
338 if (ACPI_FAILURE(status)) {
339 acpi_handle_warn(handle,
340 "Status check after eject failed (0x%x)\n", status);
341 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
342 acpi_handle_warn(handle,
343 "Eject incomplete - status 0x%llx\n", sta);
344 }
345
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100346 return 0;
347}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100348
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100349void acpi_bus_device_eject(void *data, u32 ost_src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100350{
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100351 struct acpi_device *device = data;
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100352 acpi_handle handle = device->handle;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100353 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200354 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100355
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200356 lock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100357 mutex_lock(&acpi_scan_lock);
358
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100359 if (ost_src == ACPI_NOTIFY_EJECT_REQUEST)
360 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
361 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
362
Rafael J. Wysockic1beb0b2013-11-14 00:54:08 +0100363 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100364 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100365
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200366 error = acpi_scan_hot_remove(device);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100367 if (error == -EPERM) {
368 goto err_support;
369 } else if (error) {
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200370 goto err_out;
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100373 out:
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100374 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200375 unlock_device_hotplug();
Zhang Ruic8d72a52009-06-22 11:31:16 +0800376 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100377
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100378 err_support:
379 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100380 err_out:
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100381 acpi_evaluate_hotplug_ost(handle, ost_src, ost_code, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100382 goto out;
383}
384
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100385static void acpi_scan_bus_device_check(void *data, u32 ost_source)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100386{
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100387 acpi_handle handle = data;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100388 struct acpi_device *device = NULL;
389 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
390 int error;
391
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200392 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200393 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100394
Rafael J. Wysocki8832f7e2013-07-08 02:01:53 +0200395 if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
396 acpi_bus_get_device(handle, &device);
397 if (device) {
398 dev_warn(&device->dev, "Attempt to re-insert\n");
399 goto out;
400 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100401 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100402 error = acpi_bus_scan(handle);
403 if (error) {
404 acpi_handle_warn(handle, "Namespace scan failure\n");
405 goto out;
406 }
407 error = acpi_bus_get_device(handle, &device);
408 if (error) {
409 acpi_handle_warn(handle, "Missing device node object\n");
410 goto out;
411 }
412 ost_code = ACPI_OST_SC_SUCCESS;
413 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
414 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
415
416 out:
417 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
418 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200419 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100420}
421
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000422static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
423{
424 u32 ost_status;
425
426 switch (type) {
427 case ACPI_NOTIFY_BUS_CHECK:
428 acpi_handle_debug(handle,
429 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
430 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
431 break;
432 case ACPI_NOTIFY_DEVICE_CHECK:
433 acpi_handle_debug(handle,
434 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
435 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
436 break;
437 case ACPI_NOTIFY_EJECT_REQUEST:
438 acpi_handle_debug(handle,
439 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
440 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
441 break;
442 default:
443 /* non-hotplug event; possibly handled by other handler */
444 return;
445 }
446
447 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
448}
449
450static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100451{
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000452 struct acpi_scan_handler *handler = data;
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100453 struct acpi_device *adev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100454 acpi_status status;
455
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000456 if (!handler->hotplug.enabled)
457 return acpi_hotplug_unsupported(handle, type);
458
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100459 switch (type) {
460 case ACPI_NOTIFY_BUS_CHECK:
461 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100462 break;
463 case ACPI_NOTIFY_DEVICE_CHECK:
464 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100465 break;
466 case ACPI_NOTIFY_EJECT_REQUEST:
467 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
Rafael J. Wysocki5beaee42013-11-14 00:54:00 +0100468 if (acpi_bus_get_device(handle, &adev))
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100469 goto err_out;
470
471 get_device(&adev->dev);
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100472 status = acpi_hotplug_execute(acpi_bus_device_eject, adev, type);
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100473 if (ACPI_SUCCESS(status))
474 return;
475
476 put_device(&adev->dev);
477 goto err_out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100478 default:
479 /* non-hotplug event; possibly handled by other handler */
480 return;
481 }
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100482 status = acpi_hotplug_execute(acpi_scan_bus_device_check, handle, type);
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100483 if (ACPI_SUCCESS(status))
484 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100485
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100486 err_out:
487 acpi_evaluate_hotplug_ost(handle, type,
488 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100489}
490
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100491static ssize_t real_power_state_show(struct device *dev,
492 struct device_attribute *attr, char *buf)
493{
494 struct acpi_device *adev = to_acpi_device(dev);
495 int state;
496 int ret;
497
498 ret = acpi_device_get_power(adev, &state);
499 if (ret)
500 return ret;
501
502 return sprintf(buf, "%s\n", acpi_power_state_string(state));
503}
504
505static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
506
507static ssize_t power_state_show(struct device *dev,
508 struct device_attribute *attr, char *buf)
509{
510 struct acpi_device *adev = to_acpi_device(dev);
511
512 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
513}
514
515static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800518acpi_eject_store(struct device *d, struct device_attribute *attr,
519 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800521 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100522 acpi_object_type not_used;
523 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100525 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100528 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
529 && !acpi_device->driver)
530 return -ENODEV;
531
532 status = acpi_get_type(acpi_device->handle, &not_used);
533 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
534 return -ENODEV;
535
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200536 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100537 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100538 get_device(&acpi_device->dev);
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100539 status = acpi_hotplug_execute(acpi_bus_device_eject, acpi_device,
540 ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200541 if (ACPI_SUCCESS(status))
542 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100543
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200544 put_device(&acpi_device->dev);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200545 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100546 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100547 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800550static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800552static ssize_t
553acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
554 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600556 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800557}
558static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
559
Lv Zheng923d4a42012-10-30 14:43:26 +0100560static ssize_t acpi_device_uid_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 struct acpi_device *acpi_dev = to_acpi_device(dev);
564
565 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
566}
567static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
568
569static ssize_t acpi_device_adr_show(struct device *dev,
570 struct device_attribute *attr, char *buf)
571{
572 struct acpi_device *acpi_dev = to_acpi_device(dev);
573
574 return sprintf(buf, "0x%08x\n",
575 (unsigned int)(acpi_dev->pnp.bus_address));
576}
577static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
578
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800579static ssize_t
580acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
581 struct acpi_device *acpi_dev = to_acpi_device(dev);
582 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
583 int result;
584
585 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600586 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800587 goto end;
588
589 result = sprintf(buf, "%s\n", (char*)path.pointer);
590 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600591end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800592 return result;
593}
594static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
595
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600596/* sysfs file that shows description text from the ACPI _STR method */
597static ssize_t description_show(struct device *dev,
598 struct device_attribute *attr,
599 char *buf) {
600 struct acpi_device *acpi_dev = to_acpi_device(dev);
601 int result;
602
603 if (acpi_dev->pnp.str_obj == NULL)
604 return 0;
605
606 /*
607 * The _STR object contains a Unicode identifier for a device.
608 * We need to convert to utf-8 so it can be displayed.
609 */
610 result = utf16s_to_utf8s(
611 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
612 acpi_dev->pnp.str_obj->buffer.length,
613 UTF16_LITTLE_ENDIAN, buf,
614 PAGE_SIZE);
615
616 buf[result++] = '\n';
617
618 return result;
619}
620static DEVICE_ATTR(description, 0444, description_show, NULL);
621
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100622static ssize_t
623acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
624 char *buf) {
625 struct acpi_device *acpi_dev = to_acpi_device(dev);
626
627 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
628}
629static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
630
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800631static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600633 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800634 acpi_status status;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100635 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800636 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800638 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800639 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800640 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600641 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800642 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600643 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800644 goto end;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200647 if (!list_empty(&dev->pnp.ids)) {
648 result = device_create_file(&dev->dev, &dev_attr_hid);
649 if (result)
650 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200652 result = device_create_file(&dev->dev, &dev_attr_modalias);
653 if (result)
654 goto end;
655 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200656
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600657 /*
658 * If device has _STR, 'description' file is created
659 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800660 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600661 status = acpi_evaluate_object(dev->handle, "_STR",
662 NULL, &buffer);
663 if (ACPI_FAILURE(status))
664 buffer.pointer = NULL;
665 dev->pnp.str_obj = buffer.pointer;
666 result = device_create_file(&dev->dev, &dev_attr_description);
667 if (result)
668 goto end;
669 }
670
Toshi Kanid4e1a692013-03-04 21:30:41 +0000671 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100672 result = device_create_file(&dev->dev, &dev_attr_adr);
673 if (dev->pnp.unique_id)
674 result = device_create_file(&dev->dev, &dev_attr_uid);
675
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100676 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
677 if (ACPI_SUCCESS(status)) {
678 dev->pnp.sun = (unsigned long)sun;
679 result = device_create_file(&dev->dev, &dev_attr_sun);
680 if (result)
681 goto end;
682 } else {
683 dev->pnp.sun = (unsigned long)-1;
684 }
685
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800686 /*
687 * If device has _EJ0, 'eject' file is created that is used to trigger
688 * hot-removal function from userland.
689 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800690 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800691 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100692 if (result)
693 return result;
694 }
695
696 if (dev->flags.power_manageable) {
697 result = device_create_file(&dev->dev, &dev_attr_power_state);
698 if (result)
699 return result;
700
701 if (dev->power.flags.power_resources)
702 result = device_create_file(&dev->dev,
703 &dev_attr_real_power_state);
704 }
705
Alex Chiang0c526d92009-05-14 08:31:37 -0600706end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800707 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800710static void acpi_device_remove_files(struct acpi_device *dev)
711{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100712 if (dev->flags.power_manageable) {
713 device_remove_file(&dev->dev, &dev_attr_power_state);
714 if (dev->power.flags.power_resources)
715 device_remove_file(&dev->dev,
716 &dev_attr_real_power_state);
717 }
718
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800719 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600720 * If device has _STR, remove 'description' file
721 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800722 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600723 kfree(dev->pnp.str_obj);
724 device_remove_file(&dev->dev, &dev_attr_description);
725 }
726 /*
727 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800728 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800729 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800730 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800731
Jiang Liu952c63e2013-06-29 00:24:38 +0800732 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100733 device_remove_file(&dev->dev, &dev_attr_sun);
734
Lv Zheng923d4a42012-10-30 14:43:26 +0100735 if (dev->pnp.unique_id)
736 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000737 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100738 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600739 device_remove_file(&dev->dev, &dev_attr_modalias);
740 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600741 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800742 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800743}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800745 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200747
Mika Westerbergcf761af2012-10-31 22:44:41 +0100748static const struct acpi_device_id *__acpi_match_device(
749 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200750{
751 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600752 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200753
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800754 /*
755 * If the device is not present, it is unnecessary to load device
756 * driver for it.
757 */
758 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100759 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800760
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600761 for (id = ids; id->id[0]; id++)
762 list_for_each_entry(hwid, &device->pnp.ids, list)
763 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100764 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200765
Mika Westerbergcf761af2012-10-31 22:44:41 +0100766 return NULL;
767}
768
769/**
770 * acpi_match_device - Match a struct device against a given list of ACPI IDs
771 * @ids: Array of struct acpi_device_id object to match against.
772 * @dev: The device structure to match.
773 *
774 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
775 * object for that handle and use that object to match against a given list of
776 * device IDs.
777 *
778 * Return a pointer to the first matching ID on success or %NULL on failure.
779 */
780const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
781 const struct device *dev)
782{
783 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100784 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100785
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100786 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100787 return NULL;
788
789 return __acpi_match_device(adev, ids);
790}
791EXPORT_SYMBOL_GPL(acpi_match_device);
792
793int acpi_match_device_ids(struct acpi_device *device,
794 const struct acpi_device_id *ids)
795{
796 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200797}
798EXPORT_SYMBOL(acpi_match_device_ids);
799
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100800static void acpi_free_power_resources_lists(struct acpi_device *device)
801{
802 int i;
803
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100804 if (device->wakeup.flags.valid)
805 acpi_power_resources_list_free(&device->wakeup.resources);
806
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100807 if (!device->flags.power_manageable)
808 return;
809
810 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
811 struct acpi_device_power_state *ps = &device->power.states[i];
812 acpi_power_resources_list_free(&ps->resources);
813 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600814}
815
Patrick Mochel1890a972006-12-07 20:56:31 +0800816static void acpi_device_release(struct device *dev)
817{
818 struct acpi_device *acpi_dev = to_acpi_device(dev);
819
Toshi Kanic0af4172013-03-04 21:30:42 +0000820 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100821 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800822 kfree(acpi_dev);
823}
824
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800825static int acpi_bus_match(struct device *dev, struct device_driver *drv)
826{
827 struct acpi_device *acpi_dev = to_acpi_device(dev);
828 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
829
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100830 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100831 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800832}
833
Kay Sievers7eff2e72007-08-14 15:15:12 +0200834static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800835{
836 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200837 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800838
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200839 if (list_empty(&acpi_dev->pnp.ids))
840 return 0;
841
Kay Sievers7eff2e72007-08-14 15:15:12 +0200842 if (add_uevent_var(env, "MODALIAS="))
843 return -ENOMEM;
844 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
845 sizeof(env->buf) - env->buflen);
Zhang Rui3d8e0092014-01-14 16:46:35 +0800846 if (len <= 0)
847 return len;
Kay Sievers7eff2e72007-08-14 15:15:12 +0200848 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return 0;
850}
851
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000852static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
853{
854 struct acpi_device *device = data;
855
856 device->driver->ops.notify(device, event);
857}
858
859static acpi_status acpi_device_notify_fixed(void *data)
860{
861 struct acpi_device *device = data;
862
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000863 /* Fixed hardware devices have no handles */
864 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000865 return AE_OK;
866}
867
868static int acpi_device_install_notify_handler(struct acpi_device *device)
869{
870 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000871
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000872 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000873 status =
874 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
875 acpi_device_notify_fixed,
876 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000877 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000878 status =
879 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
880 acpi_device_notify_fixed,
881 device);
882 else
883 status = acpi_install_notify_handler(device->handle,
884 ACPI_DEVICE_NOTIFY,
885 acpi_device_notify,
886 device);
887
888 if (ACPI_FAILURE(status))
889 return -EINVAL;
890 return 0;
891}
892
893static void acpi_device_remove_notify_handler(struct acpi_device *device)
894{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000895 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000896 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
897 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000898 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000899 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
900 acpi_device_notify_fixed);
901 else
902 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
903 acpi_device_notify);
904}
905
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200906static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800907{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800908 struct acpi_device *acpi_dev = to_acpi_device(dev);
909 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
910 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200912 if (acpi_dev->handler)
913 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000914
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200915 if (!acpi_drv->ops.add)
916 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800917
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200918 ret = acpi_drv->ops.add(acpi_dev);
919 if (ret)
920 return ret;
921
922 acpi_dev->driver = acpi_drv;
923 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
924 "Driver [%s] successfully bound to device [%s]\n",
925 acpi_drv->name, acpi_dev->pnp.bus_id));
926
927 if (acpi_drv->ops.notify) {
928 ret = acpi_device_install_notify_handler(acpi_dev);
929 if (ret) {
930 if (acpi_drv->ops.remove)
931 acpi_drv->ops.remove(acpi_dev);
932
933 acpi_dev->driver = NULL;
934 acpi_dev->driver_data = NULL;
935 return ret;
936 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800937 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200938
939 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
940 acpi_drv->name, acpi_dev->pnp.bus_id));
941 get_device(dev);
942 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800943}
944
945static int acpi_device_remove(struct device * dev)
946{
947 struct acpi_device *acpi_dev = to_acpi_device(dev);
948 struct acpi_driver *acpi_drv = acpi_dev->driver;
949
950 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000951 if (acpi_drv->ops.notify)
952 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800953 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100954 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800955 }
956 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700957 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800958
959 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800960 return 0;
961}
962
David Brownell55955aa2007-05-08 00:28:35 -0700963struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800964 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800965 .match = acpi_bus_match,
966 .probe = acpi_device_probe,
967 .remove = acpi_device_remove,
968 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969};
970
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200971static void acpi_bus_data_handler(acpi_handle handle, void *context)
972{
973 /* Intentionally empty. */
974}
975
976int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
977{
978 acpi_status status;
979
980 if (!device)
981 return -EINVAL;
982
983 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
984 if (ACPI_FAILURE(status) || !*device) {
985 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
986 handle));
987 return -ENODEV;
988 }
989 return 0;
990}
Rafael J. Wysocki65859252013-10-01 23:02:43 +0200991EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200992
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100993int acpi_device_add(struct acpi_device *device,
994 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800996 int result;
997 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
998 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000999
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001000 if (device->handle) {
1001 acpi_status status;
1002
1003 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
1004 device);
1005 if (ACPI_FAILURE(status)) {
1006 acpi_handle_err(device->handle,
1007 "Unable to attach device data\n");
1008 return -ENODEV;
1009 }
1010 }
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /*
1013 * Linkage
1014 * -------
1015 * Link this device to its parent and siblings.
1016 */
1017 INIT_LIST_HEAD(&device->children);
1018 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001020 INIT_LIST_HEAD(&device->physical_node_list);
1021 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001023 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1024 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001025 pr_err(PREFIX "Memory allocation error\n");
1026 result = -ENOMEM;
1027 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001028 }
1029
Shaohua Li90905892009-04-07 10:24:29 +08001030 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001031 /*
1032 * Find suitable bus_id and instance number in acpi_bus_id_list
1033 * If failed, create one and link it into acpi_bus_id_list
1034 */
1035 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001036 if (!strcmp(acpi_device_bus_id->bus_id,
1037 acpi_device_hid(device))) {
1038 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001039 found = 1;
1040 kfree(new_bus_id);
1041 break;
1042 }
1043 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001044 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001045 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001046 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001047 acpi_device_bus_id->instance_no = 0;
1048 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1049 }
Kay Sievers07944692008-10-30 01:18:59 +01001050 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001051
Len Brown33b57152008-12-15 22:09:26 -05001052 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (device->wakeup.flags.valid)
1056 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001057 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Patrick Mochel1890a972006-12-07 20:56:31 +08001059 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001060 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001061 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001062 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001063 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001064 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001065 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001066 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001067 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001068
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001069 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001070 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001071 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1072 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001073
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001074 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001075
1076 err:
Shaohua Li90905892009-04-07 10:24:29 +08001077 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001078 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001079 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001080 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001081 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001082
1083 err_detach:
1084 acpi_detach_data(device->handle, acpi_bus_data_handler);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001085 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Rafael J. Wysockib17b5372013-01-15 13:23:44 +01001088static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Shaohua Li90905892009-04-07 10:24:29 +08001090 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001091 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001095 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +08001098
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001099 acpi_power_add_remove_device(device, false);
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001100 acpi_device_remove_files(device);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +01001101 if (device->remove)
1102 device->remove(device);
1103
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001104 device_del(&device->dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001105 /*
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001106 * Transition the device to D3cold to drop the reference counts of all
1107 * power resources the device depends on and turn off the ones that have
1108 * no more references.
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001109 */
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001110 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001111 device->handle = NULL;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001112 put_device(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Zhang Rui9e89dde2006-12-07 20:56:16 +08001115/* --------------------------------------------------------------------------
1116 Driver Management
1117 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001119 * acpi_bus_register_driver - register a driver with the ACPI bus
1120 * @driver: driver being registered
1121 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001123 * devices that match the driver's criteria and binds. Returns zero for
1124 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
Len Brown4be44fc2005-08-05 00:44:28 -04001126int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Patrick Mochel1890a972006-12-07 20:56:31 +08001128 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001131 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001132 driver->drv.name = driver->name;
1133 driver->drv.bus = &acpi_bus_type;
1134 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Patrick Mochel1890a972006-12-07 20:56:31 +08001136 ret = driver_register(&driver->drv);
1137 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Len Brown4be44fc2005-08-05 00:44:28 -04001140EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001143 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001144 * @driver: driver to unregister
1145 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1147 * devices that match the driver's criteria and unbinds.
1148 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001149void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Patrick Mochel1890a972006-12-07 20:56:31 +08001151 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
Len Brown4be44fc2005-08-05 00:44:28 -04001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154EXPORT_SYMBOL(acpi_bus_unregister_driver);
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/* --------------------------------------------------------------------------
1157 Device Enumeration
1158 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001159static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1160{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001161 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001162 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001163
1164 /*
1165 * Fixed hardware devices do not appear in the namespace and do not
1166 * have handles, but we fabricate acpi_devices for them, so we have
1167 * to deal with them specially.
1168 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001169 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001170 return acpi_root;
1171
1172 do {
1173 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001174 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001175 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1176 } while (acpi_bus_get_device(handle, &device));
1177 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001178}
1179
Len Brownc8f7a622006-07-09 17:22:28 -04001180acpi_status
1181acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1182{
1183 acpi_status status;
1184 acpi_handle tmp;
1185 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1186 union acpi_object *obj;
1187
1188 status = acpi_get_handle(handle, "_EJD", &tmp);
1189 if (ACPI_FAILURE(status))
1190 return status;
1191
1192 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1193 if (ACPI_SUCCESS(status)) {
1194 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001195 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1196 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001197 kfree(buffer.pointer);
1198 }
1199 return status;
1200}
1201EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1202
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001203static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1204 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001206 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1207 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001209 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001210 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001212 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001213 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001215 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001217 /* _PRW */
1218 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1219 if (ACPI_FAILURE(status)) {
1220 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001221 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001222 }
1223
1224 package = (union acpi_object *)buffer.pointer;
1225
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001226 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001227 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001230 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001231 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (element->type == ACPI_TYPE_PACKAGE) {
1234 if ((element->package.count < 2) ||
1235 (element->package.elements[0].type !=
1236 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001237 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001238 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001239
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001240 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001242 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 (u32) element->package.elements[1].integer.value;
1244 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001245 wakeup->gpe_device = NULL;
1246 wakeup->gpe_number = element->integer.value;
1247 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001248 goto out;
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001252 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001253 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001254
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001255 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001257 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1258 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001261 if (!list_empty(&wakeup->resources)) {
1262 int sleep_state;
1263
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001264 err = acpi_power_wakeup_list_init(&wakeup->resources,
1265 &sleep_state);
1266 if (err) {
1267 acpi_handle_warn(handle, "Retrieving current states "
1268 "of wakeup power resources failed\n");
1269 acpi_power_resources_list_free(&wakeup->resources);
1270 goto out;
1271 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001272 if (sleep_state < wakeup->sleep_state) {
1273 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1274 "(S%d) by S%d from power resources\n",
1275 (int)wakeup->sleep_state, sleep_state);
1276 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Lin Mingbba63a22010-12-13 13:39:17 +08001279 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001280
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001281 out:
1282 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001283 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001286static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001288 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001289 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001290 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001291 {"PNP0C0E", 0},
1292 {"", 0},
1293 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001294 acpi_status status;
1295 acpi_event_status event_status;
1296
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001297 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001298
1299 /* Power button, Lid switch always enable wakeup */
1300 if (!acpi_match_device_ids(device, button_device_ids)) {
1301 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001302 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1303 /* Do not use Lid/sleep button for S5 wakeup */
1304 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1305 device->wakeup.sleep_state = ACPI_STATE_S4;
1306 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001307 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001308 return;
1309 }
1310
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001311 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1312 device->wakeup.gpe_number,
1313 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001314 if (status == AE_OK)
1315 device->wakeup.flags.run_wake =
1316 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1317}
1318
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001319static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001320{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001321 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001322
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001323 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001324 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001325 return;
1326
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001327 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1328 &device->wakeup);
1329 if (err) {
1330 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001331 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001335 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001336 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001337 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1338 * system for the ACPI device with the _PRW object.
1339 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1340 * So it is necessary to call _DSW object first. Only when it is not
1341 * present will the _PSW object used.
1342 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001343 err = acpi_device_sleep_wake(device, 0, 0, 0);
1344 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001345 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1346 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001349static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001351 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001352 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1353 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001354 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001355
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001356 INIT_LIST_HEAD(&ps->resources);
1357
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001358 /* Evaluate "_PRx" to get referenced power resources */
1359 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1360 if (ACPI_SUCCESS(status)) {
1361 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001362
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001363 if (buffer.length && package
1364 && package->type == ACPI_TYPE_PACKAGE
1365 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001366 int err = acpi_extract_power_resources(package, 0,
1367 &ps->resources);
1368 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001369 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001370 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001371 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001372 }
1373
1374 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001375 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001376 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001377 ps->flags.explicit_set = 1;
1378
1379 /*
1380 * State is valid if there are means to put the device into it.
1381 * D3hot is only valid if _PR3 present.
1382 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001383 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001384 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1385 ps->flags.valid = 1;
1386 ps->flags.os_accessible = 1;
1387 }
1388
1389 ps->power = -1; /* Unknown - driver assigned */
1390 ps->latency = -1; /* Unknown - driver assigned */
1391}
1392
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001393static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001395 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001397 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001398 if (!acpi_has_method(device->handle, "_PS0") &&
1399 !acpi_has_method(device->handle, "_PR0"))
1400 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001401
1402 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001405 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001407 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001408 device->power.flags.explicit_get = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001409 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001410 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001413 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001415 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1416 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001418 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Zhang Rui9e89dde2006-12-07 20:56:16 +08001420 /* Set defaults for D0 and D3 states (always valid) */
1421 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1422 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001423 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1424 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001426 /* Set D3cold's explicit_set flag if _PS3 exists. */
1427 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1428 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1429
Aaron Lu1399dfc2012-11-21 23:33:40 +01001430 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1431 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1432 device->power.flags.power_resources)
1433 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1434
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001435 if (acpi_bus_init_power(device)) {
1436 acpi_free_power_resources_lists(device);
1437 device->flags.power_manageable = 0;
1438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001441static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001444 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 device->flags.dynamic_status = 1;
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001448 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 device->flags.removable = 1;
1450
1451 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001452 if (acpi_has_method(device->handle, "_EJD") ||
1453 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001457static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Len Brown4be44fc2005-08-05 00:44:28 -04001459 char bus_id[5] = { '?', 0 };
1460 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1461 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 /*
1464 * Bus ID
1465 * ------
1466 * The device's Bus ID is simply the object name.
1467 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1468 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001469 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001471 return;
1472 }
1473
1474 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 case ACPI_BUS_TYPE_POWER_BUTTON:
1476 strcpy(device->pnp.bus_id, "PWRF");
1477 break;
1478 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1479 strcpy(device->pnp.bus_id, "SLPF");
1480 break;
1481 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001482 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* Clean up trailing underscores (if any) */
1484 for (i = 3; i > 1; i--) {
1485 if (bus_id[i] == '_')
1486 bus_id[i] = '\0';
1487 else
1488 break;
1489 }
1490 strcpy(device->pnp.bus_id, bus_id);
1491 break;
1492 }
1493}
1494
Zhang Rui54735262007-01-11 02:09:09 -05001495/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001496 * acpi_ata_match - see if an acpi object is an ATA device
1497 *
1498 * If an acpi object has one of the ACPI ATA methods defined,
1499 * then we can safely call it an ATA device.
1500 */
1501bool acpi_ata_match(acpi_handle handle)
1502{
1503 return acpi_has_method(handle, "_GTF") ||
1504 acpi_has_method(handle, "_GTM") ||
1505 acpi_has_method(handle, "_STM") ||
1506 acpi_has_method(handle, "_SDD");
1507}
1508
1509/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001510 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001511 *
1512 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1513 * then we can safely call it an ejectable drive bay
1514 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001515bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001516{
Zhang Rui54735262007-01-11 02:09:09 -05001517 acpi_handle phandle;
1518
Jiang Liu952c63e2013-06-29 00:24:38 +08001519 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001520 return false;
1521 if (acpi_ata_match(handle))
1522 return true;
1523 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1524 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001525
Jiang Liuebf4df82013-06-29 00:24:41 +08001526 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001527}
1528
Frank Seidel3620f2f2007-12-07 13:20:34 +01001529/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001530 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001531 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001532bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001533{
Jiang Liuebf4df82013-06-29 00:24:41 +08001534 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001535}
1536
Thomas Renninger620e1122010-10-01 10:54:00 +02001537const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001538{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001539 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001540
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001541 if (list_empty(&device->pnp.ids))
1542 return dummy_hid;
1543
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001544 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1545 return hid->id;
1546}
1547EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001548
Toshi Kanid4e1a692013-03-04 21:30:41 +00001549static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001550{
1551 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001552
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001553 id = kmalloc(sizeof(*id), GFP_KERNEL);
1554 if (!id)
1555 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001556
Thomas Meyer581de592011-08-06 11:32:56 +02001557 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001558 if (!id->id) {
1559 kfree(id);
1560 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001561 }
1562
Toshi Kanid4e1a692013-03-04 21:30:41 +00001563 list_add_tail(&id->list, &pnp->ids);
1564 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001565}
1566
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001567/*
1568 * Old IBM workstations have a DSDT bug wherein the SMBus object
1569 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1570 * prefix. Work around this.
1571 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001572static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001573{
Jiang Liuebf4df82013-06-29 00:24:41 +08001574 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1575 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001576
1577 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001578 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001579
1580 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001581 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1582 strcmp("SMBS", path.pointer))
1583 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001584
1585 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001586 if (acpi_has_method(handle, "SBI") &&
1587 acpi_has_method(handle, "SBR") &&
1588 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001589 return true;
1590
1591 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001592}
1593
Toshi Kanic0af4172013-03-04 21:30:42 +00001594static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1595 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Len Brown4be44fc2005-08-05 00:44:28 -04001597 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001598 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001599 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001600 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Toshi Kanic0af4172013-03-04 21:30:42 +00001602 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001604 if (handle == ACPI_ROOT_OBJECT) {
1605 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001606 break;
1607 }
1608
Toshi Kanic0af4172013-03-04 21:30:42 +00001609 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001611 pr_err(PREFIX "%s: Error reading device info\n",
1612 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return;
1614 }
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001617 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001618 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001619 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001620 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001621 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001624 pnp->bus_address = info->address;
1625 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
Lv Zhengccf78042012-10-30 14:41:07 +01001627 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001628 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001629 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001630
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001631 kfree(info);
1632
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001633 /*
1634 * Some devices don't reliably have _HIDs & _CIDs, so add
1635 * synthetic HIDs to make sure drivers can find them.
1636 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001637 if (acpi_is_video_device(handle))
1638 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001639 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001640 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001641 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001642 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001643 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001644 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1645 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1646 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1647 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1648 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001649 }
Zhang Rui54735262007-01-11 02:09:09 -05001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 break;
1652 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001653 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 break;
1655 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001656 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001659 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001662 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 break;
1664 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001665 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 break;
1667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
Toshi Kanic0af4172013-03-04 21:30:42 +00001670void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1671{
1672 struct acpi_hardware_id *id, *tmp;
1673
1674 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1675 kfree(id->id);
1676 kfree(id);
1677 }
1678 kfree(pnp->unique_id);
1679}
1680
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001681void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1682 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001684 INIT_LIST_HEAD(&device->pnp.ids);
1685 device->device_type = type;
1686 device->handle = handle;
1687 device->parent = acpi_bus_get_parent(handle);
1688 STRUCT_TO_INT(device->status) = sta;
1689 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001690 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001691 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001692 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001693 device_initialize(&device->dev);
1694 dev_set_uevent_suppress(&device->dev, true);
1695}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001696
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001697void acpi_device_add_finalize(struct acpi_device *device)
1698{
1699 dev_set_uevent_suppress(&device->dev, false);
1700 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001703static int acpi_add_single_object(struct acpi_device **child,
1704 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001705 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001707 int result;
1708 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001709 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Burman Yan36bcbec2006-12-19 12:56:11 -08001711 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001713 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001714 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001717 acpi_init_device_object(device, handle, type, sta);
1718 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001719 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001721 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001722 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001723 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001724 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001727 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001728 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001729 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1730 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1731 dev_name(&device->dev), (char *) buffer.pointer,
1732 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1733 kfree(buffer.pointer);
1734 *child = device;
1735 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001736}
1737
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001738static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1739 unsigned long long *sta)
1740{
1741 acpi_status status;
1742 acpi_object_type acpi_type;
1743
1744 status = acpi_get_type(handle, &acpi_type);
1745 if (ACPI_FAILURE(status))
1746 return -ENODEV;
1747
1748 switch (acpi_type) {
1749 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1750 case ACPI_TYPE_DEVICE:
1751 *type = ACPI_BUS_TYPE_DEVICE;
1752 status = acpi_bus_get_status_handle(handle, sta);
1753 if (ACPI_FAILURE(status))
1754 return -ENODEV;
1755 break;
1756 case ACPI_TYPE_PROCESSOR:
1757 *type = ACPI_BUS_TYPE_PROCESSOR;
1758 status = acpi_bus_get_status_handle(handle, sta);
1759 if (ACPI_FAILURE(status))
1760 return -ENODEV;
1761 break;
1762 case ACPI_TYPE_THERMAL:
1763 *type = ACPI_BUS_TYPE_THERMAL;
1764 *sta = ACPI_STA_DEFAULT;
1765 break;
1766 case ACPI_TYPE_POWER:
1767 *type = ACPI_BUS_TYPE_POWER;
1768 *sta = ACPI_STA_DEFAULT;
1769 break;
1770 default:
1771 return -ENODEV;
1772 }
1773
1774 return 0;
1775}
1776
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001777static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1778 char *idstr,
1779 const struct acpi_device_id **matchid)
1780{
1781 const struct acpi_device_id *devid;
1782
1783 for (devid = handler->ids; devid->id[0]; devid++)
1784 if (!strcmp((char *)devid->id, idstr)) {
1785 if (matchid)
1786 *matchid = devid;
1787
1788 return true;
1789 }
1790
1791 return false;
1792}
1793
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001794static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1795 const struct acpi_device_id **matchid)
1796{
1797 struct acpi_scan_handler *handler;
1798
1799 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1800 if (acpi_scan_handler_matching(handler, idstr, matchid))
1801 return handler;
1802
1803 return NULL;
1804}
1805
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001806void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1807{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001808 if (!!hotplug->enabled == !!val)
1809 return;
1810
1811 mutex_lock(&acpi_scan_lock);
1812
1813 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001814
1815 mutex_unlock(&acpi_scan_lock);
1816}
1817
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001818static void acpi_scan_init_hotplug(acpi_handle handle, int type)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001819{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001820 struct acpi_device_pnp pnp = {};
1821 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001822 struct acpi_scan_handler *handler;
1823
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001824 INIT_LIST_HEAD(&pnp.ids);
1825 acpi_set_pnp_ids(handle, &pnp, type);
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001826
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001827 if (!pnp.type.hardware_id)
Catalin Marinas7a26b532013-05-15 16:49:35 +00001828 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001829
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001830 /*
1831 * This relies on the fact that acpi_install_notify_handler() will not
1832 * install the same notify handler routine twice for the same handle.
1833 */
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001834 list_for_each_entry(hwid, &pnp.ids, list) {
1835 handler = acpi_scan_match_handler(hwid->id, NULL);
Toshi Kanica499fc2013-11-20 14:25:34 +01001836 if (handler && !handler->hotplug.ignore) {
Toshi Kani2cbb14f2013-03-05 22:33:31 +00001837 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1838 acpi_hotplug_notify_cb, handler);
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001839 break;
1840 }
1841 }
1842
Catalin Marinas7a26b532013-05-15 16:49:35 +00001843out:
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001844 acpi_free_pnp_ids(&pnp);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001845}
1846
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001847static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1848 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001850 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001851 int type;
1852 unsigned long long sta;
1853 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001854
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001855 acpi_bus_get_device(handle, &device);
1856 if (device)
1857 goto out;
1858
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001859 result = acpi_bus_type_and_status(handle, &type, &sta);
1860 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001861 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001863 if (type == ACPI_BUS_TYPE_POWER) {
1864 acpi_add_power_resource(handle);
1865 return AE_OK;
1866 }
1867
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001868 acpi_scan_init_hotplug(handle, type);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001869
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001870 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001871 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001872 struct acpi_device_wakeup wakeup;
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001873
Jiang Liu952c63e2013-06-29 00:24:38 +08001874 if (acpi_has_method(handle, "_PRW")) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001875 acpi_bus_extract_wakeup_device_power_package(handle,
1876 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001877 acpi_power_resources_list_free(&wakeup.resources);
1878 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001879 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001882 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001883 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001884 return AE_CTRL_DEPTH;
1885
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001886 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001887 if (!*return_value)
1888 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001889
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001890 return AE_OK;
1891}
1892
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001893static int acpi_scan_attach_handler(struct acpi_device *device)
1894{
1895 struct acpi_hardware_id *hwid;
1896 int ret = 0;
1897
1898 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001899 const struct acpi_device_id *devid;
1900 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001901
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001902 handler = acpi_scan_match_handler(hwid->id, &devid);
1903 if (handler) {
1904 ret = handler->attach(device, devid);
1905 if (ret > 0) {
1906 device->handler = handler;
1907 break;
1908 } else if (ret < 0) {
1909 break;
1910 }
1911 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001912 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001913 return ret;
1914}
1915
Rafael J. Wysocki0fc300b02012-12-21 00:36:41 +01001916static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1917 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001918{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001919 struct acpi_device *device;
1920 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001921 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001922
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001923 /*
1924 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1925 * namespace walks prematurely.
1926 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001927 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001928 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001929
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001930 if (acpi_bus_get_device(handle, &device))
1931 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001932
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02001933 if (device->handler)
1934 return AE_OK;
1935
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001936 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001937 if (ret < 0)
1938 return AE_CTRL_DEPTH;
1939
1940 device->flags.match_driver = true;
1941 if (ret > 0)
1942 return AE_OK;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001943
1944 ret = device_attach(&device->dev);
1945 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001948/**
1949 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1950 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001951 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001952 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1953 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001954 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001955 * If no devices were found, -ENODEV is returned, but it does not mean that
1956 * there has been a real error. There just have been no suitable ACPI objects
1957 * in the table trunk from which the kernel could create a device and add an
1958 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001959 *
1960 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001961 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001962int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001963{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001965 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001966
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001967 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001969 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001970
Thomas Renningerd2f66502010-01-29 17:48:51 +01001971 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001972 error = -ENODEV;
1973 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001974 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b02012-12-21 00:36:41 +01001975 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01001976
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001977 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001978}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001979EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001981static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1982 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001984 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001986 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001987 struct acpi_scan_handler *dev_handler = device->handler;
1988
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001989 if (dev_handler) {
1990 if (dev_handler->detach)
1991 dev_handler->detach(device);
1992
1993 device->handler = NULL;
1994 } else {
1995 device_release_driver(&device->dev);
1996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001998 return AE_OK;
1999}
2000
2001static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
2002 void *not_used, void **ret_not_used)
2003{
2004 struct acpi_device *device = NULL;
2005
2006 if (!acpi_bus_get_device(handle, &device))
2007 acpi_device_unregister(device);
2008
2009 return AE_OK;
2010}
2011
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002012/**
2013 * acpi_bus_trim - Remove ACPI device node and all of its descendants
2014 * @start: Root of the ACPI device nodes subtree to remove.
2015 *
2016 * Must be called under acpi_scan_lock.
2017 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01002018void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002020 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002021 * Execute acpi_bus_device_detach() as a post-order callback to detach
2022 * all ACPI drivers from the device nodes being removed.
2023 */
2024 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2025 acpi_bus_device_detach, NULL, NULL);
2026 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
2027 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002028 * Execute acpi_bus_remove() as a post-order callback to remove device
2029 * nodes in the given namespace scope.
2030 */
2031 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2032 acpi_bus_remove, NULL, NULL);
2033 acpi_bus_remove(start->handle, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034}
Kristen Accardiceaba662006-02-23 17:56:01 -08002035EXPORT_SYMBOL_GPL(acpi_bus_trim);
2036
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002037static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Len Brown4be44fc2005-08-05 00:44:28 -04002039 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 /*
2042 * Enumerate all fixed-feature devices.
2043 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002044 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2045 struct acpi_device *device = NULL;
2046
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002047 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002048 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002049 ACPI_STA_DEFAULT);
2050 if (result)
2051 return result;
2052
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002053 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002054 result = device_attach(&device->dev);
2055 if (result < 0)
2056 return result;
2057
Daniel Drakec10d7a12012-05-10 00:08:43 +01002058 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002061 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2062 struct acpi_device *device = NULL;
2063
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002064 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002065 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002066 ACPI_STA_DEFAULT);
2067 if (result)
2068 return result;
2069
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002070 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002071 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002074 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
Bjorn Helgaase747f272009-03-24 16:49:43 -06002077int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Patrick Mochel5b327262006-05-10 10:33:00 -04002081 result = bus_register(&acpi_bus_type);
2082 if (result) {
2083 /* We don't want to quit even if we failed to add suspend/resume */
2084 printk(KERN_ERR PREFIX "Could not register bus type\n");
2085 }
2086
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002087 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002088 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002089 acpi_processor_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002090 acpi_platform_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002091 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002092 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002093 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002094 acpi_memory_hotplug_init();
Jiang Liu94add0f2013-06-23 00:59:55 +02002095 acpi_dock_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002096
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002097 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 * Enumerate devices in the ACPI namespace.
2100 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002101 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002103 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002105 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002109 result = acpi_bus_scan_fixed();
2110 if (result) {
2111 acpi_device_unregister(acpi_root);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002112 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002113 }
2114
2115 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002116
Yinghai Lu668192b2013-01-21 13:20:48 -08002117 acpi_pci_root_hp_init();
2118
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002119 out:
2120 mutex_unlock(&acpi_scan_lock);
2121 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}