blob: cc19d623a18f3a8141bf314da74401196b22d415 [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. Wysocki7f28dde2013-11-07 01:41:14 +0100128static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
129 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200130{
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
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100139 if (device->handler && !device->handler->hotplug.enabled) {
140 *ret_p = &device->dev;
141 return AE_SUPPORT;
142 }
143
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200144 mutex_lock(&device->physical_node_lock);
145
146 list_for_each_entry(pn, &device->physical_node_list, node) {
147 int ret;
148
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200149 if (second_pass) {
150 /* Skip devices offlined by the first pass. */
151 if (pn->put_online)
152 continue;
153 } else {
154 pn->put_online = false;
155 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200156 ret = device_offline(pn->dev);
157 if (acpi_force_hot_remove)
158 continue;
159
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200160 if (ret >= 0) {
161 pn->put_online = !ret;
162 } else {
163 *ret_p = pn->dev;
164 if (second_pass) {
165 status = AE_ERROR;
166 break;
167 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200168 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200169 }
170
171 mutex_unlock(&device->physical_node_lock);
172
173 return status;
174}
175
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100176static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
177 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200178{
179 struct acpi_device *device = NULL;
180 struct acpi_device_physical_node *pn;
181
182 if (acpi_bus_get_device(handle, &device))
183 return AE_OK;
184
185 mutex_lock(&device->physical_node_lock);
186
187 list_for_each_entry(pn, &device->physical_node_list, node)
188 if (pn->put_online) {
189 device_online(pn->dev);
190 pn->put_online = false;
191 }
192
193 mutex_unlock(&device->physical_node_lock);
194
195 return AE_OK;
196}
197
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100198static int acpi_scan_hot_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Yinghai Lu5993c462013-01-11 22:40:41 +0000200 acpi_handle handle = device->handle;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200201 struct device *errdev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100202 acpi_status status;
Toshi Kani882fd122013-03-13 19:29:26 +0000203 unsigned long long sta;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100204
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100205 /* If there is no handle, the device node has been unregistered. */
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100206 if (!handle) {
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100207 dev_dbg(&device->dev, "ACPI handle missing\n");
208 put_device(&device->dev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100209 return -EINVAL;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100210 }
211
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200212 /*
213 * Carry out two passes here and ignore errors in the first pass,
214 * because if the devices in question are memory blocks and
215 * CONFIG_MEMCG is set, one of the blocks may hold data structures
216 * that the other blocks depend on, but it is not known in advance which
217 * block holds them.
218 *
219 * If the first pass is successful, the second one isn't needed, though.
220 */
221 errdev = NULL;
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100222 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
223 NULL, acpi_bus_offline, (void *)false,
224 (void **)&errdev);
225 if (status == AE_SUPPORT) {
226 dev_warn(errdev, "Offline disabled.\n");
227 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
228 acpi_bus_online, NULL, NULL, NULL);
229 put_device(&device->dev);
230 return -EPERM;
231 }
232 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200233 if (errdev) {
234 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200235 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100236 NULL, acpi_bus_offline, (void *)true,
237 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200238 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100239 acpi_bus_offline(handle, 0, (void *)true,
240 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200241
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200242 if (errdev && !acpi_force_hot_remove) {
243 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100244 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200245 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100246 ACPI_UINT32_MAX, acpi_bus_online,
247 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200248 put_device(&device->dev);
249 return -EBUSY;
250 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200251 }
252
Zhang Rui26d46862008-04-29 02:35:48 -0400253 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100254 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400255
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100256 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200257
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100258 /* Device node has been unregistered. */
259 put_device(&device->dev);
Toshi Kanib3c450c2012-10-26 13:38:57 +0200260 device = NULL;
261
Jiang Liu7d2421f2013-06-29 00:24:40 +0800262 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /*
264 * TBD: _EJD support.
265 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800266 status = acpi_evaluate_ej0(handle);
267 if (status == AE_NOT_FOUND)
268 return -ENODEV;
269 else if (ACPI_FAILURE(status))
270 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100271
Toshi Kani882fd122013-03-13 19:29:26 +0000272 /*
273 * Verify if eject was indeed successful. If not, log an error
274 * message. No need to call _OST since _EJ0 call was made OK.
275 */
276 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
277 if (ACPI_FAILURE(status)) {
278 acpi_handle_warn(handle,
279 "Status check after eject failed (0x%x)\n", status);
280 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
281 acpi_handle_warn(handle,
282 "Eject incomplete - status 0x%llx\n", sta);
283 }
284
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100285 return 0;
286}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100287
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100288static void acpi_bus_device_eject(void *context)
289{
290 acpi_handle handle = context;
291 struct acpi_device *device = NULL;
292 struct acpi_scan_handler *handler;
293 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200294 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100295
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200296 lock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100297 mutex_lock(&acpi_scan_lock);
298
299 acpi_bus_get_device(handle, &device);
300 if (!device)
301 goto err_out;
302
303 handler = device->handler;
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100304 if (!handler || !handler->hotplug.enabled)
305 goto err_support;
306
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100307 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
308 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200309 if (handler->hotplug.mode == AHM_CONTAINER)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100310 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100311
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200312 get_device(&device->dev);
313 error = acpi_scan_hot_remove(device);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100314 if (error == -EPERM) {
315 goto err_support;
316 } else if (error) {
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200317 goto err_out;
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100320 out:
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100321 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200322 unlock_device_hotplug();
Zhang Ruic8d72a52009-06-22 11:31:16 +0800323 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100324
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100325 err_support:
326 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100327 err_out:
328 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
329 NULL);
330 goto out;
331}
332
333static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
334{
335 struct acpi_device *device = NULL;
336 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
337 int error;
338
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200339 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200340 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100341
Rafael J. Wysocki8832f7e2013-07-08 02:01:53 +0200342 if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
343 acpi_bus_get_device(handle, &device);
344 if (device) {
345 dev_warn(&device->dev, "Attempt to re-insert\n");
346 goto out;
347 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100348 }
349 acpi_evaluate_hotplug_ost(handle, ost_source,
350 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
351 error = acpi_bus_scan(handle);
352 if (error) {
353 acpi_handle_warn(handle, "Namespace scan failure\n");
354 goto out;
355 }
356 error = acpi_bus_get_device(handle, &device);
357 if (error) {
358 acpi_handle_warn(handle, "Missing device node object\n");
359 goto out;
360 }
361 ost_code = ACPI_OST_SC_SUCCESS;
362 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
363 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
364
365 out:
366 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
367 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200368 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100369}
370
371static void acpi_scan_bus_check(void *context)
372{
373 acpi_scan_bus_device_check((acpi_handle)context,
374 ACPI_NOTIFY_BUS_CHECK);
375}
376
377static void acpi_scan_device_check(void *context)
378{
379 acpi_scan_bus_device_check((acpi_handle)context,
380 ACPI_NOTIFY_DEVICE_CHECK);
381}
382
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000383static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
384{
385 u32 ost_status;
386
387 switch (type) {
388 case ACPI_NOTIFY_BUS_CHECK:
389 acpi_handle_debug(handle,
390 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
391 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
392 break;
393 case ACPI_NOTIFY_DEVICE_CHECK:
394 acpi_handle_debug(handle,
395 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
396 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
397 break;
398 case ACPI_NOTIFY_EJECT_REQUEST:
399 acpi_handle_debug(handle,
400 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
401 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
402 break;
403 default:
404 /* non-hotplug event; possibly handled by other handler */
405 return;
406 }
407
408 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
409}
410
411static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100412{
413 acpi_osd_exec_callback callback;
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000414 struct acpi_scan_handler *handler = data;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100415 acpi_status status;
416
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000417 if (!handler->hotplug.enabled)
418 return acpi_hotplug_unsupported(handle, type);
419
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100420 switch (type) {
421 case ACPI_NOTIFY_BUS_CHECK:
422 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
423 callback = acpi_scan_bus_check;
424 break;
425 case ACPI_NOTIFY_DEVICE_CHECK:
426 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
427 callback = acpi_scan_device_check;
428 break;
429 case ACPI_NOTIFY_EJECT_REQUEST:
430 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
431 callback = acpi_bus_device_eject;
432 break;
433 default:
434 /* non-hotplug event; possibly handled by other handler */
435 return;
436 }
437 status = acpi_os_hotplug_execute(callback, handle);
438 if (ACPI_FAILURE(status))
439 acpi_evaluate_hotplug_ost(handle, type,
440 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
441 NULL);
442}
443
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100444void __acpi_bus_hot_remove_device(struct acpi_device *device, u32 ost_src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100445{
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100446 acpi_handle handle = device->handle;
447 int error;
448
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200449 lock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100450 mutex_lock(&acpi_scan_lock);
451
452 error = acpi_scan_hot_remove(device);
453 if (error && handle)
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100454 acpi_evaluate_hotplug_ost(handle, ost_src,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100455 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
456 NULL);
457
458 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200459 unlock_device_hotplug();
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100460}
461
462/**
463 * acpi_bus_hot_remove_device: Hot-remove a device and its children.
464 * @context: Address of the ACPI device object to hot-remove.
465 */
466void acpi_bus_hot_remove_device(void *context)
467{
468 __acpi_bus_hot_remove_device(context, ACPI_NOTIFY_EJECT_REQUEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
Toshi Kani61622ac2012-11-01 14:42:12 +0000470EXPORT_SYMBOL(acpi_bus_hot_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100472static ssize_t real_power_state_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
474{
475 struct acpi_device *adev = to_acpi_device(dev);
476 int state;
477 int ret;
478
479 ret = acpi_device_get_power(adev, &state);
480 if (ret)
481 return ret;
482
483 return sprintf(buf, "%s\n", acpi_power_state_string(state));
484}
485
486static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
487
488static ssize_t power_state_show(struct device *dev,
489 struct device_attribute *attr, char *buf)
490{
491 struct acpi_device *adev = to_acpi_device(dev);
492
493 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
494}
495
496static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
497
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100498static void acpi_eject_store_work(void *context)
499{
500 __acpi_bus_hot_remove_device(context, ACPI_OST_EC_OSPM_EJECT);
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800504acpi_eject_store(struct device *d, struct device_attribute *attr,
505 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800507 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100508 acpi_object_type not_used;
509 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100511 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100514 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
515 && !acpi_device->driver)
516 return -ENODEV;
517
518 status = acpi_get_type(acpi_device->handle, &not_used);
519 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
520 return -ENODEV;
521
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200522 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100523 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100524 get_device(&acpi_device->dev);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100525 status = acpi_os_hotplug_execute(acpi_eject_store_work, acpi_device);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200526 if (ACPI_SUCCESS(status))
527 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100528
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200529 put_device(&acpi_device->dev);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200530 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100531 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100532 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800535static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800537static ssize_t
538acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
539 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600541 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800542}
543static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
544
Lv Zheng923d4a42012-10-30 14:43:26 +0100545static ssize_t acpi_device_uid_show(struct device *dev,
546 struct device_attribute *attr, char *buf)
547{
548 struct acpi_device *acpi_dev = to_acpi_device(dev);
549
550 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
551}
552static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
553
554static ssize_t acpi_device_adr_show(struct device *dev,
555 struct device_attribute *attr, char *buf)
556{
557 struct acpi_device *acpi_dev = to_acpi_device(dev);
558
559 return sprintf(buf, "0x%08x\n",
560 (unsigned int)(acpi_dev->pnp.bus_address));
561}
562static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
563
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800564static ssize_t
565acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
566 struct acpi_device *acpi_dev = to_acpi_device(dev);
567 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
568 int result;
569
570 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600571 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800572 goto end;
573
574 result = sprintf(buf, "%s\n", (char*)path.pointer);
575 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600576end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800577 return result;
578}
579static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
580
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600581/* sysfs file that shows description text from the ACPI _STR method */
582static ssize_t description_show(struct device *dev,
583 struct device_attribute *attr,
584 char *buf) {
585 struct acpi_device *acpi_dev = to_acpi_device(dev);
586 int result;
587
588 if (acpi_dev->pnp.str_obj == NULL)
589 return 0;
590
591 /*
592 * The _STR object contains a Unicode identifier for a device.
593 * We need to convert to utf-8 so it can be displayed.
594 */
595 result = utf16s_to_utf8s(
596 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
597 acpi_dev->pnp.str_obj->buffer.length,
598 UTF16_LITTLE_ENDIAN, buf,
599 PAGE_SIZE);
600
601 buf[result++] = '\n';
602
603 return result;
604}
605static DEVICE_ATTR(description, 0444, description_show, NULL);
606
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100607static ssize_t
608acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
609 char *buf) {
610 struct acpi_device *acpi_dev = to_acpi_device(dev);
611
612 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
613}
614static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
615
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800616static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600618 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800619 acpi_status status;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100620 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800621 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800623 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800624 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800625 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600626 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800627 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600628 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800629 goto end;
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200632 if (!list_empty(&dev->pnp.ids)) {
633 result = device_create_file(&dev->dev, &dev_attr_hid);
634 if (result)
635 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200637 result = device_create_file(&dev->dev, &dev_attr_modalias);
638 if (result)
639 goto end;
640 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200641
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600642 /*
643 * If device has _STR, 'description' file is created
644 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800645 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600646 status = acpi_evaluate_object(dev->handle, "_STR",
647 NULL, &buffer);
648 if (ACPI_FAILURE(status))
649 buffer.pointer = NULL;
650 dev->pnp.str_obj = buffer.pointer;
651 result = device_create_file(&dev->dev, &dev_attr_description);
652 if (result)
653 goto end;
654 }
655
Toshi Kanid4e1a692013-03-04 21:30:41 +0000656 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100657 result = device_create_file(&dev->dev, &dev_attr_adr);
658 if (dev->pnp.unique_id)
659 result = device_create_file(&dev->dev, &dev_attr_uid);
660
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100661 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
662 if (ACPI_SUCCESS(status)) {
663 dev->pnp.sun = (unsigned long)sun;
664 result = device_create_file(&dev->dev, &dev_attr_sun);
665 if (result)
666 goto end;
667 } else {
668 dev->pnp.sun = (unsigned long)-1;
669 }
670
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800671 /*
672 * If device has _EJ0, 'eject' file is created that is used to trigger
673 * hot-removal function from userland.
674 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800675 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800676 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100677 if (result)
678 return result;
679 }
680
681 if (dev->flags.power_manageable) {
682 result = device_create_file(&dev->dev, &dev_attr_power_state);
683 if (result)
684 return result;
685
686 if (dev->power.flags.power_resources)
687 result = device_create_file(&dev->dev,
688 &dev_attr_real_power_state);
689 }
690
Alex Chiang0c526d92009-05-14 08:31:37 -0600691end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800692 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800693}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800695static void acpi_device_remove_files(struct acpi_device *dev)
696{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100697 if (dev->flags.power_manageable) {
698 device_remove_file(&dev->dev, &dev_attr_power_state);
699 if (dev->power.flags.power_resources)
700 device_remove_file(&dev->dev,
701 &dev_attr_real_power_state);
702 }
703
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800704 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600705 * If device has _STR, remove 'description' file
706 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800707 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600708 kfree(dev->pnp.str_obj);
709 device_remove_file(&dev->dev, &dev_attr_description);
710 }
711 /*
712 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800713 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800714 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800715 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800716
Jiang Liu952c63e2013-06-29 00:24:38 +0800717 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100718 device_remove_file(&dev->dev, &dev_attr_sun);
719
Lv Zheng923d4a42012-10-30 14:43:26 +0100720 if (dev->pnp.unique_id)
721 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000722 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100723 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600724 device_remove_file(&dev->dev, &dev_attr_modalias);
725 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600726 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800727 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800728}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800730 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200732
Mika Westerbergcf761af2012-10-31 22:44:41 +0100733static const struct acpi_device_id *__acpi_match_device(
734 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200735{
736 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600737 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200738
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800739 /*
740 * If the device is not present, it is unnecessary to load device
741 * driver for it.
742 */
743 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100744 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800745
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600746 for (id = ids; id->id[0]; id++)
747 list_for_each_entry(hwid, &device->pnp.ids, list)
748 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100749 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200750
Mika Westerbergcf761af2012-10-31 22:44:41 +0100751 return NULL;
752}
753
754/**
755 * acpi_match_device - Match a struct device against a given list of ACPI IDs
756 * @ids: Array of struct acpi_device_id object to match against.
757 * @dev: The device structure to match.
758 *
759 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
760 * object for that handle and use that object to match against a given list of
761 * device IDs.
762 *
763 * Return a pointer to the first matching ID on success or %NULL on failure.
764 */
765const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
766 const struct device *dev)
767{
768 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100769 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100770
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100771 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100772 return NULL;
773
774 return __acpi_match_device(adev, ids);
775}
776EXPORT_SYMBOL_GPL(acpi_match_device);
777
778int acpi_match_device_ids(struct acpi_device *device,
779 const struct acpi_device_id *ids)
780{
781 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200782}
783EXPORT_SYMBOL(acpi_match_device_ids);
784
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100785static void acpi_free_power_resources_lists(struct acpi_device *device)
786{
787 int i;
788
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100789 if (device->wakeup.flags.valid)
790 acpi_power_resources_list_free(&device->wakeup.resources);
791
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100792 if (!device->flags.power_manageable)
793 return;
794
795 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
796 struct acpi_device_power_state *ps = &device->power.states[i];
797 acpi_power_resources_list_free(&ps->resources);
798 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600799}
800
Patrick Mochel1890a972006-12-07 20:56:31 +0800801static void acpi_device_release(struct device *dev)
802{
803 struct acpi_device *acpi_dev = to_acpi_device(dev);
804
Toshi Kanic0af4172013-03-04 21:30:42 +0000805 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100806 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800807 kfree(acpi_dev);
808}
809
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800810static int acpi_bus_match(struct device *dev, struct device_driver *drv)
811{
812 struct acpi_device *acpi_dev = to_acpi_device(dev);
813 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
814
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100815 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100816 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800817}
818
Kay Sievers7eff2e72007-08-14 15:15:12 +0200819static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800820{
821 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200822 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800823
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200824 if (list_empty(&acpi_dev->pnp.ids))
825 return 0;
826
Kay Sievers7eff2e72007-08-14 15:15:12 +0200827 if (add_uevent_var(env, "MODALIAS="))
828 return -ENOMEM;
829 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
830 sizeof(env->buf) - env->buflen);
831 if (len >= (sizeof(env->buf) - env->buflen))
832 return -ENOMEM;
833 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return 0;
835}
836
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000837static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
838{
839 struct acpi_device *device = data;
840
841 device->driver->ops.notify(device, event);
842}
843
844static acpi_status acpi_device_notify_fixed(void *data)
845{
846 struct acpi_device *device = data;
847
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000848 /* Fixed hardware devices have no handles */
849 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000850 return AE_OK;
851}
852
853static int acpi_device_install_notify_handler(struct acpi_device *device)
854{
855 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000856
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000857 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000858 status =
859 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
860 acpi_device_notify_fixed,
861 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000862 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000863 status =
864 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
865 acpi_device_notify_fixed,
866 device);
867 else
868 status = acpi_install_notify_handler(device->handle,
869 ACPI_DEVICE_NOTIFY,
870 acpi_device_notify,
871 device);
872
873 if (ACPI_FAILURE(status))
874 return -EINVAL;
875 return 0;
876}
877
878static void acpi_device_remove_notify_handler(struct acpi_device *device)
879{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000880 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000881 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
882 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000883 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000884 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
885 acpi_device_notify_fixed);
886 else
887 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
888 acpi_device_notify);
889}
890
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200891static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800892{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800893 struct acpi_device *acpi_dev = to_acpi_device(dev);
894 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
895 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200897 if (acpi_dev->handler)
898 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000899
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200900 if (!acpi_drv->ops.add)
901 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800902
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200903 ret = acpi_drv->ops.add(acpi_dev);
904 if (ret)
905 return ret;
906
907 acpi_dev->driver = acpi_drv;
908 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
909 "Driver [%s] successfully bound to device [%s]\n",
910 acpi_drv->name, acpi_dev->pnp.bus_id));
911
912 if (acpi_drv->ops.notify) {
913 ret = acpi_device_install_notify_handler(acpi_dev);
914 if (ret) {
915 if (acpi_drv->ops.remove)
916 acpi_drv->ops.remove(acpi_dev);
917
918 acpi_dev->driver = NULL;
919 acpi_dev->driver_data = NULL;
920 return ret;
921 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800922 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200923
924 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
925 acpi_drv->name, acpi_dev->pnp.bus_id));
926 get_device(dev);
927 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800928}
929
930static int acpi_device_remove(struct device * dev)
931{
932 struct acpi_device *acpi_dev = to_acpi_device(dev);
933 struct acpi_driver *acpi_drv = acpi_dev->driver;
934
935 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000936 if (acpi_drv->ops.notify)
937 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800938 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100939 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800940 }
941 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700942 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800943
944 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800945 return 0;
946}
947
David Brownell55955aa2007-05-08 00:28:35 -0700948struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800949 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800950 .match = acpi_bus_match,
951 .probe = acpi_device_probe,
952 .remove = acpi_device_remove,
953 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954};
955
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200956static void acpi_bus_data_handler(acpi_handle handle, void *context)
957{
958 /* Intentionally empty. */
959}
960
961int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
962{
963 acpi_status status;
964
965 if (!device)
966 return -EINVAL;
967
968 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
969 if (ACPI_FAILURE(status) || !*device) {
970 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
971 handle));
972 return -ENODEV;
973 }
974 return 0;
975}
976EXPORT_SYMBOL_GPL(acpi_bus_get_device);
977
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100978int acpi_device_add(struct acpi_device *device,
979 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800981 int result;
982 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
983 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000984
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100985 if (device->handle) {
986 acpi_status status;
987
988 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
989 device);
990 if (ACPI_FAILURE(status)) {
991 acpi_handle_err(device->handle,
992 "Unable to attach device data\n");
993 return -ENODEV;
994 }
995 }
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /*
998 * Linkage
999 * -------
1000 * Link this device to its parent and siblings.
1001 */
1002 INIT_LIST_HEAD(&device->children);
1003 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001005 INIT_LIST_HEAD(&device->physical_node_list);
1006 mutex_init(&device->physical_node_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001007 INIT_LIST_HEAD(&device->power_dependent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001009 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1010 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001011 pr_err(PREFIX "Memory allocation error\n");
1012 result = -ENOMEM;
1013 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001014 }
1015
Shaohua Li90905892009-04-07 10:24:29 +08001016 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001017 /*
1018 * Find suitable bus_id and instance number in acpi_bus_id_list
1019 * If failed, create one and link it into acpi_bus_id_list
1020 */
1021 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001022 if (!strcmp(acpi_device_bus_id->bus_id,
1023 acpi_device_hid(device))) {
1024 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001025 found = 1;
1026 kfree(new_bus_id);
1027 break;
1028 }
1029 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001030 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001031 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001032 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001033 acpi_device_bus_id->instance_no = 0;
1034 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1035 }
Kay Sievers07944692008-10-30 01:18:59 +01001036 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 +08001037
Len Brown33b57152008-12-15 22:09:26 -05001038 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (device->wakeup.flags.valid)
1042 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001043 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Patrick Mochel1890a972006-12-07 20:56:31 +08001045 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001046 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001047 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001048 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001049 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001050 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001051 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001052 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001053 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001054
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001055 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001056 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001057 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1058 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001059
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001060 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001061
1062 err:
Shaohua Li90905892009-04-07 10:24:29 +08001063 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001064 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001065 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001066 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001067 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001068
1069 err_detach:
1070 acpi_detach_data(device->handle, acpi_bus_data_handler);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001071 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Rafael J. Wysockib17b5372013-01-15 13:23:44 +01001074static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Shaohua Li90905892009-04-07 10:24:29 +08001076 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001077 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001081 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +08001084
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001085 acpi_power_add_remove_device(device, false);
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001086 acpi_device_remove_files(device);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +01001087 if (device->remove)
1088 device->remove(device);
1089
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001090 device_del(&device->dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001091 /*
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001092 * Transition the device to D3cold to drop the reference counts of all
1093 * power resources the device depends on and turn off the ones that have
1094 * no more references.
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001095 */
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001096 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001097 device->handle = NULL;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001098 put_device(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Zhang Rui9e89dde2006-12-07 20:56:16 +08001101/* --------------------------------------------------------------------------
1102 Driver Management
1103 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001105 * acpi_bus_register_driver - register a driver with the ACPI bus
1106 * @driver: driver being registered
1107 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001109 * devices that match the driver's criteria and binds. Returns zero for
1110 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
Len Brown4be44fc2005-08-05 00:44:28 -04001112int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Patrick Mochel1890a972006-12-07 20:56:31 +08001114 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001117 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001118 driver->drv.name = driver->name;
1119 driver->drv.bus = &acpi_bus_type;
1120 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Patrick Mochel1890a972006-12-07 20:56:31 +08001122 ret = driver_register(&driver->drv);
1123 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Len Brown4be44fc2005-08-05 00:44:28 -04001126EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001129 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1130 * @driver: driver to unregister
1131 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1133 * devices that match the driver's criteria and unbinds.
1134 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001135void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Patrick Mochel1890a972006-12-07 20:56:31 +08001137 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138}
Len Brown4be44fc2005-08-05 00:44:28 -04001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140EXPORT_SYMBOL(acpi_bus_unregister_driver);
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142/* --------------------------------------------------------------------------
1143 Device Enumeration
1144 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001145static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1146{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001147 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001148 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001149
1150 /*
1151 * Fixed hardware devices do not appear in the namespace and do not
1152 * have handles, but we fabricate acpi_devices for them, so we have
1153 * to deal with them specially.
1154 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001155 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001156 return acpi_root;
1157
1158 do {
1159 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001160 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001161 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1162 } while (acpi_bus_get_device(handle, &device));
1163 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001164}
1165
Len Brownc8f7a622006-07-09 17:22:28 -04001166acpi_status
1167acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1168{
1169 acpi_status status;
1170 acpi_handle tmp;
1171 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1172 union acpi_object *obj;
1173
1174 status = acpi_get_handle(handle, "_EJD", &tmp);
1175 if (ACPI_FAILURE(status))
1176 return status;
1177
1178 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1179 if (ACPI_SUCCESS(status)) {
1180 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001181 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1182 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001183 kfree(buffer.pointer);
1184 }
1185 return status;
1186}
1187EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1188
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001189static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1190 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001192 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1193 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001195 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001196 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001198 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001199 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001201 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001203 /* _PRW */
1204 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1205 if (ACPI_FAILURE(status)) {
1206 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001207 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001208 }
1209
1210 package = (union acpi_object *)buffer.pointer;
1211
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001212 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001213 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001216 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001217 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (element->type == ACPI_TYPE_PACKAGE) {
1220 if ((element->package.count < 2) ||
1221 (element->package.elements[0].type !=
1222 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001223 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001224 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001225
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001226 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001228 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 (u32) element->package.elements[1].integer.value;
1230 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001231 wakeup->gpe_device = NULL;
1232 wakeup->gpe_number = element->integer.value;
1233 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001234 goto out;
1235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001238 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001239 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001240
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001241 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001243 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1244 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001245 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001247 if (!list_empty(&wakeup->resources)) {
1248 int sleep_state;
1249
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001250 err = acpi_power_wakeup_list_init(&wakeup->resources,
1251 &sleep_state);
1252 if (err) {
1253 acpi_handle_warn(handle, "Retrieving current states "
1254 "of wakeup power resources failed\n");
1255 acpi_power_resources_list_free(&wakeup->resources);
1256 goto out;
1257 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001258 if (sleep_state < wakeup->sleep_state) {
1259 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1260 "(S%d) by S%d from power resources\n",
1261 (int)wakeup->sleep_state, sleep_state);
1262 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Lin Mingbba63a22010-12-13 13:39:17 +08001265 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001266
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001267 out:
1268 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001269 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001272static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001274 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001275 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001276 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001277 {"PNP0C0E", 0},
1278 {"", 0},
1279 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001280 acpi_status status;
1281 acpi_event_status event_status;
1282
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001283 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001284
1285 /* Power button, Lid switch always enable wakeup */
1286 if (!acpi_match_device_ids(device, button_device_ids)) {
1287 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001288 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1289 /* Do not use Lid/sleep button for S5 wakeup */
1290 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1291 device->wakeup.sleep_state = ACPI_STATE_S4;
1292 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001293 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001294 return;
1295 }
1296
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001297 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1298 device->wakeup.gpe_number,
1299 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001300 if (status == AE_OK)
1301 device->wakeup.flags.run_wake =
1302 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1303}
1304
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001305static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001306{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001307 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001308
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001309 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001310 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001311 return;
1312
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001313 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1314 &device->wakeup);
1315 if (err) {
1316 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001317 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001321 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001322 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001323 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1324 * system for the ACPI device with the _PRW object.
1325 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1326 * So it is necessary to call _DSW object first. Only when it is not
1327 * present will the _PSW object used.
1328 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001329 err = acpi_device_sleep_wake(device, 0, 0, 0);
1330 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001331 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1332 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001335static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001337 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001338 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1339 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001340 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001341
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001342 INIT_LIST_HEAD(&ps->resources);
1343
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001344 /* Evaluate "_PRx" to get referenced power resources */
1345 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1346 if (ACPI_SUCCESS(status)) {
1347 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001348
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001349 if (buffer.length && package
1350 && package->type == ACPI_TYPE_PACKAGE
1351 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001352 int err = acpi_extract_power_resources(package, 0,
1353 &ps->resources);
1354 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001355 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001356 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001357 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001358 }
1359
1360 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001361 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001362 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001363 ps->flags.explicit_set = 1;
1364
1365 /*
1366 * State is valid if there are means to put the device into it.
1367 * D3hot is only valid if _PR3 present.
1368 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001369 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001370 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1371 ps->flags.valid = 1;
1372 ps->flags.os_accessible = 1;
1373 }
1374
1375 ps->power = -1; /* Unknown - driver assigned */
1376 ps->latency = -1; /* Unknown - driver assigned */
1377}
1378
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001379static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001381 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001383 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001384 if (!acpi_has_method(device->handle, "_PS0") &&
1385 !acpi_has_method(device->handle, "_PR0"))
1386 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001387
1388 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001391 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001393 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001394 device->power.flags.explicit_get = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001395 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001396 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001399 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001401 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1402 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001404 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Zhang Rui9e89dde2006-12-07 20:56:16 +08001406 /* Set defaults for D0 and D3 states (always valid) */
1407 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1408 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001409 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1410 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001412 /* Set D3cold's explicit_set flag if _PS3 exists. */
1413 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1414 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1415
Aaron Lu1399dfc2012-11-21 23:33:40 +01001416 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1417 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1418 device->power.flags.power_resources)
1419 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1420
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001421 if (acpi_bus_init_power(device)) {
1422 acpi_free_power_resources_lists(device);
1423 device->flags.power_manageable = 0;
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
1426
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001427static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001430 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 device->flags.dynamic_status = 1;
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001434 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 device->flags.removable = 1;
1436
1437 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001438 if (acpi_has_method(device->handle, "_EJD") ||
1439 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001443static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Len Brown4be44fc2005-08-05 00:44:28 -04001445 char bus_id[5] = { '?', 0 };
1446 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1447 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /*
1450 * Bus ID
1451 * ------
1452 * The device's Bus ID is simply the object name.
1453 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1454 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001455 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001457 return;
1458 }
1459
1460 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 case ACPI_BUS_TYPE_POWER_BUTTON:
1462 strcpy(device->pnp.bus_id, "PWRF");
1463 break;
1464 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1465 strcpy(device->pnp.bus_id, "SLPF");
1466 break;
1467 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001468 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* Clean up trailing underscores (if any) */
1470 for (i = 3; i > 1; i--) {
1471 if (bus_id[i] == '_')
1472 bus_id[i] = '\0';
1473 else
1474 break;
1475 }
1476 strcpy(device->pnp.bus_id, bus_id);
1477 break;
1478 }
1479}
1480
Zhang Rui54735262007-01-11 02:09:09 -05001481/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001482 * acpi_ata_match - see if an acpi object is an ATA device
1483 *
1484 * If an acpi object has one of the ACPI ATA methods defined,
1485 * then we can safely call it an ATA device.
1486 */
1487bool acpi_ata_match(acpi_handle handle)
1488{
1489 return acpi_has_method(handle, "_GTF") ||
1490 acpi_has_method(handle, "_GTM") ||
1491 acpi_has_method(handle, "_STM") ||
1492 acpi_has_method(handle, "_SDD");
1493}
1494
1495/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001496 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001497 *
1498 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1499 * then we can safely call it an ejectable drive bay
1500 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001501bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001502{
Zhang Rui54735262007-01-11 02:09:09 -05001503 acpi_handle phandle;
1504
Jiang Liu952c63e2013-06-29 00:24:38 +08001505 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001506 return false;
1507 if (acpi_ata_match(handle))
1508 return true;
1509 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1510 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001511
Jiang Liuebf4df82013-06-29 00:24:41 +08001512 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001513}
1514
Frank Seidel3620f2f2007-12-07 13:20:34 +01001515/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001516 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001517 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001518bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001519{
Jiang Liuebf4df82013-06-29 00:24:41 +08001520 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001521}
1522
Thomas Renninger620e1122010-10-01 10:54:00 +02001523const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001524{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001525 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001526
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001527 if (list_empty(&device->pnp.ids))
1528 return dummy_hid;
1529
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001530 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1531 return hid->id;
1532}
1533EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001534
Toshi Kanid4e1a692013-03-04 21:30:41 +00001535static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001536{
1537 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001538
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001539 id = kmalloc(sizeof(*id), GFP_KERNEL);
1540 if (!id)
1541 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001542
Thomas Meyer581de592011-08-06 11:32:56 +02001543 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001544 if (!id->id) {
1545 kfree(id);
1546 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001547 }
1548
Toshi Kanid4e1a692013-03-04 21:30:41 +00001549 list_add_tail(&id->list, &pnp->ids);
1550 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001551}
1552
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001553/*
1554 * Old IBM workstations have a DSDT bug wherein the SMBus object
1555 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1556 * prefix. Work around this.
1557 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001558static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001559{
Jiang Liuebf4df82013-06-29 00:24:41 +08001560 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1561 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001562
1563 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001564 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001565
1566 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001567 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1568 strcmp("SMBS", path.pointer))
1569 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001570
1571 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001572 if (acpi_has_method(handle, "SBI") &&
1573 acpi_has_method(handle, "SBR") &&
1574 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001575 return true;
1576
1577 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001578}
1579
Toshi Kanic0af4172013-03-04 21:30:42 +00001580static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1581 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Len Brown4be44fc2005-08-05 00:44:28 -04001583 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001584 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001585 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001586 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Toshi Kanic0af4172013-03-04 21:30:42 +00001588 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001590 if (handle == ACPI_ROOT_OBJECT) {
1591 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001592 break;
1593 }
1594
Toshi Kanic0af4172013-03-04 21:30:42 +00001595 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001597 pr_err(PREFIX "%s: Error reading device info\n",
1598 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return;
1600 }
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001603 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001604 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001605 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001606 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001607 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001610 pnp->bus_address = info->address;
1611 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
Lv Zhengccf78042012-10-30 14:41:07 +01001613 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001614 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001615 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001616
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001617 kfree(info);
1618
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001619 /*
1620 * Some devices don't reliably have _HIDs & _CIDs, so add
1621 * synthetic HIDs to make sure drivers can find them.
1622 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001623 if (acpi_is_video_device(handle))
1624 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001625 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001626 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001627 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001628 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001629 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001630 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1631 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1632 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1633 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1634 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001635 }
Zhang Rui54735262007-01-11 02:09:09 -05001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 break;
1638 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001639 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
1641 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001642 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001645 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 break;
1647 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001648 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 break;
1650 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001651 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 break;
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Toshi Kanic0af4172013-03-04 21:30:42 +00001656void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1657{
1658 struct acpi_hardware_id *id, *tmp;
1659
1660 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1661 kfree(id->id);
1662 kfree(id);
1663 }
1664 kfree(pnp->unique_id);
1665}
1666
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001667void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1668 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001670 INIT_LIST_HEAD(&device->pnp.ids);
1671 device->device_type = type;
1672 device->handle = handle;
1673 device->parent = acpi_bus_get_parent(handle);
1674 STRUCT_TO_INT(device->status) = sta;
1675 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001676 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001677 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001678 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001679 device_initialize(&device->dev);
1680 dev_set_uevent_suppress(&device->dev, true);
1681}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001682
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001683void acpi_device_add_finalize(struct acpi_device *device)
1684{
1685 dev_set_uevent_suppress(&device->dev, false);
1686 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001689static int acpi_add_single_object(struct acpi_device **child,
1690 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001691 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001693 int result;
1694 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001695 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Burman Yan36bcbec2006-12-19 12:56:11 -08001697 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001699 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001700 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001703 acpi_init_device_object(device, handle, type, sta);
1704 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001705 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001707 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001708 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001709 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001710 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001713 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001714 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001715 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1716 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1717 dev_name(&device->dev), (char *) buffer.pointer,
1718 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1719 kfree(buffer.pointer);
1720 *child = device;
1721 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001722}
1723
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001724static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1725 unsigned long long *sta)
1726{
1727 acpi_status status;
1728 acpi_object_type acpi_type;
1729
1730 status = acpi_get_type(handle, &acpi_type);
1731 if (ACPI_FAILURE(status))
1732 return -ENODEV;
1733
1734 switch (acpi_type) {
1735 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1736 case ACPI_TYPE_DEVICE:
1737 *type = ACPI_BUS_TYPE_DEVICE;
1738 status = acpi_bus_get_status_handle(handle, sta);
1739 if (ACPI_FAILURE(status))
1740 return -ENODEV;
1741 break;
1742 case ACPI_TYPE_PROCESSOR:
1743 *type = ACPI_BUS_TYPE_PROCESSOR;
1744 status = acpi_bus_get_status_handle(handle, sta);
1745 if (ACPI_FAILURE(status))
1746 return -ENODEV;
1747 break;
1748 case ACPI_TYPE_THERMAL:
1749 *type = ACPI_BUS_TYPE_THERMAL;
1750 *sta = ACPI_STA_DEFAULT;
1751 break;
1752 case ACPI_TYPE_POWER:
1753 *type = ACPI_BUS_TYPE_POWER;
1754 *sta = ACPI_STA_DEFAULT;
1755 break;
1756 default:
1757 return -ENODEV;
1758 }
1759
1760 return 0;
1761}
1762
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001763static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1764 char *idstr,
1765 const struct acpi_device_id **matchid)
1766{
1767 const struct acpi_device_id *devid;
1768
1769 for (devid = handler->ids; devid->id[0]; devid++)
1770 if (!strcmp((char *)devid->id, idstr)) {
1771 if (matchid)
1772 *matchid = devid;
1773
1774 return true;
1775 }
1776
1777 return false;
1778}
1779
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001780static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1781 const struct acpi_device_id **matchid)
1782{
1783 struct acpi_scan_handler *handler;
1784
1785 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1786 if (acpi_scan_handler_matching(handler, idstr, matchid))
1787 return handler;
1788
1789 return NULL;
1790}
1791
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001792void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1793{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001794 if (!!hotplug->enabled == !!val)
1795 return;
1796
1797 mutex_lock(&acpi_scan_lock);
1798
1799 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001800
1801 mutex_unlock(&acpi_scan_lock);
1802}
1803
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001804static void acpi_scan_init_hotplug(acpi_handle handle, int type)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001805{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001806 struct acpi_device_pnp pnp = {};
1807 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001808 struct acpi_scan_handler *handler;
1809
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001810 INIT_LIST_HEAD(&pnp.ids);
1811 acpi_set_pnp_ids(handle, &pnp, type);
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001812
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001813 if (!pnp.type.hardware_id)
Catalin Marinas7a26b532013-05-15 16:49:35 +00001814 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001815
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001816 /*
1817 * This relies on the fact that acpi_install_notify_handler() will not
1818 * install the same notify handler routine twice for the same handle.
1819 */
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001820 list_for_each_entry(hwid, &pnp.ids, list) {
1821 handler = acpi_scan_match_handler(hwid->id, NULL);
1822 if (handler) {
Toshi Kani2cbb14f2013-03-05 22:33:31 +00001823 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1824 acpi_hotplug_notify_cb, handler);
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001825 break;
1826 }
1827 }
1828
Catalin Marinas7a26b532013-05-15 16:49:35 +00001829out:
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001830 acpi_free_pnp_ids(&pnp);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001831}
1832
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001833static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1834 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001836 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001837 int type;
1838 unsigned long long sta;
1839 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001840
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001841 acpi_bus_get_device(handle, &device);
1842 if (device)
1843 goto out;
1844
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001845 result = acpi_bus_type_and_status(handle, &type, &sta);
1846 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001847 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001849 if (type == ACPI_BUS_TYPE_POWER) {
1850 acpi_add_power_resource(handle);
1851 return AE_OK;
1852 }
1853
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001854 acpi_scan_init_hotplug(handle, type);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001855
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001856 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001857 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001858 struct acpi_device_wakeup wakeup;
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001859
Jiang Liu952c63e2013-06-29 00:24:38 +08001860 if (acpi_has_method(handle, "_PRW")) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001861 acpi_bus_extract_wakeup_device_power_package(handle,
1862 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001863 acpi_power_resources_list_free(&wakeup.resources);
1864 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001865 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001868 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001869 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001870 return AE_CTRL_DEPTH;
1871
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001872 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001873 if (!*return_value)
1874 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001875
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001876 return AE_OK;
1877}
1878
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001879static int acpi_scan_attach_handler(struct acpi_device *device)
1880{
1881 struct acpi_hardware_id *hwid;
1882 int ret = 0;
1883
1884 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001885 const struct acpi_device_id *devid;
1886 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001887
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001888 handler = acpi_scan_match_handler(hwid->id, &devid);
1889 if (handler) {
1890 ret = handler->attach(device, devid);
1891 if (ret > 0) {
1892 device->handler = handler;
1893 break;
1894 } else if (ret < 0) {
1895 break;
1896 }
1897 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001898 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001899 return ret;
1900}
1901
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001902static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1903 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001904{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001905 struct acpi_device *device;
1906 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001907 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001908
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001909 /*
1910 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1911 * namespace walks prematurely.
1912 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001913 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001914 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001915
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001916 if (acpi_bus_get_device(handle, &device))
1917 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001918
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02001919 if (device->handler)
1920 return AE_OK;
1921
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001922 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001923 if (ret < 0)
1924 return AE_CTRL_DEPTH;
1925
1926 device->flags.match_driver = true;
1927 if (ret > 0)
1928 return AE_OK;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001929
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}