blob: 42982b522b3693b1ba6c5dce773451795e18cd5b [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 Helgaas859ac9a2009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020030/*
31 * If set, devices will be hot-removed even if they cannot be put offline
32 * gracefully (from the kernel's standpoint).
33 */
34bool acpi_force_hot_remove;
35
Thomas Renninger620e1122010-10-01 10:54:00 +020036static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020037
Zhang Ruie49bd2d2006-12-08 17:23:43 +080038static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010039static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010040static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080041DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042LIST_HEAD(acpi_wakeup_device_list);
43
Zhang Ruie49bd2d2006-12-08 17:23:43 +080044struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080045 char bus_id[15];
Zhang Ruie49bd2d2006-12-08 17:23:43 +080046 unsigned int instance_no;
47 struct list_head node;
48};
Thomas Renninger29b71a12007-07-23 14:43:51 +020049
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010050void acpi_scan_lock_acquire(void)
51{
52 mutex_lock(&acpi_scan_lock);
53}
54EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
55
56void acpi_scan_lock_release(void)
57{
58 mutex_unlock(&acpi_scan_lock);
59}
60EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
61
Rafael J. Wysockica589f92013-01-30 14:27:29 +010062int acpi_scan_add_handler(struct acpi_scan_handler *handler)
63{
64 if (!handler || !handler->attach)
65 return -EINVAL;
66
67 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
68 return 0;
69}
70
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010071int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
72 const char *hotplug_profile_name)
73{
74 int error;
75
76 error = acpi_scan_add_handler(handler);
77 if (error)
78 return error;
79
80 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
81 return 0;
82}
83
Thomas Renninger29b71a12007-07-23 14:43:51 +020084/*
85 * Creates hid/cid(s) string needed for modalias and uevent
86 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
87 * char *modalias: "acpi:IBM0001:ACPI0001"
88*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020089static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
90 int size)
91{
Thomas Renninger29b71a12007-07-23 14:43:51 +020092 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080093 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060094 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020095
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020096 if (list_empty(&acpi_dev->pnp.ids))
97 return 0;
98
Zhang Rui5c9fcb52008-03-20 16:40:32 +080099 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +0200100 size -= len;
101
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600102 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
103 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800104 if (count < 0 || count >= size)
105 return -EINVAL;
106 len += count;
107 size -= count;
108 }
109
Thomas Renninger29b71a12007-07-23 14:43:51 +0200110 modalias[len] = '\0';
111 return len;
112}
113
114static ssize_t
115acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
116 struct acpi_device *acpi_dev = to_acpi_device(dev);
117 int len;
118
119 /* Device has no HID and no CID or string is >1024 */
120 len = create_modalias(acpi_dev, buf, 1024);
121 if (len <= 0)
122 return 0;
123 buf[len++] = '\n';
124 return len;
125}
126static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
127
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200128static acpi_status acpi_bus_offline_companions(acpi_handle handle, u32 lvl,
129 void *data, void **ret_p)
130{
131 struct acpi_device *device = NULL;
132 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200133 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200134 acpi_status status = AE_OK;
135
136 if (acpi_bus_get_device(handle, &device))
137 return AE_OK;
138
139 mutex_lock(&device->physical_node_lock);
140
141 list_for_each_entry(pn, &device->physical_node_list, node) {
142 int ret;
143
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200144 if (second_pass) {
145 /* Skip devices offlined by the first pass. */
146 if (pn->put_online)
147 continue;
148 } else {
149 pn->put_online = false;
150 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200151 ret = device_offline(pn->dev);
152 if (acpi_force_hot_remove)
153 continue;
154
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200155 if (ret >= 0) {
156 pn->put_online = !ret;
157 } else {
158 *ret_p = pn->dev;
159 if (second_pass) {
160 status = AE_ERROR;
161 break;
162 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200163 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200164 }
165
166 mutex_unlock(&device->physical_node_lock);
167
168 return status;
169}
170
171static acpi_status acpi_bus_online_companions(acpi_handle handle, u32 lvl,
172 void *data, void **ret_p)
173{
174 struct acpi_device *device = NULL;
175 struct acpi_device_physical_node *pn;
176
177 if (acpi_bus_get_device(handle, &device))
178 return AE_OK;
179
180 mutex_lock(&device->physical_node_lock);
181
182 list_for_each_entry(pn, &device->physical_node_list, node)
183 if (pn->put_online) {
184 device_online(pn->dev);
185 pn->put_online = false;
186 }
187
188 mutex_unlock(&device->physical_node_lock);
189
190 return AE_OK;
191}
192
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100193static int acpi_scan_hot_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Yinghai Lu5993c462013-01-11 22:40:41 +0000195 acpi_handle handle = device->handle;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100196 acpi_handle not_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct acpi_object_list arg_list;
198 union acpi_object arg;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200199 struct device *errdev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100200 acpi_status status;
Toshi Kani882fd122013-03-13 19:29:26 +0000201 unsigned long long sta;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100202
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100203 /* If there is no handle, the device node has been unregistered. */
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100204 if (!handle) {
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100205 dev_dbg(&device->dev, "ACPI handle missing\n");
206 put_device(&device->dev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100207 return -EINVAL;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100208 }
209
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200210 /*
211 * Carry out two passes here and ignore errors in the first pass,
212 * because if the devices in question are memory blocks and
213 * CONFIG_MEMCG is set, one of the blocks may hold data structures
214 * that the other blocks depend on, but it is not known in advance which
215 * block holds them.
216 *
217 * If the first pass is successful, the second one isn't needed, though.
218 */
219 errdev = NULL;
220 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
221 NULL, acpi_bus_offline_companions,
222 (void *)false, (void **)&errdev);
223 acpi_bus_offline_companions(handle, 0, (void *)false, (void **)&errdev);
224 if (errdev) {
225 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200226 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200227 NULL, acpi_bus_offline_companions,
228 (void *)true , (void **)&errdev);
229 if (!errdev || acpi_force_hot_remove)
230 acpi_bus_offline_companions(handle, 0, (void *)true,
231 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200232
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200233 if (errdev && !acpi_force_hot_remove) {
234 dev_warn(errdev, "Offline failed.\n");
235 acpi_bus_online_companions(handle, 0, NULL, NULL);
236 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
237 ACPI_UINT32_MAX,
238 acpi_bus_online_companions, NULL,
239 NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200240 put_device(&device->dev);
241 return -EBUSY;
242 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200243 }
244
Zhang Rui26d46862008-04-29 02:35:48 -0400245 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100246 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400247
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100248 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200249
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100250 /* Device node has been unregistered. */
251 put_device(&device->dev);
Toshi Kanib3c450c2012-10-26 13:38:57 +0200252 device = NULL;
253
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100254 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &not_used))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 arg_list.count = 1;
256 arg_list.pointer = &arg;
257 arg.type = ACPI_TYPE_INTEGER;
258 arg.integer.value = 0;
259 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
260 }
261
262 arg_list.count = 1;
263 arg_list.pointer = &arg;
264 arg.type = ACPI_TYPE_INTEGER;
265 arg.integer.value = 1;
266
267 /*
268 * TBD: _EJD support.
269 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600271 if (ACPI_FAILURE(status)) {
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100272 if (status == AE_NOT_FOUND) {
273 return -ENODEV;
274 } else {
Toshi Kani882fd122013-03-13 19:29:26 +0000275 acpi_handle_warn(handle, "Eject failed (0x%x)\n",
276 status);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100277 return -EIO;
278 }
279 }
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100280
Toshi Kani882fd122013-03-13 19:29:26 +0000281 /*
282 * Verify if eject was indeed successful. If not, log an error
283 * message. No need to call _OST since _EJ0 call was made OK.
284 */
285 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
286 if (ACPI_FAILURE(status)) {
287 acpi_handle_warn(handle,
288 "Status check after eject failed (0x%x)\n", status);
289 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
290 acpi_handle_warn(handle,
291 "Eject incomplete - status 0x%llx\n", sta);
292 }
293
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100294 return 0;
295}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100296
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100297static void acpi_bus_device_eject(void *context)
298{
299 acpi_handle handle = context;
300 struct acpi_device *device = NULL;
301 struct acpi_scan_handler *handler;
302 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200303 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100304
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200305 lock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100306 mutex_lock(&acpi_scan_lock);
307
308 acpi_bus_get_device(handle, &device);
309 if (!device)
310 goto err_out;
311
312 handler = device->handler;
313 if (!handler || !handler->hotplug.enabled) {
314 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
315 goto err_out;
316 }
317 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
318 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200319 if (handler->hotplug.mode == AHM_CONTAINER)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100320 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100321
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200322 get_device(&device->dev);
323 error = acpi_scan_hot_remove(device);
324 if (error)
325 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100327 out:
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100328 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200329 unlock_device_hotplug();
Zhang Ruic8d72a52009-06-22 11:31:16 +0800330 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100331
332 err_out:
333 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
334 NULL);
335 goto out;
336}
337
338static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
339{
340 struct acpi_device *device = NULL;
341 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
342 int error;
343
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200344 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200345 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100346
Rafael J. Wysocki8832f7e2013-07-08 02:01:53 +0200347 if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
348 acpi_bus_get_device(handle, &device);
349 if (device) {
350 dev_warn(&device->dev, "Attempt to re-insert\n");
351 goto out;
352 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100353 }
354 acpi_evaluate_hotplug_ost(handle, ost_source,
355 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
356 error = acpi_bus_scan(handle);
357 if (error) {
358 acpi_handle_warn(handle, "Namespace scan failure\n");
359 goto out;
360 }
361 error = acpi_bus_get_device(handle, &device);
362 if (error) {
363 acpi_handle_warn(handle, "Missing device node object\n");
364 goto out;
365 }
366 ost_code = ACPI_OST_SC_SUCCESS;
367 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
368 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
369
370 out:
371 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
372 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200373 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100374}
375
376static void acpi_scan_bus_check(void *context)
377{
378 acpi_scan_bus_device_check((acpi_handle)context,
379 ACPI_NOTIFY_BUS_CHECK);
380}
381
382static void acpi_scan_device_check(void *context)
383{
384 acpi_scan_bus_device_check((acpi_handle)context,
385 ACPI_NOTIFY_DEVICE_CHECK);
386}
387
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000388static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
389{
390 u32 ost_status;
391
392 switch (type) {
393 case ACPI_NOTIFY_BUS_CHECK:
394 acpi_handle_debug(handle,
395 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
396 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
397 break;
398 case ACPI_NOTIFY_DEVICE_CHECK:
399 acpi_handle_debug(handle,
400 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
401 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
402 break;
403 case ACPI_NOTIFY_EJECT_REQUEST:
404 acpi_handle_debug(handle,
405 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
406 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
407 break;
408 default:
409 /* non-hotplug event; possibly handled by other handler */
410 return;
411 }
412
413 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
414}
415
416static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100417{
418 acpi_osd_exec_callback callback;
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000419 struct acpi_scan_handler *handler = data;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100420 acpi_status status;
421
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000422 if (!handler->hotplug.enabled)
423 return acpi_hotplug_unsupported(handle, type);
424
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100425 switch (type) {
426 case ACPI_NOTIFY_BUS_CHECK:
427 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
428 callback = acpi_scan_bus_check;
429 break;
430 case ACPI_NOTIFY_DEVICE_CHECK:
431 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
432 callback = acpi_scan_device_check;
433 break;
434 case ACPI_NOTIFY_EJECT_REQUEST:
435 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
436 callback = acpi_bus_device_eject;
437 break;
438 default:
439 /* non-hotplug event; possibly handled by other handler */
440 return;
441 }
442 status = acpi_os_hotplug_execute(callback, handle);
443 if (ACPI_FAILURE(status))
444 acpi_evaluate_hotplug_ost(handle, type,
445 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
446 NULL);
447}
448
449/**
450 * acpi_bus_hot_remove_device: hot-remove a device and its children
451 * @context: struct acpi_eject_event pointer (freed in this func)
452 *
453 * Hot-remove a device and its children. This function frees up the
454 * memory space passed by arg context, so that the caller may call
455 * this function asynchronously through acpi_os_hotplug_execute().
456 */
457void acpi_bus_hot_remove_device(void *context)
458{
459 struct acpi_eject_event *ej_event = context;
460 struct acpi_device *device = ej_event->device;
461 acpi_handle handle = device->handle;
462 int error;
463
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200464 lock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100465 mutex_lock(&acpi_scan_lock);
466
467 error = acpi_scan_hot_remove(device);
468 if (error && handle)
469 acpi_evaluate_hotplug_ost(handle, ej_event->event,
470 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
471 NULL);
472
473 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200474 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100475 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
Toshi Kani61622ac2012-11-01 14:42:12 +0000477EXPORT_SYMBOL(acpi_bus_hot_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100479static ssize_t real_power_state_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
481{
482 struct acpi_device *adev = to_acpi_device(dev);
483 int state;
484 int ret;
485
486 ret = acpi_device_get_power(adev, &state);
487 if (ret)
488 return ret;
489
490 return sprintf(buf, "%s\n", acpi_power_state_string(state));
491}
492
493static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
494
495static ssize_t power_state_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
497{
498 struct acpi_device *adev = to_acpi_device(dev);
499
500 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
501}
502
503static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800506acpi_eject_store(struct device *d, struct device_attribute *attr,
507 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800509 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600510 struct acpi_eject_event *ej_event;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100511 acpi_object_type not_used;
512 acpi_status status;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100513 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100515 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100518 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
519 && !acpi_device->driver)
520 return -ENODEV;
521
522 status = acpi_get_type(acpi_device->handle, &not_used);
523 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
524 return -ENODEV;
525
Toshi Kanic4753e52012-05-23 20:25:20 -0600526 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
527 if (!ej_event) {
528 ret = -ENOMEM;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100529 goto err_out;
Toshi Kanic4753e52012-05-23 20:25:20 -0600530 }
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200531 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100532 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Yinghai Lu5993c462013-01-11 22:40:41 +0000533 ej_event->device = acpi_device;
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200534 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100535 get_device(&acpi_device->dev);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100536 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200537 if (ACPI_SUCCESS(status))
538 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100539
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200540 put_device(&acpi_device->dev);
541 kfree(ej_event);
542 ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100543
544 err_out:
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200545 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100546 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200547 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800550static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800552static ssize_t
553acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
554 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600556 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800557}
558static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
559
Lv Zheng923d4a42012-10-30 14:43:26 +0100560static ssize_t acpi_device_uid_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 struct acpi_device *acpi_dev = to_acpi_device(dev);
564
565 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
566}
567static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
568
569static ssize_t acpi_device_adr_show(struct device *dev,
570 struct device_attribute *attr, char *buf)
571{
572 struct acpi_device *acpi_dev = to_acpi_device(dev);
573
574 return sprintf(buf, "0x%08x\n",
575 (unsigned int)(acpi_dev->pnp.bus_address));
576}
577static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
578
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800579static ssize_t
580acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
581 struct acpi_device *acpi_dev = to_acpi_device(dev);
582 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
583 int result;
584
585 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600586 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800587 goto end;
588
589 result = sprintf(buf, "%s\n", (char*)path.pointer);
590 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600591end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800592 return result;
593}
594static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
595
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600596/* sysfs file that shows description text from the ACPI _STR method */
597static ssize_t description_show(struct device *dev,
598 struct device_attribute *attr,
599 char *buf) {
600 struct acpi_device *acpi_dev = to_acpi_device(dev);
601 int result;
602
603 if (acpi_dev->pnp.str_obj == NULL)
604 return 0;
605
606 /*
607 * The _STR object contains a Unicode identifier for a device.
608 * We need to convert to utf-8 so it can be displayed.
609 */
610 result = utf16s_to_utf8s(
611 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
612 acpi_dev->pnp.str_obj->buffer.length,
613 UTF16_LITTLE_ENDIAN, buf,
614 PAGE_SIZE);
615
616 buf[result++] = '\n';
617
618 return result;
619}
620static DEVICE_ATTR(description, 0444, description_show, NULL);
621
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100622static ssize_t
623acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
624 char *buf) {
625 struct acpi_device *acpi_dev = to_acpi_device(dev);
626
627 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
628}
629static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
630
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800631static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600633 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800634 acpi_status status;
635 acpi_handle temp;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100636 unsigned long long sun;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800637 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800639 /*
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800640 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800641 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600642 if (dev->handle) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800643 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600644 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800645 goto end;
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200648 if (!list_empty(&dev->pnp.ids)) {
649 result = device_create_file(&dev->dev, &dev_attr_hid);
650 if (result)
651 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200653 result = device_create_file(&dev->dev, &dev_attr_modalias);
654 if (result)
655 goto end;
656 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200657
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600658 /*
659 * If device has _STR, 'description' file is created
660 */
661 status = acpi_get_handle(dev->handle, "_STR", &temp);
662 if (ACPI_SUCCESS(status)) {
663 status = acpi_evaluate_object(dev->handle, "_STR",
664 NULL, &buffer);
665 if (ACPI_FAILURE(status))
666 buffer.pointer = NULL;
667 dev->pnp.str_obj = buffer.pointer;
668 result = device_create_file(&dev->dev, &dev_attr_description);
669 if (result)
670 goto end;
671 }
672
Toshi Kanid4e1a692013-03-04 21:30:41 +0000673 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100674 result = device_create_file(&dev->dev, &dev_attr_adr);
675 if (dev->pnp.unique_id)
676 result = device_create_file(&dev->dev, &dev_attr_uid);
677
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100678 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
679 if (ACPI_SUCCESS(status)) {
680 dev->pnp.sun = (unsigned long)sun;
681 result = device_create_file(&dev->dev, &dev_attr_sun);
682 if (result)
683 goto end;
684 } else {
685 dev->pnp.sun = (unsigned long)-1;
686 }
687
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800688 /*
689 * If device has _EJ0, 'eject' file is created that is used to trigger
690 * hot-removal function from userland.
691 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800692 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100693 if (ACPI_SUCCESS(status)) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800694 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100695 if (result)
696 return result;
697 }
698
699 if (dev->flags.power_manageable) {
700 result = device_create_file(&dev->dev, &dev_attr_power_state);
701 if (result)
702 return result;
703
704 if (dev->power.flags.power_resources)
705 result = device_create_file(&dev->dev,
706 &dev_attr_real_power_state);
707 }
708
Alex Chiang0c526d92009-05-14 08:31:37 -0600709end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800710 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800711}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800713static void acpi_device_remove_files(struct acpi_device *dev)
714{
715 acpi_status status;
716 acpi_handle temp;
717
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100718 if (dev->flags.power_manageable) {
719 device_remove_file(&dev->dev, &dev_attr_power_state);
720 if (dev->power.flags.power_resources)
721 device_remove_file(&dev->dev,
722 &dev_attr_real_power_state);
723 }
724
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800725 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600726 * If device has _STR, remove 'description' file
727 */
728 status = acpi_get_handle(dev->handle, "_STR", &temp);
729 if (ACPI_SUCCESS(status)) {
730 kfree(dev->pnp.str_obj);
731 device_remove_file(&dev->dev, &dev_attr_description);
732 }
733 /*
734 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800735 */
736 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
737 if (ACPI_SUCCESS(status))
738 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800739
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100740 status = acpi_get_handle(dev->handle, "_SUN", &temp);
741 if (ACPI_SUCCESS(status))
742 device_remove_file(&dev->dev, &dev_attr_sun);
743
Lv Zheng923d4a42012-10-30 14:43:26 +0100744 if (dev->pnp.unique_id)
745 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000746 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100747 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600748 device_remove_file(&dev->dev, &dev_attr_modalias);
749 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600750 if (dev->handle)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800751 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800752}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800754 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200756
Mika Westerbergcf761af2012-10-31 22:44:41 +0100757static const struct acpi_device_id *__acpi_match_device(
758 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200759{
760 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600761 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200762
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800763 /*
764 * If the device is not present, it is unnecessary to load device
765 * driver for it.
766 */
767 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100768 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800769
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600770 for (id = ids; id->id[0]; id++)
771 list_for_each_entry(hwid, &device->pnp.ids, list)
772 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100773 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200774
Mika Westerbergcf761af2012-10-31 22:44:41 +0100775 return NULL;
776}
777
778/**
779 * acpi_match_device - Match a struct device against a given list of ACPI IDs
780 * @ids: Array of struct acpi_device_id object to match against.
781 * @dev: The device structure to match.
782 *
783 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
784 * object for that handle and use that object to match against a given list of
785 * device IDs.
786 *
787 * Return a pointer to the first matching ID on success or %NULL on failure.
788 */
789const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
790 const struct device *dev)
791{
792 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100793 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100794
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100795 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100796 return NULL;
797
798 return __acpi_match_device(adev, ids);
799}
800EXPORT_SYMBOL_GPL(acpi_match_device);
801
802int acpi_match_device_ids(struct acpi_device *device,
803 const struct acpi_device_id *ids)
804{
805 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200806}
807EXPORT_SYMBOL(acpi_match_device_ids);
808
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100809static void acpi_free_power_resources_lists(struct acpi_device *device)
810{
811 int i;
812
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100813 if (device->wakeup.flags.valid)
814 acpi_power_resources_list_free(&device->wakeup.resources);
815
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100816 if (!device->flags.power_manageable)
817 return;
818
819 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
820 struct acpi_device_power_state *ps = &device->power.states[i];
821 acpi_power_resources_list_free(&ps->resources);
822 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600823}
824
Patrick Mochel1890a972006-12-07 20:56:31 +0800825static void acpi_device_release(struct device *dev)
826{
827 struct acpi_device *acpi_dev = to_acpi_device(dev);
828
Toshi Kanic0af4172013-03-04 21:30:42 +0000829 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100830 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800831 kfree(acpi_dev);
832}
833
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800834static int acpi_bus_match(struct device *dev, struct device_driver *drv)
835{
836 struct acpi_device *acpi_dev = to_acpi_device(dev);
837 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
838
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100839 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d4102012-12-21 00:36:39 +0100840 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800841}
842
Kay Sievers7eff2e72007-08-14 15:15:12 +0200843static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800844{
845 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200846 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800847
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200848 if (list_empty(&acpi_dev->pnp.ids))
849 return 0;
850
Kay Sievers7eff2e72007-08-14 15:15:12 +0200851 if (add_uevent_var(env, "MODALIAS="))
852 return -ENOMEM;
853 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
854 sizeof(env->buf) - env->buflen);
855 if (len >= (sizeof(env->buf) - env->buflen))
856 return -ENOMEM;
857 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 0;
859}
860
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000861static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
862{
863 struct acpi_device *device = data;
864
865 device->driver->ops.notify(device, event);
866}
867
868static acpi_status acpi_device_notify_fixed(void *data)
869{
870 struct acpi_device *device = data;
871
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000872 /* Fixed hardware devices have no handles */
873 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000874 return AE_OK;
875}
876
877static int acpi_device_install_notify_handler(struct acpi_device *device)
878{
879 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000880
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000881 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000882 status =
883 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
884 acpi_device_notify_fixed,
885 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000886 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000887 status =
888 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
889 acpi_device_notify_fixed,
890 device);
891 else
892 status = acpi_install_notify_handler(device->handle,
893 ACPI_DEVICE_NOTIFY,
894 acpi_device_notify,
895 device);
896
897 if (ACPI_FAILURE(status))
898 return -EINVAL;
899 return 0;
900}
901
902static void acpi_device_remove_notify_handler(struct acpi_device *device)
903{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000904 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000905 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
906 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000907 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000908 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
909 acpi_device_notify_fixed);
910 else
911 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
912 acpi_device_notify);
913}
914
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200915static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800916{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800917 struct acpi_device *acpi_dev = to_acpi_device(dev);
918 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
919 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200921 if (acpi_dev->handler)
922 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000923
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200924 if (!acpi_drv->ops.add)
925 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800926
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200927 ret = acpi_drv->ops.add(acpi_dev);
928 if (ret)
929 return ret;
930
931 acpi_dev->driver = acpi_drv;
932 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
933 "Driver [%s] successfully bound to device [%s]\n",
934 acpi_drv->name, acpi_dev->pnp.bus_id));
935
936 if (acpi_drv->ops.notify) {
937 ret = acpi_device_install_notify_handler(acpi_dev);
938 if (ret) {
939 if (acpi_drv->ops.remove)
940 acpi_drv->ops.remove(acpi_dev);
941
942 acpi_dev->driver = NULL;
943 acpi_dev->driver_data = NULL;
944 return ret;
945 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800946 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200947
948 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
949 acpi_drv->name, acpi_dev->pnp.bus_id));
950 get_device(dev);
951 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800952}
953
954static int acpi_device_remove(struct device * dev)
955{
956 struct acpi_device *acpi_dev = to_acpi_device(dev);
957 struct acpi_driver *acpi_drv = acpi_dev->driver;
958
959 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000960 if (acpi_drv->ops.notify)
961 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800962 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100963 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800964 }
965 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700966 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800967
968 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800969 return 0;
970}
971
David Brownell55955aa2007-05-08 00:28:35 -0700972struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800973 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800974 .match = acpi_bus_match,
975 .probe = acpi_device_probe,
976 .remove = acpi_device_remove,
977 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978};
979
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100980int acpi_device_add(struct acpi_device *device,
981 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800983 int result;
984 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
985 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000986
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100987 if (device->handle) {
988 acpi_status status;
989
990 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
991 device);
992 if (ACPI_FAILURE(status)) {
993 acpi_handle_err(device->handle,
994 "Unable to attach device data\n");
995 return -ENODEV;
996 }
997 }
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 /*
1000 * Linkage
1001 * -------
1002 * Link this device to its parent and siblings.
1003 */
1004 INIT_LIST_HEAD(&device->children);
1005 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001007 INIT_LIST_HEAD(&device->physical_node_list);
1008 mutex_init(&device->physical_node_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001009 INIT_LIST_HEAD(&device->power_dependent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001011 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1012 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001013 pr_err(PREFIX "Memory allocation error\n");
1014 result = -ENOMEM;
1015 goto err_detach;
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001016 }
1017
Shaohua Li90905892009-04-07 10:24:29 +08001018 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001019 /*
1020 * Find suitable bus_id and instance number in acpi_bus_id_list
1021 * If failed, create one and link it into acpi_bus_id_list
1022 */
1023 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001024 if (!strcmp(acpi_device_bus_id->bus_id,
1025 acpi_device_hid(device))) {
1026 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001027 found = 1;
1028 kfree(new_bus_id);
1029 break;
1030 }
1031 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001032 if (!found) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001033 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001034 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001035 acpi_device_bus_id->instance_no = 0;
1036 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1037 }
Kay Sievers07944692008-10-30 01:18:59 +01001038 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001039
Len Brown33b57152008-12-15 22:09:26 -05001040 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (device->wakeup.flags.valid)
1044 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001045 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Patrick Mochel1890a972006-12-07 20:56:31 +08001047 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001048 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001049 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001050 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001051 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001052 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001053 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001054 goto err;
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001055 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001056
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001057 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001058 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001059 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1060 dev_name(&device->dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001061
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001062 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001063
1064 err:
Shaohua Li90905892009-04-07 10:24:29 +08001065 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001066 if (device->parent)
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001067 list_del(&device->node);
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001068 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001069 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001070
1071 err_detach:
1072 acpi_detach_data(device->handle, acpi_bus_data_handler);
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001073 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Rafael J. Wysockib17b5372013-01-15 13:23:44 +01001076static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Shaohua Li90905892009-04-07 10:24:29 +08001078 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001079 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001083 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +08001086
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001087 acpi_power_add_remove_device(device, false);
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001088 acpi_device_remove_files(device);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +01001089 if (device->remove)
1090 device->remove(device);
1091
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001092 device_del(&device->dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001093 /*
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001094 * Transition the device to D3cold to drop the reference counts of all
1095 * power resources the device depends on and turn off the ones that have
1096 * no more references.
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001097 */
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001098 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001099 device->handle = NULL;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001100 put_device(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Zhang Rui9e89dde2006-12-07 20:56:16 +08001103/* --------------------------------------------------------------------------
1104 Driver Management
1105 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001107 * acpi_bus_register_driver - register a driver with the ACPI bus
1108 * @driver: driver being registered
1109 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001111 * devices that match the driver's criteria and binds. Returns zero for
1112 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 */
Len Brown4be44fc2005-08-05 00:44:28 -04001114int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Patrick Mochel1890a972006-12-07 20:56:31 +08001116 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001119 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001120 driver->drv.name = driver->name;
1121 driver->drv.bus = &acpi_bus_type;
1122 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Patrick Mochel1890a972006-12-07 20:56:31 +08001124 ret = driver_register(&driver->drv);
1125 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Len Brown4be44fc2005-08-05 00:44:28 -04001128EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001131 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1132 * @driver: driver to unregister
1133 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1135 * devices that match the driver's criteria and unbinds.
1136 */
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -04001137void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Patrick Mochel1890a972006-12-07 20:56:31 +08001139 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
Len Brown4be44fc2005-08-05 00:44:28 -04001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142EXPORT_SYMBOL(acpi_bus_unregister_driver);
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144/* --------------------------------------------------------------------------
1145 Device Enumeration
1146 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001147static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1148{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001149 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001150 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001151
1152 /*
1153 * Fixed hardware devices do not appear in the namespace and do not
1154 * have handles, but we fabricate acpi_devices for them, so we have
1155 * to deal with them specially.
1156 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001157 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001158 return acpi_root;
1159
1160 do {
1161 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001162 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001163 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1164 } while (acpi_bus_get_device(handle, &device));
1165 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001166}
1167
Len Brownc8f7a622006-07-09 17:22:28 -04001168acpi_status
1169acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1170{
1171 acpi_status status;
1172 acpi_handle tmp;
1173 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1174 union acpi_object *obj;
1175
1176 status = acpi_get_handle(handle, "_EJD", &tmp);
1177 if (ACPI_FAILURE(status))
1178 return status;
1179
1180 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1181 if (ACPI_SUCCESS(status)) {
1182 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001183 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1184 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001185 kfree(buffer.pointer);
1186 }
1187 return status;
1188}
1189EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1190
Bob Moore8e4319c2009-06-29 13:43:27 +08001191void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193
1194 /* TBD */
1195
1196 return;
1197}
1198
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001199static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1200 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001202 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1203 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001205 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001206 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001208 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001209 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001211 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001213 /* _PRW */
1214 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1215 if (ACPI_FAILURE(status)) {
1216 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001217 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001218 }
1219
1220 package = (union acpi_object *)buffer.pointer;
1221
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001222 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001223 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001226 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001227 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (element->type == ACPI_TYPE_PACKAGE) {
1230 if ((element->package.count < 2) ||
1231 (element->package.elements[0].type !=
1232 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001233 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001234 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001235
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001236 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001238 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 (u32) element->package.elements[1].integer.value;
1240 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001241 wakeup->gpe_device = NULL;
1242 wakeup->gpe_number = element->integer.value;
1243 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001244 goto out;
1245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001248 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001249 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001250
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001251 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001253 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1254 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001255 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001257 if (!list_empty(&wakeup->resources)) {
1258 int sleep_state;
1259
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001260 err = acpi_power_wakeup_list_init(&wakeup->resources,
1261 &sleep_state);
1262 if (err) {
1263 acpi_handle_warn(handle, "Retrieving current states "
1264 "of wakeup power resources failed\n");
1265 acpi_power_resources_list_free(&wakeup->resources);
1266 goto out;
1267 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001268 if (sleep_state < wakeup->sleep_state) {
1269 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1270 "(S%d) by S%d from power resources\n",
1271 (int)wakeup->sleep_state, sleep_state);
1272 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Lin Mingbba63a22010-12-13 13:39:17 +08001275 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001276
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001277 out:
1278 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001279 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001282static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001284 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001285 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001286 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001287 {"PNP0C0E", 0},
1288 {"", 0},
1289 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001290 acpi_status status;
1291 acpi_event_status event_status;
1292
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001293 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001294
1295 /* Power button, Lid switch always enable wakeup */
1296 if (!acpi_match_device_ids(device, button_device_ids)) {
1297 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001298 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1299 /* Do not use Lid/sleep button for S5 wakeup */
1300 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1301 device->wakeup.sleep_state = ACPI_STATE_S4;
1302 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001303 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001304 return;
1305 }
1306
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001307 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1308 device->wakeup.gpe_number,
1309 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001310 if (status == AE_OK)
1311 device->wakeup.flags.run_wake =
1312 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1313}
1314
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001315static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001316{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001317 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001318 acpi_status status = 0;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001319 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001320
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001321 /* Presence of _PRW indicates wake capable */
1322 status = acpi_get_handle(device->handle, "_PRW", &temp);
1323 if (ACPI_FAILURE(status))
1324 return;
1325
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001326 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1327 &device->wakeup);
1328 if (err) {
1329 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001330 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001334 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001335 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001336 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1337 * system for the ACPI device with the _PRW object.
1338 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1339 * So it is necessary to call _DSW object first. Only when it is not
1340 * present will the _PSW object used.
1341 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001342 err = acpi_device_sleep_wake(device, 0, 0, 0);
1343 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001344 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1345 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001348static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001350 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001351 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1352 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001353 acpi_handle handle;
1354 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001355
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001356 INIT_LIST_HEAD(&ps->resources);
1357
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001358 /* Evaluate "_PRx" to get referenced power resources */
1359 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1360 if (ACPI_SUCCESS(status)) {
1361 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001362
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001363 if (buffer.length && package
1364 && package->type == ACPI_TYPE_PACKAGE
1365 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001366 int err = acpi_extract_power_resources(package, 0,
1367 &ps->resources);
1368 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001369 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001370 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001371 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001372 }
1373
1374 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001375 pathname[2] = 'S';
1376 status = acpi_get_handle(device->handle, pathname, &handle);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001377 if (ACPI_SUCCESS(status))
1378 ps->flags.explicit_set = 1;
1379
1380 /*
1381 * State is valid if there are means to put the device into it.
1382 * D3hot is only valid if _PR3 present.
1383 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001384 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001385 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1386 ps->flags.valid = 1;
1387 ps->flags.os_accessible = 1;
1388 }
1389
1390 ps->power = -1; /* Unknown - driver assigned */
1391 ps->latency = -1; /* Unknown - driver assigned */
1392}
1393
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001394static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001396 acpi_status status;
1397 acpi_handle handle;
1398 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001400 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1401 status = acpi_get_handle(device->handle, "_PS0", &handle);
1402 if (ACPI_FAILURE(status)) {
1403 status = acpi_get_handle(device->handle, "_PR0", &handle);
1404 if (ACPI_FAILURE(status))
1405 return;
1406 }
1407
1408 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001411 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 */
Zhang Rui9e89dde2006-12-07 20:56:16 +08001413 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001415 device->power.flags.explicit_get = 1;
1416 status = acpi_get_handle(device->handle, "_IRC", &handle);
1417 if (ACPI_SUCCESS(status))
1418 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001421 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001423 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1424 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001426 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Zhang Rui9e89dde2006-12-07 20:56:16 +08001428 /* Set defaults for D0 and D3 states (always valid) */
1429 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1430 device->power.states[ACPI_STATE_D0].power = 100;
1431 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1432 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001434 /* Set D3cold's explicit_set flag if _PS3 exists. */
1435 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1436 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1437
Aaron Lu1399dfc2012-11-21 23:33:40 +01001438 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1439 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1440 device->power.flags.power_resources)
1441 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1442
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001443 if (acpi_bus_init_power(device)) {
1444 acpi_free_power_resources_lists(device);
1445 device->flags.power_manageable = 0;
1446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001449static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Len Brown4be44fc2005-08-05 00:44:28 -04001451 acpi_status status = AE_OK;
1452 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /* Presence of _STA indicates 'dynamic_status' */
1455 status = acpi_get_handle(device->handle, "_STA", &temp);
1456 if (ACPI_SUCCESS(status))
1457 device->flags.dynamic_status = 1;
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 /* Presence of _RMV indicates 'removable' */
1460 status = acpi_get_handle(device->handle, "_RMV", &temp);
1461 if (ACPI_SUCCESS(status))
1462 device->flags.removable = 1;
1463
1464 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1465 status = acpi_get_handle(device->handle, "_EJD", &temp);
1466 if (ACPI_SUCCESS(status))
1467 device->flags.ejectable = 1;
1468 else {
1469 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1470 if (ACPI_SUCCESS(status))
1471 device->flags.ejectable = 1;
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001475static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Len Brown4be44fc2005-08-05 00:44:28 -04001477 char bus_id[5] = { '?', 0 };
1478 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1479 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 /*
1482 * Bus ID
1483 * ------
1484 * The device's Bus ID is simply the object name.
1485 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1486 */
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001487 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001489 return;
1490 }
1491
1492 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 case ACPI_BUS_TYPE_POWER_BUTTON:
1494 strcpy(device->pnp.bus_id, "PWRF");
1495 break;
1496 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1497 strcpy(device->pnp.bus_id, "SLPF");
1498 break;
1499 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001500 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 /* Clean up trailing underscores (if any) */
1502 for (i = 3; i > 1; i--) {
1503 if (bus_id[i] == '_')
1504 bus_id[i] = '\0';
1505 else
1506 break;
1507 }
1508 strcpy(device->pnp.bus_id, bus_id);
1509 break;
1510 }
1511}
1512
Zhang Rui54735262007-01-11 02:09:09 -05001513/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001514 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001515 *
1516 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1517 * then we can safely call it an ejectable drive bay
1518 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001519static int acpi_bay_match(acpi_handle handle)
1520{
Zhang Rui54735262007-01-11 02:09:09 -05001521 acpi_status status;
Zhang Rui54735262007-01-11 02:09:09 -05001522 acpi_handle tmp;
1523 acpi_handle phandle;
1524
Zhang Rui54735262007-01-11 02:09:09 -05001525 status = acpi_get_handle(handle, "_EJ0", &tmp);
1526 if (ACPI_FAILURE(status))
1527 return -ENODEV;
1528
1529 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1530 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1531 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1532 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1533 return 0;
1534
1535 if (acpi_get_parent(handle, &phandle))
1536 return -ENODEV;
1537
1538 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1539 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1540 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1541 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1542 return 0;
1543
1544 return -ENODEV;
1545}
1546
Frank Seidel3620f2f2007-12-07 13:20:34 +01001547/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001548 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001549 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001550static int acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001551{
1552 acpi_handle tmp;
Toshi Kanid4e1a692013-03-04 21:30:41 +00001553 return acpi_get_handle(handle, "_DCK", &tmp);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001554}
1555
Thomas Renninger620e1122010-10-01 10:54:00 +02001556const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001557{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001558 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001559
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001560 if (list_empty(&device->pnp.ids))
1561 return dummy_hid;
1562
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001563 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1564 return hid->id;
1565}
1566EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001567
Toshi Kanid4e1a692013-03-04 21:30:41 +00001568static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001569{
1570 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001571
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001572 id = kmalloc(sizeof(*id), GFP_KERNEL);
1573 if (!id)
1574 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001575
Thomas Meyer581de592011-08-06 11:32:56 +02001576 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001577 if (!id->id) {
1578 kfree(id);
1579 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001580 }
1581
Toshi Kanid4e1a692013-03-04 21:30:41 +00001582 list_add_tail(&id->list, &pnp->ids);
1583 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001584}
1585
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001586/*
1587 * Old IBM workstations have a DSDT bug wherein the SMBus object
1588 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1589 * prefix. Work around this.
1590 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001591static int acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001592{
1593 acpi_handle h_dummy;
1594 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1595 int result;
1596
1597 if (!dmi_name_in_vendors("IBM"))
1598 return -ENODEV;
1599
1600 /* Look for SMBS object */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001601 result = acpi_get_name(handle, ACPI_SINGLE_NAME, &path);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001602 if (result)
1603 return result;
1604
1605 if (strcmp("SMBS", path.pointer)) {
1606 result = -ENODEV;
1607 goto out;
1608 }
1609
1610 /* Does it have the necessary (but misnamed) methods? */
1611 result = -ENODEV;
Toshi Kanid4e1a692013-03-04 21:30:41 +00001612 if (ACPI_SUCCESS(acpi_get_handle(handle, "SBI", &h_dummy)) &&
1613 ACPI_SUCCESS(acpi_get_handle(handle, "SBR", &h_dummy)) &&
1614 ACPI_SUCCESS(acpi_get_handle(handle, "SBW", &h_dummy)))
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001615 result = 0;
1616out:
1617 kfree(path.pointer);
1618 return result;
1619}
1620
Toshi Kanic0af4172013-03-04 21:30:42 +00001621static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1622 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Len Brown4be44fc2005-08-05 00:44:28 -04001624 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001625 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001626 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001627 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Toshi Kanic0af4172013-03-04 21:30:42 +00001629 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001631 if (handle == ACPI_ROOT_OBJECT) {
1632 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001633 break;
1634 }
1635
Toshi Kanic0af4172013-03-04 21:30:42 +00001636 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001638 pr_err(PREFIX "%s: Error reading device info\n",
1639 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return;
1641 }
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001644 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001645 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001646 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001647 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001648 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001651 pnp->bus_address = info->address;
1652 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
Lv Zhengccf78042012-10-30 14:41:07 +01001654 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001655 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001656 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001657
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001658 kfree(info);
1659
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001660 /*
1661 * Some devices don't reliably have _HIDs & _CIDs, so add
1662 * synthetic HIDs to make sure drivers can find them.
1663 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001664 if (acpi_is_video_device(handle))
1665 acpi_add_id(pnp, ACPI_VIDEO_HID);
1666 else if (ACPI_SUCCESS(acpi_bay_match(handle)))
1667 acpi_add_id(pnp, ACPI_BAY_HID);
1668 else if (ACPI_SUCCESS(acpi_dock_match(handle)))
1669 acpi_add_id(pnp, ACPI_DOCK_HID);
1670 else if (!acpi_ibm_smbus_match(handle))
1671 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1672 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1673 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1674 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1675 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001676 }
Zhang Rui54735262007-01-11 02:09:09 -05001677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 break;
1679 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001680 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 break;
1682 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001683 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001686 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 break;
1688 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001689 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 break;
1691 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001692 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 break;
1694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
Toshi Kanic0af4172013-03-04 21:30:42 +00001697void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1698{
1699 struct acpi_hardware_id *id, *tmp;
1700
1701 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1702 kfree(id->id);
1703 kfree(id);
1704 }
1705 kfree(pnp->unique_id);
1706}
1707
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001708void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1709 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001711 INIT_LIST_HEAD(&device->pnp.ids);
1712 device->device_type = type;
1713 device->handle = handle;
1714 device->parent = acpi_bus_get_parent(handle);
1715 STRUCT_TO_INT(device->status) = sta;
1716 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001717 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001718 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001719 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001720 device_initialize(&device->dev);
1721 dev_set_uevent_suppress(&device->dev, true);
1722}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001723
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001724void acpi_device_add_finalize(struct acpi_device *device)
1725{
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001726 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001727 dev_set_uevent_suppress(&device->dev, false);
1728 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001731static int acpi_add_single_object(struct acpi_device **child,
1732 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001733 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001735 int result;
1736 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001737 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Burman Yan36bcbec2006-12-19 12:56:11 -08001739 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001741 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001742 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001745 acpi_init_device_object(device, handle, type, sta);
1746 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001747 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001749 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001750 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001751 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001752 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001755 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001756 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001757 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1758 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1759 dev_name(&device->dev), (char *) buffer.pointer,
1760 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1761 kfree(buffer.pointer);
1762 *child = device;
1763 return 0;
Rafael J. Wysockibf325f952010-11-25 00:10:44 +01001764}
1765
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001766static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1767 unsigned long long *sta)
1768{
1769 acpi_status status;
1770 acpi_object_type acpi_type;
1771
1772 status = acpi_get_type(handle, &acpi_type);
1773 if (ACPI_FAILURE(status))
1774 return -ENODEV;
1775
1776 switch (acpi_type) {
1777 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1778 case ACPI_TYPE_DEVICE:
1779 *type = ACPI_BUS_TYPE_DEVICE;
1780 status = acpi_bus_get_status_handle(handle, sta);
1781 if (ACPI_FAILURE(status))
1782 return -ENODEV;
1783 break;
1784 case ACPI_TYPE_PROCESSOR:
1785 *type = ACPI_BUS_TYPE_PROCESSOR;
1786 status = acpi_bus_get_status_handle(handle, sta);
1787 if (ACPI_FAILURE(status))
1788 return -ENODEV;
1789 break;
1790 case ACPI_TYPE_THERMAL:
1791 *type = ACPI_BUS_TYPE_THERMAL;
1792 *sta = ACPI_STA_DEFAULT;
1793 break;
1794 case ACPI_TYPE_POWER:
1795 *type = ACPI_BUS_TYPE_POWER;
1796 *sta = ACPI_STA_DEFAULT;
1797 break;
1798 default:
1799 return -ENODEV;
1800 }
1801
1802 return 0;
1803}
1804
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001805static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1806 char *idstr,
1807 const struct acpi_device_id **matchid)
1808{
1809 const struct acpi_device_id *devid;
1810
1811 for (devid = handler->ids; devid->id[0]; devid++)
1812 if (!strcmp((char *)devid->id, idstr)) {
1813 if (matchid)
1814 *matchid = devid;
1815
1816 return true;
1817 }
1818
1819 return false;
1820}
1821
Toshi Kani6b772e82013-03-04 21:30:43 +00001822static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1823 const struct acpi_device_id **matchid)
1824{
1825 struct acpi_scan_handler *handler;
1826
1827 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1828 if (acpi_scan_handler_matching(handler, idstr, matchid))
1829 return handler;
1830
1831 return NULL;
1832}
1833
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001834void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1835{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001836 if (!!hotplug->enabled == !!val)
1837 return;
1838
1839 mutex_lock(&acpi_scan_lock);
1840
1841 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001842
1843 mutex_unlock(&acpi_scan_lock);
1844}
1845
Toshi Kani6b772e82013-03-04 21:30:43 +00001846static void acpi_scan_init_hotplug(acpi_handle handle, int type)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001847{
Toshi Kani6b772e82013-03-04 21:30:43 +00001848 struct acpi_device_pnp pnp = {};
1849 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001850 struct acpi_scan_handler *handler;
1851
Toshi Kani6b772e82013-03-04 21:30:43 +00001852 INIT_LIST_HEAD(&pnp.ids);
1853 acpi_set_pnp_ids(handle, &pnp, type);
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001854
Toshi Kani6b772e82013-03-04 21:30:43 +00001855 if (!pnp.type.hardware_id)
Catalin Marinas7a26b532013-05-15 16:49:35 +00001856 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001857
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001858 /*
1859 * This relies on the fact that acpi_install_notify_handler() will not
1860 * install the same notify handler routine twice for the same handle.
1861 */
Toshi Kani6b772e82013-03-04 21:30:43 +00001862 list_for_each_entry(hwid, &pnp.ids, list) {
1863 handler = acpi_scan_match_handler(hwid->id, NULL);
1864 if (handler) {
Toshi Kani2cbb14f2013-03-05 22:33:31 +00001865 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1866 acpi_hotplug_notify_cb, handler);
Toshi Kani6b772e82013-03-04 21:30:43 +00001867 break;
1868 }
1869 }
1870
Catalin Marinas7a26b532013-05-15 16:49:35 +00001871out:
Toshi Kani6b772e82013-03-04 21:30:43 +00001872 acpi_free_pnp_ids(&pnp);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001873}
1874
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001875static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1876 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001878 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001879 int type;
1880 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001881 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001882 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001883
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001884 acpi_bus_get_device(handle, &device);
1885 if (device)
1886 goto out;
1887
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001888 result = acpi_bus_type_and_status(handle, &type, &sta);
1889 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001890 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001892 if (type == ACPI_BUS_TYPE_POWER) {
1893 acpi_add_power_resource(handle);
1894 return AE_OK;
1895 }
1896
Toshi Kani6b772e82013-03-04 21:30:43 +00001897 acpi_scan_init_hotplug(handle, type);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001898
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001899 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001900 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001901 struct acpi_device_wakeup wakeup;
1902 acpi_handle temp;
1903
1904 status = acpi_get_handle(handle, "_PRW", &temp);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001905 if (ACPI_SUCCESS(status)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001906 acpi_bus_extract_wakeup_device_power_package(handle,
1907 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001908 acpi_power_resources_list_free(&wakeup.resources);
1909 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001910 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001913 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001914 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001915 return AE_CTRL_DEPTH;
1916
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001917 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001918 if (!*return_value)
1919 *return_value = device;
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001920
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001921 return AE_OK;
1922}
1923
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001924static int acpi_scan_attach_handler(struct acpi_device *device)
1925{
1926 struct acpi_hardware_id *hwid;
1927 int ret = 0;
1928
1929 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001930 const struct acpi_device_id *devid;
1931 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001932
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001933 handler = acpi_scan_match_handler(hwid->id, &devid);
1934 if (handler) {
1935 ret = handler->attach(device, devid);
1936 if (ret > 0) {
1937 device->handler = handler;
1938 break;
1939 } else if (ret < 0) {
1940 break;
1941 }
1942 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001943 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001944 return ret;
1945}
1946
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001947static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1948 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001949{
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001950 struct acpi_device *device;
1951 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001952 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001953
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001954 /*
1955 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1956 * namespace walks prematurely.
1957 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001958 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001959 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001960
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001961 if (acpi_bus_get_device(handle, &device))
1962 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001963
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02001964 if (device->handler)
1965 return AE_OK;
1966
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001967 ret = acpi_scan_attach_handler(device);
1968 if (ret)
1969 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1970
1971 ret = device_attach(&device->dev);
1972 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001975/**
1976 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1977 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001978 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001979 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1980 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001981 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001982 * If no devices were found, -ENODEV is returned, but it does not mean that
1983 * there has been a real error. There just have been no suitable ACPI objects
1984 * in the table trunk from which the kernel could create a device and add an
1985 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001986 *
1987 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001988 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001989int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001990{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001992 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001993
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001994 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001996 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001997
Thomas Renningerd2f66502010-01-29 17:48:51 +01001998 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001999 error = -ENODEV;
2000 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01002001 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01002002 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01002003
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01002004 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002005}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002006EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002008static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
2009 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002011 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002013 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002014 struct acpi_scan_handler *dev_handler = device->handler;
2015
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002016 if (dev_handler) {
2017 if (dev_handler->detach)
2018 dev_handler->detach(device);
2019
2020 device->handler = NULL;
2021 } else {
2022 device_release_driver(&device->dev);
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002025 return AE_OK;
2026}
2027
2028static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
2029 void *not_used, void **ret_not_used)
2030{
2031 struct acpi_device *device = NULL;
2032
2033 if (!acpi_bus_get_device(handle, &device))
2034 acpi_device_unregister(device);
2035
2036 return AE_OK;
2037}
2038
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002039/**
2040 * acpi_bus_trim - Remove ACPI device node and all of its descendants
2041 * @start: Root of the ACPI device nodes subtree to remove.
2042 *
2043 * Must be called under acpi_scan_lock.
2044 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01002045void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002047 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002048 * Execute acpi_bus_device_detach() as a post-order callback to detach
2049 * all ACPI drivers from the device nodes being removed.
2050 */
2051 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2052 acpi_bus_device_detach, NULL, NULL);
2053 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
2054 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002055 * Execute acpi_bus_remove() as a post-order callback to remove device
2056 * nodes in the given namespace scope.
2057 */
2058 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2059 acpi_bus_remove, NULL, NULL);
2060 acpi_bus_remove(start->handle, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
Kristen Accardiceaba662006-02-23 17:56:01 -08002062EXPORT_SYMBOL_GPL(acpi_bus_trim);
2063
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002064static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Len Brown4be44fc2005-08-05 00:44:28 -04002066 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /*
2069 * Enumerate all fixed-feature devices.
2070 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002071 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2072 struct acpi_device *device = NULL;
2073
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002074 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002075 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002076 ACPI_STA_DEFAULT);
2077 if (result)
2078 return result;
2079
2080 result = device_attach(&device->dev);
2081 if (result < 0)
2082 return result;
2083
Daniel Drakec10d7a12012-05-10 00:08:43 +01002084 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002087 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2088 struct acpi_device *device = NULL;
2089
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002090 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002091 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002092 ACPI_STA_DEFAULT);
2093 if (result)
2094 return result;
2095
2096 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002099 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Bjorn Helgaase747f272009-03-24 16:49:43 -06002102int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Patrick Mochel5b327262006-05-10 10:33:00 -04002106 result = bus_register(&acpi_bus_type);
2107 if (result) {
2108 /* We don't want to quit even if we failed to add suspend/resume */
2109 printk(KERN_ERR PREFIX "Could not register bus type\n");
2110 }
2111
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002112 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002113 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002114 acpi_processor_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002115 acpi_platform_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002116 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002117 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002118 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002119 acpi_memory_hotplug_init();
Jiang Liu94add0f2013-06-23 00:59:55 +02002120 acpi_dock_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002121
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002122 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 * Enumerate devices in the ACPI namespace.
2125 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002126 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002128 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002130 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002132 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002134 result = acpi_bus_scan_fixed();
2135 if (result) {
2136 acpi_device_unregister(acpi_root);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002137 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002138 }
2139
2140 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002141
Yinghai Lu668192b2013-01-21 13:20:48 -08002142 acpi_pci_root_hp_init();
2143
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002144 out:
2145 mutex_unlock(&acpi_scan_lock);
2146 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}