blob: 65243b9dd868af87bf3f03361f60840d65e80481 [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
Rafael J. Wysockid7831562013-11-22 21:52:12 +010015#include <asm/pgtable.h>
16
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Len Brown4be44fc2005-08-05 00:44:28 -040021extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020024#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define ACPI_BUS_DEVICE_NAME "System Bus"
26
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000027#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
28
Rafael J. Wysockid7831562013-11-22 21:52:12 +010029#define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
30
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020031/*
32 * If set, devices will be hot-removed even if they cannot be put offline
33 * gracefully (from the kernel's standpoint).
34 */
35bool acpi_force_hot_remove;
36
Thomas Renninger620e1122010-10-01 10:54:00 +020037static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020038
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080039static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010040static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010041static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080042DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043LIST_HEAD(acpi_wakeup_device_list);
44
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080045struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080046 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080047 unsigned int instance_no;
48 struct list_head node;
49};
Thomas Renninger29b71a12007-07-23 14:43:51 +020050
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010051void acpi_scan_lock_acquire(void)
52{
53 mutex_lock(&acpi_scan_lock);
54}
55EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
56
57void acpi_scan_lock_release(void)
58{
59 mutex_unlock(&acpi_scan_lock);
60}
61EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
62
Rafael J. Wysockica589f92013-01-30 14:27:29 +010063int acpi_scan_add_handler(struct acpi_scan_handler *handler)
64{
65 if (!handler || !handler->attach)
66 return -EINVAL;
67
68 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
69 return 0;
70}
71
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010072int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
73 const char *hotplug_profile_name)
74{
75 int error;
76
77 error = acpi_scan_add_handler(handler);
78 if (error)
79 return error;
80
81 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
82 return 0;
83}
84
Thomas Renninger29b71a12007-07-23 14:43:51 +020085/*
86 * Creates hid/cid(s) string needed for modalias and uevent
87 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
88 * char *modalias: "acpi:IBM0001:ACPI0001"
89*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020090static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
91 int size)
92{
Thomas Renninger29b71a12007-07-23 14:43:51 +020093 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080094 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060095 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020096
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020097 if (list_empty(&acpi_dev->pnp.ids))
98 return 0;
99
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800100 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +0200101 size -= len;
102
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600103 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
104 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800105 if (count < 0 || count >= size)
106 return -EINVAL;
107 len += count;
108 size -= count;
109 }
110
Thomas Renninger29b71a12007-07-23 14:43:51 +0200111 modalias[len] = '\0';
112 return len;
113}
114
115static ssize_t
116acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
117 struct acpi_device *acpi_dev = to_acpi_device(dev);
118 int len;
119
120 /* Device has no HID and no CID or string is >1024 */
121 len = create_modalias(acpi_dev, buf, 1024);
122 if (len <= 0)
123 return 0;
124 buf[len++] = '\n';
125 return len;
126}
127static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
128
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100129static bool acpi_scan_is_offline(struct acpi_device *adev)
130{
131 struct acpi_device_physical_node *pn;
132 bool offline = true;
133
134 mutex_lock(&adev->physical_node_lock);
135
136 list_for_each_entry(pn, &adev->physical_node_list, node)
137 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
138 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
139 offline = false;
140 break;
141 }
142
143 mutex_unlock(&adev->physical_node_lock);
144 return offline;
145}
146
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100147static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
148 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200149{
150 struct acpi_device *device = NULL;
151 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200152 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200153 acpi_status status = AE_OK;
154
155 if (acpi_bus_get_device(handle, &device))
156 return AE_OK;
157
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100158 if (device->handler && !device->handler->hotplug.enabled) {
159 *ret_p = &device->dev;
160 return AE_SUPPORT;
161 }
162
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200163 mutex_lock(&device->physical_node_lock);
164
165 list_for_each_entry(pn, &device->physical_node_list, node) {
166 int ret;
167
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200168 if (second_pass) {
169 /* Skip devices offlined by the first pass. */
170 if (pn->put_online)
171 continue;
172 } else {
173 pn->put_online = false;
174 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200175 ret = device_offline(pn->dev);
176 if (acpi_force_hot_remove)
177 continue;
178
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200179 if (ret >= 0) {
180 pn->put_online = !ret;
181 } else {
182 *ret_p = pn->dev;
183 if (second_pass) {
184 status = AE_ERROR;
185 break;
186 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200187 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200188 }
189
190 mutex_unlock(&device->physical_node_lock);
191
192 return status;
193}
194
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100195static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
196 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200197{
198 struct acpi_device *device = NULL;
199 struct acpi_device_physical_node *pn;
200
201 if (acpi_bus_get_device(handle, &device))
202 return AE_OK;
203
204 mutex_lock(&device->physical_node_lock);
205
206 list_for_each_entry(pn, &device->physical_node_list, node)
207 if (pn->put_online) {
208 device_online(pn->dev);
209 pn->put_online = false;
210 }
211
212 mutex_unlock(&device->physical_node_lock);
213
214 return AE_OK;
215}
216
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100217static int acpi_scan_try_to_offline(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Yinghai Lu5993c462013-01-11 22:40:41 +0000219 acpi_handle handle = device->handle;
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100220 struct device *errdev = NULL;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100221 acpi_status status;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100222
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200223 /*
224 * Carry out two passes here and ignore errors in the first pass,
225 * because if the devices in question are memory blocks and
226 * CONFIG_MEMCG is set, one of the blocks may hold data structures
227 * that the other blocks depend on, but it is not known in advance which
228 * block holds them.
229 *
230 * If the first pass is successful, the second one isn't needed, though.
231 */
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100232 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
233 NULL, acpi_bus_offline, (void *)false,
234 (void **)&errdev);
235 if (status == AE_SUPPORT) {
236 dev_warn(errdev, "Offline disabled.\n");
237 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
238 acpi_bus_online, NULL, NULL, NULL);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100239 return -EPERM;
240 }
241 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200242 if (errdev) {
243 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200244 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100245 NULL, acpi_bus_offline, (void *)true,
246 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200247 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100248 acpi_bus_offline(handle, 0, (void *)true,
249 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200250
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200251 if (errdev && !acpi_force_hot_remove) {
252 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100253 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200254 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100255 ACPI_UINT32_MAX, acpi_bus_online,
256 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200257 return -EBUSY;
258 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200259 }
Rafael J. Wysockid22ddcb2013-12-29 15:25:35 +0100260 return 0;
261}
262
263static int acpi_scan_hot_remove(struct acpi_device *device)
264{
265 acpi_handle handle = device->handle;
266 unsigned long long sta;
267 acpi_status status;
268
269 if (device->handler->hotplug.demand_offline && !acpi_force_hot_remove) {
270 if (!acpi_scan_is_offline(device))
271 return -EBUSY;
272 } else {
273 int error = acpi_scan_try_to_offline(device);
274 if (error)
275 return error;
276 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200277
Zhang Rui26d46862008-04-29 02:35:48 -0400278 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100279 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400280
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100281 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200282
Jiang Liu7d2421f2013-06-29 00:24:40 +0800283 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /*
285 * TBD: _EJD support.
286 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800287 status = acpi_evaluate_ej0(handle);
288 if (status == AE_NOT_FOUND)
289 return -ENODEV;
290 else if (ACPI_FAILURE(status))
291 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100292
Toshi Kani882fd122013-03-13 19:29:26 +0000293 /*
294 * Verify if eject was indeed successful. If not, log an error
295 * message. No need to call _OST since _EJ0 call was made OK.
296 */
297 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
298 if (ACPI_FAILURE(status)) {
299 acpi_handle_warn(handle,
300 "Status check after eject failed (0x%x)\n", status);
301 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
302 acpi_handle_warn(handle,
303 "Eject incomplete - status 0x%llx\n", sta);
304 }
305
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100306 return 0;
307}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100308
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100309static int acpi_scan_device_not_present(struct acpi_device *adev)
310{
311 if (!acpi_device_enumerated(adev)) {
312 dev_warn(&adev->dev, "Still not present\n");
313 return -EALREADY;
314 }
315 acpi_bus_trim(adev);
316 return 0;
317}
318
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100319static int acpi_scan_device_check(struct acpi_device *adev)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100320{
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200321 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100322
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100323 acpi_bus_get_status(adev);
324 if (adev->status.present || adev->status.functional) {
325 /*
326 * This function is only called for device objects for which
327 * matching scan handlers exist. The only situation in which
328 * the scan handler is not attached to this device object yet
329 * is when the device has just appeared (either it wasn't
330 * present at all before or it was removed and then added
331 * again).
332 */
333 if (adev->handler) {
334 dev_warn(&adev->dev, "Already enumerated\n");
335 return -EALREADY;
336 }
337 error = acpi_bus_scan(adev->handle);
338 if (error) {
339 dev_warn(&adev->dev, "Namespace scan failure\n");
340 return error;
341 }
342 if (!adev->handler) {
343 dev_warn(&adev->dev, "Enumeration failure\n");
344 error = -ENODEV;
345 }
346 } else {
347 error = acpi_scan_device_not_present(adev);
348 }
349 return error;
350}
351
352static int acpi_scan_bus_check(struct acpi_device *adev)
353{
354 struct acpi_scan_handler *handler = adev->handler;
355 struct acpi_device *child;
356 int error;
357
358 acpi_bus_get_status(adev);
359 if (!(adev->status.present || adev->status.functional)) {
360 acpi_scan_device_not_present(adev);
361 return 0;
362 }
363 if (handler && handler->hotplug.scan_dependent)
364 return handler->hotplug.scan_dependent(adev);
365
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100366 error = acpi_bus_scan(adev->handle);
367 if (error) {
368 dev_warn(&adev->dev, "Namespace scan failure\n");
369 return error;
370 }
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100371 list_for_each_entry(child, &adev->children, node) {
372 error = acpi_scan_bus_check(child);
373 if (error)
374 return error;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100375 }
376 return 0;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100377}
378
Rafael J. Wysocki3338db02013-11-22 21:55:20 +0100379static void acpi_device_hotplug(void *data, u32 src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100380{
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100381 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100382 struct acpi_device *adev = data;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100383 int error;
384
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200385 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200386 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100387
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100388 /*
389 * The device object's ACPI handle cannot become invalid as long as we
390 * are holding acpi_scan_lock, but it may have become invalid before
391 * that lock was acquired.
392 */
393 if (adev->handle == INVALID_ACPI_HANDLE)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100394 goto out;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100395
396 switch (src) {
397 case ACPI_NOTIFY_BUS_CHECK:
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100398 error = acpi_scan_bus_check(adev);
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100399 break;
400 case ACPI_NOTIFY_DEVICE_CHECK:
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100401 error = acpi_scan_device_check(adev);
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100402 break;
403 case ACPI_NOTIFY_EJECT_REQUEST:
404 case ACPI_OST_EC_OSPM_EJECT:
405 error = acpi_scan_hot_remove(adev);
406 break;
407 default:
408 error = -EINVAL;
409 break;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100410 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100411 if (!error)
412 ost_code = ACPI_OST_SC_SUCCESS;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100413
414 out:
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100415 acpi_evaluate_hotplug_ost(adev->handle, src, ost_code, NULL);
416 put_device(&adev->dev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100417 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200418 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100419}
420
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000421static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100422{
Rafael J. Wysocki1ceaba02013-11-22 21:54:52 +0100423 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000424 struct acpi_scan_handler *handler = data;
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100425 struct acpi_device *adev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100426 acpi_status status;
427
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100428 if (acpi_bus_get_device(handle, &adev))
429 goto err_out;
430
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100431 switch (type) {
432 case ACPI_NOTIFY_BUS_CHECK:
433 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100434 break;
435 case ACPI_NOTIFY_DEVICE_CHECK:
436 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100437 break;
438 case ACPI_NOTIFY_EJECT_REQUEST:
439 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
Rafael J. Wysocki1ceaba02013-11-22 21:54:52 +0100440 if (!handler->hotplug.enabled) {
441 acpi_handle_err(handle, "Eject disabled\n");
442 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
443 goto err_out;
444 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100445 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
446 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
447 break;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100448 default:
449 /* non-hotplug event; possibly handled by other handler */
450 return;
451 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100452 get_device(&adev->dev);
453 status = acpi_hotplug_execute(acpi_device_hotplug, adev, type);
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100454 if (ACPI_SUCCESS(status))
455 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100456
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100457 put_device(&adev->dev);
458
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100459 err_out:
Rafael J. Wysocki1ceaba02013-11-22 21:54:52 +0100460 acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100461}
462
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100463static ssize_t real_power_state_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
465{
466 struct acpi_device *adev = to_acpi_device(dev);
467 int state;
468 int ret;
469
470 ret = acpi_device_get_power(adev, &state);
471 if (ret)
472 return ret;
473
474 return sprintf(buf, "%s\n", acpi_power_state_string(state));
475}
476
477static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
478
479static ssize_t power_state_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
481{
482 struct acpi_device *adev = to_acpi_device(dev);
483
484 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
485}
486
487static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800490acpi_eject_store(struct device *d, struct device_attribute *attr,
491 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800493 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100494 acpi_object_type not_used;
495 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100497 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100500 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
501 && !acpi_device->driver)
502 return -ENODEV;
503
504 status = acpi_get_type(acpi_device->handle, &not_used);
505 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
506 return -ENODEV;
507
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200508 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100509 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100510 get_device(&acpi_device->dev);
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100511 status = acpi_hotplug_execute(acpi_device_hotplug, acpi_device,
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100512 ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200513 if (ACPI_SUCCESS(status))
514 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100515
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200516 put_device(&acpi_device->dev);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200517 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100518 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100519 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800522static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800524static ssize_t
525acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
526 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600528 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800529}
530static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
531
Lv Zheng923d4a42012-10-30 14:43:26 +0100532static ssize_t acpi_device_uid_show(struct device *dev,
533 struct device_attribute *attr, char *buf)
534{
535 struct acpi_device *acpi_dev = to_acpi_device(dev);
536
537 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
538}
539static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
540
541static ssize_t acpi_device_adr_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
543{
544 struct acpi_device *acpi_dev = to_acpi_device(dev);
545
546 return sprintf(buf, "0x%08x\n",
547 (unsigned int)(acpi_dev->pnp.bus_address));
548}
549static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
550
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800551static ssize_t
552acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
553 struct acpi_device *acpi_dev = to_acpi_device(dev);
554 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
555 int result;
556
557 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600558 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800559 goto end;
560
561 result = sprintf(buf, "%s\n", (char*)path.pointer);
562 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600563end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800564 return result;
565}
566static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
567
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600568/* sysfs file that shows description text from the ACPI _STR method */
569static ssize_t description_show(struct device *dev,
570 struct device_attribute *attr,
571 char *buf) {
572 struct acpi_device *acpi_dev = to_acpi_device(dev);
573 int result;
574
575 if (acpi_dev->pnp.str_obj == NULL)
576 return 0;
577
578 /*
579 * The _STR object contains a Unicode identifier for a device.
580 * We need to convert to utf-8 so it can be displayed.
581 */
582 result = utf16s_to_utf8s(
583 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
584 acpi_dev->pnp.str_obj->buffer.length,
585 UTF16_LITTLE_ENDIAN, buf,
586 PAGE_SIZE);
587
588 buf[result++] = '\n';
589
590 return result;
591}
592static DEVICE_ATTR(description, 0444, description_show, NULL);
593
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100594static ssize_t
595acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
596 char *buf) {
597 struct acpi_device *acpi_dev = to_acpi_device(dev);
598
599 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
600}
601static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
602
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800603static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600605 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800606 acpi_status status;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100607 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800608 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800610 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800611 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800612 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600613 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800614 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600615 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800616 goto end;
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200619 if (!list_empty(&dev->pnp.ids)) {
620 result = device_create_file(&dev->dev, &dev_attr_hid);
621 if (result)
622 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200624 result = device_create_file(&dev->dev, &dev_attr_modalias);
625 if (result)
626 goto end;
627 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200628
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600629 /*
630 * If device has _STR, 'description' file is created
631 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800632 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600633 status = acpi_evaluate_object(dev->handle, "_STR",
634 NULL, &buffer);
635 if (ACPI_FAILURE(status))
636 buffer.pointer = NULL;
637 dev->pnp.str_obj = buffer.pointer;
638 result = device_create_file(&dev->dev, &dev_attr_description);
639 if (result)
640 goto end;
641 }
642
Toshi Kanid4e1a692013-03-04 21:30:41 +0000643 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100644 result = device_create_file(&dev->dev, &dev_attr_adr);
645 if (dev->pnp.unique_id)
646 result = device_create_file(&dev->dev, &dev_attr_uid);
647
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100648 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
649 if (ACPI_SUCCESS(status)) {
650 dev->pnp.sun = (unsigned long)sun;
651 result = device_create_file(&dev->dev, &dev_attr_sun);
652 if (result)
653 goto end;
654 } else {
655 dev->pnp.sun = (unsigned long)-1;
656 }
657
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800658 /*
659 * If device has _EJ0, 'eject' file is created that is used to trigger
660 * hot-removal function from userland.
661 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800662 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800663 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100664 if (result)
665 return result;
666 }
667
668 if (dev->flags.power_manageable) {
669 result = device_create_file(&dev->dev, &dev_attr_power_state);
670 if (result)
671 return result;
672
673 if (dev->power.flags.power_resources)
674 result = device_create_file(&dev->dev,
675 &dev_attr_real_power_state);
676 }
677
Alex Chiang0c526d92009-05-14 08:31:37 -0600678end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800679 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800680}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800682static void acpi_device_remove_files(struct acpi_device *dev)
683{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100684 if (dev->flags.power_manageable) {
685 device_remove_file(&dev->dev, &dev_attr_power_state);
686 if (dev->power.flags.power_resources)
687 device_remove_file(&dev->dev,
688 &dev_attr_real_power_state);
689 }
690
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800691 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600692 * If device has _STR, remove 'description' file
693 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800694 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600695 kfree(dev->pnp.str_obj);
696 device_remove_file(&dev->dev, &dev_attr_description);
697 }
698 /*
699 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800700 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800701 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800702 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800703
Jiang Liu952c63e2013-06-29 00:24:38 +0800704 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100705 device_remove_file(&dev->dev, &dev_attr_sun);
706
Lv Zheng923d4a42012-10-30 14:43:26 +0100707 if (dev->pnp.unique_id)
708 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000709 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100710 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600711 device_remove_file(&dev->dev, &dev_attr_modalias);
712 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600713 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800714 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800715}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800717 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200719
Mika Westerbergcf761af2012-10-31 22:44:41 +0100720static const struct acpi_device_id *__acpi_match_device(
721 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200722{
723 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600724 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200725
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800726 /*
727 * If the device is not present, it is unnecessary to load device
728 * driver for it.
729 */
730 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100731 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800732
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600733 for (id = ids; id->id[0]; id++)
734 list_for_each_entry(hwid, &device->pnp.ids, list)
735 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100736 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200737
Mika Westerbergcf761af2012-10-31 22:44:41 +0100738 return NULL;
739}
740
741/**
742 * acpi_match_device - Match a struct device against a given list of ACPI IDs
743 * @ids: Array of struct acpi_device_id object to match against.
744 * @dev: The device structure to match.
745 *
746 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
747 * object for that handle and use that object to match against a given list of
748 * device IDs.
749 *
750 * Return a pointer to the first matching ID on success or %NULL on failure.
751 */
752const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
753 const struct device *dev)
754{
755 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100756 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100757
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100758 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100759 return NULL;
760
761 return __acpi_match_device(adev, ids);
762}
763EXPORT_SYMBOL_GPL(acpi_match_device);
764
765int acpi_match_device_ids(struct acpi_device *device,
766 const struct acpi_device_id *ids)
767{
768 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200769}
770EXPORT_SYMBOL(acpi_match_device_ids);
771
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100772static void acpi_free_power_resources_lists(struct acpi_device *device)
773{
774 int i;
775
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100776 if (device->wakeup.flags.valid)
777 acpi_power_resources_list_free(&device->wakeup.resources);
778
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100779 if (!device->flags.power_manageable)
780 return;
781
782 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
783 struct acpi_device_power_state *ps = &device->power.states[i];
784 acpi_power_resources_list_free(&ps->resources);
785 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600786}
787
Patrick Mochel1890a972006-12-07 20:56:31 +0800788static void acpi_device_release(struct device *dev)
789{
790 struct acpi_device *acpi_dev = to_acpi_device(dev);
791
Toshi Kanic0af4172013-03-04 21:30:42 +0000792 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100793 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800794 kfree(acpi_dev);
795}
796
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800797static int acpi_bus_match(struct device *dev, struct device_driver *drv)
798{
799 struct acpi_device *acpi_dev = to_acpi_device(dev);
800 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
801
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100802 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100803 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800804}
805
Kay Sievers7eff2e72007-08-14 15:15:12 +0200806static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800807{
808 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200809 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800810
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200811 if (list_empty(&acpi_dev->pnp.ids))
812 return 0;
813
Kay Sievers7eff2e72007-08-14 15:15:12 +0200814 if (add_uevent_var(env, "MODALIAS="))
815 return -ENOMEM;
816 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
817 sizeof(env->buf) - env->buflen);
818 if (len >= (sizeof(env->buf) - env->buflen))
819 return -ENOMEM;
820 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
822}
823
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000824static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
825{
826 struct acpi_device *device = data;
827
828 device->driver->ops.notify(device, event);
829}
830
831static acpi_status acpi_device_notify_fixed(void *data)
832{
833 struct acpi_device *device = data;
834
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000835 /* Fixed hardware devices have no handles */
836 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000837 return AE_OK;
838}
839
840static int acpi_device_install_notify_handler(struct acpi_device *device)
841{
842 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000843
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000844 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000845 status =
846 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
847 acpi_device_notify_fixed,
848 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000849 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000850 status =
851 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
852 acpi_device_notify_fixed,
853 device);
854 else
855 status = acpi_install_notify_handler(device->handle,
856 ACPI_DEVICE_NOTIFY,
857 acpi_device_notify,
858 device);
859
860 if (ACPI_FAILURE(status))
861 return -EINVAL;
862 return 0;
863}
864
865static void acpi_device_remove_notify_handler(struct acpi_device *device)
866{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000867 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000868 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
869 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000870 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000871 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
872 acpi_device_notify_fixed);
873 else
874 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
875 acpi_device_notify);
876}
877
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200878static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800879{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800880 struct acpi_device *acpi_dev = to_acpi_device(dev);
881 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
882 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200884 if (acpi_dev->handler)
885 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000886
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200887 if (!acpi_drv->ops.add)
888 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800889
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200890 ret = acpi_drv->ops.add(acpi_dev);
891 if (ret)
892 return ret;
893
894 acpi_dev->driver = acpi_drv;
895 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
896 "Driver [%s] successfully bound to device [%s]\n",
897 acpi_drv->name, acpi_dev->pnp.bus_id));
898
899 if (acpi_drv->ops.notify) {
900 ret = acpi_device_install_notify_handler(acpi_dev);
901 if (ret) {
902 if (acpi_drv->ops.remove)
903 acpi_drv->ops.remove(acpi_dev);
904
905 acpi_dev->driver = NULL;
906 acpi_dev->driver_data = NULL;
907 return ret;
908 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800909 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200910
911 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
912 acpi_drv->name, acpi_dev->pnp.bus_id));
913 get_device(dev);
914 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800915}
916
917static int acpi_device_remove(struct device * dev)
918{
919 struct acpi_device *acpi_dev = to_acpi_device(dev);
920 struct acpi_driver *acpi_drv = acpi_dev->driver;
921
922 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000923 if (acpi_drv->ops.notify)
924 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800925 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100926 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800927 }
928 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700929 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800930
931 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800932 return 0;
933}
934
David Brownell55955aa2007-05-08 00:28:35 -0700935struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800936 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800937 .match = acpi_bus_match,
938 .probe = acpi_device_probe,
939 .remove = acpi_device_remove,
940 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941};
942
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100943static void acpi_device_del(struct acpi_device *device)
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200944{
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100945 mutex_lock(&acpi_device_lock);
946 if (device->parent)
947 list_del(&device->node);
948
949 list_del(&device->wakeup_list);
950 mutex_unlock(&acpi_device_lock);
951
952 acpi_power_add_remove_device(device, false);
953 acpi_device_remove_files(device);
954 if (device->remove)
955 device->remove(device);
956
957 device_del(&device->dev);
958}
959
960static LIST_HEAD(acpi_device_del_list);
961static DEFINE_MUTEX(acpi_device_del_lock);
962
963static void acpi_device_del_work_fn(struct work_struct *work_not_used)
964{
965 for (;;) {
966 struct acpi_device *adev;
967
968 mutex_lock(&acpi_device_del_lock);
969
970 if (list_empty(&acpi_device_del_list)) {
971 mutex_unlock(&acpi_device_del_lock);
972 break;
973 }
974 adev = list_first_entry(&acpi_device_del_list,
975 struct acpi_device, del_list);
976 list_del(&adev->del_list);
977
978 mutex_unlock(&acpi_device_del_lock);
979
980 acpi_device_del(adev);
981 /*
982 * Drop references to all power resources that might have been
983 * used by the device.
984 */
985 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
986 put_device(&adev->dev);
987 }
988}
989
990/**
991 * acpi_scan_drop_device - Drop an ACPI device object.
992 * @handle: Handle of an ACPI namespace node, not used.
993 * @context: Address of the ACPI device object to drop.
994 *
995 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
996 * namespace node the device object pointed to by @context is attached to.
997 *
998 * The unregistration is carried out asynchronously to avoid running
999 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1000 * ensure the correct ordering (the device objects must be unregistered in the
1001 * same order in which the corresponding namespace nodes are deleted).
1002 */
1003static void acpi_scan_drop_device(acpi_handle handle, void *context)
1004{
1005 static DECLARE_WORK(work, acpi_device_del_work_fn);
1006 struct acpi_device *adev = context;
1007
1008 mutex_lock(&acpi_device_del_lock);
1009
1010 /*
1011 * Use the ACPI hotplug workqueue which is ordered, so this work item
1012 * won't run after any hotplug work items submitted subsequently. That
1013 * prevents attempts to register device objects identical to those being
1014 * deleted from happening concurrently (such attempts result from
1015 * hotplug events handled via the ACPI hotplug workqueue). It also will
1016 * run after all of the work items submitted previosuly, which helps
1017 * those work items to ensure that they are not accessing stale device
1018 * objects.
1019 */
1020 if (list_empty(&acpi_device_del_list))
1021 acpi_queue_hotplug_work(&work);
1022
1023 list_add_tail(&adev->del_list, &acpi_device_del_list);
1024 /* Make acpi_ns_validate_handle() return NULL for this handle. */
1025 adev->handle = INVALID_ACPI_HANDLE;
1026
1027 mutex_unlock(&acpi_device_del_lock);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001028}
1029
1030int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1031{
1032 acpi_status status;
1033
1034 if (!device)
1035 return -EINVAL;
1036
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001037 status = acpi_get_data(handle, acpi_scan_drop_device, (void **)device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001038 if (ACPI_FAILURE(status) || !*device) {
1039 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1040 handle));
1041 return -ENODEV;
1042 }
1043 return 0;
1044}
Rafael J. Wysocki65859252013-10-01 23:02:43 +02001045EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001046
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001047int acpi_device_add(struct acpi_device *device,
1048 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001050 int result;
1051 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1052 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001053
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001054 if (device->handle) {
1055 acpi_status status;
1056
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001057 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001058 device);
1059 if (ACPI_FAILURE(status)) {
1060 acpi_handle_err(device->handle,
1061 "Unable to attach device data\n");
1062 return -ENODEV;
1063 }
1064 }
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /*
1067 * Linkage
1068 * -------
1069 * Link this device to its parent and siblings.
1070 */
1071 INIT_LIST_HEAD(&device->children);
1072 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001074 INIT_LIST_HEAD(&device->physical_node_list);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001075 INIT_LIST_HEAD(&device->del_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001076 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001078 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1079 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001080 pr_err(PREFIX "Memory allocation error\n");
1081 result = -ENOMEM;
1082 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001083 }
1084
Shaohua Li90905892009-04-07 10:24:29 +08001085 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001086 /*
1087 * Find suitable bus_id and instance number in acpi_bus_id_list
1088 * If failed, create one and link it into acpi_bus_id_list
1089 */
1090 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001091 if (!strcmp(acpi_device_bus_id->bus_id,
1092 acpi_device_hid(device))) {
1093 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001094 found = 1;
1095 kfree(new_bus_id);
1096 break;
1097 }
1098 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001099 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001100 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001101 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001102 acpi_device_bus_id->instance_no = 0;
1103 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1104 }
Kay Sievers07944692008-10-30 01:18:59 +01001105 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 +08001106
Len Brown33b57152008-12-15 22:09:26 -05001107 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (device->wakeup.flags.valid)
1111 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001112 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Patrick Mochel1890a972006-12-07 20:56:31 +08001114 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001115 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001116 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001117 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001118 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001119 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001120 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001121 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001122 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001123
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001124 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001125 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001126 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1127 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001128
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001129 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001130
1131 err:
Shaohua Li90905892009-04-07 10:24:29 +08001132 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001133 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001134 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001135 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001136 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001137
1138 err_detach:
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001139 acpi_detach_data(device->handle, acpi_scan_drop_device);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001140 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Zhang Rui9e89dde2006-12-07 20:56:16 +08001143/* --------------------------------------------------------------------------
1144 Driver Management
1145 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001147 * acpi_bus_register_driver - register a driver with the ACPI bus
1148 * @driver: driver being registered
1149 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001151 * devices that match the driver's criteria and binds. Returns zero for
1152 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 */
Len Brown4be44fc2005-08-05 00:44:28 -04001154int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Patrick Mochel1890a972006-12-07 20:56:31 +08001156 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001159 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001160 driver->drv.name = driver->name;
1161 driver->drv.bus = &acpi_bus_type;
1162 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Patrick Mochel1890a972006-12-07 20:56:31 +08001164 ret = driver_register(&driver->drv);
1165 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Len Brown4be44fc2005-08-05 00:44:28 -04001168EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001171 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001172 * @driver: driver to unregister
1173 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1175 * devices that match the driver's criteria and unbinds.
1176 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001177void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Patrick Mochel1890a972006-12-07 20:56:31 +08001179 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
Len Brown4be44fc2005-08-05 00:44:28 -04001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182EXPORT_SYMBOL(acpi_bus_unregister_driver);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184/* --------------------------------------------------------------------------
1185 Device Enumeration
1186 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001187static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1188{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001189 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001190 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001191
1192 /*
1193 * Fixed hardware devices do not appear in the namespace and do not
1194 * have handles, but we fabricate acpi_devices for them, so we have
1195 * to deal with them specially.
1196 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001197 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001198 return acpi_root;
1199
1200 do {
1201 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001202 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001203 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1204 } while (acpi_bus_get_device(handle, &device));
1205 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001206}
1207
Len Brownc8f7a622006-07-09 17:22:28 -04001208acpi_status
1209acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1210{
1211 acpi_status status;
1212 acpi_handle tmp;
1213 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1214 union acpi_object *obj;
1215
1216 status = acpi_get_handle(handle, "_EJD", &tmp);
1217 if (ACPI_FAILURE(status))
1218 return status;
1219
1220 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1221 if (ACPI_SUCCESS(status)) {
1222 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001223 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1224 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001225 kfree(buffer.pointer);
1226 }
1227 return status;
1228}
1229EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1230
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001231static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1232 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001234 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1235 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001237 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001238 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001240 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001241 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001243 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001245 /* _PRW */
1246 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1247 if (ACPI_FAILURE(status)) {
1248 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001249 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001250 }
1251
1252 package = (union acpi_object *)buffer.pointer;
1253
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001254 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001255 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001258 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001259 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (element->type == ACPI_TYPE_PACKAGE) {
1262 if ((element->package.count < 2) ||
1263 (element->package.elements[0].type !=
1264 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001265 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001266 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001267
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001268 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001270 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 (u32) element->package.elements[1].integer.value;
1272 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001273 wakeup->gpe_device = NULL;
1274 wakeup->gpe_number = element->integer.value;
1275 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001276 goto out;
1277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001280 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001281 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001282
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001283 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001285 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1286 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001287 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001289 if (!list_empty(&wakeup->resources)) {
1290 int sleep_state;
1291
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001292 err = acpi_power_wakeup_list_init(&wakeup->resources,
1293 &sleep_state);
1294 if (err) {
1295 acpi_handle_warn(handle, "Retrieving current states "
1296 "of wakeup power resources failed\n");
1297 acpi_power_resources_list_free(&wakeup->resources);
1298 goto out;
1299 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001300 if (sleep_state < wakeup->sleep_state) {
1301 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1302 "(S%d) by S%d from power resources\n",
1303 (int)wakeup->sleep_state, sleep_state);
1304 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
Lin Mingbba63a22010-12-13 13:39:17 +08001307 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001308
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001309 out:
1310 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001311 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001314static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001316 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001317 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001318 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001319 {"PNP0C0E", 0},
1320 {"", 0},
1321 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001322 acpi_status status;
1323 acpi_event_status event_status;
1324
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001325 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001326
1327 /* Power button, Lid switch always enable wakeup */
1328 if (!acpi_match_device_ids(device, button_device_ids)) {
1329 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001330 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1331 /* Do not use Lid/sleep button for S5 wakeup */
1332 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1333 device->wakeup.sleep_state = ACPI_STATE_S4;
1334 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001335 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001336 return;
1337 }
1338
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001339 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1340 device->wakeup.gpe_number,
1341 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001342 if (status == AE_OK)
1343 device->wakeup.flags.run_wake =
1344 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1345}
1346
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001347static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001348{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001349 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001350
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001351 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001352 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001353 return;
1354
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001355 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1356 &device->wakeup);
1357 if (err) {
1358 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001359 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001363 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001364 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001365 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1366 * system for the ACPI device with the _PRW object.
1367 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1368 * So it is necessary to call _DSW object first. Only when it is not
1369 * present will the _PSW object used.
1370 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001371 err = acpi_device_sleep_wake(device, 0, 0, 0);
1372 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001373 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1374 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001377static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001379 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001380 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1381 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001382 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001383
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001384 INIT_LIST_HEAD(&ps->resources);
1385
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001386 /* Evaluate "_PRx" to get referenced power resources */
1387 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1388 if (ACPI_SUCCESS(status)) {
1389 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001390
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001391 if (buffer.length && package
1392 && package->type == ACPI_TYPE_PACKAGE
1393 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001394 int err = acpi_extract_power_resources(package, 0,
1395 &ps->resources);
1396 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001397 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001398 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001399 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001400 }
1401
1402 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001403 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001404 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001405 ps->flags.explicit_set = 1;
1406
1407 /*
1408 * State is valid if there are means to put the device into it.
1409 * D3hot is only valid if _PR3 present.
1410 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001411 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001412 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1413 ps->flags.valid = 1;
1414 ps->flags.os_accessible = 1;
1415 }
1416
1417 ps->power = -1; /* Unknown - driver assigned */
1418 ps->latency = -1; /* Unknown - driver assigned */
1419}
1420
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001421static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001423 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001425 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001426 if (!acpi_has_method(device->handle, "_PS0") &&
1427 !acpi_has_method(device->handle, "_PR0"))
1428 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001429
1430 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001433 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001435 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001436 device->power.flags.explicit_get = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001437 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001438 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001441 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001443 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1444 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001446 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Zhang Rui9e89dde2006-12-07 20:56:16 +08001448 /* Set defaults for D0 and D3 states (always valid) */
1449 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1450 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001451 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1452 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001454 /* Set D3cold's explicit_set flag if _PS3 exists. */
1455 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1456 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1457
Aaron Lu1399dfc2012-11-21 23:33:40 +01001458 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1459 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1460 device->power.flags.power_resources)
1461 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1462
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001463 if (acpi_bus_init_power(device)) {
1464 acpi_free_power_resources_lists(device);
1465 device->flags.power_manageable = 0;
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001469static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001472 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 device->flags.dynamic_status = 1;
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001476 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 device->flags.removable = 1;
1478
1479 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001480 if (acpi_has_method(device->handle, "_EJD") ||
1481 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001485static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Len Brown4be44fc2005-08-05 00:44:28 -04001487 char bus_id[5] = { '?', 0 };
1488 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1489 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 /*
1492 * Bus ID
1493 * ------
1494 * The device's Bus ID is simply the object name.
1495 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1496 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001497 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001499 return;
1500 }
1501
1502 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 case ACPI_BUS_TYPE_POWER_BUTTON:
1504 strcpy(device->pnp.bus_id, "PWRF");
1505 break;
1506 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1507 strcpy(device->pnp.bus_id, "SLPF");
1508 break;
1509 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001510 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /* Clean up trailing underscores (if any) */
1512 for (i = 3; i > 1; i--) {
1513 if (bus_id[i] == '_')
1514 bus_id[i] = '\0';
1515 else
1516 break;
1517 }
1518 strcpy(device->pnp.bus_id, bus_id);
1519 break;
1520 }
1521}
1522
Zhang Rui54735262007-01-11 02:09:09 -05001523/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001524 * acpi_ata_match - see if an acpi object is an ATA device
1525 *
1526 * If an acpi object has one of the ACPI ATA methods defined,
1527 * then we can safely call it an ATA device.
1528 */
1529bool acpi_ata_match(acpi_handle handle)
1530{
1531 return acpi_has_method(handle, "_GTF") ||
1532 acpi_has_method(handle, "_GTM") ||
1533 acpi_has_method(handle, "_STM") ||
1534 acpi_has_method(handle, "_SDD");
1535}
1536
1537/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001538 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001539 *
1540 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1541 * then we can safely call it an ejectable drive bay
1542 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001543bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001544{
Zhang Rui54735262007-01-11 02:09:09 -05001545 acpi_handle phandle;
1546
Jiang Liu952c63e2013-06-29 00:24:38 +08001547 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001548 return false;
1549 if (acpi_ata_match(handle))
1550 return true;
1551 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1552 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001553
Jiang Liuebf4df82013-06-29 00:24:41 +08001554 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001555}
1556
Frank Seidel3620f2f2007-12-07 13:20:34 +01001557/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001558 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001559 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001560bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001561{
Jiang Liuebf4df82013-06-29 00:24:41 +08001562 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001563}
1564
Thomas Renninger620e1122010-10-01 10:54:00 +02001565const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001566{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001567 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001568
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001569 if (list_empty(&device->pnp.ids))
1570 return dummy_hid;
1571
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001572 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1573 return hid->id;
1574}
1575EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001576
Toshi Kanid4e1a692013-03-04 21:30:41 +00001577static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001578{
1579 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001580
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001581 id = kmalloc(sizeof(*id), GFP_KERNEL);
1582 if (!id)
1583 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001584
Thomas Meyer581de592011-08-06 11:32:56 +02001585 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001586 if (!id->id) {
1587 kfree(id);
1588 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001589 }
1590
Toshi Kanid4e1a692013-03-04 21:30:41 +00001591 list_add_tail(&id->list, &pnp->ids);
1592 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001593}
1594
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001595/*
1596 * Old IBM workstations have a DSDT bug wherein the SMBus object
1597 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1598 * prefix. Work around this.
1599 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001600static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001601{
Jiang Liuebf4df82013-06-29 00:24:41 +08001602 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1603 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001604
1605 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001606 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001607
1608 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001609 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1610 strcmp("SMBS", path.pointer))
1611 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001612
1613 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001614 if (acpi_has_method(handle, "SBI") &&
1615 acpi_has_method(handle, "SBR") &&
1616 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001617 return true;
1618
1619 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001620}
1621
Toshi Kanic0af4172013-03-04 21:30:42 +00001622static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1623 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Len Brown4be44fc2005-08-05 00:44:28 -04001625 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001626 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001627 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001628 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Toshi Kanic0af4172013-03-04 21:30:42 +00001630 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001632 if (handle == ACPI_ROOT_OBJECT) {
1633 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001634 break;
1635 }
1636
Toshi Kanic0af4172013-03-04 21:30:42 +00001637 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001639 pr_err(PREFIX "%s: Error reading device info\n",
1640 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return;
1642 }
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001645 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001646 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001647 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001648 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001649 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001652 pnp->bus_address = info->address;
1653 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
Lv Zhengccf78042012-10-30 14:41:07 +01001655 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001656 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001657 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001658
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001659 kfree(info);
1660
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001661 /*
1662 * Some devices don't reliably have _HIDs & _CIDs, so add
1663 * synthetic HIDs to make sure drivers can find them.
1664 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001665 if (acpi_is_video_device(handle))
1666 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001667 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001668 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001669 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001670 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001671 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001672 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1673 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1674 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1675 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1676 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001677 }
Zhang Rui54735262007-01-11 02:09:09 -05001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 break;
1680 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001681 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 break;
1683 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001684 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001687 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 break;
1689 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001690 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 break;
1692 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001693 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Toshi Kanic0af4172013-03-04 21:30:42 +00001698void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1699{
1700 struct acpi_hardware_id *id, *tmp;
1701
1702 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1703 kfree(id->id);
1704 kfree(id);
1705 }
1706 kfree(pnp->unique_id);
1707}
1708
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001709void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1710 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001712 INIT_LIST_HEAD(&device->pnp.ids);
1713 device->device_type = type;
1714 device->handle = handle;
1715 device->parent = acpi_bus_get_parent(handle);
Rafael J. Wysocki25db1152013-11-22 21:56:06 +01001716 acpi_set_device_status(device, sta);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001717 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001718 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001719 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001720 device->flags.match_driver = false;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001721 device->flags.initialized = true;
1722 device->flags.visited = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001723 device_initialize(&device->dev);
1724 dev_set_uevent_suppress(&device->dev, true);
1725}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001726
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001727void acpi_device_add_finalize(struct acpi_device *device)
1728{
1729 dev_set_uevent_suppress(&device->dev, false);
1730 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001733static int acpi_add_single_object(struct acpi_device **child,
1734 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001735 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001737 int result;
1738 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001739 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Burman Yan36bcbec2006-12-19 12:56:11 -08001741 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001743 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001744 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001747 acpi_init_device_object(device, handle, type, sta);
1748 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001749 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001751 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001752 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001753 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001754 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001757 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001758 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001759 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1760 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1761 dev_name(&device->dev), (char *) buffer.pointer,
1762 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1763 kfree(buffer.pointer);
1764 *child = device;
1765 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001766}
1767
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001768static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1769 unsigned long long *sta)
1770{
1771 acpi_status status;
1772 acpi_object_type acpi_type;
1773
1774 status = acpi_get_type(handle, &acpi_type);
1775 if (ACPI_FAILURE(status))
1776 return -ENODEV;
1777
1778 switch (acpi_type) {
1779 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1780 case ACPI_TYPE_DEVICE:
1781 *type = ACPI_BUS_TYPE_DEVICE;
1782 status = acpi_bus_get_status_handle(handle, sta);
1783 if (ACPI_FAILURE(status))
1784 return -ENODEV;
1785 break;
1786 case ACPI_TYPE_PROCESSOR:
1787 *type = ACPI_BUS_TYPE_PROCESSOR;
1788 status = acpi_bus_get_status_handle(handle, sta);
1789 if (ACPI_FAILURE(status))
1790 return -ENODEV;
1791 break;
1792 case ACPI_TYPE_THERMAL:
1793 *type = ACPI_BUS_TYPE_THERMAL;
1794 *sta = ACPI_STA_DEFAULT;
1795 break;
1796 case ACPI_TYPE_POWER:
1797 *type = ACPI_BUS_TYPE_POWER;
1798 *sta = ACPI_STA_DEFAULT;
1799 break;
1800 default:
1801 return -ENODEV;
1802 }
1803
1804 return 0;
1805}
1806
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001807bool acpi_device_is_present(struct acpi_device *adev)
1808{
1809 if (adev->status.present || adev->status.functional)
1810 return true;
1811
1812 adev->flags.initialized = false;
1813 return false;
1814}
1815
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001816static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1817 char *idstr,
1818 const struct acpi_device_id **matchid)
1819{
1820 const struct acpi_device_id *devid;
1821
1822 for (devid = handler->ids; devid->id[0]; devid++)
1823 if (!strcmp((char *)devid->id, idstr)) {
1824 if (matchid)
1825 *matchid = devid;
1826
1827 return true;
1828 }
1829
1830 return false;
1831}
1832
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001833static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1834 const struct acpi_device_id **matchid)
1835{
1836 struct acpi_scan_handler *handler;
1837
1838 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1839 if (acpi_scan_handler_matching(handler, idstr, matchid))
1840 return handler;
1841
1842 return NULL;
1843}
1844
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001845void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1846{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001847 if (!!hotplug->enabled == !!val)
1848 return;
1849
1850 mutex_lock(&acpi_scan_lock);
1851
1852 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001853
1854 mutex_unlock(&acpi_scan_lock);
1855}
1856
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001857static void acpi_scan_init_hotplug(acpi_handle handle, int type)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001858{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001859 struct acpi_device_pnp pnp = {};
1860 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001861 struct acpi_scan_handler *handler;
1862
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001863 INIT_LIST_HEAD(&pnp.ids);
1864 acpi_set_pnp_ids(handle, &pnp, type);
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001865
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001866 if (!pnp.type.hardware_id)
Catalin Marinas7a26b532013-05-15 16:49:35 +00001867 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001868
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001869 /*
1870 * This relies on the fact that acpi_install_notify_handler() will not
1871 * install the same notify handler routine twice for the same handle.
1872 */
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001873 list_for_each_entry(hwid, &pnp.ids, list) {
1874 handler = acpi_scan_match_handler(hwid->id, NULL);
Rafael J. Wysocki3338db02013-11-22 21:55:20 +01001875 if (handler) {
Toshi Kani2cbb14f2013-03-05 22:33:31 +00001876 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1877 acpi_hotplug_notify_cb, handler);
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001878 break;
1879 }
1880 }
1881
Catalin Marinas7a26b532013-05-15 16:49:35 +00001882out:
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001883 acpi_free_pnp_ids(&pnp);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001884}
1885
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001886static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1887 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001889 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001890 int type;
1891 unsigned long long sta;
1892 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001893
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001894 acpi_bus_get_device(handle, &device);
1895 if (device)
1896 goto out;
1897
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001898 result = acpi_bus_type_and_status(handle, &type, &sta);
1899 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001900 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001902 if (type == ACPI_BUS_TYPE_POWER) {
1903 acpi_add_power_resource(handle);
1904 return AE_OK;
1905 }
1906
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001907 acpi_scan_init_hotplug(handle, type);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001908
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001909 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001910 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001911 return AE_CTRL_DEPTH;
1912
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001913 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001914 if (!*return_value)
1915 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001916
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001917 return AE_OK;
1918}
1919
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001920static int acpi_scan_attach_handler(struct acpi_device *device)
1921{
1922 struct acpi_hardware_id *hwid;
1923 int ret = 0;
1924
1925 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001926 const struct acpi_device_id *devid;
1927 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001928
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001929 handler = acpi_scan_match_handler(hwid->id, &devid);
1930 if (handler) {
1931 ret = handler->attach(device, devid);
1932 if (ret > 0) {
1933 device->handler = handler;
1934 break;
1935 } else if (ret < 0) {
1936 break;
1937 }
1938 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001939 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001940 return ret;
1941}
1942
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001943static void acpi_bus_attach(struct acpi_device *device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001944{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001945 struct acpi_device *child;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001946 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001947
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001948 acpi_bus_get_status(device);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001949 /* Skip devices that are not present. */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001950 if (!acpi_device_is_present(device)) {
1951 device->flags.visited = false;
1952 return;
1953 }
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02001954 if (device->handler)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001955 goto ok;
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02001956
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001957 if (!device->flags.initialized) {
1958 acpi_bus_update_power(device, NULL);
1959 device->flags.initialized = true;
1960 }
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001961 device->flags.visited = false;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001962 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001963 if (ret < 0)
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001964 return;
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001965
1966 device->flags.match_driver = true;
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001967 if (!ret) {
1968 ret = device_attach(&device->dev);
1969 if (ret < 0)
1970 return;
1971 }
1972 device->flags.visited = true;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001973
1974 ok:
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01001975 list_for_each_entry(child, &device->children, node)
1976 acpi_bus_attach(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001979/**
1980 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1981 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001982 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001983 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1984 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001985 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001986 * If no devices were found, -ENODEV is returned, but it does not mean that
1987 * there has been a real error. There just have been no suitable ACPI objects
1988 * in the table trunk from which the kernel could create a device and add an
1989 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001990 *
1991 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001992 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001993int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001994{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 void *device = NULL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001996
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001997 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001999 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002000
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002001 if (device) {
2002 acpi_bus_attach(device);
2003 return 0;
2004 }
2005 return -ENODEV;
Rajesh Shah3fb02732005-04-28 00:25:52 -07002006}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002007EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002009/**
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002010 * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2011 * @adev: Root of the ACPI namespace scope to walk.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002012 *
2013 * Must be called under acpi_scan_lock.
2014 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002015void acpi_bus_trim(struct acpi_device *adev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002017 struct acpi_scan_handler *handler = adev->handler;
2018 struct acpi_device *child;
2019
2020 list_for_each_entry_reverse(child, &adev->children, node)
2021 acpi_bus_trim(child);
2022
2023 if (handler) {
2024 if (handler->detach)
2025 handler->detach(adev);
2026
2027 adev->handler = NULL;
2028 } else {
2029 device_release_driver(&adev->dev);
2030 }
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002031 /*
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002032 * Most likely, the device is going away, so put it into D3cold before
2033 * that.
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002034 */
Rafael J. Wysocki2c22e652013-11-25 00:52:21 +01002035 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2036 adev->flags.initialized = false;
2037 adev->flags.visited = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
Kristen Accardiceaba662006-02-23 17:56:01 -08002039EXPORT_SYMBOL_GPL(acpi_bus_trim);
2040
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00002041static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Len Brown4be44fc2005-08-05 00:44:28 -04002043 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /*
2046 * Enumerate all fixed-feature devices.
2047 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002048 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2049 struct acpi_device *device = NULL;
2050
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002051 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002052 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002053 ACPI_STA_DEFAULT);
2054 if (result)
2055 return result;
2056
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002057 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002058 result = device_attach(&device->dev);
2059 if (result < 0)
2060 return result;
2061
Daniel Drakec10d7a12012-05-10 00:08:43 +01002062 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002065 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2066 struct acpi_device *device = NULL;
2067
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002068 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002069 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002070 ACPI_STA_DEFAULT);
2071 if (result)
2072 return result;
2073
Rafael J. Wysocki88346162013-11-18 14:18:47 +01002074 device->flags.match_driver = true;
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002075 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002078 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
Bjorn Helgaase747f272009-03-24 16:49:43 -06002081int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Patrick Mochel5b327262006-05-10 10:33:00 -04002085 result = bus_register(&acpi_bus_type);
2086 if (result) {
2087 /* We don't want to quit even if we failed to add suspend/resume */
2088 printk(KERN_ERR PREFIX "Could not register bus type\n");
2089 }
2090
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002091 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002092 acpi_pci_link_init();
Rafael J. Wysockiac212b62013-05-03 00:26:22 +02002093 acpi_processor_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002094 acpi_platform_init();
Rafael J. Wysockif58b0822013-03-06 23:46:20 +01002095 acpi_lpss_init();
Lan Tianyu2fa97fe2013-06-05 02:27:50 +00002096 acpi_cmos_rtc_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002097 acpi_container_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002098 acpi_memory_hotplug_init();
Jiang Liu94add0f2013-06-23 00:59:55 +02002099 acpi_dock_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002100
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002101 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 * Enumerate devices in the ACPI namespace.
2104 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002105 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002107 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002109 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002111 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002113 result = acpi_bus_scan_fixed();
2114 if (result) {
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002115 acpi_detach_data(acpi_root->handle, acpi_scan_drop_device);
2116 acpi_device_del(acpi_root);
2117 put_device(&acpi_root->dev);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002118 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002119 }
2120
2121 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002122
2123 out:
2124 mutex_unlock(&acpi_scan_lock);
2125 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}