blob: c7317fe213bfc34e814f226cf60c5a948b1ec35b [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. Wysocki43d38882013-11-07 01:42:09 +0100288void acpi_bus_device_eject(struct acpi_device *device, u32 ost_src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100289{
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100290 acpi_handle handle = device->handle;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100291 struct acpi_scan_handler *handler;
292 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200293 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100294
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200295 lock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100296 mutex_lock(&acpi_scan_lock);
297
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100298 handler = device->handler;
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100299 if (!handler || !handler->hotplug.enabled) {
300 put_device(&device->dev);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100301 goto err_support;
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100302 }
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100303
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100304 if (ost_src == ACPI_NOTIFY_EJECT_REQUEST)
305 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
306 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
307
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200308 if (handler->hotplug.mode == AHM_CONTAINER)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100309 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100310
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200311 error = acpi_scan_hot_remove(device);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100312 if (error == -EPERM) {
313 goto err_support;
314 } else if (error) {
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200315 goto err_out;
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100318 out:
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100319 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200320 unlock_device_hotplug();
Zhang Ruic8d72a52009-06-22 11:31:16 +0800321 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100322
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100323 err_support:
324 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100325 err_out:
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100326 acpi_evaluate_hotplug_ost(handle, ost_src, ost_code, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100327 goto out;
328}
329
330static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
331{
332 struct acpi_device *device = NULL;
333 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
334 int error;
335
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200336 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200337 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100338
Rafael J. Wysocki8832f7e2013-07-08 02:01:53 +0200339 if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
340 acpi_bus_get_device(handle, &device);
341 if (device) {
342 dev_warn(&device->dev, "Attempt to re-insert\n");
343 goto out;
344 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100345 }
346 acpi_evaluate_hotplug_ost(handle, ost_source,
347 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
348 error = acpi_bus_scan(handle);
349 if (error) {
350 acpi_handle_warn(handle, "Namespace scan failure\n");
351 goto out;
352 }
353 error = acpi_bus_get_device(handle, &device);
354 if (error) {
355 acpi_handle_warn(handle, "Missing device node object\n");
356 goto out;
357 }
358 ost_code = ACPI_OST_SC_SUCCESS;
359 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
360 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
361
362 out:
363 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
364 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200365 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100366}
367
368static void acpi_scan_bus_check(void *context)
369{
370 acpi_scan_bus_device_check((acpi_handle)context,
371 ACPI_NOTIFY_BUS_CHECK);
372}
373
374static void acpi_scan_device_check(void *context)
375{
376 acpi_scan_bus_device_check((acpi_handle)context,
377 ACPI_NOTIFY_DEVICE_CHECK);
378}
379
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000380static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
381{
382 u32 ost_status;
383
384 switch (type) {
385 case ACPI_NOTIFY_BUS_CHECK:
386 acpi_handle_debug(handle,
387 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
388 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
389 break;
390 case ACPI_NOTIFY_DEVICE_CHECK:
391 acpi_handle_debug(handle,
392 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
393 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
394 break;
395 case ACPI_NOTIFY_EJECT_REQUEST:
396 acpi_handle_debug(handle,
397 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
398 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
399 break;
400 default:
401 /* non-hotplug event; possibly handled by other handler */
402 return;
403 }
404
405 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
406}
407
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100408/**
409 * acpi_bus_hot_remove_device: Hot-remove a device and its children.
410 * @context: Address of the ACPI device object to hot-remove.
411 */
Rafael J. Wysocki43d38882013-11-07 01:42:09 +0100412static void acpi_bus_hot_remove_device(void *context)
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100413{
414 acpi_bus_device_eject(context, ACPI_NOTIFY_EJECT_REQUEST);
415}
416
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000417static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100418{
419 acpi_osd_exec_callback callback;
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000420 struct acpi_scan_handler *handler = data;
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100421 struct acpi_device *adev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100422 acpi_status status;
423
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000424 if (!handler->hotplug.enabled)
425 return acpi_hotplug_unsupported(handle, type);
426
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100427 switch (type) {
428 case ACPI_NOTIFY_BUS_CHECK:
429 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
430 callback = acpi_scan_bus_check;
431 break;
432 case ACPI_NOTIFY_DEVICE_CHECK:
433 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
434 callback = acpi_scan_device_check;
435 break;
436 case ACPI_NOTIFY_EJECT_REQUEST:
437 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100438 status = acpi_bus_get_device(handle, &adev);
439 if (ACPI_FAILURE(status))
440 goto err_out;
441
442 get_device(&adev->dev);
443 callback = acpi_bus_hot_remove_device;
444 status = acpi_os_hotplug_execute(callback, adev);
445 if (ACPI_SUCCESS(status))
446 return;
447
448 put_device(&adev->dev);
449 goto err_out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100450 default:
451 /* non-hotplug event; possibly handled by other handler */
452 return;
453 }
454 status = acpi_os_hotplug_execute(callback, handle);
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100455 if (ACPI_SUCCESS(status))
456 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100457
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100458 err_out:
459 acpi_evaluate_hotplug_ost(handle, type,
460 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100463static ssize_t real_power_state_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
465{
466 struct acpi_device *adev = to_acpi_device(dev);
467 int state;
468 int ret;
469
470 ret = acpi_device_get_power(adev, &state);
471 if (ret)
472 return ret;
473
474 return sprintf(buf, "%s\n", acpi_power_state_string(state));
475}
476
477static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
478
479static ssize_t power_state_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
481{
482 struct acpi_device *adev = to_acpi_device(dev);
483
484 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
485}
486
487static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
488
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100489static void acpi_eject_store_work(void *context)
490{
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100491 acpi_bus_device_eject(context, ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100492}
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800495acpi_eject_store(struct device *d, struct device_attribute *attr,
496 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800498 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100499 acpi_object_type not_used;
500 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100502 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100505 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
506 && !acpi_device->driver)
507 return -ENODEV;
508
509 status = acpi_get_type(acpi_device->handle, &not_used);
510 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
511 return -ENODEV;
512
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200513 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100514 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100515 get_device(&acpi_device->dev);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100516 status = acpi_os_hotplug_execute(acpi_eject_store_work, acpi_device);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200517 if (ACPI_SUCCESS(status))
518 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100519
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200520 put_device(&acpi_device->dev);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200521 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100522 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100523 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800526static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800528static ssize_t
529acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
530 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600532 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800533}
534static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
535
Lv Zheng923d4a42012-10-30 14:43:26 +0100536static ssize_t acpi_device_uid_show(struct device *dev,
537 struct device_attribute *attr, char *buf)
538{
539 struct acpi_device *acpi_dev = to_acpi_device(dev);
540
541 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
542}
543static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
544
545static ssize_t acpi_device_adr_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, "0x%08x\n",
551 (unsigned int)(acpi_dev->pnp.bus_address));
552}
553static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
554
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800555static ssize_t
556acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
557 struct acpi_device *acpi_dev = to_acpi_device(dev);
558 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
559 int result;
560
561 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600562 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800563 goto end;
564
565 result = sprintf(buf, "%s\n", (char*)path.pointer);
566 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600567end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800568 return result;
569}
570static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
571
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600572/* sysfs file that shows description text from the ACPI _STR method */
573static ssize_t description_show(struct device *dev,
574 struct device_attribute *attr,
575 char *buf) {
576 struct acpi_device *acpi_dev = to_acpi_device(dev);
577 int result;
578
579 if (acpi_dev->pnp.str_obj == NULL)
580 return 0;
581
582 /*
583 * The _STR object contains a Unicode identifier for a device.
584 * We need to convert to utf-8 so it can be displayed.
585 */
586 result = utf16s_to_utf8s(
587 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
588 acpi_dev->pnp.str_obj->buffer.length,
589 UTF16_LITTLE_ENDIAN, buf,
590 PAGE_SIZE);
591
592 buf[result++] = '\n';
593
594 return result;
595}
596static DEVICE_ATTR(description, 0444, description_show, NULL);
597
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100598static ssize_t
599acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
600 char *buf) {
601 struct acpi_device *acpi_dev = to_acpi_device(dev);
602
603 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
604}
605static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
606
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800607static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600609 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800610 acpi_status status;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100611 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800612 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800614 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800615 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800616 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600617 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800618 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600619 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800620 goto end;
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200623 if (!list_empty(&dev->pnp.ids)) {
624 result = device_create_file(&dev->dev, &dev_attr_hid);
625 if (result)
626 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200628 result = device_create_file(&dev->dev, &dev_attr_modalias);
629 if (result)
630 goto end;
631 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200632
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600633 /*
634 * If device has _STR, 'description' file is created
635 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800636 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600637 status = acpi_evaluate_object(dev->handle, "_STR",
638 NULL, &buffer);
639 if (ACPI_FAILURE(status))
640 buffer.pointer = NULL;
641 dev->pnp.str_obj = buffer.pointer;
642 result = device_create_file(&dev->dev, &dev_attr_description);
643 if (result)
644 goto end;
645 }
646
Toshi Kanid4e1a692013-03-04 21:30:41 +0000647 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100648 result = device_create_file(&dev->dev, &dev_attr_adr);
649 if (dev->pnp.unique_id)
650 result = device_create_file(&dev->dev, &dev_attr_uid);
651
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100652 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
653 if (ACPI_SUCCESS(status)) {
654 dev->pnp.sun = (unsigned long)sun;
655 result = device_create_file(&dev->dev, &dev_attr_sun);
656 if (result)
657 goto end;
658 } else {
659 dev->pnp.sun = (unsigned long)-1;
660 }
661
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800662 /*
663 * If device has _EJ0, 'eject' file is created that is used to trigger
664 * hot-removal function from userland.
665 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800666 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800667 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100668 if (result)
669 return result;
670 }
671
672 if (dev->flags.power_manageable) {
673 result = device_create_file(&dev->dev, &dev_attr_power_state);
674 if (result)
675 return result;
676
677 if (dev->power.flags.power_resources)
678 result = device_create_file(&dev->dev,
679 &dev_attr_real_power_state);
680 }
681
Alex Chiang0c526d92009-05-14 08:31:37 -0600682end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800683 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800684}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800686static void acpi_device_remove_files(struct acpi_device *dev)
687{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100688 if (dev->flags.power_manageable) {
689 device_remove_file(&dev->dev, &dev_attr_power_state);
690 if (dev->power.flags.power_resources)
691 device_remove_file(&dev->dev,
692 &dev_attr_real_power_state);
693 }
694
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800695 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600696 * If device has _STR, remove 'description' file
697 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800698 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600699 kfree(dev->pnp.str_obj);
700 device_remove_file(&dev->dev, &dev_attr_description);
701 }
702 /*
703 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800704 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800705 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800706 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800707
Jiang Liu952c63e2013-06-29 00:24:38 +0800708 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100709 device_remove_file(&dev->dev, &dev_attr_sun);
710
Lv Zheng923d4a42012-10-30 14:43:26 +0100711 if (dev->pnp.unique_id)
712 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000713 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100714 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600715 device_remove_file(&dev->dev, &dev_attr_modalias);
716 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600717 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800718 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800719}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800721 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200723
Mika Westerbergcf761af2012-10-31 22:44:41 +0100724static const struct acpi_device_id *__acpi_match_device(
725 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200726{
727 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600728 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200729
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800730 /*
731 * If the device is not present, it is unnecessary to load device
732 * driver for it.
733 */
734 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100735 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800736
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600737 for (id = ids; id->id[0]; id++)
738 list_for_each_entry(hwid, &device->pnp.ids, list)
739 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100740 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200741
Mika Westerbergcf761af2012-10-31 22:44:41 +0100742 return NULL;
743}
744
745/**
746 * acpi_match_device - Match a struct device against a given list of ACPI IDs
747 * @ids: Array of struct acpi_device_id object to match against.
748 * @dev: The device structure to match.
749 *
750 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
751 * object for that handle and use that object to match against a given list of
752 * device IDs.
753 *
754 * Return a pointer to the first matching ID on success or %NULL on failure.
755 */
756const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
757 const struct device *dev)
758{
759 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100760 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100761
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100762 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100763 return NULL;
764
765 return __acpi_match_device(adev, ids);
766}
767EXPORT_SYMBOL_GPL(acpi_match_device);
768
769int acpi_match_device_ids(struct acpi_device *device,
770 const struct acpi_device_id *ids)
771{
772 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200773}
774EXPORT_SYMBOL(acpi_match_device_ids);
775
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100776static void acpi_free_power_resources_lists(struct acpi_device *device)
777{
778 int i;
779
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100780 if (device->wakeup.flags.valid)
781 acpi_power_resources_list_free(&device->wakeup.resources);
782
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100783 if (!device->flags.power_manageable)
784 return;
785
786 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
787 struct acpi_device_power_state *ps = &device->power.states[i];
788 acpi_power_resources_list_free(&ps->resources);
789 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600790}
791
Patrick Mochel1890a972006-12-07 20:56:31 +0800792static void acpi_device_release(struct device *dev)
793{
794 struct acpi_device *acpi_dev = to_acpi_device(dev);
795
Toshi Kanic0af4172013-03-04 21:30:42 +0000796 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100797 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800798 kfree(acpi_dev);
799}
800
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800801static int acpi_bus_match(struct device *dev, struct device_driver *drv)
802{
803 struct acpi_device *acpi_dev = to_acpi_device(dev);
804 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
805
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100806 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100807 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800808}
809
Kay Sievers7eff2e72007-08-14 15:15:12 +0200810static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800811{
812 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200813 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800814
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200815 if (list_empty(&acpi_dev->pnp.ids))
816 return 0;
817
Kay Sievers7eff2e72007-08-14 15:15:12 +0200818 if (add_uevent_var(env, "MODALIAS="))
819 return -ENOMEM;
820 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
821 sizeof(env->buf) - env->buflen);
822 if (len >= (sizeof(env->buf) - env->buflen))
823 return -ENOMEM;
824 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return 0;
826}
827
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000828static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
829{
830 struct acpi_device *device = data;
831
832 device->driver->ops.notify(device, event);
833}
834
835static acpi_status acpi_device_notify_fixed(void *data)
836{
837 struct acpi_device *device = data;
838
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000839 /* Fixed hardware devices have no handles */
840 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000841 return AE_OK;
842}
843
844static int acpi_device_install_notify_handler(struct acpi_device *device)
845{
846 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000847
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000848 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000849 status =
850 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
851 acpi_device_notify_fixed,
852 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000853 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000854 status =
855 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
856 acpi_device_notify_fixed,
857 device);
858 else
859 status = acpi_install_notify_handler(device->handle,
860 ACPI_DEVICE_NOTIFY,
861 acpi_device_notify,
862 device);
863
864 if (ACPI_FAILURE(status))
865 return -EINVAL;
866 return 0;
867}
868
869static void acpi_device_remove_notify_handler(struct acpi_device *device)
870{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000871 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000872 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
873 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000874 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000875 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
876 acpi_device_notify_fixed);
877 else
878 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
879 acpi_device_notify);
880}
881
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200882static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800883{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800884 struct acpi_device *acpi_dev = to_acpi_device(dev);
885 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
886 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200888 if (acpi_dev->handler)
889 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000890
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200891 if (!acpi_drv->ops.add)
892 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800893
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200894 ret = acpi_drv->ops.add(acpi_dev);
895 if (ret)
896 return ret;
897
898 acpi_dev->driver = acpi_drv;
899 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
900 "Driver [%s] successfully bound to device [%s]\n",
901 acpi_drv->name, acpi_dev->pnp.bus_id));
902
903 if (acpi_drv->ops.notify) {
904 ret = acpi_device_install_notify_handler(acpi_dev);
905 if (ret) {
906 if (acpi_drv->ops.remove)
907 acpi_drv->ops.remove(acpi_dev);
908
909 acpi_dev->driver = NULL;
910 acpi_dev->driver_data = NULL;
911 return ret;
912 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800913 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200914
915 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
916 acpi_drv->name, acpi_dev->pnp.bus_id));
917 get_device(dev);
918 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800919}
920
921static int acpi_device_remove(struct device * dev)
922{
923 struct acpi_device *acpi_dev = to_acpi_device(dev);
924 struct acpi_driver *acpi_drv = acpi_dev->driver;
925
926 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000927 if (acpi_drv->ops.notify)
928 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800929 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100930 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800931 }
932 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700933 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800934
935 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800936 return 0;
937}
938
David Brownell55955aa2007-05-08 00:28:35 -0700939struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800940 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800941 .match = acpi_bus_match,
942 .probe = acpi_device_probe,
943 .remove = acpi_device_remove,
944 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945};
946
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200947static void acpi_bus_data_handler(acpi_handle handle, void *context)
948{
949 /* Intentionally empty. */
950}
951
952int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
953{
954 acpi_status status;
955
956 if (!device)
957 return -EINVAL;
958
959 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
960 if (ACPI_FAILURE(status) || !*device) {
961 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
962 handle));
963 return -ENODEV;
964 }
965 return 0;
966}
967EXPORT_SYMBOL_GPL(acpi_bus_get_device);
968
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100969int acpi_device_add(struct acpi_device *device,
970 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800972 int result;
973 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
974 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000975
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100976 if (device->handle) {
977 acpi_status status;
978
979 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
980 device);
981 if (ACPI_FAILURE(status)) {
982 acpi_handle_err(device->handle,
983 "Unable to attach device data\n");
984 return -ENODEV;
985 }
986 }
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /*
989 * Linkage
990 * -------
991 * Link this device to its parent and siblings.
992 */
993 INIT_LIST_HEAD(&device->children);
994 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800996 INIT_LIST_HEAD(&device->physical_node_list);
997 mutex_init(&device->physical_node_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100998 INIT_LIST_HEAD(&device->power_dependent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001000 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1001 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001002 pr_err(PREFIX "Memory allocation error\n");
1003 result = -ENOMEM;
1004 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001005 }
1006
Shaohua Li90905892009-04-07 10:24:29 +08001007 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001008 /*
1009 * Find suitable bus_id and instance number in acpi_bus_id_list
1010 * If failed, create one and link it into acpi_bus_id_list
1011 */
1012 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001013 if (!strcmp(acpi_device_bus_id->bus_id,
1014 acpi_device_hid(device))) {
1015 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001016 found = 1;
1017 kfree(new_bus_id);
1018 break;
1019 }
1020 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001021 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001022 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001023 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001024 acpi_device_bus_id->instance_no = 0;
1025 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1026 }
Kay Sievers07944692008-10-30 01:18:59 +01001027 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 +08001028
Len Brown33b57152008-12-15 22:09:26 -05001029 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (device->wakeup.flags.valid)
1033 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001034 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Patrick Mochel1890a972006-12-07 20:56:31 +08001036 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001037 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001038 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001039 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001040 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001041 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001042 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001043 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001044 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001045
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001046 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001047 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001048 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1049 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001050
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001051 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001052
1053 err:
Shaohua Li90905892009-04-07 10:24:29 +08001054 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001055 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001056 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001057 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001058 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001059
1060 err_detach:
1061 acpi_detach_data(device->handle, acpi_bus_data_handler);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001062 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Rafael J. Wysockib17b5372013-01-15 13:23:44 +01001065static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Shaohua Li90905892009-04-07 10:24:29 +08001067 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001068 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001072 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +08001075
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001076 acpi_power_add_remove_device(device, false);
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001077 acpi_device_remove_files(device);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +01001078 if (device->remove)
1079 device->remove(device);
1080
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001081 device_del(&device->dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001082 /*
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001083 * Transition the device to D3cold to drop the reference counts of all
1084 * power resources the device depends on and turn off the ones that have
1085 * no more references.
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001086 */
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001087 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001088 device->handle = NULL;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001089 put_device(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Zhang Rui9e89dde2006-12-07 20:56:16 +08001092/* --------------------------------------------------------------------------
1093 Driver Management
1094 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001096 * acpi_bus_register_driver - register a driver with the ACPI bus
1097 * @driver: driver being registered
1098 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001100 * devices that match the driver's criteria and binds. Returns zero for
1101 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 */
Len Brown4be44fc2005-08-05 00:44:28 -04001103int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Patrick Mochel1890a972006-12-07 20:56:31 +08001105 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001108 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001109 driver->drv.name = driver->name;
1110 driver->drv.bus = &acpi_bus_type;
1111 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Patrick Mochel1890a972006-12-07 20:56:31 +08001113 ret = driver_register(&driver->drv);
1114 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Len Brown4be44fc2005-08-05 00:44:28 -04001117EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001120 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1121 * @driver: driver to unregister
1122 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1124 * devices that match the driver's criteria and unbinds.
1125 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001126void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Patrick Mochel1890a972006-12-07 20:56:31 +08001128 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
Len Brown4be44fc2005-08-05 00:44:28 -04001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131EXPORT_SYMBOL(acpi_bus_unregister_driver);
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/* --------------------------------------------------------------------------
1134 Device Enumeration
1135 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001136static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1137{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001138 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001139 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001140
1141 /*
1142 * Fixed hardware devices do not appear in the namespace and do not
1143 * have handles, but we fabricate acpi_devices for them, so we have
1144 * to deal with them specially.
1145 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001146 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001147 return acpi_root;
1148
1149 do {
1150 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001151 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001152 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1153 } while (acpi_bus_get_device(handle, &device));
1154 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001155}
1156
Len Brownc8f7a622006-07-09 17:22:28 -04001157acpi_status
1158acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1159{
1160 acpi_status status;
1161 acpi_handle tmp;
1162 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1163 union acpi_object *obj;
1164
1165 status = acpi_get_handle(handle, "_EJD", &tmp);
1166 if (ACPI_FAILURE(status))
1167 return status;
1168
1169 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1170 if (ACPI_SUCCESS(status)) {
1171 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001172 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1173 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001174 kfree(buffer.pointer);
1175 }
1176 return status;
1177}
1178EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1179
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001180static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1181 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001183 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1184 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001186 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001187 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001189 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001190 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001192 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001194 /* _PRW */
1195 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1196 if (ACPI_FAILURE(status)) {
1197 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001198 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001199 }
1200
1201 package = (union acpi_object *)buffer.pointer;
1202
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001203 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001204 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001207 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001208 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (element->type == ACPI_TYPE_PACKAGE) {
1211 if ((element->package.count < 2) ||
1212 (element->package.elements[0].type !=
1213 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001214 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001215 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001216
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001217 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001219 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 (u32) element->package.elements[1].integer.value;
1221 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001222 wakeup->gpe_device = NULL;
1223 wakeup->gpe_number = element->integer.value;
1224 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001225 goto out;
1226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001229 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001230 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001231
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001232 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001234 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1235 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001238 if (!list_empty(&wakeup->resources)) {
1239 int sleep_state;
1240
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001241 err = acpi_power_wakeup_list_init(&wakeup->resources,
1242 &sleep_state);
1243 if (err) {
1244 acpi_handle_warn(handle, "Retrieving current states "
1245 "of wakeup power resources failed\n");
1246 acpi_power_resources_list_free(&wakeup->resources);
1247 goto out;
1248 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001249 if (sleep_state < wakeup->sleep_state) {
1250 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1251 "(S%d) by S%d from power resources\n",
1252 (int)wakeup->sleep_state, sleep_state);
1253 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Lin Mingbba63a22010-12-13 13:39:17 +08001256 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001257
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001258 out:
1259 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001260 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001263static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001265 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001266 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001267 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001268 {"PNP0C0E", 0},
1269 {"", 0},
1270 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001271 acpi_status status;
1272 acpi_event_status event_status;
1273
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001274 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001275
1276 /* Power button, Lid switch always enable wakeup */
1277 if (!acpi_match_device_ids(device, button_device_ids)) {
1278 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001279 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1280 /* Do not use Lid/sleep button for S5 wakeup */
1281 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1282 device->wakeup.sleep_state = ACPI_STATE_S4;
1283 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001284 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001285 return;
1286 }
1287
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001288 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1289 device->wakeup.gpe_number,
1290 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001291 if (status == AE_OK)
1292 device->wakeup.flags.run_wake =
1293 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1294}
1295
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001296static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001297{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001298 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001299
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001300 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001301 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001302 return;
1303
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001304 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1305 &device->wakeup);
1306 if (err) {
1307 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001308 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001312 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001313 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001314 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1315 * system for the ACPI device with the _PRW object.
1316 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1317 * So it is necessary to call _DSW object first. Only when it is not
1318 * present will the _PSW object used.
1319 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001320 err = acpi_device_sleep_wake(device, 0, 0, 0);
1321 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001322 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1323 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001326static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001328 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001329 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1330 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001331 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001332
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001333 INIT_LIST_HEAD(&ps->resources);
1334
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001335 /* Evaluate "_PRx" to get referenced power resources */
1336 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1337 if (ACPI_SUCCESS(status)) {
1338 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001339
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001340 if (buffer.length && package
1341 && package->type == ACPI_TYPE_PACKAGE
1342 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001343 int err = acpi_extract_power_resources(package, 0,
1344 &ps->resources);
1345 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001346 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001347 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001348 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001349 }
1350
1351 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001352 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001353 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001354 ps->flags.explicit_set = 1;
1355
1356 /*
1357 * State is valid if there are means to put the device into it.
1358 * D3hot is only valid if _PR3 present.
1359 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001360 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001361 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1362 ps->flags.valid = 1;
1363 ps->flags.os_accessible = 1;
1364 }
1365
1366 ps->power = -1; /* Unknown - driver assigned */
1367 ps->latency = -1; /* Unknown - driver assigned */
1368}
1369
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001370static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001372 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001374 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001375 if (!acpi_has_method(device->handle, "_PS0") &&
1376 !acpi_has_method(device->handle, "_PR0"))
1377 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001378
1379 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001382 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001384 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001385 device->power.flags.explicit_get = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001386 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001387 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001390 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001392 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1393 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001395 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Zhang Rui9e89dde2006-12-07 20:56:16 +08001397 /* Set defaults for D0 and D3 states (always valid) */
1398 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1399 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001400 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1401 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001403 /* Set D3cold's explicit_set flag if _PS3 exists. */
1404 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1405 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1406
Aaron Lu1399dfc2012-11-21 23:33:40 +01001407 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1408 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1409 device->power.flags.power_resources)
1410 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1411
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001412 if (acpi_bus_init_power(device)) {
1413 acpi_free_power_resources_lists(device);
1414 device->flags.power_manageable = 0;
1415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001418static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001421 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 device->flags.dynamic_status = 1;
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001425 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 device->flags.removable = 1;
1427
1428 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001429 if (acpi_has_method(device->handle, "_EJD") ||
1430 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001434static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Len Brown4be44fc2005-08-05 00:44:28 -04001436 char bus_id[5] = { '?', 0 };
1437 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1438 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 /*
1441 * Bus ID
1442 * ------
1443 * The device's Bus ID is simply the object name.
1444 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1445 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001446 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001448 return;
1449 }
1450
1451 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 case ACPI_BUS_TYPE_POWER_BUTTON:
1453 strcpy(device->pnp.bus_id, "PWRF");
1454 break;
1455 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1456 strcpy(device->pnp.bus_id, "SLPF");
1457 break;
1458 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001459 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* Clean up trailing underscores (if any) */
1461 for (i = 3; i > 1; i--) {
1462 if (bus_id[i] == '_')
1463 bus_id[i] = '\0';
1464 else
1465 break;
1466 }
1467 strcpy(device->pnp.bus_id, bus_id);
1468 break;
1469 }
1470}
1471
Zhang Rui54735262007-01-11 02:09:09 -05001472/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001473 * acpi_ata_match - see if an acpi object is an ATA device
1474 *
1475 * If an acpi object has one of the ACPI ATA methods defined,
1476 * then we can safely call it an ATA device.
1477 */
1478bool acpi_ata_match(acpi_handle handle)
1479{
1480 return acpi_has_method(handle, "_GTF") ||
1481 acpi_has_method(handle, "_GTM") ||
1482 acpi_has_method(handle, "_STM") ||
1483 acpi_has_method(handle, "_SDD");
1484}
1485
1486/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001487 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001488 *
1489 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1490 * then we can safely call it an ejectable drive bay
1491 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001492bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001493{
Zhang Rui54735262007-01-11 02:09:09 -05001494 acpi_handle phandle;
1495
Jiang Liu952c63e2013-06-29 00:24:38 +08001496 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001497 return false;
1498 if (acpi_ata_match(handle))
1499 return true;
1500 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1501 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001502
Jiang Liuebf4df82013-06-29 00:24:41 +08001503 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001504}
1505
Frank Seidel3620f2f2007-12-07 13:20:34 +01001506/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001507 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001508 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001509bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001510{
Jiang Liuebf4df82013-06-29 00:24:41 +08001511 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001512}
1513
Thomas Renninger620e1122010-10-01 10:54:00 +02001514const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001515{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001516 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001517
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001518 if (list_empty(&device->pnp.ids))
1519 return dummy_hid;
1520
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001521 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1522 return hid->id;
1523}
1524EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001525
Toshi Kanid4e1a692013-03-04 21:30:41 +00001526static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001527{
1528 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001529
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001530 id = kmalloc(sizeof(*id), GFP_KERNEL);
1531 if (!id)
1532 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001533
Thomas Meyer581de592011-08-06 11:32:56 +02001534 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001535 if (!id->id) {
1536 kfree(id);
1537 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001538 }
1539
Toshi Kanid4e1a692013-03-04 21:30:41 +00001540 list_add_tail(&id->list, &pnp->ids);
1541 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001542}
1543
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001544/*
1545 * Old IBM workstations have a DSDT bug wherein the SMBus object
1546 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1547 * prefix. Work around this.
1548 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001549static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001550{
Jiang Liuebf4df82013-06-29 00:24:41 +08001551 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1552 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001553
1554 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001555 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001556
1557 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001558 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1559 strcmp("SMBS", path.pointer))
1560 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001561
1562 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001563 if (acpi_has_method(handle, "SBI") &&
1564 acpi_has_method(handle, "SBR") &&
1565 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001566 return true;
1567
1568 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001569}
1570
Toshi Kanic0af4172013-03-04 21:30:42 +00001571static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1572 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Len Brown4be44fc2005-08-05 00:44:28 -04001574 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001575 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001576 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001577 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Toshi Kanic0af4172013-03-04 21:30:42 +00001579 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001581 if (handle == ACPI_ROOT_OBJECT) {
1582 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001583 break;
1584 }
1585
Toshi Kanic0af4172013-03-04 21:30:42 +00001586 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001588 pr_err(PREFIX "%s: Error reading device info\n",
1589 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return;
1591 }
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001594 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001595 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001596 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001597 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001598 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001601 pnp->bus_address = info->address;
1602 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
Lv Zhengccf78042012-10-30 14:41:07 +01001604 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001605 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001606 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001607
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001608 kfree(info);
1609
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001610 /*
1611 * Some devices don't reliably have _HIDs & _CIDs, so add
1612 * synthetic HIDs to make sure drivers can find them.
1613 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001614 if (acpi_is_video_device(handle))
1615 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001616 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001617 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001618 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001619 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001620 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001621 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1622 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1623 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1624 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1625 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001626 }
Zhang Rui54735262007-01-11 02:09:09 -05001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 break;
1629 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001630 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 break;
1632 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001633 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001636 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 break;
1638 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001639 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
1641 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001642 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 break;
1644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Toshi Kanic0af4172013-03-04 21:30:42 +00001647void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1648{
1649 struct acpi_hardware_id *id, *tmp;
1650
1651 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1652 kfree(id->id);
1653 kfree(id);
1654 }
1655 kfree(pnp->unique_id);
1656}
1657
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001658void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1659 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001661 INIT_LIST_HEAD(&device->pnp.ids);
1662 device->device_type = type;
1663 device->handle = handle;
1664 device->parent = acpi_bus_get_parent(handle);
1665 STRUCT_TO_INT(device->status) = sta;
1666 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001667 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001668 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001669 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001670 device_initialize(&device->dev);
1671 dev_set_uevent_suppress(&device->dev, true);
1672}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001673
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001674void acpi_device_add_finalize(struct acpi_device *device)
1675{
1676 dev_set_uevent_suppress(&device->dev, false);
1677 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001680static int acpi_add_single_object(struct acpi_device **child,
1681 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001682 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001684 int result;
1685 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001686 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Burman Yan36bcbec2006-12-19 12:56:11 -08001688 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001690 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001691 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001694 acpi_init_device_object(device, handle, type, sta);
1695 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001696 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001698 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001699 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001700 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001701 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001704 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001705 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001706 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1707 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1708 dev_name(&device->dev), (char *) buffer.pointer,
1709 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1710 kfree(buffer.pointer);
1711 *child = device;
1712 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001713}
1714
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001715static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1716 unsigned long long *sta)
1717{
1718 acpi_status status;
1719 acpi_object_type acpi_type;
1720
1721 status = acpi_get_type(handle, &acpi_type);
1722 if (ACPI_FAILURE(status))
1723 return -ENODEV;
1724
1725 switch (acpi_type) {
1726 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1727 case ACPI_TYPE_DEVICE:
1728 *type = ACPI_BUS_TYPE_DEVICE;
1729 status = acpi_bus_get_status_handle(handle, sta);
1730 if (ACPI_FAILURE(status))
1731 return -ENODEV;
1732 break;
1733 case ACPI_TYPE_PROCESSOR:
1734 *type = ACPI_BUS_TYPE_PROCESSOR;
1735 status = acpi_bus_get_status_handle(handle, sta);
1736 if (ACPI_FAILURE(status))
1737 return -ENODEV;
1738 break;
1739 case ACPI_TYPE_THERMAL:
1740 *type = ACPI_BUS_TYPE_THERMAL;
1741 *sta = ACPI_STA_DEFAULT;
1742 break;
1743 case ACPI_TYPE_POWER:
1744 *type = ACPI_BUS_TYPE_POWER;
1745 *sta = ACPI_STA_DEFAULT;
1746 break;
1747 default:
1748 return -ENODEV;
1749 }
1750
1751 return 0;
1752}
1753
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001754static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1755 char *idstr,
1756 const struct acpi_device_id **matchid)
1757{
1758 const struct acpi_device_id *devid;
1759
1760 for (devid = handler->ids; devid->id[0]; devid++)
1761 if (!strcmp((char *)devid->id, idstr)) {
1762 if (matchid)
1763 *matchid = devid;
1764
1765 return true;
1766 }
1767
1768 return false;
1769}
1770
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001771static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1772 const struct acpi_device_id **matchid)
1773{
1774 struct acpi_scan_handler *handler;
1775
1776 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1777 if (acpi_scan_handler_matching(handler, idstr, matchid))
1778 return handler;
1779
1780 return NULL;
1781}
1782
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001783void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1784{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001785 if (!!hotplug->enabled == !!val)
1786 return;
1787
1788 mutex_lock(&acpi_scan_lock);
1789
1790 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001791
1792 mutex_unlock(&acpi_scan_lock);
1793}
1794
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001795static void acpi_scan_init_hotplug(acpi_handle handle, int type)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001796{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001797 struct acpi_device_pnp pnp = {};
1798 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001799 struct acpi_scan_handler *handler;
1800
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001801 INIT_LIST_HEAD(&pnp.ids);
1802 acpi_set_pnp_ids(handle, &pnp, type);
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001803
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001804 if (!pnp.type.hardware_id)
Catalin Marinas7a26b532013-05-15 16:49:35 +00001805 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001806
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001807 /*
1808 * This relies on the fact that acpi_install_notify_handler() will not
1809 * install the same notify handler routine twice for the same handle.
1810 */
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001811 list_for_each_entry(hwid, &pnp.ids, list) {
1812 handler = acpi_scan_match_handler(hwid->id, NULL);
1813 if (handler) {
Toshi Kani2cbb14f2013-03-05 22:33:31 +00001814 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1815 acpi_hotplug_notify_cb, handler);
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001816 break;
1817 }
1818 }
1819
Catalin Marinas7a26b532013-05-15 16:49:35 +00001820out:
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001821 acpi_free_pnp_ids(&pnp);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001822}
1823
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001824static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1825 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001827 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001828 int type;
1829 unsigned long long sta;
1830 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001831
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001832 acpi_bus_get_device(handle, &device);
1833 if (device)
1834 goto out;
1835
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001836 result = acpi_bus_type_and_status(handle, &type, &sta);
1837 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001838 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001840 if (type == ACPI_BUS_TYPE_POWER) {
1841 acpi_add_power_resource(handle);
1842 return AE_OK;
1843 }
1844
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001845 acpi_scan_init_hotplug(handle, type);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001846
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001847 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001848 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001849 struct acpi_device_wakeup wakeup;
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001850
Jiang Liu952c63e2013-06-29 00:24:38 +08001851 if (acpi_has_method(handle, "_PRW")) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001852 acpi_bus_extract_wakeup_device_power_package(handle,
1853 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001854 acpi_power_resources_list_free(&wakeup.resources);
1855 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001856 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001859 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001860 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001861 return AE_CTRL_DEPTH;
1862
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001863 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001864 if (!*return_value)
1865 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001866
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001867 return AE_OK;
1868}
1869
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001870static int acpi_scan_attach_handler(struct acpi_device *device)
1871{
1872 struct acpi_hardware_id *hwid;
1873 int ret = 0;
1874
1875 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001876 const struct acpi_device_id *devid;
1877 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001878
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001879 handler = acpi_scan_match_handler(hwid->id, &devid);
1880 if (handler) {
1881 ret = handler->attach(device, devid);
1882 if (ret > 0) {
1883 device->handler = handler;
1884 break;
1885 } else if (ret < 0) {
1886 break;
1887 }
1888 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001889 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001890 return ret;
1891}
1892
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001893static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1894 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001895{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001896 struct acpi_device *device;
1897 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001898 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001899
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001900 /*
1901 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1902 * namespace walks prematurely.
1903 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001904 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001905 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001906
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001907 if (acpi_bus_get_device(handle, &device))
1908 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001909
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02001910 if (device->handler)
1911 return AE_OK;
1912
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001913 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001914 if (ret < 0)
1915 return AE_CTRL_DEPTH;
1916
1917 device->flags.match_driver = true;
1918 if (ret > 0)
1919 return AE_OK;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001920
1921 ret = device_attach(&device->dev);
1922 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001925/**
1926 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1927 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001928 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001929 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1930 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001931 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001932 * If no devices were found, -ENODEV is returned, but it does not mean that
1933 * there has been a real error. There just have been no suitable ACPI objects
1934 * in the table trunk from which the kernel could create a device and add an
1935 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001936 *
1937 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001938 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001939int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001940{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001942 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001943
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001944 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001946 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001947
Thomas Renningerd2f66502010-01-29 17:48:51 +01001948 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001949 error = -ENODEV;
1950 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001951 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001952 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01001953
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001954 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001955}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001956EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001958static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1959 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001961 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001963 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001964 struct acpi_scan_handler *dev_handler = device->handler;
1965
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001966 if (dev_handler) {
1967 if (dev_handler->detach)
1968 dev_handler->detach(device);
1969
1970 device->handler = NULL;
1971 } else {
1972 device_release_driver(&device->dev);
1973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001975 return AE_OK;
1976}
1977
1978static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1979 void *not_used, void **ret_not_used)
1980{
1981 struct acpi_device *device = NULL;
1982
1983 if (!acpi_bus_get_device(handle, &device))
1984 acpi_device_unregister(device);
1985
1986 return AE_OK;
1987}
1988
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001989/**
1990 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1991 * @start: Root of the ACPI device nodes subtree to remove.
1992 *
1993 * Must be called under acpi_scan_lock.
1994 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01001995void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001997 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001998 * Execute acpi_bus_device_detach() as a post-order callback to detach
1999 * all ACPI drivers from the device nodes being removed.
2000 */
2001 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2002 acpi_bus_device_detach, NULL, NULL);
2003 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
2004 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002005 * Execute acpi_bus_remove() as a post-order callback to remove device
2006 * nodes in the given namespace scope.
2007 */
2008 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2009 acpi_bus_remove, NULL, NULL);
2010 acpi_bus_remove(start->handle, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
Kristen Accardiceaba662006-02-23 17:56:01 -08002012EXPORT_SYMBOL_GPL(acpi_bus_trim);
2013
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002014static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Len Brown4be44fc2005-08-05 00:44:28 -04002016 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /*
2019 * Enumerate all fixed-feature devices.
2020 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002021 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2022 struct acpi_device *device = NULL;
2023
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002024 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002025 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002026 ACPI_STA_DEFAULT);
2027 if (result)
2028 return result;
2029
2030 result = device_attach(&device->dev);
2031 if (result < 0)
2032 return result;
2033
Daniel Drakec10d7a12012-05-10 00:08:43 +01002034 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002037 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2038 struct acpi_device *device = NULL;
2039
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002040 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002041 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002042 ACPI_STA_DEFAULT);
2043 if (result)
2044 return result;
2045
2046 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002049 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
Bjorn Helgaase747f272009-03-24 16:49:43 -06002052int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Patrick Mochel5b327262006-05-10 10:33:00 -04002056 result = bus_register(&acpi_bus_type);
2057 if (result) {
2058 /* We don't want to quit even if we failed to add suspend/resume */
2059 printk(KERN_ERR PREFIX "Could not register bus type\n");
2060 }
2061
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002062 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002063 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002064 acpi_processor_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002065 acpi_platform_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002066 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002067 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002068 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002069 acpi_memory_hotplug_init();
Jiang Liu94add0f2013-06-23 00:59:55 +02002070 acpi_dock_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002071
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002072 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 * Enumerate devices in the ACPI namespace.
2075 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002076 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002078 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002080 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002082 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002084 result = acpi_bus_scan_fixed();
2085 if (result) {
2086 acpi_device_unregister(acpi_root);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002087 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002088 }
2089
2090 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002091
Yinghai Lu668192b2013-01-21 13:20:48 -08002092 acpi_pci_root_hp_init();
2093
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002094 out:
2095 mutex_unlock(&acpi_scan_lock);
2096 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}