blob: 9cb5cca3cfe3111bbb142bf3a91e217e9fc3efb7 [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
Rafael J. Wysockid7831562013-11-22 21:52:12 +010015#include <asm/pgtable.h>
16
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");
Len Brown4be44fc2005-08-05 00:44:28 -040021extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020024#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define ACPI_BUS_DEVICE_NAME "System Bus"
26
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000027#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
28
Rafael J. Wysockid7831562013-11-22 21:52:12 +010029#define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
30
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020031/*
32 * If set, devices will be hot-removed even if they cannot be put offline
33 * gracefully (from the kernel's standpoint).
34 */
35bool acpi_force_hot_remove;
36
Thomas Renninger620e1122010-10-01 10:54:00 +020037static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020038
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080039static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010040static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010041static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080042DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043LIST_HEAD(acpi_wakeup_device_list);
Rafael J. Wysockie5255062014-02-04 00:43:17 +010044static DEFINE_MUTEX(acpi_hp_context_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080046struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080047 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080048 unsigned int instance_no;
49 struct list_head node;
50};
Thomas Renninger29b71a12007-07-23 14:43:51 +020051
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010052void acpi_scan_lock_acquire(void)
53{
54 mutex_lock(&acpi_scan_lock);
55}
56EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
57
58void acpi_scan_lock_release(void)
59{
60 mutex_unlock(&acpi_scan_lock);
61}
62EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
63
Rafael J. Wysockie5255062014-02-04 00:43:17 +010064void acpi_lock_hp_context(void)
65{
66 mutex_lock(&acpi_hp_context_lock);
67}
68
69void acpi_unlock_hp_context(void)
70{
71 mutex_unlock(&acpi_hp_context_lock);
72}
73
Rafael J. Wysocki5d513202014-02-22 00:48:31 +010074void acpi_initialize_hp_context(struct acpi_device *adev,
75 struct acpi_hotplug_context *hp,
76 int (*notify)(struct acpi_device *, u32),
77 void (*uevent)(struct acpi_device *, u32))
78{
79 acpi_lock_hp_context();
Rafael J. Wysockiba574dc2014-07-15 22:03:22 +020080 hp->notify = notify;
81 hp->uevent = uevent;
82 acpi_set_hp_context(adev, hp);
Rafael J. Wysocki5d513202014-02-22 00:48:31 +010083 acpi_unlock_hp_context();
84}
85EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
86
Rafael J. Wysockica589f92013-01-30 14:27:29 +010087int acpi_scan_add_handler(struct acpi_scan_handler *handler)
88{
Rafael J. Wysockid34afa92014-05-30 04:27:31 +020089 if (!handler)
Rafael J. Wysockica589f92013-01-30 14:27:29 +010090 return -EINVAL;
91
92 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
93 return 0;
94}
95
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010096int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
97 const char *hotplug_profile_name)
98{
99 int error;
100
101 error = acpi_scan_add_handler(handler);
102 if (error)
103 return error;
104
105 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
106 return 0;
107}
108
Thomas Renninger29b71a12007-07-23 14:43:51 +0200109/*
110 * Creates hid/cid(s) string needed for modalias and uevent
111 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
112 * char *modalias: "acpi:IBM0001:ACPI0001"
Zhang Rui3d8e0092014-01-14 16:46:35 +0800113 * Return: 0: no _HID and no _CID
114 * -EINVAL: output error
115 * -ENOMEM: output is truncated
Thomas Renninger29b71a12007-07-23 14:43:51 +0200116*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +0200117static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
118 int size)
119{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200120 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800121 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600122 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200123
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200124 if (list_empty(&acpi_dev->pnp.ids))
125 return 0;
126
Mika Westerberg733e6252014-10-21 13:33:56 +0200127 /*
128 * If the device has PRP0001 we expose DT compatible modalias
129 * instead in form of of:NnameTCcompatible.
130 */
131 if (acpi_dev->data.of_compatible) {
132 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
133 const union acpi_object *of_compatible, *obj;
134 int i, nval;
135 char *c;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200136
Mika Westerberg733e6252014-10-21 13:33:56 +0200137 acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
138 /* DT strings are all in lower case */
139 for (c = buf.pointer; *c != '\0'; c++)
140 *c = tolower(*c);
141
142 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
143 ACPI_FREE(buf.pointer);
144
145 of_compatible = acpi_dev->data.of_compatible;
146 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
147 nval = of_compatible->package.count;
148 obj = of_compatible->package.elements;
149 } else { /* Must be ACPI_TYPE_STRING. */
150 nval = 1;
151 obj = of_compatible;
152 }
153 for (i = 0; i < nval; i++, obj++) {
154 count = snprintf(&modalias[len], size, "C%s",
155 obj->string.pointer);
156 if (count < 0)
157 return -EINVAL;
158 if (count >= size)
159 return -ENOMEM;
160
161 len += count;
162 size -= count;
163 }
164 } else {
165 len = snprintf(modalias, size, "acpi:");
166 size -= len;
167
168 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
169 count = snprintf(&modalias[len], size, "%s:", id->id);
170 if (count < 0)
171 return -EINVAL;
172 if (count >= size)
173 return -ENOMEM;
174 len += count;
175 size -= count;
176 }
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800177 }
178
Thomas Renninger29b71a12007-07-23 14:43:51 +0200179 modalias[len] = '\0';
180 return len;
181}
182
Zhang Rui6eb24512014-01-14 16:46:36 +0800183/*
Mika Westerberg52870782014-10-24 16:40:54 +0200184 * acpi_companion_match() - Can we match via ACPI companion device
185 * @dev: Device in question
186 *
187 * Check if the given device has an ACPI companion and if that companion has
188 * a valid list of PNP IDs, and if the device is the first (primary) physical
189 * device associated with it.
190 *
191 * If multiple physical devices are attached to a single ACPI companion, we need
192 * to be careful. The usage scenario for this kind of relationship is that all
193 * of the physical devices in question use resources provided by the ACPI
194 * companion. A typical case is an MFD device where all the sub-devices share
195 * the parent's ACPI companion. In such cases we can only allow the primary
196 * (first) physical device to be matched with the help of the companion's PNP
197 * IDs.
198 *
199 * Additional physical devices sharing the ACPI companion can still use
200 * resources available from it but they will be matched normally using functions
201 * provided by their bus types (and analogously for their modalias).
202 */
203static bool acpi_companion_match(const struct device *dev)
204{
205 struct acpi_device *adev;
206 bool ret;
207
208 adev = ACPI_COMPANION(dev);
209 if (!adev)
210 return false;
211
212 if (list_empty(&adev->pnp.ids))
213 return false;
214
215 mutex_lock(&adev->physical_node_lock);
216 if (list_empty(&adev->physical_node_list)) {
217 ret = false;
218 } else {
219 const struct acpi_device_physical_node *node;
220
221 node = list_first_entry(&adev->physical_node_list,
222 struct acpi_device_physical_node, node);
223 ret = node->dev == dev;
224 }
225 mutex_unlock(&adev->physical_node_lock);
226
227 return ret;
228}
229
230/*
Zhang Rui6eb24512014-01-14 16:46:36 +0800231 * Creates uevent modalias field for ACPI enumerated devices.
232 * Because the other buses does not support ACPI HIDs & CIDs.
233 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
234 * "acpi:IBM0001:ACPI0001"
235 */
236int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
237{
Zhang Rui6eb24512014-01-14 16:46:36 +0800238 int len;
239
Mika Westerberg52870782014-10-24 16:40:54 +0200240 if (!acpi_companion_match(dev))
Zhang Rui6eb24512014-01-14 16:46:36 +0800241 return -ENODEV;
242
243 if (add_uevent_var(env, "MODALIAS="))
244 return -ENOMEM;
Mika Westerberg52870782014-10-24 16:40:54 +0200245 len = create_modalias(ACPI_COMPANION(dev), &env->buf[env->buflen - 1],
Zhang Rui6eb24512014-01-14 16:46:36 +0800246 sizeof(env->buf) - env->buflen);
247 if (len <= 0)
248 return len;
249 env->buflen += len;
250 return 0;
251}
252EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
253
254/*
255 * Creates modalias sysfs attribute for ACPI enumerated devices.
256 * Because the other buses does not support ACPI HIDs & CIDs.
257 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
258 * "acpi:IBM0001:ACPI0001"
259 */
260int acpi_device_modalias(struct device *dev, char *buf, int size)
261{
Zhang Rui6eb24512014-01-14 16:46:36 +0800262 int len;
263
Mika Westerberg52870782014-10-24 16:40:54 +0200264 if (!acpi_companion_match(dev))
Zhang Rui6eb24512014-01-14 16:46:36 +0800265 return -ENODEV;
266
Mika Westerberg52870782014-10-24 16:40:54 +0200267 len = create_modalias(ACPI_COMPANION(dev), buf, size -1);
Zhang Rui6eb24512014-01-14 16:46:36 +0800268 if (len <= 0)
269 return len;
270 buf[len++] = '\n';
271 return len;
272}
273EXPORT_SYMBOL_GPL(acpi_device_modalias);
274
Thomas Renninger29b71a12007-07-23 14:43:51 +0200275static ssize_t
276acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
277 struct acpi_device *acpi_dev = to_acpi_device(dev);
278 int len;
279
Thomas Renninger29b71a12007-07-23 14:43:51 +0200280 len = create_modalias(acpi_dev, buf, 1024);
281 if (len <= 0)
Zhang Rui3d8e0092014-01-14 16:46:35 +0800282 return len;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200283 buf[len++] = '\n';
284 return len;
285}
286static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
287
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100288bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100289{
290 struct acpi_device_physical_node *pn;
291 bool offline = true;
292
293 mutex_lock(&adev->physical_node_lock);
294
295 list_for_each_entry(pn, &adev->physical_node_list, node)
296 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100297 if (uevent)
298 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
299
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100300 offline = false;
301 break;
302 }
303
304 mutex_unlock(&adev->physical_node_lock);
305 return offline;
306}
307
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100308static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
309 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200310{
311 struct acpi_device *device = NULL;
312 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200313 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200314 acpi_status status = AE_OK;
315
316 if (acpi_bus_get_device(handle, &device))
317 return AE_OK;
318
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100319 if (device->handler && !device->handler->hotplug.enabled) {
320 *ret_p = &device->dev;
321 return AE_SUPPORT;
322 }
323
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200324 mutex_lock(&device->physical_node_lock);
325
326 list_for_each_entry(pn, &device->physical_node_list, node) {
327 int ret;
328
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200329 if (second_pass) {
330 /* Skip devices offlined by the first pass. */
331 if (pn->put_online)
332 continue;
333 } else {
334 pn->put_online = false;
335 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200336 ret = device_offline(pn->dev);
337 if (acpi_force_hot_remove)
338 continue;
339
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200340 if (ret >= 0) {
341 pn->put_online = !ret;
342 } else {
343 *ret_p = pn->dev;
344 if (second_pass) {
345 status = AE_ERROR;
346 break;
347 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200348 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200349 }
350
351 mutex_unlock(&device->physical_node_lock);
352
353 return status;
354}
355
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100356static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
357 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200358{
359 struct acpi_device *device = NULL;
360 struct acpi_device_physical_node *pn;
361
362 if (acpi_bus_get_device(handle, &device))
363 return AE_OK;
364
365 mutex_lock(&device->physical_node_lock);
366
367 list_for_each_entry(pn, &device->physical_node_list, node)
368 if (pn->put_online) {
369 device_online(pn->dev);
370 pn->put_online = false;
371 }
372
373 mutex_unlock(&device->physical_node_lock);
374
375 return AE_OK;
376}
377
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100378static int acpi_scan_try_to_offline(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Yinghai Lu5993c462013-01-11 22:40:41 +0000380 acpi_handle handle = device->handle;
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100381 struct device *errdev = NULL;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100382 acpi_status status;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100383
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200384 /*
385 * Carry out two passes here and ignore errors in the first pass,
386 * because if the devices in question are memory blocks and
387 * CONFIG_MEMCG is set, one of the blocks may hold data structures
388 * that the other blocks depend on, but it is not known in advance which
389 * block holds them.
390 *
391 * If the first pass is successful, the second one isn't needed, though.
392 */
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100393 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
394 NULL, acpi_bus_offline, (void *)false,
395 (void **)&errdev);
396 if (status == AE_SUPPORT) {
397 dev_warn(errdev, "Offline disabled.\n");
398 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
399 acpi_bus_online, NULL, NULL, NULL);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100400 return -EPERM;
401 }
402 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200403 if (errdev) {
404 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200405 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100406 NULL, acpi_bus_offline, (void *)true,
407 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200408 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100409 acpi_bus_offline(handle, 0, (void *)true,
410 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200411
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200412 if (errdev && !acpi_force_hot_remove) {
413 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100414 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200415 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100416 ACPI_UINT32_MAX, acpi_bus_online,
417 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200418 return -EBUSY;
419 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200420 }
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100421 return 0;
422}
423
424static int acpi_scan_hot_remove(struct acpi_device *device)
425{
426 acpi_handle handle = device->handle;
427 unsigned long long sta;
428 acpi_status status;
429
Tang Chendee15922014-08-08 10:30:45 +0800430 if (device->handler && device->handler->hotplug.demand_offline
431 && !acpi_force_hot_remove) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100432 if (!acpi_scan_is_offline(device, true))
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100433 return -EBUSY;
434 } else {
435 int error = acpi_scan_try_to_offline(device);
436 if (error)
437 return error;
438 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200439
Zhang Rui26d46862008-04-29 02:35:48 -0400440 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100441 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400442
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100443 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200444
Jiang Liu7d2421f2013-06-29 00:24:40 +0800445 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /*
447 * TBD: _EJD support.
448 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800449 status = acpi_evaluate_ej0(handle);
450 if (status == AE_NOT_FOUND)
451 return -ENODEV;
452 else if (ACPI_FAILURE(status))
453 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100454
Toshi Kani882fd122013-03-13 19:29:26 +0000455 /*
456 * Verify if eject was indeed successful. If not, log an error
457 * message. No need to call _OST since _EJ0 call was made OK.
458 */
459 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
460 if (ACPI_FAILURE(status)) {
461 acpi_handle_warn(handle,
462 "Status check after eject failed (0x%x)\n", status);
463 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
464 acpi_handle_warn(handle,
465 "Eject incomplete - status 0x%llx\n", sta);
466 }
467
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100468 return 0;
469}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100470
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100471static int acpi_scan_device_not_present(struct acpi_device *adev)
472{
473 if (!acpi_device_enumerated(adev)) {
474 dev_warn(&adev->dev, "Still not present\n");
475 return -EALREADY;
476 }
477 acpi_bus_trim(adev);
478 return 0;
479}
480
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100481static int acpi_scan_device_check(struct acpi_device *adev)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100482{
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200483 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100484
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100485 acpi_bus_get_status(adev);
486 if (adev->status.present || adev->status.functional) {
487 /*
488 * This function is only called for device objects for which
489 * matching scan handlers exist. The only situation in which
490 * the scan handler is not attached to this device object yet
491 * is when the device has just appeared (either it wasn't
492 * present at all before or it was removed and then added
493 * again).
494 */
495 if (adev->handler) {
496 dev_warn(&adev->dev, "Already enumerated\n");
497 return -EALREADY;
498 }
499 error = acpi_bus_scan(adev->handle);
500 if (error) {
501 dev_warn(&adev->dev, "Namespace scan failure\n");
502 return error;
503 }
504 if (!adev->handler) {
505 dev_warn(&adev->dev, "Enumeration failure\n");
506 error = -ENODEV;
507 }
508 } else {
509 error = acpi_scan_device_not_present(adev);
510 }
511 return error;
512}
513
514static int acpi_scan_bus_check(struct acpi_device *adev)
515{
516 struct acpi_scan_handler *handler = adev->handler;
517 struct acpi_device *child;
518 int error;
519
520 acpi_bus_get_status(adev);
521 if (!(adev->status.present || adev->status.functional)) {
522 acpi_scan_device_not_present(adev);
523 return 0;
524 }
525 if (handler && handler->hotplug.scan_dependent)
526 return handler->hotplug.scan_dependent(adev);
527
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100528 error = acpi_bus_scan(adev->handle);
529 if (error) {
530 dev_warn(&adev->dev, "Namespace scan failure\n");
531 return error;
532 }
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100533 list_for_each_entry(child, &adev->children, node) {
534 error = acpi_scan_bus_check(child);
535 if (error)
536 return error;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100537 }
538 return 0;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100539}
540
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100541static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
542{
543 switch (type) {
544 case ACPI_NOTIFY_BUS_CHECK:
545 return acpi_scan_bus_check(adev);
546 case ACPI_NOTIFY_DEVICE_CHECK:
547 return acpi_scan_device_check(adev);
548 case ACPI_NOTIFY_EJECT_REQUEST:
549 case ACPI_OST_EC_OSPM_EJECT:
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100550 if (adev->handler && !adev->handler->hotplug.enabled) {
551 dev_info(&adev->dev, "Eject disabled\n");
552 return -EPERM;
553 }
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100554 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
555 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100556 return acpi_scan_hot_remove(adev);
557 }
558 return -EINVAL;
559}
560
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +0100561void acpi_device_hotplug(struct acpi_device *adev, u32 src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100562{
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100563 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100564 int error = -ENODEV;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100565
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200566 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200567 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100568
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100569 /*
570 * The device object's ACPI handle cannot become invalid as long as we
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100571 * are holding acpi_scan_lock, but it might have become invalid before
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100572 * that lock was acquired.
573 */
574 if (adev->handle == INVALID_ACPI_HANDLE)
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100575 goto err_out;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100576
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100577 if (adev->flags.is_dock_station) {
578 error = dock_notify(adev, src);
579 } else if (adev->flags.hotplug_notify) {
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100580 error = acpi_generic_hotplug_event(adev, src);
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100581 if (error == -EPERM) {
582 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
583 goto err_out;
584 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100585 } else {
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100586 int (*notify)(struct acpi_device *, u32);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100587
588 acpi_lock_hp_context();
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100589 notify = adev->hp ? adev->hp->notify : NULL;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100590 acpi_unlock_hp_context();
591 /*
592 * There may be additional notify handlers for device objects
593 * without the .event() callback, so ignore them here.
594 */
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100595 if (notify)
596 error = notify(adev, src);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100597 else
598 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100599 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100600 if (!error)
601 ost_code = ACPI_OST_SC_SUCCESS;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100602
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100603 err_out:
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100604 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100605
606 out:
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100607 acpi_bus_put_acpi_device(adev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100608 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200609 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100610}
611
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100612static ssize_t real_power_state_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
614{
615 struct acpi_device *adev = to_acpi_device(dev);
616 int state;
617 int ret;
618
619 ret = acpi_device_get_power(adev, &state);
620 if (ret)
621 return ret;
622
623 return sprintf(buf, "%s\n", acpi_power_state_string(state));
624}
625
626static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
627
628static ssize_t power_state_show(struct device *dev,
629 struct device_attribute *attr, char *buf)
630{
631 struct acpi_device *adev = to_acpi_device(dev);
632
633 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
634}
635
636static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800639acpi_eject_store(struct device *d, struct device_attribute *attr,
640 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800642 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100643 acpi_object_type not_used;
644 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100646 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100649 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
650 && !acpi_device->driver)
651 return -ENODEV;
652
653 status = acpi_get_type(acpi_device->handle, &not_used);
654 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
655 return -ENODEV;
656
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100657 get_device(&acpi_device->dev);
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +0100658 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200659 if (ACPI_SUCCESS(status))
660 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100661
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200662 put_device(&acpi_device->dev);
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100663 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
664 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100665 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800668static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800670static ssize_t
671acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
672 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600674 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800675}
676static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
677
Lv Zheng923d4a42012-10-30 14:43:26 +0100678static ssize_t acpi_device_uid_show(struct device *dev,
679 struct device_attribute *attr, char *buf)
680{
681 struct acpi_device *acpi_dev = to_acpi_device(dev);
682
683 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
684}
685static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
686
687static ssize_t acpi_device_adr_show(struct device *dev,
688 struct device_attribute *attr, char *buf)
689{
690 struct acpi_device *acpi_dev = to_acpi_device(dev);
691
692 return sprintf(buf, "0x%08x\n",
693 (unsigned int)(acpi_dev->pnp.bus_address));
694}
695static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
696
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800697static ssize_t
698acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
699 struct acpi_device *acpi_dev = to_acpi_device(dev);
700 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
701 int result;
702
703 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600704 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800705 goto end;
706
707 result = sprintf(buf, "%s\n", (char*)path.pointer);
708 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600709end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800710 return result;
711}
712static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
713
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600714/* sysfs file that shows description text from the ACPI _STR method */
715static ssize_t description_show(struct device *dev,
716 struct device_attribute *attr,
717 char *buf) {
718 struct acpi_device *acpi_dev = to_acpi_device(dev);
719 int result;
720
721 if (acpi_dev->pnp.str_obj == NULL)
722 return 0;
723
724 /*
725 * The _STR object contains a Unicode identifier for a device.
726 * We need to convert to utf-8 so it can be displayed.
727 */
728 result = utf16s_to_utf8s(
729 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
730 acpi_dev->pnp.str_obj->buffer.length,
731 UTF16_LITTLE_ENDIAN, buf,
732 PAGE_SIZE);
733
734 buf[result++] = '\n';
735
736 return result;
737}
738static DEVICE_ATTR(description, 0444, description_show, NULL);
739
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100740static ssize_t
741acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
742 char *buf) {
743 struct acpi_device *acpi_dev = to_acpi_device(dev);
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900744 acpi_status status;
745 unsigned long long sun;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100746
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900747 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
748 if (ACPI_FAILURE(status))
749 return -ENODEV;
750
751 return sprintf(buf, "%llu\n", sun);
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100752}
753static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
754
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800755static ssize_t status_show(struct device *dev, struct device_attribute *attr,
756 char *buf) {
757 struct acpi_device *acpi_dev = to_acpi_device(dev);
758 acpi_status status;
759 unsigned long long sta;
760
761 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
762 if (ACPI_FAILURE(status))
763 return -ENODEV;
764
765 return sprintf(buf, "%llu\n", sta);
766}
767static DEVICE_ATTR_RO(status);
768
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800769static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600771 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800772 acpi_status status;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800773 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800775 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800776 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800777 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600778 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800779 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600780 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800781 goto end;
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200784 if (!list_empty(&dev->pnp.ids)) {
785 result = device_create_file(&dev->dev, &dev_attr_hid);
786 if (result)
787 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200789 result = device_create_file(&dev->dev, &dev_attr_modalias);
790 if (result)
791 goto end;
792 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200793
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600794 /*
795 * If device has _STR, 'description' file is created
796 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800797 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600798 status = acpi_evaluate_object(dev->handle, "_STR",
799 NULL, &buffer);
800 if (ACPI_FAILURE(status))
801 buffer.pointer = NULL;
802 dev->pnp.str_obj = buffer.pointer;
803 result = device_create_file(&dev->dev, &dev_attr_description);
804 if (result)
805 goto end;
806 }
807
Toshi Kanid4e1a692013-03-04 21:30:41 +0000808 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100809 result = device_create_file(&dev->dev, &dev_attr_adr);
810 if (dev->pnp.unique_id)
811 result = device_create_file(&dev->dev, &dev_attr_uid);
812
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900813 if (acpi_has_method(dev->handle, "_SUN")) {
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100814 result = device_create_file(&dev->dev, &dev_attr_sun);
815 if (result)
816 goto end;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100817 }
818
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800819 if (acpi_has_method(dev->handle, "_STA")) {
820 result = device_create_file(&dev->dev, &dev_attr_status);
821 if (result)
822 goto end;
823 }
824
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800825 /*
826 * If device has _EJ0, 'eject' file is created that is used to trigger
827 * hot-removal function from userland.
828 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800829 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800830 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100831 if (result)
832 return result;
833 }
834
835 if (dev->flags.power_manageable) {
836 result = device_create_file(&dev->dev, &dev_attr_power_state);
837 if (result)
838 return result;
839
840 if (dev->power.flags.power_resources)
841 result = device_create_file(&dev->dev,
842 &dev_attr_real_power_state);
843 }
844
Alex Chiang0c526d92009-05-14 08:31:37 -0600845end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800846 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800847}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800849static void acpi_device_remove_files(struct acpi_device *dev)
850{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100851 if (dev->flags.power_manageable) {
852 device_remove_file(&dev->dev, &dev_attr_power_state);
853 if (dev->power.flags.power_resources)
854 device_remove_file(&dev->dev,
855 &dev_attr_real_power_state);
856 }
857
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800858 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600859 * If device has _STR, remove 'description' file
860 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800861 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600862 kfree(dev->pnp.str_obj);
863 device_remove_file(&dev->dev, &dev_attr_description);
864 }
865 /*
866 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800867 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800868 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800869 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800870
Jiang Liu952c63e2013-06-29 00:24:38 +0800871 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100872 device_remove_file(&dev->dev, &dev_attr_sun);
873
Lv Zheng923d4a42012-10-30 14:43:26 +0100874 if (dev->pnp.unique_id)
875 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000876 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100877 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600878 device_remove_file(&dev->dev, &dev_attr_modalias);
879 device_remove_file(&dev->dev, &dev_attr_hid);
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800880 if (acpi_has_method(dev->handle, "_STA"))
881 device_remove_file(&dev->dev, &dev_attr_status);
Alex Chiang0c526d92009-05-14 08:31:37 -0600882 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800883 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800884}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800886 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200888
Mika Westerbergcf761af2012-10-31 22:44:41 +0100889static const struct acpi_device_id *__acpi_match_device(
890 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200891{
892 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600893 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200894
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800895 /*
896 * If the device is not present, it is unnecessary to load device
897 * driver for it.
898 */
899 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100900 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800901
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600902 for (id = ids; id->id[0]; id++)
903 list_for_each_entry(hwid, &device->pnp.ids, list)
904 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100905 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200906
Mika Westerbergcf761af2012-10-31 22:44:41 +0100907 return NULL;
908}
909
910/**
911 * acpi_match_device - Match a struct device against a given list of ACPI IDs
912 * @ids: Array of struct acpi_device_id object to match against.
913 * @dev: The device structure to match.
914 *
915 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
916 * object for that handle and use that object to match against a given list of
917 * device IDs.
918 *
919 * Return a pointer to the first matching ID on success or %NULL on failure.
920 */
921const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
922 const struct device *dev)
923{
924 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100925 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100926
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100927 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100928 return NULL;
929
Mika Westerberg52870782014-10-24 16:40:54 +0200930 if (!acpi_companion_match(dev))
931 return NULL;
932
Mika Westerbergcf761af2012-10-31 22:44:41 +0100933 return __acpi_match_device(adev, ids);
934}
935EXPORT_SYMBOL_GPL(acpi_match_device);
936
937int acpi_match_device_ids(struct acpi_device *device,
938 const struct acpi_device_id *ids)
939{
940 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200941}
942EXPORT_SYMBOL(acpi_match_device_ids);
943
Mika Westerberg733e6252014-10-21 13:33:56 +0200944/* Performs match against special "PRP0001" shoehorn ACPI ID */
945static bool acpi_of_driver_match_device(struct device *dev,
946 const struct device_driver *drv)
947{
948 const union acpi_object *of_compatible, *obj;
949 struct acpi_device *adev;
950 int i, nval;
951
952 adev = ACPI_COMPANION(dev);
953 if (!adev)
954 return false;
955
956 of_compatible = adev->data.of_compatible;
957 if (!drv->of_match_table || !of_compatible)
958 return false;
959
960 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
961 nval = of_compatible->package.count;
962 obj = of_compatible->package.elements;
963 } else { /* Must be ACPI_TYPE_STRING. */
964 nval = 1;
965 obj = of_compatible;
966 }
967 /* Now we can look for the driver DT compatible strings */
968 for (i = 0; i < nval; i++, obj++) {
969 const struct of_device_id *id;
970
971 for (id = drv->of_match_table; id->compatible[0]; id++)
972 if (!strcasecmp(obj->string.pointer, id->compatible))
973 return true;
974 }
975
976 return false;
977}
978
979bool acpi_driver_match_device(struct device *dev,
980 const struct device_driver *drv)
981{
982 if (!drv->acpi_match_table)
983 return acpi_of_driver_match_device(dev, drv);
984
985 return !!acpi_match_device(drv->acpi_match_table, dev);
986}
987EXPORT_SYMBOL_GPL(acpi_driver_match_device);
988
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100989static void acpi_free_power_resources_lists(struct acpi_device *device)
990{
991 int i;
992
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100993 if (device->wakeup.flags.valid)
994 acpi_power_resources_list_free(&device->wakeup.resources);
995
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100996 if (!device->flags.power_manageable)
997 return;
998
999 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
1000 struct acpi_device_power_state *ps = &device->power.states[i];
1001 acpi_power_resources_list_free(&ps->resources);
1002 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001003}
1004
Patrick Mochel1890a972006-12-07 20:56:31 +08001005static void acpi_device_release(struct device *dev)
1006{
1007 struct acpi_device *acpi_dev = to_acpi_device(dev);
1008
Mika Westerbergffdcd952014-10-21 13:33:55 +02001009 acpi_free_properties(acpi_dev);
Toshi Kanic0af4172013-03-04 21:30:42 +00001010 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001011 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +08001012 kfree(acpi_dev);
1013}
1014
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001015static int acpi_bus_match(struct device *dev, struct device_driver *drv)
1016{
1017 struct acpi_device *acpi_dev = to_acpi_device(dev);
1018 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1019
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +01001020 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001021 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001022}
1023
Kay Sievers7eff2e72007-08-14 15:15:12 +02001024static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001025{
1026 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +02001027 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001028
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001029 if (list_empty(&acpi_dev->pnp.ids))
1030 return 0;
1031
Kay Sievers7eff2e72007-08-14 15:15:12 +02001032 if (add_uevent_var(env, "MODALIAS="))
1033 return -ENOMEM;
1034 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
1035 sizeof(env->buf) - env->buflen);
Zhang Rui3d8e0092014-01-14 16:46:35 +08001036 if (len <= 0)
1037 return len;
Kay Sievers7eff2e72007-08-14 15:15:12 +02001038 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return 0;
1040}
1041
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001042static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
1043{
1044 struct acpi_device *device = data;
1045
1046 device->driver->ops.notify(device, event);
1047}
1048
Lan Tianyu236105d2014-08-26 01:29:24 +02001049static void acpi_device_notify_fixed(void *data)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001050{
1051 struct acpi_device *device = data;
1052
Bjorn Helgaas53de5352009-08-31 22:32:20 +00001053 /* Fixed hardware devices have no handles */
1054 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Lan Tianyu236105d2014-08-26 01:29:24 +02001055}
1056
1057static acpi_status acpi_device_fixed_event(void *data)
1058{
1059 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001060 return AE_OK;
1061}
1062
1063static int acpi_device_install_notify_handler(struct acpi_device *device)
1064{
1065 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001066
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001067 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001068 status =
1069 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001070 acpi_device_fixed_event,
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001071 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001072 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001073 status =
1074 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001075 acpi_device_fixed_event,
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001076 device);
1077 else
1078 status = acpi_install_notify_handler(device->handle,
1079 ACPI_DEVICE_NOTIFY,
1080 acpi_device_notify,
1081 device);
1082
1083 if (ACPI_FAILURE(status))
1084 return -EINVAL;
1085 return 0;
1086}
1087
1088static void acpi_device_remove_notify_handler(struct acpi_device *device)
1089{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001090 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001091 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001092 acpi_device_fixed_event);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001093 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001094 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001095 acpi_device_fixed_event);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001096 else
1097 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1098 acpi_device_notify);
1099}
1100
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001101static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +08001102{
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001103 struct acpi_device *acpi_dev = to_acpi_device(dev);
1104 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1105 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Rafael J. Wysockifc2e0a82014-08-26 01:29:21 +02001107 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
Rafael J. Wysocki24071f42013-06-16 00:36:41 +02001108 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001109
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001110 if (!acpi_drv->ops.add)
1111 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001112
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001113 ret = acpi_drv->ops.add(acpi_dev);
1114 if (ret)
1115 return ret;
1116
1117 acpi_dev->driver = acpi_drv;
1118 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1119 "Driver [%s] successfully bound to device [%s]\n",
1120 acpi_drv->name, acpi_dev->pnp.bus_id));
1121
1122 if (acpi_drv->ops.notify) {
1123 ret = acpi_device_install_notify_handler(acpi_dev);
1124 if (ret) {
1125 if (acpi_drv->ops.remove)
1126 acpi_drv->ops.remove(acpi_dev);
1127
1128 acpi_dev->driver = NULL;
1129 acpi_dev->driver_data = NULL;
1130 return ret;
1131 }
Zhang Rui9e89dde2006-12-07 20:56:16 +08001132 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001133
1134 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1135 acpi_drv->name, acpi_dev->pnp.bus_id));
1136 get_device(dev);
1137 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001138}
1139
1140static int acpi_device_remove(struct device * dev)
1141{
1142 struct acpi_device *acpi_dev = to_acpi_device(dev);
1143 struct acpi_driver *acpi_drv = acpi_dev->driver;
1144
1145 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001146 if (acpi_drv->ops.notify)
1147 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001148 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001149 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001150 }
1151 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001152 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001153
1154 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +08001155 return 0;
1156}
1157
David Brownell55955aa2007-05-08 00:28:35 -07001158struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +08001159 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001160 .match = acpi_bus_match,
1161 .probe = acpi_device_probe,
1162 .remove = acpi_device_remove,
1163 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164};
1165
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001166static void acpi_device_del(struct acpi_device *device)
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001167{
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001168 mutex_lock(&acpi_device_lock);
1169 if (device->parent)
1170 list_del(&device->node);
1171
1172 list_del(&device->wakeup_list);
1173 mutex_unlock(&acpi_device_lock);
1174
1175 acpi_power_add_remove_device(device, false);
1176 acpi_device_remove_files(device);
1177 if (device->remove)
1178 device->remove(device);
1179
1180 device_del(&device->dev);
1181}
1182
1183static LIST_HEAD(acpi_device_del_list);
1184static DEFINE_MUTEX(acpi_device_del_lock);
1185
1186static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1187{
1188 for (;;) {
1189 struct acpi_device *adev;
1190
1191 mutex_lock(&acpi_device_del_lock);
1192
1193 if (list_empty(&acpi_device_del_list)) {
1194 mutex_unlock(&acpi_device_del_lock);
1195 break;
1196 }
1197 adev = list_first_entry(&acpi_device_del_list,
1198 struct acpi_device, del_list);
1199 list_del(&adev->del_list);
1200
1201 mutex_unlock(&acpi_device_del_lock);
1202
1203 acpi_device_del(adev);
1204 /*
1205 * Drop references to all power resources that might have been
1206 * used by the device.
1207 */
1208 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1209 put_device(&adev->dev);
1210 }
1211}
1212
1213/**
1214 * acpi_scan_drop_device - Drop an ACPI device object.
1215 * @handle: Handle of an ACPI namespace node, not used.
1216 * @context: Address of the ACPI device object to drop.
1217 *
1218 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1219 * namespace node the device object pointed to by @context is attached to.
1220 *
1221 * The unregistration is carried out asynchronously to avoid running
1222 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1223 * ensure the correct ordering (the device objects must be unregistered in the
1224 * same order in which the corresponding namespace nodes are deleted).
1225 */
1226static void acpi_scan_drop_device(acpi_handle handle, void *context)
1227{
1228 static DECLARE_WORK(work, acpi_device_del_work_fn);
1229 struct acpi_device *adev = context;
1230
1231 mutex_lock(&acpi_device_del_lock);
1232
1233 /*
1234 * Use the ACPI hotplug workqueue which is ordered, so this work item
1235 * won't run after any hotplug work items submitted subsequently. That
1236 * prevents attempts to register device objects identical to those being
1237 * deleted from happening concurrently (such attempts result from
1238 * hotplug events handled via the ACPI hotplug workqueue). It also will
1239 * run after all of the work items submitted previosuly, which helps
1240 * those work items to ensure that they are not accessing stale device
1241 * objects.
1242 */
1243 if (list_empty(&acpi_device_del_list))
1244 acpi_queue_hotplug_work(&work);
1245
1246 list_add_tail(&adev->del_list, &acpi_device_del_list);
1247 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1248 adev->handle = INVALID_ACPI_HANDLE;
1249
1250 mutex_unlock(&acpi_device_del_lock);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001251}
1252
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001253static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
1254 void (*callback)(void *))
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001255{
1256 acpi_status status;
1257
1258 if (!device)
1259 return -EINVAL;
1260
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001261 status = acpi_get_data_full(handle, acpi_scan_drop_device,
1262 (void **)device, callback);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001263 if (ACPI_FAILURE(status) || !*device) {
1264 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1265 handle));
1266 return -ENODEV;
1267 }
1268 return 0;
1269}
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001270
1271int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1272{
1273 return acpi_get_device_data(handle, device, NULL);
1274}
Rafael J. Wysocki65859252013-10-01 23:02:43 +02001275EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001276
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001277static void get_acpi_device(void *dev)
1278{
1279 if (dev)
1280 get_device(&((struct acpi_device *)dev)->dev);
1281}
1282
1283struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
1284{
1285 struct acpi_device *adev = NULL;
1286
1287 acpi_get_device_data(handle, &adev, get_acpi_device);
1288 return adev;
1289}
1290
1291void acpi_bus_put_acpi_device(struct acpi_device *adev)
1292{
1293 put_device(&adev->dev);
1294}
1295
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001296int acpi_device_add(struct acpi_device *device,
1297 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001299 int result;
1300 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1301 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001302
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001303 if (device->handle) {
1304 acpi_status status;
1305
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001306 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001307 device);
1308 if (ACPI_FAILURE(status)) {
1309 acpi_handle_err(device->handle,
1310 "Unable to attach device data\n");
1311 return -ENODEV;
1312 }
1313 }
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /*
1316 * Linkage
1317 * -------
1318 * Link this device to its parent and siblings.
1319 */
1320 INIT_LIST_HEAD(&device->children);
1321 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001323 INIT_LIST_HEAD(&device->physical_node_list);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001324 INIT_LIST_HEAD(&device->del_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001325 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001327 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1328 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001329 pr_err(PREFIX "Memory allocation error\n");
1330 result = -ENOMEM;
1331 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001332 }
1333
Shaohua Li90905892009-04-07 10:24:29 +08001334 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001335 /*
1336 * Find suitable bus_id and instance number in acpi_bus_id_list
1337 * If failed, create one and link it into acpi_bus_id_list
1338 */
1339 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001340 if (!strcmp(acpi_device_bus_id->bus_id,
1341 acpi_device_hid(device))) {
1342 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001343 found = 1;
1344 kfree(new_bus_id);
1345 break;
1346 }
1347 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001348 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001349 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001350 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001351 acpi_device_bus_id->instance_no = 0;
1352 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1353 }
Kay Sievers07944692008-10-30 01:18:59 +01001354 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 +08001355
Len Brown33b57152008-12-15 22:09:26 -05001356 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 if (device->wakeup.flags.valid)
1360 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001361 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Patrick Mochel1890a972006-12-07 20:56:31 +08001363 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001364 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001365 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001366 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001367 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001368 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001369 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001370 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001371 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001372
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001373 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001374 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001375 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1376 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001377
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001378 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001379
1380 err:
Shaohua Li90905892009-04-07 10:24:29 +08001381 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001382 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001383 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001384 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001385 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001386
1387 err_detach:
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001388 acpi_detach_data(device->handle, acpi_scan_drop_device);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001389 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001392struct acpi_device *acpi_get_next_child(struct device *dev,
1393 struct acpi_device *child)
1394{
1395 struct acpi_device *adev = ACPI_COMPANION(dev);
1396 struct list_head *head, *next;
1397
1398 if (!adev)
1399 return NULL;
1400
1401 head = &adev->children;
1402 if (list_empty(head))
1403 return NULL;
1404
1405 if (!child)
1406 return list_first_entry(head, struct acpi_device, node);
1407
1408 next = child->node.next;
1409 return next == head ? NULL : list_entry(next, struct acpi_device, node);
1410}
1411
Zhang Rui9e89dde2006-12-07 20:56:16 +08001412/* --------------------------------------------------------------------------
1413 Driver Management
1414 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001416 * acpi_bus_register_driver - register a driver with the ACPI bus
1417 * @driver: driver being registered
1418 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001420 * devices that match the driver's criteria and binds. Returns zero for
1421 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
Len Brown4be44fc2005-08-05 00:44:28 -04001423int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
Patrick Mochel1890a972006-12-07 20:56:31 +08001425 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001428 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001429 driver->drv.name = driver->name;
1430 driver->drv.bus = &acpi_bus_type;
1431 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Patrick Mochel1890a972006-12-07 20:56:31 +08001433 ret = driver_register(&driver->drv);
1434 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Len Brown4be44fc2005-08-05 00:44:28 -04001437EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001440 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001441 * @driver: driver to unregister
1442 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1444 * devices that match the driver's criteria and unbinds.
1445 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001446void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Patrick Mochel1890a972006-12-07 20:56:31 +08001448 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
Len Brown4be44fc2005-08-05 00:44:28 -04001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451EXPORT_SYMBOL(acpi_bus_unregister_driver);
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453/* --------------------------------------------------------------------------
1454 Device Enumeration
1455 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001456static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1457{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001458 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001459 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001460
1461 /*
1462 * Fixed hardware devices do not appear in the namespace and do not
1463 * have handles, but we fabricate acpi_devices for them, so we have
1464 * to deal with them specially.
1465 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001466 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001467 return acpi_root;
1468
1469 do {
1470 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001471 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001472 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1473 } while (acpi_bus_get_device(handle, &device));
1474 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001475}
1476
Len Brownc8f7a622006-07-09 17:22:28 -04001477acpi_status
1478acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1479{
1480 acpi_status status;
1481 acpi_handle tmp;
1482 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1483 union acpi_object *obj;
1484
1485 status = acpi_get_handle(handle, "_EJD", &tmp);
1486 if (ACPI_FAILURE(status))
1487 return status;
1488
1489 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1490 if (ACPI_SUCCESS(status)) {
1491 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001492 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1493 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001494 kfree(buffer.pointer);
1495 }
1496 return status;
1497}
1498EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1499
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001500static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1501 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001503 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1504 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001506 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001507 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001509 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001510 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001512 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001514 /* _PRW */
1515 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1516 if (ACPI_FAILURE(status)) {
1517 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001518 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001519 }
1520
1521 package = (union acpi_object *)buffer.pointer;
1522
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001523 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001524 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001527 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001528 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (element->type == ACPI_TYPE_PACKAGE) {
1531 if ((element->package.count < 2) ||
1532 (element->package.elements[0].type !=
1533 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001534 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001535 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001536
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001537 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001539 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 (u32) element->package.elements[1].integer.value;
1541 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001542 wakeup->gpe_device = NULL;
1543 wakeup->gpe_number = element->integer.value;
1544 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001545 goto out;
1546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001549 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001550 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001551
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001552 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001554 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1555 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001558 if (!list_empty(&wakeup->resources)) {
1559 int sleep_state;
1560
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001561 err = acpi_power_wakeup_list_init(&wakeup->resources,
1562 &sleep_state);
1563 if (err) {
1564 acpi_handle_warn(handle, "Retrieving current states "
1565 "of wakeup power resources failed\n");
1566 acpi_power_resources_list_free(&wakeup->resources);
1567 goto out;
1568 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001569 if (sleep_state < wakeup->sleep_state) {
1570 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1571 "(S%d) by S%d from power resources\n",
1572 (int)wakeup->sleep_state, sleep_state);
1573 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001576
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001577 out:
1578 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001579 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001582static void acpi_wakeup_gpe_init(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001584 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001585 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001586 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001587 {"PNP0C0E", 0},
1588 {"", 0},
1589 };
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001590 struct acpi_device_wakeup *wakeup = &device->wakeup;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001591 acpi_status status;
1592 acpi_event_status event_status;
1593
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001594 wakeup->flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001595
1596 /* Power button, Lid switch always enable wakeup */
1597 if (!acpi_match_device_ids(device, button_device_ids)) {
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001598 wakeup->flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001599 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1600 /* Do not use Lid/sleep button for S5 wakeup */
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001601 if (wakeup->sleep_state == ACPI_STATE_S5)
1602 wakeup->sleep_state = ACPI_STATE_S4;
Zhang Ruib7e38302012-12-04 23:23:16 +01001603 }
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001604 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001605 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001606 return;
1607 }
1608
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001609 acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
1610 wakeup->gpe_number);
1611 status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
1612 &event_status);
1613 if (ACPI_FAILURE(status))
1614 return;
1615
Lv Zheng2f857232014-10-10 10:40:05 +08001616 wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001617}
1618
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001619static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001620{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001621 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001622
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001623 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001624 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001625 return;
1626
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001627 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1628 &device->wakeup);
1629 if (err) {
1630 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001631 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001635 device->wakeup.prepare_count = 0;
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001636 acpi_wakeup_gpe_init(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001637 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1638 * system for the ACPI device with the _PRW object.
1639 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1640 * So it is necessary to call _DSW object first. Only when it is not
1641 * present will the _PSW object used.
1642 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001643 err = acpi_device_sleep_wake(device, 0, 0, 0);
1644 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001645 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1646 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001649static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001651 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001652 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1653 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001654 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001655
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001656 INIT_LIST_HEAD(&ps->resources);
1657
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001658 /* Evaluate "_PRx" to get referenced power resources */
1659 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1660 if (ACPI_SUCCESS(status)) {
1661 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001662
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001663 if (buffer.length && package
1664 && package->type == ACPI_TYPE_PACKAGE
1665 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001666 int err = acpi_extract_power_resources(package, 0,
1667 &ps->resources);
1668 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001669 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001670 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001671 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001672 }
1673
1674 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001675 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001676 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001677 ps->flags.explicit_set = 1;
1678
1679 /*
1680 * State is valid if there are means to put the device into it.
1681 * D3hot is only valid if _PR3 present.
1682 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001683 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001684 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1685 ps->flags.valid = 1;
1686 ps->flags.os_accessible = 1;
1687 }
1688
1689 ps->power = -1; /* Unknown - driver assigned */
1690 ps->latency = -1; /* Unknown - driver assigned */
1691}
1692
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001693static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001695 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001697 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001698 if (!acpi_has_method(device->handle, "_PS0") &&
1699 !acpi_has_method(device->handle, "_PR0"))
1700 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001701
1702 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001705 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001707 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001708 device->power.flags.explicit_get = 1;
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +02001709
Jiang Liu952c63e2013-06-29 00:24:38 +08001710 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001711 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +02001713 if (acpi_has_method(device->handle, "_DSW"))
1714 device->power.flags.dsw_present = 1;
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001717 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001719 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1720 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001722 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Zhang Rui9e89dde2006-12-07 20:56:16 +08001724 /* Set defaults for D0 and D3 states (always valid) */
1725 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1726 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001727 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1728 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001730 /* Set D3cold's explicit_set flag if _PS3 exists. */
1731 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1732 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1733
Aaron Lu1399dfc2012-11-21 23:33:40 +01001734 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1735 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1736 device->power.flags.power_resources)
1737 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1738
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001739 if (acpi_bus_init_power(device)) {
1740 acpi_free_power_resources_lists(device);
1741 device->flags.power_manageable = 0;
1742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001745static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001748 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 device->flags.dynamic_status = 1;
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001752 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 device->flags.removable = 1;
1754
1755 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001756 if (acpi_has_method(device->handle, "_EJD") ||
1757 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001761static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Len Brown4be44fc2005-08-05 00:44:28 -04001763 char bus_id[5] = { '?', 0 };
1764 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1765 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 /*
1768 * Bus ID
1769 * ------
1770 * The device's Bus ID is simply the object name.
1771 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1772 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001773 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001775 return;
1776 }
1777
1778 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 case ACPI_BUS_TYPE_POWER_BUTTON:
1780 strcpy(device->pnp.bus_id, "PWRF");
1781 break;
1782 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1783 strcpy(device->pnp.bus_id, "SLPF");
1784 break;
1785 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001786 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /* Clean up trailing underscores (if any) */
1788 for (i = 3; i > 1; i--) {
1789 if (bus_id[i] == '_')
1790 bus_id[i] = '\0';
1791 else
1792 break;
1793 }
1794 strcpy(device->pnp.bus_id, bus_id);
1795 break;
1796 }
1797}
1798
Zhang Rui54735262007-01-11 02:09:09 -05001799/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001800 * acpi_ata_match - see if an acpi object is an ATA device
1801 *
1802 * If an acpi object has one of the ACPI ATA methods defined,
1803 * then we can safely call it an ATA device.
1804 */
1805bool acpi_ata_match(acpi_handle handle)
1806{
1807 return acpi_has_method(handle, "_GTF") ||
1808 acpi_has_method(handle, "_GTM") ||
1809 acpi_has_method(handle, "_STM") ||
1810 acpi_has_method(handle, "_SDD");
1811}
1812
1813/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001814 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001815 *
1816 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1817 * then we can safely call it an ejectable drive bay
1818 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001819bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001820{
Zhang Rui54735262007-01-11 02:09:09 -05001821 acpi_handle phandle;
1822
Jiang Liu952c63e2013-06-29 00:24:38 +08001823 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001824 return false;
1825 if (acpi_ata_match(handle))
1826 return true;
1827 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1828 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001829
Jiang Liuebf4df82013-06-29 00:24:41 +08001830 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001831}
1832
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001833bool acpi_device_is_battery(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001834{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001835 struct acpi_hardware_id *hwid;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001836
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001837 list_for_each_entry(hwid, &adev->pnp.ids, list)
1838 if (!strcmp("PNP0C0A", hwid->id))
1839 return true;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001840
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001841 return false;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001842}
1843
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001844static bool is_ejectable_bay(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001845{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001846 acpi_handle handle = adev->handle;
1847
1848 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001849 return true;
1850
1851 return acpi_bay_match(handle);
1852}
1853
Frank Seidel3620f2f2007-12-07 13:20:34 +01001854/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001855 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001856 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001857bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001858{
Jiang Liuebf4df82013-06-29 00:24:41 +08001859 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001860}
1861
Thomas Renninger620e1122010-10-01 10:54:00 +02001862const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001863{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001864 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001865
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001866 if (list_empty(&device->pnp.ids))
1867 return dummy_hid;
1868
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001869 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1870 return hid->id;
1871}
1872EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001873
Toshi Kanid4e1a692013-03-04 21:30:41 +00001874static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001875{
1876 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001877
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001878 id = kmalloc(sizeof(*id), GFP_KERNEL);
1879 if (!id)
1880 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001881
Thomas Meyer581de592011-08-06 11:32:56 +02001882 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001883 if (!id->id) {
1884 kfree(id);
1885 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001886 }
1887
Toshi Kanid4e1a692013-03-04 21:30:41 +00001888 list_add_tail(&id->list, &pnp->ids);
1889 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001890}
1891
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001892/*
1893 * Old IBM workstations have a DSDT bug wherein the SMBus object
1894 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1895 * prefix. Work around this.
1896 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001897static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001898{
Jiang Liuebf4df82013-06-29 00:24:41 +08001899 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1900 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001901
1902 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001903 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001904
1905 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001906 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1907 strcmp("SMBS", path.pointer))
1908 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001909
1910 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001911 if (acpi_has_method(handle, "SBI") &&
1912 acpi_has_method(handle, "SBR") &&
1913 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001914 return true;
1915
1916 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001917}
1918
Jiang Liuae3caa82014-02-19 14:02:19 +08001919static bool acpi_object_is_system_bus(acpi_handle handle)
1920{
1921 acpi_handle tmp;
1922
1923 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
1924 tmp == handle)
1925 return true;
1926 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
1927 tmp == handle)
1928 return true;
1929
1930 return false;
1931}
1932
Toshi Kanic0af4172013-03-04 21:30:42 +00001933static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1934 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Len Brown4be44fc2005-08-05 00:44:28 -04001936 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001937 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001938 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001939 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Toshi Kanic0af4172013-03-04 21:30:42 +00001941 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001943 if (handle == ACPI_ROOT_OBJECT) {
1944 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001945 break;
1946 }
1947
Toshi Kanic0af4172013-03-04 21:30:42 +00001948 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001950 pr_err(PREFIX "%s: Error reading device info\n",
1951 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 return;
1953 }
1954
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02001955 if (info->valid & ACPI_VALID_HID) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001956 acpi_add_id(pnp, info->hardware_id.string);
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02001957 pnp->type.platform_id = 1;
1958 }
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001959 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001960 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001961 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001962 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001965 pnp->bus_address = info->address;
1966 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
Lv Zhengccf78042012-10-30 14:41:07 +01001968 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001969 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001970 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001971
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001972 kfree(info);
1973
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001974 /*
1975 * Some devices don't reliably have _HIDs & _CIDs, so add
1976 * synthetic HIDs to make sure drivers can find them.
1977 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001978 if (acpi_is_video_device(handle))
1979 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001980 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001981 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001982 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001983 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001984 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001985 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
Jiang Liuae3caa82014-02-19 14:02:19 +08001986 else if (list_empty(&pnp->ids) &&
1987 acpi_object_is_system_bus(handle)) {
1988 /* \_SB, \_TZ, LNXSYBUS */
1989 acpi_add_id(pnp, ACPI_BUS_HID);
Toshi Kanic0af4172013-03-04 21:30:42 +00001990 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1991 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001992 }
Zhang Rui54735262007-01-11 02:09:09 -05001993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 break;
1995 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001996 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
1998 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001999 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00002002 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 break;
2004 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00002005 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 break;
2007 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00002008 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 break;
2010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Toshi Kanic0af4172013-03-04 21:30:42 +00002013void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
2014{
2015 struct acpi_hardware_id *id, *tmp;
2016
2017 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
2018 kfree(id->id);
2019 kfree(id);
2020 }
2021 kfree(pnp->unique_id);
2022}
2023
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01002024void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
2025 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002027 INIT_LIST_HEAD(&device->pnp.ids);
2028 device->device_type = type;
2029 device->handle = handle;
2030 device->parent = acpi_bus_get_parent(handle);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01002031 device->fwnode.type = FWNODE_ACPI;
Rafael J. Wysocki25db1152013-11-22 21:56:06 +01002032 acpi_set_device_status(device, sta);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002033 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00002034 acpi_set_pnp_ids(handle, &device->pnp, type);
Mika Westerbergffdcd952014-10-21 13:33:55 +02002035 acpi_init_properties(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002036 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002037 device->flags.match_driver = false;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002038 device->flags.initialized = true;
2039 device->flags.visited = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002040 device_initialize(&device->dev);
2041 dev_set_uevent_suppress(&device->dev, true);
2042}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00002043
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002044void acpi_device_add_finalize(struct acpi_device *device)
2045{
2046 dev_set_uevent_suppress(&device->dev, false);
2047 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002050static int acpi_add_single_object(struct acpi_device **child,
2051 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002052 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00002054 int result;
2055 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00002056 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Burman Yan36bcbec2006-12-19 12:56:11 -08002058 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04002060 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04002061 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002064 acpi_init_device_object(device, handle, type, sta);
2065 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01002066 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002068 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002069 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00002070 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002071 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002074 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002075 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002076 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
2077 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
2078 dev_name(&device->dev), (char *) buffer.pointer,
2079 device->parent ? dev_name(&device->parent->dev) : "(null)"));
2080 kfree(buffer.pointer);
2081 *child = device;
2082 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01002083}
2084
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002085static int acpi_bus_type_and_status(acpi_handle handle, int *type,
2086 unsigned long long *sta)
2087{
2088 acpi_status status;
2089 acpi_object_type acpi_type;
2090
2091 status = acpi_get_type(handle, &acpi_type);
2092 if (ACPI_FAILURE(status))
2093 return -ENODEV;
2094
2095 switch (acpi_type) {
2096 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
2097 case ACPI_TYPE_DEVICE:
2098 *type = ACPI_BUS_TYPE_DEVICE;
2099 status = acpi_bus_get_status_handle(handle, sta);
2100 if (ACPI_FAILURE(status))
2101 return -ENODEV;
2102 break;
2103 case ACPI_TYPE_PROCESSOR:
2104 *type = ACPI_BUS_TYPE_PROCESSOR;
2105 status = acpi_bus_get_status_handle(handle, sta);
2106 if (ACPI_FAILURE(status))
2107 return -ENODEV;
2108 break;
2109 case ACPI_TYPE_THERMAL:
2110 *type = ACPI_BUS_TYPE_THERMAL;
2111 *sta = ACPI_STA_DEFAULT;
2112 break;
2113 case ACPI_TYPE_POWER:
2114 *type = ACPI_BUS_TYPE_POWER;
2115 *sta = ACPI_STA_DEFAULT;
2116 break;
2117 default:
2118 return -ENODEV;
2119 }
2120
2121 return 0;
2122}
2123
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002124bool acpi_device_is_present(struct acpi_device *adev)
2125{
2126 if (adev->status.present || adev->status.functional)
2127 return true;
2128
2129 adev->flags.initialized = false;
2130 return false;
2131}
2132
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01002133static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
2134 char *idstr,
2135 const struct acpi_device_id **matchid)
2136{
2137 const struct acpi_device_id *devid;
2138
Rafael J. Wysockiaca0a4e2014-05-30 04:21:52 +02002139 if (handler->match)
2140 return handler->match(idstr, matchid);
2141
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01002142 for (devid = handler->ids; devid->id[0]; devid++)
2143 if (!strcmp((char *)devid->id, idstr)) {
2144 if (matchid)
2145 *matchid = devid;
2146
2147 return true;
2148 }
2149
2150 return false;
2151}
2152
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002153static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
2154 const struct acpi_device_id **matchid)
2155{
2156 struct acpi_scan_handler *handler;
2157
2158 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
2159 if (acpi_scan_handler_matching(handler, idstr, matchid))
2160 return handler;
2161
2162 return NULL;
2163}
2164
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002165void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
2166{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002167 if (!!hotplug->enabled == !!val)
2168 return;
2169
2170 mutex_lock(&acpi_scan_lock);
2171
2172 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002173
2174 mutex_unlock(&acpi_scan_lock);
2175}
2176
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002177static void acpi_scan_init_hotplug(struct acpi_device *adev)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002178{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002179 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002180
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01002181 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002182 acpi_dock_add(adev);
2183 return;
2184 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002185 list_for_each_entry(hwid, &adev->pnp.ids, list) {
2186 struct acpi_scan_handler *handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002187
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002188 handler = acpi_scan_match_handler(hwid->id, NULL);
Rafael J. Wysocki1a699472014-02-06 13:58:13 +01002189 if (handler) {
2190 adev->flags.hotplug_notify = true;
2191 break;
2192 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002193 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01002194}
2195
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002196static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
2197 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002199 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002200 int type;
2201 unsigned long long sta;
2202 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002203
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002204 acpi_bus_get_device(handle, &device);
2205 if (device)
2206 goto out;
2207
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002208 result = acpi_bus_type_and_status(handle, &type, &sta);
2209 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002210 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01002212 if (type == ACPI_BUS_TYPE_POWER) {
2213 acpi_add_power_resource(handle);
2214 return AE_OK;
2215 }
2216
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002217 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00002218 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002219 return AE_CTRL_DEPTH;
2220
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002221 acpi_scan_init_hotplug(device);
2222
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002223 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002224 if (!*return_value)
2225 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002226
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002227 return AE_OK;
2228}
2229
Zhang Rui48459342014-05-30 14:35:34 +02002230static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
2231{
2232 bool *is_spi_i2c_slave_p = data;
2233
2234 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2235 return 1;
2236
2237 /*
2238 * devices that are connected to UART still need to be enumerated to
2239 * platform bus
2240 */
2241 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
2242 *is_spi_i2c_slave_p = true;
2243
2244 /* no need to do more checking */
2245 return -1;
2246}
2247
2248static void acpi_default_enumeration(struct acpi_device *device)
2249{
2250 struct list_head resource_list;
2251 bool is_spi_i2c_slave = false;
2252
2253 if (!device->pnp.type.platform_id || device->handler)
2254 return;
2255
2256 /*
2257 * Do not enemerate SPI/I2C slaves as they will be enuerated by their
2258 * respective parents.
2259 */
2260 INIT_LIST_HEAD(&resource_list);
2261 acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
2262 &is_spi_i2c_slave);
2263 acpi_dev_free_resource_list(&resource_list);
2264 if (!is_spi_i2c_slave)
2265 acpi_create_platform_device(device);
2266}
2267
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002268static int acpi_scan_attach_handler(struct acpi_device *device)
2269{
2270 struct acpi_hardware_id *hwid;
2271 int ret = 0;
2272
2273 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002274 const struct acpi_device_id *devid;
2275 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002276
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002277 handler = acpi_scan_match_handler(hwid->id, &devid);
2278 if (handler) {
Rafael J. Wysockid34afa92014-05-30 04:27:31 +02002279 if (!handler->attach) {
2280 device->pnp.type.platform_id = 0;
2281 continue;
2282 }
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002283 device->handler = handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002284 ret = handler->attach(device, devid);
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002285 if (ret > 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002286 break;
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002287
2288 device->handler = NULL;
2289 if (ret < 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002290 break;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002291 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002292 }
Zhang Rui48459342014-05-30 14:35:34 +02002293 if (!ret)
2294 acpi_default_enumeration(device);
2295
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002296 return ret;
2297}
2298
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002299static void acpi_bus_attach(struct acpi_device *device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002300{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002301 struct acpi_device *child;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002302 acpi_handle ejd;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002303 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002304
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002305 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2306 register_dock_dependent_device(device, ejd);
2307
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002308 acpi_bus_get_status(device);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002309 /* Skip devices that are not present. */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002310 if (!acpi_device_is_present(device)) {
2311 device->flags.visited = false;
2312 return;
2313 }
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002314 if (device->handler)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002315 goto ok;
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002316
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002317 if (!device->flags.initialized) {
2318 acpi_bus_update_power(device, NULL);
2319 device->flags.initialized = true;
2320 }
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002321 device->flags.visited = false;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002322 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002323 if (ret < 0)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002324 return;
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002325
2326 device->flags.match_driver = true;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002327 if (!ret) {
2328 ret = device_attach(&device->dev);
2329 if (ret < 0)
2330 return;
2331 }
2332 device->flags.visited = true;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002333
2334 ok:
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002335 list_for_each_entry(child, &device->children, node)
2336 acpi_bus_attach(child);
Rafael J. Wysocki8ab17fc2014-09-21 02:58:18 +02002337
2338 if (device->handler && device->handler->hotplug.notify_online)
2339 device->handler->hotplug.notify_online(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002342/**
2343 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2344 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01002345 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002346 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2347 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01002348 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002349 * If no devices were found, -ENODEV is returned, but it does not mean that
2350 * there has been a real error. There just have been no suitable ACPI objects
2351 * in the table trunk from which the kernel could create a device and add an
2352 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002353 *
2354 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01002355 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002356int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07002357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 void *device = NULL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002359
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002360 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002362 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002363
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002364 if (device) {
2365 acpi_bus_attach(device);
2366 return 0;
2367 }
2368 return -ENODEV;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002369}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002370EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002372/**
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002373 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2374 * @adev: Root of the ACPI namespace scope to walk.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002375 *
2376 * Must be called under acpi_scan_lock.
2377 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002378void acpi_bus_trim(struct acpi_device *adev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002380 struct acpi_scan_handler *handler = adev->handler;
2381 struct acpi_device *child;
2382
2383 list_for_each_entry_reverse(child, &adev->children, node)
2384 acpi_bus_trim(child);
2385
Rafael J. Wysockia951c772014-01-27 23:08:09 +01002386 adev->flags.match_driver = false;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002387 if (handler) {
2388 if (handler->detach)
2389 handler->detach(adev);
2390
2391 adev->handler = NULL;
2392 } else {
2393 device_release_driver(&adev->dev);
2394 }
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002395 /*
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002396 * Most likely, the device is going away, so put it into D3cold before
2397 * that.
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002398 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002399 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2400 adev->flags.initialized = false;
2401 adev->flags.visited = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
Kristen Accardiceaba662006-02-23 17:56:01 -08002403EXPORT_SYMBOL_GPL(acpi_bus_trim);
2404
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002405static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Len Brown4be44fc2005-08-05 00:44:28 -04002407 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 /*
2410 * Enumerate all fixed-feature devices.
2411 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002412 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2413 struct acpi_device *device = NULL;
2414
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002415 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002416 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002417 ACPI_STA_DEFAULT);
2418 if (result)
2419 return result;
2420
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002421 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002422 result = device_attach(&device->dev);
2423 if (result < 0)
2424 return result;
2425
Daniel Drakec10d7a12012-05-10 00:08:43 +01002426 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002429 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2430 struct acpi_device *device = NULL;
2431
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002432 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002433 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002434 ACPI_STA_DEFAULT);
2435 if (result)
2436 return result;
2437
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002438 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002439 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002442 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
Bjorn Helgaase747f272009-03-24 16:49:43 -06002445int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
2447 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Patrick Mochel5b327262006-05-10 10:33:00 -04002449 result = bus_register(&acpi_bus_type);
2450 if (result) {
2451 /* We don't want to quit even if we failed to add suspend/resume */
2452 printk(KERN_ERR PREFIX "Could not register bus type\n");
2453 }
2454
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002455 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002456 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002457 acpi_processor_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002458 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002459 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002460 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002461 acpi_memory_hotplug_init();
Zhang Ruieec15ed2014-05-30 04:23:01 +02002462 acpi_pnp_init();
Zhang Rui3230bbf2014-03-14 00:34:05 +08002463 acpi_int340x_thermal_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002464
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002465 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 * Enumerate devices in the ACPI namespace.
2468 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002469 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002471 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002473 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002475 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Aaron Lu2650ef42014-04-28 10:38:04 +08002477 /* Fixed feature devices do not exist on HW-reduced platform */
2478 if (!acpi_gbl_reduced_hardware) {
2479 result = acpi_bus_scan_fixed();
2480 if (result) {
2481 acpi_detach_data(acpi_root->handle,
2482 acpi_scan_drop_device);
2483 acpi_device_del(acpi_root);
2484 put_device(&acpi_root->dev);
2485 goto out;
2486 }
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002487 }
2488
2489 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002490
2491 out:
2492 mutex_unlock(&acpi_scan_lock);
2493 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}