blob: cc1b0020478b48d5e7c9241852cd3878ff06b338 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040022extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020025#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUS_DEVICE_NAME "System Bus"
27
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
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 Ruie49bd2dd2006-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 Ruie49bd2dd2006-12-08 17:23:43 +080039struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080040 char bus_id[15];
Zhang Ruie49bd2dd2006-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
Thomas Renninger29b71a12007-07-23 14:43:51 +020066/*
67 * Creates hid/cid(s) string needed for modalias and uevent
68 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
69 * char *modalias: "acpi:IBM0001:ACPI0001"
70*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020071static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
72 int size)
73{
Thomas Renninger29b71a12007-07-23 14:43:51 +020074 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080075 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060076 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020077
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020078 if (list_empty(&acpi_dev->pnp.ids))
79 return 0;
80
Zhang Rui5c9fcb52008-03-20 16:40:32 +080081 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020082 size -= len;
83
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060084 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
85 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080086 if (count < 0 || count >= size)
87 return -EINVAL;
88 len += count;
89 size -= count;
90 }
91
Thomas Renninger29b71a12007-07-23 14:43:51 +020092 modalias[len] = '\0';
93 return len;
94}
95
96static ssize_t
97acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
98 struct acpi_device *acpi_dev = to_acpi_device(dev);
99 int len;
100
101 /* Device has no HID and no CID or string is >1024 */
102 len = create_modalias(acpi_dev, buf, 1024);
103 if (len <= 0)
104 return 0;
105 buf[len++] = '\n';
106 return len;
107}
108static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
109
Toshi Kanic4753e52012-05-23 20:25:20 -0600110/**
111 * acpi_bus_hot_remove_device: hot-remove a device and its children
112 * @context: struct acpi_eject_event pointer (freed in this func)
113 *
114 * Hot-remove a device and its children. This function frees up the
115 * memory space passed by arg context, so that the caller may call
116 * this function asynchronously through acpi_os_hotplug_execute().
117 */
118void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100120 struct acpi_eject_event *ej_event = context;
Yinghai Lu5993c462013-01-11 22:40:41 +0000121 struct acpi_device *device = ej_event->device;
122 acpi_handle handle = device->handle;
Toshi Kanib3c450c2012-10-26 13:38:57 +0200123 acpi_handle temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 struct acpi_object_list arg_list;
125 union acpi_object arg;
126 acpi_status status = AE_OK;
Toshi Kanic4753e52012-05-23 20:25:20 -0600127 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100129 mutex_lock(&acpi_scan_lock);
130
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100131 /* If there is no handle, the device node has been unregistered. */
132 if (!device->handle) {
133 dev_dbg(&device->dev, "ACPI handle missing\n");
134 put_device(&device->dev);
135 goto out;
136 }
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
Toshi Kanib3c450c2012-10-26 13:38:57 +0200146 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
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)) {
164 if (status != AE_NOT_FOUND)
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100165 acpi_handle_warn(handle, "Eject failed\n");
166
167 /* Tell the firmware the hot-remove operation has failed. */
168 acpi_evaluate_hotplug_ost(handle, ej_event->event,
169 ost_code, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100172 out:
Rafael J. Wysockif058cdf2013-02-09 15:29:11 +0100173 mutex_unlock(&acpi_scan_lock);
Toshi Kanic4753e52012-05-23 20:25:20 -0600174 kfree(context);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800175 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
Toshi Kani61622ac2012-11-01 14:42:12 +0000177EXPORT_SYMBOL(acpi_bus_hot_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100179static ssize_t real_power_state_show(struct device *dev,
180 struct device_attribute *attr, char *buf)
181{
182 struct acpi_device *adev = to_acpi_device(dev);
183 int state;
184 int ret;
185
186 ret = acpi_device_get_power(adev, &state);
187 if (ret)
188 return ret;
189
190 return sprintf(buf, "%s\n", acpi_power_state_string(state));
191}
192
193static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
194
195static ssize_t power_state_show(struct device *dev,
196 struct device_attribute *attr, char *buf)
197{
198 struct acpi_device *adev = to_acpi_device(dev);
199
200 return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
201}
202
203static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800206acpi_eject_store(struct device *d, struct device_attribute *attr,
207 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Len Brown4be44fc2005-08-05 00:44:28 -0400209 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400210 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400211 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800212 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600213 struct acpi_eject_event *ej_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 if ((!count) || (buf[0] != '1')) {
216 return -EINVAL;
217 }
Toshi Kanice7685a2013-02-07 12:50:53 +0100218 if (!acpi_device->driver && !acpi_device->handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 ret = -ENODEV;
220 goto err;
221 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800222 status = acpi_get_type(acpi_device->handle, &type);
223 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 ret = -ENODEV;
225 goto err;
226 }
227
Toshi Kanic4753e52012-05-23 20:25:20 -0600228 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
229 if (!ej_event) {
230 ret = -ENOMEM;
231 goto err;
232 }
233
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100234 get_device(&acpi_device->dev);
Yinghai Lu5993c462013-01-11 22:40:41 +0000235 ej_event->device = acpi_device;
Toshi Kanic4753e52012-05-23 20:25:20 -0600236 if (acpi_device->flags.eject_pending) {
237 /* event originated from ACPI eject notification */
238 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
239 acpi_device->flags.eject_pending = 0;
240 } else {
241 /* event originated from user */
242 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
Yinghai Lu5993c462013-01-11 22:40:41 +0000243 (void) acpi_evaluate_hotplug_ost(acpi_device->handle,
Toshi Kanic4753e52012-05-23 20:25:20 -0600244 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
245 }
246
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100247 status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device, ej_event);
248 if (ACPI_FAILURE(status)) {
249 put_device(&acpi_device->dev);
250 kfree(ej_event);
251 }
Alok N Kataria74523c92008-06-13 12:54:24 -0400252err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800256static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800258static ssize_t
259acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
260 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600262 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800263}
264static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
265
Lv Zheng923d4a42012-10-30 14:43:26 +0100266static ssize_t acpi_device_uid_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
268{
269 struct acpi_device *acpi_dev = to_acpi_device(dev);
270
271 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
272}
273static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
274
275static ssize_t acpi_device_adr_show(struct device *dev,
276 struct device_attribute *attr, char *buf)
277{
278 struct acpi_device *acpi_dev = to_acpi_device(dev);
279
280 return sprintf(buf, "0x%08x\n",
281 (unsigned int)(acpi_dev->pnp.bus_address));
282}
283static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
284
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800285static ssize_t
286acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
287 struct acpi_device *acpi_dev = to_acpi_device(dev);
288 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
289 int result;
290
291 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600292 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800293 goto end;
294
295 result = sprintf(buf, "%s\n", (char*)path.pointer);
296 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600297end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800298 return result;
299}
300static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
301
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600302/* sysfs file that shows description text from the ACPI _STR method */
303static ssize_t description_show(struct device *dev,
304 struct device_attribute *attr,
305 char *buf) {
306 struct acpi_device *acpi_dev = to_acpi_device(dev);
307 int result;
308
309 if (acpi_dev->pnp.str_obj == NULL)
310 return 0;
311
312 /*
313 * The _STR object contains a Unicode identifier for a device.
314 * We need to convert to utf-8 so it can be displayed.
315 */
316 result = utf16s_to_utf8s(
317 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
318 acpi_dev->pnp.str_obj->buffer.length,
319 UTF16_LITTLE_ENDIAN, buf,
320 PAGE_SIZE);
321
322 buf[result++] = '\n';
323
324 return result;
325}
326static DEVICE_ATTR(description, 0444, description_show, NULL);
327
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100328static ssize_t
329acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
330 char *buf) {
331 struct acpi_device *acpi_dev = to_acpi_device(dev);
332
333 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
334}
335static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
336
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800337static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600339 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800340 acpi_status status;
341 acpi_handle temp;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100342 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800343 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800345 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800346 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800347 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600348 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800349 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600350 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800351 goto end;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200354 if (!list_empty(&dev->pnp.ids)) {
355 result = device_create_file(&dev->dev, &dev_attr_hid);
356 if (result)
357 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200359 result = device_create_file(&dev->dev, &dev_attr_modalias);
360 if (result)
361 goto end;
362 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200363
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600364 /*
365 * If device has _STR, 'description' file is created
366 */
367 status = acpi_get_handle(dev->handle, "_STR", &temp);
368 if (ACPI_SUCCESS(status)) {
369 status = acpi_evaluate_object(dev->handle, "_STR",
370 NULL, &buffer);
371 if (ACPI_FAILURE(status))
372 buffer.pointer = NULL;
373 dev->pnp.str_obj = buffer.pointer;
374 result = device_create_file(&dev->dev, &dev_attr_description);
375 if (result)
376 goto end;
377 }
378
Lv Zheng923d4a42012-10-30 14:43:26 +0100379 if (dev->flags.bus_address)
380 result = device_create_file(&dev->dev, &dev_attr_adr);
381 if (dev->pnp.unique_id)
382 result = device_create_file(&dev->dev, &dev_attr_uid);
383
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100384 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
385 if (ACPI_SUCCESS(status)) {
386 dev->pnp.sun = (unsigned long)sun;
387 result = device_create_file(&dev->dev, &dev_attr_sun);
388 if (result)
389 goto end;
390 } else {
391 dev->pnp.sun = (unsigned long)-1;
392 }
393
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800394 /*
395 * If device has _EJ0, 'eject' file is created that is used to trigger
396 * hot-removal function from userland.
397 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800398 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100399 if (ACPI_SUCCESS(status)) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800400 result = device_create_file(&dev->dev, &dev_attr_eject);
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100401 if (result)
402 return result;
403 }
404
405 if (dev->flags.power_manageable) {
406 result = device_create_file(&dev->dev, &dev_attr_power_state);
407 if (result)
408 return result;
409
410 if (dev->power.flags.power_resources)
411 result = device_create_file(&dev->dev,
412 &dev_attr_real_power_state);
413 }
414
Alex Chiang0c526d92009-05-14 08:31:37 -0600415end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800416 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800419static void acpi_device_remove_files(struct acpi_device *dev)
420{
421 acpi_status status;
422 acpi_handle temp;
423
Rafael J. Wysocki836aedb2013-01-24 12:49:59 +0100424 if (dev->flags.power_manageable) {
425 device_remove_file(&dev->dev, &dev_attr_power_state);
426 if (dev->power.flags.power_resources)
427 device_remove_file(&dev->dev,
428 &dev_attr_real_power_state);
429 }
430
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800431 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600432 * If device has _STR, remove 'description' file
433 */
434 status = acpi_get_handle(dev->handle, "_STR", &temp);
435 if (ACPI_SUCCESS(status)) {
436 kfree(dev->pnp.str_obj);
437 device_remove_file(&dev->dev, &dev_attr_description);
438 }
439 /*
440 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800441 */
442 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
443 if (ACPI_SUCCESS(status))
444 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800445
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100446 status = acpi_get_handle(dev->handle, "_SUN", &temp);
447 if (ACPI_SUCCESS(status))
448 device_remove_file(&dev->dev, &dev_attr_sun);
449
Lv Zheng923d4a42012-10-30 14:43:26 +0100450 if (dev->pnp.unique_id)
451 device_remove_file(&dev->dev, &dev_attr_uid);
452 if (dev->flags.bus_address)
453 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600454 device_remove_file(&dev->dev, &dev_attr_modalias);
455 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600456 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800457 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800458}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800460 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200462
Mika Westerbergcf761af2012-10-31 22:44:41 +0100463static const struct acpi_device_id *__acpi_match_device(
464 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200465{
466 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600467 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200468
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800469 /*
470 * If the device is not present, it is unnecessary to load device
471 * driver for it.
472 */
473 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100474 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800475
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600476 for (id = ids; id->id[0]; id++)
477 list_for_each_entry(hwid, &device->pnp.ids, list)
478 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100479 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200480
Mika Westerbergcf761af2012-10-31 22:44:41 +0100481 return NULL;
482}
483
484/**
485 * acpi_match_device - Match a struct device against a given list of ACPI IDs
486 * @ids: Array of struct acpi_device_id object to match against.
487 * @dev: The device structure to match.
488 *
489 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
490 * object for that handle and use that object to match against a given list of
491 * device IDs.
492 *
493 * Return a pointer to the first matching ID on success or %NULL on failure.
494 */
495const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
496 const struct device *dev)
497{
498 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100499 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100500
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100501 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100502 return NULL;
503
504 return __acpi_match_device(adev, ids);
505}
506EXPORT_SYMBOL_GPL(acpi_match_device);
507
508int acpi_match_device_ids(struct acpi_device *device,
509 const struct acpi_device_id *ids)
510{
511 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200512}
513EXPORT_SYMBOL(acpi_match_device_ids);
514
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100515void acpi_free_ids(struct acpi_device *device)
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600516{
517 struct acpi_hardware_id *id, *tmp;
518
519 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
520 kfree(id->id);
521 kfree(id);
522 }
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100523 kfree(device->pnp.unique_id);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600524}
525
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100526static void acpi_free_power_resources_lists(struct acpi_device *device)
527{
528 int i;
529
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100530 if (device->wakeup.flags.valid)
531 acpi_power_resources_list_free(&device->wakeup.resources);
532
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100533 if (!device->flags.power_manageable)
534 return;
535
536 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
537 struct acpi_device_power_state *ps = &device->power.states[i];
538 acpi_power_resources_list_free(&ps->resources);
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Patrick Mochel1890a972006-12-07 20:56:31 +0800542static void acpi_device_release(struct device *dev)
543{
544 struct acpi_device *acpi_dev = to_acpi_device(dev);
545
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600546 acpi_free_ids(acpi_dev);
Rafael J. Wysocki0b224522013-01-17 14:11:06 +0100547 acpi_free_power_resources_lists(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800548 kfree(acpi_dev);
549}
550
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800551static int acpi_bus_match(struct device *dev, struct device_driver *drv)
552{
553 struct acpi_device *acpi_dev = to_acpi_device(dev);
554 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
555
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100556 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100557 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800558}
559
Kay Sievers7eff2e72007-08-14 15:15:12 +0200560static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800561{
562 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200563 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800564
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200565 if (list_empty(&acpi_dev->pnp.ids))
566 return 0;
567
Kay Sievers7eff2e72007-08-14 15:15:12 +0200568 if (add_uevent_var(env, "MODALIAS="))
569 return -ENOMEM;
570 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
571 sizeof(env->buf) - env->buflen);
572 if (len >= (sizeof(env->buf) - env->buflen))
573 return -ENOMEM;
574 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return 0;
576}
577
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000578static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
579{
580 struct acpi_device *device = data;
581
582 device->driver->ops.notify(device, event);
583}
584
585static acpi_status acpi_device_notify_fixed(void *data)
586{
587 struct acpi_device *device = data;
588
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000589 /* Fixed hardware devices have no handles */
590 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000591 return AE_OK;
592}
593
594static int acpi_device_install_notify_handler(struct acpi_device *device)
595{
596 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000597
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000598 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000599 status =
600 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
601 acpi_device_notify_fixed,
602 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000603 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000604 status =
605 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
606 acpi_device_notify_fixed,
607 device);
608 else
609 status = acpi_install_notify_handler(device->handle,
610 ACPI_DEVICE_NOTIFY,
611 acpi_device_notify,
612 device);
613
614 if (ACPI_FAILURE(status))
615 return -EINVAL;
616 return 0;
617}
618
619static void acpi_device_remove_notify_handler(struct acpi_device *device)
620{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000621 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000622 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
623 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000624 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000625 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
626 acpi_device_notify_fixed);
627 else
628 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
629 acpi_device_notify);
630}
631
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800632static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800633static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800634{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800635 struct acpi_device *acpi_dev = to_acpi_device(dev);
636 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
637 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800639 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
640 if (!ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000641 if (acpi_drv->ops.notify) {
642 ret = acpi_device_install_notify_handler(acpi_dev);
643 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000644 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100645 acpi_drv->ops.remove(acpi_dev);
Toshi Kani5f27ee82013-02-07 21:19:13 +0000646 acpi_dev->driver = NULL;
647 acpi_dev->driver_data = NULL;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000648 return ret;
649 }
650 }
651
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800652 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
653 "Found driver [%s] for device [%s]\n",
654 acpi_drv->name, acpi_dev->pnp.bus_id));
655 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800656 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800657 return ret;
658}
659
660static int acpi_device_remove(struct device * dev)
661{
662 struct acpi_device *acpi_dev = to_acpi_device(dev);
663 struct acpi_driver *acpi_drv = acpi_dev->driver;
664
665 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000666 if (acpi_drv->ops.notify)
667 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800668 if (acpi_drv->ops.remove)
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100669 acpi_drv->ops.remove(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800670 }
671 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700672 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800673
674 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800675 return 0;
676}
677
David Brownell55955aa2007-05-08 00:28:35 -0700678struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800679 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800680 .match = acpi_bus_match,
681 .probe = acpi_device_probe,
682 .remove = acpi_device_remove,
683 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684};
685
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100686int acpi_device_add(struct acpi_device *device,
687 void (*release)(struct device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800689 int result;
690 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
691 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000692
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100693 if (device->handle) {
694 acpi_status status;
695
696 status = acpi_attach_data(device->handle, acpi_bus_data_handler,
697 device);
698 if (ACPI_FAILURE(status)) {
699 acpi_handle_err(device->handle,
700 "Unable to attach device data\n");
701 return -ENODEV;
702 }
703 }
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 /*
706 * Linkage
707 * -------
708 * Link this device to its parent and siblings.
709 */
710 INIT_LIST_HEAD(&device->children);
711 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800713 INIT_LIST_HEAD(&device->physical_node_list);
714 mutex_init(&device->physical_node_lock);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100715 INIT_LIST_HEAD(&device->power_dependent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800717 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
718 if (!new_bus_id) {
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100719 pr_err(PREFIX "Memory allocation error\n");
720 result = -ENOMEM;
721 goto err_detach;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800722 }
723
Shaohua Li90905892009-04-07 10:24:29 +0800724 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800725 /*
726 * Find suitable bus_id and instance number in acpi_bus_id_list
727 * If failed, create one and link it into acpi_bus_id_list
728 */
729 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600730 if (!strcmp(acpi_device_bus_id->bus_id,
731 acpi_device_hid(device))) {
732 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800733 found = 1;
734 kfree(new_bus_id);
735 break;
736 }
737 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600738 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800739 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600740 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800741 acpi_device_bus_id->instance_no = 0;
742 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
743 }
Kay Sievers07944692008-10-30 01:18:59 +0100744 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800745
Len Brown33b57152008-12-15 22:09:26 -0500746 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (device->wakeup.flags.valid)
750 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800751 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Patrick Mochel1890a972006-12-07 20:56:31 +0800753 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000754 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800755 device->dev.bus = &acpi_bus_type;
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +0100756 device->dev.release = release;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100757 result = device_add(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600758 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600759 dev_err(&device->dev, "Error registering device\n");
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100760 goto err;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800761 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800762
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800763 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600764 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100765 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
766 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800767
Li Shaohua96333572006-12-07 20:56:46 +0800768 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800769 return 0;
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100770
771 err:
Shaohua Li90905892009-04-07 10:24:29 +0800772 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500773 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800774 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800775 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800776 mutex_unlock(&acpi_device_lock);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +0100777
778 err_detach:
779 acpi_detach_data(device->handle, acpi_bus_data_handler);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800780 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Rafael J. Wysockib17b5372013-01-15 13:23:44 +0100783static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Shaohua Li90905892009-04-07 10:24:29 +0800785 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500786 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800790 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800793
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100794 acpi_power_add_remove_device(device, false);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800795 acpi_device_remove_files(device);
Rafael J. Wysockib1c0f992013-01-24 12:50:09 +0100796 if (device->remove)
797 device->remove(device);
798
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100799 device_del(&device->dev);
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100800 /*
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +0100801 * Transition the device to D3cold to drop the reference counts of all
802 * power resources the device depends on and turn off the ones that have
803 * no more references.
Rafael J. Wysockibc9b6402013-01-17 14:11:05 +0100804 */
Rafael J. Wysocki0aa120a2013-02-09 15:29:20 +0100805 acpi_device_set_power(device, ACPI_STATE_D3_COLD);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100806 device->handle = NULL;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +0100807 put_device(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Zhang Rui9e89dde2006-12-07 20:56:16 +0800810/* --------------------------------------------------------------------------
811 Driver Management
812 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500814 * acpi_bus_driver_init - add a device to a driver
815 * @device: the device to add and initialize
816 * @driver: driver for the device
817 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600818 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800819 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
821static int
Len Brown4be44fc2005-08-05 00:44:28 -0400822acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Len Brown4be44fc2005-08-05 00:44:28 -0400824 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400827 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400830 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 result = driver->ops.add(device);
833 if (result) {
834 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700835 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400836 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
839 device->driver = driver;
840
841 /*
842 * TBD - Configuration Management: Assign resources to device based
843 * upon possible configuration and currently allocated resources.
844 */
845
Len Brown4be44fc2005-08-05 00:44:28 -0400846 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
847 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400848 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500852 * acpi_bus_register_driver - register a driver with the ACPI bus
853 * @driver: driver being registered
854 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500856 * devices that match the driver's criteria and binds. Returns zero for
857 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 */
Len Brown4be44fc2005-08-05 00:44:28 -0400859int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Patrick Mochel1890a972006-12-07 20:56:31 +0800861 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400864 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800865 driver->drv.name = driver->name;
866 driver->drv.bus = &acpi_bus_type;
867 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Patrick Mochel1890a972006-12-07 20:56:31 +0800869 ret = driver_register(&driver->drv);
870 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Len Brown4be44fc2005-08-05 00:44:28 -0400873EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500876 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
877 * @driver: driver to unregister
878 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 * Unregisters a driver with the ACPI bus. Searches the namespace for all
880 * devices that match the driver's criteria and unbinds.
881 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400882void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Patrick Mochel1890a972006-12-07 20:56:31 +0800884 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
Len Brown4be44fc2005-08-05 00:44:28 -0400886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887EXPORT_SYMBOL(acpi_bus_unregister_driver);
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889/* --------------------------------------------------------------------------
890 Device Enumeration
891 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000892static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
893{
Rafael J. Wysocki456de892013-01-31 20:57:40 +0100894 struct acpi_device *device = NULL;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000895 acpi_status status;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000896
897 /*
898 * Fixed hardware devices do not appear in the namespace and do not
899 * have handles, but we fabricate acpi_devices for them, so we have
900 * to deal with them specially.
901 */
Rafael J. Wysocki456de892013-01-31 20:57:40 +0100902 if (!handle)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000903 return acpi_root;
904
905 do {
906 status = acpi_get_parent(handle, &handle);
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000907 if (ACPI_FAILURE(status))
Rafael J. Wysocki456de892013-01-31 20:57:40 +0100908 return status == AE_NULL_ENTRY ? NULL : acpi_root;
909 } while (acpi_bus_get_device(handle, &device));
910 return device;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000911}
912
Len Brownc8f7a62c2006-07-09 17:22:28 -0400913acpi_status
914acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
915{
916 acpi_status status;
917 acpi_handle tmp;
918 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
919 union acpi_object *obj;
920
921 status = acpi_get_handle(handle, "_EJD", &tmp);
922 if (ACPI_FAILURE(status))
923 return status;
924
925 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
926 if (ACPI_SUCCESS(status)) {
927 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100928 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
929 ejd);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400930 kfree(buffer.pointer);
931 }
932 return status;
933}
934EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
935
Bob Moore8e4319c2009-06-29 13:43:27 +0800936void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938
939 /* TBD */
940
941 return;
942}
943
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100944static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
945 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100947 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
948 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100950 acpi_status status;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100951 int err = -ENODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100953 if (!wakeup)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100954 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +0100956 INIT_LIST_HEAD(&wakeup->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100958 /* _PRW */
959 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
960 if (ACPI_FAILURE(status)) {
961 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100962 return err;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100963 }
964
965 package = (union acpi_object *)buffer.pointer;
966
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100967 if (!package || package->package.count < 2)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100968 goto out;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 element = &(package->package.elements[0]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100971 if (!element)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100972 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (element->type == ACPI_TYPE_PACKAGE) {
975 if ((element->package.count < 2) ||
976 (element->package.elements[0].type !=
977 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100978 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100979 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100980
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100981 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100983 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 (u32) element->package.elements[1].integer.value;
985 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100986 wakeup->gpe_device = NULL;
987 wakeup->gpe_number = element->integer.value;
988 } else {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100989 goto out;
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 element = &(package->package.elements[1]);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100993 if (element->type != ACPI_TYPE_INTEGER)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100994 goto out;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100995
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100996 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +0100998 err = acpi_extract_power_resources(package, 2, &wakeup->resources);
999 if (err)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001000 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001002 if (!list_empty(&wakeup->resources)) {
1003 int sleep_state;
1004
Rafael J. Wysockib5d667e2013-02-23 23:15:21 +01001005 err = acpi_power_wakeup_list_init(&wakeup->resources,
1006 &sleep_state);
1007 if (err) {
1008 acpi_handle_warn(handle, "Retrieving current states "
1009 "of wakeup power resources failed\n");
1010 acpi_power_resources_list_free(&wakeup->resources);
1011 goto out;
1012 }
Rafael J. Wysocki0596a522013-01-17 14:11:07 +01001013 if (sleep_state < wakeup->sleep_state) {
1014 acpi_handle_warn(handle, "Overriding _PRW sleep state "
1015 "(S%d) by S%d from power resources\n",
1016 (int)wakeup->sleep_state, sleep_state);
1017 wakeup->sleep_state = sleep_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Lin Mingbba63a22010-12-13 13:39:17 +08001020 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +02001021
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001022 out:
1023 kfree(buffer.pointer);
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001024 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001027static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Thomas Renninger29b71a12007-07-23 14:43:51 +02001029 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +02001030 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +01001031 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +02001032 {"PNP0C0E", 0},
1033 {"", 0},
1034 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001035 acpi_status status;
1036 acpi_event_status event_status;
1037
Rafael J. Wysockib67ea762010-02-17 23:44:09 +01001038 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001039
1040 /* Power button, Lid switch always enable wakeup */
1041 if (!acpi_match_device_ids(device, button_device_ids)) {
1042 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +01001043 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1044 /* Do not use Lid/sleep button for S5 wakeup */
1045 if (device->wakeup.sleep_state == ACPI_STATE_S5)
1046 device->wakeup.sleep_state = ACPI_STATE_S4;
1047 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +01001048 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001049 return;
1050 }
1051
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +02001052 status = acpi_get_gpe_status(device->wakeup.gpe_device,
1053 device->wakeup.gpe_number,
1054 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001055 if (status == AE_OK)
1056 device->wakeup.flags.run_wake =
1057 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1058}
1059
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001060static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001061{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001062 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001063 acpi_status status = 0;
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001064 int err;
Thomas Renninger29b71a12007-07-23 14:43:51 +02001065
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001066 /* Presence of _PRW indicates wake capable */
1067 status = acpi_get_handle(device->handle, "_PRW", &temp);
1068 if (ACPI_FAILURE(status))
1069 return;
1070
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001071 err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1072 &device->wakeup);
1073 if (err) {
1074 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001075 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +02001079 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +01001080 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +08001081 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1082 * system for the ACPI device with the _PRW object.
1083 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1084 * So it is necessary to call _DSW object first. Only when it is not
1085 * present will the _PSW object used.
1086 */
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001087 err = acpi_device_sleep_wake(device, 0, 0, 0);
1088 if (err)
Rafael J. Wysocki77e76602008-07-07 03:33:34 +02001089 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1090 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001093static void acpi_bus_init_power_state(struct acpi_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001095 struct acpi_device_power_state *ps = &device->power.states[state];
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001096 char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1097 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001098 acpi_handle handle;
1099 acpi_status status;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001100
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001101 INIT_LIST_HEAD(&ps->resources);
1102
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001103 /* Evaluate "_PRx" to get referenced power resources */
1104 status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1105 if (ACPI_SUCCESS(status)) {
1106 union acpi_object *package = buffer.pointer;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001107
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001108 if (buffer.length && package
1109 && package->type == ACPI_TYPE_PACKAGE
1110 && package->package.count) {
Rafael J. Wysockie88c9c62013-01-17 14:11:07 +01001111 int err = acpi_extract_power_resources(package, 0,
1112 &ps->resources);
1113 if (!err)
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001114 device->power.flags.power_resources = 1;
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001115 }
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001116 ACPI_FREE(buffer.pointer);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001117 }
1118
1119 /* Evaluate "_PSx" to see if we can do explicit sets */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001120 pathname[2] = 'S';
1121 status = acpi_get_handle(device->handle, pathname, &handle);
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001122 if (ACPI_SUCCESS(status))
1123 ps->flags.explicit_set = 1;
1124
1125 /*
1126 * State is valid if there are means to put the device into it.
1127 * D3hot is only valid if _PR3 present.
1128 */
Rafael J. Wysockief85bdb2013-01-17 14:11:07 +01001129 if (!list_empty(&ps->resources)
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001130 || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1131 ps->flags.valid = 1;
1132 ps->flags.os_accessible = 1;
1133 }
1134
1135 ps->power = -1; /* Unknown - driver assigned */
1136 ps->latency = -1; /* Unknown - driver assigned */
1137}
1138
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001139static void acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Rafael J. Wysocki8bc50532013-01-17 14:11:07 +01001141 acpi_status status;
1142 acpi_handle handle;
1143 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001145 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1146 status = acpi_get_handle(device->handle, "_PS0", &handle);
1147 if (ACPI_FAILURE(status)) {
1148 status = acpi_get_handle(device->handle, "_PR0", &handle);
1149 if (ACPI_FAILURE(status))
1150 return;
1151 }
1152
1153 device->flags.power_manageable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001156 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 */
Zhang Rui9e89dde2006-12-07 20:56:16 +08001158 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001160 device->power.flags.explicit_get = 1;
1161 status = acpi_get_handle(device->handle, "_IRC", &handle);
1162 if (ACPI_SUCCESS(status))
1163 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001166 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 */
Rafael J. Wysockif33ce562013-01-17 14:11:06 +01001168 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1169 acpi_bus_init_power_state(device, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Rafael J. Wysocki0b224522013-01-17 14:11:06 +01001171 INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Zhang Rui9e89dde2006-12-07 20:56:16 +08001173 /* Set defaults for D0 and D3 states (always valid) */
1174 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1175 device->power.states[ACPI_STATE_D0].power = 100;
1176 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1177 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001179 /* Set D3cold's explicit_set flag if _PS3 exists. */
1180 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1181 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1182
Aaron Lu1399dfc2012-11-21 23:33:40 +01001183 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1184 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1185 device->power.flags.power_resources)
1186 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1187
Rafael J. Wysockib3785492013-02-01 23:43:02 +01001188 if (acpi_bus_init_power(device)) {
1189 acpi_free_power_resources_lists(device);
1190 device->flags.power_manageable = 0;
1191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001194static void acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Len Brown4be44fc2005-08-05 00:44:28 -04001196 acpi_status status = AE_OK;
1197 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /* Presence of _STA indicates 'dynamic_status' */
1200 status = acpi_get_handle(device->handle, "_STA", &temp);
1201 if (ACPI_SUCCESS(status))
1202 device->flags.dynamic_status = 1;
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 /* Presence of _RMV indicates 'removable' */
1205 status = acpi_get_handle(device->handle, "_RMV", &temp);
1206 if (ACPI_SUCCESS(status))
1207 device->flags.removable = 1;
1208
1209 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1210 status = acpi_get_handle(device->handle, "_EJD", &temp);
1211 if (ACPI_SUCCESS(status))
1212 device->flags.ejectable = 1;
1213 else {
1214 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1215 if (ACPI_SUCCESS(status))
1216 device->flags.ejectable = 1;
1217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001220static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Len Brown4be44fc2005-08-05 00:44:28 -04001222 char bus_id[5] = { '?', 0 };
1223 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1224 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /*
1227 * Bus ID
1228 * ------
1229 * The device's Bus ID is simply the object name.
1230 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1231 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001232 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001234 return;
1235 }
1236
1237 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 case ACPI_BUS_TYPE_POWER_BUTTON:
1239 strcpy(device->pnp.bus_id, "PWRF");
1240 break;
1241 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1242 strcpy(device->pnp.bus_id, "SLPF");
1243 break;
1244 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001245 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /* Clean up trailing underscores (if any) */
1247 for (i = 3; i > 1; i--) {
1248 if (bus_id[i] == '_')
1249 bus_id[i] = '\0';
1250 else
1251 break;
1252 }
1253 strcpy(device->pnp.bus_id, bus_id);
1254 break;
1255 }
1256}
1257
Zhang Rui54735262007-01-11 02:09:09 -05001258/*
1259 * acpi_bay_match - see if a device is an ejectable driver bay
1260 *
1261 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1262 * then we can safely call it an ejectable drive bay
1263 */
1264static int acpi_bay_match(struct acpi_device *device){
1265 acpi_status status;
1266 acpi_handle handle;
1267 acpi_handle tmp;
1268 acpi_handle phandle;
1269
1270 handle = device->handle;
1271
1272 status = acpi_get_handle(handle, "_EJ0", &tmp);
1273 if (ACPI_FAILURE(status))
1274 return -ENODEV;
1275
1276 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1277 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1278 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1279 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1280 return 0;
1281
1282 if (acpi_get_parent(handle, &phandle))
1283 return -ENODEV;
1284
1285 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1286 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1287 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1288 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1289 return 0;
1290
1291 return -ENODEV;
1292}
1293
Frank Seidel3620f2f2007-12-07 13:20:34 +01001294/*
1295 * acpi_dock_match - see if a device has a _DCK method
1296 */
1297static int acpi_dock_match(struct acpi_device *device)
1298{
1299 acpi_handle tmp;
1300 return acpi_get_handle(device->handle, "_DCK", &tmp);
1301}
1302
Thomas Renninger620e1122010-10-01 10:54:00 +02001303const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001304{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001305 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001306
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001307 if (list_empty(&device->pnp.ids))
1308 return dummy_hid;
1309
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001310 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1311 return hid->id;
1312}
1313EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001314
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001315static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1316{
1317 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001318
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001319 id = kmalloc(sizeof(*id), GFP_KERNEL);
1320 if (!id)
1321 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001322
Thomas Meyer581de592011-08-06 11:32:56 +02001323 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001324 if (!id->id) {
1325 kfree(id);
1326 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001327 }
1328
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001329 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001330}
1331
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001332/*
1333 * Old IBM workstations have a DSDT bug wherein the SMBus object
1334 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1335 * prefix. Work around this.
1336 */
1337static int acpi_ibm_smbus_match(struct acpi_device *device)
1338{
1339 acpi_handle h_dummy;
1340 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1341 int result;
1342
1343 if (!dmi_name_in_vendors("IBM"))
1344 return -ENODEV;
1345
1346 /* Look for SMBS object */
1347 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1348 if (result)
1349 return result;
1350
1351 if (strcmp("SMBS", path.pointer)) {
1352 result = -ENODEV;
1353 goto out;
1354 }
1355
1356 /* Does it have the necessary (but misnamed) methods? */
1357 result = -ENODEV;
1358 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1359 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1360 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1361 result = 0;
1362out:
1363 kfree(path.pointer);
1364 return result;
1365}
1366
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001367static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Len Brown4be44fc2005-08-05 00:44:28 -04001369 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001370 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001371 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001372 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001374 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001376 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001377 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001378 break;
1379 }
1380
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001381 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001383 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return;
1385 }
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001388 acpi_add_id(device, info->hardware_id.string);
1389 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001390 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001391 for (i = 0; i < cid_list->count; i++)
1392 acpi_add_id(device, cid_list->ids[i].string);
1393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (info->valid & ACPI_VALID_ADR) {
1395 device->pnp.bus_address = info->address;
1396 device->flags.bus_address = 1;
1397 }
Lv Zhengccf78042012-10-30 14:41:07 +01001398 if (info->valid & ACPI_VALID_UID)
1399 device->pnp.unique_id = kstrdup(info->unique_id.string,
1400 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001401
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001402 kfree(info);
1403
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001404 /*
1405 * Some devices don't reliably have _HIDs & _CIDs, so add
1406 * synthetic HIDs to make sure drivers can find them.
1407 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001408 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001409 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001410 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001411 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001412 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001413 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001414 else if (!acpi_ibm_smbus_match(device))
1415 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Rafael J. Wysocki4f5f64c2013-01-04 23:00:54 +01001416 else if (list_empty(&device->pnp.ids) &&
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001417 ACPI_IS_ROOT_DEVICE(device->parent)) {
1418 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1419 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1420 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1421 }
Zhang Rui54735262007-01-11 02:09:09 -05001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 break;
1424 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001425 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001428 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001431 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 break;
1433 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001434 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 break;
1436 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001437 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 break;
1439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001442void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1443 int type, unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001445 INIT_LIST_HEAD(&device->pnp.ids);
1446 device->device_type = type;
1447 device->handle = handle;
1448 device->parent = acpi_bus_get_parent(handle);
1449 STRUCT_TO_INT(device->status) = sta;
1450 acpi_device_get_busid(device);
1451 acpi_device_set_id(device);
1452 acpi_bus_get_flags(device);
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001453 device->flags.match_driver = false;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001454 device_initialize(&device->dev);
1455 dev_set_uevent_suppress(&device->dev, true);
1456}
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001457
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001458void acpi_device_add_finalize(struct acpi_device *device)
1459{
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001460 device->flags.match_driver = true;
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001461 dev_set_uevent_suppress(&device->dev, false);
1462 kobject_uevent(&device->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001465static int acpi_add_single_object(struct acpi_device **child,
1466 acpi_handle handle, int type,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001467 unsigned long long sta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001469 int result;
1470 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001471 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Burman Yan36bcbec2006-12-19 12:56:11 -08001473 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001475 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001476 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001479 acpi_init_device_object(device, handle, type, sta);
1480 acpi_bus_get_power_flags(device);
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001481 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001483 result = acpi_device_add(device, acpi_device_release);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001484 if (result) {
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001485 acpi_device_release(&device->dev);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001486 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001489 acpi_power_add_remove_device(device, true);
Rafael J. Wysockicf860be2013-01-24 12:49:49 +01001490 acpi_device_add_finalize(device);
Rafael J. Wysockid43e1672013-01-17 14:11:05 +01001491 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1492 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1493 dev_name(&device->dev), (char *) buffer.pointer,
1494 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1495 kfree(buffer.pointer);
1496 *child = device;
1497 return 0;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001498}
1499
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001500static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1501 unsigned long long *sta)
1502{
1503 acpi_status status;
1504 acpi_object_type acpi_type;
1505
1506 status = acpi_get_type(handle, &acpi_type);
1507 if (ACPI_FAILURE(status))
1508 return -ENODEV;
1509
1510 switch (acpi_type) {
1511 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1512 case ACPI_TYPE_DEVICE:
1513 *type = ACPI_BUS_TYPE_DEVICE;
1514 status = acpi_bus_get_status_handle(handle, sta);
1515 if (ACPI_FAILURE(status))
1516 return -ENODEV;
1517 break;
1518 case ACPI_TYPE_PROCESSOR:
1519 *type = ACPI_BUS_TYPE_PROCESSOR;
1520 status = acpi_bus_get_status_handle(handle, sta);
1521 if (ACPI_FAILURE(status))
1522 return -ENODEV;
1523 break;
1524 case ACPI_TYPE_THERMAL:
1525 *type = ACPI_BUS_TYPE_THERMAL;
1526 *sta = ACPI_STA_DEFAULT;
1527 break;
1528 case ACPI_TYPE_POWER:
1529 *type = ACPI_BUS_TYPE_POWER;
1530 *sta = ACPI_STA_DEFAULT;
1531 break;
1532 default:
1533 return -ENODEV;
1534 }
1535
1536 return 0;
1537}
1538
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001539static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1540 const struct acpi_device_id **matchid)
1541{
1542 struct acpi_scan_handler *handler;
1543
1544 list_for_each_entry(handler, &acpi_scan_handlers_list, list_node) {
1545 const struct acpi_device_id *devid;
1546
1547 for (devid = handler->ids; devid->id[0]; devid++)
1548 if (!strcmp((char *)devid->id, idstr)) {
1549 if (matchid)
1550 *matchid = devid;
1551
1552 return handler;
1553 }
1554 }
1555 return NULL;
1556}
1557
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001558static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1559 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001561 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001562 int type;
1563 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001564 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001565 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001566
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001567 acpi_bus_get_device(handle, &device);
1568 if (device)
1569 goto out;
1570
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001571 result = acpi_bus_type_and_status(handle, &type, &sta);
1572 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001573 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Rafael J. Wysocki82c7d5e2013-01-17 14:11:05 +01001575 if (type == ACPI_BUS_TYPE_POWER) {
1576 acpi_add_power_resource(handle);
1577 return AE_OK;
1578 }
1579
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001580 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001581 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001582 struct acpi_device_wakeup wakeup;
1583 acpi_handle temp;
1584
1585 status = acpi_get_handle(handle, "_PRW", &temp);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001586 if (ACPI_SUCCESS(status)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001587 acpi_bus_extract_wakeup_device_power_package(handle,
1588 &wakeup);
Rafael J. Wysocki993cbe52013-01-17 14:11:06 +01001589 acpi_power_resources_list_free(&wakeup.resources);
1590 }
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001591 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001594 acpi_add_single_object(&device, handle, type, sta);
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001595 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001596 return AE_CTRL_DEPTH;
1597
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001598 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001599 if (!*return_value)
1600 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001601
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001602 return AE_OK;
1603}
1604
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001605static int acpi_scan_attach_handler(struct acpi_device *device)
1606{
1607 struct acpi_hardware_id *hwid;
1608 int ret = 0;
1609
1610 list_for_each_entry(hwid, &device->pnp.ids, list) {
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001611 const struct acpi_device_id *devid;
1612 struct acpi_scan_handler *handler;
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001613
Rafael J. Wysockic5698072013-03-03 23:05:14 +01001614 handler = acpi_scan_match_handler(hwid->id, &devid);
1615 if (handler) {
1616 ret = handler->attach(device, devid);
1617 if (ret > 0) {
1618 device->handler = handler;
1619 break;
1620 } else if (ret < 0) {
1621 break;
1622 }
1623 }
Rafael J. Wysocki87b85b32013-02-06 13:05:22 +01001624 }
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001625 return ret;
1626}
1627
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001628static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1629 void *not_used, void **ret_not_used)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001630{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001631 struct acpi_device *device;
1632 unsigned long long sta_not_used;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001633 int ret;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001634
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001635 /*
1636 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1637 * namespace walks prematurely.
1638 */
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001639 if (acpi_bus_type_and_status(handle, &ret, &sta_not_used))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001640 return AE_OK;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001641
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001642 if (acpi_bus_get_device(handle, &device))
1643 return AE_CTRL_DEPTH;
Thomas Renninger77796882010-01-29 17:48:52 +01001644
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001645 ret = acpi_scan_attach_handler(device);
1646 if (ret)
1647 return ret > 0 ? AE_OK : AE_CTRL_DEPTH;
1648
1649 ret = device_attach(&device->dev);
1650 return ret >= 0 ? AE_OK : AE_CTRL_DEPTH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001653/**
1654 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1655 * @handle: Root of the namespace scope to scan.
Thomas Renninger77796882010-01-29 17:48:52 +01001656 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001657 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1658 * found devices.
Thomas Renninger77796882010-01-29 17:48:52 +01001659 *
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001660 * If no devices were found, -ENODEV is returned, but it does not mean that
1661 * there has been a real error. There just have been no suitable ACPI objects
1662 * in the table trunk from which the kernel could create a device and add an
1663 * appropriate driver.
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001664 *
1665 * Must be called under acpi_scan_lock.
Thomas Renninger77796882010-01-29 17:48:52 +01001666 */
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001667int acpi_bus_scan(acpi_handle handle)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001668{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001670 int error = 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001671
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001672 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001674 acpi_bus_check_add, NULL, NULL, &device);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001675
Thomas Renningerd2f66502010-01-29 17:48:51 +01001676 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001677 error = -ENODEV;
1678 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001679 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001680 acpi_bus_device_attach, NULL, NULL, NULL);
Thomas Renningerd2f66502010-01-29 17:48:51 +01001681
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001682 return error;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001683}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001684EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001686static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1687 void *not_used, void **ret_not_used)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001689 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001691 if (!acpi_bus_get_device(handle, &device)) {
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001692 struct acpi_scan_handler *dev_handler = device->handler;
1693
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001694 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
Rafael J. Wysockica589f92013-01-30 14:27:29 +01001695 if (dev_handler) {
1696 if (dev_handler->detach)
1697 dev_handler->detach(device);
1698
1699 device->handler = NULL;
1700 } else {
1701 device_release_driver(&device->dev);
1702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001704 return AE_OK;
1705}
1706
1707static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1708 void *not_used, void **ret_not_used)
1709{
1710 struct acpi_device *device = NULL;
1711
1712 if (!acpi_bus_get_device(handle, &device))
1713 acpi_device_unregister(device);
1714
1715 return AE_OK;
1716}
1717
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001718/**
1719 * acpi_bus_trim - Remove ACPI device node and all of its descendants
1720 * @start: Root of the ACPI device nodes subtree to remove.
1721 *
1722 * Must be called under acpi_scan_lock.
1723 */
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01001724void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001726 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001727 * Execute acpi_bus_device_detach() as a post-order callback to detach
1728 * all ACPI drivers from the device nodes being removed.
1729 */
1730 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1731 acpi_bus_device_detach, NULL, NULL);
1732 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1733 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001734 * Execute acpi_bus_remove() as a post-order callback to remove device
1735 * nodes in the given namespace scope.
1736 */
1737 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1738 acpi_bus_remove, NULL, NULL);
1739 acpi_bus_remove(start->handle, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
Kristen Accardiceaba662006-02-23 17:56:01 -08001741EXPORT_SYMBOL_GPL(acpi_bus_trim);
1742
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001743static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Len Brown4be44fc2005-08-05 00:44:28 -04001745 int result = 0;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /*
1748 * Enumerate all fixed-feature devices.
1749 */
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001750 if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
1751 struct acpi_device *device = NULL;
1752
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001753 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001754 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001755 ACPI_STA_DEFAULT);
1756 if (result)
1757 return result;
1758
1759 result = device_attach(&device->dev);
1760 if (result < 0)
1761 return result;
1762
Daniel Drakec10d7a132012-05-10 00:08:43 +01001763 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001766 if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
1767 struct acpi_device *device = NULL;
1768
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001769 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001770 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001771 ACPI_STA_DEFAULT);
1772 if (result)
1773 return result;
1774
1775 result = device_attach(&device->dev);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Rafael J. Wysocki2c0d4fe2013-01-29 13:57:20 +01001778 return result < 0 ? result : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Bjorn Helgaase747f272009-03-24 16:49:43 -06001781int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Patrick Mochel5b327262006-05-10 10:33:00 -04001785 result = bus_register(&acpi_bus_type);
1786 if (result) {
1787 /* We don't want to quit even if we failed to add suspend/resume */
1788 printk(KERN_ERR PREFIX "Could not register bus type\n");
1789 }
1790
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01001791 acpi_pci_root_init();
Rafael J. Wysocki4daeaf62013-01-30 14:27:37 +01001792 acpi_pci_link_init();
Rafael J. Wysocki141a2972013-01-30 14:27:40 +01001793 acpi_platform_init();
Mika Westerberg13176bb2013-01-17 09:59:33 +00001794 acpi_csrt_init();
Rafael J. Wysocki737f1a92013-02-08 23:52:39 +01001795 acpi_container_init();
Jiang Liuab1a2e02013-01-19 00:07:42 +08001796 acpi_pci_slot_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001797
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001798 mutex_lock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 * Enumerate devices in the ACPI namespace.
1801 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001802 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001804 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001806 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (result)
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001808 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001810 result = acpi_bus_scan_fixed();
1811 if (result) {
1812 acpi_device_unregister(acpi_root);
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001813 goto out;
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001814 }
1815
1816 acpi_update_all_gpes();
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001817
Yinghai Lu668192b2013-01-21 13:20:48 -08001818 acpi_pci_root_hp_init();
1819
Rafael J. Wysocki3757b942013-02-13 14:36:47 +01001820 out:
1821 mutex_unlock(&acpi_scan_lock);
1822 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}