blob: 2959fe1ce43e71203ea440279d9869a959ffee1c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040022extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020025#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUS_DEVICE_NAME "System Bus"
27
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020030/*
31 * If set, devices will be hot-removed even if they cannot be put offline
32 * gracefully (from the kernel's standpoint).
33 */
34bool acpi_force_hot_remove;
35
Thomas Renninger620e1122010-10-01 10:54:00 +020036static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2d2006-12-08 17:23:43 +080039static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010040static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010041static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080042DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043LIST_HEAD(acpi_wakeup_device_list);
44
Zhang Ruie49bd2d2006-12-08 17:23:43 +080045struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080046 char bus_id[15];
Zhang Ruie49bd2d2006-12-08 17:23:43 +080047 unsigned int instance_no;
48 struct list_head node;
49};
Thomas Renninger29b71a12007-07-23 14:43:51 +020050
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010051void acpi_scan_lock_acquire(void)
52{
53 mutex_lock(&acpi_scan_lock);
54}
55EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
56
57void acpi_scan_lock_release(void)
58{
59 mutex_unlock(&acpi_scan_lock);
60}
61EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
62
Rafael J. Wysockica589f92013-01-30 14:27:29 +010063int acpi_scan_add_handler(struct acpi_scan_handler *handler)
64{
65 if (!handler || !handler->attach)
66 return -EINVAL;
67
68 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
69 return 0;
70}
71
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010072int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
73 const char *hotplug_profile_name)
74{
75 int error;
76
77 error = acpi_scan_add_handler(handler);
78 if (error)
79 return error;
80
81 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
82 return 0;
83}
84
Thomas Renninger29b71a12007-07-23 14:43:51 +020085/*
86 * Creates hid/cid(s) string needed for modalias and uevent
87 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
88 * char *modalias: "acpi:IBM0001:ACPI0001"
89*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020090static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
91 int size)
92{
Thomas Renninger29b71a12007-07-23 14:43:51 +020093 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080094 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060095 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020096
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020097 if (list_empty(&acpi_dev->pnp.ids))
98 return 0;
99
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800100 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +0200101 size -= len;
102
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600103 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
104 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800105 if (count < 0 || count >= size)
106 return -EINVAL;
107 len += count;
108 size -= count;
109 }
110
Thomas Renninger29b71a12007-07-23 14:43:51 +0200111 modalias[len] = '\0';
112 return len;
113}
114
115static ssize_t
116acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
117 struct acpi_device *acpi_dev = to_acpi_device(dev);
118 int len;
119
120 /* Device has no HID and no CID or string is >1024 */
121 len = create_modalias(acpi_dev, buf, 1024);
122 if (len <= 0)
123 return 0;
124 buf[len++] = '\n';
125 return len;
126}
127static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
128
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200129static acpi_status acpi_bus_offline_companions(acpi_handle handle, u32 lvl,
130 void *data, void **ret_p)
131{
132 struct acpi_device *device = NULL;
133 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200134 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200135 acpi_status status = AE_OK;
136
137 if (acpi_bus_get_device(handle, &device))
138 return AE_OK;
139
140 mutex_lock(&device->physical_node_lock);
141
142 list_for_each_entry(pn, &device->physical_node_list, node) {
143 int ret;
144
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200145 if (second_pass) {
146 /* Skip devices offlined by the first pass. */
147 if (pn->put_online)
148 continue;
149 } else {
150 pn->put_online = false;
151 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200152 ret = device_offline(pn->dev);
153 if (acpi_force_hot_remove)
154 continue;
155
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200156 if (ret >= 0) {
157 pn->put_online = !ret;
158 } else {
159 *ret_p = pn->dev;
160 if (second_pass) {
161 status = AE_ERROR;
162 break;
163 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200164 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200165 }
166
167 mutex_unlock(&device->physical_node_lock);
168
169 return status;
170}
171
172static acpi_status acpi_bus_online_companions(acpi_handle handle, u32 lvl,
173 void *data, void **ret_p)
174{
175 struct acpi_device *device = NULL;
176 struct acpi_device_physical_node *pn;
177
178 if (acpi_bus_get_device(handle, &device))
179 return AE_OK;
180
181 mutex_lock(&device->physical_node_lock);
182
183 list_for_each_entry(pn, &device->physical_node_list, node)
184 if (pn->put_online) {
185 device_online(pn->dev);
186 pn->put_online = false;
187 }
188
189 mutex_unlock(&device->physical_node_lock);
190
191 return AE_OK;
192}
193
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100194static int acpi_scan_hot_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Yinghai Lu5993c462013-01-11 22:40:41 +0000196 acpi_handle handle = device->handle;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100197 acpi_handle not_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct acpi_object_list arg_list;
199 union acpi_object arg;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200200 struct device *errdev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100201 acpi_status status;
Toshi Kani882fd122013-03-13 19:29:26 +0000202 unsigned long long sta;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100203
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100204 /* If there is no handle, the device node has been unregistered. */
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100205 if (!handle) {
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100206 dev_dbg(&device->dev, "ACPI handle missing\n");
207 put_device(&device->dev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100208 return -EINVAL;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100209 }
210
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200211 lock_device_hotplug();
212
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200213 /*
214 * Carry out two passes here and ignore errors in the first pass,
215 * because if the devices in question are memory blocks and
216 * CONFIG_MEMCG is set, one of the blocks may hold data structures
217 * that the other blocks depend on, but it is not known in advance which
218 * block holds them.
219 *
220 * If the first pass is successful, the second one isn't needed, though.
221 */
222 errdev = NULL;
223 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
224 NULL, acpi_bus_offline_companions,
225 (void *)false, (void **)&errdev);
226 acpi_bus_offline_companions(handle, 0, (void *)false, (void **)&errdev);
227 if (errdev) {
228 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200229 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200230 NULL, acpi_bus_offline_companions,
231 (void *)true , (void **)&errdev);
232 if (!errdev || acpi_force_hot_remove)
233 acpi_bus_offline_companions(handle, 0, (void *)true,
234 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200235
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200236 if (errdev && !acpi_force_hot_remove) {
237 dev_warn(errdev, "Offline failed.\n");
238 acpi_bus_online_companions(handle, 0, NULL, NULL);
239 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
240 ACPI_UINT32_MAX,
241 acpi_bus_online_companions, NULL,
242 NULL, NULL);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200243
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200244 unlock_device_hotplug();
245
246 put_device(&device->dev);
247 return -EBUSY;
248 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200249 }
250
Zhang Rui26d46862008-04-29 02:35:48 -0400251 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100252 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400253
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100254 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200255
256 unlock_device_hotplug();
257
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100258 /* Device node has been unregistered. */
259 put_device(&device->dev);
Toshi Kanib3c450c2012-10-26 13:38:57 +0200260 device = NULL;
261
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100262 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &not_used))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 arg_list.count = 1;
264 arg_list.pointer = &arg;
265 arg.type = ACPI_TYPE_INTEGER;
266 arg.integer.value = 0;
267 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
268 }
269
270 arg_list.count = 1;
271 arg_list.pointer = &arg;
272 arg.type = ACPI_TYPE_INTEGER;
273 arg.integer.value = 1;
274
275 /*
276 * TBD: _EJD support.
277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600279 if (ACPI_FAILURE(status)) {
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100280 if (status == AE_NOT_FOUND) {
281 return -ENODEV;
282 } else {
Toshi Kani882fd122013-03-13 19:29:26 +0000283 acpi_handle_warn(handle, "Eject failed (0x%x)\n",
284 status);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100285 return -EIO;
286 }
287 }
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100288
Toshi Kani882fd122013-03-13 19:29:26 +0000289 /*
290 * Verify if eject was indeed successful. If not, log an error
291 * message. No need to call _OST since _EJ0 call was made OK.
292 */
293 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
294 if (ACPI_FAILURE(status)) {
295 acpi_handle_warn(handle,
296 "Status check after eject failed (0x%x)\n", status);
297 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
298 acpi_handle_warn(handle,
299 "Eject incomplete - status 0x%llx\n", sta);
300 }
301
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100302 return 0;
303}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100304
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100305static void acpi_bus_device_eject(void *context)
306{
307 acpi_handle handle = context;
308 struct acpi_device *device = NULL;
309 struct acpi_scan_handler *handler;
310 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
311
312 mutex_lock(&acpi_scan_lock);
313
314 acpi_bus_get_device(handle, &device);
315 if (!device)
316 goto err_out;
317
318 handler = device->handler;
319 if (!handler || !handler->hotplug.enabled) {
320 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
321 goto err_out;
322 }
323 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
324 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
325 if (handler->hotplug.mode == AHM_CONTAINER) {
326 device->flags.eject_pending = true;
327 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
328 } else {
329 int error;
330
331 get_device(&device->dev);
332 error = acpi_scan_hot_remove(device);
333 if (error)
334 goto err_out;
Toshi Kanic4753e52012-05-23 20:25:20 -0600335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100337 out:
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100338 mutex_unlock(&acpi_scan_lock);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800339 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100340
341 err_out:
342 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
343 NULL);
344 goto out;
345}
346
347static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
348{
349 struct acpi_device *device = NULL;
350 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
351 int error;
352
353 mutex_lock(&acpi_scan_lock);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200354 lock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100355
356 acpi_bus_get_device(handle, &device);
357 if (device) {
358 dev_warn(&device->dev, "Attempt to re-insert\n");
359 goto out;
360 }
361 acpi_evaluate_hotplug_ost(handle, ost_source,
362 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
363 error = acpi_bus_scan(handle);
364 if (error) {
365 acpi_handle_warn(handle, "Namespace scan failure\n");
366 goto out;
367 }
368 error = acpi_bus_get_device(handle, &device);
369 if (error) {
370 acpi_handle_warn(handle, "Missing device node object\n");
371 goto out;
372 }
373 ost_code = ACPI_OST_SC_SUCCESS;
374 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
375 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
376
377 out:
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200378 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100379 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
380 mutex_unlock(&acpi_scan_lock);
381}
382
383static void acpi_scan_bus_check(void *context)
384{
385 acpi_scan_bus_device_check((acpi_handle)context,
386 ACPI_NOTIFY_BUS_CHECK);
387}
388
389static void acpi_scan_device_check(void *context)
390{
391 acpi_scan_bus_device_check((acpi_handle)context,
392 ACPI_NOTIFY_DEVICE_CHECK);
393}
394
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000395static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
396{
397 u32 ost_status;
398
399 switch (type) {
400 case ACPI_NOTIFY_BUS_CHECK:
401 acpi_handle_debug(handle,
402 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
403 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
404 break;
405 case ACPI_NOTIFY_DEVICE_CHECK:
406 acpi_handle_debug(handle,
407 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
408 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
409 break;
410 case ACPI_NOTIFY_EJECT_REQUEST:
411 acpi_handle_debug(handle,
412 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
413 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
414 break;
415 default:
416 /* non-hotplug event; possibly handled by other handler */
417 return;
418 }
419
420 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
421}
422
423static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100424{
425 acpi_osd_exec_callback callback;
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000426 struct acpi_scan_handler *handler = data;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100427 acpi_status status;
428
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000429 if (!handler->hotplug.enabled)
430 return acpi_hotplug_unsupported(handle, type);
431
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100432 switch (type) {
433 case ACPI_NOTIFY_BUS_CHECK:
434 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
435 callback = acpi_scan_bus_check;
436 break;
437 case ACPI_NOTIFY_DEVICE_CHECK:
438 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
439 callback = acpi_scan_device_check;
440 break;
441 case ACPI_NOTIFY_EJECT_REQUEST:
442 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
443 callback = acpi_bus_device_eject;
444 break;
445 default:
446 /* non-hotplug event; possibly handled by other handler */
447 return;
448 }
449 status = acpi_os_hotplug_execute(callback, handle);
450 if (ACPI_FAILURE(status))
451 acpi_evaluate_hotplug_ost(handle, type,
452 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
453 NULL);
454}
455
456/**
457 * acpi_bus_hot_remove_device: hot-remove a device and its children
458 * @context: struct acpi_eject_event pointer (freed in this func)
459 *
460 * Hot-remove a device and its children. This function frees up the
461 * memory space passed by arg context, so that the caller may call
462 * this function asynchronously through acpi_os_hotplug_execute().
463 */
464void acpi_bus_hot_remove_device(void *context)
465{
466 struct acpi_eject_event *ej_event = context;
467 struct acpi_device *device = ej_event->device;
468 acpi_handle handle = device->handle;
469 int error;
470
471 mutex_lock(&acpi_scan_lock);
472
473 error = acpi_scan_hot_remove(device);
474 if (error && handle)
475 acpi_evaluate_hotplug_ost(handle, ej_event->event,
476 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
477 NULL);
478
479 mutex_unlock(&acpi_scan_lock);
480 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
Toshi Kani61622ac2012-11-01 14:42:12 +0000482EXPORT_SYMBOL(acpi_bus_hot_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100484static ssize_t real_power_state_show(struct device *dev,
485 struct device_attribute *attr, char *buf)
486{
487 struct acpi_device *adev = to_acpi_device(dev);
488 int state;
489 int ret;
490
491 ret = acpi_device_get_power(adev, &state);
492 if (ret)
493 return ret;
494
495 return sprintf(buf, "%s\n", acpi_power_state_string(state));
496}
497
498static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
499
500static ssize_t power_state_show(struct device *dev,
501 struct device_attribute *attr, char *buf)
502{
503 struct acpi_device *adev = to_acpi_device(dev);
504
505 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
506}
507
508static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800511acpi_eject_store(struct device *d, struct device_attribute *attr,
512 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800514 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600515 struct acpi_eject_event *ej_event;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100516 acpi_object_type not_used;
517 acpi_status status;
518 u32 ost_source;
519 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100521 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100524 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
525 && !acpi_device->driver)
526 return -ENODEV;
527
528 status = acpi_get_type(acpi_device->handle, &not_used);
529 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
530 return -ENODEV;
531
532 mutex_lock(&acpi_scan_lock);
533
534 if (acpi_device->flags.eject_pending) {
535 /* ACPI eject notification event. */
536 ost_source = ACPI_NOTIFY_EJECT_REQUEST;
537 acpi_device->flags.eject_pending = 0;
538 } else {
539 /* Eject initiated by user space. */
540 ost_source = ACPI_OST_EC_OSPM_EJECT;
541 }
Toshi Kanic4753e52012-05-23 20:25:20 -0600542 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
543 if (!ej_event) {
544 ret = -ENOMEM;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100545 goto err_out;
Toshi Kanic4753e52012-05-23 20:25:20 -0600546 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100547 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
548 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Yinghai Lu5993c462013-01-11 22:40:41 +0000549 ej_event->device = acpi_device;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100550 ej_event->event = ost_source;
551 get_device(&acpi_device->dev);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100552 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
553 if (ACPI_FAILURE(status)) {
554 put_device(&acpi_device->dev);
555 kfree(ej_event);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100556 ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
557 goto err_out;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100558 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100559 ret = count;
560
561 out:
562 mutex_unlock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return ret;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100564
565 err_out:
566 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
567 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
568 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800571static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800573static ssize_t
574acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
575 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600577 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800578}
579static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
580
Lv Zheng923d4a42012-10-30 14:43:26 +0100581static ssize_t acpi_device_uid_show(struct device *dev,
582 struct device_attribute *attr, char *buf)
583{
584 struct acpi_device *acpi_dev = to_acpi_device(dev);
585
586 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
587}
588static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
589
590static ssize_t acpi_device_adr_show(struct device *dev,
591 struct device_attribute *attr, char *buf)
592{
593 struct acpi_device *acpi_dev = to_acpi_device(dev);
594
595 return sprintf(buf, "0x%08x\n",
596 (unsigned int)(acpi_dev->pnp.bus_address));
597}
598static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
599
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800600static ssize_t
601acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
602 struct acpi_device *acpi_dev = to_acpi_device(dev);
603 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
604 int result;
605
606 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600607 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800608 goto end;
609
610 result = sprintf(buf, "%s\n", (char*)path.pointer);
611 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600612end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800613 return result;
614}
615static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
616
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600617/* sysfs file that shows description text from the ACPI _STR method */
618static ssize_t description_show(struct device *dev,
619 struct device_attribute *attr,
620 char *buf) {
621 struct acpi_device *acpi_dev = to_acpi_device(dev);
622 int result;
623
624 if (acpi_dev->pnp.str_obj == NULL)
625 return 0;
626
627 /*
628 * The _STR object contains a Unicode identifier for a device.
629 * We need to convert to utf-8 so it can be displayed.
630 */
631 result = utf16s_to_utf8s(
632 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
633 acpi_dev->pnp.str_obj->buffer.length,
634 UTF16_LITTLE_ENDIAN, buf,
635 PAGE_SIZE);
636
637 buf[result++] = '\n';
638
639 return result;
640}
641static DEVICE_ATTR(description, 0444, description_show, NULL);
642
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100643static ssize_t
644acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
645 char *buf) {
646 struct acpi_device *acpi_dev = to_acpi_device(dev);
647
648 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
649}
650static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
651
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800652static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600654 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800655 acpi_status status;
656 acpi_handle temp;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100657 unsigned long long sun;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800658 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800660 /*
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800661 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800662 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600663 if (dev->handle) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800664 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600665 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800666 goto end;
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200669 if (!list_empty(&dev->pnp.ids)) {
670 result = device_create_file(&dev->dev, &dev_attr_hid);
671 if (result)
672 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200674 result = device_create_file(&dev->dev, &dev_attr_modalias);
675 if (result)
676 goto end;
677 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200678
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600679 /*
680 * If device has _STR, 'description' file is created
681 */
682 status = acpi_get_handle(dev->handle, "_STR", &temp);
683 if (ACPI_SUCCESS(status)) {
684 status = acpi_evaluate_object(dev->handle, "_STR",
685 NULL, &buffer);
686 if (ACPI_FAILURE(status))
687 buffer.pointer = NULL;
688 dev->pnp.str_obj = buffer.pointer;
689 result = device_create_file(&dev->dev, &dev_attr_description);
690 if (result)
691 goto end;
692 }
693
Toshi Kanid4e1a692013-03-04 21:30:41 +0000694 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100695 result = device_create_file(&dev->dev, &dev_attr_adr);
696 if (dev->pnp.unique_id)
697 result = device_create_file(&dev->dev, &dev_attr_uid);
698
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100699 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
700 if (ACPI_SUCCESS(status)) {
701 dev->pnp.sun = (unsigned long)sun;
702 result = device_create_file(&dev->dev, &dev_attr_sun);
703 if (result)
704 goto end;
705 } else {
706 dev->pnp.sun = (unsigned long)-1;
707 }
708
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800709 /*
710 * If device has _EJ0, 'eject' file is created that is used to trigger
711 * hot-removal function from userland.
712 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800713 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100714 if (ACPI_SUCCESS(status)) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800715 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100716 if (result)
717 return result;
718 }
719
720 if (dev->flags.power_manageable) {
721 result = device_create_file(&dev->dev, &dev_attr_power_state);
722 if (result)
723 return result;
724
725 if (dev->power.flags.power_resources)
726 result = device_create_file(&dev->dev,
727 &dev_attr_real_power_state);
728 }
729
Alex Chiang0c526d92009-05-14 08:31:37 -0600730end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800731 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800732}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800734static void acpi_device_remove_files(struct acpi_device *dev)
735{
736 acpi_status status;
737 acpi_handle temp;
738
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100739 if (dev->flags.power_manageable) {
740 device_remove_file(&dev->dev, &dev_attr_power_state);
741 if (dev->power.flags.power_resources)
742 device_remove_file(&dev->dev,
743 &dev_attr_real_power_state);
744 }
745
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800746 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600747 * If device has _STR, remove 'description' file
748 */
749 status = acpi_get_handle(dev->handle, "_STR", &temp);
750 if (ACPI_SUCCESS(status)) {
751 kfree(dev->pnp.str_obj);
752 device_remove_file(&dev->dev, &dev_attr_description);
753 }
754 /*
755 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800756 */
757 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
758 if (ACPI_SUCCESS(status))
759 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800760
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100761 status = acpi_get_handle(dev->handle, "_SUN", &temp);
762 if (ACPI_SUCCESS(status))
763 device_remove_file(&dev->dev, &dev_attr_sun);
764
Lv Zheng923d4a42012-10-30 14:43:26 +0100765 if (dev->pnp.unique_id)
766 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000767 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100768 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600769 device_remove_file(&dev->dev, &dev_attr_modalias);
770 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600771 if (dev->handle)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800772 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800773}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800775 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200777
Mika Westerbergcf761af2012-10-31 22:44:41 +0100778static const struct acpi_device_id *__acpi_match_device(
779 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200780{
781 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600782 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200783
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800784 /*
785 * If the device is not present, it is unnecessary to load device
786 * driver for it.
787 */
788 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100789 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800790
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600791 for (id = ids; id->id[0]; id++)
792 list_for_each_entry(hwid, &device->pnp.ids, list)
793 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100794 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200795
Mika Westerbergcf761af2012-10-31 22:44:41 +0100796 return NULL;
797}
798
799/**
800 * acpi_match_device - Match a struct device against a given list of ACPI IDs
801 * @ids: Array of struct acpi_device_id object to match against.
802 * @dev: The device structure to match.
803 *
804 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
805 * object for that handle and use that object to match against a given list of
806 * device IDs.
807 *
808 * Return a pointer to the first matching ID on success or %NULL on failure.
809 */
810const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
811 const struct device *dev)
812{
813 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100814 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100815
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100816 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100817 return NULL;
818
819 return __acpi_match_device(adev, ids);
820}
821EXPORT_SYMBOL_GPL(acpi_match_device);
822
823int acpi_match_device_ids(struct acpi_device *device,
824 const struct acpi_device_id *ids)
825{
826 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200827}
828EXPORT_SYMBOL(acpi_match_device_ids);
829
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100830static void acpi_free_power_resources_lists(struct acpi_device *device)
831{
832 int i;
833
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100834 if (device->wakeup.flags.valid)
835 acpi_power_resources_list_free(&device->wakeup.resources);
836
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100837 if (!device->flags.power_manageable)
838 return;
839
840 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
841 struct acpi_device_power_state *ps = &device->power.states[i];
842 acpi_power_resources_list_free(&ps->resources);
843 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600844}
845
Patrick Mochel1890a972006-12-07 20:56:31 +0800846static void acpi_device_release(struct device *dev)
847{
848 struct acpi_device *acpi_dev = to_acpi_device(dev);
849
Toshi Kanic0af4172013-03-04 21:30:42 +0000850 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100851 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800852 kfree(acpi_dev);
853}
854
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800855static int acpi_bus_match(struct device *dev, struct device_driver *drv)
856{
857 struct acpi_device *acpi_dev = to_acpi_device(dev);
858 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
859
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100860 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d4102012-12-21 00:36:39 +0100861 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800862}
863
Kay Sievers7eff2e72007-08-14 15:15:12 +0200864static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800865{
866 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200867 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800868
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200869 if (list_empty(&acpi_dev->pnp.ids))
870 return 0;
871
Kay Sievers7eff2e72007-08-14 15:15:12 +0200872 if (add_uevent_var(env, "MODALIAS="))
873 return -ENOMEM;
874 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
875 sizeof(env->buf) - env->buflen);
876 if (len >= (sizeof(env->buf) - env->buflen))
877 return -ENOMEM;
878 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return 0;
880}
881
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000882static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
883{
884 struct acpi_device *device = data;
885
886 device->driver->ops.notify(device, event);
887}
888
889static acpi_status acpi_device_notify_fixed(void *data)
890{
891 struct acpi_device *device = data;
892
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000893 /* Fixed hardware devices have no handles */
894 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000895 return AE_OK;
896}
897
898static int acpi_device_install_notify_handler(struct acpi_device *device)
899{
900 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000901
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000902 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000903 status =
904 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
905 acpi_device_notify_fixed,
906 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000907 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000908 status =
909 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
910 acpi_device_notify_fixed,
911 device);
912 else
913 status = acpi_install_notify_handler(device->handle,
914 ACPI_DEVICE_NOTIFY,
915 acpi_device_notify,
916 device);
917
918 if (ACPI_FAILURE(status))
919 return -EINVAL;
920 return 0;
921}
922
923static void acpi_device_remove_notify_handler(struct acpi_device *device)
924{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000925 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000926 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
927 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000928 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000929 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
930 acpi_device_notify_fixed);
931 else
932 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
933 acpi_device_notify);
934}
935
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800936static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800937static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800938{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800939 struct acpi_device *acpi_dev = to_acpi_device(dev);
940 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
941 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800943 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
944 if (!ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000945 if (acpi_drv->ops.notify) {
946 ret = acpi_device_install_notify_handler(acpi_dev);
947 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000948 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100949 acpi_drv->ops.remove(acpi_dev);
Toshi Kani5f27ee82013-02-07 21:19:13 +0000950 acpi_dev->driver = NULL;
951 acpi_dev->driver_data = NULL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000952 return ret;
953 }
954 }
955
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800956 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
957 "Found driver [%s] for device [%s]\n",
958 acpi_drv->name, acpi_dev->pnp.bus_id));
959 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800960 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800961 return ret;
962}
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 Ruie49bd2d2006-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 Ruie49bd2d2006-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 Ruie49bd2d2006-12-08 17:23:43 +08001026 }
1027
Shaohua Li90905892009-04-07 10:24:29 +08001028 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2d2006-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 Ruie49bd2d2006-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 Ruie49bd2d2006-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 Ruie49bd2d2006-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 Ruie49bd2d2006-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 Ruie49bd2d2006-12-08 17:23:43 +08001065 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001066
Zhang Ruie49bd2d2006-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 Ruie49bd2d2006-12-08 17:23:43 +08001071
Zhang Ruie49bd2d2006-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 Ruie49bd2d2006-12-08 17:23:43 +08001077 list_del(&device->node);
Zhang Ruie49bd2d2006-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 Ruie49bd2d2006-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_driver_init - add a device to a driver
1118 * @device: the device to add and initialize
1119 * @driver: driver for the device
1120 *
Alex Chiang0c526d92009-05-14 08:31:37 -06001121 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +08001122 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 */
1124static int
Len Brown4be44fc2005-08-05 00:44:28 -04001125acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Len Brown4be44fc2005-08-05 00:44:28 -04001127 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -04001130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -04001133 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 result = driver->ops.add(device);
1136 if (result) {
1137 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001138 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -04001139 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141
1142 device->driver = driver;
1143
1144 /*
1145 * TBD - Configuration Management: Assign resources to device based
1146 * upon possible configuration and currently allocated resources.
1147 */
1148
Len Brown4be44fc2005-08-05 00:44:28 -04001149 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1150 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001151 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001152}
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001155 * acpi_bus_register_driver - register a driver with the ACPI bus
1156 * @driver: driver being registered
1157 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001159 * devices that match the driver's criteria and binds. Returns zero for
1160 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
Len Brown4be44fc2005-08-05 00:44:28 -04001162int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Patrick Mochel1890a972006-12-07 20:56:31 +08001164 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001167 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001168 driver->drv.name = driver->name;
1169 driver->drv.bus = &acpi_bus_type;
1170 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Patrick Mochel1890a972006-12-07 20:56:31 +08001172 ret = driver_register(&driver->drv);
1173 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Len Brown4be44fc2005-08-05 00:44:28 -04001176EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001179 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1180 * @driver: driver to unregister
1181 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1183 * devices that match the driver's criteria and unbinds.
1184 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001185void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Patrick Mochel1890a972006-12-07 20:56:31 +08001187 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
Len Brown4be44fc2005-08-05 00:44:28 -04001189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190EXPORT_SYMBOL(acpi_bus_unregister_driver);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192/* --------------------------------------------------------------------------
1193 Device Enumeration
1194 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001195static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1196{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001197 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001198 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001199
1200 /*
1201 * Fixed hardware devices do not appear in the namespace and do not
1202 * have handles, but we fabricate acpi_devices for them, so we have
1203 * to deal with them specially.
1204 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001205 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001206 return acpi_root;
1207
1208 do {
1209 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001210 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001211 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1212 } while (acpi_bus_get_device(handle, &device));
1213 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001214}
1215
Len Brownc8f7a622006-07-09 17:22:28 -04001216acpi_status
1217acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1218{
1219 acpi_status status;
1220 acpi_handle tmp;
1221 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1222 union acpi_object *obj;
1223
1224 status = acpi_get_handle(handle, "_EJD", &tmp);
1225 if (ACPI_FAILURE(status))
1226 return status;
1227
1228 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1229 if (ACPI_SUCCESS(status)) {
1230 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001231 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1232 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001233 kfree(buffer.pointer);
1234 }
1235 return status;
1236}
1237EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1238
Bob Moore8e4319c2009-06-29 13:43:27 +08001239void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241
1242 /* TBD */
1243
1244 return;
1245}
1246
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001247static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1248 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001250 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1251 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001253 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001254 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001256 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001257 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001259 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001261 /* _PRW */
1262 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1263 if (ACPI_FAILURE(status)) {
1264 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001265 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001266 }
1267
1268 package = (union acpi_object *)buffer.pointer;
1269
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001270 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001271 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001274 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001275 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (element->type == ACPI_TYPE_PACKAGE) {
1278 if ((element->package.count < 2) ||
1279 (element->package.elements[0].type !=
1280 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001281 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001282 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001283
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001284 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001286 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 (u32) element->package.elements[1].integer.value;
1288 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001289 wakeup->gpe_device = NULL;
1290 wakeup->gpe_number = element->integer.value;
1291 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001292 goto out;
1293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001296 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001297 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001298
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001299 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001301 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1302 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001303 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001305 if (!list_empty(&wakeup->resources)) {
1306 int sleep_state;
1307
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001308 err = acpi_power_wakeup_list_init(&wakeup->resources,
1309 &sleep_state);
1310 if (err) {
1311 acpi_handle_warn(handle, "Retrieving current states "
1312 "of wakeup power resources failed\n");
1313 acpi_power_resources_list_free(&wakeup->resources);
1314 goto out;
1315 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001316 if (sleep_state < wakeup->sleep_state) {
1317 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1318 "(S%d) by S%d from power resources\n",
1319 (int)wakeup->sleep_state, sleep_state);
1320 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
Lin Mingbba63a22010-12-13 13:39:17 +08001323 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001324
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001325 out:
1326 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001327 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001330static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001332 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001333 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001334 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001335 {"PNP0C0E", 0},
1336 {"", 0},
1337 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001338 acpi_status status;
1339 acpi_event_status event_status;
1340
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001341 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001342
1343 /* Power button, Lid switch always enable wakeup */
1344 if (!acpi_match_device_ids(device, button_device_ids)) {
1345 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001346 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1347 /* Do not use Lid/sleep button for S5 wakeup */
1348 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1349 device->wakeup.sleep_state = ACPI_STATE_S4;
1350 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001351 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001352 return;
1353 }
1354
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001355 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1356 device->wakeup.gpe_number,
1357 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001358 if (status == AE_OK)
1359 device->wakeup.flags.run_wake =
1360 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1361}
1362
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001363static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001364{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001365 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001366 acpi_status status = 0;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001367 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001368
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001369 /* Presence of _PRW indicates wake capable */
1370 status = acpi_get_handle(device->handle, "_PRW", &temp);
1371 if (ACPI_FAILURE(status))
1372 return;
1373
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001374 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1375 &device->wakeup);
1376 if (err) {
1377 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001378 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001382 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001383 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001384 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1385 * system for the ACPI device with the _PRW object.
1386 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1387 * So it is necessary to call _DSW object first. Only when it is not
1388 * present will the _PSW object used.
1389 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001390 err = acpi_device_sleep_wake(device, 0, 0, 0);
1391 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001392 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1393 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001396static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001398 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001399 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1400 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001401 acpi_handle handle;
1402 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001403
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001404 INIT_LIST_HEAD(&ps->resources);
1405
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001406 /* Evaluate "_PRx" to get referenced power resources */
1407 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1408 if (ACPI_SUCCESS(status)) {
1409 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001410
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001411 if (buffer.length && package
1412 && package->type == ACPI_TYPE_PACKAGE
1413 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001414 int err = acpi_extract_power_resources(package, 0,
1415 &ps->resources);
1416 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001417 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001418 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001419 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001420 }
1421
1422 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001423 pathname[2] = 'S';
1424 status = acpi_get_handle(device->handle, pathname, &handle);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001425 if (ACPI_SUCCESS(status))
1426 ps->flags.explicit_set = 1;
1427
1428 /*
1429 * State is valid if there are means to put the device into it.
1430 * D3hot is only valid if _PR3 present.
1431 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001432 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001433 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1434 ps->flags.valid = 1;
1435 ps->flags.os_accessible = 1;
1436 }
1437
1438 ps->power = -1; /* Unknown - driver assigned */
1439 ps->latency = -1; /* Unknown - driver assigned */
1440}
1441
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001442static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001444 acpi_status status;
1445 acpi_handle handle;
1446 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001448 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1449 status = acpi_get_handle(device->handle, "_PS0", &handle);
1450 if (ACPI_FAILURE(status)) {
1451 status = acpi_get_handle(device->handle, "_PR0", &handle);
1452 if (ACPI_FAILURE(status))
1453 return;
1454 }
1455
1456 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001459 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 */
Zhang Rui9e89dde2006-12-07 20:56:16 +08001461 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001463 device->power.flags.explicit_get = 1;
1464 status = acpi_get_handle(device->handle, "_IRC", &handle);
1465 if (ACPI_SUCCESS(status))
1466 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001469 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001471 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1472 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001474 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Zhang Rui9e89dde2006-12-07 20:56:16 +08001476 /* Set defaults for D0 and D3 states (always valid) */
1477 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1478 device->power.states[ACPI_STATE_D0].power = 100;
1479 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1480 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001482 /* Set D3cold's explicit_set flag if _PS3 exists. */
1483 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1484 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1485
Aaron Lu1399dfc2012-11-21 23:33:40 +01001486 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1487 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1488 device->power.flags.power_resources)
1489 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1490
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001491 if (acpi_bus_init_power(device)) {
1492 acpi_free_power_resources_lists(device);
1493 device->flags.power_manageable = 0;
1494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001497static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
Len Brown4be44fc2005-08-05 00:44:28 -04001499 acpi_status status = AE_OK;
1500 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 /* Presence of _STA indicates 'dynamic_status' */
1503 status = acpi_get_handle(device->handle, "_STA", &temp);
1504 if (ACPI_SUCCESS(status))
1505 device->flags.dynamic_status = 1;
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /* Presence of _RMV indicates 'removable' */
1508 status = acpi_get_handle(device->handle, "_RMV", &temp);
1509 if (ACPI_SUCCESS(status))
1510 device->flags.removable = 1;
1511
1512 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1513 status = acpi_get_handle(device->handle, "_EJD", &temp);
1514 if (ACPI_SUCCESS(status))
1515 device->flags.ejectable = 1;
1516 else {
1517 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1518 if (ACPI_SUCCESS(status))
1519 device->flags.ejectable = 1;
1520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001523static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Len Brown4be44fc2005-08-05 00:44:28 -04001525 char bus_id[5] = { '?', 0 };
1526 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1527 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 /*
1530 * Bus ID
1531 * ------
1532 * The device's Bus ID is simply the object name.
1533 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1534 */
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001535 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001537 return;
1538 }
1539
1540 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 case ACPI_BUS_TYPE_POWER_BUTTON:
1542 strcpy(device->pnp.bus_id, "PWRF");
1543 break;
1544 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1545 strcpy(device->pnp.bus_id, "SLPF");
1546 break;
1547 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001548 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 /* Clean up trailing underscores (if any) */
1550 for (i = 3; i > 1; i--) {
1551 if (bus_id[i] == '_')
1552 bus_id[i] = '\0';
1553 else
1554 break;
1555 }
1556 strcpy(device->pnp.bus_id, bus_id);
1557 break;
1558 }
1559}
1560
Zhang Rui54735262007-01-11 02:09:09 -05001561/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001562 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001563 *
1564 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1565 * then we can safely call it an ejectable drive bay
1566 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001567static int acpi_bay_match(acpi_handle handle)
1568{
Zhang Rui54735262007-01-11 02:09:09 -05001569 acpi_status status;
Zhang Rui54735262007-01-11 02:09:09 -05001570 acpi_handle tmp;
1571 acpi_handle phandle;
1572
Zhang Rui54735262007-01-11 02:09:09 -05001573 status = acpi_get_handle(handle, "_EJ0", &tmp);
1574 if (ACPI_FAILURE(status))
1575 return -ENODEV;
1576
1577 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1578 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1579 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1580 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1581 return 0;
1582
1583 if (acpi_get_parent(handle, &phandle))
1584 return -ENODEV;
1585
1586 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1587 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1588 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1589 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1590 return 0;
1591
1592 return -ENODEV;
1593}
1594
Frank Seidel3620f2f2007-12-07 13:20:34 +01001595/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001596 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001597 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001598static int acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001599{
1600 acpi_handle tmp;
Toshi Kanid4e1a692013-03-04 21:30:41 +00001601 return acpi_get_handle(handle, "_DCK", &tmp);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001602}
1603
Thomas Renninger620e1122010-10-01 10:54:00 +02001604const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001605{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001606 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001607
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001608 if (list_empty(&device->pnp.ids))
1609 return dummy_hid;
1610
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001611 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1612 return hid->id;
1613}
1614EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001615
Toshi Kanid4e1a692013-03-04 21:30:41 +00001616static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001617{
1618 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001619
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001620 id = kmalloc(sizeof(*id), GFP_KERNEL);
1621 if (!id)
1622 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001623
Thomas Meyer581de592011-08-06 11:32:56 +02001624 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001625 if (!id->id) {
1626 kfree(id);
1627 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001628 }
1629
Toshi Kanid4e1a692013-03-04 21:30:41 +00001630 list_add_tail(&id->list, &pnp->ids);
1631 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001632}
1633
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001634/*
1635 * Old IBM workstations have a DSDT bug wherein the SMBus object
1636 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1637 * prefix. Work around this.
1638 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001639static int acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001640{
1641 acpi_handle h_dummy;
1642 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1643 int result;
1644
1645 if (!dmi_name_in_vendors("IBM"))
1646 return -ENODEV;
1647
1648 /* Look for SMBS object */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001649 result = acpi_get_name(handle, ACPI_SINGLE_NAME, &path);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001650 if (result)
1651 return result;
1652
1653 if (strcmp("SMBS", path.pointer)) {
1654 result = -ENODEV;
1655 goto out;
1656 }
1657
1658 /* Does it have the necessary (but misnamed) methods? */
1659 result = -ENODEV;
Toshi Kanid4e1a692013-03-04 21:30:41 +00001660 if (ACPI_SUCCESS(acpi_get_handle(handle, "SBI", &h_dummy)) &&
1661 ACPI_SUCCESS(acpi_get_handle(handle, "SBR", &h_dummy)) &&
1662 ACPI_SUCCESS(acpi_get_handle(handle, "SBW", &h_dummy)))
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001663 result = 0;
1664out:
1665 kfree(path.pointer);
1666 return result;
1667}
1668
Toshi Kanic0af4172013-03-04 21:30:42 +00001669static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1670 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Len Brown4be44fc2005-08-05 00:44:28 -04001672 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001673 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001674 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001675 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Toshi Kanic0af4172013-03-04 21:30:42 +00001677 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001679 if (handle == ACPI_ROOT_OBJECT) {
1680 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001681 break;
1682 }
1683
Toshi Kanic0af4172013-03-04 21:30:42 +00001684 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001686 pr_err(PREFIX "%s: Error reading device info\n",
1687 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return;
1689 }
1690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001692 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001693 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001694 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001695 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001696 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001699 pnp->bus_address = info->address;
1700 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
Lv Zhengccf78042012-10-30 14:41:07 +01001702 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001703 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001704 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001705
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001706 kfree(info);
1707
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001708 /*
1709 * Some devices don't reliably have _HIDs & _CIDs, so add
1710 * synthetic HIDs to make sure drivers can find them.
1711 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001712 if (acpi_is_video_device(handle))
1713 acpi_add_id(pnp, ACPI_VIDEO_HID);
1714 else if (ACPI_SUCCESS(acpi_bay_match(handle)))
1715 acpi_add_id(pnp, ACPI_BAY_HID);
1716 else if (ACPI_SUCCESS(acpi_dock_match(handle)))
1717 acpi_add_id(pnp, ACPI_DOCK_HID);
1718 else if (!acpi_ibm_smbus_match(handle))
1719 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1720 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1721 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1722 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1723 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001724 }
Zhang Rui54735262007-01-11 02:09:09 -05001725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 break;
1727 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001728 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 break;
1730 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001731 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001734 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 break;
1736 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001737 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 break;
1739 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001740 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 break;
1742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Toshi Kanic0af4172013-03-04 21:30:42 +00001745void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1746{
1747 struct acpi_hardware_id *id, *tmp;
1748
1749 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1750 kfree(id->id);
1751 kfree(id);
1752 }
1753 kfree(pnp->unique_id);
1754}
1755
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001756void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1757 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001759 INIT_LIST_HEAD(&device->pnp.ids);
1760 device->device_type = type;
1761 device->handle = handle;
1762 device->parent = acpi_bus_get_parent(handle);
1763 STRUCT_TO_INT(device->status) = sta;
1764 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001765 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001766 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001767 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001768 device_initialize(&device->dev);
1769 dev_set_uevent_suppress(&device->dev, true);
1770}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001771
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001772void acpi_device_add_finalize(struct acpi_device *device)
1773{
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001774 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001775 dev_set_uevent_suppress(&device->dev, false);
1776 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001779static int acpi_add_single_object(struct acpi_device **child,
1780 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001781 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001783 int result;
1784 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001785 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Burman Yan36bcbec2006-12-19 12:56:11 -08001787 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001789 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001790 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001793 acpi_init_device_object(device, handle, type, sta);
1794 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001795 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001797 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001798 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001799 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001800 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001803 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001804 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001805 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1806 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1807 dev_name(&device->dev), (char *) buffer.pointer,
1808 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1809 kfree(buffer.pointer);
1810 *child = device;
1811 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001812}
1813
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001814static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1815 unsigned long long *sta)
1816{
1817 acpi_status status;
1818 acpi_object_type acpi_type;
1819
1820 status = acpi_get_type(handle, &acpi_type);
1821 if (ACPI_FAILURE(status))
1822 return -ENODEV;
1823
1824 switch (acpi_type) {
1825 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1826 case ACPI_TYPE_DEVICE:
1827 *type = ACPI_BUS_TYPE_DEVICE;
1828 status = acpi_bus_get_status_handle(handle, sta);
1829 if (ACPI_FAILURE(status))
1830 return -ENODEV;
1831 break;
1832 case ACPI_TYPE_PROCESSOR:
1833 *type = ACPI_BUS_TYPE_PROCESSOR;
1834 status = acpi_bus_get_status_handle(handle, sta);
1835 if (ACPI_FAILURE(status))
1836 return -ENODEV;
1837 break;
1838 case ACPI_TYPE_THERMAL:
1839 *type = ACPI_BUS_TYPE_THERMAL;
1840 *sta = ACPI_STA_DEFAULT;
1841 break;
1842 case ACPI_TYPE_POWER:
1843 *type = ACPI_BUS_TYPE_POWER;
1844 *sta = ACPI_STA_DEFAULT;
1845 break;
1846 default:
1847 return -ENODEV;
1848 }
1849
1850 return 0;
1851}
1852
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001853static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1854 char *idstr,
1855 const struct acpi_device_id **matchid)
1856{
1857 const struct acpi_device_id *devid;
1858
1859 for (devid = handler->ids; devid->id[0]; devid++)
1860 if (!strcmp((char *)devid->id, idstr)) {
1861 if (matchid)
1862 *matchid = devid;
1863
1864 return true;
1865 }
1866
1867 return false;
1868}
1869
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001870static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1871 const struct acpi_device_id **matchid)
1872{
1873 struct acpi_scan_handler *handler;
1874
1875 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1876 if (acpi_scan_handler_matching(handler, idstr, matchid))
1877 return handler;
1878
1879 return NULL;
1880}
1881
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001882void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1883{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001884 if (!!hotplug->enabled == !!val)
1885 return;
1886
1887 mutex_lock(&acpi_scan_lock);
1888
1889 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001890
1891 mutex_unlock(&acpi_scan_lock);
1892}
1893
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001894static void acpi_scan_init_hotplug(acpi_handle handle, int type)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001895{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001896 struct acpi_device_pnp pnp = {};
1897 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001898 struct acpi_scan_handler *handler;
1899
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001900 INIT_LIST_HEAD(&pnp.ids);
1901 acpi_set_pnp_ids(handle, &pnp, type);
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001902
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001903 if (!pnp.type.hardware_id)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001904 return;
1905
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001906 /*
1907 * This relies on the fact that acpi_install_notify_handler() will not
1908 * install the same notify handler routine twice for the same handle.
1909 */
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001910 list_for_each_entry(hwid, &pnp.ids, list) {
1911 handler = acpi_scan_match_handler(hwid->id, NULL);
1912 if (handler) {
Toshi Kani2cbb14f2013-03-05 22:33:31 +00001913 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1914 acpi_hotplug_notify_cb, handler);
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001915 break;
1916 }
1917 }
1918
1919 acpi_free_pnp_ids(&pnp);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001920}
1921
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001922static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1923 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001925 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001926 int type;
1927 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001928 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001929 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001930
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001931 acpi_bus_get_device(handle, &device);
1932 if (device)
1933 goto out;
1934
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001935 result = acpi_bus_type_and_status(handle, &type, &sta);
1936 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001937 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001939 if (type == ACPI_BUS_TYPE_POWER) {
1940 acpi_add_power_resource(handle);
1941 return AE_OK;
1942 }
1943
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001944 acpi_scan_init_hotplug(handle, type);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001945
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001946 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001947 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001948 struct acpi_device_wakeup wakeup;
1949 acpi_handle temp;
1950
1951 status = acpi_get_handle(handle, "_PRW", &temp);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001952 if (ACPI_SUCCESS(status)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001953 acpi_bus_extract_wakeup_device_power_package(handle,
1954 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001955 acpi_power_resources_list_free(&wakeup.resources);
1956 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001957 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001960 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001961 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001962 return AE_CTRL_DEPTH;
1963
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001964 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001965 if (!*return_value)
1966 *return_value = device;
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001967
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001968 return AE_OK;
1969}
1970
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001971static int acpi_scan_attach_handler(struct acpi_device *device)
1972{
1973 struct acpi_hardware_id *hwid;
1974 int ret = 0;
1975
1976 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001977 const struct acpi_device_id *devid;
1978 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001979
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001980 handler = acpi_scan_match_handler(hwid->id, &devid);
1981 if (handler) {
1982 ret = handler->attach(device, devid);
1983 if (ret > 0) {
1984 device->handler = handler;
1985 break;
1986 } else if (ret < 0) {
1987 break;
1988 }
1989 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001990 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001991 return ret;
1992}
1993
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001994static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1995 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001996{
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001997 struct acpi_device *device;
1998 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001999 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002000
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01002001 /*
2002 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
2003 * namespace walks prematurely.
2004 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002005 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01002006 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00002007
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01002008 if (acpi_bus_get_device(handle, &device))
2009 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01002010
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002011 ret = acpi_scan_attach_handler(device);
2012 if (ret)
2013 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
2014
2015 ret = device_attach(&device->dev);
2016 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002019/**
2020 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2021 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01002022 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002023 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2024 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01002025 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002026 * If no devices were found, -ENODEV is returned, but it does not mean that
2027 * there has been a real error. There just have been no suitable ACPI objects
2028 * in the table trunk from which the kernel could create a device and add an
2029 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002030 *
2031 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01002032 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002033int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07002034{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01002036 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002037
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002038 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01002040 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002041
Thomas Renningerd2f66502010-01-29 17:48:51 +01002042 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01002043 error = -ENODEV;
2044 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01002045 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01002046 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01002047
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01002048 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002049}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002050EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002052static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
2053 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002055 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002057 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002058 struct acpi_scan_handler *dev_handler = device->handler;
2059
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002060 if (dev_handler) {
2061 if (dev_handler->detach)
2062 dev_handler->detach(device);
2063
2064 device->handler = NULL;
2065 } else {
2066 device_release_driver(&device->dev);
2067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002069 return AE_OK;
2070}
2071
2072static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
2073 void *not_used, void **ret_not_used)
2074{
2075 struct acpi_device *device = NULL;
2076
2077 if (!acpi_bus_get_device(handle, &device))
2078 acpi_device_unregister(device);
2079
2080 return AE_OK;
2081}
2082
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002083/**
2084 * acpi_bus_trim - Remove ACPI device node and all of its descendants
2085 * @start: Root of the ACPI device nodes subtree to remove.
2086 *
2087 * Must be called under acpi_scan_lock.
2088 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01002089void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002091 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002092 * Execute acpi_bus_device_detach() as a post-order callback to detach
2093 * all ACPI drivers from the device nodes being removed.
2094 */
2095 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2096 acpi_bus_device_detach, NULL, NULL);
2097 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
2098 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002099 * Execute acpi_bus_remove() as a post-order callback to remove device
2100 * nodes in the given namespace scope.
2101 */
2102 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2103 acpi_bus_remove, NULL, NULL);
2104 acpi_bus_remove(start->handle, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
Kristen Accardiceaba662006-02-23 17:56:01 -08002106EXPORT_SYMBOL_GPL(acpi_bus_trim);
2107
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002108static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Len Brown4be44fc2005-08-05 00:44:28 -04002110 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 /*
2113 * Enumerate all fixed-feature devices.
2114 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002115 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2116 struct acpi_device *device = NULL;
2117
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002118 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002119 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002120 ACPI_STA_DEFAULT);
2121 if (result)
2122 return result;
2123
2124 result = device_attach(&device->dev);
2125 if (result < 0)
2126 return result;
2127
Daniel Drakec10d7a12012-05-10 00:08:43 +01002128 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002131 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2132 struct acpi_device *device = NULL;
2133
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002134 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002135 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002136 ACPI_STA_DEFAULT);
2137 if (result)
2138 return result;
2139
2140 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002143 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Bjorn Helgaase747f272009-03-24 16:49:43 -06002146int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
2148 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Patrick Mochel5b327262006-05-10 10:33:00 -04002150 result = bus_register(&acpi_bus_type);
2151 if (result) {
2152 /* We don't want to quit even if we failed to add suspend/resume */
2153 printk(KERN_ERR PREFIX "Could not register bus type\n");
2154 }
2155
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002156 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002157 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002158 acpi_processor_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002159 acpi_platform_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002160 acpi_lpss_init();
Mika Westerberg13176bb2013-01-17 09:59:33 +00002161 acpi_csrt_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002162 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002163 acpi_memory_hotplug_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002164
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002165 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 * Enumerate devices in the ACPI namespace.
2168 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002169 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002173 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002177 result = acpi_bus_scan_fixed();
2178 if (result) {
2179 acpi_device_unregister(acpi_root);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002180 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002181 }
2182
2183 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002184
Yinghai Lu668192b2013-01-21 13:20:48 -08002185 acpi_pci_root_hp_init();
2186
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002187 out:
2188 mutex_unlock(&acpi_scan_lock);
2189 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}