blob: 45fbe95ba1f34cb08ca05c74130c11af195fe971 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040022extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020025#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUS_DEVICE_NAME "System Bus"
27
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
Thomas Renninger620e1122010-10-01 10:54:00 +020030static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080033static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010034static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010035static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080036DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037LIST_HEAD(acpi_wakeup_device_list);
38
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080039struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080040 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080041 unsigned int instance_no;
42 struct list_head node;
43};
Thomas Renninger29b71a12007-07-23 14:43:51 +020044
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010045void acpi_scan_lock_acquire(void)
46{
47 mutex_lock(&acpi_scan_lock);
48}
49EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
50
51void acpi_scan_lock_release(void)
52{
53 mutex_unlock(&acpi_scan_lock);
54}
55EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
56
Rafael J. Wysockica589f92013-01-30 14:27:29 +010057int acpi_scan_add_handler(struct acpi_scan_handler *handler)
58{
59 if (!handler || !handler->attach)
60 return -EINVAL;
61
62 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
63 return 0;
64}
65
Thomas Renninger29b71a12007-07-23 14:43:51 +020066/*
67 * Creates hid/cid(s) string needed for modalias and uevent
68 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
69 * char *modalias: "acpi:IBM0001:ACPI0001"
70*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020071static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
72 int size)
73{
Thomas Renninger29b71a12007-07-23 14:43:51 +020074 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080075 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060076 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020077
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020078 if (list_empty(&acpi_dev->pnp.ids))
79 return 0;
80
Zhang Rui5c9fcb52008-03-20 16:40:32 +080081 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020082 size -= len;
83
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060084 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
85 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080086 if (count < 0 || count >= size)
87 return -EINVAL;
88 len += count;
89 size -= count;
90 }
91
Thomas Renninger29b71a12007-07-23 14:43:51 +020092 modalias[len] = '\0';
93 return len;
94}
95
96static ssize_t
97acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
98 struct acpi_device *acpi_dev = to_acpi_device(dev);
99 int len;
100
101 /* Device has no HID and no CID or string is >1024 */
102 len = create_modalias(acpi_dev, buf, 1024);
103 if (len <= 0)
104 return 0;
105 buf[len++] = '\n';
106 return len;
107}
108static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
109
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100110static int acpi_scan_hot_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Yinghai Lu5993c462013-01-11 22:40:41 +0000112 acpi_handle handle = device->handle;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100113 acpi_handle not_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 struct acpi_object_list arg_list;
115 union acpi_object arg;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100116 acpi_status status;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100117
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100118 /* If there is no handle, the device node has been unregistered. */
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100119 if (!handle) {
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100120 dev_dbg(&device->dev, "ACPI handle missing\n");
121 put_device(&device->dev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100122 return -EINVAL;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100123 }
124
Zhang Rui26d46862008-04-29 02:35:48 -0400125 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100126 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400127
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100128 acpi_bus_trim(device);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100129 /* Device node has been unregistered. */
130 put_device(&device->dev);
Toshi Kanib3c450c2012-10-26 13:38:57 +0200131 device = NULL;
132
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100133 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &not_used))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 arg_list.count = 1;
135 arg_list.pointer = &arg;
136 arg.type = ACPI_TYPE_INTEGER;
137 arg.integer.value = 0;
138 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
139 }
140
141 arg_list.count = 1;
142 arg_list.pointer = &arg;
143 arg.type = ACPI_TYPE_INTEGER;
144 arg.integer.value = 1;
145
146 /*
147 * TBD: _EJD support.
148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600150 if (ACPI_FAILURE(status)) {
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100151 if (status == AE_NOT_FOUND) {
152 return -ENODEV;
153 } else {
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100154 acpi_handle_warn(handle, "Eject failed\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100155 return -EIO;
156 }
157 }
158 return 0;
159}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100160
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100161static void acpi_bus_device_eject(void *context)
162{
163 acpi_handle handle = context;
164 struct acpi_device *device = NULL;
165 struct acpi_scan_handler *handler;
166 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
167
168 mutex_lock(&acpi_scan_lock);
169
170 acpi_bus_get_device(handle, &device);
171 if (!device)
172 goto err_out;
173
174 handler = device->handler;
175 if (!handler || !handler->hotplug.enabled) {
176 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
177 goto err_out;
178 }
179 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
180 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
181 if (handler->hotplug.mode == AHM_CONTAINER) {
182 device->flags.eject_pending = true;
183 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
184 } else {
185 int error;
186
187 get_device(&device->dev);
188 error = acpi_scan_hot_remove(device);
189 if (error)
190 goto err_out;
Toshi Kanic4753e52012-05-23 20:25:20 -0600191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100193 out:
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100194 mutex_unlock(&acpi_scan_lock);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800195 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100196
197 err_out:
198 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
199 NULL);
200 goto out;
201}
202
203static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
204{
205 struct acpi_device *device = NULL;
206 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
207 int error;
208
209 mutex_lock(&acpi_scan_lock);
210
211 acpi_bus_get_device(handle, &device);
212 if (device) {
213 dev_warn(&device->dev, "Attempt to re-insert\n");
214 goto out;
215 }
216 acpi_evaluate_hotplug_ost(handle, ost_source,
217 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
218 error = acpi_bus_scan(handle);
219 if (error) {
220 acpi_handle_warn(handle, "Namespace scan failure\n");
221 goto out;
222 }
223 error = acpi_bus_get_device(handle, &device);
224 if (error) {
225 acpi_handle_warn(handle, "Missing device node object\n");
226 goto out;
227 }
228 ost_code = ACPI_OST_SC_SUCCESS;
229 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
230 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
231
232 out:
233 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
234 mutex_unlock(&acpi_scan_lock);
235}
236
237static void acpi_scan_bus_check(void *context)
238{
239 acpi_scan_bus_device_check((acpi_handle)context,
240 ACPI_NOTIFY_BUS_CHECK);
241}
242
243static void acpi_scan_device_check(void *context)
244{
245 acpi_scan_bus_device_check((acpi_handle)context,
246 ACPI_NOTIFY_DEVICE_CHECK);
247}
248
249static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *not_used)
250{
251 acpi_osd_exec_callback callback;
252 acpi_status status;
253
254 switch (type) {
255 case ACPI_NOTIFY_BUS_CHECK:
256 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
257 callback = acpi_scan_bus_check;
258 break;
259 case ACPI_NOTIFY_DEVICE_CHECK:
260 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
261 callback = acpi_scan_device_check;
262 break;
263 case ACPI_NOTIFY_EJECT_REQUEST:
264 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
265 callback = acpi_bus_device_eject;
266 break;
267 default:
268 /* non-hotplug event; possibly handled by other handler */
269 return;
270 }
271 status = acpi_os_hotplug_execute(callback, handle);
272 if (ACPI_FAILURE(status))
273 acpi_evaluate_hotplug_ost(handle, type,
274 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
275 NULL);
276}
277
278/**
279 * acpi_bus_hot_remove_device: hot-remove a device and its children
280 * @context: struct acpi_eject_event pointer (freed in this func)
281 *
282 * Hot-remove a device and its children. This function frees up the
283 * memory space passed by arg context, so that the caller may call
284 * this function asynchronously through acpi_os_hotplug_execute().
285 */
286void acpi_bus_hot_remove_device(void *context)
287{
288 struct acpi_eject_event *ej_event = context;
289 struct acpi_device *device = ej_event->device;
290 acpi_handle handle = device->handle;
291 int error;
292
293 mutex_lock(&acpi_scan_lock);
294
295 error = acpi_scan_hot_remove(device);
296 if (error && handle)
297 acpi_evaluate_hotplug_ost(handle, ej_event->event,
298 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
299 NULL);
300
301 mutex_unlock(&acpi_scan_lock);
302 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
Toshi Kani61622ac2012-11-01 14:42:12 +0000304EXPORT_SYMBOL(acpi_bus_hot_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100306static ssize_t real_power_state_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
308{
309 struct acpi_device *adev = to_acpi_device(dev);
310 int state;
311 int ret;
312
313 ret = acpi_device_get_power(adev, &state);
314 if (ret)
315 return ret;
316
317 return sprintf(buf, "%s\n", acpi_power_state_string(state));
318}
319
320static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
321
322static ssize_t power_state_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
324{
325 struct acpi_device *adev = to_acpi_device(dev);
326
327 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
328}
329
330static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800333acpi_eject_store(struct device *d, struct device_attribute *attr,
334 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800336 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600337 struct acpi_eject_event *ej_event;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100338 acpi_object_type not_used;
339 acpi_status status;
340 u32 ost_source;
341 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100343 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100346 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
347 && !acpi_device->driver)
348 return -ENODEV;
349
350 status = acpi_get_type(acpi_device->handle, &not_used);
351 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
352 return -ENODEV;
353
354 mutex_lock(&acpi_scan_lock);
355
356 if (acpi_device->flags.eject_pending) {
357 /* ACPI eject notification event. */
358 ost_source = ACPI_NOTIFY_EJECT_REQUEST;
359 acpi_device->flags.eject_pending = 0;
360 } else {
361 /* Eject initiated by user space. */
362 ost_source = ACPI_OST_EC_OSPM_EJECT;
363 }
Toshi Kanic4753e52012-05-23 20:25:20 -0600364 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
365 if (!ej_event) {
366 ret = -ENOMEM;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100367 goto err_out;
Toshi Kanic4753e52012-05-23 20:25:20 -0600368 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100369 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
370 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Yinghai Lu5993c462013-01-11 22:40:41 +0000371 ej_event->device = acpi_device;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100372 ej_event->event = ost_source;
373 get_device(&acpi_device->dev);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100374 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
375 if (ACPI_FAILURE(status)) {
376 put_device(&acpi_device->dev);
377 kfree(ej_event);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100378 ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
379 goto err_out;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100380 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100381 ret = count;
382
383 out:
384 mutex_unlock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return ret;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100386
387 err_out:
388 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
389 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
390 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800393static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800395static ssize_t
396acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
397 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600399 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800400}
401static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
402
Lv Zheng923d4a42012-10-30 14:43:26 +0100403static ssize_t acpi_device_uid_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
405{
406 struct acpi_device *acpi_dev = to_acpi_device(dev);
407
408 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
409}
410static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
411
412static ssize_t acpi_device_adr_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
414{
415 struct acpi_device *acpi_dev = to_acpi_device(dev);
416
417 return sprintf(buf, "0x%08x\n",
418 (unsigned int)(acpi_dev->pnp.bus_address));
419}
420static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
421
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800422static ssize_t
423acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
424 struct acpi_device *acpi_dev = to_acpi_device(dev);
425 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
426 int result;
427
428 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600429 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800430 goto end;
431
432 result = sprintf(buf, "%s\n", (char*)path.pointer);
433 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600434end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800435 return result;
436}
437static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
438
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600439/* sysfs file that shows description text from the ACPI _STR method */
440static ssize_t description_show(struct device *dev,
441 struct device_attribute *attr,
442 char *buf) {
443 struct acpi_device *acpi_dev = to_acpi_device(dev);
444 int result;
445
446 if (acpi_dev->pnp.str_obj == NULL)
447 return 0;
448
449 /*
450 * The _STR object contains a Unicode identifier for a device.
451 * We need to convert to utf-8 so it can be displayed.
452 */
453 result = utf16s_to_utf8s(
454 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
455 acpi_dev->pnp.str_obj->buffer.length,
456 UTF16_LITTLE_ENDIAN, buf,
457 PAGE_SIZE);
458
459 buf[result++] = '\n';
460
461 return result;
462}
463static DEVICE_ATTR(description, 0444, description_show, NULL);
464
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100465static ssize_t
466acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
467 char *buf) {
468 struct acpi_device *acpi_dev = to_acpi_device(dev);
469
470 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
471}
472static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
473
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800474static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600476 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800477 acpi_status status;
478 acpi_handle temp;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100479 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800480 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800482 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800483 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800484 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600485 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800486 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600487 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800488 goto end;
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200491 if (!list_empty(&dev->pnp.ids)) {
492 result = device_create_file(&dev->dev, &dev_attr_hid);
493 if (result)
494 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200496 result = device_create_file(&dev->dev, &dev_attr_modalias);
497 if (result)
498 goto end;
499 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200500
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600501 /*
502 * If device has _STR, 'description' file is created
503 */
504 status = acpi_get_handle(dev->handle, "_STR", &temp);
505 if (ACPI_SUCCESS(status)) {
506 status = acpi_evaluate_object(dev->handle, "_STR",
507 NULL, &buffer);
508 if (ACPI_FAILURE(status))
509 buffer.pointer = NULL;
510 dev->pnp.str_obj = buffer.pointer;
511 result = device_create_file(&dev->dev, &dev_attr_description);
512 if (result)
513 goto end;
514 }
515
Lv Zheng923d4a42012-10-30 14:43:26 +0100516 if (dev->flags.bus_address)
517 result = device_create_file(&dev->dev, &dev_attr_adr);
518 if (dev->pnp.unique_id)
519 result = device_create_file(&dev->dev, &dev_attr_uid);
520
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100521 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
522 if (ACPI_SUCCESS(status)) {
523 dev->pnp.sun = (unsigned long)sun;
524 result = device_create_file(&dev->dev, &dev_attr_sun);
525 if (result)
526 goto end;
527 } else {
528 dev->pnp.sun = (unsigned long)-1;
529 }
530
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800531 /*
532 * If device has _EJ0, 'eject' file is created that is used to trigger
533 * hot-removal function from userland.
534 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800535 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100536 if (ACPI_SUCCESS(status)) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800537 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100538 if (result)
539 return result;
540 }
541
542 if (dev->flags.power_manageable) {
543 result = device_create_file(&dev->dev, &dev_attr_power_state);
544 if (result)
545 return result;
546
547 if (dev->power.flags.power_resources)
548 result = device_create_file(&dev->dev,
549 &dev_attr_real_power_state);
550 }
551
Alex Chiang0c526d92009-05-14 08:31:37 -0600552end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800553 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800554}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800556static void acpi_device_remove_files(struct acpi_device *dev)
557{
558 acpi_status status;
559 acpi_handle temp;
560
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100561 if (dev->flags.power_manageable) {
562 device_remove_file(&dev->dev, &dev_attr_power_state);
563 if (dev->power.flags.power_resources)
564 device_remove_file(&dev->dev,
565 &dev_attr_real_power_state);
566 }
567
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800568 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600569 * If device has _STR, remove 'description' file
570 */
571 status = acpi_get_handle(dev->handle, "_STR", &temp);
572 if (ACPI_SUCCESS(status)) {
573 kfree(dev->pnp.str_obj);
574 device_remove_file(&dev->dev, &dev_attr_description);
575 }
576 /*
577 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800578 */
579 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
580 if (ACPI_SUCCESS(status))
581 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800582
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100583 status = acpi_get_handle(dev->handle, "_SUN", &temp);
584 if (ACPI_SUCCESS(status))
585 device_remove_file(&dev->dev, &dev_attr_sun);
586
Lv Zheng923d4a42012-10-30 14:43:26 +0100587 if (dev->pnp.unique_id)
588 device_remove_file(&dev->dev, &dev_attr_uid);
589 if (dev->flags.bus_address)
590 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600591 device_remove_file(&dev->dev, &dev_attr_modalias);
592 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600593 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800594 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800595}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800597 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200599
Mika Westerbergcf761af2012-10-31 22:44:41 +0100600static const struct acpi_device_id *__acpi_match_device(
601 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200602{
603 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600604 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200605
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800606 /*
607 * If the device is not present, it is unnecessary to load device
608 * driver for it.
609 */
610 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100611 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800612
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600613 for (id = ids; id->id[0]; id++)
614 list_for_each_entry(hwid, &device->pnp.ids, list)
615 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100616 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200617
Mika Westerbergcf761af2012-10-31 22:44:41 +0100618 return NULL;
619}
620
621/**
622 * acpi_match_device - Match a struct device against a given list of ACPI IDs
623 * @ids: Array of struct acpi_device_id object to match against.
624 * @dev: The device structure to match.
625 *
626 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
627 * object for that handle and use that object to match against a given list of
628 * device IDs.
629 *
630 * Return a pointer to the first matching ID on success or %NULL on failure.
631 */
632const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
633 const struct device *dev)
634{
635 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100636 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100637
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100638 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100639 return NULL;
640
641 return __acpi_match_device(adev, ids);
642}
643EXPORT_SYMBOL_GPL(acpi_match_device);
644
645int acpi_match_device_ids(struct acpi_device *device,
646 const struct acpi_device_id *ids)
647{
648 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200649}
650EXPORT_SYMBOL(acpi_match_device_ids);
651
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100652void acpi_free_ids(struct acpi_device *device)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600653{
654 struct acpi_hardware_id *id, *tmp;
655
656 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
657 kfree(id->id);
658 kfree(id);
659 }
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100660 kfree(device->pnp.unique_id);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600661}
662
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100663static void acpi_free_power_resources_lists(struct acpi_device *device)
664{
665 int i;
666
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100667 if (device->wakeup.flags.valid)
668 acpi_power_resources_list_free(&device->wakeup.resources);
669
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100670 if (!device->flags.power_manageable)
671 return;
672
673 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
674 struct acpi_device_power_state *ps = &device->power.states[i];
675 acpi_power_resources_list_free(&ps->resources);
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Patrick Mochel1890a972006-12-07 20:56:31 +0800679static void acpi_device_release(struct device *dev)
680{
681 struct acpi_device *acpi_dev = to_acpi_device(dev);
682
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600683 acpi_free_ids(acpi_dev);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100684 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800685 kfree(acpi_dev);
686}
687
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800688static int acpi_bus_match(struct device *dev, struct device_driver *drv)
689{
690 struct acpi_device *acpi_dev = to_acpi_device(dev);
691 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
692
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100693 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100694 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800695}
696
Kay Sievers7eff2e72007-08-14 15:15:12 +0200697static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800698{
699 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200700 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800701
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200702 if (list_empty(&acpi_dev->pnp.ids))
703 return 0;
704
Kay Sievers7eff2e72007-08-14 15:15:12 +0200705 if (add_uevent_var(env, "MODALIAS="))
706 return -ENOMEM;
707 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
708 sizeof(env->buf) - env->buflen);
709 if (len >= (sizeof(env->buf) - env->buflen))
710 return -ENOMEM;
711 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 0;
713}
714
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000715static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
716{
717 struct acpi_device *device = data;
718
719 device->driver->ops.notify(device, event);
720}
721
722static acpi_status acpi_device_notify_fixed(void *data)
723{
724 struct acpi_device *device = data;
725
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000726 /* Fixed hardware devices have no handles */
727 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000728 return AE_OK;
729}
730
731static int acpi_device_install_notify_handler(struct acpi_device *device)
732{
733 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000734
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000735 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000736 status =
737 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
738 acpi_device_notify_fixed,
739 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000740 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000741 status =
742 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
743 acpi_device_notify_fixed,
744 device);
745 else
746 status = acpi_install_notify_handler(device->handle,
747 ACPI_DEVICE_NOTIFY,
748 acpi_device_notify,
749 device);
750
751 if (ACPI_FAILURE(status))
752 return -EINVAL;
753 return 0;
754}
755
756static void acpi_device_remove_notify_handler(struct acpi_device *device)
757{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000758 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000759 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
760 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000761 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000762 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
763 acpi_device_notify_fixed);
764 else
765 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
766 acpi_device_notify);
767}
768
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800769static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800770static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800771{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800772 struct acpi_device *acpi_dev = to_acpi_device(dev);
773 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
774 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800776 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
777 if (!ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000778 if (acpi_drv->ops.notify) {
779 ret = acpi_device_install_notify_handler(acpi_dev);
780 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000781 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100782 acpi_drv->ops.remove(acpi_dev);
Toshi Kani5f27ee82013-02-07 21:19:13 +0000783 acpi_dev->driver = NULL;
784 acpi_dev->driver_data = NULL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000785 return ret;
786 }
787 }
788
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800789 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
790 "Found driver [%s] for device [%s]\n",
791 acpi_drv->name, acpi_dev->pnp.bus_id));
792 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800793 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800794 return ret;
795}
796
797static int acpi_device_remove(struct device * dev)
798{
799 struct acpi_device *acpi_dev = to_acpi_device(dev);
800 struct acpi_driver *acpi_drv = acpi_dev->driver;
801
802 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000803 if (acpi_drv->ops.notify)
804 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800805 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100806 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800807 }
808 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700809 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800810
811 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800812 return 0;
813}
814
David Brownell55955aa2007-05-08 00:28:35 -0700815struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800816 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800817 .match = acpi_bus_match,
818 .probe = acpi_device_probe,
819 .remove = acpi_device_remove,
820 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821};
822
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100823int acpi_device_add(struct acpi_device *device,
824 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800826 int result;
827 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
828 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000829
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100830 if (device->handle) {
831 acpi_status status;
832
833 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
834 device);
835 if (ACPI_FAILURE(status)) {
836 acpi_handle_err(device->handle,
837 "Unable to attach device data\n");
838 return -ENODEV;
839 }
840 }
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /*
843 * Linkage
844 * -------
845 * Link this device to its parent and siblings.
846 */
847 INIT_LIST_HEAD(&device->children);
848 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800850 INIT_LIST_HEAD(&device->physical_node_list);
851 mutex_init(&device->physical_node_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100852 INIT_LIST_HEAD(&device->power_dependent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800854 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
855 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100856 pr_err(PREFIX "Memory allocation error\n");
857 result = -ENOMEM;
858 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800859 }
860
Shaohua Li90905892009-04-07 10:24:29 +0800861 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800862 /*
863 * Find suitable bus_id and instance number in acpi_bus_id_list
864 * If failed, create one and link it into acpi_bus_id_list
865 */
866 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600867 if (!strcmp(acpi_device_bus_id->bus_id,
868 acpi_device_hid(device))) {
869 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800870 found = 1;
871 kfree(new_bus_id);
872 break;
873 }
874 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600875 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800876 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600877 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800878 acpi_device_bus_id->instance_no = 0;
879 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
880 }
Kay Sievers07944692008-10-30 01:18:59 +0100881 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 +0800882
Len Brown33b57152008-12-15 22:09:26 -0500883 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (device->wakeup.flags.valid)
887 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800888 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Patrick Mochel1890a972006-12-07 20:56:31 +0800890 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000891 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800892 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100893 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100894 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600895 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600896 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100897 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800898 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800899
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800900 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600901 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100902 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
903 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800904
Li Shaohua96333572006-12-07 20:56:46 +0800905 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800906 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100907
908 err:
Shaohua Li90905892009-04-07 10:24:29 +0800909 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500910 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800911 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800912 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800913 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100914
915 err_detach:
916 acpi_detach_data(device->handle, acpi_bus_data_handler);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800917 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Rafael J. Wysockib17b5372013-01-15 13:23:44 +0100920static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Shaohua Li90905892009-04-07 10:24:29 +0800922 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500923 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800927 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800930
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100931 acpi_power_add_remove_device(device, false);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800932 acpi_device_remove_files(device);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100933 if (device->remove)
934 device->remove(device);
935
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100936 device_del(&device->dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100937 /*
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +0100938 * Transition the device to D3cold to drop the reference counts of all
939 * power resources the device depends on and turn off the ones that have
940 * no more references.
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100941 */
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +0100942 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100943 device->handle = NULL;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100944 put_device(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Zhang Rui9e89dde2006-12-07 20:56:16 +0800947/* --------------------------------------------------------------------------
948 Driver Management
949 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500951 * acpi_bus_driver_init - add a device to a driver
952 * @device: the device to add and initialize
953 * @driver: driver for the device
954 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600955 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800956 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
958static int
Len Brown4be44fc2005-08-05 00:44:28 -0400959acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Len Brown4be44fc2005-08-05 00:44:28 -0400961 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400964 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400967 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 result = driver->ops.add(device);
970 if (result) {
971 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700972 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400973 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975
976 device->driver = driver;
977
978 /*
979 * TBD - Configuration Management: Assign resources to device based
980 * upon possible configuration and currently allocated resources.
981 */
982
Len Brown4be44fc2005-08-05 00:44:28 -0400983 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
984 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400985 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500989 * acpi_bus_register_driver - register a driver with the ACPI bus
990 * @driver: driver being registered
991 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500993 * devices that match the driver's criteria and binds. Returns zero for
994 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
Len Brown4be44fc2005-08-05 00:44:28 -0400996int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Patrick Mochel1890a972006-12-07 20:56:31 +0800998 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001001 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001002 driver->drv.name = driver->name;
1003 driver->drv.bus = &acpi_bus_type;
1004 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Patrick Mochel1890a972006-12-07 20:56:31 +08001006 ret = driver_register(&driver->drv);
1007 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Len Brown4be44fc2005-08-05 00:44:28 -04001010EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001013 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1014 * @driver: driver to unregister
1015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1017 * devices that match the driver's criteria and unbinds.
1018 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001019void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Patrick Mochel1890a972006-12-07 20:56:31 +08001021 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
Len Brown4be44fc2005-08-05 00:44:28 -04001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024EXPORT_SYMBOL(acpi_bus_unregister_driver);
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/* --------------------------------------------------------------------------
1027 Device Enumeration
1028 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001029static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1030{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001031 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001032 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001033
1034 /*
1035 * Fixed hardware devices do not appear in the namespace and do not
1036 * have handles, but we fabricate acpi_devices for them, so we have
1037 * to deal with them specially.
1038 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001039 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001040 return acpi_root;
1041
1042 do {
1043 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001044 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001045 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1046 } while (acpi_bus_get_device(handle, &device));
1047 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001048}
1049
Len Brownc8f7a622006-07-09 17:22:28 -04001050acpi_status
1051acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1052{
1053 acpi_status status;
1054 acpi_handle tmp;
1055 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1056 union acpi_object *obj;
1057
1058 status = acpi_get_handle(handle, "_EJD", &tmp);
1059 if (ACPI_FAILURE(status))
1060 return status;
1061
1062 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1063 if (ACPI_SUCCESS(status)) {
1064 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001065 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1066 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001067 kfree(buffer.pointer);
1068 }
1069 return status;
1070}
1071EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1072
Bob Moore8e4319c2009-06-29 13:43:27 +08001073void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075
1076 /* TBD */
1077
1078 return;
1079}
1080
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001081static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1082 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001084 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1085 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001087 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001088 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001090 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001091 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001093 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001095 /* _PRW */
1096 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1097 if (ACPI_FAILURE(status)) {
1098 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001099 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001100 }
1101
1102 package = (union acpi_object *)buffer.pointer;
1103
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001104 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001105 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001108 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001109 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (element->type == ACPI_TYPE_PACKAGE) {
1112 if ((element->package.count < 2) ||
1113 (element->package.elements[0].type !=
1114 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001115 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001116 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001117
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001118 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001120 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 (u32) element->package.elements[1].integer.value;
1122 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001123 wakeup->gpe_device = NULL;
1124 wakeup->gpe_number = element->integer.value;
1125 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001126 goto out;
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001130 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001131 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001132
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001133 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001135 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1136 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001139 if (!list_empty(&wakeup->resources)) {
1140 int sleep_state;
1141
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001142 err = acpi_power_wakeup_list_init(&wakeup->resources,
1143 &sleep_state);
1144 if (err) {
1145 acpi_handle_warn(handle, "Retrieving current states "
1146 "of wakeup power resources failed\n");
1147 acpi_power_resources_list_free(&wakeup->resources);
1148 goto out;
1149 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001150 if (sleep_state < wakeup->sleep_state) {
1151 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1152 "(S%d) by S%d from power resources\n",
1153 (int)wakeup->sleep_state, sleep_state);
1154 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Lin Mingbba63a22010-12-13 13:39:17 +08001157 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001158
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001159 out:
1160 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001161 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001164static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001166 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001167 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001168 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001169 {"PNP0C0E", 0},
1170 {"", 0},
1171 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001172 acpi_status status;
1173 acpi_event_status event_status;
1174
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001175 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001176
1177 /* Power button, Lid switch always enable wakeup */
1178 if (!acpi_match_device_ids(device, button_device_ids)) {
1179 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001180 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1181 /* Do not use Lid/sleep button for S5 wakeup */
1182 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1183 device->wakeup.sleep_state = ACPI_STATE_S4;
1184 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001185 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001186 return;
1187 }
1188
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001189 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1190 device->wakeup.gpe_number,
1191 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001192 if (status == AE_OK)
1193 device->wakeup.flags.run_wake =
1194 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1195}
1196
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001197static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001198{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001199 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001200 acpi_status status = 0;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001201 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001202
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001203 /* Presence of _PRW indicates wake capable */
1204 status = acpi_get_handle(device->handle, "_PRW", &temp);
1205 if (ACPI_FAILURE(status))
1206 return;
1207
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001208 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1209 &device->wakeup);
1210 if (err) {
1211 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001212 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001216 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001217 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001218 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1219 * system for the ACPI device with the _PRW object.
1220 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1221 * So it is necessary to call _DSW object first. Only when it is not
1222 * present will the _PSW object used.
1223 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001224 err = acpi_device_sleep_wake(device, 0, 0, 0);
1225 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001226 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1227 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001230static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001232 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001233 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1234 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001235 acpi_handle handle;
1236 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001237
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001238 INIT_LIST_HEAD(&ps->resources);
1239
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001240 /* Evaluate "_PRx" to get referenced power resources */
1241 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1242 if (ACPI_SUCCESS(status)) {
1243 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001244
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001245 if (buffer.length && package
1246 && package->type == ACPI_TYPE_PACKAGE
1247 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001248 int err = acpi_extract_power_resources(package, 0,
1249 &ps->resources);
1250 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001251 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001252 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001253 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001254 }
1255
1256 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001257 pathname[2] = 'S';
1258 status = acpi_get_handle(device->handle, pathname, &handle);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001259 if (ACPI_SUCCESS(status))
1260 ps->flags.explicit_set = 1;
1261
1262 /*
1263 * State is valid if there are means to put the device into it.
1264 * D3hot is only valid if _PR3 present.
1265 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001266 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001267 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1268 ps->flags.valid = 1;
1269 ps->flags.os_accessible = 1;
1270 }
1271
1272 ps->power = -1; /* Unknown - driver assigned */
1273 ps->latency = -1; /* Unknown - driver assigned */
1274}
1275
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001276static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001278 acpi_status status;
1279 acpi_handle handle;
1280 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001282 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1283 status = acpi_get_handle(device->handle, "_PS0", &handle);
1284 if (ACPI_FAILURE(status)) {
1285 status = acpi_get_handle(device->handle, "_PR0", &handle);
1286 if (ACPI_FAILURE(status))
1287 return;
1288 }
1289
1290 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001293 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 */
Zhang Rui9e89dde2006-12-07 20:56:16 +08001295 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001297 device->power.flags.explicit_get = 1;
1298 status = acpi_get_handle(device->handle, "_IRC", &handle);
1299 if (ACPI_SUCCESS(status))
1300 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001303 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001305 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1306 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001308 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Zhang Rui9e89dde2006-12-07 20:56:16 +08001310 /* Set defaults for D0 and D3 states (always valid) */
1311 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1312 device->power.states[ACPI_STATE_D0].power = 100;
1313 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1314 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001316 /* Set D3cold's explicit_set flag if _PS3 exists. */
1317 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1318 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1319
Aaron Lu1399dfc2012-11-21 23:33:40 +01001320 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1321 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1322 device->power.flags.power_resources)
1323 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1324
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001325 if (acpi_bus_init_power(device)) {
1326 acpi_free_power_resources_lists(device);
1327 device->flags.power_manageable = 0;
1328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001331static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Len Brown4be44fc2005-08-05 00:44:28 -04001333 acpi_status status = AE_OK;
1334 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 /* Presence of _STA indicates 'dynamic_status' */
1337 status = acpi_get_handle(device->handle, "_STA", &temp);
1338 if (ACPI_SUCCESS(status))
1339 device->flags.dynamic_status = 1;
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 /* Presence of _RMV indicates 'removable' */
1342 status = acpi_get_handle(device->handle, "_RMV", &temp);
1343 if (ACPI_SUCCESS(status))
1344 device->flags.removable = 1;
1345
1346 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1347 status = acpi_get_handle(device->handle, "_EJD", &temp);
1348 if (ACPI_SUCCESS(status))
1349 device->flags.ejectable = 1;
1350 else {
1351 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1352 if (ACPI_SUCCESS(status))
1353 device->flags.ejectable = 1;
1354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001357static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
Len Brown4be44fc2005-08-05 00:44:28 -04001359 char bus_id[5] = { '?', 0 };
1360 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1361 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /*
1364 * Bus ID
1365 * ------
1366 * The device's Bus ID is simply the object name.
1367 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1368 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001369 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001371 return;
1372 }
1373
1374 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 case ACPI_BUS_TYPE_POWER_BUTTON:
1376 strcpy(device->pnp.bus_id, "PWRF");
1377 break;
1378 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1379 strcpy(device->pnp.bus_id, "SLPF");
1380 break;
1381 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001382 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /* Clean up trailing underscores (if any) */
1384 for (i = 3; i > 1; i--) {
1385 if (bus_id[i] == '_')
1386 bus_id[i] = '\0';
1387 else
1388 break;
1389 }
1390 strcpy(device->pnp.bus_id, bus_id);
1391 break;
1392 }
1393}
1394
Zhang Rui54735262007-01-11 02:09:09 -05001395/*
1396 * acpi_bay_match - see if a device is an ejectable driver bay
1397 *
1398 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1399 * then we can safely call it an ejectable drive bay
1400 */
1401static int acpi_bay_match(struct acpi_device *device){
1402 acpi_status status;
1403 acpi_handle handle;
1404 acpi_handle tmp;
1405 acpi_handle phandle;
1406
1407 handle = device->handle;
1408
1409 status = acpi_get_handle(handle, "_EJ0", &tmp);
1410 if (ACPI_FAILURE(status))
1411 return -ENODEV;
1412
1413 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1414 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1415 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1416 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1417 return 0;
1418
1419 if (acpi_get_parent(handle, &phandle))
1420 return -ENODEV;
1421
1422 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1423 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1424 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1425 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1426 return 0;
1427
1428 return -ENODEV;
1429}
1430
Frank Seidel3620f2f2007-12-07 13:20:34 +01001431/*
1432 * acpi_dock_match - see if a device has a _DCK method
1433 */
1434static int acpi_dock_match(struct acpi_device *device)
1435{
1436 acpi_handle tmp;
1437 return acpi_get_handle(device->handle, "_DCK", &tmp);
1438}
1439
Thomas Renninger620e1122010-10-01 10:54:00 +02001440const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001441{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001442 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001443
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001444 if (list_empty(&device->pnp.ids))
1445 return dummy_hid;
1446
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001447 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1448 return hid->id;
1449}
1450EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001451
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001452static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1453{
1454 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001455
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001456 id = kmalloc(sizeof(*id), GFP_KERNEL);
1457 if (!id)
1458 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001459
Thomas Meyer581de592011-08-06 11:32:56 +02001460 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001461 if (!id->id) {
1462 kfree(id);
1463 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001464 }
1465
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001466 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001467}
1468
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001469/*
1470 * Old IBM workstations have a DSDT bug wherein the SMBus object
1471 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1472 * prefix. Work around this.
1473 */
1474static int acpi_ibm_smbus_match(struct acpi_device *device)
1475{
1476 acpi_handle h_dummy;
1477 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1478 int result;
1479
1480 if (!dmi_name_in_vendors("IBM"))
1481 return -ENODEV;
1482
1483 /* Look for SMBS object */
1484 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1485 if (result)
1486 return result;
1487
1488 if (strcmp("SMBS", path.pointer)) {
1489 result = -ENODEV;
1490 goto out;
1491 }
1492
1493 /* Does it have the necessary (but misnamed) methods? */
1494 result = -ENODEV;
1495 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1496 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1497 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1498 result = 0;
1499out:
1500 kfree(path.pointer);
1501 return result;
1502}
1503
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001504static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Len Brown4be44fc2005-08-05 00:44:28 -04001506 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001507 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001508 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001509 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001511 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001513 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001514 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001515 break;
1516 }
1517
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001518 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001520 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return;
1522 }
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001525 acpi_add_id(device, info->hardware_id.string);
1526 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001527 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001528 for (i = 0; i < cid_list->count; i++)
1529 acpi_add_id(device, cid_list->ids[i].string);
1530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (info->valid & ACPI_VALID_ADR) {
1532 device->pnp.bus_address = info->address;
1533 device->flags.bus_address = 1;
1534 }
Lv Zhengccf78042012-10-30 14:41:07 +01001535 if (info->valid & ACPI_VALID_UID)
1536 device->pnp.unique_id = kstrdup(info->unique_id.string,
1537 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001538
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001539 kfree(info);
1540
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001541 /*
1542 * Some devices don't reliably have _HIDs & _CIDs, so add
1543 * synthetic HIDs to make sure drivers can find them.
1544 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001545 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001546 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001547 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001548 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001549 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001550 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001551 else if (!acpi_ibm_smbus_match(device))
1552 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Rafael J. Wysocki4f5f64c2013-01-04 23:00:54 +01001553 else if (list_empty(&device->pnp.ids) &&
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001554 ACPI_IS_ROOT_DEVICE(device->parent)) {
1555 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1556 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1557 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1558 }
Zhang Rui54735262007-01-11 02:09:09 -05001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 break;
1561 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001562 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 break;
1564 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001565 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001568 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 break;
1570 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001571 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 break;
1573 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001574 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 break;
1576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001579void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1580 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001582 INIT_LIST_HEAD(&device->pnp.ids);
1583 device->device_type = type;
1584 device->handle = handle;
1585 device->parent = acpi_bus_get_parent(handle);
1586 STRUCT_TO_INT(device->status) = sta;
1587 acpi_device_get_busid(device);
1588 acpi_device_set_id(device);
1589 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001590 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001591 device_initialize(&device->dev);
1592 dev_set_uevent_suppress(&device->dev, true);
1593}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001594
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001595void acpi_device_add_finalize(struct acpi_device *device)
1596{
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001597 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001598 dev_set_uevent_suppress(&device->dev, false);
1599 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001602static int acpi_add_single_object(struct acpi_device **child,
1603 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001604 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001606 int result;
1607 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001608 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Burman Yan36bcbec2006-12-19 12:56:11 -08001610 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001612 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001613 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001616 acpi_init_device_object(device, handle, type, sta);
1617 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001618 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001620 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001621 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001622 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001623 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001626 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001627 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001628 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1629 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1630 dev_name(&device->dev), (char *) buffer.pointer,
1631 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1632 kfree(buffer.pointer);
1633 *child = device;
1634 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001635}
1636
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001637static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1638 unsigned long long *sta)
1639{
1640 acpi_status status;
1641 acpi_object_type acpi_type;
1642
1643 status = acpi_get_type(handle, &acpi_type);
1644 if (ACPI_FAILURE(status))
1645 return -ENODEV;
1646
1647 switch (acpi_type) {
1648 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1649 case ACPI_TYPE_DEVICE:
1650 *type = ACPI_BUS_TYPE_DEVICE;
1651 status = acpi_bus_get_status_handle(handle, sta);
1652 if (ACPI_FAILURE(status))
1653 return -ENODEV;
1654 break;
1655 case ACPI_TYPE_PROCESSOR:
1656 *type = ACPI_BUS_TYPE_PROCESSOR;
1657 status = acpi_bus_get_status_handle(handle, sta);
1658 if (ACPI_FAILURE(status))
1659 return -ENODEV;
1660 break;
1661 case ACPI_TYPE_THERMAL:
1662 *type = ACPI_BUS_TYPE_THERMAL;
1663 *sta = ACPI_STA_DEFAULT;
1664 break;
1665 case ACPI_TYPE_POWER:
1666 *type = ACPI_BUS_TYPE_POWER;
1667 *sta = ACPI_STA_DEFAULT;
1668 break;
1669 default:
1670 return -ENODEV;
1671 }
1672
1673 return 0;
1674}
1675
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001676static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1677 char *idstr,
1678 const struct acpi_device_id **matchid)
1679{
1680 const struct acpi_device_id *devid;
1681
1682 for (devid = handler->ids; devid->id[0]; devid++)
1683 if (!strcmp((char *)devid->id, idstr)) {
1684 if (matchid)
1685 *matchid = devid;
1686
1687 return true;
1688 }
1689
1690 return false;
1691}
1692
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001693static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1694 const struct acpi_device_id **matchid)
1695{
1696 struct acpi_scan_handler *handler;
1697
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001698 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1699 if (acpi_scan_handler_matching(handler, idstr, matchid))
1700 return handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001701
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001702 return NULL;
1703}
1704
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001705static void acpi_scan_init_hotplug(acpi_handle handle)
1706{
1707 struct acpi_device_info *info;
1708 struct acpi_scan_handler *handler;
1709
1710 if (ACPI_FAILURE(acpi_get_object_info(handle, &info)))
1711 return;
1712
1713 if (!(info->valid & ACPI_VALID_HID)) {
1714 kfree(info);
1715 return;
1716 }
1717
1718 /*
1719 * This relies on the fact that acpi_install_notify_handler() will not
1720 * install the same notify handler routine twice for the same handle.
1721 */
1722 handler = acpi_scan_match_handler(info->hardware_id.string, NULL);
1723 kfree(info);
1724 if (handler && handler->hotplug.enabled)
1725 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1726 acpi_hotplug_notify_cb, NULL);
1727}
1728
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001729static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1730 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001732 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001733 int type;
1734 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001735 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001736 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001737
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001738 acpi_bus_get_device(handle, &device);
1739 if (device)
1740 goto out;
1741
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001742 result = acpi_bus_type_and_status(handle, &type, &sta);
1743 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001744 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001746 if (type == ACPI_BUS_TYPE_POWER) {
1747 acpi_add_power_resource(handle);
1748 return AE_OK;
1749 }
1750
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001751 acpi_scan_init_hotplug(handle);
1752
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001753 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001754 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001755 struct acpi_device_wakeup wakeup;
1756 acpi_handle temp;
1757
1758 status = acpi_get_handle(handle, "_PRW", &temp);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001759 if (ACPI_SUCCESS(status)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001760 acpi_bus_extract_wakeup_device_power_package(handle,
1761 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001762 acpi_power_resources_list_free(&wakeup.resources);
1763 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001764 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001767 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001768 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001769 return AE_CTRL_DEPTH;
1770
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001771 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001772 if (!*return_value)
1773 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001774
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001775 return AE_OK;
1776}
1777
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001778static int acpi_scan_attach_handler(struct acpi_device *device)
1779{
1780 struct acpi_hardware_id *hwid;
1781 int ret = 0;
1782
1783 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001784 const struct acpi_device_id *devid;
1785 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001786
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001787 handler = acpi_scan_match_handler(hwid->id, &devid);
1788 if (handler) {
1789 ret = handler->attach(device, devid);
1790 if (ret > 0) {
1791 device->handler = handler;
1792 break;
1793 } else if (ret < 0) {
1794 break;
1795 }
1796 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001797 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001798 return ret;
1799}
1800
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001801static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1802 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001803{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001804 struct acpi_device *device;
1805 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001806 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001807
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001808 /*
1809 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1810 * namespace walks prematurely.
1811 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001812 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001813 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001814
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001815 if (acpi_bus_get_device(handle, &device))
1816 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001817
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001818 ret = acpi_scan_attach_handler(device);
1819 if (ret)
1820 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1821
1822 ret = device_attach(&device->dev);
1823 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001826/**
1827 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1828 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001829 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001830 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1831 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001832 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001833 * If no devices were found, -ENODEV is returned, but it does not mean that
1834 * there has been a real error. There just have been no suitable ACPI objects
1835 * in the table trunk from which the kernel could create a device and add an
1836 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001837 *
1838 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001839 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001840int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001841{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001843 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001844
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001845 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001847 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001848
Thomas Renningerd2f66502010-01-29 17:48:51 +01001849 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001850 error = -ENODEV;
1851 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001852 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001853 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01001854
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001855 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001856}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001857EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001859static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1860 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001862 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001864 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001865 struct acpi_scan_handler *dev_handler = device->handler;
1866
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001867 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001868 if (dev_handler) {
1869 if (dev_handler->detach)
1870 dev_handler->detach(device);
1871
1872 device->handler = NULL;
1873 } else {
1874 device_release_driver(&device->dev);
1875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001877 return AE_OK;
1878}
1879
1880static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1881 void *not_used, void **ret_not_used)
1882{
1883 struct acpi_device *device = NULL;
1884
1885 if (!acpi_bus_get_device(handle, &device))
1886 acpi_device_unregister(device);
1887
1888 return AE_OK;
1889}
1890
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001891/**
1892 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1893 * @start: Root of the ACPI device nodes subtree to remove.
1894 *
1895 * Must be called under acpi_scan_lock.
1896 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01001897void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001899 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001900 * Execute acpi_bus_device_detach() as a post-order callback to detach
1901 * all ACPI drivers from the device nodes being removed.
1902 */
1903 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1904 acpi_bus_device_detach, NULL, NULL);
1905 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1906 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001907 * Execute acpi_bus_remove() as a post-order callback to remove device
1908 * nodes in the given namespace scope.
1909 */
1910 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1911 acpi_bus_remove, NULL, NULL);
1912 acpi_bus_remove(start->handle, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
Kristen Accardiceaba662006-02-23 17:56:01 -08001914EXPORT_SYMBOL_GPL(acpi_bus_trim);
1915
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001916static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Len Brown4be44fc2005-08-05 00:44:28 -04001918 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 /*
1921 * Enumerate all fixed-feature devices.
1922 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001923 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1924 struct acpi_device *device = NULL;
1925
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001926 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001927 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001928 ACPI_STA_DEFAULT);
1929 if (result)
1930 return result;
1931
1932 result = device_attach(&device->dev);
1933 if (result < 0)
1934 return result;
1935
Daniel Drakec10d7a12012-05-10 00:08:43 +01001936 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001939 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1940 struct acpi_device *device = NULL;
1941
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001942 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001943 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001944 ACPI_STA_DEFAULT);
1945 if (result)
1946 return result;
1947
1948 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001951 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
Bjorn Helgaase747f272009-03-24 16:49:43 -06001954int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Patrick Mochel5b327262006-05-10 10:33:00 -04001958 result = bus_register(&acpi_bus_type);
1959 if (result) {
1960 /* We don't want to quit even if we failed to add suspend/resume */
1961 printk(KERN_ERR PREFIX "Could not register bus type\n");
1962 }
1963
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01001964 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01001965 acpi_pci_link_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01001966 acpi_platform_init();
Mika Westerberg13176bb2013-01-17 09:59:33 +00001967 acpi_csrt_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01001968 acpi_container_init();
Jiang Liuab1a2e02013-01-19 00:07:42 +08001969 acpi_pci_slot_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001970
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001971 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 * Enumerate devices in the ACPI namespace.
1974 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001975 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001979 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001981 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001983 result = acpi_bus_scan_fixed();
1984 if (result) {
1985 acpi_device_unregister(acpi_root);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001986 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001987 }
1988
1989 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001990
Yinghai Lu668192b2013-01-21 13:20:48 -08001991 acpi_pci_root_hp_init();
1992
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001993 out:
1994 mutex_unlock(&acpi_scan_lock);
1995 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}