blob: d2e3c3e3f9c98eed36074a1a77721adb2eb72a30 [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
Lan Tianyu40e7fcb2014-11-23 21:22:54 +080039static LIST_HEAD(acpi_dep_list);
40static DEFINE_MUTEX(acpi_dep_list_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080041static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010042static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010043static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080044DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045LIST_HEAD(acpi_wakeup_device_list);
Rafael J. Wysockie5255062014-02-04 00:43:17 +010046static DEFINE_MUTEX(acpi_hp_context_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Lan Tianyu40e7fcb2014-11-23 21:22:54 +080048struct acpi_dep_data {
49 struct list_head node;
50 acpi_handle master;
51 acpi_handle slave;
52};
53
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080054struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080055 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080056 unsigned int instance_no;
57 struct list_head node;
58};
Thomas Renninger29b71a12007-07-23 14:43:51 +020059
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010060void acpi_scan_lock_acquire(void)
61{
62 mutex_lock(&acpi_scan_lock);
63}
64EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
65
66void acpi_scan_lock_release(void)
67{
68 mutex_unlock(&acpi_scan_lock);
69}
70EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
71
Rafael J. Wysockie5255062014-02-04 00:43:17 +010072void acpi_lock_hp_context(void)
73{
74 mutex_lock(&acpi_hp_context_lock);
75}
76
77void acpi_unlock_hp_context(void)
78{
79 mutex_unlock(&acpi_hp_context_lock);
80}
81
Rafael J. Wysocki5d513202014-02-22 00:48:31 +010082void acpi_initialize_hp_context(struct acpi_device *adev,
83 struct acpi_hotplug_context *hp,
84 int (*notify)(struct acpi_device *, u32),
85 void (*uevent)(struct acpi_device *, u32))
86{
87 acpi_lock_hp_context();
Rafael J. Wysockiba574dc2014-07-15 22:03:22 +020088 hp->notify = notify;
89 hp->uevent = uevent;
90 acpi_set_hp_context(adev, hp);
Rafael J. Wysocki5d513202014-02-22 00:48:31 +010091 acpi_unlock_hp_context();
92}
93EXPORT_SYMBOL_GPL(acpi_initialize_hp_context);
94
Rafael J. Wysockica589f92013-01-30 14:27:29 +010095int acpi_scan_add_handler(struct acpi_scan_handler *handler)
96{
Rafael J. Wysockid34afa92014-05-30 04:27:31 +020097 if (!handler)
Rafael J. Wysockica589f92013-01-30 14:27:29 +010098 return -EINVAL;
99
100 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
101 return 0;
102}
103
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +0100104int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
105 const char *hotplug_profile_name)
106{
107 int error;
108
109 error = acpi_scan_add_handler(handler);
110 if (error)
111 return error;
112
113 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
114 return 0;
115}
116
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200117/**
118 * create_pnp_modalias - Create hid/cid(s) string for modalias and uevent
119 * @acpi_dev: ACPI device object.
120 * @modalias: Buffer to print into.
121 * @size: Size of the buffer.
122 *
Thomas Renninger29b71a12007-07-23 14:43:51 +0200123 * Creates hid/cid(s) string needed for modalias and uevent
124 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
125 * char *modalias: "acpi:IBM0001:ACPI0001"
Zhang Rui3d8e0092014-01-14 16:46:35 +0800126 * Return: 0: no _HID and no _CID
127 * -EINVAL: output error
128 * -ENOMEM: output is truncated
Thomas Renninger29b71a12007-07-23 14:43:51 +0200129*/
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200130static int create_pnp_modalias(struct acpi_device *acpi_dev, char *modalias,
131 int size)
Adrian Bunkb3e572d2007-08-14 23:22:35 +0200132{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200133 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800134 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600135 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200136
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200137 /*
138 * Since we skip PRP0001 from the modalias below, 0 should be returned
139 * if PRP0001 is the only ACPI/PNP ID in the device's list.
140 */
141 count = 0;
142 list_for_each_entry(id, &acpi_dev->pnp.ids, list)
143 if (strcmp(id->id, "PRP0001"))
144 count++;
145
146 if (!count)
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200147 return 0;
148
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200149 len = snprintf(modalias, size, "acpi:");
150 if (len <= 0)
151 return len;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200152
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200153 size -= len;
Mika Westerberg733e6252014-10-21 13:33:56 +0200154
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200155 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
156 if (!strcmp(id->id, "PRP0001"))
157 continue;
Mika Westerberg733e6252014-10-21 13:33:56 +0200158
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200159 count = snprintf(&modalias[len], size, "%s:", id->id);
160 if (count < 0)
161 return -EINVAL;
Mika Westerberg733e6252014-10-21 13:33:56 +0200162
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200163 if (count >= size)
164 return -ENOMEM;
Mika Westerberg733e6252014-10-21 13:33:56 +0200165
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200166 len += count;
167 size -= count;
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800168 }
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200169 modalias[len] = '\0';
170 return len;
171}
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800172
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200173/**
174 * create_of_modalias - Creates DT compatible string for modalias and uevent
175 * @acpi_dev: ACPI device object.
176 * @modalias: Buffer to print into.
177 * @size: Size of the buffer.
178 *
179 * Expose DT compatible modalias as of:NnameTCcompatible. This function should
180 * only be called for devices having PRP0001 in their list of ACPI/PNP IDs.
181 */
182static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias,
183 int size)
184{
185 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
186 const union acpi_object *of_compatible, *obj;
187 int len, count;
188 int i, nval;
189 char *c;
190
191 acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
192 /* DT strings are all in lower case */
193 for (c = buf.pointer; *c != '\0'; c++)
194 *c = tolower(*c);
195
196 len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
197 ACPI_FREE(buf.pointer);
198
199 if (len <= 0)
200 return len;
201
202 of_compatible = acpi_dev->data.of_compatible;
203 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
204 nval = of_compatible->package.count;
205 obj = of_compatible->package.elements;
206 } else { /* Must be ACPI_TYPE_STRING. */
207 nval = 1;
208 obj = of_compatible;
209 }
210 for (i = 0; i < nval; i++, obj++) {
211 count = snprintf(&modalias[len], size, "C%s",
212 obj->string.pointer);
213 if (count < 0)
214 return -EINVAL;
215
216 if (count >= size)
217 return -ENOMEM;
218
219 len += count;
220 size -= count;
221 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200222 modalias[len] = '\0';
223 return len;
224}
225
Zhang Rui6eb24512014-01-14 16:46:36 +0800226/*
Mika Westerberg52870782014-10-24 16:40:54 +0200227 * acpi_companion_match() - Can we match via ACPI companion device
228 * @dev: Device in question
229 *
230 * Check if the given device has an ACPI companion and if that companion has
231 * a valid list of PNP IDs, and if the device is the first (primary) physical
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200232 * device associated with it. Return the companion pointer if that's the case
233 * or NULL otherwise.
Mika Westerberg52870782014-10-24 16:40:54 +0200234 *
235 * If multiple physical devices are attached to a single ACPI companion, we need
236 * to be careful. The usage scenario for this kind of relationship is that all
237 * of the physical devices in question use resources provided by the ACPI
238 * companion. A typical case is an MFD device where all the sub-devices share
239 * the parent's ACPI companion. In such cases we can only allow the primary
240 * (first) physical device to be matched with the help of the companion's PNP
241 * IDs.
242 *
243 * Additional physical devices sharing the ACPI companion can still use
244 * resources available from it but they will be matched normally using functions
245 * provided by their bus types (and analogously for their modalias).
246 */
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200247static struct acpi_device *acpi_companion_match(const struct device *dev)
Mika Westerberg52870782014-10-24 16:40:54 +0200248{
249 struct acpi_device *adev;
Mika Westerberg52870782014-10-24 16:40:54 +0200250
251 adev = ACPI_COMPANION(dev);
252 if (!adev)
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200253 return NULL;
Mika Westerberg52870782014-10-24 16:40:54 +0200254
255 if (list_empty(&adev->pnp.ids))
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200256 return NULL;
Mika Westerberg52870782014-10-24 16:40:54 +0200257
258 mutex_lock(&adev->physical_node_lock);
259 if (list_empty(&adev->physical_node_list)) {
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200260 adev = NULL;
Mika Westerberg52870782014-10-24 16:40:54 +0200261 } else {
262 const struct acpi_device_physical_node *node;
263
264 node = list_first_entry(&adev->physical_node_list,
265 struct acpi_device_physical_node, node);
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200266 if (node->dev != dev)
267 adev = NULL;
Mika Westerberg52870782014-10-24 16:40:54 +0200268 }
269 mutex_unlock(&adev->physical_node_lock);
270
Rafael J. Wysockie1acdeb2015-04-10 16:07:38 +0200271 return adev;
Mika Westerberg52870782014-10-24 16:40:54 +0200272}
273
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200274static int __acpi_device_uevent_modalias(struct acpi_device *adev,
275 struct kobj_uevent_env *env)
276{
277 int len;
278
279 if (!adev)
280 return -ENODEV;
281
282 if (list_empty(&adev->pnp.ids))
283 return 0;
284
285 if (add_uevent_var(env, "MODALIAS="))
286 return -ENOMEM;
287
288 len = create_pnp_modalias(adev, &env->buf[env->buflen - 1],
289 sizeof(env->buf) - env->buflen);
290 if (len < 0)
291 return len;
292
293 env->buflen += len;
294 if (!adev->data.of_compatible)
295 return 0;
296
297 if (len > 0 && add_uevent_var(env, "MODALIAS="))
298 return -ENOMEM;
299
300 len = create_of_modalias(adev, &env->buf[env->buflen - 1],
301 sizeof(env->buf) - env->buflen);
302 if (len < 0)
303 return len;
304
305 env->buflen += len;
306
307 return 0;
308}
309
Mika Westerberg52870782014-10-24 16:40:54 +0200310/*
Zhang Rui6eb24512014-01-14 16:46:36 +0800311 * Creates uevent modalias field for ACPI enumerated devices.
312 * Because the other buses does not support ACPI HIDs & CIDs.
313 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
314 * "acpi:IBM0001:ACPI0001"
315 */
316int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
317{
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200318 return __acpi_device_uevent_modalias(acpi_companion_match(dev), env);
Zhang Rui6eb24512014-01-14 16:46:36 +0800319}
320EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
321
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200322static int __acpi_device_modalias(struct acpi_device *adev, char *buf, int size)
323{
324 int len, count;
325
326 if (!adev)
327 return -ENODEV;
328
329 if (list_empty(&adev->pnp.ids))
330 return 0;
331
332 len = create_pnp_modalias(adev, buf, size - 1);
333 if (len < 0) {
334 return len;
335 } else if (len > 0) {
336 buf[len++] = '\n';
337 size -= len;
338 }
339 if (!adev->data.of_compatible)
340 return len;
341
342 count = create_of_modalias(adev, buf + len, size - 1);
343 if (count < 0) {
344 return count;
345 } else if (count > 0) {
346 len += count;
347 buf[len++] = '\n';
348 }
349
350 return len;
351}
352
Zhang Rui6eb24512014-01-14 16:46:36 +0800353/*
354 * Creates modalias sysfs attribute for ACPI enumerated devices.
355 * Because the other buses does not support ACPI HIDs & CIDs.
356 * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
357 * "acpi:IBM0001:ACPI0001"
358 */
359int acpi_device_modalias(struct device *dev, char *buf, int size)
360{
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200361 return __acpi_device_modalias(acpi_companion_match(dev), buf, size);
Zhang Rui6eb24512014-01-14 16:46:36 +0800362}
363EXPORT_SYMBOL_GPL(acpi_device_modalias);
364
Thomas Renninger29b71a12007-07-23 14:43:51 +0200365static ssize_t
366acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +0200367 return __acpi_device_modalias(to_acpi_device(dev), buf, 1024);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200368}
369static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
370
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100371bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100372{
373 struct acpi_device_physical_node *pn;
374 bool offline = true;
375
376 mutex_lock(&adev->physical_node_lock);
377
378 list_for_each_entry(pn, &adev->physical_node_list, node)
379 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100380 if (uevent)
381 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
382
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100383 offline = false;
384 break;
385 }
386
387 mutex_unlock(&adev->physical_node_lock);
388 return offline;
389}
390
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100391static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
392 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200393{
394 struct acpi_device *device = NULL;
395 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200396 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200397 acpi_status status = AE_OK;
398
399 if (acpi_bus_get_device(handle, &device))
400 return AE_OK;
401
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100402 if (device->handler && !device->handler->hotplug.enabled) {
403 *ret_p = &device->dev;
404 return AE_SUPPORT;
405 }
406
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200407 mutex_lock(&device->physical_node_lock);
408
409 list_for_each_entry(pn, &device->physical_node_list, node) {
410 int ret;
411
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200412 if (second_pass) {
413 /* Skip devices offlined by the first pass. */
414 if (pn->put_online)
415 continue;
416 } else {
417 pn->put_online = false;
418 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200419 ret = device_offline(pn->dev);
420 if (acpi_force_hot_remove)
421 continue;
422
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200423 if (ret >= 0) {
424 pn->put_online = !ret;
425 } else {
426 *ret_p = pn->dev;
427 if (second_pass) {
428 status = AE_ERROR;
429 break;
430 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200431 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200432 }
433
434 mutex_unlock(&device->physical_node_lock);
435
436 return status;
437}
438
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100439static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
440 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200441{
442 struct acpi_device *device = NULL;
443 struct acpi_device_physical_node *pn;
444
445 if (acpi_bus_get_device(handle, &device))
446 return AE_OK;
447
448 mutex_lock(&device->physical_node_lock);
449
450 list_for_each_entry(pn, &device->physical_node_list, node)
451 if (pn->put_online) {
452 device_online(pn->dev);
453 pn->put_online = false;
454 }
455
456 mutex_unlock(&device->physical_node_lock);
457
458 return AE_OK;
459}
460
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100461static int acpi_scan_try_to_offline(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Yinghai Lu5993c462013-01-11 22:40:41 +0000463 acpi_handle handle = device->handle;
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100464 struct device *errdev = NULL;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100465 acpi_status status;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100466
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200467 /*
468 * Carry out two passes here and ignore errors in the first pass,
469 * because if the devices in question are memory blocks and
470 * CONFIG_MEMCG is set, one of the blocks may hold data structures
471 * that the other blocks depend on, but it is not known in advance which
472 * block holds them.
473 *
474 * If the first pass is successful, the second one isn't needed, though.
475 */
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100476 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
477 NULL, acpi_bus_offline, (void *)false,
478 (void **)&errdev);
479 if (status == AE_SUPPORT) {
480 dev_warn(errdev, "Offline disabled.\n");
481 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
482 acpi_bus_online, NULL, NULL, NULL);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100483 return -EPERM;
484 }
485 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200486 if (errdev) {
487 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200488 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100489 NULL, acpi_bus_offline, (void *)true,
490 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200491 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100492 acpi_bus_offline(handle, 0, (void *)true,
493 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200494
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200495 if (errdev && !acpi_force_hot_remove) {
496 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100497 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200498 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100499 ACPI_UINT32_MAX, acpi_bus_online,
500 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200501 return -EBUSY;
502 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200503 }
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100504 return 0;
505}
506
507static int acpi_scan_hot_remove(struct acpi_device *device)
508{
509 acpi_handle handle = device->handle;
510 unsigned long long sta;
511 acpi_status status;
512
Tang Chendee15922014-08-08 10:30:45 +0800513 if (device->handler && device->handler->hotplug.demand_offline
514 && !acpi_force_hot_remove) {
Rafael J. Wysockicaa73ea2013-12-29 15:25:48 +0100515 if (!acpi_scan_is_offline(device, true))
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100516 return -EBUSY;
517 } else {
518 int error = acpi_scan_try_to_offline(device);
519 if (error)
520 return error;
521 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200522
Zhang Rui26d46862008-04-29 02:35:48 -0400523 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100524 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400525
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100526 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200527
Jiang Liu7d2421f2013-06-29 00:24:40 +0800528 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
530 * TBD: _EJD support.
531 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800532 status = acpi_evaluate_ej0(handle);
533 if (status == AE_NOT_FOUND)
534 return -ENODEV;
535 else if (ACPI_FAILURE(status))
536 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100537
Toshi Kani882fd122013-03-13 19:29:26 +0000538 /*
539 * Verify if eject was indeed successful. If not, log an error
540 * message. No need to call _OST since _EJ0 call was made OK.
541 */
542 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
543 if (ACPI_FAILURE(status)) {
544 acpi_handle_warn(handle,
545 "Status check after eject failed (0x%x)\n", status);
546 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
547 acpi_handle_warn(handle,
548 "Eject incomplete - status 0x%llx\n", sta);
549 }
550
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100551 return 0;
552}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100553
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100554static int acpi_scan_device_not_present(struct acpi_device *adev)
555{
556 if (!acpi_device_enumerated(adev)) {
557 dev_warn(&adev->dev, "Still not present\n");
558 return -EALREADY;
559 }
560 acpi_bus_trim(adev);
561 return 0;
562}
563
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100564static int acpi_scan_device_check(struct acpi_device *adev)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100565{
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200566 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100567
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100568 acpi_bus_get_status(adev);
569 if (adev->status.present || adev->status.functional) {
570 /*
571 * This function is only called for device objects for which
572 * matching scan handlers exist. The only situation in which
573 * the scan handler is not attached to this device object yet
574 * is when the device has just appeared (either it wasn't
575 * present at all before or it was removed and then added
576 * again).
577 */
578 if (adev->handler) {
579 dev_warn(&adev->dev, "Already enumerated\n");
580 return -EALREADY;
581 }
582 error = acpi_bus_scan(adev->handle);
583 if (error) {
584 dev_warn(&adev->dev, "Namespace scan failure\n");
585 return error;
586 }
587 if (!adev->handler) {
588 dev_warn(&adev->dev, "Enumeration failure\n");
589 error = -ENODEV;
590 }
591 } else {
592 error = acpi_scan_device_not_present(adev);
593 }
594 return error;
595}
596
597static int acpi_scan_bus_check(struct acpi_device *adev)
598{
599 struct acpi_scan_handler *handler = adev->handler;
600 struct acpi_device *child;
601 int error;
602
603 acpi_bus_get_status(adev);
604 if (!(adev->status.present || adev->status.functional)) {
605 acpi_scan_device_not_present(adev);
606 return 0;
607 }
608 if (handler && handler->hotplug.scan_dependent)
609 return handler->hotplug.scan_dependent(adev);
610
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100611 error = acpi_bus_scan(adev->handle);
612 if (error) {
613 dev_warn(&adev->dev, "Namespace scan failure\n");
614 return error;
615 }
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100616 list_for_each_entry(child, &adev->children, node) {
617 error = acpi_scan_bus_check(child);
618 if (error)
619 return error;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100620 }
621 return 0;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100622}
623
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100624static int acpi_generic_hotplug_event(struct acpi_device *adev, u32 type)
625{
626 switch (type) {
627 case ACPI_NOTIFY_BUS_CHECK:
628 return acpi_scan_bus_check(adev);
629 case ACPI_NOTIFY_DEVICE_CHECK:
630 return acpi_scan_device_check(adev);
631 case ACPI_NOTIFY_EJECT_REQUEST:
632 case ACPI_OST_EC_OSPM_EJECT:
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100633 if (adev->handler && !adev->handler->hotplug.enabled) {
634 dev_info(&adev->dev, "Eject disabled\n");
635 return -EPERM;
636 }
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100637 acpi_evaluate_ost(adev->handle, ACPI_NOTIFY_EJECT_REQUEST,
638 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100639 return acpi_scan_hot_remove(adev);
640 }
641 return -EINVAL;
642}
643
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +0100644void acpi_device_hotplug(struct acpi_device *adev, u32 src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100645{
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100646 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100647 int error = -ENODEV;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100648
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200649 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200650 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100651
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100652 /*
653 * The device object's ACPI handle cannot become invalid as long as we
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100654 * are holding acpi_scan_lock, but it might have become invalid before
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100655 * that lock was acquired.
656 */
657 if (adev->handle == INVALID_ACPI_HANDLE)
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100658 goto err_out;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100659
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +0100660 if (adev->flags.is_dock_station) {
661 error = dock_notify(adev, src);
662 } else if (adev->flags.hotplug_notify) {
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100663 error = acpi_generic_hotplug_event(adev, src);
Rafael J. Wysockidd2151b2014-02-04 00:44:02 +0100664 if (error == -EPERM) {
665 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
666 goto err_out;
667 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100668 } else {
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100669 int (*notify)(struct acpi_device *, u32);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100670
671 acpi_lock_hp_context();
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100672 notify = adev->hp ? adev->hp->notify : NULL;
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100673 acpi_unlock_hp_context();
674 /*
675 * There may be additional notify handlers for device objects
676 * without the .event() callback, so ignore them here.
677 */
Rafael J. Wysockibe27b3d2014-02-21 01:10:27 +0100678 if (notify)
679 error = notify(adev, src);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100680 else
681 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100682 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100683 if (!error)
684 ost_code = ACPI_OST_SC_SUCCESS;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100685
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100686 err_out:
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100687 acpi_evaluate_ost(adev->handle, src, ost_code, NULL);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +0100688
689 out:
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +0100690 acpi_bus_put_acpi_device(adev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100691 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200692 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100693}
694
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100695static ssize_t real_power_state_show(struct device *dev,
696 struct device_attribute *attr, char *buf)
697{
698 struct acpi_device *adev = to_acpi_device(dev);
699 int state;
700 int ret;
701
702 ret = acpi_device_get_power(adev, &state);
703 if (ret)
704 return ret;
705
706 return sprintf(buf, "%s\n", acpi_power_state_string(state));
707}
708
709static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
710
711static ssize_t power_state_show(struct device *dev,
712 struct device_attribute *attr, char *buf)
713{
714 struct acpi_device *adev = to_acpi_device(dev);
715
716 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
717}
718
719static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800722acpi_eject_store(struct device *d, struct device_attribute *attr,
723 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800725 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100726 acpi_object_type not_used;
727 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100729 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100732 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
733 && !acpi_device->driver)
734 return -ENODEV;
735
736 status = acpi_get_type(acpi_device->handle, &not_used);
737 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
738 return -ENODEV;
739
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100740 get_device(&acpi_device->dev);
Rafael J. Wysocki1e3bcb52014-03-03 00:40:38 +0100741 status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200742 if (ACPI_SUCCESS(status))
743 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100744
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200745 put_device(&acpi_device->dev);
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100746 acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
747 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100748 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800751static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800753static ssize_t
754acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
755 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600757 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800758}
759static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
760
Lv Zheng923d4a42012-10-30 14:43:26 +0100761static ssize_t acpi_device_uid_show(struct device *dev,
762 struct device_attribute *attr, char *buf)
763{
764 struct acpi_device *acpi_dev = to_acpi_device(dev);
765
766 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
767}
768static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
769
770static ssize_t acpi_device_adr_show(struct device *dev,
771 struct device_attribute *attr, char *buf)
772{
773 struct acpi_device *acpi_dev = to_acpi_device(dev);
774
775 return sprintf(buf, "0x%08x\n",
776 (unsigned int)(acpi_dev->pnp.bus_address));
777}
778static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
779
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800780static ssize_t
781acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
782 struct acpi_device *acpi_dev = to_acpi_device(dev);
783 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
784 int result;
785
786 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600787 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800788 goto end;
789
790 result = sprintf(buf, "%s\n", (char*)path.pointer);
791 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600792end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800793 return result;
794}
795static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
796
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600797/* sysfs file that shows description text from the ACPI _STR method */
798static ssize_t description_show(struct device *dev,
799 struct device_attribute *attr,
800 char *buf) {
801 struct acpi_device *acpi_dev = to_acpi_device(dev);
802 int result;
803
804 if (acpi_dev->pnp.str_obj == NULL)
805 return 0;
806
807 /*
808 * The _STR object contains a Unicode identifier for a device.
809 * We need to convert to utf-8 so it can be displayed.
810 */
811 result = utf16s_to_utf8s(
812 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
813 acpi_dev->pnp.str_obj->buffer.length,
814 UTF16_LITTLE_ENDIAN, buf,
815 PAGE_SIZE);
816
817 buf[result++] = '\n';
818
819 return result;
820}
821static DEVICE_ATTR(description, 0444, description_show, NULL);
822
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100823static ssize_t
824acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
825 char *buf) {
826 struct acpi_device *acpi_dev = to_acpi_device(dev);
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900827 acpi_status status;
828 unsigned long long sun;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100829
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900830 status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
831 if (ACPI_FAILURE(status))
832 return -ENODEV;
833
834 return sprintf(buf, "%llu\n", sun);
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100835}
836static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
837
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800838static ssize_t status_show(struct device *dev, struct device_attribute *attr,
839 char *buf) {
840 struct acpi_device *acpi_dev = to_acpi_device(dev);
841 acpi_status status;
842 unsigned long long sta;
843
844 status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
845 if (ACPI_FAILURE(status))
846 return -ENODEV;
847
848 return sprintf(buf, "%llu\n", sta);
849}
850static DEVICE_ATTR_RO(status);
851
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800852static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600854 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800855 acpi_status status;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800856 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800858 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800859 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800860 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600861 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800862 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600863 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800864 goto end;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200867 if (!list_empty(&dev->pnp.ids)) {
868 result = device_create_file(&dev->dev, &dev_attr_hid);
869 if (result)
870 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200872 result = device_create_file(&dev->dev, &dev_attr_modalias);
873 if (result)
874 goto end;
875 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200876
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600877 /*
878 * If device has _STR, 'description' file is created
879 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800880 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600881 status = acpi_evaluate_object(dev->handle, "_STR",
882 NULL, &buffer);
883 if (ACPI_FAILURE(status))
884 buffer.pointer = NULL;
885 dev->pnp.str_obj = buffer.pointer;
886 result = device_create_file(&dev->dev, &dev_attr_description);
887 if (result)
888 goto end;
889 }
890
Toshi Kanid4e1a692013-03-04 21:30:41 +0000891 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100892 result = device_create_file(&dev->dev, &dev_attr_adr);
893 if (dev->pnp.unique_id)
894 result = device_create_file(&dev->dev, &dev_attr_uid);
895
Yasuaki Ishimatsua383b682014-09-03 13:39:13 +0900896 if (acpi_has_method(dev->handle, "_SUN")) {
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100897 result = device_create_file(&dev->dev, &dev_attr_sun);
898 if (result)
899 goto end;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100900 }
901
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800902 if (acpi_has_method(dev->handle, "_STA")) {
903 result = device_create_file(&dev->dev, &dev_attr_status);
904 if (result)
905 goto end;
906 }
907
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800908 /*
909 * If device has _EJ0, 'eject' file is created that is used to trigger
910 * hot-removal function from userland.
911 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800912 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800913 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100914 if (result)
915 return result;
916 }
917
918 if (dev->flags.power_manageable) {
919 result = device_create_file(&dev->dev, &dev_attr_power_state);
920 if (result)
921 return result;
922
923 if (dev->power.flags.power_resources)
924 result = device_create_file(&dev->dev,
925 &dev_attr_real_power_state);
926 }
927
Alex Chiang0c526d92009-05-14 08:31:37 -0600928end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800929 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800930}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800932static void acpi_device_remove_files(struct acpi_device *dev)
933{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100934 if (dev->flags.power_manageable) {
935 device_remove_file(&dev->dev, &dev_attr_power_state);
936 if (dev->power.flags.power_resources)
937 device_remove_file(&dev->dev,
938 &dev_attr_real_power_state);
939 }
940
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800941 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600942 * If device has _STR, remove 'description' file
943 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800944 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600945 kfree(dev->pnp.str_obj);
946 device_remove_file(&dev->dev, &dev_attr_description);
947 }
948 /*
949 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800950 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800951 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800952 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800953
Jiang Liu952c63e2013-06-29 00:24:38 +0800954 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100955 device_remove_file(&dev->dev, &dev_attr_sun);
956
Lv Zheng923d4a42012-10-30 14:43:26 +0100957 if (dev->pnp.unique_id)
958 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000959 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100960 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600961 device_remove_file(&dev->dev, &dev_attr_modalias);
962 device_remove_file(&dev->dev, &dev_attr_hid);
Srinivas Pandruvadac713cd72014-01-10 16:00:05 -0800963 if (acpi_has_method(dev->handle, "_STA"))
964 device_remove_file(&dev->dev, &dev_attr_status);
Alex Chiang0c526d92009-05-14 08:31:37 -0600965 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800966 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800967}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800969 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200971
Mika Westerbergcf761af2012-10-31 22:44:41 +0100972/**
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +0200973 * acpi_of_match_device - Match device object using the "compatible" property.
974 * @adev: ACPI device object to match.
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +0200975 * @of_match_table: List of device IDs to match against.
976 *
977 * If @dev has an ACPI companion which has the special PRP0001 device ID in its
978 * list of identifiers and a _DSD object with the "compatible" property, use
979 * that property to match against the given list of identifiers.
980 */
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +0200981static bool acpi_of_match_device(struct acpi_device *adev,
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +0200982 const struct of_device_id *of_match_table)
Mika Westerberg733e6252014-10-21 13:33:56 +0200983{
984 const union acpi_object *of_compatible, *obj;
Mika Westerberg733e6252014-10-21 13:33:56 +0200985 int i, nval;
986
Mika Westerberg733e6252014-10-21 13:33:56 +0200987 if (!adev)
988 return false;
989
990 of_compatible = adev->data.of_compatible;
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +0200991 if (!of_match_table || !of_compatible)
Mika Westerberg733e6252014-10-21 13:33:56 +0200992 return false;
993
994 if (of_compatible->type == ACPI_TYPE_PACKAGE) {
995 nval = of_compatible->package.count;
996 obj = of_compatible->package.elements;
997 } else { /* Must be ACPI_TYPE_STRING. */
998 nval = 1;
999 obj = of_compatible;
1000 }
1001 /* Now we can look for the driver DT compatible strings */
1002 for (i = 0; i < nval; i++, obj++) {
1003 const struct of_device_id *id;
1004
Rafael J. Wysocki54fe9ce2015-04-10 16:07:30 +02001005 for (id = of_match_table; id->compatible[0]; id++)
Mika Westerberg733e6252014-10-21 13:33:56 +02001006 if (!strcasecmp(obj->string.pointer, id->compatible))
1007 return true;
1008 }
1009
1010 return false;
1011}
1012
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +02001013static const struct acpi_device_id *__acpi_match_device(
1014 struct acpi_device *device,
1015 const struct acpi_device_id *ids,
1016 const struct of_device_id *of_ids)
1017{
1018 const struct acpi_device_id *id;
1019 struct acpi_hardware_id *hwid;
1020
1021 /*
1022 * If the device is not present, it is unnecessary to load device
1023 * driver for it.
1024 */
1025 if (!device || !device->status.present)
1026 return NULL;
1027
1028 list_for_each_entry(hwid, &device->pnp.ids, list) {
1029 /* First, check the ACPI/PNP IDs provided by the caller. */
1030 for (id = ids; id->id[0]; id++)
1031 if (!strcmp((char *) id->id, hwid->id))
1032 return id;
1033
1034 /*
1035 * Next, check the special "PRP0001" ID and try to match the
1036 * "compatible" property if found.
1037 *
1038 * The id returned by the below is not valid, but the only
1039 * caller passing non-NULL of_ids here is only interested in
1040 * whether or not the return value is NULL.
1041 */
1042 if (!strcmp("PRP0001", hwid->id)
1043 && acpi_of_match_device(device, of_ids))
1044 return id;
1045 }
1046 return NULL;
1047}
1048
1049/**
1050 * acpi_match_device - Match a struct device against a given list of ACPI IDs
1051 * @ids: Array of struct acpi_device_id object to match against.
1052 * @dev: The device structure to match.
1053 *
1054 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
1055 * object for that handle and use that object to match against a given list of
1056 * device IDs.
1057 *
1058 * Return a pointer to the first matching ID on success or %NULL on failure.
1059 */
1060const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
1061 const struct device *dev)
1062{
1063 return __acpi_match_device(acpi_companion_match(dev), ids, NULL);
1064}
1065EXPORT_SYMBOL_GPL(acpi_match_device);
1066
1067int acpi_match_device_ids(struct acpi_device *device,
1068 const struct acpi_device_id *ids)
1069{
1070 return __acpi_match_device(device, ids, NULL) ? 0 : -ENOENT;
1071}
1072EXPORT_SYMBOL(acpi_match_device_ids);
1073
Mika Westerberg733e6252014-10-21 13:33:56 +02001074bool acpi_driver_match_device(struct device *dev,
1075 const struct device_driver *drv)
1076{
1077 if (!drv->acpi_match_table)
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +02001078 return acpi_of_match_device(ACPI_COMPANION(dev),
1079 drv->of_match_table);
Mika Westerberg733e6252014-10-21 13:33:56 +02001080
Rafael J. Wysocki2b9c6982015-04-10 16:07:51 +02001081 return !!__acpi_match_device(acpi_companion_match(dev),
1082 drv->acpi_match_table, drv->of_match_table);
Mika Westerberg733e6252014-10-21 13:33:56 +02001083}
1084EXPORT_SYMBOL_GPL(acpi_driver_match_device);
1085
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001086static void acpi_free_power_resources_lists(struct acpi_device *device)
1087{
1088 int i;
1089
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001090 if (device->wakeup.flags.valid)
1091 acpi_power_resources_list_free(&device->wakeup.resources);
1092
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01001093 if (!device->power.flags.power_resources)
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001094 return;
1095
1096 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
1097 struct acpi_device_power_state *ps = &device->power.states[i];
1098 acpi_power_resources_list_free(&ps->resources);
1099 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001100}
1101
Patrick Mochel1890a972006-12-07 20:56:31 +08001102static void acpi_device_release(struct device *dev)
1103{
1104 struct acpi_device *acpi_dev = to_acpi_device(dev);
1105
Mika Westerbergffdcd952014-10-21 13:33:55 +02001106 acpi_free_properties(acpi_dev);
Toshi Kanic0af4172013-03-04 21:30:42 +00001107 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001108 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +08001109 kfree(acpi_dev);
1110}
1111
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001112static int acpi_bus_match(struct device *dev, struct device_driver *drv)
1113{
1114 struct acpi_device *acpi_dev = to_acpi_device(dev);
1115 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1116
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +01001117 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001118 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001119}
1120
Kay Sievers7eff2e72007-08-14 15:15:12 +02001121static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001122{
Rafael J. Wysocki8765c5b2015-04-13 00:29:36 +02001123 return __acpi_device_uevent_modalias(to_acpi_device(dev), env);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001126static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
1127{
1128 struct acpi_device *device = data;
1129
1130 device->driver->ops.notify(device, event);
1131}
1132
Lan Tianyu236105d2014-08-26 01:29:24 +02001133static void acpi_device_notify_fixed(void *data)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001134{
1135 struct acpi_device *device = data;
1136
Bjorn Helgaas53de5352009-08-31 22:32:20 +00001137 /* Fixed hardware devices have no handles */
1138 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Lan Tianyu236105d2014-08-26 01:29:24 +02001139}
1140
Aaron Lufd9caef2015-03-13 13:52:50 +08001141static u32 acpi_device_fixed_event(void *data)
Lan Tianyu236105d2014-08-26 01:29:24 +02001142{
1143 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
Aaron Lufd9caef2015-03-13 13:52:50 +08001144 return ACPI_INTERRUPT_HANDLED;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001145}
1146
1147static int acpi_device_install_notify_handler(struct acpi_device *device)
1148{
1149 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001150
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001151 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001152 status =
1153 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001154 acpi_device_fixed_event,
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001155 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001156 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001157 status =
1158 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001159 acpi_device_fixed_event,
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001160 device);
1161 else
1162 status = acpi_install_notify_handler(device->handle,
1163 ACPI_DEVICE_NOTIFY,
1164 acpi_device_notify,
1165 device);
1166
1167 if (ACPI_FAILURE(status))
1168 return -EINVAL;
1169 return 0;
1170}
1171
1172static void acpi_device_remove_notify_handler(struct acpi_device *device)
1173{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001174 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001175 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001176 acpi_device_fixed_event);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +00001177 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001178 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
Lan Tianyu236105d2014-08-26 01:29:24 +02001179 acpi_device_fixed_event);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001180 else
1181 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1182 acpi_device_notify);
1183}
1184
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001185static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +08001186{
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001187 struct acpi_device *acpi_dev = to_acpi_device(dev);
1188 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
1189 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Rafael J. Wysockifc2e0a82014-08-26 01:29:21 +02001191 if (acpi_dev->handler && !acpi_is_pnp_device(acpi_dev))
Rafael J. Wysocki24071f42013-06-16 00:36:41 +02001192 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001193
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001194 if (!acpi_drv->ops.add)
1195 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001196
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001197 ret = acpi_drv->ops.add(acpi_dev);
1198 if (ret)
1199 return ret;
1200
1201 acpi_dev->driver = acpi_drv;
1202 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1203 "Driver [%s] successfully bound to device [%s]\n",
1204 acpi_drv->name, acpi_dev->pnp.bus_id));
1205
1206 if (acpi_drv->ops.notify) {
1207 ret = acpi_device_install_notify_handler(acpi_dev);
1208 if (ret) {
1209 if (acpi_drv->ops.remove)
1210 acpi_drv->ops.remove(acpi_dev);
1211
1212 acpi_dev->driver = NULL;
1213 acpi_dev->driver_data = NULL;
1214 return ret;
1215 }
Zhang Rui9e89dde2006-12-07 20:56:16 +08001216 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +02001217
1218 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1219 acpi_drv->name, acpi_dev->pnp.bus_id));
1220 get_device(dev);
1221 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001222}
1223
1224static int acpi_device_remove(struct device * dev)
1225{
1226 struct acpi_device *acpi_dev = to_acpi_device(dev);
1227 struct acpi_driver *acpi_drv = acpi_dev->driver;
1228
1229 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +00001230 if (acpi_drv->ops.notify)
1231 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001232 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +01001233 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001234 }
1235 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001236 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001237
1238 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +08001239 return 0;
1240}
1241
David Brownell55955aa2007-05-08 00:28:35 -07001242struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +08001243 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +08001244 .match = acpi_bus_match,
1245 .probe = acpi_device_probe,
1246 .remove = acpi_device_remove,
1247 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248};
1249
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001250static void acpi_device_del(struct acpi_device *device)
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001251{
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001252 mutex_lock(&acpi_device_lock);
1253 if (device->parent)
1254 list_del(&device->node);
1255
1256 list_del(&device->wakeup_list);
1257 mutex_unlock(&acpi_device_lock);
1258
1259 acpi_power_add_remove_device(device, false);
1260 acpi_device_remove_files(device);
1261 if (device->remove)
1262 device->remove(device);
1263
1264 device_del(&device->dev);
1265}
1266
1267static LIST_HEAD(acpi_device_del_list);
1268static DEFINE_MUTEX(acpi_device_del_lock);
1269
1270static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1271{
1272 for (;;) {
1273 struct acpi_device *adev;
1274
1275 mutex_lock(&acpi_device_del_lock);
1276
1277 if (list_empty(&acpi_device_del_list)) {
1278 mutex_unlock(&acpi_device_del_lock);
1279 break;
1280 }
1281 adev = list_first_entry(&acpi_device_del_list,
1282 struct acpi_device, del_list);
1283 list_del(&adev->del_list);
1284
1285 mutex_unlock(&acpi_device_del_lock);
1286
1287 acpi_device_del(adev);
1288 /*
1289 * Drop references to all power resources that might have been
1290 * used by the device.
1291 */
1292 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1293 put_device(&adev->dev);
1294 }
1295}
1296
1297/**
1298 * acpi_scan_drop_device - Drop an ACPI device object.
1299 * @handle: Handle of an ACPI namespace node, not used.
1300 * @context: Address of the ACPI device object to drop.
1301 *
1302 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1303 * namespace node the device object pointed to by @context is attached to.
1304 *
1305 * The unregistration is carried out asynchronously to avoid running
1306 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1307 * ensure the correct ordering (the device objects must be unregistered in the
1308 * same order in which the corresponding namespace nodes are deleted).
1309 */
1310static void acpi_scan_drop_device(acpi_handle handle, void *context)
1311{
1312 static DECLARE_WORK(work, acpi_device_del_work_fn);
1313 struct acpi_device *adev = context;
1314
1315 mutex_lock(&acpi_device_del_lock);
1316
1317 /*
1318 * Use the ACPI hotplug workqueue which is ordered, so this work item
1319 * won't run after any hotplug work items submitted subsequently. That
1320 * prevents attempts to register device objects identical to those being
1321 * deleted from happening concurrently (such attempts result from
1322 * hotplug events handled via the ACPI hotplug workqueue). It also will
1323 * run after all of the work items submitted previosuly, which helps
1324 * those work items to ensure that they are not accessing stale device
1325 * objects.
1326 */
1327 if (list_empty(&acpi_device_del_list))
1328 acpi_queue_hotplug_work(&work);
1329
1330 list_add_tail(&adev->del_list, &acpi_device_del_list);
1331 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1332 adev->handle = INVALID_ACPI_HANDLE;
1333
1334 mutex_unlock(&acpi_device_del_lock);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001335}
1336
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001337static int acpi_get_device_data(acpi_handle handle, struct acpi_device **device,
1338 void (*callback)(void *))
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001339{
1340 acpi_status status;
1341
1342 if (!device)
1343 return -EINVAL;
1344
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001345 status = acpi_get_data_full(handle, acpi_scan_drop_device,
1346 (void **)device, callback);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001347 if (ACPI_FAILURE(status) || !*device) {
1348 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1349 handle));
1350 return -ENODEV;
1351 }
1352 return 0;
1353}
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001354
1355int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1356{
1357 return acpi_get_device_data(handle, device, NULL);
1358}
Rafael J. Wysocki65859252013-10-01 23:02:43 +02001359EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001360
Rafael J. Wysocki78ea4632014-02-04 00:43:05 +01001361static void get_acpi_device(void *dev)
1362{
1363 if (dev)
1364 get_device(&((struct acpi_device *)dev)->dev);
1365}
1366
1367struct acpi_device *acpi_bus_get_acpi_device(acpi_handle handle)
1368{
1369 struct acpi_device *adev = NULL;
1370
1371 acpi_get_device_data(handle, &adev, get_acpi_device);
1372 return adev;
1373}
1374
1375void acpi_bus_put_acpi_device(struct acpi_device *adev)
1376{
1377 put_device(&adev->dev);
1378}
1379
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001380int acpi_device_add(struct acpi_device *device,
1381 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001383 int result;
1384 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1385 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001386
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001387 if (device->handle) {
1388 acpi_status status;
1389
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001390 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001391 device);
1392 if (ACPI_FAILURE(status)) {
1393 acpi_handle_err(device->handle,
1394 "Unable to attach device data\n");
1395 return -ENODEV;
1396 }
1397 }
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /*
1400 * Linkage
1401 * -------
1402 * Link this device to its parent and siblings.
1403 */
1404 INIT_LIST_HEAD(&device->children);
1405 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001407 INIT_LIST_HEAD(&device->physical_node_list);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001408 INIT_LIST_HEAD(&device->del_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001409 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001411 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1412 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001413 pr_err(PREFIX "Memory allocation error\n");
1414 result = -ENOMEM;
1415 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001416 }
1417
Shaohua Li90905892009-04-07 10:24:29 +08001418 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001419 /*
1420 * Find suitable bus_id and instance number in acpi_bus_id_list
1421 * If failed, create one and link it into acpi_bus_id_list
1422 */
1423 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001424 if (!strcmp(acpi_device_bus_id->bus_id,
1425 acpi_device_hid(device))) {
1426 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001427 found = 1;
1428 kfree(new_bus_id);
1429 break;
1430 }
1431 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001432 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001433 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001434 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001435 acpi_device_bus_id->instance_no = 0;
1436 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1437 }
Kay Sievers07944692008-10-30 01:18:59 +01001438 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 +08001439
Len Brown33b57152008-12-15 22:09:26 -05001440 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (device->wakeup.flags.valid)
1444 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001445 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Patrick Mochel1890a972006-12-07 20:56:31 +08001447 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001448 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001449 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001450 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001451 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001452 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001453 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001454 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001455 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001456
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001457 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001458 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001459 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1460 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001461
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001462 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001463
1464 err:
Shaohua Li90905892009-04-07 10:24:29 +08001465 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001466 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001467 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001468 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001469 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001470
1471 err_detach:
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001472 acpi_detach_data(device->handle, acpi_scan_drop_device);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001473 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01001476struct acpi_device *acpi_get_next_child(struct device *dev,
1477 struct acpi_device *child)
1478{
1479 struct acpi_device *adev = ACPI_COMPANION(dev);
1480 struct list_head *head, *next;
1481
1482 if (!adev)
1483 return NULL;
1484
1485 head = &adev->children;
1486 if (list_empty(head))
1487 return NULL;
1488
1489 if (!child)
1490 return list_first_entry(head, struct acpi_device, node);
1491
1492 next = child->node.next;
1493 return next == head ? NULL : list_entry(next, struct acpi_device, node);
1494}
1495
Zhang Rui9e89dde2006-12-07 20:56:16 +08001496/* --------------------------------------------------------------------------
1497 Driver Management
1498 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001500 * acpi_bus_register_driver - register a driver with the ACPI bus
1501 * @driver: driver being registered
1502 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001504 * devices that match the driver's criteria and binds. Returns zero for
1505 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 */
Len Brown4be44fc2005-08-05 00:44:28 -04001507int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Patrick Mochel1890a972006-12-07 20:56:31 +08001509 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001512 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001513 driver->drv.name = driver->name;
1514 driver->drv.bus = &acpi_bus_type;
1515 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Patrick Mochel1890a972006-12-07 20:56:31 +08001517 ret = driver_register(&driver->drv);
1518 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Len Brown4be44fc2005-08-05 00:44:28 -04001521EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001524 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001525 * @driver: driver to unregister
1526 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1528 * devices that match the driver's criteria and unbinds.
1529 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001530void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Patrick Mochel1890a972006-12-07 20:56:31 +08001532 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
Len Brown4be44fc2005-08-05 00:44:28 -04001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535EXPORT_SYMBOL(acpi_bus_unregister_driver);
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/* --------------------------------------------------------------------------
1538 Device Enumeration
1539 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001540static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1541{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001542 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001543 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001544
1545 /*
1546 * Fixed hardware devices do not appear in the namespace and do not
1547 * have handles, but we fabricate acpi_devices for them, so we have
1548 * to deal with them specially.
1549 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001550 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001551 return acpi_root;
1552
1553 do {
1554 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001555 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001556 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1557 } while (acpi_bus_get_device(handle, &device));
1558 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001559}
1560
Len Brownc8f7a622006-07-09 17:22:28 -04001561acpi_status
1562acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1563{
1564 acpi_status status;
1565 acpi_handle tmp;
1566 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1567 union acpi_object *obj;
1568
1569 status = acpi_get_handle(handle, "_EJD", &tmp);
1570 if (ACPI_FAILURE(status))
1571 return status;
1572
1573 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1574 if (ACPI_SUCCESS(status)) {
1575 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001576 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1577 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001578 kfree(buffer.pointer);
1579 }
1580 return status;
1581}
1582EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1583
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001584static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1585 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001587 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1588 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001590 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001591 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001593 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001594 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001596 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001598 /* _PRW */
1599 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1600 if (ACPI_FAILURE(status)) {
1601 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001602 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001603 }
1604
1605 package = (union acpi_object *)buffer.pointer;
1606
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001607 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001608 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001611 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001612 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (element->type == ACPI_TYPE_PACKAGE) {
1615 if ((element->package.count < 2) ||
1616 (element->package.elements[0].type !=
1617 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001618 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001619 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001620
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001621 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001623 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 (u32) element->package.elements[1].integer.value;
1625 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001626 wakeup->gpe_device = NULL;
1627 wakeup->gpe_number = element->integer.value;
1628 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001629 goto out;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001633 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001634 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001635
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001636 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001638 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1639 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001642 if (!list_empty(&wakeup->resources)) {
1643 int sleep_state;
1644
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001645 err = acpi_power_wakeup_list_init(&wakeup->resources,
1646 &sleep_state);
1647 if (err) {
1648 acpi_handle_warn(handle, "Retrieving current states "
1649 "of wakeup power resources failed\n");
1650 acpi_power_resources_list_free(&wakeup->resources);
1651 goto out;
1652 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001653 if (sleep_state < wakeup->sleep_state) {
1654 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1655 "(S%d) by S%d from power resources\n",
1656 (int)wakeup->sleep_state, sleep_state);
1657 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001660
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001661 out:
1662 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001663 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664}
1665
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001666static void acpi_wakeup_gpe_init(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001668 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001669 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001670 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001671 {"PNP0C0E", 0},
1672 {"", 0},
1673 };
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001674 struct acpi_device_wakeup *wakeup = &device->wakeup;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001675 acpi_status status;
1676 acpi_event_status event_status;
1677
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001678 wakeup->flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001679
1680 /* Power button, Lid switch always enable wakeup */
1681 if (!acpi_match_device_ids(device, button_device_ids)) {
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001682 wakeup->flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001683 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1684 /* Do not use Lid/sleep button for S5 wakeup */
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001685 if (wakeup->sleep_state == ACPI_STATE_S5)
1686 wakeup->sleep_state = ACPI_STATE_S4;
Zhang Ruib7e38302012-12-04 23:23:16 +01001687 }
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001688 acpi_mark_gpe_for_wake(wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001689 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001690 return;
1691 }
1692
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001693 acpi_setup_gpe_for_wake(device->handle, wakeup->gpe_device,
1694 wakeup->gpe_number);
1695 status = acpi_get_gpe_status(wakeup->gpe_device, wakeup->gpe_number,
1696 &event_status);
1697 if (ACPI_FAILURE(status))
1698 return;
1699
Lv Zheng2f857232014-10-10 10:40:05 +08001700 wakeup->flags.run_wake = !!(event_status & ACPI_EVENT_FLAG_HAS_HANDLER);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001701}
1702
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001703static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001704{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001705 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001706
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001707 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001708 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001709 return;
1710
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001711 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1712 &device->wakeup);
1713 if (err) {
1714 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001715 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001719 device->wakeup.prepare_count = 0;
Rafael J. Wysockibd9b2f92014-07-14 22:41:41 +02001720 acpi_wakeup_gpe_init(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001721 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1722 * system for the ACPI device with the _PRW object.
1723 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1724 * So it is necessary to call _DSW object first. Only when it is not
1725 * present will the _PSW object used.
1726 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001727 err = acpi_device_sleep_wake(device, 0, 0, 0);
1728 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001729 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1730 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001733static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001735 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001736 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1737 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001738 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001739
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001740 INIT_LIST_HEAD(&ps->resources);
1741
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001742 /* Evaluate "_PRx" to get referenced power resources */
1743 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1744 if (ACPI_SUCCESS(status)) {
1745 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001746
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001747 if (buffer.length && package
1748 && package->type == ACPI_TYPE_PACKAGE
1749 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001750 int err = acpi_extract_power_resources(package, 0,
1751 &ps->resources);
1752 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001753 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001754 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001755 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001756 }
1757
1758 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001759 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001760 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001761 ps->flags.explicit_set = 1;
1762
1763 /*
1764 * State is valid if there are means to put the device into it.
1765 * D3hot is only valid if _PR3 present.
1766 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001767 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001768 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1769 ps->flags.valid = 1;
1770 ps->flags.os_accessible = 1;
1771 }
1772
1773 ps->power = -1; /* Unknown - driver assigned */
1774 ps->latency = -1; /* Unknown - driver assigned */
1775}
1776
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001777static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001779 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001781 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001782 if (!acpi_has_method(device->handle, "_PS0") &&
1783 !acpi_has_method(device->handle, "_PR0"))
1784 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001785
1786 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001789 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001791 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001792 device->power.flags.explicit_get = 1;
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +02001793
Jiang Liu952c63e2013-06-29 00:24:38 +08001794 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001795 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Rafael J. Wysockif25c0ae2014-05-17 00:18:13 +02001797 if (acpi_has_method(device->handle, "_DSW"))
1798 device->power.flags.dsw_present = 1;
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001801 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001803 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1804 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001806 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Zhang Rui9e89dde2006-12-07 20:56:16 +08001808 /* Set defaults for D0 and D3 states (always valid) */
1809 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1810 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001811 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1812 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001814 /* Set D3cold's explicit_set flag if _PS3 exists. */
1815 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1816 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1817
Aaron Lu1399dfc2012-11-21 23:33:40 +01001818 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1819 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1820 device->power.flags.power_resources)
1821 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1822
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01001823 if (acpi_bus_init_power(device))
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001824 device->flags.power_manageable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001827static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001830 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 device->flags.dynamic_status = 1;
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001834 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 device->flags.removable = 1;
1836
1837 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001838 if (acpi_has_method(device->handle, "_EJD") ||
1839 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001843static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Len Brown4be44fc2005-08-05 00:44:28 -04001845 char bus_id[5] = { '?', 0 };
1846 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1847 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 /*
1850 * Bus ID
1851 * ------
1852 * The device's Bus ID is simply the object name.
1853 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1854 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001855 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001857 return;
1858 }
1859
1860 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 case ACPI_BUS_TYPE_POWER_BUTTON:
1862 strcpy(device->pnp.bus_id, "PWRF");
1863 break;
1864 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1865 strcpy(device->pnp.bus_id, "SLPF");
1866 break;
1867 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001868 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /* Clean up trailing underscores (if any) */
1870 for (i = 3; i > 1; i--) {
1871 if (bus_id[i] == '_')
1872 bus_id[i] = '\0';
1873 else
1874 break;
1875 }
1876 strcpy(device->pnp.bus_id, bus_id);
1877 break;
1878 }
1879}
1880
Zhang Rui54735262007-01-11 02:09:09 -05001881/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001882 * acpi_ata_match - see if an acpi object is an ATA device
1883 *
1884 * If an acpi object has one of the ACPI ATA methods defined,
1885 * then we can safely call it an ATA device.
1886 */
1887bool acpi_ata_match(acpi_handle handle)
1888{
1889 return acpi_has_method(handle, "_GTF") ||
1890 acpi_has_method(handle, "_GTM") ||
1891 acpi_has_method(handle, "_STM") ||
1892 acpi_has_method(handle, "_SDD");
1893}
1894
1895/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001896 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001897 *
1898 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1899 * then we can safely call it an ejectable drive bay
1900 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001901bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001902{
Zhang Rui54735262007-01-11 02:09:09 -05001903 acpi_handle phandle;
1904
Jiang Liu952c63e2013-06-29 00:24:38 +08001905 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001906 return false;
1907 if (acpi_ata_match(handle))
1908 return true;
1909 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1910 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001911
Jiang Liuebf4df82013-06-29 00:24:41 +08001912 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001913}
1914
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001915bool acpi_device_is_battery(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001916{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001917 struct acpi_hardware_id *hwid;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001918
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001919 list_for_each_entry(hwid, &adev->pnp.ids, list)
1920 if (!strcmp("PNP0C0A", hwid->id))
1921 return true;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001922
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001923 return false;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001924}
1925
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001926static bool is_ejectable_bay(struct acpi_device *adev)
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001927{
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01001928 acpi_handle handle = adev->handle;
1929
1930 if (acpi_has_method(handle, "_EJ0") && acpi_device_is_battery(adev))
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01001931 return true;
1932
1933 return acpi_bay_match(handle);
1934}
1935
Frank Seidel3620f2f2007-12-07 13:20:34 +01001936/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001937 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001938 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001939bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001940{
Jiang Liuebf4df82013-06-29 00:24:41 +08001941 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001942}
1943
Thomas Renninger620e1122010-10-01 10:54:00 +02001944const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001945{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001946 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001947
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001948 if (list_empty(&device->pnp.ids))
1949 return dummy_hid;
1950
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001951 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1952 return hid->id;
1953}
1954EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001955
Toshi Kanid4e1a692013-03-04 21:30:41 +00001956static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001957{
1958 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001959
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001960 id = kmalloc(sizeof(*id), GFP_KERNEL);
1961 if (!id)
1962 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001963
Thomas Meyer581de592011-08-06 11:32:56 +02001964 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001965 if (!id->id) {
1966 kfree(id);
1967 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001968 }
1969
Toshi Kanid4e1a692013-03-04 21:30:41 +00001970 list_add_tail(&id->list, &pnp->ids);
1971 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001972}
1973
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001974/*
1975 * Old IBM workstations have a DSDT bug wherein the SMBus object
1976 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1977 * prefix. Work around this.
1978 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001979static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001980{
Jiang Liuebf4df82013-06-29 00:24:41 +08001981 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1982 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001983
1984 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001985 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001986
1987 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001988 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1989 strcmp("SMBS", path.pointer))
1990 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001991
1992 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001993 if (acpi_has_method(handle, "SBI") &&
1994 acpi_has_method(handle, "SBR") &&
1995 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001996 return true;
1997
1998 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001999}
2000
Jiang Liuae3caa82014-02-19 14:02:19 +08002001static bool acpi_object_is_system_bus(acpi_handle handle)
2002{
2003 acpi_handle tmp;
2004
2005 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_SB", &tmp)) &&
2006 tmp == handle)
2007 return true;
2008 if (ACPI_SUCCESS(acpi_get_handle(NULL, "\\_TZ", &tmp)) &&
2009 tmp == handle)
2010 return true;
2011
2012 return false;
2013}
2014
Toshi Kanic0af4172013-03-04 21:30:42 +00002015static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
2016 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Len Brown4be44fc2005-08-05 00:44:28 -04002018 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06002019 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00002020 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06002021 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Toshi Kanic0af4172013-03-04 21:30:42 +00002023 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00002025 if (handle == ACPI_ROOT_OBJECT) {
2026 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00002027 break;
2028 }
2029
Toshi Kanic0af4172013-03-04 21:30:42 +00002030 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00002032 pr_err(PREFIX "%s: Error reading device info\n",
2033 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return;
2035 }
2036
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02002037 if (info->valid & ACPI_VALID_HID) {
Toshi Kanic0af4172013-03-04 21:30:42 +00002038 acpi_add_id(pnp, info->hardware_id.string);
Rafael J. Wysocki549e6842014-05-30 04:26:18 +02002039 pnp->type.platform_id = 1;
2040 }
Bjorn Helgaas57f36742009-09-21 13:35:40 -06002041 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08002042 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06002043 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00002044 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06002045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00002047 pnp->bus_address = info->address;
2048 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
Lv Zhengccf78042012-10-30 14:41:07 +01002050 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00002051 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01002052 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08002053
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04002054 kfree(info);
2055
Bjorn Helgaas57f36742009-09-21 13:35:40 -06002056 /*
2057 * Some devices don't reliably have _HIDs & _CIDs, so add
2058 * synthetic HIDs to make sure drivers can find them.
2059 */
Toshi Kanic0af4172013-03-04 21:30:42 +00002060 if (acpi_is_video_device(handle))
2061 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08002062 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00002063 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08002064 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00002065 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08002066 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00002067 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
Jiang Liuae3caa82014-02-19 14:02:19 +08002068 else if (list_empty(&pnp->ids) &&
2069 acpi_object_is_system_bus(handle)) {
2070 /* \_SB, \_TZ, LNXSYBUS */
2071 acpi_add_id(pnp, ACPI_BUS_HID);
Toshi Kanic0af4172013-03-04 21:30:42 +00002072 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
2073 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06002074 }
Zhang Rui54735262007-01-11 02:09:09 -05002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 break;
2077 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00002078 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 break;
2080 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00002081 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00002084 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 break;
2086 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00002087 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 break;
2089 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00002090 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 break;
2092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Toshi Kanic0af4172013-03-04 21:30:42 +00002095void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
2096{
2097 struct acpi_hardware_id *id, *tmp;
2098
2099 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
2100 kfree(id->id);
2101 kfree(id);
2102 }
2103 kfree(pnp->unique_id);
2104}
2105
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01002106void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
2107 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002109 INIT_LIST_HEAD(&device->pnp.ids);
2110 device->device_type = type;
2111 device->handle = handle;
2112 device->parent = acpi_bus_get_parent(handle);
Rafael J. Wysocki8a0662d2014-11-04 14:03:59 +01002113 device->fwnode.type = FWNODE_ACPI;
Rafael J. Wysocki25db1152013-11-22 21:56:06 +01002114 acpi_set_device_status(device, sta);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002115 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00002116 acpi_set_pnp_ids(handle, &device->pnp, type);
Mika Westerbergffdcd952014-10-21 13:33:55 +02002117 acpi_init_properties(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002118 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002119 device->flags.match_driver = false;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002120 device->flags.initialized = true;
2121 device->flags.visited = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002122 device_initialize(&device->dev);
2123 dev_set_uevent_suppress(&device->dev, true);
2124}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00002125
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002126void acpi_device_add_finalize(struct acpi_device *device)
2127{
2128 dev_set_uevent_suppress(&device->dev, false);
2129 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002132static int acpi_add_single_object(struct acpi_device **child,
2133 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002134 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00002136 int result;
2137 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00002138 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Burman Yan36bcbec2006-12-19 12:56:11 -08002140 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04002142 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04002143 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002146 acpi_init_device_object(device, handle, type, sta);
2147 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01002148 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002150 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002151 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00002152 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002153 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002156 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01002157 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01002158 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
2159 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
2160 dev_name(&device->dev), (char *) buffer.pointer,
2161 device->parent ? dev_name(&device->parent->dev) : "(null)"));
2162 kfree(buffer.pointer);
2163 *child = device;
2164 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01002165}
2166
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002167static int acpi_bus_type_and_status(acpi_handle handle, int *type,
2168 unsigned long long *sta)
2169{
2170 acpi_status status;
2171 acpi_object_type acpi_type;
2172
2173 status = acpi_get_type(handle, &acpi_type);
2174 if (ACPI_FAILURE(status))
2175 return -ENODEV;
2176
2177 switch (acpi_type) {
2178 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
2179 case ACPI_TYPE_DEVICE:
2180 *type = ACPI_BUS_TYPE_DEVICE;
2181 status = acpi_bus_get_status_handle(handle, sta);
2182 if (ACPI_FAILURE(status))
2183 return -ENODEV;
2184 break;
2185 case ACPI_TYPE_PROCESSOR:
2186 *type = ACPI_BUS_TYPE_PROCESSOR;
2187 status = acpi_bus_get_status_handle(handle, sta);
2188 if (ACPI_FAILURE(status))
2189 return -ENODEV;
2190 break;
2191 case ACPI_TYPE_THERMAL:
2192 *type = ACPI_BUS_TYPE_THERMAL;
2193 *sta = ACPI_STA_DEFAULT;
2194 break;
2195 case ACPI_TYPE_POWER:
2196 *type = ACPI_BUS_TYPE_POWER;
2197 *sta = ACPI_STA_DEFAULT;
2198 break;
2199 default:
2200 return -ENODEV;
2201 }
2202
2203 return 0;
2204}
2205
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002206bool acpi_device_is_present(struct acpi_device *adev)
2207{
2208 if (adev->status.present || adev->status.functional)
2209 return true;
2210
2211 adev->flags.initialized = false;
2212 return false;
2213}
2214
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01002215static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
2216 char *idstr,
2217 const struct acpi_device_id **matchid)
2218{
2219 const struct acpi_device_id *devid;
2220
Rafael J. Wysockiaca0a4e2014-05-30 04:21:52 +02002221 if (handler->match)
2222 return handler->match(idstr, matchid);
2223
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01002224 for (devid = handler->ids; devid->id[0]; devid++)
2225 if (!strcmp((char *)devid->id, idstr)) {
2226 if (matchid)
2227 *matchid = devid;
2228
2229 return true;
2230 }
2231
2232 return false;
2233}
2234
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002235static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
2236 const struct acpi_device_id **matchid)
2237{
2238 struct acpi_scan_handler *handler;
2239
2240 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
2241 if (acpi_scan_handler_matching(handler, idstr, matchid))
2242 return handler;
2243
2244 return NULL;
2245}
2246
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002247void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
2248{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002249 if (!!hotplug->enabled == !!val)
2250 return;
2251
2252 mutex_lock(&acpi_scan_lock);
2253
2254 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01002255
2256 mutex_unlock(&acpi_scan_lock);
2257}
2258
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002259static void acpi_scan_init_hotplug(struct acpi_device *adev)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002260{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002261 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002262
Rafael J. Wysockib43109f2014-02-16 00:09:34 +01002263 if (acpi_dock_match(adev->handle) || is_ejectable_bay(adev)) {
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002264 acpi_dock_add(adev);
2265 return;
2266 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002267 list_for_each_entry(hwid, &adev->pnp.ids, list) {
2268 struct acpi_scan_handler *handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002269
Toshi Kani6b772e8f2013-03-04 21:30:43 +00002270 handler = acpi_scan_match_handler(hwid->id, NULL);
Rafael J. Wysocki1a699472014-02-06 13:58:13 +01002271 if (handler) {
2272 adev->flags.hotplug_notify = true;
2273 break;
2274 }
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002275 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01002276}
2277
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002278static void acpi_device_dep_initialize(struct acpi_device *adev)
2279{
2280 struct acpi_dep_data *dep;
2281 struct acpi_handle_list dep_devices;
2282 acpi_status status;
2283 int i;
2284
2285 if (!acpi_has_method(adev->handle, "_DEP"))
2286 return;
2287
2288 status = acpi_evaluate_reference(adev->handle, "_DEP", NULL,
2289 &dep_devices);
2290 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki80167a22014-12-12 22:48:44 +01002291 dev_dbg(&adev->dev, "Failed to evaluate _DEP.\n");
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002292 return;
2293 }
2294
2295 for (i = 0; i < dep_devices.count; i++) {
2296 struct acpi_device_info *info;
2297 int skip;
2298
2299 status = acpi_get_object_info(dep_devices.handles[i], &info);
2300 if (ACPI_FAILURE(status)) {
Rafael J. Wysocki80167a22014-12-12 22:48:44 +01002301 dev_dbg(&adev->dev, "Error reading _DEP device info\n");
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002302 continue;
2303 }
2304
2305 /*
2306 * Skip the dependency of Windows System Power
2307 * Management Controller
2308 */
2309 skip = info->valid & ACPI_VALID_HID &&
2310 !strcmp(info->hardware_id.string, "INT3396");
2311
2312 kfree(info);
2313
2314 if (skip)
2315 continue;
2316
2317 dep = kzalloc(sizeof(struct acpi_dep_data), GFP_KERNEL);
2318 if (!dep)
2319 return;
2320
2321 dep->master = dep_devices.handles[i];
2322 dep->slave = adev->handle;
2323 adev->dep_unmet++;
2324
2325 mutex_lock(&acpi_dep_list_lock);
2326 list_add_tail(&dep->node , &acpi_dep_list);
2327 mutex_unlock(&acpi_dep_list_lock);
2328 }
2329}
2330
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002331static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
2332 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002334 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002335 int type;
2336 unsigned long long sta;
2337 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002338
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002339 acpi_bus_get_device(handle, &device);
2340 if (device)
2341 goto out;
2342
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00002343 result = acpi_bus_type_and_status(handle, &type, &sta);
2344 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002345 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01002347 if (type == ACPI_BUS_TYPE_POWER) {
2348 acpi_add_power_resource(handle);
2349 return AE_OK;
2350 }
2351
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002352 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00002353 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002354 return AE_CTRL_DEPTH;
2355
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002356 acpi_scan_init_hotplug(device);
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002357 acpi_device_dep_initialize(device);
Rafael J. Wysocki3c2cc7f2014-02-06 17:31:37 +01002358
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01002359 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002360 if (!*return_value)
2361 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01002362
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002363 return AE_OK;
2364}
2365
Zhang Rui48459342014-05-30 14:35:34 +02002366static int acpi_check_spi_i2c_slave(struct acpi_resource *ares, void *data)
2367{
2368 bool *is_spi_i2c_slave_p = data;
2369
2370 if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
2371 return 1;
2372
2373 /*
2374 * devices that are connected to UART still need to be enumerated to
2375 * platform bus
2376 */
2377 if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
2378 *is_spi_i2c_slave_p = true;
2379
2380 /* no need to do more checking */
2381 return -1;
2382}
2383
2384static void acpi_default_enumeration(struct acpi_device *device)
2385{
2386 struct list_head resource_list;
2387 bool is_spi_i2c_slave = false;
2388
2389 if (!device->pnp.type.platform_id || device->handler)
2390 return;
2391
2392 /*
2393 * Do not enemerate SPI/I2C slaves as they will be enuerated by their
2394 * respective parents.
2395 */
2396 INIT_LIST_HEAD(&resource_list);
2397 acpi_dev_get_resources(device, &resource_list, acpi_check_spi_i2c_slave,
2398 &is_spi_i2c_slave);
2399 acpi_dev_free_resource_list(&resource_list);
2400 if (!is_spi_i2c_slave)
2401 acpi_create_platform_device(device);
2402}
2403
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002404static int acpi_scan_attach_handler(struct acpi_device *device)
2405{
2406 struct acpi_hardware_id *hwid;
2407 int ret = 0;
2408
2409 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002410 const struct acpi_device_id *devid;
2411 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002412
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002413 handler = acpi_scan_match_handler(hwid->id, &devid);
2414 if (handler) {
Rafael J. Wysockid34afa92014-05-30 04:27:31 +02002415 if (!handler->attach) {
2416 device->pnp.type.platform_id = 0;
2417 continue;
2418 }
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002419 device->handler = handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002420 ret = handler->attach(device, devid);
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002421 if (ret > 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002422 break;
Rafael J. Wysocki9cb32ac2014-02-11 00:35:46 +01002423
2424 device->handler = NULL;
2425 if (ret < 0)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002426 break;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01002427 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01002428 }
Zhang Rui48459342014-05-30 14:35:34 +02002429 if (!ret)
2430 acpi_default_enumeration(device);
2431
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002432 return ret;
2433}
2434
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002435static void acpi_bus_attach(struct acpi_device *device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002436{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002437 struct acpi_device *child;
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002438 acpi_handle ejd;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002439 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002440
Rafael J. Wysocki1e2380c2014-02-16 01:51:01 +01002441 if (ACPI_SUCCESS(acpi_bus_get_ejd(device->handle, &ejd)))
2442 register_dock_dependent_device(device, ejd);
2443
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002444 acpi_bus_get_status(device);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002445 /* Skip devices that are not present. */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002446 if (!acpi_device_is_present(device)) {
2447 device->flags.visited = false;
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01002448 device->flags.power_manageable = 0;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002449 return;
2450 }
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002451 if (device->handler)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002452 goto ok;
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02002453
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002454 if (!device->flags.initialized) {
Rafael J. Wysocki1b1f3e12015-01-01 23:38:28 +01002455 device->flags.power_manageable =
2456 device->power.states[ACPI_STATE_D0].flags.valid;
2457 if (acpi_bus_init_power(device))
2458 device->flags.power_manageable = 0;
2459
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002460 device->flags.initialized = true;
2461 }
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002462 device->flags.visited = false;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002463 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002464 if (ret < 0)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002465 return;
Rafael J. Wysocki69310072013-11-07 01:41:01 +01002466
2467 device->flags.match_driver = true;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002468 if (!ret) {
2469 ret = device_attach(&device->dev);
2470 if (ret < 0)
2471 return;
2472 }
2473 device->flags.visited = true;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002474
2475 ok:
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002476 list_for_each_entry(child, &device->children, node)
2477 acpi_bus_attach(child);
Rafael J. Wysocki8ab17fc2014-09-21 02:58:18 +02002478
2479 if (device->handler && device->handler->hotplug.notify_online)
2480 device->handler->hotplug.notify_online(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Lan Tianyu40e7fcb2014-11-23 21:22:54 +08002483void acpi_walk_dep_device_list(acpi_handle handle)
2484{
2485 struct acpi_dep_data *dep, *tmp;
2486 struct acpi_device *adev;
2487
2488 mutex_lock(&acpi_dep_list_lock);
2489 list_for_each_entry_safe(dep, tmp, &acpi_dep_list, node) {
2490 if (dep->master == handle) {
2491 acpi_bus_get_device(dep->slave, &adev);
2492 if (!adev)
2493 continue;
2494
2495 adev->dep_unmet--;
2496 if (!adev->dep_unmet)
2497 acpi_bus_attach(adev);
2498 list_del(&dep->node);
2499 kfree(dep);
2500 }
2501 }
2502 mutex_unlock(&acpi_dep_list_lock);
2503}
2504EXPORT_SYMBOL_GPL(acpi_walk_dep_device_list);
2505
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002506/**
2507 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2508 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01002509 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002510 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2511 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01002512 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002513 * If no devices were found, -ENODEV is returned, but it does not mean that
2514 * there has been a real error. There just have been no suitable ACPI objects
2515 * in the table trunk from which the kernel could create a device and add an
2516 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002517 *
2518 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01002519 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002520int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07002521{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 void *device = NULL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002523
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002524 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002526 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002527
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002528 if (device) {
2529 acpi_bus_attach(device);
2530 return 0;
2531 }
2532 return -ENODEV;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002533}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002534EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002536/**
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002537 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2538 * @adev: Root of the ACPI namespace scope to walk.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002539 *
2540 * Must be called under acpi_scan_lock.
2541 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002542void acpi_bus_trim(struct acpi_device *adev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002544 struct acpi_scan_handler *handler = adev->handler;
2545 struct acpi_device *child;
2546
2547 list_for_each_entry_reverse(child, &adev->children, node)
2548 acpi_bus_trim(child);
2549
Rafael J. Wysockia951c772014-01-27 23:08:09 +01002550 adev->flags.match_driver = false;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002551 if (handler) {
2552 if (handler->detach)
2553 handler->detach(adev);
2554
2555 adev->handler = NULL;
2556 } else {
2557 device_release_driver(&adev->dev);
2558 }
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002559 /*
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002560 * Most likely, the device is going away, so put it into D3cold before
2561 * that.
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002562 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002563 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2564 adev->flags.initialized = false;
2565 adev->flags.visited = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
Kristen Accardiceaba662006-02-23 17:56:01 -08002567EXPORT_SYMBOL_GPL(acpi_bus_trim);
2568
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002569static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Len Brown4be44fc2005-08-05 00:44:28 -04002571 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 /*
2574 * Enumerate all fixed-feature devices.
2575 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002576 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2577 struct acpi_device *device = NULL;
2578
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002579 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002580 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002581 ACPI_STA_DEFAULT);
2582 if (result)
2583 return result;
2584
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002585 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002586 result = device_attach(&device->dev);
2587 if (result < 0)
2588 return result;
2589
Daniel Drakec10d7a12012-05-10 00:08:43 +01002590 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002593 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2594 struct acpi_device *device = NULL;
2595
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002596 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002597 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002598 ACPI_STA_DEFAULT);
2599 if (result)
2600 return result;
2601
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002602 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002603 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002606 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
2608
Bjorn Helgaase747f272009-03-24 16:49:43 -06002609int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
2611 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Patrick Mochel5b327262006-05-10 10:33:00 -04002613 result = bus_register(&acpi_bus_type);
2614 if (result) {
2615 /* We don't want to quit even if we failed to add suspend/resume */
2616 printk(KERN_ERR PREFIX "Could not register bus type\n");
2617 }
2618
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002619 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002620 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002621 acpi_processor_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002622 acpi_lpss_init();
Ken Xue92082a82015-02-06 08:27:51 +08002623 acpi_apd_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002624 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002625 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002626 acpi_memory_hotplug_init();
Zhang Ruieec15ed2014-05-30 04:23:01 +02002627 acpi_pnp_init();
Zhang Rui3230bbf2014-03-14 00:34:05 +08002628 acpi_int340x_thermal_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002629
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002630 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 * Enumerate devices in the ACPI namespace.
2633 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002634 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002636 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002638 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Aaron Lu2650ef42014-04-28 10:38:04 +08002642 /* Fixed feature devices do not exist on HW-reduced platform */
2643 if (!acpi_gbl_reduced_hardware) {
2644 result = acpi_bus_scan_fixed();
2645 if (result) {
2646 acpi_detach_data(acpi_root->handle,
2647 acpi_scan_drop_device);
2648 acpi_device_del(acpi_root);
2649 put_device(&acpi_root->dev);
2650 goto out;
2651 }
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002652 }
2653
2654 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002655
2656 out:
2657 mutex_unlock(&acpi_scan_lock);
2658 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}