blob: 4c25c3b7ef811467fa88e8eb2f75e8067f7123d9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040022extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020025#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUS_DEVICE_NAME "System Bus"
27
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020030/*
31 * If set, devices will be hot-removed even if they cannot be put offline
32 * gracefully (from the kernel's standpoint).
33 */
34bool acpi_force_hot_remove;
35
Thomas Renninger620e1122010-10-01 10:54:00 +020036static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020037
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080038static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010039static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010040static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080041DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042LIST_HEAD(acpi_wakeup_device_list);
43
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080044struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080045 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080046 unsigned int instance_no;
47 struct list_head node;
48};
Thomas Renninger29b71a12007-07-23 14:43:51 +020049
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010050void acpi_scan_lock_acquire(void)
51{
52 mutex_lock(&acpi_scan_lock);
53}
54EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
55
56void acpi_scan_lock_release(void)
57{
58 mutex_unlock(&acpi_scan_lock);
59}
60EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
61
Rafael J. Wysockica589f92013-01-30 14:27:29 +010062int acpi_scan_add_handler(struct acpi_scan_handler *handler)
63{
64 if (!handler || !handler->attach)
65 return -EINVAL;
66
67 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
68 return 0;
69}
70
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010071int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
72 const char *hotplug_profile_name)
73{
74 int error;
75
76 error = acpi_scan_add_handler(handler);
77 if (error)
78 return error;
79
80 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
81 return 0;
82}
83
Thomas Renninger29b71a12007-07-23 14:43:51 +020084/*
85 * Creates hid/cid(s) string needed for modalias and uevent
86 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
87 * char *modalias: "acpi:IBM0001:ACPI0001"
88*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020089static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
90 int size)
91{
Thomas Renninger29b71a12007-07-23 14:43:51 +020092 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080093 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060094 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020095
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020096 if (list_empty(&acpi_dev->pnp.ids))
97 return 0;
98
Zhang Rui5c9fcb52008-03-20 16:40:32 +080099 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +0200100 size -= len;
101
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600102 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
103 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800104 if (count < 0 || count >= size)
105 return -EINVAL;
106 len += count;
107 size -= count;
108 }
109
Thomas Renninger29b71a12007-07-23 14:43:51 +0200110 modalias[len] = '\0';
111 return len;
112}
113
114static ssize_t
115acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
116 struct acpi_device *acpi_dev = to_acpi_device(dev);
117 int len;
118
119 /* Device has no HID and no CID or string is >1024 */
120 len = create_modalias(acpi_dev, buf, 1024);
121 if (len <= 0)
122 return 0;
123 buf[len++] = '\n';
124 return len;
125}
126static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
127
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200128static acpi_status acpi_bus_offline_companions(acpi_handle handle, u32 lvl,
129 void *data, void **ret_p)
130{
131 struct acpi_device *device = NULL;
132 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200133 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200134 acpi_status status = AE_OK;
135
136 if (acpi_bus_get_device(handle, &device))
137 return AE_OK;
138
139 mutex_lock(&device->physical_node_lock);
140
141 list_for_each_entry(pn, &device->physical_node_list, node) {
142 int ret;
143
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200144 if (second_pass) {
145 /* Skip devices offlined by the first pass. */
146 if (pn->put_online)
147 continue;
148 } else {
149 pn->put_online = false;
150 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200151 ret = device_offline(pn->dev);
152 if (acpi_force_hot_remove)
153 continue;
154
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200155 if (ret >= 0) {
156 pn->put_online = !ret;
157 } else {
158 *ret_p = pn->dev;
159 if (second_pass) {
160 status = AE_ERROR;
161 break;
162 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200163 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200164 }
165
166 mutex_unlock(&device->physical_node_lock);
167
168 return status;
169}
170
171static acpi_status acpi_bus_online_companions(acpi_handle handle, u32 lvl,
172 void *data, void **ret_p)
173{
174 struct acpi_device *device = NULL;
175 struct acpi_device_physical_node *pn;
176
177 if (acpi_bus_get_device(handle, &device))
178 return AE_OK;
179
180 mutex_lock(&device->physical_node_lock);
181
182 list_for_each_entry(pn, &device->physical_node_list, node)
183 if (pn->put_online) {
184 device_online(pn->dev);
185 pn->put_online = false;
186 }
187
188 mutex_unlock(&device->physical_node_lock);
189
190 return AE_OK;
191}
192
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100193static int acpi_scan_hot_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Yinghai Lu5993c462013-01-11 22:40:41 +0000195 acpi_handle handle = device->handle;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200196 struct device *errdev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100197 acpi_status status;
Toshi Kani882fd122013-03-13 19:29:26 +0000198 unsigned long long sta;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100199
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100200 /* If there is no handle, the device node has been unregistered. */
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100201 if (!handle) {
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100202 dev_dbg(&device->dev, "ACPI handle missing\n");
203 put_device(&device->dev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100204 return -EINVAL;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100205 }
206
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200207 lock_device_hotplug();
208
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200209 /*
210 * Carry out two passes here and ignore errors in the first pass,
211 * because if the devices in question are memory blocks and
212 * CONFIG_MEMCG is set, one of the blocks may hold data structures
213 * that the other blocks depend on, but it is not known in advance which
214 * block holds them.
215 *
216 * If the first pass is successful, the second one isn't needed, though.
217 */
218 errdev = NULL;
219 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
220 NULL, acpi_bus_offline_companions,
221 (void *)false, (void **)&errdev);
222 acpi_bus_offline_companions(handle, 0, (void *)false, (void **)&errdev);
223 if (errdev) {
224 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200225 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200226 NULL, acpi_bus_offline_companions,
227 (void *)true , (void **)&errdev);
228 if (!errdev || acpi_force_hot_remove)
229 acpi_bus_offline_companions(handle, 0, (void *)true,
230 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200231
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200232 if (errdev && !acpi_force_hot_remove) {
233 dev_warn(errdev, "Offline failed.\n");
234 acpi_bus_online_companions(handle, 0, NULL, NULL);
235 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
236 ACPI_UINT32_MAX,
237 acpi_bus_online_companions, NULL,
238 NULL, NULL);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200239
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200240 unlock_device_hotplug();
241
242 put_device(&device->dev);
243 return -EBUSY;
244 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200245 }
246
Zhang Rui26d46862008-04-29 02:35:48 -0400247 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100248 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400249
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100250 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200251
252 unlock_device_hotplug();
253
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100254 /* Device node has been unregistered. */
255 put_device(&device->dev);
Toshi Kanib3c450c2012-10-26 13:38:57 +0200256 device = NULL;
257
Jiang Liu7d2421f2013-06-29 00:24:40 +0800258 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /*
260 * TBD: _EJD support.
261 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800262 status = acpi_evaluate_ej0(handle);
263 if (status == AE_NOT_FOUND)
264 return -ENODEV;
265 else if (ACPI_FAILURE(status))
266 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100267
Toshi Kani882fd122013-03-13 19:29:26 +0000268 /*
269 * Verify if eject was indeed successful. If not, log an error
270 * message. No need to call _OST since _EJ0 call was made OK.
271 */
272 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
273 if (ACPI_FAILURE(status)) {
274 acpi_handle_warn(handle,
275 "Status check after eject failed (0x%x)\n", status);
276 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
277 acpi_handle_warn(handle,
278 "Eject incomplete - status 0x%llx\n", sta);
279 }
280
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100281 return 0;
282}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100283
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100284static void acpi_bus_device_eject(void *context)
285{
286 acpi_handle handle = context;
287 struct acpi_device *device = NULL;
288 struct acpi_scan_handler *handler;
289 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
290
291 mutex_lock(&acpi_scan_lock);
292
293 acpi_bus_get_device(handle, &device);
294 if (!device)
295 goto err_out;
296
297 handler = device->handler;
298 if (!handler || !handler->hotplug.enabled) {
299 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
300 goto err_out;
301 }
302 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
303 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
304 if (handler->hotplug.mode == AHM_CONTAINER) {
305 device->flags.eject_pending = true;
306 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
307 } else {
308 int error;
309
310 get_device(&device->dev);
311 error = acpi_scan_hot_remove(device);
312 if (error)
313 goto err_out;
Toshi Kanic4753e52012-05-23 20:25:20 -0600314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100316 out:
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100317 mutex_unlock(&acpi_scan_lock);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800318 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100319
320 err_out:
321 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
322 NULL);
323 goto out;
324}
325
326static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
327{
328 struct acpi_device *device = NULL;
329 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
330 int error;
331
332 mutex_lock(&acpi_scan_lock);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200333 lock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100334
335 acpi_bus_get_device(handle, &device);
336 if (device) {
337 dev_warn(&device->dev, "Attempt to re-insert\n");
338 goto out;
339 }
340 acpi_evaluate_hotplug_ost(handle, ost_source,
341 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
342 error = acpi_bus_scan(handle);
343 if (error) {
344 acpi_handle_warn(handle, "Namespace scan failure\n");
345 goto out;
346 }
347 error = acpi_bus_get_device(handle, &device);
348 if (error) {
349 acpi_handle_warn(handle, "Missing device node object\n");
350 goto out;
351 }
352 ost_code = ACPI_OST_SC_SUCCESS;
353 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
354 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
355
356 out:
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200357 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100358 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
359 mutex_unlock(&acpi_scan_lock);
360}
361
362static void acpi_scan_bus_check(void *context)
363{
364 acpi_scan_bus_device_check((acpi_handle)context,
365 ACPI_NOTIFY_BUS_CHECK);
366}
367
368static void acpi_scan_device_check(void *context)
369{
370 acpi_scan_bus_device_check((acpi_handle)context,
371 ACPI_NOTIFY_DEVICE_CHECK);
372}
373
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000374static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
375{
376 u32 ost_status;
377
378 switch (type) {
379 case ACPI_NOTIFY_BUS_CHECK:
380 acpi_handle_debug(handle,
381 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
382 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
383 break;
384 case ACPI_NOTIFY_DEVICE_CHECK:
385 acpi_handle_debug(handle,
386 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
387 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
388 break;
389 case ACPI_NOTIFY_EJECT_REQUEST:
390 acpi_handle_debug(handle,
391 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
392 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
393 break;
394 default:
395 /* non-hotplug event; possibly handled by other handler */
396 return;
397 }
398
399 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
400}
401
402static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100403{
404 acpi_osd_exec_callback callback;
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000405 struct acpi_scan_handler *handler = data;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100406 acpi_status status;
407
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000408 if (!handler->hotplug.enabled)
409 return acpi_hotplug_unsupported(handle, type);
410
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100411 switch (type) {
412 case ACPI_NOTIFY_BUS_CHECK:
413 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
414 callback = acpi_scan_bus_check;
415 break;
416 case ACPI_NOTIFY_DEVICE_CHECK:
417 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
418 callback = acpi_scan_device_check;
419 break;
420 case ACPI_NOTIFY_EJECT_REQUEST:
421 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
422 callback = acpi_bus_device_eject;
423 break;
424 default:
425 /* non-hotplug event; possibly handled by other handler */
426 return;
427 }
428 status = acpi_os_hotplug_execute(callback, handle);
429 if (ACPI_FAILURE(status))
430 acpi_evaluate_hotplug_ost(handle, type,
431 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
432 NULL);
433}
434
435/**
436 * acpi_bus_hot_remove_device: hot-remove a device and its children
437 * @context: struct acpi_eject_event pointer (freed in this func)
438 *
439 * Hot-remove a device and its children. This function frees up the
440 * memory space passed by arg context, so that the caller may call
441 * this function asynchronously through acpi_os_hotplug_execute().
442 */
443void acpi_bus_hot_remove_device(void *context)
444{
445 struct acpi_eject_event *ej_event = context;
446 struct acpi_device *device = ej_event->device;
447 acpi_handle handle = device->handle;
448 int error;
449
450 mutex_lock(&acpi_scan_lock);
451
452 error = acpi_scan_hot_remove(device);
453 if (error && handle)
454 acpi_evaluate_hotplug_ost(handle, ej_event->event,
455 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
456 NULL);
457
458 mutex_unlock(&acpi_scan_lock);
459 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
Toshi Kani61622ac2012-11-01 14:42:12 +0000461EXPORT_SYMBOL(acpi_bus_hot_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100463static ssize_t real_power_state_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
465{
466 struct acpi_device *adev = to_acpi_device(dev);
467 int state;
468 int ret;
469
470 ret = acpi_device_get_power(adev, &state);
471 if (ret)
472 return ret;
473
474 return sprintf(buf, "%s\n", acpi_power_state_string(state));
475}
476
477static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
478
479static ssize_t power_state_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
481{
482 struct acpi_device *adev = to_acpi_device(dev);
483
484 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
485}
486
487static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800490acpi_eject_store(struct device *d, struct device_attribute *attr,
491 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800493 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600494 struct acpi_eject_event *ej_event;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100495 acpi_object_type not_used;
496 acpi_status status;
497 u32 ost_source;
498 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100500 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100503 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
504 && !acpi_device->driver)
505 return -ENODEV;
506
507 status = acpi_get_type(acpi_device->handle, &not_used);
508 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
509 return -ENODEV;
510
511 mutex_lock(&acpi_scan_lock);
512
513 if (acpi_device->flags.eject_pending) {
514 /* ACPI eject notification event. */
515 ost_source = ACPI_NOTIFY_EJECT_REQUEST;
516 acpi_device->flags.eject_pending = 0;
517 } else {
518 /* Eject initiated by user space. */
519 ost_source = ACPI_OST_EC_OSPM_EJECT;
520 }
Toshi Kanic4753e52012-05-23 20:25:20 -0600521 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
522 if (!ej_event) {
523 ret = -ENOMEM;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100524 goto err_out;
Toshi Kanic4753e52012-05-23 20:25:20 -0600525 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100526 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
527 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Yinghai Lu5993c462013-01-11 22:40:41 +0000528 ej_event->device = acpi_device;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100529 ej_event->event = ost_source;
530 get_device(&acpi_device->dev);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100531 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
532 if (ACPI_FAILURE(status)) {
533 put_device(&acpi_device->dev);
534 kfree(ej_event);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100535 ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
536 goto err_out;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100537 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100538 ret = count;
539
540 out:
541 mutex_unlock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return ret;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100543
544 err_out:
545 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
546 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
547 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800550static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800552static ssize_t
553acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
554 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600556 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800557}
558static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
559
Lv Zheng923d4a42012-10-30 14:43:26 +0100560static ssize_t acpi_device_uid_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 struct acpi_device *acpi_dev = to_acpi_device(dev);
564
565 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
566}
567static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
568
569static ssize_t acpi_device_adr_show(struct device *dev,
570 struct device_attribute *attr, char *buf)
571{
572 struct acpi_device *acpi_dev = to_acpi_device(dev);
573
574 return sprintf(buf, "0x%08x\n",
575 (unsigned int)(acpi_dev->pnp.bus_address));
576}
577static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
578
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800579static ssize_t
580acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
581 struct acpi_device *acpi_dev = to_acpi_device(dev);
582 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
583 int result;
584
585 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600586 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800587 goto end;
588
589 result = sprintf(buf, "%s\n", (char*)path.pointer);
590 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600591end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800592 return result;
593}
594static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
595
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600596/* sysfs file that shows description text from the ACPI _STR method */
597static ssize_t description_show(struct device *dev,
598 struct device_attribute *attr,
599 char *buf) {
600 struct acpi_device *acpi_dev = to_acpi_device(dev);
601 int result;
602
603 if (acpi_dev->pnp.str_obj == NULL)
604 return 0;
605
606 /*
607 * The _STR object contains a Unicode identifier for a device.
608 * We need to convert to utf-8 so it can be displayed.
609 */
610 result = utf16s_to_utf8s(
611 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
612 acpi_dev->pnp.str_obj->buffer.length,
613 UTF16_LITTLE_ENDIAN, buf,
614 PAGE_SIZE);
615
616 buf[result++] = '\n';
617
618 return result;
619}
620static DEVICE_ATTR(description, 0444, description_show, NULL);
621
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100622static ssize_t
623acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
624 char *buf) {
625 struct acpi_device *acpi_dev = to_acpi_device(dev);
626
627 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
628}
629static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
630
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800631static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600633 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800634 acpi_status status;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100635 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800636 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800638 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800639 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800640 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600641 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800642 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600643 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800644 goto end;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200647 if (!list_empty(&dev->pnp.ids)) {
648 result = device_create_file(&dev->dev, &dev_attr_hid);
649 if (result)
650 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200652 result = device_create_file(&dev->dev, &dev_attr_modalias);
653 if (result)
654 goto end;
655 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200656
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600657 /*
658 * If device has _STR, 'description' file is created
659 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800660 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600661 status = acpi_evaluate_object(dev->handle, "_STR",
662 NULL, &buffer);
663 if (ACPI_FAILURE(status))
664 buffer.pointer = NULL;
665 dev->pnp.str_obj = buffer.pointer;
666 result = device_create_file(&dev->dev, &dev_attr_description);
667 if (result)
668 goto end;
669 }
670
Toshi Kanid4e1a692013-03-04 21:30:41 +0000671 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100672 result = device_create_file(&dev->dev, &dev_attr_adr);
673 if (dev->pnp.unique_id)
674 result = device_create_file(&dev->dev, &dev_attr_uid);
675
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100676 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
677 if (ACPI_SUCCESS(status)) {
678 dev->pnp.sun = (unsigned long)sun;
679 result = device_create_file(&dev->dev, &dev_attr_sun);
680 if (result)
681 goto end;
682 } else {
683 dev->pnp.sun = (unsigned long)-1;
684 }
685
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800686 /*
687 * If device has _EJ0, 'eject' file is created that is used to trigger
688 * hot-removal function from userland.
689 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800690 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800691 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100692 if (result)
693 return result;
694 }
695
696 if (dev->flags.power_manageable) {
697 result = device_create_file(&dev->dev, &dev_attr_power_state);
698 if (result)
699 return result;
700
701 if (dev->power.flags.power_resources)
702 result = device_create_file(&dev->dev,
703 &dev_attr_real_power_state);
704 }
705
Alex Chiang0c526d92009-05-14 08:31:37 -0600706end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800707 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800708}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800710static void acpi_device_remove_files(struct acpi_device *dev)
711{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100712 if (dev->flags.power_manageable) {
713 device_remove_file(&dev->dev, &dev_attr_power_state);
714 if (dev->power.flags.power_resources)
715 device_remove_file(&dev->dev,
716 &dev_attr_real_power_state);
717 }
718
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800719 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600720 * If device has _STR, remove 'description' file
721 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800722 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600723 kfree(dev->pnp.str_obj);
724 device_remove_file(&dev->dev, &dev_attr_description);
725 }
726 /*
727 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800728 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800729 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800730 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800731
Jiang Liu952c63e2013-06-29 00:24:38 +0800732 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100733 device_remove_file(&dev->dev, &dev_attr_sun);
734
Lv Zheng923d4a42012-10-30 14:43:26 +0100735 if (dev->pnp.unique_id)
736 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000737 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100738 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600739 device_remove_file(&dev->dev, &dev_attr_modalias);
740 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600741 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800742 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800743}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800745 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200747
Mika Westerbergcf761af2012-10-31 22:44:41 +0100748static const struct acpi_device_id *__acpi_match_device(
749 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200750{
751 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600752 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200753
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800754 /*
755 * If the device is not present, it is unnecessary to load device
756 * driver for it.
757 */
758 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100759 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800760
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600761 for (id = ids; id->id[0]; id++)
762 list_for_each_entry(hwid, &device->pnp.ids, list)
763 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100764 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200765
Mika Westerbergcf761af2012-10-31 22:44:41 +0100766 return NULL;
767}
768
769/**
770 * acpi_match_device - Match a struct device against a given list of ACPI IDs
771 * @ids: Array of struct acpi_device_id object to match against.
772 * @dev: The device structure to match.
773 *
774 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
775 * object for that handle and use that object to match against a given list of
776 * device IDs.
777 *
778 * Return a pointer to the first matching ID on success or %NULL on failure.
779 */
780const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
781 const struct device *dev)
782{
783 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100784 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100785
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100786 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100787 return NULL;
788
789 return __acpi_match_device(adev, ids);
790}
791EXPORT_SYMBOL_GPL(acpi_match_device);
792
793int acpi_match_device_ids(struct acpi_device *device,
794 const struct acpi_device_id *ids)
795{
796 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200797}
798EXPORT_SYMBOL(acpi_match_device_ids);
799
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100800static void acpi_free_power_resources_lists(struct acpi_device *device)
801{
802 int i;
803
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100804 if (device->wakeup.flags.valid)
805 acpi_power_resources_list_free(&device->wakeup.resources);
806
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100807 if (!device->flags.power_manageable)
808 return;
809
810 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
811 struct acpi_device_power_state *ps = &device->power.states[i];
812 acpi_power_resources_list_free(&ps->resources);
813 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600814}
815
Patrick Mochel1890a972006-12-07 20:56:31 +0800816static void acpi_device_release(struct device *dev)
817{
818 struct acpi_device *acpi_dev = to_acpi_device(dev);
819
Toshi Kanic0af4172013-03-04 21:30:42 +0000820 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100821 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800822 kfree(acpi_dev);
823}
824
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800825static int acpi_bus_match(struct device *dev, struct device_driver *drv)
826{
827 struct acpi_device *acpi_dev = to_acpi_device(dev);
828 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
829
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100830 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100831 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800832}
833
Kay Sievers7eff2e72007-08-14 15:15:12 +0200834static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800835{
836 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200837 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800838
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200839 if (list_empty(&acpi_dev->pnp.ids))
840 return 0;
841
Kay Sievers7eff2e72007-08-14 15:15:12 +0200842 if (add_uevent_var(env, "MODALIAS="))
843 return -ENOMEM;
844 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
845 sizeof(env->buf) - env->buflen);
846 if (len >= (sizeof(env->buf) - env->buflen))
847 return -ENOMEM;
848 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return 0;
850}
851
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000852static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
853{
854 struct acpi_device *device = data;
855
856 device->driver->ops.notify(device, event);
857}
858
859static acpi_status acpi_device_notify_fixed(void *data)
860{
861 struct acpi_device *device = data;
862
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000863 /* Fixed hardware devices have no handles */
864 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000865 return AE_OK;
866}
867
868static int acpi_device_install_notify_handler(struct acpi_device *device)
869{
870 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000871
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000872 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000873 status =
874 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
875 acpi_device_notify_fixed,
876 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000877 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000878 status =
879 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
880 acpi_device_notify_fixed,
881 device);
882 else
883 status = acpi_install_notify_handler(device->handle,
884 ACPI_DEVICE_NOTIFY,
885 acpi_device_notify,
886 device);
887
888 if (ACPI_FAILURE(status))
889 return -EINVAL;
890 return 0;
891}
892
893static void acpi_device_remove_notify_handler(struct acpi_device *device)
894{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000895 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000896 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
897 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000898 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000899 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
900 acpi_device_notify_fixed);
901 else
902 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
903 acpi_device_notify);
904}
905
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200906static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800907{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800908 struct acpi_device *acpi_dev = to_acpi_device(dev);
909 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
910 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200912 if (acpi_dev->handler)
913 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000914
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200915 if (!acpi_drv->ops.add)
916 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800917
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200918 ret = acpi_drv->ops.add(acpi_dev);
919 if (ret)
920 return ret;
921
922 acpi_dev->driver = acpi_drv;
923 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
924 "Driver [%s] successfully bound to device [%s]\n",
925 acpi_drv->name, acpi_dev->pnp.bus_id));
926
927 if (acpi_drv->ops.notify) {
928 ret = acpi_device_install_notify_handler(acpi_dev);
929 if (ret) {
930 if (acpi_drv->ops.remove)
931 acpi_drv->ops.remove(acpi_dev);
932
933 acpi_dev->driver = NULL;
934 acpi_dev->driver_data = NULL;
935 return ret;
936 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800937 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200938
939 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
940 acpi_drv->name, acpi_dev->pnp.bus_id));
941 get_device(dev);
942 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800943}
944
945static int acpi_device_remove(struct device * dev)
946{
947 struct acpi_device *acpi_dev = to_acpi_device(dev);
948 struct acpi_driver *acpi_drv = acpi_dev->driver;
949
950 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000951 if (acpi_drv->ops.notify)
952 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800953 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100954 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800955 }
956 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700957 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800958
959 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800960 return 0;
961}
962
David Brownell55955aa2007-05-08 00:28:35 -0700963struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800964 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800965 .match = acpi_bus_match,
966 .probe = acpi_device_probe,
967 .remove = acpi_device_remove,
968 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969};
970
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100971int acpi_device_add(struct acpi_device *device,
972 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800974 int result;
975 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
976 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000977
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100978 if (device->handle) {
979 acpi_status status;
980
981 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
982 device);
983 if (ACPI_FAILURE(status)) {
984 acpi_handle_err(device->handle,
985 "Unable to attach device data\n");
986 return -ENODEV;
987 }
988 }
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /*
991 * Linkage
992 * -------
993 * Link this device to its parent and siblings.
994 */
995 INIT_LIST_HEAD(&device->children);
996 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800998 INIT_LIST_HEAD(&device->physical_node_list);
999 mutex_init(&device->physical_node_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001000 INIT_LIST_HEAD(&device->power_dependent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001002 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1003 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001004 pr_err(PREFIX "Memory allocation error\n");
1005 result = -ENOMEM;
1006 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001007 }
1008
Shaohua Li90905892009-04-07 10:24:29 +08001009 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001010 /*
1011 * Find suitable bus_id and instance number in acpi_bus_id_list
1012 * If failed, create one and link it into acpi_bus_id_list
1013 */
1014 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001015 if (!strcmp(acpi_device_bus_id->bus_id,
1016 acpi_device_hid(device))) {
1017 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001018 found = 1;
1019 kfree(new_bus_id);
1020 break;
1021 }
1022 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001023 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001024 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001025 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001026 acpi_device_bus_id->instance_no = 0;
1027 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1028 }
Kay Sievers07944692008-10-30 01:18:59 +01001029 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001030
Len Brown33b57152008-12-15 22:09:26 -05001031 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 if (device->wakeup.flags.valid)
1035 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001036 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Patrick Mochel1890a972006-12-07 20:56:31 +08001038 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001039 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001040 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001041 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001042 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001043 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001044 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001045 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001046 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001047
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001048 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001049 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001050 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1051 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001052
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001053 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001054
1055 err:
Shaohua Li90905892009-04-07 10:24:29 +08001056 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001057 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001058 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001059 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001060 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001061
1062 err_detach:
1063 acpi_detach_data(device->handle, acpi_bus_data_handler);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001064 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Rafael J. Wysockib17b5372013-01-15 13:23:44 +01001067static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Shaohua Li90905892009-04-07 10:24:29 +08001069 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001070 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001074 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +08001077
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001078 acpi_power_add_remove_device(device, false);
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001079 acpi_device_remove_files(device);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +01001080 if (device->remove)
1081 device->remove(device);
1082
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001083 device_del(&device->dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001084 /*
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001085 * Transition the device to D3cold to drop the reference counts of all
1086 * power resources the device depends on and turn off the ones that have
1087 * no more references.
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001088 */
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001089 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001090 device->handle = NULL;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001091 put_device(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Zhang Rui9e89dde2006-12-07 20:56:16 +08001094/* --------------------------------------------------------------------------
1095 Driver Management
1096 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001098 * acpi_bus_register_driver - register a driver with the ACPI bus
1099 * @driver: driver being registered
1100 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001102 * devices that match the driver's criteria and binds. Returns zero for
1103 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Len Brown4be44fc2005-08-05 00:44:28 -04001105int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Patrick Mochel1890a972006-12-07 20:56:31 +08001107 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001110 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001111 driver->drv.name = driver->name;
1112 driver->drv.bus = &acpi_bus_type;
1113 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Patrick Mochel1890a972006-12-07 20:56:31 +08001115 ret = driver_register(&driver->drv);
1116 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Len Brown4be44fc2005-08-05 00:44:28 -04001119EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001122 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1123 * @driver: driver to unregister
1124 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1126 * devices that match the driver's criteria and unbinds.
1127 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001128void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Patrick Mochel1890a972006-12-07 20:56:31 +08001130 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
Len Brown4be44fc2005-08-05 00:44:28 -04001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133EXPORT_SYMBOL(acpi_bus_unregister_driver);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135/* --------------------------------------------------------------------------
1136 Device Enumeration
1137 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001138static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1139{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001140 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001141 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001142
1143 /*
1144 * Fixed hardware devices do not appear in the namespace and do not
1145 * have handles, but we fabricate acpi_devices for them, so we have
1146 * to deal with them specially.
1147 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001148 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001149 return acpi_root;
1150
1151 do {
1152 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001153 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001154 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1155 } while (acpi_bus_get_device(handle, &device));
1156 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001157}
1158
Len Brownc8f7a622006-07-09 17:22:28 -04001159acpi_status
1160acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1161{
1162 acpi_status status;
1163 acpi_handle tmp;
1164 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1165 union acpi_object *obj;
1166
1167 status = acpi_get_handle(handle, "_EJD", &tmp);
1168 if (ACPI_FAILURE(status))
1169 return status;
1170
1171 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1172 if (ACPI_SUCCESS(status)) {
1173 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001174 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1175 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001176 kfree(buffer.pointer);
1177 }
1178 return status;
1179}
1180EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1181
Bob Moore8e4319c2009-06-29 13:43:27 +08001182void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184
1185 /* TBD */
1186
1187 return;
1188}
1189
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001190static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1191 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001193 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1194 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001196 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001197 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001199 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001200 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001202 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001204 /* _PRW */
1205 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1206 if (ACPI_FAILURE(status)) {
1207 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001208 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001209 }
1210
1211 package = (union acpi_object *)buffer.pointer;
1212
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001213 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001214 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001217 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001218 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (element->type == ACPI_TYPE_PACKAGE) {
1221 if ((element->package.count < 2) ||
1222 (element->package.elements[0].type !=
1223 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001224 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001225 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001226
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001227 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001229 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 (u32) element->package.elements[1].integer.value;
1231 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001232 wakeup->gpe_device = NULL;
1233 wakeup->gpe_number = element->integer.value;
1234 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001235 goto out;
1236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001239 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001240 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001241
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001242 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001244 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1245 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001248 if (!list_empty(&wakeup->resources)) {
1249 int sleep_state;
1250
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001251 err = acpi_power_wakeup_list_init(&wakeup->resources,
1252 &sleep_state);
1253 if (err) {
1254 acpi_handle_warn(handle, "Retrieving current states "
1255 "of wakeup power resources failed\n");
1256 acpi_power_resources_list_free(&wakeup->resources);
1257 goto out;
1258 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001259 if (sleep_state < wakeup->sleep_state) {
1260 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1261 "(S%d) by S%d from power resources\n",
1262 (int)wakeup->sleep_state, sleep_state);
1263 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Lin Mingbba63a22010-12-13 13:39:17 +08001266 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001267
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001268 out:
1269 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001270 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001273static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001275 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001276 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001277 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001278 {"PNP0C0E", 0},
1279 {"", 0},
1280 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001281 acpi_status status;
1282 acpi_event_status event_status;
1283
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001284 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001285
1286 /* Power button, Lid switch always enable wakeup */
1287 if (!acpi_match_device_ids(device, button_device_ids)) {
1288 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001289 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1290 /* Do not use Lid/sleep button for S5 wakeup */
1291 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1292 device->wakeup.sleep_state = ACPI_STATE_S4;
1293 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001294 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001295 return;
1296 }
1297
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001298 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1299 device->wakeup.gpe_number,
1300 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001301 if (status == AE_OK)
1302 device->wakeup.flags.run_wake =
1303 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1304}
1305
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001306static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001307{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001308 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001309
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001310 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001311 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001312 return;
1313
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001314 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1315 &device->wakeup);
1316 if (err) {
1317 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001318 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 }
1320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001322 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001323 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001324 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1325 * system for the ACPI device with the _PRW object.
1326 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1327 * So it is necessary to call _DSW object first. Only when it is not
1328 * present will the _PSW object used.
1329 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001330 err = acpi_device_sleep_wake(device, 0, 0, 0);
1331 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001332 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1333 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001336static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001338 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001339 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1340 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001341 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001342
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001343 INIT_LIST_HEAD(&ps->resources);
1344
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001345 /* Evaluate "_PRx" to get referenced power resources */
1346 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1347 if (ACPI_SUCCESS(status)) {
1348 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001349
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001350 if (buffer.length && package
1351 && package->type == ACPI_TYPE_PACKAGE
1352 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001353 int err = acpi_extract_power_resources(package, 0,
1354 &ps->resources);
1355 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001356 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001357 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001358 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001359 }
1360
1361 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001362 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001363 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001364 ps->flags.explicit_set = 1;
1365
1366 /*
1367 * State is valid if there are means to put the device into it.
1368 * D3hot is only valid if _PR3 present.
1369 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001370 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001371 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1372 ps->flags.valid = 1;
1373 ps->flags.os_accessible = 1;
1374 }
1375
1376 ps->power = -1; /* Unknown - driver assigned */
1377 ps->latency = -1; /* Unknown - driver assigned */
1378}
1379
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001380static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001382 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001384 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001385 if (!acpi_has_method(device->handle, "_PS0") &&
1386 !acpi_has_method(device->handle, "_PR0"))
1387 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001388
1389 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001392 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001394 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001395 device->power.flags.explicit_get = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001396 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001397 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001400 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001402 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1403 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001405 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Zhang Rui9e89dde2006-12-07 20:56:16 +08001407 /* Set defaults for D0 and D3 states (always valid) */
1408 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1409 device->power.states[ACPI_STATE_D0].power = 100;
1410 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1411 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001413 /* Set D3cold's explicit_set flag if _PS3 exists. */
1414 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1415 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1416
Aaron Lu1399dfc2012-11-21 23:33:40 +01001417 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1418 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1419 device->power.flags.power_resources)
1420 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1421
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001422 if (acpi_bus_init_power(device)) {
1423 acpi_free_power_resources_lists(device);
1424 device->flags.power_manageable = 0;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001428static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001431 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 device->flags.dynamic_status = 1;
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001435 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 device->flags.removable = 1;
1437
1438 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001439 if (acpi_has_method(device->handle, "_EJD") ||
1440 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001444static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Len Brown4be44fc2005-08-05 00:44:28 -04001446 char bus_id[5] = { '?', 0 };
1447 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1448 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /*
1451 * Bus ID
1452 * ------
1453 * The device's Bus ID is simply the object name.
1454 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1455 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001456 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001458 return;
1459 }
1460
1461 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 case ACPI_BUS_TYPE_POWER_BUTTON:
1463 strcpy(device->pnp.bus_id, "PWRF");
1464 break;
1465 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1466 strcpy(device->pnp.bus_id, "SLPF");
1467 break;
1468 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001469 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* Clean up trailing underscores (if any) */
1471 for (i = 3; i > 1; i--) {
1472 if (bus_id[i] == '_')
1473 bus_id[i] = '\0';
1474 else
1475 break;
1476 }
1477 strcpy(device->pnp.bus_id, bus_id);
1478 break;
1479 }
1480}
1481
Zhang Rui54735262007-01-11 02:09:09 -05001482/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001483 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001484 *
1485 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1486 * then we can safely call it an ejectable drive bay
1487 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001488static int acpi_bay_match(acpi_handle handle)
1489{
Zhang Rui54735262007-01-11 02:09:09 -05001490 acpi_handle phandle;
1491
Jiang Liu952c63e2013-06-29 00:24:38 +08001492 if (!acpi_has_method(handle, "_EJ0"))
Zhang Rui54735262007-01-11 02:09:09 -05001493 return -ENODEV;
1494
Jiang Liu952c63e2013-06-29 00:24:38 +08001495 if (acpi_has_method(handle, "_GTF") ||
1496 acpi_has_method(handle, "_GTM") ||
1497 acpi_has_method(handle, "_STM") ||
1498 acpi_has_method(handle, "_SDD"))
Zhang Rui54735262007-01-11 02:09:09 -05001499 return 0;
1500
1501 if (acpi_get_parent(handle, &phandle))
1502 return -ENODEV;
1503
Jiang Liu952c63e2013-06-29 00:24:38 +08001504 if (acpi_has_method(phandle, "_GTF") ||
1505 acpi_has_method(phandle, "_GTM") ||
1506 acpi_has_method(phandle, "_STM") ||
1507 acpi_has_method(phandle, "_SDD"))
Zhang Rui54735262007-01-11 02:09:09 -05001508 return 0;
1509
1510 return -ENODEV;
1511}
1512
Frank Seidel3620f2f2007-12-07 13:20:34 +01001513/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001514 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001515 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001516static int acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001517{
1518 acpi_handle tmp;
Toshi Kanid4e1a692013-03-04 21:30:41 +00001519 return acpi_get_handle(handle, "_DCK", &tmp);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001520}
1521
Thomas Renninger620e1122010-10-01 10:54:00 +02001522const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001523{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001524 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001525
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001526 if (list_empty(&device->pnp.ids))
1527 return dummy_hid;
1528
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001529 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1530 return hid->id;
1531}
1532EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001533
Toshi Kanid4e1a692013-03-04 21:30:41 +00001534static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001535{
1536 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001537
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001538 id = kmalloc(sizeof(*id), GFP_KERNEL);
1539 if (!id)
1540 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001541
Thomas Meyer581de592011-08-06 11:32:56 +02001542 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001543 if (!id->id) {
1544 kfree(id);
1545 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001546 }
1547
Toshi Kanid4e1a692013-03-04 21:30:41 +00001548 list_add_tail(&id->list, &pnp->ids);
1549 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001550}
1551
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001552/*
1553 * Old IBM workstations have a DSDT bug wherein the SMBus object
1554 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1555 * prefix. Work around this.
1556 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001557static int acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001558{
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001559 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1560 int result;
1561
1562 if (!dmi_name_in_vendors("IBM"))
1563 return -ENODEV;
1564
1565 /* Look for SMBS object */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001566 result = acpi_get_name(handle, ACPI_SINGLE_NAME, &path);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001567 if (result)
1568 return result;
1569
1570 if (strcmp("SMBS", path.pointer)) {
1571 result = -ENODEV;
1572 goto out;
1573 }
1574
1575 /* Does it have the necessary (but misnamed) methods? */
1576 result = -ENODEV;
Jiang Liu952c63e2013-06-29 00:24:38 +08001577 if (acpi_has_method(handle, "SBI") &&
1578 acpi_has_method(handle, "SBR") &&
1579 acpi_has_method(handle, "SBW"))
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001580 result = 0;
1581out:
1582 kfree(path.pointer);
1583 return result;
1584}
1585
Toshi Kanic0af4172013-03-04 21:30:42 +00001586static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1587 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Len Brown4be44fc2005-08-05 00:44:28 -04001589 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001590 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001591 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001592 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Toshi Kanic0af4172013-03-04 21:30:42 +00001594 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001596 if (handle == ACPI_ROOT_OBJECT) {
1597 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001598 break;
1599 }
1600
Toshi Kanic0af4172013-03-04 21:30:42 +00001601 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001603 pr_err(PREFIX "%s: Error reading device info\n",
1604 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return;
1606 }
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001609 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001610 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001611 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001612 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001613 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001616 pnp->bus_address = info->address;
1617 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 }
Lv Zhengccf78042012-10-30 14:41:07 +01001619 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001620 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001621 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001622
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001623 kfree(info);
1624
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001625 /*
1626 * Some devices don't reliably have _HIDs & _CIDs, so add
1627 * synthetic HIDs to make sure drivers can find them.
1628 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001629 if (acpi_is_video_device(handle))
1630 acpi_add_id(pnp, ACPI_VIDEO_HID);
1631 else if (ACPI_SUCCESS(acpi_bay_match(handle)))
1632 acpi_add_id(pnp, ACPI_BAY_HID);
1633 else if (ACPI_SUCCESS(acpi_dock_match(handle)))
1634 acpi_add_id(pnp, ACPI_DOCK_HID);
1635 else if (!acpi_ibm_smbus_match(handle))
1636 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1637 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1638 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1639 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1640 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001641 }
Zhang Rui54735262007-01-11 02:09:09 -05001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 break;
1644 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001645 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 break;
1647 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001648 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001651 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 break;
1653 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001654 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 break;
1656 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001657 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 break;
1659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
Toshi Kanic0af4172013-03-04 21:30:42 +00001662void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1663{
1664 struct acpi_hardware_id *id, *tmp;
1665
1666 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1667 kfree(id->id);
1668 kfree(id);
1669 }
1670 kfree(pnp->unique_id);
1671}
1672
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001673void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1674 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001676 INIT_LIST_HEAD(&device->pnp.ids);
1677 device->device_type = type;
1678 device->handle = handle;
1679 device->parent = acpi_bus_get_parent(handle);
1680 STRUCT_TO_INT(device->status) = sta;
1681 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001682 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001683 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001684 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001685 device_initialize(&device->dev);
1686 dev_set_uevent_suppress(&device->dev, true);
1687}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001688
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001689void acpi_device_add_finalize(struct acpi_device *device)
1690{
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001691 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001692 dev_set_uevent_suppress(&device->dev, false);
1693 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001696static int acpi_add_single_object(struct acpi_device **child,
1697 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001698 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001700 int result;
1701 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001702 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Burman Yan36bcbec2006-12-19 12:56:11 -08001704 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001706 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001707 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001710 acpi_init_device_object(device, handle, type, sta);
1711 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001712 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001714 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001715 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001716 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001717 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001720 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001721 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001722 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1723 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1724 dev_name(&device->dev), (char *) buffer.pointer,
1725 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1726 kfree(buffer.pointer);
1727 *child = device;
1728 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001729}
1730
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001731static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1732 unsigned long long *sta)
1733{
1734 acpi_status status;
1735 acpi_object_type acpi_type;
1736
1737 status = acpi_get_type(handle, &acpi_type);
1738 if (ACPI_FAILURE(status))
1739 return -ENODEV;
1740
1741 switch (acpi_type) {
1742 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1743 case ACPI_TYPE_DEVICE:
1744 *type = ACPI_BUS_TYPE_DEVICE;
1745 status = acpi_bus_get_status_handle(handle, sta);
1746 if (ACPI_FAILURE(status))
1747 return -ENODEV;
1748 break;
1749 case ACPI_TYPE_PROCESSOR:
1750 *type = ACPI_BUS_TYPE_PROCESSOR;
1751 status = acpi_bus_get_status_handle(handle, sta);
1752 if (ACPI_FAILURE(status))
1753 return -ENODEV;
1754 break;
1755 case ACPI_TYPE_THERMAL:
1756 *type = ACPI_BUS_TYPE_THERMAL;
1757 *sta = ACPI_STA_DEFAULT;
1758 break;
1759 case ACPI_TYPE_POWER:
1760 *type = ACPI_BUS_TYPE_POWER;
1761 *sta = ACPI_STA_DEFAULT;
1762 break;
1763 default:
1764 return -ENODEV;
1765 }
1766
1767 return 0;
1768}
1769
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001770static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1771 char *idstr,
1772 const struct acpi_device_id **matchid)
1773{
1774 const struct acpi_device_id *devid;
1775
1776 for (devid = handler->ids; devid->id[0]; devid++)
1777 if (!strcmp((char *)devid->id, idstr)) {
1778 if (matchid)
1779 *matchid = devid;
1780
1781 return true;
1782 }
1783
1784 return false;
1785}
1786
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001787static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1788 const struct acpi_device_id **matchid)
1789{
1790 struct acpi_scan_handler *handler;
1791
1792 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1793 if (acpi_scan_handler_matching(handler, idstr, matchid))
1794 return handler;
1795
1796 return NULL;
1797}
1798
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001799void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1800{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001801 if (!!hotplug->enabled == !!val)
1802 return;
1803
1804 mutex_lock(&acpi_scan_lock);
1805
1806 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001807
1808 mutex_unlock(&acpi_scan_lock);
1809}
1810
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001811static void acpi_scan_init_hotplug(acpi_handle handle, int type)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001812{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001813 struct acpi_device_pnp pnp = {};
1814 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001815 struct acpi_scan_handler *handler;
1816
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001817 INIT_LIST_HEAD(&pnp.ids);
1818 acpi_set_pnp_ids(handle, &pnp, type);
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001819
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001820 if (!pnp.type.hardware_id)
Catalin Marinas7a26b532013-05-15 16:49:35 +00001821 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001822
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001823 /*
1824 * This relies on the fact that acpi_install_notify_handler() will not
1825 * install the same notify handler routine twice for the same handle.
1826 */
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001827 list_for_each_entry(hwid, &pnp.ids, list) {
1828 handler = acpi_scan_match_handler(hwid->id, NULL);
1829 if (handler) {
Toshi Kani2cbb14f2013-03-05 22:33:31 +00001830 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1831 acpi_hotplug_notify_cb, handler);
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001832 break;
1833 }
1834 }
1835
Catalin Marinas7a26b532013-05-15 16:49:35 +00001836out:
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001837 acpi_free_pnp_ids(&pnp);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001838}
1839
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001840static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1841 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001843 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001844 int type;
1845 unsigned long long sta;
1846 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001847
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001848 acpi_bus_get_device(handle, &device);
1849 if (device)
1850 goto out;
1851
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001852 result = acpi_bus_type_and_status(handle, &type, &sta);
1853 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001854 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001856 if (type == ACPI_BUS_TYPE_POWER) {
1857 acpi_add_power_resource(handle);
1858 return AE_OK;
1859 }
1860
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001861 acpi_scan_init_hotplug(handle, type);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001862
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001863 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001864 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001865 struct acpi_device_wakeup wakeup;
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001866
Jiang Liu952c63e2013-06-29 00:24:38 +08001867 if (acpi_has_method(handle, "_PRW")) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001868 acpi_bus_extract_wakeup_device_power_package(handle,
1869 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001870 acpi_power_resources_list_free(&wakeup.resources);
1871 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001872 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001875 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001876 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001877 return AE_CTRL_DEPTH;
1878
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001879 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001880 if (!*return_value)
1881 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001882
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001883 return AE_OK;
1884}
1885
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001886static int acpi_scan_attach_handler(struct acpi_device *device)
1887{
1888 struct acpi_hardware_id *hwid;
1889 int ret = 0;
1890
1891 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001892 const struct acpi_device_id *devid;
1893 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001894
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001895 handler = acpi_scan_match_handler(hwid->id, &devid);
1896 if (handler) {
1897 ret = handler->attach(device, devid);
1898 if (ret > 0) {
1899 device->handler = handler;
1900 break;
1901 } else if (ret < 0) {
1902 break;
1903 }
1904 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001905 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001906 return ret;
1907}
1908
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001909static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1910 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001911{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001912 struct acpi_device *device;
1913 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001914 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001915
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001916 /*
1917 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1918 * namespace walks prematurely.
1919 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001920 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001921 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001922
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001923 if (acpi_bus_get_device(handle, &device))
1924 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001925
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001926 ret = acpi_scan_attach_handler(device);
1927 if (ret)
1928 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1929
1930 ret = device_attach(&device->dev);
1931 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001934/**
1935 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1936 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001937 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001938 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1939 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001940 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001941 * If no devices were found, -ENODEV is returned, but it does not mean that
1942 * there has been a real error. There just have been no suitable ACPI objects
1943 * in the table trunk from which the kernel could create a device and add an
1944 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001945 *
1946 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001947 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001948int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001949{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001951 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001952
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001953 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001955 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001956
Thomas Renningerd2f66502010-01-29 17:48:51 +01001957 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001958 error = -ENODEV;
1959 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001960 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001961 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01001962
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001963 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001964}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001965EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001967static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1968 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001970 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001972 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001973 struct acpi_scan_handler *dev_handler = device->handler;
1974
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001975 if (dev_handler) {
1976 if (dev_handler->detach)
1977 dev_handler->detach(device);
1978
1979 device->handler = NULL;
1980 } else {
1981 device_release_driver(&device->dev);
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001984 return AE_OK;
1985}
1986
1987static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1988 void *not_used, void **ret_not_used)
1989{
1990 struct acpi_device *device = NULL;
1991
1992 if (!acpi_bus_get_device(handle, &device))
1993 acpi_device_unregister(device);
1994
1995 return AE_OK;
1996}
1997
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001998/**
1999 * acpi_bus_trim - Remove ACPI device node and all of its descendants
2000 * @start: Root of the ACPI device nodes subtree to remove.
2001 *
2002 * Must be called under acpi_scan_lock.
2003 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01002004void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002006 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002007 * Execute acpi_bus_device_detach() as a post-order callback to detach
2008 * all ACPI drivers from the device nodes being removed.
2009 */
2010 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2011 acpi_bus_device_detach, NULL, NULL);
2012 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
2013 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002014 * Execute acpi_bus_remove() as a post-order callback to remove device
2015 * nodes in the given namespace scope.
2016 */
2017 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2018 acpi_bus_remove, NULL, NULL);
2019 acpi_bus_remove(start->handle, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
Kristen Accardiceaba662006-02-23 17:56:01 -08002021EXPORT_SYMBOL_GPL(acpi_bus_trim);
2022
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002023static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Len Brown4be44fc2005-08-05 00:44:28 -04002025 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /*
2028 * Enumerate all fixed-feature devices.
2029 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002030 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2031 struct acpi_device *device = NULL;
2032
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002033 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002034 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002035 ACPI_STA_DEFAULT);
2036 if (result)
2037 return result;
2038
2039 result = device_attach(&device->dev);
2040 if (result < 0)
2041 return result;
2042
Daniel Drakec10d7a12012-05-10 00:08:43 +01002043 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002046 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2047 struct acpi_device *device = NULL;
2048
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002049 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002050 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002051 ACPI_STA_DEFAULT);
2052 if (result)
2053 return result;
2054
2055 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002058 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Bjorn Helgaase747f272009-03-24 16:49:43 -06002061int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Patrick Mochel5b327262006-05-10 10:33:00 -04002065 result = bus_register(&acpi_bus_type);
2066 if (result) {
2067 /* We don't want to quit even if we failed to add suspend/resume */
2068 printk(KERN_ERR PREFIX "Could not register bus type\n");
2069 }
2070
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002071 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002072 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002073 acpi_processor_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002074 acpi_platform_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002075 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002076 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002077 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002078 acpi_memory_hotplug_init();
Jiang Liu94add0f2013-06-23 00:59:55 +02002079 acpi_dock_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002080
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002081 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 * Enumerate devices in the ACPI namespace.
2084 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002085 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002087 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002089 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002091 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002093 result = acpi_bus_scan_fixed();
2094 if (result) {
2095 acpi_device_unregister(acpi_root);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002096 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002097 }
2098
2099 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002100
Yinghai Lu668192b2013-01-21 13:20:48 -08002101 acpi_pci_root_hp_init();
2102
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002103 out:
2104 mutex_unlock(&acpi_scan_lock);
2105 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}