blob: ada0b4cf2ba5d4b19cf7e27edca3fd306ad06cf4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040022extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020025#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUS_DEVICE_NAME "System Bus"
27
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020030/*
31 * If set, devices will be hot-removed even if they cannot be put offline
32 * gracefully (from the kernel's standpoint).
33 */
34bool acpi_force_hot_remove;
35
Thomas Renninger620e1122010-10-01 10:54:00 +020036static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020037
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080038static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010039static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010040static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080041DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042LIST_HEAD(acpi_wakeup_device_list);
43
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080044struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080045 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080046 unsigned int instance_no;
47 struct list_head node;
48};
Thomas Renninger29b71a12007-07-23 14:43:51 +020049
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010050void acpi_scan_lock_acquire(void)
51{
52 mutex_lock(&acpi_scan_lock);
53}
54EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
55
56void acpi_scan_lock_release(void)
57{
58 mutex_unlock(&acpi_scan_lock);
59}
60EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
61
Rafael J. Wysockica589f92013-01-30 14:27:29 +010062int acpi_scan_add_handler(struct acpi_scan_handler *handler)
63{
64 if (!handler || !handler->attach)
65 return -EINVAL;
66
67 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
68 return 0;
69}
70
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010071int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
72 const char *hotplug_profile_name)
73{
74 int error;
75
76 error = acpi_scan_add_handler(handler);
77 if (error)
78 return error;
79
80 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
81 return 0;
82}
83
Thomas Renninger29b71a12007-07-23 14:43:51 +020084/*
85 * Creates hid/cid(s) string needed for modalias and uevent
86 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
87 * char *modalias: "acpi:IBM0001:ACPI0001"
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct acpi_object_list arg_list;
197 union acpi_object arg;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200198 struct device *errdev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100199 acpi_status status;
Toshi Kani882fd122013-03-13 19:29:26 +0000200 unsigned long long sta;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100201
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100202 /* If there is no handle, the device node has been unregistered. */
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100203 if (!handle) {
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100204 dev_dbg(&device->dev, "ACPI handle missing\n");
205 put_device(&device->dev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100206 return -EINVAL;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100207 }
208
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200209 lock_device_hotplug();
210
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200211 /*
212 * Carry out two passes here and ignore errors in the first pass,
213 * because if the devices in question are memory blocks and
214 * CONFIG_MEMCG is set, one of the blocks may hold data structures
215 * that the other blocks depend on, but it is not known in advance which
216 * block holds them.
217 *
218 * If the first pass is successful, the second one isn't needed, though.
219 */
220 errdev = NULL;
221 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
222 NULL, acpi_bus_offline_companions,
223 (void *)false, (void **)&errdev);
224 acpi_bus_offline_companions(handle, 0, (void *)false, (void **)&errdev);
225 if (errdev) {
226 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200227 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200228 NULL, acpi_bus_offline_companions,
229 (void *)true , (void **)&errdev);
230 if (!errdev || acpi_force_hot_remove)
231 acpi_bus_offline_companions(handle, 0, (void *)true,
232 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200233
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200234 if (errdev && !acpi_force_hot_remove) {
235 dev_warn(errdev, "Offline failed.\n");
236 acpi_bus_online_companions(handle, 0, NULL, NULL);
237 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
238 ACPI_UINT32_MAX,
239 acpi_bus_online_companions, NULL,
240 NULL, NULL);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200241
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200242 unlock_device_hotplug();
243
244 put_device(&device->dev);
245 return -EBUSY;
246 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200247 }
248
Zhang Rui26d46862008-04-29 02:35:48 -0400249 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100250 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400251
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100252 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200253
254 unlock_device_hotplug();
255
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100256 /* Device node has been unregistered. */
257 put_device(&device->dev);
Toshi Kanib3c450c2012-10-26 13:38:57 +0200258 device = NULL;
259
Jiang Liu952c63e2013-06-29 00:24:38 +0800260 if (acpi_has_method(handle, "_LCK")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 arg_list.count = 1;
262 arg_list.pointer = &arg;
263 arg.type = ACPI_TYPE_INTEGER;
264 arg.integer.value = 0;
265 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
266 }
267
268 arg_list.count = 1;
269 arg_list.pointer = &arg;
270 arg.type = ACPI_TYPE_INTEGER;
271 arg.integer.value = 1;
272
273 /*
274 * TBD: _EJD support.
275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600277 if (ACPI_FAILURE(status)) {
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100278 if (status == AE_NOT_FOUND) {
279 return -ENODEV;
280 } else {
Toshi Kani882fd122013-03-13 19:29:26 +0000281 acpi_handle_warn(handle, "Eject failed (0x%x)\n",
282 status);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100283 return -EIO;
284 }
285 }
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100286
Toshi Kani882fd122013-03-13 19:29:26 +0000287 /*
288 * Verify if eject was indeed successful. If not, log an error
289 * message. No need to call _OST since _EJ0 call was made OK.
290 */
291 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
292 if (ACPI_FAILURE(status)) {
293 acpi_handle_warn(handle,
294 "Status check after eject failed (0x%x)\n", status);
295 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
296 acpi_handle_warn(handle,
297 "Eject incomplete - status 0x%llx\n", sta);
298 }
299
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100300 return 0;
301}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100302
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100303static void acpi_bus_device_eject(void *context)
304{
305 acpi_handle handle = context;
306 struct acpi_device *device = NULL;
307 struct acpi_scan_handler *handler;
308 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
309
310 mutex_lock(&acpi_scan_lock);
311
312 acpi_bus_get_device(handle, &device);
313 if (!device)
314 goto err_out;
315
316 handler = device->handler;
317 if (!handler || !handler->hotplug.enabled) {
318 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
319 goto err_out;
320 }
321 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
322 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
323 if (handler->hotplug.mode == AHM_CONTAINER) {
324 device->flags.eject_pending = true;
325 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
326 } else {
327 int error;
328
329 get_device(&device->dev);
330 error = acpi_scan_hot_remove(device);
331 if (error)
332 goto err_out;
Toshi Kanic4753e52012-05-23 20:25:20 -0600333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100335 out:
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100336 mutex_unlock(&acpi_scan_lock);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800337 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100338
339 err_out:
340 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
341 NULL);
342 goto out;
343}
344
345static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
346{
347 struct acpi_device *device = NULL;
348 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
349 int error;
350
351 mutex_lock(&acpi_scan_lock);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200352 lock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100353
354 acpi_bus_get_device(handle, &device);
355 if (device) {
356 dev_warn(&device->dev, "Attempt to re-insert\n");
357 goto out;
358 }
359 acpi_evaluate_hotplug_ost(handle, ost_source,
360 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
361 error = acpi_bus_scan(handle);
362 if (error) {
363 acpi_handle_warn(handle, "Namespace scan failure\n");
364 goto out;
365 }
366 error = acpi_bus_get_device(handle, &device);
367 if (error) {
368 acpi_handle_warn(handle, "Missing device node object\n");
369 goto out;
370 }
371 ost_code = ACPI_OST_SC_SUCCESS;
372 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
373 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
374
375 out:
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200376 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100377 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
378 mutex_unlock(&acpi_scan_lock);
379}
380
381static void acpi_scan_bus_check(void *context)
382{
383 acpi_scan_bus_device_check((acpi_handle)context,
384 ACPI_NOTIFY_BUS_CHECK);
385}
386
387static void acpi_scan_device_check(void *context)
388{
389 acpi_scan_bus_device_check((acpi_handle)context,
390 ACPI_NOTIFY_DEVICE_CHECK);
391}
392
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000393static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
394{
395 u32 ost_status;
396
397 switch (type) {
398 case ACPI_NOTIFY_BUS_CHECK:
399 acpi_handle_debug(handle,
400 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
401 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
402 break;
403 case ACPI_NOTIFY_DEVICE_CHECK:
404 acpi_handle_debug(handle,
405 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
406 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
407 break;
408 case ACPI_NOTIFY_EJECT_REQUEST:
409 acpi_handle_debug(handle,
410 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
411 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
412 break;
413 default:
414 /* non-hotplug event; possibly handled by other handler */
415 return;
416 }
417
418 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
419}
420
421static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100422{
423 acpi_osd_exec_callback callback;
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000424 struct acpi_scan_handler *handler = data;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100425 acpi_status status;
426
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000427 if (!handler->hotplug.enabled)
428 return acpi_hotplug_unsupported(handle, type);
429
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100430 switch (type) {
431 case ACPI_NOTIFY_BUS_CHECK:
432 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
433 callback = acpi_scan_bus_check;
434 break;
435 case ACPI_NOTIFY_DEVICE_CHECK:
436 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
437 callback = acpi_scan_device_check;
438 break;
439 case ACPI_NOTIFY_EJECT_REQUEST:
440 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
441 callback = acpi_bus_device_eject;
442 break;
443 default:
444 /* non-hotplug event; possibly handled by other handler */
445 return;
446 }
447 status = acpi_os_hotplug_execute(callback, handle);
448 if (ACPI_FAILURE(status))
449 acpi_evaluate_hotplug_ost(handle, type,
450 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
451 NULL);
452}
453
454/**
455 * acpi_bus_hot_remove_device: hot-remove a device and its children
456 * @context: struct acpi_eject_event pointer (freed in this func)
457 *
458 * Hot-remove a device and its children. This function frees up the
459 * memory space passed by arg context, so that the caller may call
460 * this function asynchronously through acpi_os_hotplug_execute().
461 */
462void acpi_bus_hot_remove_device(void *context)
463{
464 struct acpi_eject_event *ej_event = context;
465 struct acpi_device *device = ej_event->device;
466 acpi_handle handle = device->handle;
467 int error;
468
469 mutex_lock(&acpi_scan_lock);
470
471 error = acpi_scan_hot_remove(device);
472 if (error && handle)
473 acpi_evaluate_hotplug_ost(handle, ej_event->event,
474 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
475 NULL);
476
477 mutex_unlock(&acpi_scan_lock);
478 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
Toshi Kani61622ac2012-11-01 14:42:12 +0000480EXPORT_SYMBOL(acpi_bus_hot_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100482static ssize_t real_power_state_show(struct device *dev,
483 struct device_attribute *attr, char *buf)
484{
485 struct acpi_device *adev = to_acpi_device(dev);
486 int state;
487 int ret;
488
489 ret = acpi_device_get_power(adev, &state);
490 if (ret)
491 return ret;
492
493 return sprintf(buf, "%s\n", acpi_power_state_string(state));
494}
495
496static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
497
498static ssize_t power_state_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
500{
501 struct acpi_device *adev = to_acpi_device(dev);
502
503 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
504}
505
506static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800509acpi_eject_store(struct device *d, struct device_attribute *attr,
510 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800512 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600513 struct acpi_eject_event *ej_event;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100514 acpi_object_type not_used;
515 acpi_status status;
516 u32 ost_source;
517 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100519 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100522 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
523 && !acpi_device->driver)
524 return -ENODEV;
525
526 status = acpi_get_type(acpi_device->handle, &not_used);
527 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
528 return -ENODEV;
529
530 mutex_lock(&acpi_scan_lock);
531
532 if (acpi_device->flags.eject_pending) {
533 /* ACPI eject notification event. */
534 ost_source = ACPI_NOTIFY_EJECT_REQUEST;
535 acpi_device->flags.eject_pending = 0;
536 } else {
537 /* Eject initiated by user space. */
538 ost_source = ACPI_OST_EC_OSPM_EJECT;
539 }
Toshi Kanic4753e52012-05-23 20:25:20 -0600540 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
541 if (!ej_event) {
542 ret = -ENOMEM;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100543 goto err_out;
Toshi Kanic4753e52012-05-23 20:25:20 -0600544 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100545 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
546 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Yinghai Lu5993c462013-01-11 22:40:41 +0000547 ej_event->device = acpi_device;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100548 ej_event->event = ost_source;
549 get_device(&acpi_device->dev);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100550 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
551 if (ACPI_FAILURE(status)) {
552 put_device(&acpi_device->dev);
553 kfree(ej_event);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100554 ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
555 goto err_out;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100556 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100557 ret = count;
558
559 out:
560 mutex_unlock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return ret;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100562
563 err_out:
564 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
565 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
566 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800569static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800571static ssize_t
572acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
573 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600575 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800576}
577static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
578
Lv Zheng923d4a42012-10-30 14:43:26 +0100579static ssize_t acpi_device_uid_show(struct device *dev,
580 struct device_attribute *attr, char *buf)
581{
582 struct acpi_device *acpi_dev = to_acpi_device(dev);
583
584 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
585}
586static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
587
588static ssize_t acpi_device_adr_show(struct device *dev,
589 struct device_attribute *attr, char *buf)
590{
591 struct acpi_device *acpi_dev = to_acpi_device(dev);
592
593 return sprintf(buf, "0x%08x\n",
594 (unsigned int)(acpi_dev->pnp.bus_address));
595}
596static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
597
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800598static ssize_t
599acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
600 struct acpi_device *acpi_dev = to_acpi_device(dev);
601 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
602 int result;
603
604 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600605 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800606 goto end;
607
608 result = sprintf(buf, "%s\n", (char*)path.pointer);
609 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600610end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800611 return result;
612}
613static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
614
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600615/* sysfs file that shows description text from the ACPI _STR method */
616static ssize_t description_show(struct device *dev,
617 struct device_attribute *attr,
618 char *buf) {
619 struct acpi_device *acpi_dev = to_acpi_device(dev);
620 int result;
621
622 if (acpi_dev->pnp.str_obj == NULL)
623 return 0;
624
625 /*
626 * The _STR object contains a Unicode identifier for a device.
627 * We need to convert to utf-8 so it can be displayed.
628 */
629 result = utf16s_to_utf8s(
630 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
631 acpi_dev->pnp.str_obj->buffer.length,
632 UTF16_LITTLE_ENDIAN, buf,
633 PAGE_SIZE);
634
635 buf[result++] = '\n';
636
637 return result;
638}
639static DEVICE_ATTR(description, 0444, description_show, NULL);
640
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100641static ssize_t
642acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
643 char *buf) {
644 struct acpi_device *acpi_dev = to_acpi_device(dev);
645
646 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
647}
648static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
649
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800650static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600652 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800653 acpi_status status;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100654 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800655 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800657 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800658 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800659 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600660 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800661 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600662 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800663 goto end;
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200666 if (!list_empty(&dev->pnp.ids)) {
667 result = device_create_file(&dev->dev, &dev_attr_hid);
668 if (result)
669 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200671 result = device_create_file(&dev->dev, &dev_attr_modalias);
672 if (result)
673 goto end;
674 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200675
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600676 /*
677 * If device has _STR, 'description' file is created
678 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800679 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600680 status = acpi_evaluate_object(dev->handle, "_STR",
681 NULL, &buffer);
682 if (ACPI_FAILURE(status))
683 buffer.pointer = NULL;
684 dev->pnp.str_obj = buffer.pointer;
685 result = device_create_file(&dev->dev, &dev_attr_description);
686 if (result)
687 goto end;
688 }
689
Toshi Kanid4e1a692013-03-04 21:30:41 +0000690 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100691 result = device_create_file(&dev->dev, &dev_attr_adr);
692 if (dev->pnp.unique_id)
693 result = device_create_file(&dev->dev, &dev_attr_uid);
694
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100695 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
696 if (ACPI_SUCCESS(status)) {
697 dev->pnp.sun = (unsigned long)sun;
698 result = device_create_file(&dev->dev, &dev_attr_sun);
699 if (result)
700 goto end;
701 } else {
702 dev->pnp.sun = (unsigned long)-1;
703 }
704
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800705 /*
706 * If device has _EJ0, 'eject' file is created that is used to trigger
707 * hot-removal function from userland.
708 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800709 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800710 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100711 if (result)
712 return result;
713 }
714
715 if (dev->flags.power_manageable) {
716 result = device_create_file(&dev->dev, &dev_attr_power_state);
717 if (result)
718 return result;
719
720 if (dev->power.flags.power_resources)
721 result = device_create_file(&dev->dev,
722 &dev_attr_real_power_state);
723 }
724
Alex Chiang0c526d92009-05-14 08:31:37 -0600725end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800726 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800727}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800729static void acpi_device_remove_files(struct acpi_device *dev)
730{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100731 if (dev->flags.power_manageable) {
732 device_remove_file(&dev->dev, &dev_attr_power_state);
733 if (dev->power.flags.power_resources)
734 device_remove_file(&dev->dev,
735 &dev_attr_real_power_state);
736 }
737
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800738 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600739 * If device has _STR, remove 'description' file
740 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800741 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600742 kfree(dev->pnp.str_obj);
743 device_remove_file(&dev->dev, &dev_attr_description);
744 }
745 /*
746 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800747 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800748 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800749 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800750
Jiang Liu952c63e2013-06-29 00:24:38 +0800751 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100752 device_remove_file(&dev->dev, &dev_attr_sun);
753
Lv Zheng923d4a42012-10-30 14:43:26 +0100754 if (dev->pnp.unique_id)
755 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000756 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100757 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600758 device_remove_file(&dev->dev, &dev_attr_modalias);
759 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600760 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800761 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800762}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800764 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200766
Mika Westerbergcf761af2012-10-31 22:44:41 +0100767static const struct acpi_device_id *__acpi_match_device(
768 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200769{
770 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600771 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200772
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800773 /*
774 * If the device is not present, it is unnecessary to load device
775 * driver for it.
776 */
777 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100778 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800779
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600780 for (id = ids; id->id[0]; id++)
781 list_for_each_entry(hwid, &device->pnp.ids, list)
782 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100783 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200784
Mika Westerbergcf761af2012-10-31 22:44:41 +0100785 return NULL;
786}
787
788/**
789 * acpi_match_device - Match a struct device against a given list of ACPI IDs
790 * @ids: Array of struct acpi_device_id object to match against.
791 * @dev: The device structure to match.
792 *
793 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
794 * object for that handle and use that object to match against a given list of
795 * device IDs.
796 *
797 * Return a pointer to the first matching ID on success or %NULL on failure.
798 */
799const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
800 const struct device *dev)
801{
802 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100803 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100804
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100805 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100806 return NULL;
807
808 return __acpi_match_device(adev, ids);
809}
810EXPORT_SYMBOL_GPL(acpi_match_device);
811
812int acpi_match_device_ids(struct acpi_device *device,
813 const struct acpi_device_id *ids)
814{
815 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200816}
817EXPORT_SYMBOL(acpi_match_device_ids);
818
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100819static void acpi_free_power_resources_lists(struct acpi_device *device)
820{
821 int i;
822
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100823 if (device->wakeup.flags.valid)
824 acpi_power_resources_list_free(&device->wakeup.resources);
825
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100826 if (!device->flags.power_manageable)
827 return;
828
829 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
830 struct acpi_device_power_state *ps = &device->power.states[i];
831 acpi_power_resources_list_free(&ps->resources);
832 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600833}
834
Patrick Mochel1890a972006-12-07 20:56:31 +0800835static void acpi_device_release(struct device *dev)
836{
837 struct acpi_device *acpi_dev = to_acpi_device(dev);
838
Toshi Kanic0af4172013-03-04 21:30:42 +0000839 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100840 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800841 kfree(acpi_dev);
842}
843
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800844static int acpi_bus_match(struct device *dev, struct device_driver *drv)
845{
846 struct acpi_device *acpi_dev = to_acpi_device(dev);
847 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
848
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100849 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100850 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800851}
852
Kay Sievers7eff2e72007-08-14 15:15:12 +0200853static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800854{
855 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200856 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800857
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200858 if (list_empty(&acpi_dev->pnp.ids))
859 return 0;
860
Kay Sievers7eff2e72007-08-14 15:15:12 +0200861 if (add_uevent_var(env, "MODALIAS="))
862 return -ENOMEM;
863 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
864 sizeof(env->buf) - env->buflen);
865 if (len >= (sizeof(env->buf) - env->buflen))
866 return -ENOMEM;
867 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869}
870
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000871static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
872{
873 struct acpi_device *device = data;
874
875 device->driver->ops.notify(device, event);
876}
877
878static acpi_status acpi_device_notify_fixed(void *data)
879{
880 struct acpi_device *device = data;
881
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000882 /* Fixed hardware devices have no handles */
883 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000884 return AE_OK;
885}
886
887static int acpi_device_install_notify_handler(struct acpi_device *device)
888{
889 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000890
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000891 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000892 status =
893 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
894 acpi_device_notify_fixed,
895 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000896 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000897 status =
898 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
899 acpi_device_notify_fixed,
900 device);
901 else
902 status = acpi_install_notify_handler(device->handle,
903 ACPI_DEVICE_NOTIFY,
904 acpi_device_notify,
905 device);
906
907 if (ACPI_FAILURE(status))
908 return -EINVAL;
909 return 0;
910}
911
912static void acpi_device_remove_notify_handler(struct acpi_device *device)
913{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000914 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000915 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
916 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000917 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000918 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
919 acpi_device_notify_fixed);
920 else
921 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
922 acpi_device_notify);
923}
924
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200925static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800926{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800927 struct acpi_device *acpi_dev = to_acpi_device(dev);
928 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
929 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200931 if (acpi_dev->handler)
932 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000933
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200934 if (!acpi_drv->ops.add)
935 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800936
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200937 ret = acpi_drv->ops.add(acpi_dev);
938 if (ret)
939 return ret;
940
941 acpi_dev->driver = acpi_drv;
942 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
943 "Driver [%s] successfully bound to device [%s]\n",
944 acpi_drv->name, acpi_dev->pnp.bus_id));
945
946 if (acpi_drv->ops.notify) {
947 ret = acpi_device_install_notify_handler(acpi_dev);
948 if (ret) {
949 if (acpi_drv->ops.remove)
950 acpi_drv->ops.remove(acpi_dev);
951
952 acpi_dev->driver = NULL;
953 acpi_dev->driver_data = NULL;
954 return ret;
955 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800956 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200957
958 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
959 acpi_drv->name, acpi_dev->pnp.bus_id));
960 get_device(dev);
961 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800962}
963
964static int acpi_device_remove(struct device * dev)
965{
966 struct acpi_device *acpi_dev = to_acpi_device(dev);
967 struct acpi_driver *acpi_drv = acpi_dev->driver;
968
969 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000970 if (acpi_drv->ops.notify)
971 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800972 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100973 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800974 }
975 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700976 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800977
978 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800979 return 0;
980}
981
David Brownell55955aa2007-05-08 00:28:35 -0700982struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800983 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800984 .match = acpi_bus_match,
985 .probe = acpi_device_probe,
986 .remove = acpi_device_remove,
987 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988};
989
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100990int acpi_device_add(struct acpi_device *device,
991 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800993 int result;
994 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
995 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000996
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100997 if (device->handle) {
998 acpi_status status;
999
1000 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
1001 device);
1002 if (ACPI_FAILURE(status)) {
1003 acpi_handle_err(device->handle,
1004 "Unable to attach device data\n");
1005 return -ENODEV;
1006 }
1007 }
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /*
1010 * Linkage
1011 * -------
1012 * Link this device to its parent and siblings.
1013 */
1014 INIT_LIST_HEAD(&device->children);
1015 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001017 INIT_LIST_HEAD(&device->physical_node_list);
1018 mutex_init(&device->physical_node_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001019 INIT_LIST_HEAD(&device->power_dependent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001021 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1022 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001023 pr_err(PREFIX "Memory allocation error\n");
1024 result = -ENOMEM;
1025 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001026 }
1027
Shaohua Li90905892009-04-07 10:24:29 +08001028 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001029 /*
1030 * Find suitable bus_id and instance number in acpi_bus_id_list
1031 * If failed, create one and link it into acpi_bus_id_list
1032 */
1033 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001034 if (!strcmp(acpi_device_bus_id->bus_id,
1035 acpi_device_hid(device))) {
1036 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001037 found = 1;
1038 kfree(new_bus_id);
1039 break;
1040 }
1041 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001042 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001043 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001044 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001045 acpi_device_bus_id->instance_no = 0;
1046 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1047 }
Kay Sievers07944692008-10-30 01:18:59 +01001048 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001049
Len Brown33b57152008-12-15 22:09:26 -05001050 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (device->wakeup.flags.valid)
1054 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001055 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Patrick Mochel1890a972006-12-07 20:56:31 +08001057 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001058 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001059 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001060 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001061 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001062 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001063 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001064 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001065 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001066
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001067 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001068 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001069 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1070 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001071
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001072 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001073
1074 err:
Shaohua Li90905892009-04-07 10:24:29 +08001075 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001076 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001077 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001078 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001079 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001080
1081 err_detach:
1082 acpi_detach_data(device->handle, acpi_bus_data_handler);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001083 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084}
1085
Rafael J. Wysockib17b5372013-01-15 13:23:44 +01001086static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Shaohua Li90905892009-04-07 10:24:29 +08001088 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001089 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001093 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +08001096
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001097 acpi_power_add_remove_device(device, false);
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001098 acpi_device_remove_files(device);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +01001099 if (device->remove)
1100 device->remove(device);
1101
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001102 device_del(&device->dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001103 /*
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001104 * Transition the device to D3cold to drop the reference counts of all
1105 * power resources the device depends on and turn off the ones that have
1106 * no more references.
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001107 */
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001108 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001109 device->handle = NULL;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001110 put_device(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Zhang Rui9e89dde2006-12-07 20:56:16 +08001113/* --------------------------------------------------------------------------
1114 Driver Management
1115 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001117 * acpi_bus_register_driver - register a driver with the ACPI bus
1118 * @driver: driver being registered
1119 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001121 * devices that match the driver's criteria and binds. Returns zero for
1122 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 */
Len Brown4be44fc2005-08-05 00:44:28 -04001124int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Patrick Mochel1890a972006-12-07 20:56:31 +08001126 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001129 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001130 driver->drv.name = driver->name;
1131 driver->drv.bus = &acpi_bus_type;
1132 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Patrick Mochel1890a972006-12-07 20:56:31 +08001134 ret = driver_register(&driver->drv);
1135 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Len Brown4be44fc2005-08-05 00:44:28 -04001138EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001141 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1142 * @driver: driver to unregister
1143 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1145 * devices that match the driver's criteria and unbinds.
1146 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001147void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Patrick Mochel1890a972006-12-07 20:56:31 +08001149 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
Len Brown4be44fc2005-08-05 00:44:28 -04001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152EXPORT_SYMBOL(acpi_bus_unregister_driver);
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/* --------------------------------------------------------------------------
1155 Device Enumeration
1156 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001157static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1158{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001159 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001160 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001161
1162 /*
1163 * Fixed hardware devices do not appear in the namespace and do not
1164 * have handles, but we fabricate acpi_devices for them, so we have
1165 * to deal with them specially.
1166 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001167 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001168 return acpi_root;
1169
1170 do {
1171 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001172 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001173 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1174 } while (acpi_bus_get_device(handle, &device));
1175 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001176}
1177
Len Brownc8f7a622006-07-09 17:22:28 -04001178acpi_status
1179acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1180{
1181 acpi_status status;
1182 acpi_handle tmp;
1183 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1184 union acpi_object *obj;
1185
1186 status = acpi_get_handle(handle, "_EJD", &tmp);
1187 if (ACPI_FAILURE(status))
1188 return status;
1189
1190 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1191 if (ACPI_SUCCESS(status)) {
1192 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001193 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1194 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001195 kfree(buffer.pointer);
1196 }
1197 return status;
1198}
1199EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1200
Bob Moore8e4319c2009-06-29 13:43:27 +08001201void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203
1204 /* TBD */
1205
1206 return;
1207}
1208
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001209static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1210 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001212 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1213 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001215 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001216 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001218 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001219 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001221 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001223 /* _PRW */
1224 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1225 if (ACPI_FAILURE(status)) {
1226 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001227 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001228 }
1229
1230 package = (union acpi_object *)buffer.pointer;
1231
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001232 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001233 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001236 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001237 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (element->type == ACPI_TYPE_PACKAGE) {
1240 if ((element->package.count < 2) ||
1241 (element->package.elements[0].type !=
1242 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001243 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001244 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001245
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001246 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001248 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 (u32) element->package.elements[1].integer.value;
1250 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001251 wakeup->gpe_device = NULL;
1252 wakeup->gpe_number = element->integer.value;
1253 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001254 goto out;
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001258 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001259 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001260
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001261 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001263 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1264 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001265 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001267 if (!list_empty(&wakeup->resources)) {
1268 int sleep_state;
1269
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001270 err = acpi_power_wakeup_list_init(&wakeup->resources,
1271 &sleep_state);
1272 if (err) {
1273 acpi_handle_warn(handle, "Retrieving current states "
1274 "of wakeup power resources failed\n");
1275 acpi_power_resources_list_free(&wakeup->resources);
1276 goto out;
1277 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001278 if (sleep_state < wakeup->sleep_state) {
1279 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1280 "(S%d) by S%d from power resources\n",
1281 (int)wakeup->sleep_state, sleep_state);
1282 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
Lin Mingbba63a22010-12-13 13:39:17 +08001285 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001286
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001287 out:
1288 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001289 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001292static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001294 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001295 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001296 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001297 {"PNP0C0E", 0},
1298 {"", 0},
1299 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001300 acpi_status status;
1301 acpi_event_status event_status;
1302
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001303 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001304
1305 /* Power button, Lid switch always enable wakeup */
1306 if (!acpi_match_device_ids(device, button_device_ids)) {
1307 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001308 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1309 /* Do not use Lid/sleep button for S5 wakeup */
1310 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1311 device->wakeup.sleep_state = ACPI_STATE_S4;
1312 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001313 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001314 return;
1315 }
1316
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001317 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1318 device->wakeup.gpe_number,
1319 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001320 if (status == AE_OK)
1321 device->wakeup.flags.run_wake =
1322 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1323}
1324
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001325static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001326{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001327 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001328
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001329 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001330 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001331 return;
1332
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001333 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1334 &device->wakeup);
1335 if (err) {
1336 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001337 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001341 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001342 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001343 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1344 * system for the ACPI device with the _PRW object.
1345 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1346 * So it is necessary to call _DSW object first. Only when it is not
1347 * present will the _PSW object used.
1348 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001349 err = acpi_device_sleep_wake(device, 0, 0, 0);
1350 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001351 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1352 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001355static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001357 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001358 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1359 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001360 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001361
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001362 INIT_LIST_HEAD(&ps->resources);
1363
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001364 /* Evaluate "_PRx" to get referenced power resources */
1365 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1366 if (ACPI_SUCCESS(status)) {
1367 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001368
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001369 if (buffer.length && package
1370 && package->type == ACPI_TYPE_PACKAGE
1371 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001372 int err = acpi_extract_power_resources(package, 0,
1373 &ps->resources);
1374 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001375 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001376 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001377 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001378 }
1379
1380 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001381 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001382 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001383 ps->flags.explicit_set = 1;
1384
1385 /*
1386 * State is valid if there are means to put the device into it.
1387 * D3hot is only valid if _PR3 present.
1388 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001389 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001390 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1391 ps->flags.valid = 1;
1392 ps->flags.os_accessible = 1;
1393 }
1394
1395 ps->power = -1; /* Unknown - driver assigned */
1396 ps->latency = -1; /* Unknown - driver assigned */
1397}
1398
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001399static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001401 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001403 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001404 if (!acpi_has_method(device->handle, "_PS0") &&
1405 !acpi_has_method(device->handle, "_PR0"))
1406 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001407
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 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001413 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001414 device->power.flags.explicit_get = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001415 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001416 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001419 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001421 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1422 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001424 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Zhang Rui9e89dde2006-12-07 20:56:16 +08001426 /* Set defaults for D0 and D3 states (always valid) */
1427 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1428 device->power.states[ACPI_STATE_D0].power = 100;
1429 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1430 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001432 /* Set D3cold's explicit_set flag if _PS3 exists. */
1433 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1434 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1435
Aaron Lu1399dfc2012-11-21 23:33:40 +01001436 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1437 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1438 device->power.flags.power_resources)
1439 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1440
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001441 if (acpi_bus_init_power(device)) {
1442 acpi_free_power_resources_lists(device);
1443 device->flags.power_manageable = 0;
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001447static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001450 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 device->flags.dynamic_status = 1;
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001454 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 device->flags.removable = 1;
1456
1457 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001458 if (acpi_has_method(device->handle, "_EJD") ||
1459 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001463static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
Len Brown4be44fc2005-08-05 00:44:28 -04001465 char bus_id[5] = { '?', 0 };
1466 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1467 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 /*
1470 * Bus ID
1471 * ------
1472 * The device's Bus ID is simply the object name.
1473 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1474 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001475 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001477 return;
1478 }
1479
1480 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 case ACPI_BUS_TYPE_POWER_BUTTON:
1482 strcpy(device->pnp.bus_id, "PWRF");
1483 break;
1484 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1485 strcpy(device->pnp.bus_id, "SLPF");
1486 break;
1487 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001488 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 /* Clean up trailing underscores (if any) */
1490 for (i = 3; i > 1; i--) {
1491 if (bus_id[i] == '_')
1492 bus_id[i] = '\0';
1493 else
1494 break;
1495 }
1496 strcpy(device->pnp.bus_id, bus_id);
1497 break;
1498 }
1499}
1500
Zhang Rui54735262007-01-11 02:09:09 -05001501/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001502 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001503 *
1504 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1505 * then we can safely call it an ejectable drive bay
1506 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001507static int acpi_bay_match(acpi_handle handle)
1508{
Zhang Rui54735262007-01-11 02:09:09 -05001509 acpi_handle phandle;
1510
Jiang Liu952c63e2013-06-29 00:24:38 +08001511 if (!acpi_has_method(handle, "_EJ0"))
Zhang Rui54735262007-01-11 02:09:09 -05001512 return -ENODEV;
1513
Jiang Liu952c63e2013-06-29 00:24:38 +08001514 if (acpi_has_method(handle, "_GTF") ||
1515 acpi_has_method(handle, "_GTM") ||
1516 acpi_has_method(handle, "_STM") ||
1517 acpi_has_method(handle, "_SDD"))
Zhang Rui54735262007-01-11 02:09:09 -05001518 return 0;
1519
1520 if (acpi_get_parent(handle, &phandle))
1521 return -ENODEV;
1522
Jiang Liu952c63e2013-06-29 00:24:38 +08001523 if (acpi_has_method(phandle, "_GTF") ||
1524 acpi_has_method(phandle, "_GTM") ||
1525 acpi_has_method(phandle, "_STM") ||
1526 acpi_has_method(phandle, "_SDD"))
Zhang Rui54735262007-01-11 02:09:09 -05001527 return 0;
1528
1529 return -ENODEV;
1530}
1531
Frank Seidel3620f2f2007-12-07 13:20:34 +01001532/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001533 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001534 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001535static int acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001536{
1537 acpi_handle tmp;
Toshi Kanid4e1a692013-03-04 21:30:41 +00001538 return acpi_get_handle(handle, "_DCK", &tmp);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001539}
1540
Thomas Renninger620e1122010-10-01 10:54:00 +02001541const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001542{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001543 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001544
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001545 if (list_empty(&device->pnp.ids))
1546 return dummy_hid;
1547
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001548 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1549 return hid->id;
1550}
1551EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001552
Toshi Kanid4e1a692013-03-04 21:30:41 +00001553static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001554{
1555 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001556
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001557 id = kmalloc(sizeof(*id), GFP_KERNEL);
1558 if (!id)
1559 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001560
Thomas Meyer581de592011-08-06 11:32:56 +02001561 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001562 if (!id->id) {
1563 kfree(id);
1564 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001565 }
1566
Toshi Kanid4e1a692013-03-04 21:30:41 +00001567 list_add_tail(&id->list, &pnp->ids);
1568 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001569}
1570
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001571/*
1572 * Old IBM workstations have a DSDT bug wherein the SMBus object
1573 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1574 * prefix. Work around this.
1575 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001576static int acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001577{
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001578 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1579 int result;
1580
1581 if (!dmi_name_in_vendors("IBM"))
1582 return -ENODEV;
1583
1584 /* Look for SMBS object */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001585 result = acpi_get_name(handle, ACPI_SINGLE_NAME, &path);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001586 if (result)
1587 return result;
1588
1589 if (strcmp("SMBS", path.pointer)) {
1590 result = -ENODEV;
1591 goto out;
1592 }
1593
1594 /* Does it have the necessary (but misnamed) methods? */
1595 result = -ENODEV;
Jiang Liu952c63e2013-06-29 00:24:38 +08001596 if (acpi_has_method(handle, "SBI") &&
1597 acpi_has_method(handle, "SBR") &&
1598 acpi_has_method(handle, "SBW"))
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001599 result = 0;
1600out:
1601 kfree(path.pointer);
1602 return result;
1603}
1604
Toshi Kanic0af4172013-03-04 21:30:42 +00001605static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1606 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Len Brown4be44fc2005-08-05 00:44:28 -04001608 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001609 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001610 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001611 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Toshi Kanic0af4172013-03-04 21:30:42 +00001613 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001615 if (handle == ACPI_ROOT_OBJECT) {
1616 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001617 break;
1618 }
1619
Toshi Kanic0af4172013-03-04 21:30:42 +00001620 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001622 pr_err(PREFIX "%s: Error reading device info\n",
1623 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return;
1625 }
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001628 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001629 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001630 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001631 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001632 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001635 pnp->bus_address = info->address;
1636 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
Lv Zhengccf78042012-10-30 14:41:07 +01001638 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001639 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001640 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001641
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001642 kfree(info);
1643
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001644 /*
1645 * Some devices don't reliably have _HIDs & _CIDs, so add
1646 * synthetic HIDs to make sure drivers can find them.
1647 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001648 if (acpi_is_video_device(handle))
1649 acpi_add_id(pnp, ACPI_VIDEO_HID);
1650 else if (ACPI_SUCCESS(acpi_bay_match(handle)))
1651 acpi_add_id(pnp, ACPI_BAY_HID);
1652 else if (ACPI_SUCCESS(acpi_dock_match(handle)))
1653 acpi_add_id(pnp, ACPI_DOCK_HID);
1654 else if (!acpi_ibm_smbus_match(handle))
1655 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1656 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1657 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1658 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1659 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001660 }
Zhang Rui54735262007-01-11 02:09:09 -05001661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 break;
1663 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001664 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 break;
1666 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001667 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001670 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 break;
1672 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001673 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 break;
1675 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001676 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 break;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Toshi Kanic0af4172013-03-04 21:30:42 +00001681void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1682{
1683 struct acpi_hardware_id *id, *tmp;
1684
1685 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1686 kfree(id->id);
1687 kfree(id);
1688 }
1689 kfree(pnp->unique_id);
1690}
1691
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001692void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1693 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001695 INIT_LIST_HEAD(&device->pnp.ids);
1696 device->device_type = type;
1697 device->handle = handle;
1698 device->parent = acpi_bus_get_parent(handle);
1699 STRUCT_TO_INT(device->status) = sta;
1700 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001701 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001702 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001703 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001704 device_initialize(&device->dev);
1705 dev_set_uevent_suppress(&device->dev, true);
1706}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001707
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001708void acpi_device_add_finalize(struct acpi_device *device)
1709{
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001710 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001711 dev_set_uevent_suppress(&device->dev, false);
1712 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713}
1714
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001715static int acpi_add_single_object(struct acpi_device **child,
1716 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001717 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001719 int result;
1720 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001721 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Burman Yan36bcbec2006-12-19 12:56:11 -08001723 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001725 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001726 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001729 acpi_init_device_object(device, handle, type, sta);
1730 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001731 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001733 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001734 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001735 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001736 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001739 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001740 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001741 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1742 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1743 dev_name(&device->dev), (char *) buffer.pointer,
1744 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1745 kfree(buffer.pointer);
1746 *child = device;
1747 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001748}
1749
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001750static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1751 unsigned long long *sta)
1752{
1753 acpi_status status;
1754 acpi_object_type acpi_type;
1755
1756 status = acpi_get_type(handle, &acpi_type);
1757 if (ACPI_FAILURE(status))
1758 return -ENODEV;
1759
1760 switch (acpi_type) {
1761 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1762 case ACPI_TYPE_DEVICE:
1763 *type = ACPI_BUS_TYPE_DEVICE;
1764 status = acpi_bus_get_status_handle(handle, sta);
1765 if (ACPI_FAILURE(status))
1766 return -ENODEV;
1767 break;
1768 case ACPI_TYPE_PROCESSOR:
1769 *type = ACPI_BUS_TYPE_PROCESSOR;
1770 status = acpi_bus_get_status_handle(handle, sta);
1771 if (ACPI_FAILURE(status))
1772 return -ENODEV;
1773 break;
1774 case ACPI_TYPE_THERMAL:
1775 *type = ACPI_BUS_TYPE_THERMAL;
1776 *sta = ACPI_STA_DEFAULT;
1777 break;
1778 case ACPI_TYPE_POWER:
1779 *type = ACPI_BUS_TYPE_POWER;
1780 *sta = ACPI_STA_DEFAULT;
1781 break;
1782 default:
1783 return -ENODEV;
1784 }
1785
1786 return 0;
1787}
1788
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001789static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1790 char *idstr,
1791 const struct acpi_device_id **matchid)
1792{
1793 const struct acpi_device_id *devid;
1794
1795 for (devid = handler->ids; devid->id[0]; devid++)
1796 if (!strcmp((char *)devid->id, idstr)) {
1797 if (matchid)
1798 *matchid = devid;
1799
1800 return true;
1801 }
1802
1803 return false;
1804}
1805
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001806static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1807 const struct acpi_device_id **matchid)
1808{
1809 struct acpi_scan_handler *handler;
1810
1811 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1812 if (acpi_scan_handler_matching(handler, idstr, matchid))
1813 return handler;
1814
1815 return NULL;
1816}
1817
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001818void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1819{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001820 if (!!hotplug->enabled == !!val)
1821 return;
1822
1823 mutex_lock(&acpi_scan_lock);
1824
1825 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001826
1827 mutex_unlock(&acpi_scan_lock);
1828}
1829
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001830static void acpi_scan_init_hotplug(acpi_handle handle, int type)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001831{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001832 struct acpi_device_pnp pnp = {};
1833 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001834 struct acpi_scan_handler *handler;
1835
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001836 INIT_LIST_HEAD(&pnp.ids);
1837 acpi_set_pnp_ids(handle, &pnp, type);
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001838
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001839 if (!pnp.type.hardware_id)
Catalin Marinas7a26b532013-05-15 16:49:35 +00001840 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001841
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001842 /*
1843 * This relies on the fact that acpi_install_notify_handler() will not
1844 * install the same notify handler routine twice for the same handle.
1845 */
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001846 list_for_each_entry(hwid, &pnp.ids, list) {
1847 handler = acpi_scan_match_handler(hwid->id, NULL);
1848 if (handler) {
Toshi Kani2cbb14f2013-03-05 22:33:31 +00001849 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1850 acpi_hotplug_notify_cb, handler);
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001851 break;
1852 }
1853 }
1854
Catalin Marinas7a26b532013-05-15 16:49:35 +00001855out:
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001856 acpi_free_pnp_ids(&pnp);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001857}
1858
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001859static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1860 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001862 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001863 int type;
1864 unsigned long long sta;
1865 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001866
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001867 acpi_bus_get_device(handle, &device);
1868 if (device)
1869 goto out;
1870
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001871 result = acpi_bus_type_and_status(handle, &type, &sta);
1872 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001873 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001875 if (type == ACPI_BUS_TYPE_POWER) {
1876 acpi_add_power_resource(handle);
1877 return AE_OK;
1878 }
1879
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001880 acpi_scan_init_hotplug(handle, type);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001881
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001882 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001883 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001884 struct acpi_device_wakeup wakeup;
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001885
Jiang Liu952c63e2013-06-29 00:24:38 +08001886 if (acpi_has_method(handle, "_PRW")) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001887 acpi_bus_extract_wakeup_device_power_package(handle,
1888 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001889 acpi_power_resources_list_free(&wakeup.resources);
1890 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001891 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001894 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001895 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001896 return AE_CTRL_DEPTH;
1897
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001898 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001899 if (!*return_value)
1900 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001901
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001902 return AE_OK;
1903}
1904
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001905static int acpi_scan_attach_handler(struct acpi_device *device)
1906{
1907 struct acpi_hardware_id *hwid;
1908 int ret = 0;
1909
1910 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001911 const struct acpi_device_id *devid;
1912 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001913
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001914 handler = acpi_scan_match_handler(hwid->id, &devid);
1915 if (handler) {
1916 ret = handler->attach(device, devid);
1917 if (ret > 0) {
1918 device->handler = handler;
1919 break;
1920 } else if (ret < 0) {
1921 break;
1922 }
1923 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001924 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001925 return ret;
1926}
1927
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001928static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1929 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001930{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001931 struct acpi_device *device;
1932 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001933 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001934
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001935 /*
1936 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1937 * namespace walks prematurely.
1938 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001939 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001940 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001941
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001942 if (acpi_bus_get_device(handle, &device))
1943 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001944
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001945 ret = acpi_scan_attach_handler(device);
1946 if (ret)
1947 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1948
1949 ret = device_attach(&device->dev);
1950 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001953/**
1954 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1955 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001956 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001957 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1958 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001959 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001960 * If no devices were found, -ENODEV is returned, but it does not mean that
1961 * there has been a real error. There just have been no suitable ACPI objects
1962 * in the table trunk from which the kernel could create a device and add an
1963 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001964 *
1965 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001966 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001967int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001970 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001971
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001972 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001974 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001975
Thomas Renningerd2f66502010-01-29 17:48:51 +01001976 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001977 error = -ENODEV;
1978 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001979 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001980 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01001981
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001982 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001983}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001984EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001986static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1987 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001989 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001991 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001992 struct acpi_scan_handler *dev_handler = device->handler;
1993
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001994 if (dev_handler) {
1995 if (dev_handler->detach)
1996 dev_handler->detach(device);
1997
1998 device->handler = NULL;
1999 } else {
2000 device_release_driver(&device->dev);
2001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002003 return AE_OK;
2004}
2005
2006static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
2007 void *not_used, void **ret_not_used)
2008{
2009 struct acpi_device *device = NULL;
2010
2011 if (!acpi_bus_get_device(handle, &device))
2012 acpi_device_unregister(device);
2013
2014 return AE_OK;
2015}
2016
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002017/**
2018 * acpi_bus_trim - Remove ACPI device node and all of its descendants
2019 * @start: Root of the ACPI device nodes subtree to remove.
2020 *
2021 * Must be called under acpi_scan_lock.
2022 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01002023void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002025 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002026 * Execute acpi_bus_device_detach() as a post-order callback to detach
2027 * all ACPI drivers from the device nodes being removed.
2028 */
2029 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2030 acpi_bus_device_detach, NULL, NULL);
2031 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
2032 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002033 * Execute acpi_bus_remove() as a post-order callback to remove device
2034 * nodes in the given namespace scope.
2035 */
2036 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2037 acpi_bus_remove, NULL, NULL);
2038 acpi_bus_remove(start->handle, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
Kristen Accardiceaba662006-02-23 17:56:01 -08002040EXPORT_SYMBOL_GPL(acpi_bus_trim);
2041
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002042static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Len Brown4be44fc2005-08-05 00:44:28 -04002044 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 /*
2047 * Enumerate all fixed-feature devices.
2048 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002049 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2050 struct acpi_device *device = NULL;
2051
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002052 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002053 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002054 ACPI_STA_DEFAULT);
2055 if (result)
2056 return result;
2057
2058 result = device_attach(&device->dev);
2059 if (result < 0)
2060 return result;
2061
Daniel Drakec10d7a12012-05-10 00:08:43 +01002062 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002065 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2066 struct acpi_device *device = NULL;
2067
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002068 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002069 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002070 ACPI_STA_DEFAULT);
2071 if (result)
2072 return result;
2073
2074 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002077 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Bjorn Helgaase747f272009-03-24 16:49:43 -06002080int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Patrick Mochel5b327262006-05-10 10:33:00 -04002084 result = bus_register(&acpi_bus_type);
2085 if (result) {
2086 /* We don't want to quit even if we failed to add suspend/resume */
2087 printk(KERN_ERR PREFIX "Could not register bus type\n");
2088 }
2089
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002090 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002091 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002092 acpi_processor_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002093 acpi_platform_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002094 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002095 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002096 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002097 acpi_memory_hotplug_init();
Jiang Liu94add0f2013-06-23 00:59:55 +02002098 acpi_dock_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002099
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002100 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 * Enumerate devices in the ACPI namespace.
2103 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002104 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002108 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002112 result = acpi_bus_scan_fixed();
2113 if (result) {
2114 acpi_device_unregister(acpi_root);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002115 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002116 }
2117
2118 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002119
Yinghai Lu668192b2013-01-21 13:20:48 -08002120 acpi_pci_root_hp_init();
2121
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002122 out:
2123 mutex_unlock(&acpi_scan_lock);
2124 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}