blob: 06db2c0360852953abf309df98388e953740ac0b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Rafael J. Wysockid7831562013-11-22 21:52:12 +010017#include <asm/pgtable.h>
18
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060019#include "internal.h"
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050022ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040024extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020027#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#define ACPI_BUS_DEVICE_NAME "System Bus"
29
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000030#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
31
Rafael J. Wysockid7831562013-11-22 21:52:12 +010032#define INVALID_ACPI_HANDLE ((acpi_handle)empty_zero_page)
33
Rafael J. Wysocki683058e2013-05-03 00:26:16 +020034/*
35 * If set, devices will be hot-removed even if they cannot be put offline
36 * gracefully (from the kernel's standpoint).
37 */
38bool acpi_force_hot_remove;
39
Thomas Renninger620e1122010-10-01 10:54:00 +020040static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020041
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080042static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010043static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010044static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080045DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046LIST_HEAD(acpi_wakeup_device_list);
47
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080048struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080049 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080050 unsigned int instance_no;
51 struct list_head node;
52};
Thomas Renninger29b71a12007-07-23 14:43:51 +020053
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010054void acpi_scan_lock_acquire(void)
55{
56 mutex_lock(&acpi_scan_lock);
57}
58EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
59
60void acpi_scan_lock_release(void)
61{
62 mutex_unlock(&acpi_scan_lock);
63}
64EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
65
Rafael J. Wysockica589f92013-01-30 14:27:29 +010066int acpi_scan_add_handler(struct acpi_scan_handler *handler)
67{
68 if (!handler || !handler->attach)
69 return -EINVAL;
70
71 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
72 return 0;
73}
74
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010075int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
76 const char *hotplug_profile_name)
77{
78 int error;
79
80 error = acpi_scan_add_handler(handler);
81 if (error)
82 return error;
83
84 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
85 return 0;
86}
87
Thomas Renninger29b71a12007-07-23 14:43:51 +020088/*
89 * Creates hid/cid(s) string needed for modalias and uevent
90 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
91 * char *modalias: "acpi:IBM0001:ACPI0001"
92*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020093static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
94 int size)
95{
Thomas Renninger29b71a12007-07-23 14:43:51 +020096 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080097 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060098 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020099
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200100 if (list_empty(&acpi_dev->pnp.ids))
101 return 0;
102
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800103 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +0200104 size -= len;
105
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600106 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
107 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +0800108 if (count < 0 || count >= size)
109 return -EINVAL;
110 len += count;
111 size -= count;
112 }
113
Thomas Renninger29b71a12007-07-23 14:43:51 +0200114 modalias[len] = '\0';
115 return len;
116}
117
118static ssize_t
119acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
120 struct acpi_device *acpi_dev = to_acpi_device(dev);
121 int len;
122
123 /* Device has no HID and no CID or string is >1024 */
124 len = create_modalias(acpi_dev, buf, 1024);
125 if (len <= 0)
126 return 0;
127 buf[len++] = '\n';
128 return len;
129}
130static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
131
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100132static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
133 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200134{
135 struct acpi_device *device = NULL;
136 struct acpi_device_physical_node *pn;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200137 bool second_pass = (bool)data;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200138 acpi_status status = AE_OK;
139
140 if (acpi_bus_get_device(handle, &device))
141 return AE_OK;
142
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100143 if (device->handler && !device->handler->hotplug.enabled) {
144 *ret_p = &device->dev;
145 return AE_SUPPORT;
146 }
147
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200148 mutex_lock(&device->physical_node_lock);
149
150 list_for_each_entry(pn, &device->physical_node_list, node) {
151 int ret;
152
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200153 if (second_pass) {
154 /* Skip devices offlined by the first pass. */
155 if (pn->put_online)
156 continue;
157 } else {
158 pn->put_online = false;
159 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200160 ret = device_offline(pn->dev);
161 if (acpi_force_hot_remove)
162 continue;
163
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200164 if (ret >= 0) {
165 pn->put_online = !ret;
166 } else {
167 *ret_p = pn->dev;
168 if (second_pass) {
169 status = AE_ERROR;
170 break;
171 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200172 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200173 }
174
175 mutex_unlock(&device->physical_node_lock);
176
177 return status;
178}
179
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100180static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
181 void **ret_p)
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200182{
183 struct acpi_device *device = NULL;
184 struct acpi_device_physical_node *pn;
185
186 if (acpi_bus_get_device(handle, &device))
187 return AE_OK;
188
189 mutex_lock(&device->physical_node_lock);
190
191 list_for_each_entry(pn, &device->physical_node_list, node)
192 if (pn->put_online) {
193 device_online(pn->dev);
194 pn->put_online = false;
195 }
196
197 mutex_unlock(&device->physical_node_lock);
198
199 return AE_OK;
200}
201
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100202static int acpi_scan_hot_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Yinghai Lu5993c462013-01-11 22:40:41 +0000204 acpi_handle handle = device->handle;
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200205 struct device *errdev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100206 acpi_status status;
Toshi Kani882fd122013-03-13 19:29:26 +0000207 unsigned long long sta;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100208
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200209 /*
210 * Carry out two passes here and ignore errors in the first pass,
211 * because if the devices in question are memory blocks and
212 * CONFIG_MEMCG is set, one of the blocks may hold data structures
213 * that the other blocks depend on, but it is not known in advance which
214 * block holds them.
215 *
216 * If the first pass is successful, the second one isn't needed, though.
217 */
218 errdev = NULL;
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100219 status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
220 NULL, acpi_bus_offline, (void *)false,
221 (void **)&errdev);
222 if (status == AE_SUPPORT) {
223 dev_warn(errdev, "Offline disabled.\n");
224 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
225 acpi_bus_online, NULL, NULL, NULL);
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100226 return -EPERM;
227 }
228 acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200229 if (errdev) {
230 errdev = NULL;
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200231 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100232 NULL, acpi_bus_offline, (void *)true,
233 (void **)&errdev);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200234 if (!errdev || acpi_force_hot_remove)
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100235 acpi_bus_offline(handle, 0, (void *)true,
236 (void **)&errdev);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200237
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200238 if (errdev && !acpi_force_hot_remove) {
239 dev_warn(errdev, "Offline failed.\n");
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100240 acpi_bus_online(handle, 0, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200241 acpi_walk_namespace(ACPI_TYPE_ANY, handle,
Rafael J. Wysocki7f28dde2013-11-07 01:41:14 +0100242 ACPI_UINT32_MAX, acpi_bus_online,
243 NULL, NULL, NULL);
Rafael J. Wysocki303bfdb2013-05-23 10:43:13 +0200244 return -EBUSY;
245 }
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200246 }
247
Zhang Rui26d46862008-04-29 02:35:48 -0400248 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100249 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400250
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100251 acpi_bus_trim(device);
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200252
Jiang Liu7d2421f2013-06-29 00:24:40 +0800253 acpi_evaluate_lck(handle, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /*
255 * TBD: _EJD support.
256 */
Jiang Liu7d2421f2013-06-29 00:24:40 +0800257 status = acpi_evaluate_ej0(handle);
258 if (status == AE_NOT_FOUND)
259 return -ENODEV;
260 else if (ACPI_FAILURE(status))
261 return -EIO;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100262
Toshi Kani882fd122013-03-13 19:29:26 +0000263 /*
264 * Verify if eject was indeed successful. If not, log an error
265 * message. No need to call _OST since _EJ0 call was made OK.
266 */
267 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
268 if (ACPI_FAILURE(status)) {
269 acpi_handle_warn(handle,
270 "Status check after eject failed (0x%x)\n", status);
271 } else if (sta & ACPI_STA_DEVICE_ENABLED) {
272 acpi_handle_warn(handle,
273 "Eject incomplete - status 0x%llx\n", sta);
274 }
275
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100276 return 0;
277}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100278
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100279static int acpi_scan_device_not_present(struct acpi_device *adev)
280{
281 if (!acpi_device_enumerated(adev)) {
282 dev_warn(&adev->dev, "Still not present\n");
283 return -EALREADY;
284 }
285 acpi_bus_trim(adev);
286 return 0;
287}
288
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100289static int acpi_scan_device_check(struct acpi_device *adev)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100290{
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200291 int error;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100292
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100293 acpi_bus_get_status(adev);
294 if (adev->status.present || adev->status.functional) {
295 /*
296 * This function is only called for device objects for which
297 * matching scan handlers exist. The only situation in which
298 * the scan handler is not attached to this device object yet
299 * is when the device has just appeared (either it wasn't
300 * present at all before or it was removed and then added
301 * again).
302 */
303 if (adev->handler) {
304 dev_warn(&adev->dev, "Already enumerated\n");
305 return -EALREADY;
306 }
307 error = acpi_bus_scan(adev->handle);
308 if (error) {
309 dev_warn(&adev->dev, "Namespace scan failure\n");
310 return error;
311 }
312 if (!adev->handler) {
313 dev_warn(&adev->dev, "Enumeration failure\n");
314 error = -ENODEV;
315 }
316 } else {
317 error = acpi_scan_device_not_present(adev);
318 }
319 return error;
320}
321
322static int acpi_scan_bus_check(struct acpi_device *adev)
323{
324 struct acpi_scan_handler *handler = adev->handler;
325 struct acpi_device *child;
326 int error;
327
328 acpi_bus_get_status(adev);
329 if (!(adev->status.present || adev->status.functional)) {
330 acpi_scan_device_not_present(adev);
331 return 0;
332 }
333 if (handler && handler->hotplug.scan_dependent)
334 return handler->hotplug.scan_dependent(adev);
335
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100336 error = acpi_bus_scan(adev->handle);
337 if (error) {
338 dev_warn(&adev->dev, "Namespace scan failure\n");
339 return error;
340 }
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100341 list_for_each_entry(child, &adev->children, node) {
342 error = acpi_scan_bus_check(child);
343 if (error)
344 return error;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100345 }
346 return 0;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100347}
348
Rafael J. Wysocki3338db02013-11-22 21:55:20 +0100349static void acpi_device_hotplug(void *data, u32 src)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100350{
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100351 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100352 struct acpi_device *adev = data;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100353 int error;
354
Rafael J. Wysocki683058e2013-05-03 00:26:16 +0200355 lock_device_hotplug();
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200356 mutex_lock(&acpi_scan_lock);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100357
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100358 /*
359 * The device object's ACPI handle cannot become invalid as long as we
360 * are holding acpi_scan_lock, but it may have become invalid before
361 * that lock was acquired.
362 */
363 if (adev->handle == INVALID_ACPI_HANDLE)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100364 goto out;
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100365
366 switch (src) {
367 case ACPI_NOTIFY_BUS_CHECK:
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100368 error = acpi_scan_bus_check(adev);
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100369 break;
370 case ACPI_NOTIFY_DEVICE_CHECK:
Rafael J. Wysocki443fc822013-11-22 21:55:43 +0100371 error = acpi_scan_device_check(adev);
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100372 break;
373 case ACPI_NOTIFY_EJECT_REQUEST:
374 case ACPI_OST_EC_OSPM_EJECT:
375 error = acpi_scan_hot_remove(adev);
376 break;
377 default:
378 error = -EINVAL;
379 break;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100380 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100381 if (!error)
382 ost_code = ACPI_OST_SC_SUCCESS;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100383
384 out:
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100385 acpi_evaluate_hotplug_ost(adev->handle, src, ost_code, NULL);
386 put_device(&adev->dev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100387 mutex_unlock(&acpi_scan_lock);
Rafael J. Wysockie0ae8fe2013-08-30 14:19:29 +0200388 unlock_device_hotplug();
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100389}
390
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000391static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100392{
Rafael J. Wysocki1ceaba02013-11-22 21:54:52 +0100393 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
Toshi Kani2cbb14f2013-03-05 22:33:31 +0000394 struct acpi_scan_handler *handler = data;
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100395 struct acpi_device *adev;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100396 acpi_status status;
397
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100398 if (acpi_bus_get_device(handle, &adev))
399 goto err_out;
400
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100401 switch (type) {
402 case ACPI_NOTIFY_BUS_CHECK:
403 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100404 break;
405 case ACPI_NOTIFY_DEVICE_CHECK:
406 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100407 break;
408 case ACPI_NOTIFY_EJECT_REQUEST:
409 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
Rafael J. Wysocki1ceaba02013-11-22 21:54:52 +0100410 if (!handler->hotplug.enabled) {
411 acpi_handle_err(handle, "Eject disabled\n");
412 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
413 goto err_out;
414 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100415 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
416 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
417 break;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100418 default:
419 /* non-hotplug event; possibly handled by other handler */
420 return;
421 }
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100422 get_device(&adev->dev);
423 status = acpi_hotplug_execute(acpi_device_hotplug, adev, type);
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100424 if (ACPI_SUCCESS(status))
425 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100426
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100427 put_device(&adev->dev);
428
Rafael J. Wysockia3b1b1e2013-11-07 01:41:58 +0100429 err_out:
Rafael J. Wysocki1ceaba02013-11-22 21:54:52 +0100430 acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100431}
432
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100433static ssize_t real_power_state_show(struct device *dev,
434 struct device_attribute *attr, char *buf)
435{
436 struct acpi_device *adev = to_acpi_device(dev);
437 int state;
438 int ret;
439
440 ret = acpi_device_get_power(adev, &state);
441 if (ret)
442 return ret;
443
444 return sprintf(buf, "%s\n", acpi_power_state_string(state));
445}
446
447static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
448
449static ssize_t power_state_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
452 struct acpi_device *adev = to_acpi_device(dev);
453
454 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
455}
456
457static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800460acpi_eject_store(struct device *d, struct device_attribute *attr,
461 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800463 struct acpi_device *acpi_device = to_acpi_device(d);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100464 acpi_object_type not_used;
465 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100467 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100470 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
471 && !acpi_device->driver)
472 return -ENODEV;
473
474 status = acpi_get_type(acpi_device->handle, &not_used);
475 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
476 return -ENODEV;
477
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200478 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100479 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100480 get_device(&acpi_device->dev);
Rafael J. Wysockic27b2c32013-11-22 21:55:07 +0100481 status = acpi_hotplug_execute(acpi_device_hotplug, acpi_device,
Rafael J. Wysocki7b981182013-11-07 01:45:40 +0100482 ACPI_OST_EC_OSPM_EJECT);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200483 if (ACPI_SUCCESS(status))
484 return count;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100485
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200486 put_device(&acpi_device->dev);
Rafael J. Wysockif943db42013-08-28 21:41:07 +0200487 acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100488 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
Rafael J. Wysocki5add99c2013-11-07 01:41:39 +0100489 return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800492static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800494static ssize_t
495acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
496 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600498 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800499}
500static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
501
Lv Zheng923d4a42012-10-30 14:43:26 +0100502static ssize_t acpi_device_uid_show(struct device *dev,
503 struct device_attribute *attr, char *buf)
504{
505 struct acpi_device *acpi_dev = to_acpi_device(dev);
506
507 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
508}
509static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
510
511static ssize_t acpi_device_adr_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
513{
514 struct acpi_device *acpi_dev = to_acpi_device(dev);
515
516 return sprintf(buf, "0x%08x\n",
517 (unsigned int)(acpi_dev->pnp.bus_address));
518}
519static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
520
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800521static ssize_t
522acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
523 struct acpi_device *acpi_dev = to_acpi_device(dev);
524 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
525 int result;
526
527 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600528 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800529 goto end;
530
531 result = sprintf(buf, "%s\n", (char*)path.pointer);
532 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600533end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800534 return result;
535}
536static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
537
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600538/* sysfs file that shows description text from the ACPI _STR method */
539static ssize_t description_show(struct device *dev,
540 struct device_attribute *attr,
541 char *buf) {
542 struct acpi_device *acpi_dev = to_acpi_device(dev);
543 int result;
544
545 if (acpi_dev->pnp.str_obj == NULL)
546 return 0;
547
548 /*
549 * The _STR object contains a Unicode identifier for a device.
550 * We need to convert to utf-8 so it can be displayed.
551 */
552 result = utf16s_to_utf8s(
553 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
554 acpi_dev->pnp.str_obj->buffer.length,
555 UTF16_LITTLE_ENDIAN, buf,
556 PAGE_SIZE);
557
558 buf[result++] = '\n';
559
560 return result;
561}
562static DEVICE_ATTR(description, 0444, description_show, NULL);
563
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100564static ssize_t
565acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
566 char *buf) {
567 struct acpi_device *acpi_dev = to_acpi_device(dev);
568
569 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
570}
571static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
572
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800573static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600575 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800576 acpi_status status;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100577 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800578 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800580 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800581 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800582 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600583 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800584 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600585 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800586 goto end;
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200589 if (!list_empty(&dev->pnp.ids)) {
590 result = device_create_file(&dev->dev, &dev_attr_hid);
591 if (result)
592 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200594 result = device_create_file(&dev->dev, &dev_attr_modalias);
595 if (result)
596 goto end;
597 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200598
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600599 /*
600 * If device has _STR, 'description' file is created
601 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800602 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600603 status = acpi_evaluate_object(dev->handle, "_STR",
604 NULL, &buffer);
605 if (ACPI_FAILURE(status))
606 buffer.pointer = NULL;
607 dev->pnp.str_obj = buffer.pointer;
608 result = device_create_file(&dev->dev, &dev_attr_description);
609 if (result)
610 goto end;
611 }
612
Toshi Kanid4e1a692013-03-04 21:30:41 +0000613 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100614 result = device_create_file(&dev->dev, &dev_attr_adr);
615 if (dev->pnp.unique_id)
616 result = device_create_file(&dev->dev, &dev_attr_uid);
617
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100618 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
619 if (ACPI_SUCCESS(status)) {
620 dev->pnp.sun = (unsigned long)sun;
621 result = device_create_file(&dev->dev, &dev_attr_sun);
622 if (result)
623 goto end;
624 } else {
625 dev->pnp.sun = (unsigned long)-1;
626 }
627
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800628 /*
629 * If device has _EJ0, 'eject' file is created that is used to trigger
630 * hot-removal function from userland.
631 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800632 if (acpi_has_method(dev->handle, "_EJ0")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800633 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100634 if (result)
635 return result;
636 }
637
638 if (dev->flags.power_manageable) {
639 result = device_create_file(&dev->dev, &dev_attr_power_state);
640 if (result)
641 return result;
642
643 if (dev->power.flags.power_resources)
644 result = device_create_file(&dev->dev,
645 &dev_attr_real_power_state);
646 }
647
Alex Chiang0c526d92009-05-14 08:31:37 -0600648end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800649 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800650}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800652static void acpi_device_remove_files(struct acpi_device *dev)
653{
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100654 if (dev->flags.power_manageable) {
655 device_remove_file(&dev->dev, &dev_attr_power_state);
656 if (dev->power.flags.power_resources)
657 device_remove_file(&dev->dev,
658 &dev_attr_real_power_state);
659 }
660
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800661 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600662 * If device has _STR, remove 'description' file
663 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800664 if (acpi_has_method(dev->handle, "_STR")) {
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600665 kfree(dev->pnp.str_obj);
666 device_remove_file(&dev->dev, &dev_attr_description);
667 }
668 /*
669 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800670 */
Jiang Liu952c63e2013-06-29 00:24:38 +0800671 if (acpi_has_method(dev->handle, "_EJ0"))
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800672 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800673
Jiang Liu952c63e2013-06-29 00:24:38 +0800674 if (acpi_has_method(dev->handle, "_SUN"))
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100675 device_remove_file(&dev->dev, &dev_attr_sun);
676
Lv Zheng923d4a42012-10-30 14:43:26 +0100677 if (dev->pnp.unique_id)
678 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000679 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100680 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600681 device_remove_file(&dev->dev, &dev_attr_modalias);
682 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600683 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800684 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800687 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200689
Mika Westerbergcf761af2012-10-31 22:44:41 +0100690static const struct acpi_device_id *__acpi_match_device(
691 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200692{
693 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600694 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200695
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800696 /*
697 * If the device is not present, it is unnecessary to load device
698 * driver for it.
699 */
700 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100701 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800702
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600703 for (id = ids; id->id[0]; id++)
704 list_for_each_entry(hwid, &device->pnp.ids, list)
705 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100706 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200707
Mika Westerbergcf761af2012-10-31 22:44:41 +0100708 return NULL;
709}
710
711/**
712 * acpi_match_device - Match a struct device against a given list of ACPI IDs
713 * @ids: Array of struct acpi_device_id object to match against.
714 * @dev: The device structure to match.
715 *
716 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
717 * object for that handle and use that object to match against a given list of
718 * device IDs.
719 *
720 * Return a pointer to the first matching ID on success or %NULL on failure.
721 */
722const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
723 const struct device *dev)
724{
725 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100726 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100727
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100728 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100729 return NULL;
730
731 return __acpi_match_device(adev, ids);
732}
733EXPORT_SYMBOL_GPL(acpi_match_device);
734
735int acpi_match_device_ids(struct acpi_device *device,
736 const struct acpi_device_id *ids)
737{
738 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200739}
740EXPORT_SYMBOL(acpi_match_device_ids);
741
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100742static void acpi_free_power_resources_lists(struct acpi_device *device)
743{
744 int i;
745
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100746 if (device->wakeup.flags.valid)
747 acpi_power_resources_list_free(&device->wakeup.resources);
748
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100749 if (!device->flags.power_manageable)
750 return;
751
752 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
753 struct acpi_device_power_state *ps = &device->power.states[i];
754 acpi_power_resources_list_free(&ps->resources);
755 }
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600756}
757
Patrick Mochel1890a972006-12-07 20:56:31 +0800758static void acpi_device_release(struct device *dev)
759{
760 struct acpi_device *acpi_dev = to_acpi_device(dev);
761
Toshi Kanic0af4172013-03-04 21:30:42 +0000762 acpi_free_pnp_ids(&acpi_dev->pnp);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100763 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800764 kfree(acpi_dev);
765}
766
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800767static int acpi_bus_match(struct device *dev, struct device_driver *drv)
768{
769 struct acpi_device *acpi_dev = to_acpi_device(dev);
770 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
771
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100772 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100773 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800774}
775
Kay Sievers7eff2e72007-08-14 15:15:12 +0200776static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800777{
778 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200779 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800780
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200781 if (list_empty(&acpi_dev->pnp.ids))
782 return 0;
783
Kay Sievers7eff2e72007-08-14 15:15:12 +0200784 if (add_uevent_var(env, "MODALIAS="))
785 return -ENOMEM;
786 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
787 sizeof(env->buf) - env->buflen);
788 if (len >= (sizeof(env->buf) - env->buflen))
789 return -ENOMEM;
790 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return 0;
792}
793
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000794static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
795{
796 struct acpi_device *device = data;
797
798 device->driver->ops.notify(device, event);
799}
800
801static acpi_status acpi_device_notify_fixed(void *data)
802{
803 struct acpi_device *device = data;
804
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000805 /* Fixed hardware devices have no handles */
806 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000807 return AE_OK;
808}
809
810static int acpi_device_install_notify_handler(struct acpi_device *device)
811{
812 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000813
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000814 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000815 status =
816 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
817 acpi_device_notify_fixed,
818 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000819 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000820 status =
821 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
822 acpi_device_notify_fixed,
823 device);
824 else
825 status = acpi_install_notify_handler(device->handle,
826 ACPI_DEVICE_NOTIFY,
827 acpi_device_notify,
828 device);
829
830 if (ACPI_FAILURE(status))
831 return -EINVAL;
832 return 0;
833}
834
835static void acpi_device_remove_notify_handler(struct acpi_device *device)
836{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000837 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000838 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
839 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000840 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000841 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
842 acpi_device_notify_fixed);
843 else
844 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
845 acpi_device_notify);
846}
847
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200848static int acpi_device_probe(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800849{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800850 struct acpi_device *acpi_dev = to_acpi_device(dev);
851 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
852 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Rafael J. Wysocki24071f42013-06-16 00:36:41 +0200854 if (acpi_dev->handler)
855 return -EINVAL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000856
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200857 if (!acpi_drv->ops.add)
858 return -ENOSYS;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800859
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200860 ret = acpi_drv->ops.add(acpi_dev);
861 if (ret)
862 return ret;
863
864 acpi_dev->driver = acpi_drv;
865 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
866 "Driver [%s] successfully bound to device [%s]\n",
867 acpi_drv->name, acpi_dev->pnp.bus_id));
868
869 if (acpi_drv->ops.notify) {
870 ret = acpi_device_install_notify_handler(acpi_dev);
871 if (ret) {
872 if (acpi_drv->ops.remove)
873 acpi_drv->ops.remove(acpi_dev);
874
875 acpi_dev->driver = NULL;
876 acpi_dev->driver_data = NULL;
877 return ret;
878 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800879 }
Rafael J. Wysockid9e455f2013-06-10 13:00:50 +0200880
881 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
882 acpi_drv->name, acpi_dev->pnp.bus_id));
883 get_device(dev);
884 return 0;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800885}
886
887static int acpi_device_remove(struct device * dev)
888{
889 struct acpi_device *acpi_dev = to_acpi_device(dev);
890 struct acpi_driver *acpi_drv = acpi_dev->driver;
891
892 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000893 if (acpi_drv->ops.notify)
894 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800895 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100896 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800897 }
898 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700899 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800900
901 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800902 return 0;
903}
904
David Brownell55955aa2007-05-08 00:28:35 -0700905struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800906 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800907 .match = acpi_bus_match,
908 .probe = acpi_device_probe,
909 .remove = acpi_device_remove,
910 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911};
912
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100913static void acpi_device_del(struct acpi_device *device)
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200914{
Rafael J. Wysockid7831562013-11-22 21:52:12 +0100915 mutex_lock(&acpi_device_lock);
916 if (device->parent)
917 list_del(&device->node);
918
919 list_del(&device->wakeup_list);
920 mutex_unlock(&acpi_device_lock);
921
922 acpi_power_add_remove_device(device, false);
923 acpi_device_remove_files(device);
924 if (device->remove)
925 device->remove(device);
926
927 device_del(&device->dev);
928}
929
930static LIST_HEAD(acpi_device_del_list);
931static DEFINE_MUTEX(acpi_device_del_lock);
932
933static void acpi_device_del_work_fn(struct work_struct *work_not_used)
934{
935 for (;;) {
936 struct acpi_device *adev;
937
938 mutex_lock(&acpi_device_del_lock);
939
940 if (list_empty(&acpi_device_del_list)) {
941 mutex_unlock(&acpi_device_del_lock);
942 break;
943 }
944 adev = list_first_entry(&acpi_device_del_list,
945 struct acpi_device, del_list);
946 list_del(&adev->del_list);
947
948 mutex_unlock(&acpi_device_del_lock);
949
950 acpi_device_del(adev);
951 /*
952 * Drop references to all power resources that might have been
953 * used by the device.
954 */
955 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
956 put_device(&adev->dev);
957 }
958}
959
960/**
961 * acpi_scan_drop_device - Drop an ACPI device object.
962 * @handle: Handle of an ACPI namespace node, not used.
963 * @context: Address of the ACPI device object to drop.
964 *
965 * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
966 * namespace node the device object pointed to by @context is attached to.
967 *
968 * The unregistration is carried out asynchronously to avoid running
969 * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
970 * ensure the correct ordering (the device objects must be unregistered in the
971 * same order in which the corresponding namespace nodes are deleted).
972 */
973static void acpi_scan_drop_device(acpi_handle handle, void *context)
974{
975 static DECLARE_WORK(work, acpi_device_del_work_fn);
976 struct acpi_device *adev = context;
977
978 mutex_lock(&acpi_device_del_lock);
979
980 /*
981 * Use the ACPI hotplug workqueue which is ordered, so this work item
982 * won't run after any hotplug work items submitted subsequently. That
983 * prevents attempts to register device objects identical to those being
984 * deleted from happening concurrently (such attempts result from
985 * hotplug events handled via the ACPI hotplug workqueue). It also will
986 * run after all of the work items submitted previosuly, which helps
987 * those work items to ensure that they are not accessing stale device
988 * objects.
989 */
990 if (list_empty(&acpi_device_del_list))
991 acpi_queue_hotplug_work(&work);
992
993 list_add_tail(&adev->del_list, &acpi_device_del_list);
994 /* Make acpi_ns_validate_handle() return NULL for this handle. */
995 adev->handle = INVALID_ACPI_HANDLE;
996
997 mutex_unlock(&acpi_device_del_lock);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +0200998}
999
1000int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1001{
1002 acpi_status status;
1003
1004 if (!device)
1005 return -EINVAL;
1006
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001007 status = acpi_get_data(handle, acpi_scan_drop_device, (void **)device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001008 if (ACPI_FAILURE(status) || !*device) {
1009 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1010 handle));
1011 return -ENODEV;
1012 }
1013 return 0;
1014}
Rafael J. Wysocki65859252013-10-01 23:02:43 +02001015EXPORT_SYMBOL(acpi_bus_get_device);
Rafael J. Wysockicaf5c032013-07-30 14:38:34 +02001016
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001017int acpi_device_add(struct acpi_device *device,
1018 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001020 int result;
1021 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1022 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001023
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001024 if (device->handle) {
1025 acpi_status status;
1026
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001027 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001028 device);
1029 if (ACPI_FAILURE(status)) {
1030 acpi_handle_err(device->handle,
1031 "Unable to attach device data\n");
1032 return -ENODEV;
1033 }
1034 }
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 /*
1037 * Linkage
1038 * -------
1039 * Link this device to its parent and siblings.
1040 */
1041 INIT_LIST_HEAD(&device->children);
1042 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001044 INIT_LIST_HEAD(&device->physical_node_list);
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001045 INIT_LIST_HEAD(&device->del_list);
Lan Tianyu1033f902012-08-17 14:44:09 +08001046 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001048 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1049 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001050 pr_err(PREFIX "Memory allocation error\n");
1051 result = -ENOMEM;
1052 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001053 }
1054
Shaohua Li90905892009-04-07 10:24:29 +08001055 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001056 /*
1057 * Find suitable bus_id and instance number in acpi_bus_id_list
1058 * If failed, create one and link it into acpi_bus_id_list
1059 */
1060 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001061 if (!strcmp(acpi_device_bus_id->bus_id,
1062 acpi_device_hid(device))) {
1063 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001064 found = 1;
1065 kfree(new_bus_id);
1066 break;
1067 }
1068 }
Alex Chiang0c526d92009-05-14 08:31:37 -06001069 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001070 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -06001071 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001072 acpi_device_bus_id->instance_no = 0;
1073 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1074 }
Kay Sievers07944692008-10-30 01:18:59 +01001075 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 +08001076
Len Brown33b57152008-12-15 22:09:26 -05001077 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -05001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (device->wakeup.flags.valid)
1081 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +08001082 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Patrick Mochel1890a972006-12-07 20:56:31 +08001084 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001085 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +08001086 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001087 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001088 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -06001089 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -06001090 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001091 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001092 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +08001093
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001094 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -06001095 if (result)
Kay Sievers07944692008-10-30 01:18:59 +01001096 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1097 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001098
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001099 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001100
1101 err:
Shaohua Li90905892009-04-07 10:24:29 +08001102 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -05001103 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001104 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001105 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +08001106 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001107
1108 err_detach:
Rafael J. Wysockid7831562013-11-22 21:52:12 +01001109 acpi_detach_data(device->handle, acpi_scan_drop_device);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001110 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
Zhang Rui9e89dde2006-12-07 20:56:16 +08001113/* --------------------------------------------------------------------------
1114 Driver Management
1115 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001117 * acpi_bus_register_driver - register a driver with the ACPI bus
1118 * @driver: driver being registered
1119 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001121 * devices that match the driver's criteria and binds. Returns zero for
1122 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 */
Len Brown4be44fc2005-08-05 00:44:28 -04001124int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Patrick Mochel1890a972006-12-07 20:56:31 +08001126 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001129 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001130 driver->drv.name = driver->name;
1131 driver->drv.bus = &acpi_bus_type;
1132 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Patrick Mochel1890a972006-12-07 20:56:31 +08001134 ret = driver_register(&driver->drv);
1135 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Len Brown4be44fc2005-08-05 00:44:28 -04001138EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140/**
Hanjun Guob27b14c2013-09-22 15:42:41 +08001141 * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001142 * @driver: driver to unregister
1143 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1145 * devices that match the driver's criteria and unbinds.
1146 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001147void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Patrick Mochel1890a972006-12-07 20:56:31 +08001149 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
Len Brown4be44fc2005-08-05 00:44:28 -04001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152EXPORT_SYMBOL(acpi_bus_unregister_driver);
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154/* --------------------------------------------------------------------------
1155 Device Enumeration
1156 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001157static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1158{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001159 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001160 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001161
1162 /*
1163 * Fixed hardware devices do not appear in the namespace and do not
1164 * have handles, but we fabricate acpi_devices for them, so we have
1165 * to deal with them specially.
1166 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001167 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001168 return acpi_root;
1169
1170 do {
1171 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001172 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001173 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1174 } while (acpi_bus_get_device(handle, &device));
1175 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001176}
1177
Len Brownc8f7a622006-07-09 17:22:28 -04001178acpi_status
1179acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1180{
1181 acpi_status status;
1182 acpi_handle tmp;
1183 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1184 union acpi_object *obj;
1185
1186 status = acpi_get_handle(handle, "_EJD", &tmp);
1187 if (ACPI_FAILURE(status))
1188 return status;
1189
1190 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1191 if (ACPI_SUCCESS(status)) {
1192 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001193 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1194 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001195 kfree(buffer.pointer);
1196 }
1197 return status;
1198}
1199EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1200
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001201static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1202 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001204 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1205 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001207 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001208 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001210 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001211 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001213 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001215 /* _PRW */
1216 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1217 if (ACPI_FAILURE(status)) {
1218 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001219 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001220 }
1221
1222 package = (union acpi_object *)buffer.pointer;
1223
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001224 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001225 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001228 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001229 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (element->type == ACPI_TYPE_PACKAGE) {
1232 if ((element->package.count < 2) ||
1233 (element->package.elements[0].type !=
1234 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001235 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001236 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001237
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001238 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001240 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 (u32) element->package.elements[1].integer.value;
1242 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001243 wakeup->gpe_device = NULL;
1244 wakeup->gpe_number = element->integer.value;
1245 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001246 goto out;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001250 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001251 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001252
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001253 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001255 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1256 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001257 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001259 if (!list_empty(&wakeup->resources)) {
1260 int sleep_state;
1261
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001262 err = acpi_power_wakeup_list_init(&wakeup->resources,
1263 &sleep_state);
1264 if (err) {
1265 acpi_handle_warn(handle, "Retrieving current states "
1266 "of wakeup power resources failed\n");
1267 acpi_power_resources_list_free(&wakeup->resources);
1268 goto out;
1269 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001270 if (sleep_state < wakeup->sleep_state) {
1271 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1272 "(S%d) by S%d from power resources\n",
1273 (int)wakeup->sleep_state, sleep_state);
1274 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Lin Mingbba63a22010-12-13 13:39:17 +08001277 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001278
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001279 out:
1280 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001281 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001284static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001286 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001287 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001288 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001289 {"PNP0C0E", 0},
1290 {"", 0},
1291 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001292 acpi_status status;
1293 acpi_event_status event_status;
1294
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001295 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001296
1297 /* Power button, Lid switch always enable wakeup */
1298 if (!acpi_match_device_ids(device, button_device_ids)) {
1299 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001300 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1301 /* Do not use Lid/sleep button for S5 wakeup */
1302 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1303 device->wakeup.sleep_state = ACPI_STATE_S4;
1304 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001305 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001306 return;
1307 }
1308
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001309 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1310 device->wakeup.gpe_number,
1311 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001312 if (status == AE_OK)
1313 device->wakeup.flags.run_wake =
1314 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1315}
1316
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001317static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001318{
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001319 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001320
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001321 /* Presence of _PRW indicates wake capable */
Jiang Liu952c63e2013-06-29 00:24:38 +08001322 if (!acpi_has_method(device->handle, "_PRW"))
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001323 return;
1324
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001325 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1326 &device->wakeup);
1327 if (err) {
1328 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001329 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001333 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001334 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001335 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1336 * system for the ACPI device with the _PRW object.
1337 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1338 * So it is necessary to call _DSW object first. Only when it is not
1339 * present will the _PSW object used.
1340 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001341 err = acpi_device_sleep_wake(device, 0, 0, 0);
1342 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001343 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1344 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001347static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001349 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001350 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1351 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001352 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001353
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001354 INIT_LIST_HEAD(&ps->resources);
1355
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001356 /* Evaluate "_PRx" to get referenced power resources */
1357 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1358 if (ACPI_SUCCESS(status)) {
1359 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001360
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001361 if (buffer.length && package
1362 && package->type == ACPI_TYPE_PACKAGE
1363 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001364 int err = acpi_extract_power_resources(package, 0,
1365 &ps->resources);
1366 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001367 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001368 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001369 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001370 }
1371
1372 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001373 pathname[2] = 'S';
Jiang Liu952c63e2013-06-29 00:24:38 +08001374 if (acpi_has_method(device->handle, pathname))
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001375 ps->flags.explicit_set = 1;
1376
1377 /*
1378 * State is valid if there are means to put the device into it.
1379 * D3hot is only valid if _PR3 present.
1380 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001381 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001382 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1383 ps->flags.valid = 1;
1384 ps->flags.os_accessible = 1;
1385 }
1386
1387 ps->power = -1; /* Unknown - driver assigned */
1388 ps->latency = -1; /* Unknown - driver assigned */
1389}
1390
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001391static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001393 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001395 /* Presence of _PS0|_PR0 indicates 'power manageable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001396 if (!acpi_has_method(device->handle, "_PS0") &&
1397 !acpi_has_method(device->handle, "_PR0"))
1398 return;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001399
1400 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001403 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
Jiang Liu952c63e2013-06-29 00:24:38 +08001405 if (acpi_has_method(device->handle, "_PSC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001406 device->power.flags.explicit_get = 1;
Jiang Liu952c63e2013-06-29 00:24:38 +08001407 if (acpi_has_method(device->handle, "_IRC"))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001408 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001411 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001413 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1414 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001416 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Zhang Rui9e89dde2006-12-07 20:56:16 +08001418 /* Set defaults for D0 and D3 states (always valid) */
1419 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1420 device->power.states[ACPI_STATE_D0].power = 100;
Rafael J. Wysocki8ad928d2013-07-30 14:36:20 +02001421 device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1422 device->power.states[ACPI_STATE_D3_COLD].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001424 /* Set D3cold's explicit_set flag if _PS3 exists. */
1425 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1426 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1427
Aaron Lu1399dfc2012-11-21 23:33:40 +01001428 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1429 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1430 device->power.flags.power_resources)
1431 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1432
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001433 if (acpi_bus_init_power(device)) {
1434 acpi_free_power_resources_lists(device);
1435 device->flags.power_manageable = 0;
1436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001439static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 /* Presence of _STA indicates 'dynamic_status' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001442 if (acpi_has_method(device->handle, "_STA"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 device->flags.dynamic_status = 1;
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /* Presence of _RMV indicates 'removable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001446 if (acpi_has_method(device->handle, "_RMV"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 device->flags.removable = 1;
1448
1449 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
Jiang Liu952c63e2013-06-29 00:24:38 +08001450 if (acpi_has_method(device->handle, "_EJD") ||
1451 acpi_has_method(device->handle, "_EJ0"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 device->flags.ejectable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001455static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456{
Len Brown4be44fc2005-08-05 00:44:28 -04001457 char bus_id[5] = { '?', 0 };
1458 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1459 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 /*
1462 * Bus ID
1463 * ------
1464 * The device's Bus ID is simply the object name.
1465 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1466 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001467 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001469 return;
1470 }
1471
1472 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 case ACPI_BUS_TYPE_POWER_BUTTON:
1474 strcpy(device->pnp.bus_id, "PWRF");
1475 break;
1476 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1477 strcpy(device->pnp.bus_id, "SLPF");
1478 break;
1479 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001480 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /* Clean up trailing underscores (if any) */
1482 for (i = 3; i > 1; i--) {
1483 if (bus_id[i] == '_')
1484 bus_id[i] = '\0';
1485 else
1486 break;
1487 }
1488 strcpy(device->pnp.bus_id, bus_id);
1489 break;
1490 }
1491}
1492
Zhang Rui54735262007-01-11 02:09:09 -05001493/*
Jiang Liuebf4df82013-06-29 00:24:41 +08001494 * acpi_ata_match - see if an acpi object is an ATA device
1495 *
1496 * If an acpi object has one of the ACPI ATA methods defined,
1497 * then we can safely call it an ATA device.
1498 */
1499bool acpi_ata_match(acpi_handle handle)
1500{
1501 return acpi_has_method(handle, "_GTF") ||
1502 acpi_has_method(handle, "_GTM") ||
1503 acpi_has_method(handle, "_STM") ||
1504 acpi_has_method(handle, "_SDD");
1505}
1506
1507/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001508 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001509 *
1510 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1511 * then we can safely call it an ejectable drive bay
1512 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001513bool acpi_bay_match(acpi_handle handle)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001514{
Zhang Rui54735262007-01-11 02:09:09 -05001515 acpi_handle phandle;
1516
Jiang Liu952c63e2013-06-29 00:24:38 +08001517 if (!acpi_has_method(handle, "_EJ0"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001518 return false;
1519 if (acpi_ata_match(handle))
1520 return true;
1521 if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1522 return false;
Zhang Rui54735262007-01-11 02:09:09 -05001523
Jiang Liuebf4df82013-06-29 00:24:41 +08001524 return acpi_ata_match(phandle);
Zhang Rui54735262007-01-11 02:09:09 -05001525}
1526
Frank Seidel3620f2f2007-12-07 13:20:34 +01001527/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001528 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001529 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001530bool acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001531{
Jiang Liuebf4df82013-06-29 00:24:41 +08001532 return acpi_has_method(handle, "_DCK");
Frank Seidel3620f2f2007-12-07 13:20:34 +01001533}
1534
Thomas Renninger620e1122010-10-01 10:54:00 +02001535const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001536{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001537 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001538
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001539 if (list_empty(&device->pnp.ids))
1540 return dummy_hid;
1541
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001542 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1543 return hid->id;
1544}
1545EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001546
Toshi Kanid4e1a692013-03-04 21:30:41 +00001547static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001548{
1549 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001550
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001551 id = kmalloc(sizeof(*id), GFP_KERNEL);
1552 if (!id)
1553 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001554
Thomas Meyer581de592011-08-06 11:32:56 +02001555 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001556 if (!id->id) {
1557 kfree(id);
1558 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001559 }
1560
Toshi Kanid4e1a692013-03-04 21:30:41 +00001561 list_add_tail(&id->list, &pnp->ids);
1562 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001563}
1564
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001565/*
1566 * Old IBM workstations have a DSDT bug wherein the SMBus object
1567 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1568 * prefix. Work around this.
1569 */
Jiang Liuebf4df82013-06-29 00:24:41 +08001570static bool acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001571{
Jiang Liuebf4df82013-06-29 00:24:41 +08001572 char node_name[ACPI_PATH_SEGMENT_LENGTH];
1573 struct acpi_buffer path = { sizeof(node_name), node_name };
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001574
1575 if (!dmi_name_in_vendors("IBM"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001576 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001577
1578 /* Look for SMBS object */
Jiang Liuebf4df82013-06-29 00:24:41 +08001579 if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1580 strcmp("SMBS", path.pointer))
1581 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001582
1583 /* Does it have the necessary (but misnamed) methods? */
Jiang Liu952c63e2013-06-29 00:24:38 +08001584 if (acpi_has_method(handle, "SBI") &&
1585 acpi_has_method(handle, "SBR") &&
1586 acpi_has_method(handle, "SBW"))
Jiang Liuebf4df82013-06-29 00:24:41 +08001587 return true;
1588
1589 return false;
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001590}
1591
Toshi Kanic0af4172013-03-04 21:30:42 +00001592static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1593 int device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Len Brown4be44fc2005-08-05 00:44:28 -04001595 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001596 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001597 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001598 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Toshi Kanic0af4172013-03-04 21:30:42 +00001600 switch (device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 case ACPI_BUS_TYPE_DEVICE:
Toshi Kanic0af4172013-03-04 21:30:42 +00001602 if (handle == ACPI_ROOT_OBJECT) {
1603 acpi_add_id(pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001604 break;
1605 }
1606
Toshi Kanic0af4172013-03-04 21:30:42 +00001607 status = acpi_get_object_info(handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (ACPI_FAILURE(status)) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001609 pr_err(PREFIX "%s: Error reading device info\n",
1610 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return;
1612 }
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (info->valid & ACPI_VALID_HID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001615 acpi_add_id(pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001616 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001617 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001618 for (i = 0; i < cid_list->count; i++)
Toshi Kanic0af4172013-03-04 21:30:42 +00001619 acpi_add_id(pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (info->valid & ACPI_VALID_ADR) {
Toshi Kanic0af4172013-03-04 21:30:42 +00001622 pnp->bus_address = info->address;
1623 pnp->type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
Lv Zhengccf78042012-10-30 14:41:07 +01001625 if (info->valid & ACPI_VALID_UID)
Toshi Kanic0af4172013-03-04 21:30:42 +00001626 pnp->unique_id = kstrdup(info->unique_id.string,
Lv Zhengccf78042012-10-30 14:41:07 +01001627 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001628
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001629 kfree(info);
1630
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001631 /*
1632 * Some devices don't reliably have _HIDs & _CIDs, so add
1633 * synthetic HIDs to make sure drivers can find them.
1634 */
Toshi Kanic0af4172013-03-04 21:30:42 +00001635 if (acpi_is_video_device(handle))
1636 acpi_add_id(pnp, ACPI_VIDEO_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001637 else if (acpi_bay_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001638 acpi_add_id(pnp, ACPI_BAY_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001639 else if (acpi_dock_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001640 acpi_add_id(pnp, ACPI_DOCK_HID);
Jiang Liuebf4df82013-06-29 00:24:41 +08001641 else if (acpi_ibm_smbus_match(handle))
Toshi Kanic0af4172013-03-04 21:30:42 +00001642 acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1643 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1644 acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1645 strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1646 strcpy(pnp->device_class, ACPI_BUS_CLASS);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001647 }
Zhang Rui54735262007-01-11 02:09:09 -05001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 break;
1650 case ACPI_BUS_TYPE_POWER:
Toshi Kanic0af4172013-03-04 21:30:42 +00001651 acpi_add_id(pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 break;
1653 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanic0af4172013-03-04 21:30:42 +00001654 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanic0af4172013-03-04 21:30:42 +00001657 acpi_add_id(pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 break;
1659 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001660 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 break;
1662 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanic0af4172013-03-04 21:30:42 +00001663 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 break;
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
Toshi Kanic0af4172013-03-04 21:30:42 +00001668void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1669{
1670 struct acpi_hardware_id *id, *tmp;
1671
1672 list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1673 kfree(id->id);
1674 kfree(id);
1675 }
1676 kfree(pnp->unique_id);
1677}
1678
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001679void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1680 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001682 INIT_LIST_HEAD(&device->pnp.ids);
1683 device->device_type = type;
1684 device->handle = handle;
1685 device->parent = acpi_bus_get_parent(handle);
1686 STRUCT_TO_INT(device->status) = sta;
1687 acpi_device_get_busid(device);
Toshi Kanic0af4172013-03-04 21:30:42 +00001688 acpi_set_pnp_ids(handle, &device->pnp, type);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001689 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001690 device->flags.match_driver = false;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001691 device->flags.initialized = true;
1692 device->flags.visited = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001693 device_initialize(&device->dev);
1694 dev_set_uevent_suppress(&device->dev, true);
1695}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001696
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001697void acpi_device_add_finalize(struct acpi_device *device)
1698{
1699 dev_set_uevent_suppress(&device->dev, false);
1700 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001703static int acpi_add_single_object(struct acpi_device **child,
1704 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001705 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001707 int result;
1708 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001709 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Burman Yan36bcbec2006-12-19 12:56:11 -08001711 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001713 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001714 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001717 acpi_init_device_object(device, handle, type, sta);
1718 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001719 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001721 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001722 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001723 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001724 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001727 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001728 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001729 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1730 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1731 dev_name(&device->dev), (char *) buffer.pointer,
1732 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1733 kfree(buffer.pointer);
1734 *child = device;
1735 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001736}
1737
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001738static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1739 unsigned long long *sta)
1740{
1741 acpi_status status;
1742 acpi_object_type acpi_type;
1743
1744 status = acpi_get_type(handle, &acpi_type);
1745 if (ACPI_FAILURE(status))
1746 return -ENODEV;
1747
1748 switch (acpi_type) {
1749 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1750 case ACPI_TYPE_DEVICE:
1751 *type = ACPI_BUS_TYPE_DEVICE;
1752 status = acpi_bus_get_status_handle(handle, sta);
1753 if (ACPI_FAILURE(status))
1754 return -ENODEV;
1755 break;
1756 case ACPI_TYPE_PROCESSOR:
1757 *type = ACPI_BUS_TYPE_PROCESSOR;
1758 status = acpi_bus_get_status_handle(handle, sta);
1759 if (ACPI_FAILURE(status))
1760 return -ENODEV;
1761 break;
1762 case ACPI_TYPE_THERMAL:
1763 *type = ACPI_BUS_TYPE_THERMAL;
1764 *sta = ACPI_STA_DEFAULT;
1765 break;
1766 case ACPI_TYPE_POWER:
1767 *type = ACPI_BUS_TYPE_POWER;
1768 *sta = ACPI_STA_DEFAULT;
1769 break;
1770 default:
1771 return -ENODEV;
1772 }
1773
1774 return 0;
1775}
1776
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001777bool acpi_device_is_present(struct acpi_device *adev)
1778{
1779 if (adev->status.present || adev->status.functional)
1780 return true;
1781
1782 adev->flags.initialized = false;
1783 return false;
1784}
1785
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001786static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1787 char *idstr,
1788 const struct acpi_device_id **matchid)
1789{
1790 const struct acpi_device_id *devid;
1791
1792 for (devid = handler->ids; devid->id[0]; devid++)
1793 if (!strcmp((char *)devid->id, idstr)) {
1794 if (matchid)
1795 *matchid = devid;
1796
1797 return true;
1798 }
1799
1800 return false;
1801}
1802
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001803static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1804 const struct acpi_device_id **matchid)
1805{
1806 struct acpi_scan_handler *handler;
1807
1808 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1809 if (acpi_scan_handler_matching(handler, idstr, matchid))
1810 return handler;
1811
1812 return NULL;
1813}
1814
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001815void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1816{
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001817 if (!!hotplug->enabled == !!val)
1818 return;
1819
1820 mutex_lock(&acpi_scan_lock);
1821
1822 hotplug->enabled = val;
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001823
1824 mutex_unlock(&acpi_scan_lock);
1825}
1826
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001827static void acpi_scan_init_hotplug(acpi_handle handle, int type)
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001828{
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001829 struct acpi_device_pnp pnp = {};
1830 struct acpi_hardware_id *hwid;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001831 struct acpi_scan_handler *handler;
1832
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001833 INIT_LIST_HEAD(&pnp.ids);
1834 acpi_set_pnp_ids(handle, &pnp, type);
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001835
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001836 if (!pnp.type.hardware_id)
Catalin Marinas7a26b532013-05-15 16:49:35 +00001837 goto out;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001838
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001839 /*
1840 * This relies on the fact that acpi_install_notify_handler() will not
1841 * install the same notify handler routine twice for the same handle.
1842 */
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001843 list_for_each_entry(hwid, &pnp.ids, list) {
1844 handler = acpi_scan_match_handler(hwid->id, NULL);
Rafael J. Wysocki3338db02013-11-22 21:55:20 +01001845 if (handler) {
Toshi Kani2cbb14f2013-03-05 22:33:31 +00001846 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1847 acpi_hotplug_notify_cb, handler);
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001848 break;
1849 }
1850 }
1851
Catalin Marinas7a26b532013-05-15 16:49:35 +00001852out:
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001853 acpi_free_pnp_ids(&pnp);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001854}
1855
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001856static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1857 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001859 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001860 int type;
1861 unsigned long long sta;
1862 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001863
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001864 acpi_bus_get_device(handle, &device);
1865 if (device)
1866 goto out;
1867
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001868 result = acpi_bus_type_and_status(handle, &type, &sta);
1869 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001870 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001872 if (type == ACPI_BUS_TYPE_POWER) {
1873 acpi_add_power_resource(handle);
1874 return AE_OK;
1875 }
1876
Toshi Kani6b772e8f2013-03-04 21:30:43 +00001877 acpi_scan_init_hotplug(handle, type);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001878
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001879 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001880 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001881 return AE_CTRL_DEPTH;
1882
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001883 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001884 if (!*return_value)
1885 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001886
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001887 return AE_OK;
1888}
1889
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001890static int acpi_scan_attach_handler(struct acpi_device *device)
1891{
1892 struct acpi_hardware_id *hwid;
1893 int ret = 0;
1894
1895 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001896 const struct acpi_device_id *devid;
1897 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001898
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001899 handler = acpi_scan_match_handler(hwid->id, &devid);
1900 if (handler) {
1901 ret = handler->attach(device, devid);
1902 if (ret > 0) {
1903 device->handler = handler;
1904 break;
1905 } else if (ret < 0) {
1906 break;
1907 }
1908 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001909 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001910 return ret;
1911}
1912
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001913static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1914 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001915{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001916 struct acpi_device *device;
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001917 unsigned long long sta;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001918 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001919
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001920 /*
1921 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1922 * namespace walks prematurely.
1923 */
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001924 if (acpi_bus_type_and_status(handle, &ret, &sta))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001925 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001926
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001927 if (acpi_bus_get_device(handle, &device))
1928 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001929
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001930 STRUCT_TO_INT(device->status) = sta;
1931 /* Skip devices that are not present. */
1932 if (!acpi_device_is_present(device))
1933 goto err;
1934
Rafael J. Wysocki3a391a32013-07-12 13:45:59 +02001935 if (device->handler)
1936 return AE_OK;
1937
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001938 if (!device->flags.initialized) {
1939 acpi_bus_update_power(device, NULL);
1940 device->flags.initialized = true;
1941 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001942 ret = acpi_scan_attach_handler(device);
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001943 if (ret < 0)
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001944 goto err;
Rafael J. Wysocki69310072013-11-07 01:41:01 +01001945
1946 device->flags.match_driver = true;
1947 if (ret > 0)
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001948 goto ok;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001949
1950 ret = device_attach(&device->dev);
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01001951 if (ret < 0)
1952 goto err;
1953
1954 ok:
1955 device->flags.visited = true;
1956 return AE_OK;
1957
1958 err:
1959 device->flags.visited = false;
1960 return AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001963/**
1964 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1965 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001966 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001967 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1968 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001969 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001970 * If no devices were found, -ENODEV is returned, but it does not mean that
1971 * there has been a real error. There just have been no suitable ACPI objects
1972 * in the table trunk from which the kernel could create a device and add an
1973 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001974 *
1975 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001976 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001977int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001978{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001980 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001981
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001982 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001984 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001985
Thomas Renningerd2f66502010-01-29 17:48:51 +01001986 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001987 error = -ENODEV;
1988 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001989 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001990 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01001991
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001992 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001993}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001994EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001996static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1997 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001999 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002001 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002002 struct acpi_scan_handler *dev_handler = device->handler;
2003
Rafael J. Wysockica589f92013-01-30 14:27:29 +01002004 if (dev_handler) {
2005 if (dev_handler->detach)
2006 dev_handler->detach(device);
2007
2008 device->handler = NULL;
2009 } else {
2010 device_release_driver(&device->dev);
2011 }
Rafael J. Wysocki202317a2013-11-22 21:54:37 +01002012 /*
2013 * Most likely, the device is going away, so put it into D3cold
2014 * before that.
2015 */
2016 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
2017 device->flags.initialized = false;
2018 device->flags.visited = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002020 return AE_OK;
2021}
2022
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002023/**
2024 * acpi_bus_trim - Remove ACPI device node and all of its descendants
2025 * @start: Root of the ACPI device nodes subtree to remove.
2026 *
2027 * Must be called under acpi_scan_lock.
2028 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01002029void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01002031 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01002032 * Execute acpi_bus_device_detach() as a post-order callback to detach
2033 * all ACPI drivers from the device nodes being removed.
2034 */
2035 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
2036 acpi_bus_device_detach, NULL, NULL);
2037 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
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}