blob: cf773c9181024963184ddd70ab778541e66a4315 [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
Rafael J. Wysockid7831562013-11-22 21:52:12 +010017#include <asm/pgtable.h>
18
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060019#include "internal.h"
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050022ACPI_MODULE_NAME("scan");
Len Brown4be44fc2005-08-05 00:44:28 -040023extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020026#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define ACPI_BUS_DEVICE_NAME "System Bus"
28
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000029#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
30
Rafael J. Wysockid7831562013-11-22 21:52:12 +010031#define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
32
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020033/*
34 * If set, devices will be hot-removed even if they cannot be put offline
35 * gracefully (from the kernel's standpoint).
36 */
37bool acpi_force_hot_remove;
38
Thomas Renninger620e1122010-10-01 10:54:00 +020039static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020040
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080041static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010042static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010043static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080044DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045LIST_HEAD(acpi_wakeup_device_list);
46
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080047struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080048 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080049 unsigned int instance_no;
50 struct list_head node;
51};
Thomas Renninger29b71a12007-07-23 14:43:51 +020052
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010053void acpi_scan_lock_acquire(void)
54{
55 mutex_lock(&acpi_scan_lock);
56}
57EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
58
59void acpi_scan_lock_release(void)
60{
61 mutex_unlock(&acpi_scan_lock);
62}
63EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
64
Rafael J. Wysockica589f92013-01-30 14:27:29 +010065int acpi_scan_add_handler(struct acpi_scan_handler *handler)
66{
67 if (!handler || !handler->attach)
68 return -EINVAL;
69
70 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
71 return 0;
72}
73
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010074int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
75 const char *hotplug_profile_name)
76{
77 int error;
78
79 error = acpi_scan_add_handler(handler);
80 if (error)
81 return error;
82
83 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
84 return 0;
85}
86
Thomas Renninger29b71a12007-07-23 14:43:51 +020087/*
88 * Creates hid/cid(s) string needed for modalias and uevent
89 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
90 * char *modalias: "acpi:IBM0001:ACPI0001"
91*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020092static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
93 int size)
94{
Thomas Renninger29b71a12007-07-23 14:43:51 +020095 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080096 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060097 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020098
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020099 if (list_empty(&acpi_dev->pnp.ids))
100 return 0;
101
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800102 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +0200103 size -= len;
104
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600105 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
106 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800107 if (count < 0 || count >= size)
108 return -EINVAL;
109 len += count;
110 size -= count;
111 }
112
Thomas Renninger29b71a12007-07-23 14:43:51 +0200113 modalias[len] = '\0';
114 return len;
115}
116
117static ssize_t
118acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
119 struct acpi_device *acpi_dev = to_acpi_device(dev);
120 int len;
121
122 /* Device has no HID and no CID or string is >1024 */
123 len = create_modalias(acpi_dev, buf, 1024);
124 if (len <= 0)
125 return 0;
126 buf[len++] = '\n';
127 return len;
128}
129static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
130
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100131static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
132 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200133{
134 struct acpi_device *device = NULL;
135 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200136 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200137 acpi_status status = AE_OK;
138
139 if (acpi_bus_get_device(handle, &device))
140 return AE_OK;
141
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100142 if (device->handler && !device->handler->hotplug.enabled) {
143 *ret_p = &device->dev;
144 return AE_SUPPORT;
145 }
146
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200147 mutex_lock(&device->physical_node_lock);
148
149 list_for_each_entry(pn, &device->physical_node_list, node) {
150 int ret;
151
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200152 if (second_pass) {
153 /* Skip devices offlined by the first pass. */
154 if (pn->put_online)
155 continue;
156 } else {
157 pn->put_online = false;
158 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200159 ret = device_offline(pn->dev);
160 if (acpi_force_hot_remove)
161 continue;
162
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200163 if (ret >= 0) {
164 pn->put_online = !ret;
165 } else {
166 *ret_p = pn->dev;
167 if (second_pass) {
168 status = AE_ERROR;
169 break;
170 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200171 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200172 }
173
174 mutex_unlock(&device->physical_node_lock);
175
176 return status;
177}
178
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100179static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
180 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200181{
182 struct acpi_device *device = NULL;
183 struct acpi_device_physical_node *pn;
184
185 if (acpi_bus_get_device(handle, &device))
186 return AE_OK;
187
188 mutex_lock(&device->physical_node_lock);
189
190 list_for_each_entry(pn, &device->physical_node_list, node)
191 if (pn->put_online) {
192 device_online(pn->dev);
193 pn->put_online = false;
194 }
195
196 mutex_unlock(&device->physical_node_lock);
197
198 return AE_OK;
199}
200
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100201static int acpi_scan_hot_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Yinghai Lu5993c462013-01-11 22:40:41 +0000203 acpi_handle handle = device->handle;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200204 struct device *errdev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100205 acpi_status status;
Toshi Kani882fd122013-03-13 19:29:26 +0000206 unsigned long long sta;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100207
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200208 /*
209 * Carry out two passes here and ignore errors in the first pass,
210 * because if the devices in question are memory blocks and
211 * CONFIG_MEMCG is set, one of the blocks may hold data structures
212 * that the other blocks depend on, but it is not known in advance which
213 * block holds them.
214 *
215 * If the first pass is successful, the second one isn't needed, though.
216 */
217 errdev = NULL;
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100218 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
219 NULL, acpi_bus_offline, (void *)false,
220 (void **)&errdev);
221 if (status == AE_SUPPORT) {
222 dev_warn(errdev, "Offline disabled.\n");
223 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
224 acpi_bus_online, NULL, NULL, NULL);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100225 return -EPERM;
226 }
227 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200228 if (errdev) {
229 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200230 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100231 NULL, acpi_bus_offline, (void *)true,
232 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200233 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100234 acpi_bus_offline(handle, 0, (void *)true,
235 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200236
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200237 if (errdev && !acpi_force_hot_remove) {
238 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100239 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200240 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100241 ACPI_UINT32_MAX, acpi_bus_online,
242 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200243 return -EBUSY;
244 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200245 }
246
Zhang Rui26d46862008-04-29 02:35:48 -0400247 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100248 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400249
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100250 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200251
Jiang Liu7d2421f2013-06-29 00:24:40 +0800252 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /*
254 * TBD: _EJD support.
255 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800256 status = acpi_evaluate_ej0(handle);
257 if (status == AE_NOT_FOUND)
258 return -ENODEV;
259 else if (ACPI_FAILURE(status))
260 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100261
Toshi Kani882fd122013-03-13 19:29:26 +0000262 /*
263 * Verify if eject was indeed successful. If not, log an error
264 * message. No need to call _OST since _EJ0 call was made OK.
265 */
266 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
267 if (ACPI_FAILURE(status)) {
268 acpi_handle_warn(handle,
269 "Status check after eject failed (0x%x)\n", status);
270 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
271 acpi_handle_warn(handle,
272 "Eject incomplete - status 0x%llx\n", sta);
273 }
274
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100275 return 0;
276}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100277
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100278static int acpi_scan_device_not_present(struct acpi_device *adev)
279{
280 if (!acpi_device_enumerated(adev)) {
281 dev_warn(&adev->dev, "Still not present\n");
282 return -EALREADY;
283 }
284 acpi_bus_trim(adev);
285 return 0;
286}
287
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100288static int acpi_scan_device_check(struct acpi_device *adev)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100289{
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200290 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100291
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100292 acpi_bus_get_status(adev);
293 if (adev->status.present || adev->status.functional) {
294 /*
295 * This function is only called for device objects for which
296 * matching scan handlers exist. The only situation in which
297 * the scan handler is not attached to this device object yet
298 * is when the device has just appeared (either it wasn't
299 * present at all before or it was removed and then added
300 * again).
301 */
302 if (adev->handler) {
303 dev_warn(&adev->dev, "Already enumerated\n");
304 return -EALREADY;
305 }
306 error = acpi_bus_scan(adev->handle);
307 if (error) {
308 dev_warn(&adev->dev, "Namespace scan failure\n");
309 return error;
310 }
311 if (!adev->handler) {
312 dev_warn(&adev->dev, "Enumeration failure\n");
313 error = -ENODEV;
314 }
315 } else {
316 error = acpi_scan_device_not_present(adev);
317 }
318 return error;
319}
320
321static int acpi_scan_bus_check(struct acpi_device *adev)
322{
323 struct acpi_scan_handler *handler = adev->handler;
324 struct acpi_device *child;
325 int error;
326
327 acpi_bus_get_status(adev);
328 if (!(adev->status.present || adev->status.functional)) {
329 acpi_scan_device_not_present(adev);
330 return 0;
331 }
332 if (handler && handler->hotplug.scan_dependent)
333 return handler->hotplug.scan_dependent(adev);
334
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100335 error = acpi_bus_scan(adev->handle);
336 if (error) {
337 dev_warn(&adev->dev, "Namespace scan failure\n");
338 return error;
339 }
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100340 list_for_each_entry(child, &adev->children, node) {
341 error = acpi_scan_bus_check(child);
342 if (error)
343 return error;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100344 }
345 return 0;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100346}
347
Rafael J. Wysocki3338db02013-11-22 21:55:20 +0100348static void acpi_device_hotplug(void *data, u32 src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100349{
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100350 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100351 struct acpi_device *adev = data;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100352 int error;
353
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200354 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200355 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100356
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100357 /*
358 * The device object's ACPI handle cannot become invalid as long as we
359 * are holding acpi_scan_lock, but it may have become invalid before
360 * that lock was acquired.
361 */
362 if (adev->handle == INVALID_ACPI_HANDLE)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100363 goto out;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100364
365 switch (src) {
366 case ACPI_NOTIFY_BUS_CHECK:
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100367 error = acpi_scan_bus_check(adev);
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100368 break;
369 case ACPI_NOTIFY_DEVICE_CHECK:
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100370 error = acpi_scan_device_check(adev);
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100371 break;
372 case ACPI_NOTIFY_EJECT_REQUEST:
373 case ACPI_OST_EC_OSPM_EJECT:
374 error = acpi_scan_hot_remove(adev);
375 break;
376 default:
377 error = -EINVAL;
378 break;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100379 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100380 if (!error)
381 ost_code = ACPI_OST_SC_SUCCESS;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100382
383 out:
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100384 acpi_evaluate_hotplug_ost(adev->handle, src, ost_code, NULL);
385 put_device(&adev->dev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100386 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200387 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100388}
389
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000390static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100391{
Rafael J. Wysocki1ceaba02013-11-22 21:54:52 +0100392 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000393 struct acpi_scan_handler *handler = data;
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100394 struct acpi_device *adev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100395 acpi_status status;
396
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100397 if (acpi_bus_get_device(handle, &adev))
398 goto err_out;
399
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100400 switch (type) {
401 case ACPI_NOTIFY_BUS_CHECK:
402 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100403 break;
404 case ACPI_NOTIFY_DEVICE_CHECK:
405 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100406 break;
407 case ACPI_NOTIFY_EJECT_REQUEST:
408 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
Rafael J. Wysocki1ceaba02013-11-22 21:54:52 +0100409 if (!handler->hotplug.enabled) {
410 acpi_handle_err(handle, "Eject disabled\n");
411 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
412 goto err_out;
413 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100414 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
415 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
416 break;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100417 default:
418 /* non-hotplug event; possibly handled by other handler */
419 return;
420 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100421 get_device(&adev->dev);
422 status = acpi_hotplug_execute(acpi_device_hotplug, adev, type);
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100423 if (ACPI_SUCCESS(status))
424 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100425
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100426 put_device(&adev->dev);
427
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100428 err_out:
Rafael J. Wysocki1ceaba02013-11-22 21:54:52 +0100429 acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100430}
431
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100432static ssize_t real_power_state_show(struct device *dev,
433 struct device_attribute *attr, char *buf)
434{
435 struct acpi_device *adev = to_acpi_device(dev);
436 int state;
437 int ret;
438
439 ret = acpi_device_get_power(adev, &state);
440 if (ret)
441 return ret;
442
443 return sprintf(buf, "%s\n", acpi_power_state_string(state));
444}
445
446static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
447
448static ssize_t power_state_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
450{
451 struct acpi_device *adev = to_acpi_device(dev);
452
453 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
454}
455
456static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800459acpi_eject_store(struct device *d, struct device_attribute *attr,
460 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800462 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100463 acpi_object_type not_used;
464 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100466 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100469 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
470 && !acpi_device->driver)
471 return -ENODEV;
472
473 status = acpi_get_type(acpi_device->handle, &not_used);
474 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
475 return -ENODEV;
476
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200477 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100478 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100479 get_device(&acpi_device->dev);
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100480 status = acpi_hotplug_execute(acpi_device_hotplug, acpi_device,
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100481 ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200482 if (ACPI_SUCCESS(status))
483 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100484
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200485 put_device(&acpi_device->dev);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200486 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100487 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100488 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800491static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800493static ssize_t
494acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
495 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600497 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800498}
499static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
500
Lv Zheng923d4a42012-10-30 14:43:26 +0100501static ssize_t acpi_device_uid_show(struct device *dev,
502 struct device_attribute *attr, char *buf)
503{
504 struct acpi_device *acpi_dev = to_acpi_device(dev);
505
506 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
507}
508static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
509
510static ssize_t acpi_device_adr_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
512{
513 struct acpi_device *acpi_dev = to_acpi_device(dev);
514
515 return sprintf(buf, "0x%08x\n",
516 (unsigned int)(acpi_dev->pnp.bus_address));
517}
518static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
519
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800520static ssize_t
521acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
522 struct acpi_device *acpi_dev = to_acpi_device(dev);
523 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
524 int result;
525
526 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600527 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800528 goto end;
529
530 result = sprintf(buf, "%s\n", (char*)path.pointer);
531 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600532end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800533 return result;
534}
535static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
536
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600537/* sysfs file that shows description text from the ACPI _STR method */
538static ssize_t description_show(struct device *dev,
539 struct device_attribute *attr,
540 char *buf) {
541 struct acpi_device *acpi_dev = to_acpi_device(dev);
542 int result;
543
544 if (acpi_dev->pnp.str_obj == NULL)
545 return 0;
546
547 /*
548 * The _STR object contains a Unicode identifier for a device.
549 * We need to convert to utf-8 so it can be displayed.
550 */
551 result = utf16s_to_utf8s(
552 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
553 acpi_dev->pnp.str_obj->buffer.length,
554 UTF16_LITTLE_ENDIAN, buf,
555 PAGE_SIZE);
556
557 buf[result++] = '\n';
558
559 return result;
560}
561static DEVICE_ATTR(description, 0444, description_show, NULL);
562
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100563static ssize_t
564acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
565 char *buf) {
566 struct acpi_device *acpi_dev = to_acpi_device(dev);
567
568 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
569}
570static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
571
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800572static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600574 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800575 acpi_status status;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100576 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800577 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800579 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800580 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800581 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600582 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800583 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600584 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800585 goto end;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200588 if (!list_empty(&dev->pnp.ids)) {
589 result = device_create_file(&dev->dev, &dev_attr_hid);
590 if (result)
591 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200593 result = device_create_file(&dev->dev, &dev_attr_modalias);
594 if (result)
595 goto end;
596 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200597
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600598 /*
599 * If device has _STR, 'description' file is created
600 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800601 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600602 status = acpi_evaluate_object(dev->handle, "_STR",
603 NULL, &buffer);
604 if (ACPI_FAILURE(status))
605 buffer.pointer = NULL;
606 dev->pnp.str_obj = buffer.pointer;
607 result = device_create_file(&dev->dev, &dev_attr_description);
608 if (result)
609 goto end;
610 }
611
Toshi Kanid4e1a692013-03-04 21:30:41 +0000612 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100613 result = device_create_file(&dev->dev, &dev_attr_adr);
614 if (dev->pnp.unique_id)
615 result = device_create_file(&dev->dev, &dev_attr_uid);
616
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100617 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
618 if (ACPI_SUCCESS(status)) {
619 dev->pnp.sun = (unsigned long)sun;
620 result = device_create_file(&dev->dev, &dev_attr_sun);
621 if (result)
622 goto end;
623 } else {
624 dev->pnp.sun = (unsigned long)-1;
625 }
626
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800627 /*
628 * If device has _EJ0, 'eject' file is created that is used to trigger
629 * hot-removal function from userland.
630 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800631 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800632 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100633 if (result)
634 return result;
635 }
636
637 if (dev->flags.power_manageable) {
638 result = device_create_file(&dev->dev, &dev_attr_power_state);
639 if (result)
640 return result;
641
642 if (dev->power.flags.power_resources)
643 result = device_create_file(&dev->dev,
644 &dev_attr_real_power_state);
645 }
646
Alex Chiang0c526d92009-05-14 08:31:37 -0600647end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800648 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800649}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800651static void acpi_device_remove_files(struct acpi_device *dev)
652{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100653 if (dev->flags.power_manageable) {
654 device_remove_file(&dev->dev, &dev_attr_power_state);
655 if (dev->power.flags.power_resources)
656 device_remove_file(&dev->dev,
657 &dev_attr_real_power_state);
658 }
659
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800660 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600661 * If device has _STR, remove 'description' file
662 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800663 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600664 kfree(dev->pnp.str_obj);
665 device_remove_file(&dev->dev, &dev_attr_description);
666 }
667 /*
668 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800669 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800670 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800671 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800672
Jiang Liu952c63e2013-06-29 00:24:38 +0800673 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100674 device_remove_file(&dev->dev, &dev_attr_sun);
675
Lv Zheng923d4a42012-10-30 14:43:26 +0100676 if (dev->pnp.unique_id)
677 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000678 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100679 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600680 device_remove_file(&dev->dev, &dev_attr_modalias);
681 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600682 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800683 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800684}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800686 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200688
Mika Westerbergcf761af2012-10-31 22:44:41 +0100689static const struct acpi_device_id *__acpi_match_device(
690 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200691{
692 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600693 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200694
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800695 /*
696 * If the device is not present, it is unnecessary to load device
697 * driver for it.
698 */
699 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100700 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800701
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600702 for (id = ids; id->id[0]; id++)
703 list_for_each_entry(hwid, &device->pnp.ids, list)
704 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100705 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200706
Mika Westerbergcf761af2012-10-31 22:44:41 +0100707 return NULL;
708}
709
710/**
711 * acpi_match_device - Match a struct device against a given list of ACPI IDs
712 * @ids: Array of struct acpi_device_id object to match against.
713 * @dev: The device structure to match.
714 *
715 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
716 * object for that handle and use that object to match against a given list of
717 * device IDs.
718 *
719 * Return a pointer to the first matching ID on success or %NULL on failure.
720 */
721const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
722 const struct device *dev)
723{
724 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100725 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100726
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100727 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100728 return NULL;
729
730 return __acpi_match_device(adev, ids);
731}
732EXPORT_SYMBOL_GPL(acpi_match_device);
733
734int acpi_match_device_ids(struct acpi_device *device,
735 const struct acpi_device_id *ids)
736{
737 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200738}
739EXPORT_SYMBOL(acpi_match_device_ids);
740
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100741static void acpi_free_power_resources_lists(struct acpi_device *device)
742{
743 int i;
744
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100745 if (device->wakeup.flags.valid)
746 acpi_power_resources_list_free(&device->wakeup.resources);
747
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100748 if (!device->flags.power_manageable)
749 return;
750
751 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
752 struct acpi_device_power_state *ps = &device->power.states[i];
753 acpi_power_resources_list_free(&ps->resources);
754 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600755}
756
Patrick Mochel1890a972006-12-07 20:56:31 +0800757static void acpi_device_release(struct device *dev)
758{
759 struct acpi_device *acpi_dev = to_acpi_device(dev);
760
Toshi Kanic0af4172013-03-04 21:30:42 +0000761 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100762 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800763 kfree(acpi_dev);
764}
765
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800766static int acpi_bus_match(struct device *dev, struct device_driver *drv)
767{
768 struct acpi_device *acpi_dev = to_acpi_device(dev);
769 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
770
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100771 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100772 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800773}
774
Kay Sievers7eff2e72007-08-14 15:15:12 +0200775static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800776{
777 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200778 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800779
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200780 if (list_empty(&acpi_dev->pnp.ids))
781 return 0;
782
Kay Sievers7eff2e72007-08-14 15:15:12 +0200783 if (add_uevent_var(env, "MODALIAS="))
784 return -ENOMEM;
785 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
786 sizeof(env->buf) - env->buflen);
787 if (len >= (sizeof(env->buf) - env->buflen))
788 return -ENOMEM;
789 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return 0;
791}
792
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000793static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
794{
795 struct acpi_device *device = data;
796
797 device->driver->ops.notify(device, event);
798}
799
800static acpi_status acpi_device_notify_fixed(void *data)
801{
802 struct acpi_device *device = data;
803
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000804 /* Fixed hardware devices have no handles */
805 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000806 return AE_OK;
807}
808
809static int acpi_device_install_notify_handler(struct acpi_device *device)
810{
811 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000812
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000813 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000814 status =
815 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
816 acpi_device_notify_fixed,
817 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000818 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000819 status =
820 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
821 acpi_device_notify_fixed,
822 device);
823 else
824 status = acpi_install_notify_handler(device->handle,
825 ACPI_DEVICE_NOTIFY,
826 acpi_device_notify,
827 device);
828
829 if (ACPI_FAILURE(status))
830 return -EINVAL;
831 return 0;
832}
833
834static void acpi_device_remove_notify_handler(struct acpi_device *device)
835{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000836 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000837 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
838 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000839 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000840 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
841 acpi_device_notify_fixed);
842 else
843 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
844 acpi_device_notify);
845}
846
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200847static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800848{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800849 struct acpi_device *acpi_dev = to_acpi_device(dev);
850 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
851 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200853 if (acpi_dev->handler)
854 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000855
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200856 if (!acpi_drv->ops.add)
857 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800858
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200859 ret = acpi_drv->ops.add(acpi_dev);
860 if (ret)
861 return ret;
862
863 acpi_dev->driver = acpi_drv;
864 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
865 "Driver [%s] successfully bound to device [%s]\n",
866 acpi_drv->name, acpi_dev->pnp.bus_id));
867
868 if (acpi_drv->ops.notify) {
869 ret = acpi_device_install_notify_handler(acpi_dev);
870 if (ret) {
871 if (acpi_drv->ops.remove)
872 acpi_drv->ops.remove(acpi_dev);
873
874 acpi_dev->driver = NULL;
875 acpi_dev->driver_data = NULL;
876 return ret;
877 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800878 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200879
880 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
881 acpi_drv->name, acpi_dev->pnp.bus_id));
882 get_device(dev);
883 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800884}
885
886static int acpi_device_remove(struct device * dev)
887{
888 struct acpi_device *acpi_dev = to_acpi_device(dev);
889 struct acpi_driver *acpi_drv = acpi_dev->driver;
890
891 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000892 if (acpi_drv->ops.notify)
893 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800894 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100895 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800896 }
897 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700898 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800899
900 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800901 return 0;
902}
903
David Brownell55955aa2007-05-08 00:28:35 -0700904struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800905 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800906 .match = acpi_bus_match,
907 .probe = acpi_device_probe,
908 .remove = acpi_device_remove,
909 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910};
911
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100912static void acpi_device_del(struct acpi_device *device)
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200913{
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100914 mutex_lock(&acpi_device_lock);
915 if (device->parent)
916 list_del(&device->node);
917
918 list_del(&device->wakeup_list);
919 mutex_unlock(&acpi_device_lock);
920
921 acpi_power_add_remove_device(device, false);
922 acpi_device_remove_files(device);
923 if (device->remove)
924 device->remove(device);
925
926 device_del(&device->dev);
927}
928
929static LIST_HEAD(acpi_device_del_list);
930static DEFINE_MUTEX(acpi_device_del_lock);
931
932static void acpi_device_del_work_fn(struct work_struct *work_not_used)
933{
934 for (;;) {
935 struct acpi_device *adev;
936
937 mutex_lock(&acpi_device_del_lock);
938
939 if (list_empty(&acpi_device_del_list)) {
940 mutex_unlock(&acpi_device_del_lock);
941 break;
942 }
943 adev = list_first_entry(&acpi_device_del_list,
944 struct acpi_device, del_list);
945 list_del(&adev->del_list);
946
947 mutex_unlock(&acpi_device_del_lock);
948
949 acpi_device_del(adev);
950 /*
951 * Drop references to all power resources that might have been
952 * used by the device.
953 */
954 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
955 put_device(&adev->dev);
956 }
957}
958
959/**
960 * acpi_scan_drop_device - Drop an ACPI device object.
961 * @handle: Handle of an ACPI namespace node, not used.
962 * @context: Address of the ACPI device object to drop.
963 *
964 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
965 * namespace node the device object pointed to by @context is attached to.
966 *
967 * The unregistration is carried out asynchronously to avoid running
968 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
969 * ensure the correct ordering (the device objects must be unregistered in the
970 * same order in which the corresponding namespace nodes are deleted).
971 */
972static void acpi_scan_drop_device(acpi_handle handle, void *context)
973{
974 static DECLARE_WORK(work, acpi_device_del_work_fn);
975 struct acpi_device *adev = context;
976
977 mutex_lock(&acpi_device_del_lock);
978
979 /*
980 * Use the ACPI hotplug workqueue which is ordered, so this work item
981 * won't run after any hotplug work items submitted subsequently. That
982 * prevents attempts to register device objects identical to those being
983 * deleted from happening concurrently (such attempts result from
984 * hotplug events handled via the ACPI hotplug workqueue). It also will
985 * run after all of the work items submitted previosuly, which helps
986 * those work items to ensure that they are not accessing stale device
987 * objects.
988 */
989 if (list_empty(&acpi_device_del_list))
990 acpi_queue_hotplug_work(&work);
991
992 list_add_tail(&adev->del_list, &acpi_device_del_list);
993 /* Make acpi_ns_validate_handle() return NULL for this handle. */
994 adev->handle = INVALID_ACPI_HANDLE;
995
996 mutex_unlock(&acpi_device_del_lock);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200997}
998
999int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1000{
1001 acpi_status status;
1002
1003 if (!device)
1004 return -EINVAL;
1005
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001006 status = acpi_get_data(handle, acpi_scan_drop_device, (void **)device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001007 if (ACPI_FAILURE(status) || !*device) {
1008 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1009 handle));
1010 return -ENODEV;
1011 }
1012 return 0;
1013}
Rafael J. Wysocki65859252013-10-01 23:02:43 +02001014EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001015
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001016int acpi_device_add(struct acpi_device *device,
1017 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001019 int result;
1020 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1021 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001022
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001023 if (device->handle) {
1024 acpi_status status;
1025
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001026 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001027 device);
1028 if (ACPI_FAILURE(status)) {
1029 acpi_handle_err(device->handle,
1030 "Unable to attach device data\n");
1031 return -ENODEV;
1032 }
1033 }
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /*
1036 * Linkage
1037 * -------
1038 * Link this device to its parent and siblings.
1039 */
1040 INIT_LIST_HEAD(&device->children);
1041 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001043 INIT_LIST_HEAD(&device->physical_node_list);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001044 INIT_LIST_HEAD(&device->del_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001045 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001047 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1048 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001049 pr_err(PREFIX "Memory allocation error\n");
1050 result = -ENOMEM;
1051 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001052 }
1053
Shaohua Li90905892009-04-07 10:24:29 +08001054 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001055 /*
1056 * Find suitable bus_id and instance number in acpi_bus_id_list
1057 * If failed, create one and link it into acpi_bus_id_list
1058 */
1059 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001060 if (!strcmp(acpi_device_bus_id->bus_id,
1061 acpi_device_hid(device))) {
1062 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001063 found = 1;
1064 kfree(new_bus_id);
1065 break;
1066 }
1067 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001068 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001069 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001070 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001071 acpi_device_bus_id->instance_no = 0;
1072 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1073 }
Kay Sievers07944692008-10-30 01:18:59 +01001074 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 +08001075
Len Brown33b57152008-12-15 22:09:26 -05001076 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (device->wakeup.flags.valid)
1080 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001081 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Patrick Mochel1890a972006-12-07 20:56:31 +08001083 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001084 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001085 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001086 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001087 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001088 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001089 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001090 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001091 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001092
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001093 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001094 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001095 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1096 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001097
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001098 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001099
1100 err:
Shaohua Li90905892009-04-07 10:24:29 +08001101 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001102 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001103 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001104 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001105 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001106
1107 err_detach:
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001108 acpi_detach_data(device->handle, acpi_scan_drop_device);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001109 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Zhang Rui9e89dde2006-12-07 20:56:16 +08001112/* --------------------------------------------------------------------------
1113 Driver Management
1114 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001116 * acpi_bus_register_driver - register a driver with the ACPI bus
1117 * @driver: driver being registered
1118 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001120 * devices that match the driver's criteria and binds. Returns zero for
1121 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 */
Len Brown4be44fc2005-08-05 00:44:28 -04001123int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Patrick Mochel1890a972006-12-07 20:56:31 +08001125 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001128 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001129 driver->drv.name = driver->name;
1130 driver->drv.bus = &acpi_bus_type;
1131 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Patrick Mochel1890a972006-12-07 20:56:31 +08001133 ret = driver_register(&driver->drv);
1134 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Len Brown4be44fc2005-08-05 00:44:28 -04001137EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001140 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001141 * @driver: driver to unregister
1142 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1144 * devices that match the driver's criteria and unbinds.
1145 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001146void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Patrick Mochel1890a972006-12-07 20:56:31 +08001148 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
Len Brown4be44fc2005-08-05 00:44:28 -04001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151EXPORT_SYMBOL(acpi_bus_unregister_driver);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/* --------------------------------------------------------------------------
1154 Device Enumeration
1155 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001156static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1157{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001158 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001159 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001160
1161 /*
1162 * Fixed hardware devices do not appear in the namespace and do not
1163 * have handles, but we fabricate acpi_devices for them, so we have
1164 * to deal with them specially.
1165 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001166 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001167 return acpi_root;
1168
1169 do {
1170 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001171 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001172 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1173 } while (acpi_bus_get_device(handle, &device));
1174 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001175}
1176
Len Brownc8f7a622006-07-09 17:22:28 -04001177acpi_status
1178acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1179{
1180 acpi_status status;
1181 acpi_handle tmp;
1182 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1183 union acpi_object *obj;
1184
1185 status = acpi_get_handle(handle, "_EJD", &tmp);
1186 if (ACPI_FAILURE(status))
1187 return status;
1188
1189 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1190 if (ACPI_SUCCESS(status)) {
1191 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001192 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1193 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001194 kfree(buffer.pointer);
1195 }
1196 return status;
1197}
1198EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1199
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001200static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1201 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001203 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1204 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001206 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001207 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001209 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001210 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001212 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001214 /* _PRW */
1215 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1216 if (ACPI_FAILURE(status)) {
1217 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001218 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001219 }
1220
1221 package = (union acpi_object *)buffer.pointer;
1222
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001223 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001224 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001227 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001228 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (element->type == ACPI_TYPE_PACKAGE) {
1231 if ((element->package.count < 2) ||
1232 (element->package.elements[0].type !=
1233 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001234 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001235 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001236
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001237 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001239 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 (u32) element->package.elements[1].integer.value;
1241 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001242 wakeup->gpe_device = NULL;
1243 wakeup->gpe_number = element->integer.value;
1244 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001245 goto out;
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001249 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001250 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001251
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001252 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001254 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1255 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001256 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001258 if (!list_empty(&wakeup->resources)) {
1259 int sleep_state;
1260
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001261 err = acpi_power_wakeup_list_init(&wakeup->resources,
1262 &sleep_state);
1263 if (err) {
1264 acpi_handle_warn(handle, "Retrieving current states "
1265 "of wakeup power resources failed\n");
1266 acpi_power_resources_list_free(&wakeup->resources);
1267 goto out;
1268 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001269 if (sleep_state < wakeup->sleep_state) {
1270 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1271 "(S%d) by S%d from power resources\n",
1272 (int)wakeup->sleep_state, sleep_state);
1273 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Lin Mingbba63a22010-12-13 13:39:17 +08001276 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001277
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001278 out:
1279 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001283static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001285 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001286 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001287 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001288 {"PNP0C0E", 0},
1289 {"", 0},
1290 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001291 acpi_status status;
1292 acpi_event_status event_status;
1293
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001294 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001295
1296 /* Power button, Lid switch always enable wakeup */
1297 if (!acpi_match_device_ids(device, button_device_ids)) {
1298 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001299 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1300 /* Do not use Lid/sleep button for S5 wakeup */
1301 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1302 device->wakeup.sleep_state = ACPI_STATE_S4;
1303 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001304 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001305 return;
1306 }
1307
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001308 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1309 device->wakeup.gpe_number,
1310 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001311 if (status == AE_OK)
1312 device->wakeup.flags.run_wake =
1313 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1314}
1315
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001316static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001317{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001318 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001319
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001320 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001321 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001322 return;
1323
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001324 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1325 &device->wakeup);
1326 if (err) {
1327 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001328 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001332 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001333 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001334 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1335 * system for the ACPI device with the _PRW object.
1336 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1337 * So it is necessary to call _DSW object first. Only when it is not
1338 * present will the _PSW object used.
1339 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001340 err = acpi_device_sleep_wake(device, 0, 0, 0);
1341 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001342 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1343 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001346static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001348 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001349 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1350 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001351 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001352
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001353 INIT_LIST_HEAD(&ps->resources);
1354
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001355 /* Evaluate "_PRx" to get referenced power resources */
1356 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1357 if (ACPI_SUCCESS(status)) {
1358 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001359
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001360 if (buffer.length && package
1361 && package->type == ACPI_TYPE_PACKAGE
1362 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001363 int err = acpi_extract_power_resources(package, 0,
1364 &ps->resources);
1365 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001366 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001367 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001368 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001369 }
1370
1371 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001372 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001373 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001374 ps->flags.explicit_set = 1;
1375
1376 /*
1377 * State is valid if there are means to put the device into it.
1378 * D3hot is only valid if _PR3 present.
1379 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001380 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001381 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1382 ps->flags.valid = 1;
1383 ps->flags.os_accessible = 1;
1384 }
1385
1386 ps->power = -1; /* Unknown - driver assigned */
1387 ps->latency = -1; /* Unknown - driver assigned */
1388}
1389
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001390static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001392 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001394 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001395 if (!acpi_has_method(device->handle, "_PS0") &&
1396 !acpi_has_method(device->handle, "_PR0"))
1397 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001398
1399 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001402 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001404 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001405 device->power.flags.explicit_get = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001406 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001407 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001410 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001412 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1413 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001415 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Zhang Rui9e89dde2006-12-07 20:56:16 +08001417 /* Set defaults for D0 and D3 states (always valid) */
1418 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1419 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001420 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1421 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001423 /* Set D3cold's explicit_set flag if _PS3 exists. */
1424 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1425 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1426
Aaron Lu1399dfc2012-11-21 23:33:40 +01001427 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1428 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1429 device->power.flags.power_resources)
1430 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1431
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001432 if (acpi_bus_init_power(device)) {
1433 acpi_free_power_resources_lists(device);
1434 device->flags.power_manageable = 0;
1435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001438static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001441 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 device->flags.dynamic_status = 1;
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001445 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 device->flags.removable = 1;
1447
1448 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001449 if (acpi_has_method(device->handle, "_EJD") ||
1450 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001454static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455{
Len Brown4be44fc2005-08-05 00:44:28 -04001456 char bus_id[5] = { '?', 0 };
1457 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1458 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 /*
1461 * Bus ID
1462 * ------
1463 * The device's Bus ID is simply the object name.
1464 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1465 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001466 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001468 return;
1469 }
1470
1471 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 case ACPI_BUS_TYPE_POWER_BUTTON:
1473 strcpy(device->pnp.bus_id, "PWRF");
1474 break;
1475 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1476 strcpy(device->pnp.bus_id, "SLPF");
1477 break;
1478 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001479 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /* Clean up trailing underscores (if any) */
1481 for (i = 3; i > 1; i--) {
1482 if (bus_id[i] == '_')
1483 bus_id[i] = '\0';
1484 else
1485 break;
1486 }
1487 strcpy(device->pnp.bus_id, bus_id);
1488 break;
1489 }
1490}
1491
Zhang Rui54735262007-01-11 02:09:09 -05001492/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001493 * acpi_ata_match - see if an acpi object is an ATA device
1494 *
1495 * If an acpi object has one of the ACPI ATA methods defined,
1496 * then we can safely call it an ATA device.
1497 */
1498bool acpi_ata_match(acpi_handle handle)
1499{
1500 return acpi_has_method(handle, "_GTF") ||
1501 acpi_has_method(handle, "_GTM") ||
1502 acpi_has_method(handle, "_STM") ||
1503 acpi_has_method(handle, "_SDD");
1504}
1505
1506/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001507 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001508 *
1509 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1510 * then we can safely call it an ejectable drive bay
1511 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001512bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001513{
Zhang Rui54735262007-01-11 02:09:09 -05001514 acpi_handle phandle;
1515
Jiang Liu952c63e2013-06-29 00:24:38 +08001516 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001517 return false;
1518 if (acpi_ata_match(handle))
1519 return true;
1520 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1521 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001522
Jiang Liuebf4df82013-06-29 00:24:41 +08001523 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001524}
1525
Frank Seidel3620f2f2007-12-07 13:20:34 +01001526/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001527 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001528 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001529bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001530{
Jiang Liuebf4df82013-06-29 00:24:41 +08001531 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001532}
1533
Thomas Renninger620e1122010-10-01 10:54:00 +02001534const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001535{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001536 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001537
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001538 if (list_empty(&device->pnp.ids))
1539 return dummy_hid;
1540
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001541 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1542 return hid->id;
1543}
1544EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001545
Toshi Kanid4e1a692013-03-04 21:30:41 +00001546static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001547{
1548 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001549
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001550 id = kmalloc(sizeof(*id), GFP_KERNEL);
1551 if (!id)
1552 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001553
Thomas Meyer581de592011-08-06 11:32:56 +02001554 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001555 if (!id->id) {
1556 kfree(id);
1557 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001558 }
1559
Toshi Kanid4e1a692013-03-04 21:30:41 +00001560 list_add_tail(&id->list, &pnp->ids);
1561 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001562}
1563
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001564/*
1565 * Old IBM workstations have a DSDT bug wherein the SMBus object
1566 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1567 * prefix. Work around this.
1568 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001569static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001570{
Jiang Liuebf4df82013-06-29 00:24:41 +08001571 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1572 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001573
1574 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001575 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001576
1577 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001578 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1579 strcmp("SMBS", path.pointer))
1580 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001581
1582 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001583 if (acpi_has_method(handle, "SBI") &&
1584 acpi_has_method(handle, "SBR") &&
1585 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001586 return true;
1587
1588 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001589}
1590
Toshi Kanic0af4172013-03-04 21:30:42 +00001591static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1592 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Len Brown4be44fc2005-08-05 00:44:28 -04001594 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001595 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001596 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001597 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Toshi Kanic0af4172013-03-04 21:30:42 +00001599 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001601 if (handle == ACPI_ROOT_OBJECT) {
1602 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001603 break;
1604 }
1605
Toshi Kanic0af4172013-03-04 21:30:42 +00001606 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001608 pr_err(PREFIX "%s: Error reading device info\n",
1609 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return;
1611 }
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001614 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001615 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001616 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001617 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001618 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001621 pnp->bus_address = info->address;
1622 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
Lv Zhengccf78042012-10-30 14:41:07 +01001624 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001625 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001626 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001627
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001628 kfree(info);
1629
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001630 /*
1631 * Some devices don't reliably have _HIDs & _CIDs, so add
1632 * synthetic HIDs to make sure drivers can find them.
1633 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001634 if (acpi_is_video_device(handle))
1635 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001636 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001637 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001638 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001639 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001640 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001641 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1642 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1643 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1644 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1645 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001646 }
Zhang Rui54735262007-01-11 02:09:09 -05001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 break;
1649 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001650 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 break;
1652 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001653 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001656 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 break;
1658 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001659 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001662 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 break;
1664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Toshi Kanic0af4172013-03-04 21:30:42 +00001667void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1668{
1669 struct acpi_hardware_id *id, *tmp;
1670
1671 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1672 kfree(id->id);
1673 kfree(id);
1674 }
1675 kfree(pnp->unique_id);
1676}
1677
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001678void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1679 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001681 INIT_LIST_HEAD(&device->pnp.ids);
1682 device->device_type = type;
1683 device->handle = handle;
1684 device->parent = acpi_bus_get_parent(handle);
Rafael J. Wysocki25db1152013-11-22 21:56:06 +01001685 acpi_set_device_status(device, sta);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001686 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001687 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001688 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001689 device->flags.match_driver = false;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001690 device->flags.initialized = true;
1691 device->flags.visited = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001692 device_initialize(&device->dev);
1693 dev_set_uevent_suppress(&device->dev, true);
1694}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001695
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001696void acpi_device_add_finalize(struct acpi_device *device)
1697{
1698 dev_set_uevent_suppress(&device->dev, false);
1699 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001702static int acpi_add_single_object(struct acpi_device **child,
1703 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001704 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001706 int result;
1707 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001708 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Burman Yan36bcbec2006-12-19 12:56:11 -08001710 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001712 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001713 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001716 acpi_init_device_object(device, handle, type, sta);
1717 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001718 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001720 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001721 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001722 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001723 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001726 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001727 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001728 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1729 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1730 dev_name(&device->dev), (char *) buffer.pointer,
1731 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1732 kfree(buffer.pointer);
1733 *child = device;
1734 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001735}
1736
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001737static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1738 unsigned long long *sta)
1739{
1740 acpi_status status;
1741 acpi_object_type acpi_type;
1742
1743 status = acpi_get_type(handle, &acpi_type);
1744 if (ACPI_FAILURE(status))
1745 return -ENODEV;
1746
1747 switch (acpi_type) {
1748 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1749 case ACPI_TYPE_DEVICE:
1750 *type = ACPI_BUS_TYPE_DEVICE;
1751 status = acpi_bus_get_status_handle(handle, sta);
1752 if (ACPI_FAILURE(status))
1753 return -ENODEV;
1754 break;
1755 case ACPI_TYPE_PROCESSOR:
1756 *type = ACPI_BUS_TYPE_PROCESSOR;
1757 status = acpi_bus_get_status_handle(handle, sta);
1758 if (ACPI_FAILURE(status))
1759 return -ENODEV;
1760 break;
1761 case ACPI_TYPE_THERMAL:
1762 *type = ACPI_BUS_TYPE_THERMAL;
1763 *sta = ACPI_STA_DEFAULT;
1764 break;
1765 case ACPI_TYPE_POWER:
1766 *type = ACPI_BUS_TYPE_POWER;
1767 *sta = ACPI_STA_DEFAULT;
1768 break;
1769 default:
1770 return -ENODEV;
1771 }
1772
1773 return 0;
1774}
1775
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001776bool acpi_device_is_present(struct acpi_device *adev)
1777{
1778 if (adev->status.present || adev->status.functional)
1779 return true;
1780
1781 adev->flags.initialized = false;
1782 return false;
1783}
1784
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001785static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1786 char *idstr,
1787 const struct acpi_device_id **matchid)
1788{
1789 const struct acpi_device_id *devid;
1790
1791 for (devid = handler->ids; devid->id[0]; devid++)
1792 if (!strcmp((char *)devid->id, idstr)) {
1793 if (matchid)
1794 *matchid = devid;
1795
1796 return true;
1797 }
1798
1799 return false;
1800}
1801
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001802static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1803 const struct acpi_device_id **matchid)
1804{
1805 struct acpi_scan_handler *handler;
1806
1807 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1808 if (acpi_scan_handler_matching(handler, idstr, matchid))
1809 return handler;
1810
1811 return NULL;
1812}
1813
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001814void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1815{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001816 if (!!hotplug->enabled == !!val)
1817 return;
1818
1819 mutex_lock(&acpi_scan_lock);
1820
1821 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001822
1823 mutex_unlock(&acpi_scan_lock);
1824}
1825
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001826static void acpi_scan_init_hotplug(acpi_handle handle, int type)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001827{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001828 struct acpi_device_pnp pnp = {};
1829 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001830 struct acpi_scan_handler *handler;
1831
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001832 INIT_LIST_HEAD(&pnp.ids);
1833 acpi_set_pnp_ids(handle, &pnp, type);
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001834
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001835 if (!pnp.type.hardware_id)
Catalin Marinas7a26b532013-05-15 16:49:35 +00001836 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001837
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001838 /*
1839 * This relies on the fact that acpi_install_notify_handler() will not
1840 * install the same notify handler routine twice for the same handle.
1841 */
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001842 list_for_each_entry(hwid, &pnp.ids, list) {
1843 handler = acpi_scan_match_handler(hwid->id, NULL);
Rafael J. Wysocki3338db02013-11-22 21:55:20 +01001844 if (handler) {
Toshi Kani2cbb14f2013-03-05 22:33:31 +00001845 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1846 acpi_hotplug_notify_cb, handler);
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001847 break;
1848 }
1849 }
1850
Catalin Marinas7a26b532013-05-15 16:49:35 +00001851out:
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001852 acpi_free_pnp_ids(&pnp);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001853}
1854
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001855static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1856 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001858 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001859 int type;
1860 unsigned long long sta;
1861 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001862
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001863 acpi_bus_get_device(handle, &device);
1864 if (device)
1865 goto out;
1866
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001867 result = acpi_bus_type_and_status(handle, &type, &sta);
1868 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001869 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001871 if (type == ACPI_BUS_TYPE_POWER) {
1872 acpi_add_power_resource(handle);
1873 return AE_OK;
1874 }
1875
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001876 acpi_scan_init_hotplug(handle, type);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001877
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001878 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001879 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001880 return AE_CTRL_DEPTH;
1881
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001882 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001883 if (!*return_value)
1884 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001885
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001886 return AE_OK;
1887}
1888
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001889static int acpi_scan_attach_handler(struct acpi_device *device)
1890{
1891 struct acpi_hardware_id *hwid;
1892 int ret = 0;
1893
1894 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001895 const struct acpi_device_id *devid;
1896 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001897
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001898 handler = acpi_scan_match_handler(hwid->id, &devid);
1899 if (handler) {
1900 ret = handler->attach(device, devid);
1901 if (ret > 0) {
1902 device->handler = handler;
1903 break;
1904 } else if (ret < 0) {
1905 break;
1906 }
1907 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001908 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001909 return ret;
1910}
1911
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001912static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1913 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001914{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001915 struct acpi_device *device;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001916 unsigned long long sta;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001917 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001918
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001919 /*
1920 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1921 * namespace walks prematurely.
1922 */
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001923 if (acpi_bus_type_and_status(handle, &ret, &sta))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001924 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001925
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001926 if (acpi_bus_get_device(handle, &device))
1927 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001928
Rafael J. Wysocki25db1152013-11-22 21:56:06 +01001929 acpi_set_device_status(device, sta);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001930 /* Skip devices that are not present. */
1931 if (!acpi_device_is_present(device))
1932 goto err;
1933
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02001934 if (device->handler)
1935 return AE_OK;
1936
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001937 if (!device->flags.initialized) {
1938 acpi_bus_update_power(device, NULL);
1939 device->flags.initialized = true;
1940 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001941 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001942 if (ret < 0)
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001943 goto err;
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001944
1945 device->flags.match_driver = true;
1946 if (ret > 0)
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001947 goto ok;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001948
1949 ret = device_attach(&device->dev);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001950 if (ret < 0)
1951 goto err;
1952
1953 ok:
1954 device->flags.visited = true;
1955 return AE_OK;
1956
1957 err:
1958 device->flags.visited = false;
1959 return AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001962/**
1963 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1964 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001965 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001966 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1967 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001968 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001969 * If no devices were found, -ENODEV is returned, but it does not mean that
1970 * there has been a real error. There just have been no suitable ACPI objects
1971 * in the table trunk from which the kernel could create a device and add an
1972 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001973 *
1974 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001975 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001976int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001977{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001979 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001980
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001981 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001983 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001984
Thomas Renningerd2f66502010-01-29 17:48:51 +01001985 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001986 error = -ENODEV;
1987 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001988 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001989 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01001990
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001991 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001992}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001993EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001995static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1996 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001998 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002000 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002001 struct acpi_scan_handler *dev_handler = device->handler;
2002
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002003 if (dev_handler) {
2004 if (dev_handler->detach)
2005 dev_handler->detach(device);
2006
2007 device->handler = NULL;
2008 } else {
2009 device_release_driver(&device->dev);
2010 }
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002011 /*
2012 * Most likely, the device is going away, so put it into D3cold
2013 * before that.
2014 */
2015 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
2016 device->flags.initialized = false;
2017 device->flags.visited = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002019 return AE_OK;
2020}
2021
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002022/**
2023 * acpi_bus_trim - Remove ACPI device node and all of its descendants
2024 * @start: Root of the ACPI device nodes subtree to remove.
2025 *
2026 * Must be called under acpi_scan_lock.
2027 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01002028void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002030 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002031 * Execute acpi_bus_device_detach() as a post-order callback to detach
2032 * all ACPI drivers from the device nodes being removed.
2033 */
2034 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2035 acpi_bus_device_detach, NULL, NULL);
2036 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
Kristen Accardiceaba662006-02-23 17:56:01 -08002038EXPORT_SYMBOL_GPL(acpi_bus_trim);
2039
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002040static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Len Brown4be44fc2005-08-05 00:44:28 -04002042 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /*
2045 * Enumerate all fixed-feature devices.
2046 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002047 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2048 struct acpi_device *device = NULL;
2049
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002050 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002051 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002052 ACPI_STA_DEFAULT);
2053 if (result)
2054 return result;
2055
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002056 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002057 result = device_attach(&device->dev);
2058 if (result < 0)
2059 return result;
2060
Daniel Drakec10d7a12012-05-10 00:08:43 +01002061 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002064 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2065 struct acpi_device *device = NULL;
2066
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002067 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002068 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002069 ACPI_STA_DEFAULT);
2070 if (result)
2071 return result;
2072
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002073 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002074 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002077 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Bjorn Helgaase747f272009-03-24 16:49:43 -06002080int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Patrick Mochel5b327262006-05-10 10:33:00 -04002084 result = bus_register(&acpi_bus_type);
2085 if (result) {
2086 /* We don't want to quit even if we failed to add suspend/resume */
2087 printk(KERN_ERR PREFIX "Could not register bus type\n");
2088 }
2089
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002090 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002091 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002092 acpi_processor_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002093 acpi_platform_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002094 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002095 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002096 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002097 acpi_memory_hotplug_init();
Jiang Liu94add0f2013-06-23 00:59:55 +02002098 acpi_dock_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002099
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002100 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 * Enumerate devices in the ACPI namespace.
2103 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002104 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002108 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002110 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002112 result = acpi_bus_scan_fixed();
2113 if (result) {
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002114 acpi_detach_data(acpi_root->handle, acpi_scan_drop_device);
2115 acpi_device_del(acpi_root);
2116 put_device(&acpi_root->dev);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002117 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002118 }
2119
2120 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002121
2122 out:
2123 mutex_unlock(&acpi_scan_lock);
2124 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}