blob: 55f9dedbbf9fedc5aae2cf21852c7474514cd0d1 [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. Wysocki7b981182013-11-07 01:45:40 +0100288void acpi_bus_device_eject(void *data, u32 ost_src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100289{
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100290 struct acpi_device *device = data;
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100291 acpi_handle handle = device->handle;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100292 struct acpi_scan_handler *handler;
293 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200294 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100295
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200296 lock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100297 mutex_lock(&acpi_scan_lock);
298
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100299 handler = device->handler;
300 if (!handler || !handler->hotplug.enabled) {
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100301 put_device(&device->dev);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100302 goto err_support;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100303 }
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100304
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100305 if (ost_src == ACPI_NOTIFY_EJECT_REQUEST)
306 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
307 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
308
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200309 if (handler->hotplug.mode == AHM_CONTAINER)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100310 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100311
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200312 error = acpi_scan_hot_remove(device);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100313 if (error == -EPERM) {
314 goto err_support;
315 } else if (error) {
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200316 goto err_out;
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100319 out:
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100320 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200321 unlock_device_hotplug();
Zhang Ruic8d72a52009-06-22 11:31:16 +0800322 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100323
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100324 err_support:
325 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100326 err_out:
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100327 acpi_evaluate_hotplug_ost(handle, ost_src, ost_code, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100328 goto out;
329}
330
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100331static void acpi_scan_bus_device_check(void *data, u32 ost_source)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100332{
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100333 acpi_handle handle = data;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100334 struct acpi_device *device = NULL;
335 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
336 int error;
337
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200338 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200339 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100340
Rafael J. Wysocki8832f7e2013-07-08 02:01:53 +0200341 if (ost_source != ACPI_NOTIFY_BUS_CHECK) {
342 acpi_bus_get_device(handle, &device);
343 if (device) {
344 dev_warn(&device->dev, "Attempt to re-insert\n");
345 goto out;
346 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100347 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100348 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
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000368static void acpi_hotplug_unsupported(acpi_handle handle, u32 type)
369{
370 u32 ost_status;
371
372 switch (type) {
373 case ACPI_NOTIFY_BUS_CHECK:
374 acpi_handle_debug(handle,
375 "ACPI_NOTIFY_BUS_CHECK event: unsupported\n");
376 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
377 break;
378 case ACPI_NOTIFY_DEVICE_CHECK:
379 acpi_handle_debug(handle,
380 "ACPI_NOTIFY_DEVICE_CHECK event: unsupported\n");
381 ost_status = ACPI_OST_SC_INSERT_NOT_SUPPORTED;
382 break;
383 case ACPI_NOTIFY_EJECT_REQUEST:
384 acpi_handle_debug(handle,
385 "ACPI_NOTIFY_EJECT_REQUEST event: unsupported\n");
386 ost_status = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
387 break;
388 default:
389 /* non-hotplug event; possibly handled by other handler */
390 return;
391 }
392
393 acpi_evaluate_hotplug_ost(handle, type, ost_status, NULL);
394}
395
396static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100397{
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000398 struct acpi_scan_handler *handler = data;
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100399 struct acpi_device *adev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100400 acpi_status status;
401
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000402 if (!handler->hotplug.enabled)
403 return acpi_hotplug_unsupported(handle, type);
404
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100405 switch (type) {
406 case ACPI_NOTIFY_BUS_CHECK:
407 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100408 break;
409 case ACPI_NOTIFY_DEVICE_CHECK:
410 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100411 break;
412 case ACPI_NOTIFY_EJECT_REQUEST:
413 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100414 status = acpi_bus_get_device(handle, &adev);
415 if (ACPI_FAILURE(status))
416 goto err_out;
417
418 get_device(&adev->dev);
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100419 status = acpi_hotplug_execute(acpi_bus_device_eject, adev, type);
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100420 if (ACPI_SUCCESS(status))
421 return;
422
423 put_device(&adev->dev);
424 goto err_out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100425 default:
426 /* non-hotplug event; possibly handled by other handler */
427 return;
428 }
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100429 status = acpi_hotplug_execute(acpi_scan_bus_device_check, handle, type);
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100430 if (ACPI_SUCCESS(status))
431 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100432
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100433 err_out:
434 acpi_evaluate_hotplug_ost(handle, type,
435 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100436}
437
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100438static ssize_t real_power_state_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
440{
441 struct acpi_device *adev = to_acpi_device(dev);
442 int state;
443 int ret;
444
445 ret = acpi_device_get_power(adev, &state);
446 if (ret)
447 return ret;
448
449 return sprintf(buf, "%s\n", acpi_power_state_string(state));
450}
451
452static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
453
454static ssize_t power_state_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
456{
457 struct acpi_device *adev = to_acpi_device(dev);
458
459 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
460}
461
462static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800465acpi_eject_store(struct device *d, struct device_attribute *attr,
466 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800468 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100469 acpi_object_type not_used;
470 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100472 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100475 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
476 && !acpi_device->driver)
477 return -ENODEV;
478
479 status = acpi_get_type(acpi_device->handle, &not_used);
480 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
481 return -ENODEV;
482
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200483 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100484 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100485 get_device(&acpi_device->dev);
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100486 status = acpi_hotplug_execute(acpi_bus_device_eject, acpi_device,
487 ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200488 if (ACPI_SUCCESS(status))
489 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100490
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200491 put_device(&acpi_device->dev);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200492 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100493 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100494 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800497static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800499static ssize_t
500acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
501 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600503 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800504}
505static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
506
Lv Zheng923d4a42012-10-30 14:43:26 +0100507static ssize_t acpi_device_uid_show(struct device *dev,
508 struct device_attribute *attr, char *buf)
509{
510 struct acpi_device *acpi_dev = to_acpi_device(dev);
511
512 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
513}
514static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
515
516static ssize_t acpi_device_adr_show(struct device *dev,
517 struct device_attribute *attr, char *buf)
518{
519 struct acpi_device *acpi_dev = to_acpi_device(dev);
520
521 return sprintf(buf, "0x%08x\n",
522 (unsigned int)(acpi_dev->pnp.bus_address));
523}
524static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
525
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800526static ssize_t
527acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
528 struct acpi_device *acpi_dev = to_acpi_device(dev);
529 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
530 int result;
531
532 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600533 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800534 goto end;
535
536 result = sprintf(buf, "%s\n", (char*)path.pointer);
537 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600538end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800539 return result;
540}
541static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
542
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600543/* sysfs file that shows description text from the ACPI _STR method */
544static ssize_t description_show(struct device *dev,
545 struct device_attribute *attr,
546 char *buf) {
547 struct acpi_device *acpi_dev = to_acpi_device(dev);
548 int result;
549
550 if (acpi_dev->pnp.str_obj == NULL)
551 return 0;
552
553 /*
554 * The _STR object contains a Unicode identifier for a device.
555 * We need to convert to utf-8 so it can be displayed.
556 */
557 result = utf16s_to_utf8s(
558 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
559 acpi_dev->pnp.str_obj->buffer.length,
560 UTF16_LITTLE_ENDIAN, buf,
561 PAGE_SIZE);
562
563 buf[result++] = '\n';
564
565 return result;
566}
567static DEVICE_ATTR(description, 0444, description_show, NULL);
568
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100569static ssize_t
570acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
571 char *buf) {
572 struct acpi_device *acpi_dev = to_acpi_device(dev);
573
574 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
575}
576static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
577
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800578static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600580 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800581 acpi_status status;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100582 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800583 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800585 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800586 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800587 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600588 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800589 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600590 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800591 goto end;
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200594 if (!list_empty(&dev->pnp.ids)) {
595 result = device_create_file(&dev->dev, &dev_attr_hid);
596 if (result)
597 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200599 result = device_create_file(&dev->dev, &dev_attr_modalias);
600 if (result)
601 goto end;
602 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200603
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600604 /*
605 * If device has _STR, 'description' file is created
606 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800607 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600608 status = acpi_evaluate_object(dev->handle, "_STR",
609 NULL, &buffer);
610 if (ACPI_FAILURE(status))
611 buffer.pointer = NULL;
612 dev->pnp.str_obj = buffer.pointer;
613 result = device_create_file(&dev->dev, &dev_attr_description);
614 if (result)
615 goto end;
616 }
617
Toshi Kanid4e1a692013-03-04 21:30:41 +0000618 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100619 result = device_create_file(&dev->dev, &dev_attr_adr);
620 if (dev->pnp.unique_id)
621 result = device_create_file(&dev->dev, &dev_attr_uid);
622
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100623 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
624 if (ACPI_SUCCESS(status)) {
625 dev->pnp.sun = (unsigned long)sun;
626 result = device_create_file(&dev->dev, &dev_attr_sun);
627 if (result)
628 goto end;
629 } else {
630 dev->pnp.sun = (unsigned long)-1;
631 }
632
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800633 /*
634 * If device has _EJ0, 'eject' file is created that is used to trigger
635 * hot-removal function from userland.
636 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800637 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800638 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100639 if (result)
640 return result;
641 }
642
643 if (dev->flags.power_manageable) {
644 result = device_create_file(&dev->dev, &dev_attr_power_state);
645 if (result)
646 return result;
647
648 if (dev->power.flags.power_resources)
649 result = device_create_file(&dev->dev,
650 &dev_attr_real_power_state);
651 }
652
Alex Chiang0c526d92009-05-14 08:31:37 -0600653end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800654 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800655}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800657static void acpi_device_remove_files(struct acpi_device *dev)
658{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100659 if (dev->flags.power_manageable) {
660 device_remove_file(&dev->dev, &dev_attr_power_state);
661 if (dev->power.flags.power_resources)
662 device_remove_file(&dev->dev,
663 &dev_attr_real_power_state);
664 }
665
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800666 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600667 * If device has _STR, remove 'description' file
668 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800669 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600670 kfree(dev->pnp.str_obj);
671 device_remove_file(&dev->dev, &dev_attr_description);
672 }
673 /*
674 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800675 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800676 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800677 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800678
Jiang Liu952c63e2013-06-29 00:24:38 +0800679 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100680 device_remove_file(&dev->dev, &dev_attr_sun);
681
Lv Zheng923d4a42012-10-30 14:43:26 +0100682 if (dev->pnp.unique_id)
683 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000684 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100685 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600686 device_remove_file(&dev->dev, &dev_attr_modalias);
687 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600688 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800689 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800690}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800692 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200694
Mika Westerbergcf761af2012-10-31 22:44:41 +0100695static const struct acpi_device_id *__acpi_match_device(
696 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200697{
698 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600699 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200700
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800701 /*
702 * If the device is not present, it is unnecessary to load device
703 * driver for it.
704 */
705 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100706 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800707
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600708 for (id = ids; id->id[0]; id++)
709 list_for_each_entry(hwid, &device->pnp.ids, list)
710 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100711 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200712
Mika Westerbergcf761af2012-10-31 22:44:41 +0100713 return NULL;
714}
715
716/**
717 * acpi_match_device - Match a struct device against a given list of ACPI IDs
718 * @ids: Array of struct acpi_device_id object to match against.
719 * @dev: The device structure to match.
720 *
721 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
722 * object for that handle and use that object to match against a given list of
723 * device IDs.
724 *
725 * Return a pointer to the first matching ID on success or %NULL on failure.
726 */
727const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
728 const struct device *dev)
729{
730 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100731 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100732
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100733 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100734 return NULL;
735
736 return __acpi_match_device(adev, ids);
737}
738EXPORT_SYMBOL_GPL(acpi_match_device);
739
740int acpi_match_device_ids(struct acpi_device *device,
741 const struct acpi_device_id *ids)
742{
743 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200744}
745EXPORT_SYMBOL(acpi_match_device_ids);
746
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100747static void acpi_free_power_resources_lists(struct acpi_device *device)
748{
749 int i;
750
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100751 if (device->wakeup.flags.valid)
752 acpi_power_resources_list_free(&device->wakeup.resources);
753
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100754 if (!device->flags.power_manageable)
755 return;
756
757 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
758 struct acpi_device_power_state *ps = &device->power.states[i];
759 acpi_power_resources_list_free(&ps->resources);
760 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600761}
762
Patrick Mochel1890a972006-12-07 20:56:31 +0800763static void acpi_device_release(struct device *dev)
764{
765 struct acpi_device *acpi_dev = to_acpi_device(dev);
766
Toshi Kanic0af4172013-03-04 21:30:42 +0000767 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100768 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800769 kfree(acpi_dev);
770}
771
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800772static int acpi_bus_match(struct device *dev, struct device_driver *drv)
773{
774 struct acpi_device *acpi_dev = to_acpi_device(dev);
775 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
776
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100777 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100778 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800779}
780
Kay Sievers7eff2e72007-08-14 15:15:12 +0200781static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800782{
783 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200784 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800785
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200786 if (list_empty(&acpi_dev->pnp.ids))
787 return 0;
788
Kay Sievers7eff2e72007-08-14 15:15:12 +0200789 if (add_uevent_var(env, "MODALIAS="))
790 return -ENOMEM;
791 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
792 sizeof(env->buf) - env->buflen);
793 if (len >= (sizeof(env->buf) - env->buflen))
794 return -ENOMEM;
795 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return 0;
797}
798
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000799static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
800{
801 struct acpi_device *device = data;
802
803 device->driver->ops.notify(device, event);
804}
805
806static acpi_status acpi_device_notify_fixed(void *data)
807{
808 struct acpi_device *device = data;
809
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000810 /* Fixed hardware devices have no handles */
811 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000812 return AE_OK;
813}
814
815static int acpi_device_install_notify_handler(struct acpi_device *device)
816{
817 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000818
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000819 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000820 status =
821 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
822 acpi_device_notify_fixed,
823 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000824 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000825 status =
826 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
827 acpi_device_notify_fixed,
828 device);
829 else
830 status = acpi_install_notify_handler(device->handle,
831 ACPI_DEVICE_NOTIFY,
832 acpi_device_notify,
833 device);
834
835 if (ACPI_FAILURE(status))
836 return -EINVAL;
837 return 0;
838}
839
840static void acpi_device_remove_notify_handler(struct acpi_device *device)
841{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000842 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000843 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
844 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000845 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000846 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
847 acpi_device_notify_fixed);
848 else
849 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
850 acpi_device_notify);
851}
852
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200853static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800854{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800855 struct acpi_device *acpi_dev = to_acpi_device(dev);
856 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
857 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200859 if (acpi_dev->handler)
860 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000861
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200862 if (!acpi_drv->ops.add)
863 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800864
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200865 ret = acpi_drv->ops.add(acpi_dev);
866 if (ret)
867 return ret;
868
869 acpi_dev->driver = acpi_drv;
870 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
871 "Driver [%s] successfully bound to device [%s]\n",
872 acpi_drv->name, acpi_dev->pnp.bus_id));
873
874 if (acpi_drv->ops.notify) {
875 ret = acpi_device_install_notify_handler(acpi_dev);
876 if (ret) {
877 if (acpi_drv->ops.remove)
878 acpi_drv->ops.remove(acpi_dev);
879
880 acpi_dev->driver = NULL;
881 acpi_dev->driver_data = NULL;
882 return ret;
883 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800884 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200885
886 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
887 acpi_drv->name, acpi_dev->pnp.bus_id));
888 get_device(dev);
889 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800890}
891
892static int acpi_device_remove(struct device * dev)
893{
894 struct acpi_device *acpi_dev = to_acpi_device(dev);
895 struct acpi_driver *acpi_drv = acpi_dev->driver;
896
897 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000898 if (acpi_drv->ops.notify)
899 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800900 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100901 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800902 }
903 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700904 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800905
906 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800907 return 0;
908}
909
David Brownell55955aa2007-05-08 00:28:35 -0700910struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800911 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800912 .match = acpi_bus_match,
913 .probe = acpi_device_probe,
914 .remove = acpi_device_remove,
915 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916};
917
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200918static void acpi_bus_data_handler(acpi_handle handle, void *context)
919{
920 /* Intentionally empty. */
921}
922
923int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
924{
925 acpi_status status;
926
927 if (!device)
928 return -EINVAL;
929
930 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
931 if (ACPI_FAILURE(status) || !*device) {
932 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
933 handle));
934 return -ENODEV;
935 }
936 return 0;
937}
Rafael J. Wysocki65859252013-10-01 23:02:43 +0200938EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200939
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100940int acpi_device_add(struct acpi_device *device,
941 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800943 int result;
944 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
945 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000946
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100947 if (device->handle) {
948 acpi_status status;
949
950 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
951 device);
952 if (ACPI_FAILURE(status)) {
953 acpi_handle_err(device->handle,
954 "Unable to attach device data\n");
955 return -ENODEV;
956 }
957 }
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 /*
960 * Linkage
961 * -------
962 * Link this device to its parent and siblings.
963 */
964 INIT_LIST_HEAD(&device->children);
965 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800967 INIT_LIST_HEAD(&device->physical_node_list);
968 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800970 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
971 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100972 pr_err(PREFIX "Memory allocation error\n");
973 result = -ENOMEM;
974 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800975 }
976
Shaohua Li90905892009-04-07 10:24:29 +0800977 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800978 /*
979 * Find suitable bus_id and instance number in acpi_bus_id_list
980 * If failed, create one and link it into acpi_bus_id_list
981 */
982 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600983 if (!strcmp(acpi_device_bus_id->bus_id,
984 acpi_device_hid(device))) {
985 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800986 found = 1;
987 kfree(new_bus_id);
988 break;
989 }
990 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600991 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800992 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600993 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800994 acpi_device_bus_id->instance_no = 0;
995 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
996 }
Kay Sievers07944692008-10-30 01:18:59 +0100997 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 +0800998
Len Brown33b57152008-12-15 22:09:26 -0500999 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (device->wakeup.flags.valid)
1003 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001004 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Patrick Mochel1890a972006-12-07 20:56:31 +08001006 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001007 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001008 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001009 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001010 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001011 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001012 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001013 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001014 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001015
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001016 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001017 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001018 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1019 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001020
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001021 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001022
1023 err:
Shaohua Li90905892009-04-07 10:24:29 +08001024 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001025 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001026 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001027 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001028 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001029
1030 err_detach:
1031 acpi_detach_data(device->handle, acpi_bus_data_handler);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001032 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Rafael J. Wysockib17b5372013-01-15 13:23:44 +01001035static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Shaohua Li90905892009-04-07 10:24:29 +08001037 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001038 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001042 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +08001045
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001046 acpi_power_add_remove_device(device, false);
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001047 acpi_device_remove_files(device);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +01001048 if (device->remove)
1049 device->remove(device);
1050
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001051 device_del(&device->dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001052 /*
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001053 * Transition the device to D3cold to drop the reference counts of all
1054 * power resources the device depends on and turn off the ones that have
1055 * no more references.
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +01001056 */
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +01001057 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001058 device->handle = NULL;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001059 put_device(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Zhang Rui9e89dde2006-12-07 20:56:16 +08001062/* --------------------------------------------------------------------------
1063 Driver Management
1064 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001066 * acpi_bus_register_driver - register a driver with the ACPI bus
1067 * @driver: driver being registered
1068 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001070 * devices that match the driver's criteria and binds. Returns zero for
1071 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
Len Brown4be44fc2005-08-05 00:44:28 -04001073int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Patrick Mochel1890a972006-12-07 20:56:31 +08001075 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001078 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001079 driver->drv.name = driver->name;
1080 driver->drv.bus = &acpi_bus_type;
1081 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Patrick Mochel1890a972006-12-07 20:56:31 +08001083 ret = driver_register(&driver->drv);
1084 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Len Brown4be44fc2005-08-05 00:44:28 -04001087EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001090 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001091 * @driver: driver to unregister
1092 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1094 * devices that match the driver's criteria and unbinds.
1095 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001096void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Patrick Mochel1890a972006-12-07 20:56:31 +08001098 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
Len Brown4be44fc2005-08-05 00:44:28 -04001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101EXPORT_SYMBOL(acpi_bus_unregister_driver);
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103/* --------------------------------------------------------------------------
1104 Device Enumeration
1105 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001106static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1107{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001108 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001109 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001110
1111 /*
1112 * Fixed hardware devices do not appear in the namespace and do not
1113 * have handles, but we fabricate acpi_devices for them, so we have
1114 * to deal with them specially.
1115 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001116 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001117 return acpi_root;
1118
1119 do {
1120 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001121 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001122 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1123 } while (acpi_bus_get_device(handle, &device));
1124 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001125}
1126
Len Brownc8f7a622006-07-09 17:22:28 -04001127acpi_status
1128acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1129{
1130 acpi_status status;
1131 acpi_handle tmp;
1132 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1133 union acpi_object *obj;
1134
1135 status = acpi_get_handle(handle, "_EJD", &tmp);
1136 if (ACPI_FAILURE(status))
1137 return status;
1138
1139 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1140 if (ACPI_SUCCESS(status)) {
1141 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001142 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1143 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001144 kfree(buffer.pointer);
1145 }
1146 return status;
1147}
1148EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1149
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001150static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1151 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001153 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1154 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001156 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001157 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001159 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001160 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001162 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001164 /* _PRW */
1165 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1166 if (ACPI_FAILURE(status)) {
1167 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001168 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001169 }
1170
1171 package = (union acpi_object *)buffer.pointer;
1172
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001173 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001174 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001177 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001178 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (element->type == ACPI_TYPE_PACKAGE) {
1181 if ((element->package.count < 2) ||
1182 (element->package.elements[0].type !=
1183 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001184 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001185 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001186
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001187 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001189 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 (u32) element->package.elements[1].integer.value;
1191 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001192 wakeup->gpe_device = NULL;
1193 wakeup->gpe_number = element->integer.value;
1194 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001195 goto out;
1196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001199 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001200 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001201
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001202 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001204 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1205 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001206 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001208 if (!list_empty(&wakeup->resources)) {
1209 int sleep_state;
1210
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001211 err = acpi_power_wakeup_list_init(&wakeup->resources,
1212 &sleep_state);
1213 if (err) {
1214 acpi_handle_warn(handle, "Retrieving current states "
1215 "of wakeup power resources failed\n");
1216 acpi_power_resources_list_free(&wakeup->resources);
1217 goto out;
1218 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001219 if (sleep_state < wakeup->sleep_state) {
1220 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1221 "(S%d) by S%d from power resources\n",
1222 (int)wakeup->sleep_state, sleep_state);
1223 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
Lin Mingbba63a22010-12-13 13:39:17 +08001226 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001227
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001228 out:
1229 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001230 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001233static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001235 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001236 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001237 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001238 {"PNP0C0E", 0},
1239 {"", 0},
1240 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001241 acpi_status status;
1242 acpi_event_status event_status;
1243
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001244 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001245
1246 /* Power button, Lid switch always enable wakeup */
1247 if (!acpi_match_device_ids(device, button_device_ids)) {
1248 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001249 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1250 /* Do not use Lid/sleep button for S5 wakeup */
1251 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1252 device->wakeup.sleep_state = ACPI_STATE_S4;
1253 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001254 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001255 return;
1256 }
1257
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001258 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1259 device->wakeup.gpe_number,
1260 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001261 if (status == AE_OK)
1262 device->wakeup.flags.run_wake =
1263 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1264}
1265
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001266static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001267{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001268 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001269
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001270 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001271 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001272 return;
1273
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001274 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1275 &device->wakeup);
1276 if (err) {
1277 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001278 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001282 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001283 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001284 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1285 * system for the ACPI device with the _PRW object.
1286 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1287 * So it is necessary to call _DSW object first. Only when it is not
1288 * present will the _PSW object used.
1289 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001290 err = acpi_device_sleep_wake(device, 0, 0, 0);
1291 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001292 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1293 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001296static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001298 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001299 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1300 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001301 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001302
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001303 INIT_LIST_HEAD(&ps->resources);
1304
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001305 /* Evaluate "_PRx" to get referenced power resources */
1306 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1307 if (ACPI_SUCCESS(status)) {
1308 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001309
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001310 if (buffer.length && package
1311 && package->type == ACPI_TYPE_PACKAGE
1312 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001313 int err = acpi_extract_power_resources(package, 0,
1314 &ps->resources);
1315 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001316 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001317 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001318 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001319 }
1320
1321 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001322 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001323 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001324 ps->flags.explicit_set = 1;
1325
1326 /*
1327 * State is valid if there are means to put the device into it.
1328 * D3hot is only valid if _PR3 present.
1329 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001330 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001331 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1332 ps->flags.valid = 1;
1333 ps->flags.os_accessible = 1;
1334 }
1335
1336 ps->power = -1; /* Unknown - driver assigned */
1337 ps->latency = -1; /* Unknown - driver assigned */
1338}
1339
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001340static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001342 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001344 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001345 if (!acpi_has_method(device->handle, "_PS0") &&
1346 !acpi_has_method(device->handle, "_PR0"))
1347 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001348
1349 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001352 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001354 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001355 device->power.flags.explicit_get = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001356 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001357 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001360 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001362 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1363 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001365 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Zhang Rui9e89dde2006-12-07 20:56:16 +08001367 /* Set defaults for D0 and D3 states (always valid) */
1368 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1369 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001370 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1371 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001373 /* Set D3cold's explicit_set flag if _PS3 exists. */
1374 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1375 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1376
Aaron Lu1399dfc2012-11-21 23:33:40 +01001377 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1378 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1379 device->power.flags.power_resources)
1380 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1381
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001382 if (acpi_bus_init_power(device)) {
1383 acpi_free_power_resources_lists(device);
1384 device->flags.power_manageable = 0;
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001388static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001391 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 device->flags.dynamic_status = 1;
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001395 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 device->flags.removable = 1;
1397
1398 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001399 if (acpi_has_method(device->handle, "_EJD") ||
1400 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001404static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Len Brown4be44fc2005-08-05 00:44:28 -04001406 char bus_id[5] = { '?', 0 };
1407 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1408 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /*
1411 * Bus ID
1412 * ------
1413 * The device's Bus ID is simply the object name.
1414 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1415 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001416 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001418 return;
1419 }
1420
1421 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 case ACPI_BUS_TYPE_POWER_BUTTON:
1423 strcpy(device->pnp.bus_id, "PWRF");
1424 break;
1425 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1426 strcpy(device->pnp.bus_id, "SLPF");
1427 break;
1428 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001429 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /* Clean up trailing underscores (if any) */
1431 for (i = 3; i > 1; i--) {
1432 if (bus_id[i] == '_')
1433 bus_id[i] = '\0';
1434 else
1435 break;
1436 }
1437 strcpy(device->pnp.bus_id, bus_id);
1438 break;
1439 }
1440}
1441
Zhang Rui54735262007-01-11 02:09:09 -05001442/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001443 * acpi_ata_match - see if an acpi object is an ATA device
1444 *
1445 * If an acpi object has one of the ACPI ATA methods defined,
1446 * then we can safely call it an ATA device.
1447 */
1448bool acpi_ata_match(acpi_handle handle)
1449{
1450 return acpi_has_method(handle, "_GTF") ||
1451 acpi_has_method(handle, "_GTM") ||
1452 acpi_has_method(handle, "_STM") ||
1453 acpi_has_method(handle, "_SDD");
1454}
1455
1456/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001457 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001458 *
1459 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1460 * then we can safely call it an ejectable drive bay
1461 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001462bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001463{
Zhang Rui54735262007-01-11 02:09:09 -05001464 acpi_handle phandle;
1465
Jiang Liu952c63e2013-06-29 00:24:38 +08001466 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001467 return false;
1468 if (acpi_ata_match(handle))
1469 return true;
1470 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1471 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001472
Jiang Liuebf4df82013-06-29 00:24:41 +08001473 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001474}
1475
Frank Seidel3620f2f2007-12-07 13:20:34 +01001476/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001477 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001478 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001479bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001480{
Jiang Liuebf4df82013-06-29 00:24:41 +08001481 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001482}
1483
Thomas Renninger620e1122010-10-01 10:54:00 +02001484const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001485{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001486 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001487
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001488 if (list_empty(&device->pnp.ids))
1489 return dummy_hid;
1490
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001491 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1492 return hid->id;
1493}
1494EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001495
Toshi Kanid4e1a692013-03-04 21:30:41 +00001496static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001497{
1498 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001499
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001500 id = kmalloc(sizeof(*id), GFP_KERNEL);
1501 if (!id)
1502 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001503
Thomas Meyer581de592011-08-06 11:32:56 +02001504 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001505 if (!id->id) {
1506 kfree(id);
1507 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001508 }
1509
Toshi Kanid4e1a692013-03-04 21:30:41 +00001510 list_add_tail(&id->list, &pnp->ids);
1511 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001512}
1513
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001514/*
1515 * Old IBM workstations have a DSDT bug wherein the SMBus object
1516 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1517 * prefix. Work around this.
1518 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001519static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001520{
Jiang Liuebf4df82013-06-29 00:24:41 +08001521 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1522 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001523
1524 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001525 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001526
1527 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001528 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1529 strcmp("SMBS", path.pointer))
1530 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001531
1532 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001533 if (acpi_has_method(handle, "SBI") &&
1534 acpi_has_method(handle, "SBR") &&
1535 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001536 return true;
1537
1538 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001539}
1540
Toshi Kanic0af4172013-03-04 21:30:42 +00001541static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1542 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Len Brown4be44fc2005-08-05 00:44:28 -04001544 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001545 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001546 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001547 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Toshi Kanic0af4172013-03-04 21:30:42 +00001549 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001551 if (handle == ACPI_ROOT_OBJECT) {
1552 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001553 break;
1554 }
1555
Toshi Kanic0af4172013-03-04 21:30:42 +00001556 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001558 pr_err(PREFIX "%s: Error reading device info\n",
1559 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return;
1561 }
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001564 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001565 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001566 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001567 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001568 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001571 pnp->bus_address = info->address;
1572 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
Lv Zhengccf78042012-10-30 14:41:07 +01001574 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001575 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001576 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001577
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001578 kfree(info);
1579
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001580 /*
1581 * Some devices don't reliably have _HIDs & _CIDs, so add
1582 * synthetic HIDs to make sure drivers can find them.
1583 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001584 if (acpi_is_video_device(handle))
1585 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001586 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001587 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001588 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001589 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001590 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001591 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1592 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1593 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1594 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1595 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001596 }
Zhang Rui54735262007-01-11 02:09:09 -05001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 break;
1599 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001600 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 break;
1602 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001603 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001606 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 break;
1608 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001609 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 break;
1611 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001612 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 break;
1614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
Toshi Kanic0af4172013-03-04 21:30:42 +00001617void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1618{
1619 struct acpi_hardware_id *id, *tmp;
1620
1621 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1622 kfree(id->id);
1623 kfree(id);
1624 }
1625 kfree(pnp->unique_id);
1626}
1627
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001628void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1629 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001631 INIT_LIST_HEAD(&device->pnp.ids);
1632 device->device_type = type;
1633 device->handle = handle;
1634 device->parent = acpi_bus_get_parent(handle);
1635 STRUCT_TO_INT(device->status) = sta;
1636 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001637 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001638 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001639 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001640 device_initialize(&device->dev);
1641 dev_set_uevent_suppress(&device->dev, true);
1642}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001643
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001644void acpi_device_add_finalize(struct acpi_device *device)
1645{
1646 dev_set_uevent_suppress(&device->dev, false);
1647 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001650static int acpi_add_single_object(struct acpi_device **child,
1651 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001652 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001654 int result;
1655 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001656 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Burman Yan36bcbec2006-12-19 12:56:11 -08001658 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001660 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001661 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001664 acpi_init_device_object(device, handle, type, sta);
1665 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001666 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001668 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001669 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001670 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001671 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001674 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001675 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001676 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1677 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1678 dev_name(&device->dev), (char *) buffer.pointer,
1679 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1680 kfree(buffer.pointer);
1681 *child = device;
1682 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001683}
1684
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001685static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1686 unsigned long long *sta)
1687{
1688 acpi_status status;
1689 acpi_object_type acpi_type;
1690
1691 status = acpi_get_type(handle, &acpi_type);
1692 if (ACPI_FAILURE(status))
1693 return -ENODEV;
1694
1695 switch (acpi_type) {
1696 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1697 case ACPI_TYPE_DEVICE:
1698 *type = ACPI_BUS_TYPE_DEVICE;
1699 status = acpi_bus_get_status_handle(handle, sta);
1700 if (ACPI_FAILURE(status))
1701 return -ENODEV;
1702 break;
1703 case ACPI_TYPE_PROCESSOR:
1704 *type = ACPI_BUS_TYPE_PROCESSOR;
1705 status = acpi_bus_get_status_handle(handle, sta);
1706 if (ACPI_FAILURE(status))
1707 return -ENODEV;
1708 break;
1709 case ACPI_TYPE_THERMAL:
1710 *type = ACPI_BUS_TYPE_THERMAL;
1711 *sta = ACPI_STA_DEFAULT;
1712 break;
1713 case ACPI_TYPE_POWER:
1714 *type = ACPI_BUS_TYPE_POWER;
1715 *sta = ACPI_STA_DEFAULT;
1716 break;
1717 default:
1718 return -ENODEV;
1719 }
1720
1721 return 0;
1722}
1723
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001724static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1725 char *idstr,
1726 const struct acpi_device_id **matchid)
1727{
1728 const struct acpi_device_id *devid;
1729
1730 for (devid = handler->ids; devid->id[0]; devid++)
1731 if (!strcmp((char *)devid->id, idstr)) {
1732 if (matchid)
1733 *matchid = devid;
1734
1735 return true;
1736 }
1737
1738 return false;
1739}
1740
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001741static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1742 const struct acpi_device_id **matchid)
1743{
1744 struct acpi_scan_handler *handler;
1745
1746 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1747 if (acpi_scan_handler_matching(handler, idstr, matchid))
1748 return handler;
1749
1750 return NULL;
1751}
1752
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001753void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1754{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001755 if (!!hotplug->enabled == !!val)
1756 return;
1757
1758 mutex_lock(&acpi_scan_lock);
1759
1760 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001761
1762 mutex_unlock(&acpi_scan_lock);
1763}
1764
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001765static void acpi_scan_init_hotplug(acpi_handle handle, int type)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001766{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001767 struct acpi_device_pnp pnp = {};
1768 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001769 struct acpi_scan_handler *handler;
1770
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001771 INIT_LIST_HEAD(&pnp.ids);
1772 acpi_set_pnp_ids(handle, &pnp, type);
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001773
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001774 if (!pnp.type.hardware_id)
Catalin Marinas7a26b532013-05-15 16:49:35 +00001775 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001776
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001777 /*
1778 * This relies on the fact that acpi_install_notify_handler() will not
1779 * install the same notify handler routine twice for the same handle.
1780 */
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001781 list_for_each_entry(hwid, &pnp.ids, list) {
1782 handler = acpi_scan_match_handler(hwid->id, NULL);
1783 if (handler) {
Toshi Kani2cbb14f2013-03-05 22:33:31 +00001784 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1785 acpi_hotplug_notify_cb, handler);
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001786 break;
1787 }
1788 }
1789
Catalin Marinas7a26b532013-05-15 16:49:35 +00001790out:
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001791 acpi_free_pnp_ids(&pnp);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001792}
1793
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001794static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1795 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001797 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001798 int type;
1799 unsigned long long sta;
1800 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001801
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001802 acpi_bus_get_device(handle, &device);
1803 if (device)
1804 goto out;
1805
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001806 result = acpi_bus_type_and_status(handle, &type, &sta);
1807 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001808 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001810 if (type == ACPI_BUS_TYPE_POWER) {
1811 acpi_add_power_resource(handle);
1812 return AE_OK;
1813 }
1814
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001815 acpi_scan_init_hotplug(handle, type);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001816
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001817 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001818 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001819 struct acpi_device_wakeup wakeup;
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001820
Jiang Liu952c63e2013-06-29 00:24:38 +08001821 if (acpi_has_method(handle, "_PRW")) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001822 acpi_bus_extract_wakeup_device_power_package(handle,
1823 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001824 acpi_power_resources_list_free(&wakeup.resources);
1825 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001826 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001829 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001830 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001831 return AE_CTRL_DEPTH;
1832
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001833 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001834 if (!*return_value)
1835 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001836
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001837 return AE_OK;
1838}
1839
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001840static int acpi_scan_attach_handler(struct acpi_device *device)
1841{
1842 struct acpi_hardware_id *hwid;
1843 int ret = 0;
1844
1845 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001846 const struct acpi_device_id *devid;
1847 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001848
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001849 handler = acpi_scan_match_handler(hwid->id, &devid);
1850 if (handler) {
1851 ret = handler->attach(device, devid);
1852 if (ret > 0) {
1853 device->handler = handler;
1854 break;
1855 } else if (ret < 0) {
1856 break;
1857 }
1858 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001859 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001860 return ret;
1861}
1862
Rafael J. Wysocki0fc300b02012-12-21 00:36:41 +01001863static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1864 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001865{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001866 struct acpi_device *device;
1867 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001868 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001869
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001870 /*
1871 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1872 * namespace walks prematurely.
1873 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001874 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001875 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001876
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001877 if (acpi_bus_get_device(handle, &device))
1878 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001879
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02001880 if (device->handler)
1881 return AE_OK;
1882
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001883 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001884 if (ret < 0)
1885 return AE_CTRL_DEPTH;
1886
1887 device->flags.match_driver = true;
1888 if (ret > 0)
1889 return AE_OK;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001890
1891 ret = device_attach(&device->dev);
1892 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001895/**
1896 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1897 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001898 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001899 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1900 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001901 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001902 * If no devices were found, -ENODEV is returned, but it does not mean that
1903 * there has been a real error. There just have been no suitable ACPI objects
1904 * in the table trunk from which the kernel could create a device and add an
1905 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001906 *
1907 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001908 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001909int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001910{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001912 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001913
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001914 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001916 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001917
Thomas Renningerd2f66502010-01-29 17:48:51 +01001918 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001919 error = -ENODEV;
1920 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001921 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b02012-12-21 00:36:41 +01001922 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01001923
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001924 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001925}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001926EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001928static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1929 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001931 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001933 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001934 struct acpi_scan_handler *dev_handler = device->handler;
1935
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001936 if (dev_handler) {
1937 if (dev_handler->detach)
1938 dev_handler->detach(device);
1939
1940 device->handler = NULL;
1941 } else {
1942 device_release_driver(&device->dev);
1943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001945 return AE_OK;
1946}
1947
1948static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1949 void *not_used, void **ret_not_used)
1950{
1951 struct acpi_device *device = NULL;
1952
1953 if (!acpi_bus_get_device(handle, &device))
1954 acpi_device_unregister(device);
1955
1956 return AE_OK;
1957}
1958
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001959/**
1960 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1961 * @start: Root of the ACPI device nodes subtree to remove.
1962 *
1963 * Must be called under acpi_scan_lock.
1964 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01001965void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001967 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001968 * Execute acpi_bus_device_detach() as a post-order callback to detach
1969 * all ACPI drivers from the device nodes being removed.
1970 */
1971 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1972 acpi_bus_device_detach, NULL, NULL);
1973 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1974 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001975 * Execute acpi_bus_remove() as a post-order callback to remove device
1976 * nodes in the given namespace scope.
1977 */
1978 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1979 acpi_bus_remove, NULL, NULL);
1980 acpi_bus_remove(start->handle, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
Kristen Accardiceaba662006-02-23 17:56:01 -08001982EXPORT_SYMBOL_GPL(acpi_bus_trim);
1983
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001984static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Len Brown4be44fc2005-08-05 00:44:28 -04001986 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /*
1989 * Enumerate all fixed-feature devices.
1990 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001991 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1992 struct acpi_device *device = NULL;
1993
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001994 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001995 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001996 ACPI_STA_DEFAULT);
1997 if (result)
1998 return result;
1999
2000 result = device_attach(&device->dev);
2001 if (result < 0)
2002 return result;
2003
Daniel Drakec10d7a12012-05-10 00:08:43 +01002004 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002007 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2008 struct acpi_device *device = NULL;
2009
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002010 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002011 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002012 ACPI_STA_DEFAULT);
2013 if (result)
2014 return result;
2015
2016 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002019 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
2021
Bjorn Helgaase747f272009-03-24 16:49:43 -06002022int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
2024 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Patrick Mochel5b327262006-05-10 10:33:00 -04002026 result = bus_register(&acpi_bus_type);
2027 if (result) {
2028 /* We don't want to quit even if we failed to add suspend/resume */
2029 printk(KERN_ERR PREFIX "Could not register bus type\n");
2030 }
2031
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002032 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002033 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002034 acpi_processor_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002035 acpi_platform_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002036 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002037 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002038 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002039 acpi_memory_hotplug_init();
Jiang Liu94add0f2013-06-23 00:59:55 +02002040 acpi_dock_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002041
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002042 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 * Enumerate devices in the ACPI namespace.
2045 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002046 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002048 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002050 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002052 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002054 result = acpi_bus_scan_fixed();
2055 if (result) {
2056 acpi_device_unregister(acpi_root);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002057 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002058 }
2059
2060 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002061
Yinghai Lu668192b2013-01-21 13:20:48 -08002062 acpi_pci_root_hp_init();
2063
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002064 out:
2065 mutex_unlock(&acpi_scan_lock);
2066 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}