blob: f9c698d766f13eddc2e8f67675b2ae191ab3eedd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040022extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020025#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUS_DEVICE_NAME "System Bus"
27
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
Thomas Renninger620e1122010-10-01 10:54:00 +020030static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2d2006-12-08 17:23:43 +080033static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010034static DEFINE_MUTEX(acpi_scan_lock);
Rafael J. Wysockica589f92013-01-30 14:27:29 +010035static LIST_HEAD(acpi_scan_handlers_list);
Shaohua Li90905892009-04-07 10:24:29 +080036DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037LIST_HEAD(acpi_wakeup_device_list);
38
Zhang Ruie49bd2d2006-12-08 17:23:43 +080039struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080040 char bus_id[15];
Zhang Ruie49bd2d2006-12-08 17:23:43 +080041 unsigned int instance_no;
42 struct list_head node;
43};
Thomas Renninger29b71a12007-07-23 14:43:51 +020044
Rafael J. Wysocki3757b942013-02-13 14:36:47 +010045void acpi_scan_lock_acquire(void)
46{
47 mutex_lock(&acpi_scan_lock);
48}
49EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
50
51void acpi_scan_lock_release(void)
52{
53 mutex_unlock(&acpi_scan_lock);
54}
55EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
56
Rafael J. Wysockica589f92013-01-30 14:27:29 +010057int acpi_scan_add_handler(struct acpi_scan_handler *handler)
58{
59 if (!handler || !handler->attach)
60 return -EINVAL;
61
62 list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
63 return 0;
64}
65
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +010066int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
67 const char *hotplug_profile_name)
68{
69 int error;
70
71 error = acpi_scan_add_handler(handler);
72 if (error)
73 return error;
74
75 acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
76 return 0;
77}
78
Thomas Renninger29b71a12007-07-23 14:43:51 +020079/*
80 * Creates hid/cid(s) string needed for modalias and uevent
81 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
82 * char *modalias: "acpi:IBM0001:ACPI0001"
83*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020084static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
85 int size)
86{
Thomas Renninger29b71a12007-07-23 14:43:51 +020087 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080088 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060089 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020090
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020091 if (list_empty(&acpi_dev->pnp.ids))
92 return 0;
93
Zhang Rui5c9fcb52008-03-20 16:40:32 +080094 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020095 size -= len;
96
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060097 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
98 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080099 if (count < 0 || count >= size)
100 return -EINVAL;
101 len += count;
102 size -= count;
103 }
104
Thomas Renninger29b71a12007-07-23 14:43:51 +0200105 modalias[len] = '\0';
106 return len;
107}
108
109static ssize_t
110acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
111 struct acpi_device *acpi_dev = to_acpi_device(dev);
112 int len;
113
114 /* Device has no HID and no CID or string is >1024 */
115 len = create_modalias(acpi_dev, buf, 1024);
116 if (len <= 0)
117 return 0;
118 buf[len++] = '\n';
119 return len;
120}
121static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
122
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100123static int acpi_scan_hot_remove(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Yinghai Lu5993c462013-01-11 22:40:41 +0000125 acpi_handle handle = device->handle;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100126 acpi_handle not_used;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct acpi_object_list arg_list;
128 union acpi_object arg;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100129 acpi_status status;
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100130
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100131 /* If there is no handle, the device node has been unregistered. */
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100132 if (!handle) {
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100133 dev_dbg(&device->dev, "ACPI handle missing\n");
134 put_device(&device->dev);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100135 return -EINVAL;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100136 }
137
Zhang Rui26d46862008-04-29 02:35:48 -0400138 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100139 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400140
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100141 acpi_bus_trim(device);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100142 /* Device node has been unregistered. */
143 put_device(&device->dev);
Toshi Kanib3c450c2012-10-26 13:38:57 +0200144 device = NULL;
145
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100146 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &not_used))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 arg_list.count = 1;
148 arg_list.pointer = &arg;
149 arg.type = ACPI_TYPE_INTEGER;
150 arg.integer.value = 0;
151 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
152 }
153
154 arg_list.count = 1;
155 arg_list.pointer = &arg;
156 arg.type = ACPI_TYPE_INTEGER;
157 arg.integer.value = 1;
158
159 /*
160 * TBD: _EJD support.
161 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600163 if (ACPI_FAILURE(status)) {
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100164 if (status == AE_NOT_FOUND) {
165 return -ENODEV;
166 } else {
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100167 acpi_handle_warn(handle, "Eject failed\n");
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100168 return -EIO;
169 }
170 }
171 return 0;
172}
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100173
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100174static void acpi_bus_device_eject(void *context)
175{
176 acpi_handle handle = context;
177 struct acpi_device *device = NULL;
178 struct acpi_scan_handler *handler;
179 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
180
181 mutex_lock(&acpi_scan_lock);
182
183 acpi_bus_get_device(handle, &device);
184 if (!device)
185 goto err_out;
186
187 handler = device->handler;
188 if (!handler || !handler->hotplug.enabled) {
189 ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
190 goto err_out;
191 }
192 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
193 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
194 if (handler->hotplug.mode == AHM_CONTAINER) {
195 device->flags.eject_pending = true;
196 kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
197 } else {
198 int error;
199
200 get_device(&device->dev);
201 error = acpi_scan_hot_remove(device);
202 if (error)
203 goto err_out;
Toshi Kanic4753e52012-05-23 20:25:20 -0600204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100206 out:
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100207 mutex_unlock(&acpi_scan_lock);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800208 return;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100209
210 err_out:
211 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST, ost_code,
212 NULL);
213 goto out;
214}
215
216static void acpi_scan_bus_device_check(acpi_handle handle, u32 ost_source)
217{
218 struct acpi_device *device = NULL;
219 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
220 int error;
221
222 mutex_lock(&acpi_scan_lock);
223
224 acpi_bus_get_device(handle, &device);
225 if (device) {
226 dev_warn(&device->dev, "Attempt to re-insert\n");
227 goto out;
228 }
229 acpi_evaluate_hotplug_ost(handle, ost_source,
230 ACPI_OST_SC_INSERT_IN_PROGRESS, NULL);
231 error = acpi_bus_scan(handle);
232 if (error) {
233 acpi_handle_warn(handle, "Namespace scan failure\n");
234 goto out;
235 }
236 error = acpi_bus_get_device(handle, &device);
237 if (error) {
238 acpi_handle_warn(handle, "Missing device node object\n");
239 goto out;
240 }
241 ost_code = ACPI_OST_SC_SUCCESS;
242 if (device->handler && device->handler->hotplug.mode == AHM_CONTAINER)
243 kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
244
245 out:
246 acpi_evaluate_hotplug_ost(handle, ost_source, ost_code, NULL);
247 mutex_unlock(&acpi_scan_lock);
248}
249
250static void acpi_scan_bus_check(void *context)
251{
252 acpi_scan_bus_device_check((acpi_handle)context,
253 ACPI_NOTIFY_BUS_CHECK);
254}
255
256static void acpi_scan_device_check(void *context)
257{
258 acpi_scan_bus_device_check((acpi_handle)context,
259 ACPI_NOTIFY_DEVICE_CHECK);
260}
261
262static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *not_used)
263{
264 acpi_osd_exec_callback callback;
265 acpi_status status;
266
267 switch (type) {
268 case ACPI_NOTIFY_BUS_CHECK:
269 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
270 callback = acpi_scan_bus_check;
271 break;
272 case ACPI_NOTIFY_DEVICE_CHECK:
273 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
274 callback = acpi_scan_device_check;
275 break;
276 case ACPI_NOTIFY_EJECT_REQUEST:
277 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
278 callback = acpi_bus_device_eject;
279 break;
280 default:
281 /* non-hotplug event; possibly handled by other handler */
282 return;
283 }
284 status = acpi_os_hotplug_execute(callback, handle);
285 if (ACPI_FAILURE(status))
286 acpi_evaluate_hotplug_ost(handle, type,
287 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
288 NULL);
289}
290
291/**
292 * acpi_bus_hot_remove_device: hot-remove a device and its children
293 * @context: struct acpi_eject_event pointer (freed in this func)
294 *
295 * Hot-remove a device and its children. This function frees up the
296 * memory space passed by arg context, so that the caller may call
297 * this function asynchronously through acpi_os_hotplug_execute().
298 */
299void acpi_bus_hot_remove_device(void *context)
300{
301 struct acpi_eject_event *ej_event = context;
302 struct acpi_device *device = ej_event->device;
303 acpi_handle handle = device->handle;
304 int error;
305
306 mutex_lock(&acpi_scan_lock);
307
308 error = acpi_scan_hot_remove(device);
309 if (error && handle)
310 acpi_evaluate_hotplug_ost(handle, ej_event->event,
311 ACPI_OST_SC_NON_SPECIFIC_FAILURE,
312 NULL);
313
314 mutex_unlock(&acpi_scan_lock);
315 kfree(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
Toshi Kani61622ac2012-11-01 14:42:12 +0000317EXPORT_SYMBOL(acpi_bus_hot_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100319static ssize_t real_power_state_show(struct device *dev,
320 struct device_attribute *attr, char *buf)
321{
322 struct acpi_device *adev = to_acpi_device(dev);
323 int state;
324 int ret;
325
326 ret = acpi_device_get_power(adev, &state);
327 if (ret)
328 return ret;
329
330 return sprintf(buf, "%s\n", acpi_power_state_string(state));
331}
332
333static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
334
335static ssize_t power_state_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
337{
338 struct acpi_device *adev = to_acpi_device(dev);
339
340 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
341}
342
343static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800346acpi_eject_store(struct device *d, struct device_attribute *attr,
347 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800349 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600350 struct acpi_eject_event *ej_event;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100351 acpi_object_type not_used;
352 acpi_status status;
353 u32 ost_source;
354 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100356 if (!count || buf[0] != '1')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100359 if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
360 && !acpi_device->driver)
361 return -ENODEV;
362
363 status = acpi_get_type(acpi_device->handle, &not_used);
364 if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
365 return -ENODEV;
366
367 mutex_lock(&acpi_scan_lock);
368
369 if (acpi_device->flags.eject_pending) {
370 /* ACPI eject notification event. */
371 ost_source = ACPI_NOTIFY_EJECT_REQUEST;
372 acpi_device->flags.eject_pending = 0;
373 } else {
374 /* Eject initiated by user space. */
375 ost_source = ACPI_OST_EC_OSPM_EJECT;
376 }
Toshi Kanic4753e52012-05-23 20:25:20 -0600377 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
378 if (!ej_event) {
379 ret = -ENOMEM;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100380 goto err_out;
Toshi Kanic4753e52012-05-23 20:25:20 -0600381 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100382 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
383 ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
Yinghai Lu5993c462013-01-11 22:40:41 +0000384 ej_event->device = acpi_device;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100385 ej_event->event = ost_source;
386 get_device(&acpi_device->dev);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100387 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
388 if (ACPI_FAILURE(status)) {
389 put_device(&acpi_device->dev);
390 kfree(ej_event);
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100391 ret = status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
392 goto err_out;
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100393 }
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100394 ret = count;
395
396 out:
397 mutex_unlock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return ret;
Rafael J. Wysockia33ec392013-03-03 23:05:29 +0100399
400 err_out:
401 acpi_evaluate_hotplug_ost(acpi_device->handle, ost_source,
402 ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
403 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800406static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800408static ssize_t
409acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
410 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600412 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800413}
414static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
415
Lv Zheng923d4a42012-10-30 14:43:26 +0100416static ssize_t acpi_device_uid_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
418{
419 struct acpi_device *acpi_dev = to_acpi_device(dev);
420
421 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
422}
423static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
424
425static ssize_t acpi_device_adr_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
428 struct acpi_device *acpi_dev = to_acpi_device(dev);
429
430 return sprintf(buf, "0x%08x\n",
431 (unsigned int)(acpi_dev->pnp.bus_address));
432}
433static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
434
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800435static ssize_t
436acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
437 struct acpi_device *acpi_dev = to_acpi_device(dev);
438 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
439 int result;
440
441 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600442 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800443 goto end;
444
445 result = sprintf(buf, "%s\n", (char*)path.pointer);
446 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600447end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800448 return result;
449}
450static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
451
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600452/* sysfs file that shows description text from the ACPI _STR method */
453static ssize_t description_show(struct device *dev,
454 struct device_attribute *attr,
455 char *buf) {
456 struct acpi_device *acpi_dev = to_acpi_device(dev);
457 int result;
458
459 if (acpi_dev->pnp.str_obj == NULL)
460 return 0;
461
462 /*
463 * The _STR object contains a Unicode identifier for a device.
464 * We need to convert to utf-8 so it can be displayed.
465 */
466 result = utf16s_to_utf8s(
467 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
468 acpi_dev->pnp.str_obj->buffer.length,
469 UTF16_LITTLE_ENDIAN, buf,
470 PAGE_SIZE);
471
472 buf[result++] = '\n';
473
474 return result;
475}
476static DEVICE_ATTR(description, 0444, description_show, NULL);
477
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100478static ssize_t
479acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
480 char *buf) {
481 struct acpi_device *acpi_dev = to_acpi_device(dev);
482
483 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
484}
485static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
486
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800487static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600489 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800490 acpi_status status;
491 acpi_handle temp;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100492 unsigned long long sun;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800493 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800495 /*
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800496 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800497 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600498 if (dev->handle) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800499 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600500 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800501 goto end;
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200504 if (!list_empty(&dev->pnp.ids)) {
505 result = device_create_file(&dev->dev, &dev_attr_hid);
506 if (result)
507 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200509 result = device_create_file(&dev->dev, &dev_attr_modalias);
510 if (result)
511 goto end;
512 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200513
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600514 /*
515 * If device has _STR, 'description' file is created
516 */
517 status = acpi_get_handle(dev->handle, "_STR", &temp);
518 if (ACPI_SUCCESS(status)) {
519 status = acpi_evaluate_object(dev->handle, "_STR",
520 NULL, &buffer);
521 if (ACPI_FAILURE(status))
522 buffer.pointer = NULL;
523 dev->pnp.str_obj = buffer.pointer;
524 result = device_create_file(&dev->dev, &dev_attr_description);
525 if (result)
526 goto end;
527 }
528
Toshi Kanid4e1a692013-03-04 21:30:41 +0000529 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100530 result = device_create_file(&dev->dev, &dev_attr_adr);
531 if (dev->pnp.unique_id)
532 result = device_create_file(&dev->dev, &dev_attr_uid);
533
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100534 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
535 if (ACPI_SUCCESS(status)) {
536 dev->pnp.sun = (unsigned long)sun;
537 result = device_create_file(&dev->dev, &dev_attr_sun);
538 if (result)
539 goto end;
540 } else {
541 dev->pnp.sun = (unsigned long)-1;
542 }
543
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800544 /*
545 * If device has _EJ0, 'eject' file is created that is used to trigger
546 * hot-removal function from userland.
547 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800548 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100549 if (ACPI_SUCCESS(status)) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800550 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100551 if (result)
552 return result;
553 }
554
555 if (dev->flags.power_manageable) {
556 result = device_create_file(&dev->dev, &dev_attr_power_state);
557 if (result)
558 return result;
559
560 if (dev->power.flags.power_resources)
561 result = device_create_file(&dev->dev,
562 &dev_attr_real_power_state);
563 }
564
Alex Chiang0c526d92009-05-14 08:31:37 -0600565end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800566 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800567}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800569static void acpi_device_remove_files(struct acpi_device *dev)
570{
571 acpi_status status;
572 acpi_handle temp;
573
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100574 if (dev->flags.power_manageable) {
575 device_remove_file(&dev->dev, &dev_attr_power_state);
576 if (dev->power.flags.power_resources)
577 device_remove_file(&dev->dev,
578 &dev_attr_real_power_state);
579 }
580
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800581 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600582 * If device has _STR, remove 'description' file
583 */
584 status = acpi_get_handle(dev->handle, "_STR", &temp);
585 if (ACPI_SUCCESS(status)) {
586 kfree(dev->pnp.str_obj);
587 device_remove_file(&dev->dev, &dev_attr_description);
588 }
589 /*
590 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800591 */
592 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
593 if (ACPI_SUCCESS(status))
594 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800595
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100596 status = acpi_get_handle(dev->handle, "_SUN", &temp);
597 if (ACPI_SUCCESS(status))
598 device_remove_file(&dev->dev, &dev_attr_sun);
599
Lv Zheng923d4a42012-10-30 14:43:26 +0100600 if (dev->pnp.unique_id)
601 device_remove_file(&dev->dev, &dev_attr_uid);
Toshi Kanid4e1a692013-03-04 21:30:41 +0000602 if (dev->pnp.type.bus_address)
Lv Zheng923d4a42012-10-30 14:43:26 +0100603 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600604 device_remove_file(&dev->dev, &dev_attr_modalias);
605 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600606 if (dev->handle)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800607 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800608}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800610 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200612
Mika Westerbergcf761af2012-10-31 22:44:41 +0100613static const struct acpi_device_id *__acpi_match_device(
614 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200615{
616 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600617 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200618
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800619 /*
620 * If the device is not present, it is unnecessary to load device
621 * driver for it.
622 */
623 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100624 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800625
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600626 for (id = ids; id->id[0]; id++)
627 list_for_each_entry(hwid, &device->pnp.ids, list)
628 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100629 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200630
Mika Westerbergcf761af2012-10-31 22:44:41 +0100631 return NULL;
632}
633
634/**
635 * acpi_match_device - Match a struct device against a given list of ACPI IDs
636 * @ids: Array of struct acpi_device_id object to match against.
637 * @dev: The device structure to match.
638 *
639 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
640 * object for that handle and use that object to match against a given list of
641 * device IDs.
642 *
643 * Return a pointer to the first matching ID on success or %NULL on failure.
644 */
645const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
646 const struct device *dev)
647{
648 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100649 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100650
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100651 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100652 return NULL;
653
654 return __acpi_match_device(adev, ids);
655}
656EXPORT_SYMBOL_GPL(acpi_match_device);
657
658int acpi_match_device_ids(struct acpi_device *device,
659 const struct acpi_device_id *ids)
660{
661 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200662}
663EXPORT_SYMBOL(acpi_match_device_ids);
664
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100665void acpi_free_ids(struct acpi_device *device)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600666{
667 struct acpi_hardware_id *id, *tmp;
668
669 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
670 kfree(id->id);
671 kfree(id);
672 }
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100673 kfree(device->pnp.unique_id);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600674}
675
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100676static void acpi_free_power_resources_lists(struct acpi_device *device)
677{
678 int i;
679
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100680 if (device->wakeup.flags.valid)
681 acpi_power_resources_list_free(&device->wakeup.resources);
682
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100683 if (!device->flags.power_manageable)
684 return;
685
686 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
687 struct acpi_device_power_state *ps = &device->power.states[i];
688 acpi_power_resources_list_free(&ps->resources);
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Patrick Mochel1890a972006-12-07 20:56:31 +0800692static void acpi_device_release(struct device *dev)
693{
694 struct acpi_device *acpi_dev = to_acpi_device(dev);
695
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600696 acpi_free_ids(acpi_dev);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100697 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800698 kfree(acpi_dev);
699}
700
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800701static int acpi_bus_match(struct device *dev, struct device_driver *drv)
702{
703 struct acpi_device *acpi_dev = to_acpi_device(dev);
704 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
705
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100706 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d4102012-12-21 00:36:39 +0100707 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800708}
709
Kay Sievers7eff2e72007-08-14 15:15:12 +0200710static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800711{
712 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200713 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800714
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200715 if (list_empty(&acpi_dev->pnp.ids))
716 return 0;
717
Kay Sievers7eff2e72007-08-14 15:15:12 +0200718 if (add_uevent_var(env, "MODALIAS="))
719 return -ENOMEM;
720 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
721 sizeof(env->buf) - env->buflen);
722 if (len >= (sizeof(env->buf) - env->buflen))
723 return -ENOMEM;
724 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726}
727
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000728static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
729{
730 struct acpi_device *device = data;
731
732 device->driver->ops.notify(device, event);
733}
734
735static acpi_status acpi_device_notify_fixed(void *data)
736{
737 struct acpi_device *device = data;
738
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000739 /* Fixed hardware devices have no handles */
740 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000741 return AE_OK;
742}
743
744static int acpi_device_install_notify_handler(struct acpi_device *device)
745{
746 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000747
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000748 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000749 status =
750 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
751 acpi_device_notify_fixed,
752 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000753 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000754 status =
755 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
756 acpi_device_notify_fixed,
757 device);
758 else
759 status = acpi_install_notify_handler(device->handle,
760 ACPI_DEVICE_NOTIFY,
761 acpi_device_notify,
762 device);
763
764 if (ACPI_FAILURE(status))
765 return -EINVAL;
766 return 0;
767}
768
769static void acpi_device_remove_notify_handler(struct acpi_device *device)
770{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000771 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000772 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
773 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000774 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000775 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
776 acpi_device_notify_fixed);
777 else
778 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
779 acpi_device_notify);
780}
781
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800782static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800783static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800784{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800785 struct acpi_device *acpi_dev = to_acpi_device(dev);
786 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
787 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800789 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
790 if (!ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000791 if (acpi_drv->ops.notify) {
792 ret = acpi_device_install_notify_handler(acpi_dev);
793 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000794 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100795 acpi_drv->ops.remove(acpi_dev);
Toshi Kani5f27ee82013-02-07 21:19:13 +0000796 acpi_dev->driver = NULL;
797 acpi_dev->driver_data = NULL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000798 return ret;
799 }
800 }
801
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800802 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
803 "Found driver [%s] for device [%s]\n",
804 acpi_drv->name, acpi_dev->pnp.bus_id));
805 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800806 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800807 return ret;
808}
809
810static int acpi_device_remove(struct device * dev)
811{
812 struct acpi_device *acpi_dev = to_acpi_device(dev);
813 struct acpi_driver *acpi_drv = acpi_dev->driver;
814
815 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000816 if (acpi_drv->ops.notify)
817 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800818 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100819 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800820 }
821 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700822 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800823
824 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800825 return 0;
826}
827
David Brownell55955aa2007-05-08 00:28:35 -0700828struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800829 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800830 .match = acpi_bus_match,
831 .probe = acpi_device_probe,
832 .remove = acpi_device_remove,
833 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834};
835
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100836int acpi_device_add(struct acpi_device *device,
837 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800839 int result;
840 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
841 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000842
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100843 if (device->handle) {
844 acpi_status status;
845
846 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
847 device);
848 if (ACPI_FAILURE(status)) {
849 acpi_handle_err(device->handle,
850 "Unable to attach device data\n");
851 return -ENODEV;
852 }
853 }
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /*
856 * Linkage
857 * -------
858 * Link this device to its parent and siblings.
859 */
860 INIT_LIST_HEAD(&device->children);
861 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800863 INIT_LIST_HEAD(&device->physical_node_list);
864 mutex_init(&device->physical_node_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100865 INIT_LIST_HEAD(&device->power_dependent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800867 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
868 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100869 pr_err(PREFIX "Memory allocation error\n");
870 result = -ENOMEM;
871 goto err_detach;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800872 }
873
Shaohua Li90905892009-04-07 10:24:29 +0800874 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800875 /*
876 * Find suitable bus_id and instance number in acpi_bus_id_list
877 * If failed, create one and link it into acpi_bus_id_list
878 */
879 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600880 if (!strcmp(acpi_device_bus_id->bus_id,
881 acpi_device_hid(device))) {
882 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800883 found = 1;
884 kfree(new_bus_id);
885 break;
886 }
887 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600888 if (!found) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800889 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600890 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800891 acpi_device_bus_id->instance_no = 0;
892 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
893 }
Kay Sievers07944692008-10-30 01:18:59 +0100894 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800895
Len Brown33b57152008-12-15 22:09:26 -0500896 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (device->wakeup.flags.valid)
900 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800901 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Patrick Mochel1890a972006-12-07 20:56:31 +0800903 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000904 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800905 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100906 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100907 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600908 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600909 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100910 goto err;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800911 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800912
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800913 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600914 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100915 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
916 dev_name(&device->dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800917
Li Shaohua96333572006-12-07 20:56:46 +0800918 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800919 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100920
921 err:
Shaohua Li90905892009-04-07 10:24:29 +0800922 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500923 if (device->parent)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800924 list_del(&device->node);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800925 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800926 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100927
928 err_detach:
929 acpi_detach_data(device->handle, acpi_bus_data_handler);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800930 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Rafael J. Wysockib17b5372013-01-15 13:23:44 +0100933static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Shaohua Li90905892009-04-07 10:24:29 +0800935 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500936 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800940 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800943
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100944 acpi_power_add_remove_device(device, false);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800945 acpi_device_remove_files(device);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100946 if (device->remove)
947 device->remove(device);
948
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100949 device_del(&device->dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100950 /*
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +0100951 * Transition the device to D3cold to drop the reference counts of all
952 * power resources the device depends on and turn off the ones that have
953 * no more references.
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100954 */
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +0100955 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100956 device->handle = NULL;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100957 put_device(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
Zhang Rui9e89dde2006-12-07 20:56:16 +0800960/* --------------------------------------------------------------------------
961 Driver Management
962 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500964 * acpi_bus_driver_init - add a device to a driver
965 * @device: the device to add and initialize
966 * @driver: driver for the device
967 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600968 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800969 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
971static int
Len Brown4be44fc2005-08-05 00:44:28 -0400972acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Len Brown4be44fc2005-08-05 00:44:28 -0400974 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400977 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400980 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 result = driver->ops.add(device);
983 if (result) {
984 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700985 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400986 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988
989 device->driver = driver;
990
991 /*
992 * TBD - Configuration Management: Assign resources to device based
993 * upon possible configuration and currently allocated resources.
994 */
995
Len Brown4be44fc2005-08-05 00:44:28 -0400996 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
997 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400998 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700999}
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001002 * acpi_bus_register_driver - register a driver with the ACPI bus
1003 * @driver: driver being registered
1004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -05001006 * devices that match the driver's criteria and binds. Returns zero for
1007 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
Len Brown4be44fc2005-08-05 00:44:28 -04001009int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Patrick Mochel1890a972006-12-07 20:56:31 +08001011 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001014 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +08001015 driver->drv.name = driver->name;
1016 driver->drv.bus = &acpi_bus_type;
1017 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Patrick Mochel1890a972006-12-07 20:56:31 +08001019 ret = driver_register(&driver->drv);
1020 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Len Brown4be44fc2005-08-05 00:44:28 -04001023EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -05001026 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
1027 * @driver: driver to unregister
1028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * Unregisters a driver with the ACPI bus. Searches the namespace for all
1030 * devices that match the driver's criteria and unbinds.
1031 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -04001032void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Patrick Mochel1890a972006-12-07 20:56:31 +08001034 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
Len Brown4be44fc2005-08-05 00:44:28 -04001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037EXPORT_SYMBOL(acpi_bus_unregister_driver);
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039/* --------------------------------------------------------------------------
1040 Device Enumeration
1041 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001042static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1043{
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001044 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001045 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001046
1047 /*
1048 * Fixed hardware devices do not appear in the namespace and do not
1049 * have handles, but we fabricate acpi_devices for them, so we have
1050 * to deal with them specially.
1051 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001052 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001053 return acpi_root;
1054
1055 do {
1056 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001057 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +01001058 return status == AE_NULL_ENTRY ? NULL : acpi_root;
1059 } while (acpi_bus_get_device(handle, &device));
1060 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001061}
1062
Len Brownc8f7a622006-07-09 17:22:28 -04001063acpi_status
1064acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1065{
1066 acpi_status status;
1067 acpi_handle tmp;
1068 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1069 union acpi_object *obj;
1070
1071 status = acpi_get_handle(handle, "_EJD", &tmp);
1072 if (ACPI_FAILURE(status))
1073 return status;
1074
1075 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1076 if (ACPI_SUCCESS(status)) {
1077 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +01001078 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1079 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -04001080 kfree(buffer.pointer);
1081 }
1082 return status;
1083}
1084EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1085
Bob Moore8e4319c2009-06-29 13:43:27 +08001086void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
1088
1089 /* TBD */
1090
1091 return;
1092}
1093
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001094static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1095 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001097 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1098 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001100 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001101 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001103 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001104 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001106 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001108 /* _PRW */
1109 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1110 if (ACPI_FAILURE(status)) {
1111 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001112 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001113 }
1114
1115 package = (union acpi_object *)buffer.pointer;
1116
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001117 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001118 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001121 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001122 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (element->type == ACPI_TYPE_PACKAGE) {
1125 if ((element->package.count < 2) ||
1126 (element->package.elements[0].type !=
1127 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001128 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001129 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001130
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001131 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001133 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 (u32) element->package.elements[1].integer.value;
1135 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001136 wakeup->gpe_device = NULL;
1137 wakeup->gpe_number = element->integer.value;
1138 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001139 goto out;
1140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001143 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001144 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001145
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001146 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001148 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1149 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001150 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001152 if (!list_empty(&wakeup->resources)) {
1153 int sleep_state;
1154
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001155 err = acpi_power_wakeup_list_init(&wakeup->resources,
1156 &sleep_state);
1157 if (err) {
1158 acpi_handle_warn(handle, "Retrieving current states "
1159 "of wakeup power resources failed\n");
1160 acpi_power_resources_list_free(&wakeup->resources);
1161 goto out;
1162 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001163 if (sleep_state < wakeup->sleep_state) {
1164 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1165 "(S%d) by S%d from power resources\n",
1166 (int)wakeup->sleep_state, sleep_state);
1167 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Lin Mingbba63a22010-12-13 13:39:17 +08001170 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001171
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001172 out:
1173 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001174 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001177static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001179 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001180 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001181 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001182 {"PNP0C0E", 0},
1183 {"", 0},
1184 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001185 acpi_status status;
1186 acpi_event_status event_status;
1187
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001188 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001189
1190 /* Power button, Lid switch always enable wakeup */
1191 if (!acpi_match_device_ids(device, button_device_ids)) {
1192 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001193 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1194 /* Do not use Lid/sleep button for S5 wakeup */
1195 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1196 device->wakeup.sleep_state = ACPI_STATE_S4;
1197 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001198 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001199 return;
1200 }
1201
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001202 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1203 device->wakeup.gpe_number,
1204 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001205 if (status == AE_OK)
1206 device->wakeup.flags.run_wake =
1207 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1208}
1209
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001210static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001211{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001212 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001213 acpi_status status = 0;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001214 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001215
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001216 /* Presence of _PRW indicates wake capable */
1217 status = acpi_get_handle(device->handle, "_PRW", &temp);
1218 if (ACPI_FAILURE(status))
1219 return;
1220
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001221 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1222 &device->wakeup);
1223 if (err) {
1224 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001225 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001229 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001230 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001231 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1232 * system for the ACPI device with the _PRW object.
1233 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1234 * So it is necessary to call _DSW object first. Only when it is not
1235 * present will the _PSW object used.
1236 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001237 err = acpi_device_sleep_wake(device, 0, 0, 0);
1238 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001239 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1240 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001243static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001245 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001246 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1247 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001248 acpi_handle handle;
1249 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001250
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001251 INIT_LIST_HEAD(&ps->resources);
1252
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001253 /* Evaluate "_PRx" to get referenced power resources */
1254 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1255 if (ACPI_SUCCESS(status)) {
1256 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001257
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001258 if (buffer.length && package
1259 && package->type == ACPI_TYPE_PACKAGE
1260 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001261 int err = acpi_extract_power_resources(package, 0,
1262 &ps->resources);
1263 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001264 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001265 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001266 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001267 }
1268
1269 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001270 pathname[2] = 'S';
1271 status = acpi_get_handle(device->handle, pathname, &handle);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001272 if (ACPI_SUCCESS(status))
1273 ps->flags.explicit_set = 1;
1274
1275 /*
1276 * State is valid if there are means to put the device into it.
1277 * D3hot is only valid if _PR3 present.
1278 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001279 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001280 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1281 ps->flags.valid = 1;
1282 ps->flags.os_accessible = 1;
1283 }
1284
1285 ps->power = -1; /* Unknown - driver assigned */
1286 ps->latency = -1; /* Unknown - driver assigned */
1287}
1288
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001289static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001291 acpi_status status;
1292 acpi_handle handle;
1293 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001295 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1296 status = acpi_get_handle(device->handle, "_PS0", &handle);
1297 if (ACPI_FAILURE(status)) {
1298 status = acpi_get_handle(device->handle, "_PR0", &handle);
1299 if (ACPI_FAILURE(status))
1300 return;
1301 }
1302
1303 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001306 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
Zhang Rui9e89dde2006-12-07 20:56:16 +08001308 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001310 device->power.flags.explicit_get = 1;
1311 status = acpi_get_handle(device->handle, "_IRC", &handle);
1312 if (ACPI_SUCCESS(status))
1313 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001316 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001318 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1319 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001321 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Zhang Rui9e89dde2006-12-07 20:56:16 +08001323 /* Set defaults for D0 and D3 states (always valid) */
1324 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1325 device->power.states[ACPI_STATE_D0].power = 100;
1326 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1327 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001329 /* Set D3cold's explicit_set flag if _PS3 exists. */
1330 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1331 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1332
Aaron Lu1399dfc2012-11-21 23:33:40 +01001333 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1334 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1335 device->power.flags.power_resources)
1336 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1337
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001338 if (acpi_bus_init_power(device)) {
1339 acpi_free_power_resources_lists(device);
1340 device->flags.power_manageable = 0;
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001344static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Len Brown4be44fc2005-08-05 00:44:28 -04001346 acpi_status status = AE_OK;
1347 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 /* Presence of _STA indicates 'dynamic_status' */
1350 status = acpi_get_handle(device->handle, "_STA", &temp);
1351 if (ACPI_SUCCESS(status))
1352 device->flags.dynamic_status = 1;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /* Presence of _RMV indicates 'removable' */
1355 status = acpi_get_handle(device->handle, "_RMV", &temp);
1356 if (ACPI_SUCCESS(status))
1357 device->flags.removable = 1;
1358
1359 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1360 status = acpi_get_handle(device->handle, "_EJD", &temp);
1361 if (ACPI_SUCCESS(status))
1362 device->flags.ejectable = 1;
1363 else {
1364 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1365 if (ACPI_SUCCESS(status))
1366 device->flags.ejectable = 1;
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001370static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Len Brown4be44fc2005-08-05 00:44:28 -04001372 char bus_id[5] = { '?', 0 };
1373 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1374 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /*
1377 * Bus ID
1378 * ------
1379 * The device's Bus ID is simply the object name.
1380 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1381 */
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001382 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001384 return;
1385 }
1386
1387 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 case ACPI_BUS_TYPE_POWER_BUTTON:
1389 strcpy(device->pnp.bus_id, "PWRF");
1390 break;
1391 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1392 strcpy(device->pnp.bus_id, "SLPF");
1393 break;
1394 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001395 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 /* Clean up trailing underscores (if any) */
1397 for (i = 3; i > 1; i--) {
1398 if (bus_id[i] == '_')
1399 bus_id[i] = '\0';
1400 else
1401 break;
1402 }
1403 strcpy(device->pnp.bus_id, bus_id);
1404 break;
1405 }
1406}
1407
Zhang Rui54735262007-01-11 02:09:09 -05001408/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001409 * acpi_bay_match - see if an acpi object is an ejectable driver bay
Zhang Rui54735262007-01-11 02:09:09 -05001410 *
1411 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1412 * then we can safely call it an ejectable drive bay
1413 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001414static int acpi_bay_match(acpi_handle handle)
1415{
Zhang Rui54735262007-01-11 02:09:09 -05001416 acpi_status status;
Zhang Rui54735262007-01-11 02:09:09 -05001417 acpi_handle tmp;
1418 acpi_handle phandle;
1419
Zhang Rui54735262007-01-11 02:09:09 -05001420 status = acpi_get_handle(handle, "_EJ0", &tmp);
1421 if (ACPI_FAILURE(status))
1422 return -ENODEV;
1423
1424 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1425 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1426 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1427 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1428 return 0;
1429
1430 if (acpi_get_parent(handle, &phandle))
1431 return -ENODEV;
1432
1433 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1434 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1435 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1436 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1437 return 0;
1438
1439 return -ENODEV;
1440}
1441
Frank Seidel3620f2f2007-12-07 13:20:34 +01001442/*
Toshi Kanid4e1a692013-03-04 21:30:41 +00001443 * acpi_dock_match - see if an acpi object has a _DCK method
Frank Seidel3620f2f2007-12-07 13:20:34 +01001444 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001445static int acpi_dock_match(acpi_handle handle)
Frank Seidel3620f2f2007-12-07 13:20:34 +01001446{
1447 acpi_handle tmp;
Toshi Kanid4e1a692013-03-04 21:30:41 +00001448 return acpi_get_handle(handle, "_DCK", &tmp);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001449}
1450
Thomas Renninger620e1122010-10-01 10:54:00 +02001451const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001452{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001453 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001454
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001455 if (list_empty(&device->pnp.ids))
1456 return dummy_hid;
1457
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001458 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1459 return hid->id;
1460}
1461EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001462
Toshi Kanid4e1a692013-03-04 21:30:41 +00001463static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001464{
1465 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001466
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001467 id = kmalloc(sizeof(*id), GFP_KERNEL);
1468 if (!id)
1469 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001470
Thomas Meyer581de592011-08-06 11:32:56 +02001471 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001472 if (!id->id) {
1473 kfree(id);
1474 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001475 }
1476
Toshi Kanid4e1a692013-03-04 21:30:41 +00001477 list_add_tail(&id->list, &pnp->ids);
1478 pnp->type.hardware_id = 1;
Bob Moore15b8dd52009-06-29 13:39:29 +08001479}
1480
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001481/*
1482 * Old IBM workstations have a DSDT bug wherein the SMBus object
1483 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1484 * prefix. Work around this.
1485 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001486static int acpi_ibm_smbus_match(acpi_handle handle)
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001487{
1488 acpi_handle h_dummy;
1489 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1490 int result;
1491
1492 if (!dmi_name_in_vendors("IBM"))
1493 return -ENODEV;
1494
1495 /* Look for SMBS object */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001496 result = acpi_get_name(handle, ACPI_SINGLE_NAME, &path);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001497 if (result)
1498 return result;
1499
1500 if (strcmp("SMBS", path.pointer)) {
1501 result = -ENODEV;
1502 goto out;
1503 }
1504
1505 /* Does it have the necessary (but misnamed) methods? */
1506 result = -ENODEV;
Toshi Kanid4e1a692013-03-04 21:30:41 +00001507 if (ACPI_SUCCESS(acpi_get_handle(handle, "SBI", &h_dummy)) &&
1508 ACPI_SUCCESS(acpi_get_handle(handle, "SBR", &h_dummy)) &&
1509 ACPI_SUCCESS(acpi_get_handle(handle, "SBW", &h_dummy)))
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001510 result = 0;
1511out:
1512 kfree(path.pointer);
1513 return result;
1514}
1515
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001516static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Len Brown4be44fc2005-08-05 00:44:28 -04001518 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001519 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001520 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001521 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001523 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001525 if (ACPI_IS_ROOT_DEVICE(device)) {
Toshi Kanid4e1a692013-03-04 21:30:41 +00001526 acpi_add_id(&device->pnp, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001527 break;
1528 }
1529
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001530 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001532 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return;
1534 }
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (info->valid & ACPI_VALID_HID)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001537 acpi_add_id(&device->pnp, info->hardware_id.string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001538 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001539 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001540 for (i = 0; i < cid_list->count; i++)
Toshi Kanid4e1a692013-03-04 21:30:41 +00001541 acpi_add_id(&device->pnp, cid_list->ids[i].string);
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (info->valid & ACPI_VALID_ADR) {
1544 device->pnp.bus_address = info->address;
Toshi Kanid4e1a692013-03-04 21:30:41 +00001545 device->pnp.type.bus_address = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Lv Zhengccf78042012-10-30 14:41:07 +01001547 if (info->valid & ACPI_VALID_UID)
1548 device->pnp.unique_id = kstrdup(info->unique_id.string,
1549 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001550
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001551 kfree(info);
1552
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001553 /*
1554 * Some devices don't reliably have _HIDs & _CIDs, so add
1555 * synthetic HIDs to make sure drivers can find them.
1556 */
Toshi Kanid4e1a692013-03-04 21:30:41 +00001557 if (acpi_is_video_device(device->handle))
1558 acpi_add_id(&device->pnp, ACPI_VIDEO_HID);
1559 else if (ACPI_SUCCESS(acpi_bay_match(device->handle)))
1560 acpi_add_id(&device->pnp, ACPI_BAY_HID);
1561 else if (ACPI_SUCCESS(acpi_dock_match(device->handle)))
1562 acpi_add_id(&device->pnp, ACPI_DOCK_HID);
1563 else if (!acpi_ibm_smbus_match(device->handle))
1564 acpi_add_id(&device->pnp, ACPI_SMBUS_IBM_HID);
Rafael J. Wysocki4f5f64c2013-01-04 23:00:54 +01001565 else if (list_empty(&device->pnp.ids) &&
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001566 ACPI_IS_ROOT_DEVICE(device->parent)) {
Toshi Kanid4e1a692013-03-04 21:30:41 +00001567 acpi_add_id(&device->pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001568 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1569 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1570 }
Zhang Rui54735262007-01-11 02:09:09 -05001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 break;
1573 case ACPI_BUS_TYPE_POWER:
Toshi Kanid4e1a692013-03-04 21:30:41 +00001574 acpi_add_id(&device->pnp, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 break;
1576 case ACPI_BUS_TYPE_PROCESSOR:
Toshi Kanid4e1a692013-03-04 21:30:41 +00001577 acpi_add_id(&device->pnp, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 case ACPI_BUS_TYPE_THERMAL:
Toshi Kanid4e1a692013-03-04 21:30:41 +00001580 acpi_add_id(&device->pnp, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 break;
1582 case ACPI_BUS_TYPE_POWER_BUTTON:
Toshi Kanid4e1a692013-03-04 21:30:41 +00001583 acpi_add_id(&device->pnp, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 break;
1585 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Toshi Kanid4e1a692013-03-04 21:30:41 +00001586 acpi_add_id(&device->pnp, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 break;
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001591void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1592 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001594 INIT_LIST_HEAD(&device->pnp.ids);
1595 device->device_type = type;
1596 device->handle = handle;
1597 device->parent = acpi_bus_get_parent(handle);
1598 STRUCT_TO_INT(device->status) = sta;
1599 acpi_device_get_busid(device);
1600 acpi_device_set_id(device);
1601 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001602 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001603 device_initialize(&device->dev);
1604 dev_set_uevent_suppress(&device->dev, true);
1605}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001606
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001607void acpi_device_add_finalize(struct acpi_device *device)
1608{
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001609 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001610 dev_set_uevent_suppress(&device->dev, false);
1611 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001614static int acpi_add_single_object(struct acpi_device **child,
1615 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001616 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001618 int result;
1619 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001620 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Burman Yan36bcbec2006-12-19 12:56:11 -08001622 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001624 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001625 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001628 acpi_init_device_object(device, handle, type, sta);
1629 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001630 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001632 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001633 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001634 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001635 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001638 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001639 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001640 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1641 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1642 dev_name(&device->dev), (char *) buffer.pointer,
1643 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1644 kfree(buffer.pointer);
1645 *child = device;
1646 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001647}
1648
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001649static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1650 unsigned long long *sta)
1651{
1652 acpi_status status;
1653 acpi_object_type acpi_type;
1654
1655 status = acpi_get_type(handle, &acpi_type);
1656 if (ACPI_FAILURE(status))
1657 return -ENODEV;
1658
1659 switch (acpi_type) {
1660 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1661 case ACPI_TYPE_DEVICE:
1662 *type = ACPI_BUS_TYPE_DEVICE;
1663 status = acpi_bus_get_status_handle(handle, sta);
1664 if (ACPI_FAILURE(status))
1665 return -ENODEV;
1666 break;
1667 case ACPI_TYPE_PROCESSOR:
1668 *type = ACPI_BUS_TYPE_PROCESSOR;
1669 status = acpi_bus_get_status_handle(handle, sta);
1670 if (ACPI_FAILURE(status))
1671 return -ENODEV;
1672 break;
1673 case ACPI_TYPE_THERMAL:
1674 *type = ACPI_BUS_TYPE_THERMAL;
1675 *sta = ACPI_STA_DEFAULT;
1676 break;
1677 case ACPI_TYPE_POWER:
1678 *type = ACPI_BUS_TYPE_POWER;
1679 *sta = ACPI_STA_DEFAULT;
1680 break;
1681 default:
1682 return -ENODEV;
1683 }
1684
1685 return 0;
1686}
1687
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001688static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1689 char *idstr,
1690 const struct acpi_device_id **matchid)
1691{
1692 const struct acpi_device_id *devid;
1693
1694 for (devid = handler->ids; devid->id[0]; devid++)
1695 if (!strcmp((char *)devid->id, idstr)) {
1696 if (matchid)
1697 *matchid = devid;
1698
1699 return true;
1700 }
1701
1702 return false;
1703}
1704
Rafael J. Wysocki3f8055c2013-03-03 23:08:16 +01001705static acpi_status acpi_scan_hotplug_modify(acpi_handle handle,
1706 u32 lvl_not_used, void *data,
1707 void **ret_not_used)
1708{
1709 struct acpi_scan_handler *handler = data;
1710 struct acpi_device_info *info;
1711 bool match = false;
1712
1713 if (ACPI_FAILURE(acpi_get_object_info(handle, &info)))
1714 return AE_OK;
1715
1716 if (info->valid & ACPI_VALID_HID) {
1717 char *idstr = info->hardware_id.string;
1718 match = acpi_scan_handler_matching(handler, idstr, NULL);
1719 }
1720 kfree(info);
1721 if (!match)
1722 return AE_OK;
1723
1724 if (handler->hotplug.enabled)
1725 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1726 acpi_hotplug_notify_cb, NULL);
1727 else
1728 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1729 acpi_hotplug_notify_cb);
1730
1731 return AE_OK;
1732}
1733
1734void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1735{
1736 struct acpi_scan_handler *handler;
1737
1738 if (!!hotplug->enabled == !!val)
1739 return;
1740
1741 mutex_lock(&acpi_scan_lock);
1742
1743 hotplug->enabled = val;
1744 handler = container_of(hotplug, struct acpi_scan_handler, hotplug);
1745 acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
1746 acpi_scan_hotplug_modify, NULL, handler, NULL);
1747
1748 mutex_unlock(&acpi_scan_lock);
1749}
1750
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001751static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1752 const struct acpi_device_id **matchid)
1753{
1754 struct acpi_scan_handler *handler;
1755
Rafael J. Wysocki4b59cc12013-03-03 23:06:21 +01001756 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1757 if (acpi_scan_handler_matching(handler, idstr, matchid))
1758 return handler;
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001759
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001760 return NULL;
1761}
1762
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001763static void acpi_scan_init_hotplug(acpi_handle handle)
1764{
1765 struct acpi_device_info *info;
1766 struct acpi_scan_handler *handler;
1767
1768 if (ACPI_FAILURE(acpi_get_object_info(handle, &info)))
1769 return;
1770
1771 if (!(info->valid & ACPI_VALID_HID)) {
1772 kfree(info);
1773 return;
1774 }
1775
1776 /*
1777 * This relies on the fact that acpi_install_notify_handler() will not
1778 * install the same notify handler routine twice for the same handle.
1779 */
1780 handler = acpi_scan_match_handler(info->hardware_id.string, NULL);
1781 kfree(info);
1782 if (handler && handler->hotplug.enabled)
1783 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1784 acpi_hotplug_notify_cb, NULL);
1785}
1786
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001787static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1788 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001790 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001791 int type;
1792 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001793 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001794 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001795
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001796 acpi_bus_get_device(handle, &device);
1797 if (device)
1798 goto out;
1799
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001800 result = acpi_bus_type_and_status(handle, &type, &sta);
1801 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001802 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001804 if (type == ACPI_BUS_TYPE_POWER) {
1805 acpi_add_power_resource(handle);
1806 return AE_OK;
1807 }
1808
Rafael J. Wysockia33ec392013-03-03 23:05:29 +01001809 acpi_scan_init_hotplug(handle);
1810
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001811 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001812 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001813 struct acpi_device_wakeup wakeup;
1814 acpi_handle temp;
1815
1816 status = acpi_get_handle(handle, "_PRW", &temp);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001817 if (ACPI_SUCCESS(status)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001818 acpi_bus_extract_wakeup_device_power_package(handle,
1819 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001820 acpi_power_resources_list_free(&wakeup.resources);
1821 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001822 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001825 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001826 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001827 return AE_CTRL_DEPTH;
1828
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001829 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001830 if (!*return_value)
1831 *return_value = device;
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001832
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001833 return AE_OK;
1834}
1835
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001836static int acpi_scan_attach_handler(struct acpi_device *device)
1837{
1838 struct acpi_hardware_id *hwid;
1839 int ret = 0;
1840
1841 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001842 const struct acpi_device_id *devid;
1843 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001844
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001845 handler = acpi_scan_match_handler(hwid->id, &devid);
1846 if (handler) {
1847 ret = handler->attach(device, devid);
1848 if (ret > 0) {
1849 device->handler = handler;
1850 break;
1851 } else if (ret < 0) {
1852 break;
1853 }
1854 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001855 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001856 return ret;
1857}
1858
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001859static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1860 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001861{
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001862 struct acpi_device *device;
1863 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001864 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001865
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001866 /*
1867 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1868 * namespace walks prematurely.
1869 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001870 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001871 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001872
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001873 if (acpi_bus_get_device(handle, &device))
1874 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001875
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001876 ret = acpi_scan_attach_handler(device);
1877 if (ret)
1878 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1879
1880 ret = device_attach(&device->dev);
1881 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001884/**
1885 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1886 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001887 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001888 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1889 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001890 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001891 * If no devices were found, -ENODEV is returned, but it does not mean that
1892 * there has been a real error. There just have been no suitable ACPI objects
1893 * in the table trunk from which the kernel could create a device and add an
1894 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001895 *
1896 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001897 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001898int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001899{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001901 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001902
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001903 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001905 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001906
Thomas Renningerd2f66502010-01-29 17:48:51 +01001907 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001908 error = -ENODEV;
1909 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d4102012-12-21 00:36:39 +01001910 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001911 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01001912
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001913 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001914}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001915EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001917static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1918 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001920 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001922 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001923 struct acpi_scan_handler *dev_handler = device->handler;
1924
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001925 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001926 if (dev_handler) {
1927 if (dev_handler->detach)
1928 dev_handler->detach(device);
1929
1930 device->handler = NULL;
1931 } else {
1932 device_release_driver(&device->dev);
1933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001935 return AE_OK;
1936}
1937
1938static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1939 void *not_used, void **ret_not_used)
1940{
1941 struct acpi_device *device = NULL;
1942
1943 if (!acpi_bus_get_device(handle, &device))
1944 acpi_device_unregister(device);
1945
1946 return AE_OK;
1947}
1948
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001949/**
1950 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1951 * @start: Root of the ACPI device nodes subtree to remove.
1952 *
1953 * Must be called under acpi_scan_lock.
1954 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01001955void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001957 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001958 * Execute acpi_bus_device_detach() as a post-order callback to detach
1959 * all ACPI drivers from the device nodes being removed.
1960 */
1961 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1962 acpi_bus_device_detach, NULL, NULL);
1963 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1964 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001965 * Execute acpi_bus_remove() as a post-order callback to remove device
1966 * nodes in the given namespace scope.
1967 */
1968 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1969 acpi_bus_remove, NULL, NULL);
1970 acpi_bus_remove(start->handle, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
Kristen Accardiceaba662006-02-23 17:56:01 -08001972EXPORT_SYMBOL_GPL(acpi_bus_trim);
1973
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001974static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Len Brown4be44fc2005-08-05 00:44:28 -04001976 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 /*
1979 * Enumerate all fixed-feature devices.
1980 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001981 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1982 struct acpi_device *device = NULL;
1983
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001984 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001985 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001986 ACPI_STA_DEFAULT);
1987 if (result)
1988 return result;
1989
1990 result = device_attach(&device->dev);
1991 if (result < 0)
1992 return result;
1993
Daniel Drakec10d7a12012-05-10 00:08:43 +01001994 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001997 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1998 struct acpi_device *device = NULL;
1999
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00002000 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08002001 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002002 ACPI_STA_DEFAULT);
2003 if (result)
2004 return result;
2005
2006 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07002007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01002009 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Bjorn Helgaase747f272009-03-24 16:49:43 -06002012int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Patrick Mochel5b327262006-05-10 10:33:00 -04002016 result = bus_register(&acpi_bus_type);
2017 if (result) {
2018 /* We don't want to quit even if we failed to add suspend/resume */
2019 printk(KERN_ERR PREFIX "Could not register bus type\n");
2020 }
2021
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01002022 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01002023 acpi_pci_link_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01002024 acpi_platform_init();
Mika Westerberg13176bb2013-01-17 09:59:33 +00002025 acpi_csrt_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01002026 acpi_container_init();
Jiang Liuab1a2e02013-01-19 00:07:42 +08002027 acpi_pci_slot_init();
Rafael J. Wysocki0a347642013-03-03 23:18:03 +01002028 acpi_memory_hotplug_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01002029
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002030 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 * Enumerate devices in the ACPI namespace.
2033 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002034 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002036 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01002038 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002040 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002042 result = acpi_bus_scan_fixed();
2043 if (result) {
2044 acpi_device_unregister(acpi_root);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002045 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01002046 }
2047
2048 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002049
Yinghai Lu668192b2013-01-21 13:20:48 -08002050 acpi_pci_root_hp_init();
2051
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01002052 out:
2053 mutex_unlock(&acpi_scan_lock);
2054 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}