blob: a4224727f09c5ee5092acd7fd21ccb4c3aff4047 [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
Mika Westerberg91e56872012-10-31 22:45:02 +010032/*
33 * The following ACPI IDs are known to be suitable for representing as
34 * platform devices.
35 */
36static const struct acpi_device_id acpi_platform_device_ids[] = {
37
Adrian Hunter142b0072012-11-23 21:11:47 +010038 { "PNP0D40" },
39
Mika Westerberg5e7779f2012-12-07 23:12:01 +010040 /* Haswell LPSS devices */
41 { "INT33C0", 0 },
42 { "INT33C1", 0 },
43 { "INT33C2", 0 },
44 { "INT33C3", 0 },
45 { "INT33C4", 0 },
46 { "INT33C5", 0 },
47 { "INT33C6", 0 },
48 { "INT33C7", 0 },
49
Mika Westerberg91e56872012-10-31 22:45:02 +010050 { }
51};
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080054static LIST_HEAD(acpi_bus_id_list);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +010055static DEFINE_MUTEX(acpi_scan_lock);
Shaohua Li90905892009-04-07 10:24:29 +080056DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057LIST_HEAD(acpi_wakeup_device_list);
58
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080059struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080060 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080061 unsigned int instance_no;
62 struct list_head node;
63};
Thomas Renninger29b71a12007-07-23 14:43:51 +020064
65/*
66 * Creates hid/cid(s) string needed for modalias and uevent
67 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
68 * char *modalias: "acpi:IBM0001:ACPI0001"
69*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020070static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
71 int size)
72{
Thomas Renninger29b71a12007-07-23 14:43:51 +020073 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080074 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060075 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020076
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020077 if (list_empty(&acpi_dev->pnp.ids))
78 return 0;
79
Zhang Rui5c9fcb52008-03-20 16:40:32 +080080 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020081 size -= len;
82
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060083 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
84 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080085 if (count < 0 || count >= size)
86 return -EINVAL;
87 len += count;
88 size -= count;
89 }
90
Thomas Renninger29b71a12007-07-23 14:43:51 +020091 modalias[len] = '\0';
92 return len;
93}
94
95static ssize_t
96acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
97 struct acpi_device *acpi_dev = to_acpi_device(dev);
98 int len;
99
100 /* Device has no HID and no CID or string is >1024 */
101 len = create_modalias(acpi_dev, buf, 1024);
102 if (len <= 0)
103 return 0;
104 buf[len++] = '\n';
105 return len;
106}
107static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
108
Toshi Kanic4753e52012-05-23 20:25:20 -0600109/**
110 * acpi_bus_hot_remove_device: hot-remove a device and its children
111 * @context: struct acpi_eject_event pointer (freed in this func)
112 *
113 * Hot-remove a device and its children. This function frees up the
114 * memory space passed by arg context, so that the caller may call
115 * this function asynchronously through acpi_os_hotplug_execute().
116 */
117void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Toshi Kanic4753e52012-05-23 20:25:20 -0600119 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
Yinghai Lu5993c462013-01-11 22:40:41 +0000120 struct acpi_device *device = ej_event->device;
121 acpi_handle handle = device->handle;
Toshi Kanib3c450c2012-10-26 13:38:57 +0200122 acpi_handle temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct acpi_object_list arg_list;
124 union acpi_object arg;
125 acpi_status status = AE_OK;
Toshi Kanic4753e52012-05-23 20:25:20 -0600126 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Zhang Rui26d46862008-04-29 02:35:48 -0400128 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100129 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400130
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +0100131 acpi_bus_trim(device);
132 /* Device node has been released. */
Toshi Kanib3c450c2012-10-26 13:38:57 +0200133 device = NULL;
134
Zhang Rui26d46862008-04-29 02:35:48 -0400135 /* power off device */
136 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
137 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800138 printk(KERN_WARNING PREFIX
139 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400140
Toshi Kanib3c450c2012-10-26 13:38:57 +0200141 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 arg_list.count = 1;
143 arg_list.pointer = &arg;
144 arg.type = ACPI_TYPE_INTEGER;
145 arg.integer.value = 0;
146 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
147 }
148
149 arg_list.count = 1;
150 arg_list.pointer = &arg;
151 arg.type = ACPI_TYPE_INTEGER;
152 arg.integer.value = 1;
153
154 /*
155 * TBD: _EJD support.
156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600158 if (ACPI_FAILURE(status)) {
159 if (status != AE_NOT_FOUND)
160 printk(KERN_WARNING PREFIX
161 "Eject device failed\n");
162 goto err_out;
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Toshi Kanic4753e52012-05-23 20:25:20 -0600165 kfree(context);
166 return;
167
168err_out:
169 /* Inform firmware the hot-remove operation has completed w/ error */
170 (void) acpi_evaluate_hotplug_ost(handle,
171 ej_event->event, ost_code, NULL);
172 kfree(context);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800173 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
Toshi Kani61622ac2012-11-01 14:42:12 +0000175EXPORT_SYMBOL(acpi_bus_hot_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800178acpi_eject_store(struct device *d, struct device_attribute *attr,
179 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Len Brown4be44fc2005-08-05 00:44:28 -0400181 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400182 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400183 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800184 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600185 struct acpi_eject_event *ej_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if ((!count) || (buf[0] != '1')) {
188 return -EINVAL;
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800191 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 ret = -ENODEV;
193 goto err;
194 }
195#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800196 status = acpi_get_type(acpi_device->handle, &type);
197 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 ret = -ENODEV;
199 goto err;
200 }
201
Toshi Kanic4753e52012-05-23 20:25:20 -0600202 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
203 if (!ej_event) {
204 ret = -ENOMEM;
205 goto err;
206 }
207
Yinghai Lu5993c462013-01-11 22:40:41 +0000208 ej_event->device = acpi_device;
Toshi Kanic4753e52012-05-23 20:25:20 -0600209 if (acpi_device->flags.eject_pending) {
210 /* event originated from ACPI eject notification */
211 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
212 acpi_device->flags.eject_pending = 0;
213 } else {
214 /* event originated from user */
215 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
Yinghai Lu5993c462013-01-11 22:40:41 +0000216 (void) acpi_evaluate_hotplug_ost(acpi_device->handle,
Toshi Kanic4753e52012-05-23 20:25:20 -0600217 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
218 }
219
220 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
Alok N Kataria74523c92008-06-13 12:54:24 -0400221err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800225static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800227static ssize_t
228acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
229 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600231 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800232}
233static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
234
Lv Zheng923d4a42012-10-30 14:43:26 +0100235static ssize_t acpi_device_uid_show(struct device *dev,
236 struct device_attribute *attr, char *buf)
237{
238 struct acpi_device *acpi_dev = to_acpi_device(dev);
239
240 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
241}
242static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
243
244static ssize_t acpi_device_adr_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
246{
247 struct acpi_device *acpi_dev = to_acpi_device(dev);
248
249 return sprintf(buf, "0x%08x\n",
250 (unsigned int)(acpi_dev->pnp.bus_address));
251}
252static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
253
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800254static ssize_t
255acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
256 struct acpi_device *acpi_dev = to_acpi_device(dev);
257 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
258 int result;
259
260 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600261 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800262 goto end;
263
264 result = sprintf(buf, "%s\n", (char*)path.pointer);
265 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600266end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800267 return result;
268}
269static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
270
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600271/* sysfs file that shows description text from the ACPI _STR method */
272static ssize_t description_show(struct device *dev,
273 struct device_attribute *attr,
274 char *buf) {
275 struct acpi_device *acpi_dev = to_acpi_device(dev);
276 int result;
277
278 if (acpi_dev->pnp.str_obj == NULL)
279 return 0;
280
281 /*
282 * The _STR object contains a Unicode identifier for a device.
283 * We need to convert to utf-8 so it can be displayed.
284 */
285 result = utf16s_to_utf8s(
286 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
287 acpi_dev->pnp.str_obj->buffer.length,
288 UTF16_LITTLE_ENDIAN, buf,
289 PAGE_SIZE);
290
291 buf[result++] = '\n';
292
293 return result;
294}
295static DEVICE_ATTR(description, 0444, description_show, NULL);
296
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100297static ssize_t
298acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
299 char *buf) {
300 struct acpi_device *acpi_dev = to_acpi_device(dev);
301
302 return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
303}
304static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
305
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800306static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600308 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800309 acpi_status status;
310 acpi_handle temp;
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100311 unsigned long long sun;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800312 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800314 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800315 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800316 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600317 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800318 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600319 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800320 goto end;
321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200323 if (!list_empty(&dev->pnp.ids)) {
324 result = device_create_file(&dev->dev, &dev_attr_hid);
325 if (result)
326 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200328 result = device_create_file(&dev->dev, &dev_attr_modalias);
329 if (result)
330 goto end;
331 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200332
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600333 /*
334 * If device has _STR, 'description' file is created
335 */
336 status = acpi_get_handle(dev->handle, "_STR", &temp);
337 if (ACPI_SUCCESS(status)) {
338 status = acpi_evaluate_object(dev->handle, "_STR",
339 NULL, &buffer);
340 if (ACPI_FAILURE(status))
341 buffer.pointer = NULL;
342 dev->pnp.str_obj = buffer.pointer;
343 result = device_create_file(&dev->dev, &dev_attr_description);
344 if (result)
345 goto end;
346 }
347
Lv Zheng923d4a42012-10-30 14:43:26 +0100348 if (dev->flags.bus_address)
349 result = device_create_file(&dev->dev, &dev_attr_adr);
350 if (dev->pnp.unique_id)
351 result = device_create_file(&dev->dev, &dev_attr_uid);
352
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100353 status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
354 if (ACPI_SUCCESS(status)) {
355 dev->pnp.sun = (unsigned long)sun;
356 result = device_create_file(&dev->dev, &dev_attr_sun);
357 if (result)
358 goto end;
359 } else {
360 dev->pnp.sun = (unsigned long)-1;
361 }
362
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800363 /*
364 * If device has _EJ0, 'eject' file is created that is used to trigger
365 * hot-removal function from userland.
366 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800367 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
368 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800369 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600370end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800371 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800374static void acpi_device_remove_files(struct acpi_device *dev)
375{
376 acpi_status status;
377 acpi_handle temp;
378
379 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600380 * If device has _STR, remove 'description' file
381 */
382 status = acpi_get_handle(dev->handle, "_STR", &temp);
383 if (ACPI_SUCCESS(status)) {
384 kfree(dev->pnp.str_obj);
385 device_remove_file(&dev->dev, &dev_attr_description);
386 }
387 /*
388 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800389 */
390 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
391 if (ACPI_SUCCESS(status))
392 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800393
Yasuaki Ishimatsubb74ac22012-11-16 02:56:59 +0100394 status = acpi_get_handle(dev->handle, "_SUN", &temp);
395 if (ACPI_SUCCESS(status))
396 device_remove_file(&dev->dev, &dev_attr_sun);
397
Lv Zheng923d4a42012-10-30 14:43:26 +0100398 if (dev->pnp.unique_id)
399 device_remove_file(&dev->dev, &dev_attr_uid);
400 if (dev->flags.bus_address)
401 device_remove_file(&dev->dev, &dev_attr_adr);
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600402 device_remove_file(&dev->dev, &dev_attr_modalias);
403 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600404 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800405 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800406}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800408 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200410
Mika Westerbergcf761af2012-10-31 22:44:41 +0100411static const struct acpi_device_id *__acpi_match_device(
412 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200413{
414 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600415 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200416
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800417 /*
418 * If the device is not present, it is unnecessary to load device
419 * driver for it.
420 */
421 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100422 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800423
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600424 for (id = ids; id->id[0]; id++)
425 list_for_each_entry(hwid, &device->pnp.ids, list)
426 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100427 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200428
Mika Westerbergcf761af2012-10-31 22:44:41 +0100429 return NULL;
430}
431
432/**
433 * acpi_match_device - Match a struct device against a given list of ACPI IDs
434 * @ids: Array of struct acpi_device_id object to match against.
435 * @dev: The device structure to match.
436 *
437 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
438 * object for that handle and use that object to match against a given list of
439 * device IDs.
440 *
441 * Return a pointer to the first matching ID on success or %NULL on failure.
442 */
443const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
444 const struct device *dev)
445{
446 struct acpi_device *adev;
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100447 acpi_handle handle = ACPI_HANDLE(dev);
Mika Westerbergcf761af2012-10-31 22:44:41 +0100448
Rafael J. Wysocki0613e1f2013-01-31 20:54:05 +0100449 if (!ids || !handle || acpi_bus_get_device(handle, &adev))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100450 return NULL;
451
452 return __acpi_match_device(adev, ids);
453}
454EXPORT_SYMBOL_GPL(acpi_match_device);
455
456int acpi_match_device_ids(struct acpi_device *device,
457 const struct acpi_device_id *ids)
458{
459 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200460}
461EXPORT_SYMBOL(acpi_match_device_ids);
462
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600463static void acpi_free_ids(struct acpi_device *device)
464{
465 struct acpi_hardware_id *id, *tmp;
466
467 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
468 kfree(id->id);
469 kfree(id);
470 }
471}
472
Patrick Mochel1890a972006-12-07 20:56:31 +0800473static void acpi_device_release(struct device *dev)
474{
475 struct acpi_device *acpi_dev = to_acpi_device(dev);
476
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600477 acpi_free_ids(acpi_dev);
Lv Zhengccf78042012-10-30 14:41:07 +0100478 kfree(acpi_dev->pnp.unique_id);
Patrick Mochel1890a972006-12-07 20:56:31 +0800479 kfree(acpi_dev);
480}
481
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800482static int acpi_bus_match(struct device *dev, struct device_driver *drv)
483{
484 struct acpi_device *acpi_dev = to_acpi_device(dev);
485 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
486
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +0100487 return acpi_dev->flags.match_driver
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +0100488 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800489}
490
Kay Sievers7eff2e72007-08-14 15:15:12 +0200491static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800492{
493 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200494 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800495
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200496 if (list_empty(&acpi_dev->pnp.ids))
497 return 0;
498
Kay Sievers7eff2e72007-08-14 15:15:12 +0200499 if (add_uevent_var(env, "MODALIAS="))
500 return -ENOMEM;
501 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
502 sizeof(env->buf) - env->buflen);
503 if (len >= (sizeof(env->buf) - env->buflen))
504 return -ENOMEM;
505 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return 0;
507}
508
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000509static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
510{
511 struct acpi_device *device = data;
512
513 device->driver->ops.notify(device, event);
514}
515
516static acpi_status acpi_device_notify_fixed(void *data)
517{
518 struct acpi_device *device = data;
519
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000520 /* Fixed hardware devices have no handles */
521 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000522 return AE_OK;
523}
524
525static int acpi_device_install_notify_handler(struct acpi_device *device)
526{
527 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000528
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000529 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000530 status =
531 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
532 acpi_device_notify_fixed,
533 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000534 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000535 status =
536 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
537 acpi_device_notify_fixed,
538 device);
539 else
540 status = acpi_install_notify_handler(device->handle,
541 ACPI_DEVICE_NOTIFY,
542 acpi_device_notify,
543 device);
544
545 if (ACPI_FAILURE(status))
546 return -EINVAL;
547 return 0;
548}
549
550static void acpi_device_remove_notify_handler(struct acpi_device *device)
551{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000552 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000553 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
554 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000555 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000556 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
557 acpi_device_notify_fixed);
558 else
559 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
560 acpi_device_notify);
561}
562
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800563static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800564static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800565{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800566 struct acpi_device *acpi_dev = to_acpi_device(dev);
567 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
568 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800570 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
571 if (!ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000572 if (acpi_drv->ops.notify) {
573 ret = acpi_device_install_notify_handler(acpi_dev);
574 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000575 if (acpi_drv->ops.remove)
576 acpi_drv->ops.remove(acpi_dev,
577 acpi_dev->removal_type);
578 return ret;
579 }
580 }
581
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800582 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
583 "Found driver [%s] for device [%s]\n",
584 acpi_drv->name, acpi_dev->pnp.bus_id));
585 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800586 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800587 return ret;
588}
589
590static int acpi_device_remove(struct device * dev)
591{
592 struct acpi_device *acpi_dev = to_acpi_device(dev);
593 struct acpi_driver *acpi_drv = acpi_dev->driver;
594
595 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000596 if (acpi_drv->ops.notify)
597 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800598 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800599 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800600 }
601 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700602 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800603
604 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800605 return 0;
606}
607
David Brownell55955aa2007-05-08 00:28:35 -0700608struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800609 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800610 .match = acpi_bus_match,
611 .probe = acpi_device_probe,
612 .remove = acpi_device_remove,
613 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614};
615
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000616static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800618 int result;
619 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
620 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 /*
623 * Linkage
624 * -------
625 * Link this device to its parent and siblings.
626 */
627 INIT_LIST_HEAD(&device->children);
628 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800630 INIT_LIST_HEAD(&device->physical_node_list);
631 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800633 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
634 if (!new_bus_id) {
635 printk(KERN_ERR PREFIX "Memory allocation error\n");
636 return -ENOMEM;
637 }
638
Shaohua Li90905892009-04-07 10:24:29 +0800639 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800640 /*
641 * Find suitable bus_id and instance number in acpi_bus_id_list
642 * If failed, create one and link it into acpi_bus_id_list
643 */
644 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600645 if (!strcmp(acpi_device_bus_id->bus_id,
646 acpi_device_hid(device))) {
647 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800648 found = 1;
649 kfree(new_bus_id);
650 break;
651 }
652 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600653 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800654 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600655 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800656 acpi_device_bus_id->instance_no = 0;
657 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
658 }
Kay Sievers07944692008-10-30 01:18:59 +0100659 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 +0800660
Len Brown33b57152008-12-15 22:09:26 -0500661 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (device->wakeup.flags.valid)
665 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800666 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Patrick Mochel1890a972006-12-07 20:56:31 +0800668 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000669 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800670 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800671 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600672 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600673 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600674 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800675 goto end;
676 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800677
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800678 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600679 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100680 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
681 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800682
Li Shaohua96333572006-12-07 20:56:46 +0800683 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800684 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600685end:
Shaohua Li90905892009-04-07 10:24:29 +0800686 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500687 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800688 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800689 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800690 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800691 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Rafael J. Wysockib17b5372013-01-15 13:23:44 +0100694static void acpi_device_unregister(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Shaohua Li90905892009-04-07 10:24:29 +0800696 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500697 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800701 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800704
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800705 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800706 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Zhang Rui9e89dde2006-12-07 20:56:16 +0800709/* --------------------------------------------------------------------------
710 Driver Management
711 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500713 * acpi_bus_driver_init - add a device to a driver
714 * @device: the device to add and initialize
715 * @driver: driver for the device
716 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600717 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800718 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
720static int
Len Brown4be44fc2005-08-05 00:44:28 -0400721acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Len Brown4be44fc2005-08-05 00:44:28 -0400723 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400726 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400729 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 result = driver->ops.add(device);
732 if (result) {
733 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700734 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400735 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
737
738 device->driver = driver;
739
740 /*
741 * TBD - Configuration Management: Assign resources to device based
742 * upon possible configuration and currently allocated resources.
743 */
744
Len Brown4be44fc2005-08-05 00:44:28 -0400745 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
746 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400747 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500751 * acpi_bus_register_driver - register a driver with the ACPI bus
752 * @driver: driver being registered
753 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500755 * devices that match the driver's criteria and binds. Returns zero for
756 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 */
Len Brown4be44fc2005-08-05 00:44:28 -0400758int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Patrick Mochel1890a972006-12-07 20:56:31 +0800760 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400763 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800764 driver->drv.name = driver->name;
765 driver->drv.bus = &acpi_bus_type;
766 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Patrick Mochel1890a972006-12-07 20:56:31 +0800768 ret = driver_register(&driver->drv);
769 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Len Brown4be44fc2005-08-05 00:44:28 -0400772EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500775 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
776 * @driver: driver to unregister
777 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * Unregisters a driver with the ACPI bus. Searches the namespace for all
779 * devices that match the driver's criteria and unbinds.
780 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400781void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Patrick Mochel1890a972006-12-07 20:56:31 +0800783 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
Len Brown4be44fc2005-08-05 00:44:28 -0400785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786EXPORT_SYMBOL(acpi_bus_unregister_driver);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/* --------------------------------------------------------------------------
789 Device Enumeration
790 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000791static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
792{
793 acpi_status status;
794 int ret;
795 struct acpi_device *device;
796
797 /*
798 * Fixed hardware devices do not appear in the namespace and do not
799 * have handles, but we fabricate acpi_devices for them, so we have
800 * to deal with them specially.
801 */
802 if (handle == NULL)
803 return acpi_root;
804
805 do {
806 status = acpi_get_parent(handle, &handle);
807 if (status == AE_NULL_ENTRY)
808 return NULL;
809 if (ACPI_FAILURE(status))
810 return acpi_root;
811
812 ret = acpi_bus_get_device(handle, &device);
813 if (ret == 0)
814 return device;
815 } while (1);
816}
817
Len Brownc8f7a62c2006-07-09 17:22:28 -0400818acpi_status
819acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
820{
821 acpi_status status;
822 acpi_handle tmp;
823 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
824 union acpi_object *obj;
825
826 status = acpi_get_handle(handle, "_EJD", &tmp);
827 if (ACPI_FAILURE(status))
828 return status;
829
830 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
831 if (ACPI_SUCCESS(status)) {
832 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100833 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
834 ejd);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400835 kfree(buffer.pointer);
836 }
837 return status;
838}
839EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
840
Bob Moore8e4319c2009-06-29 13:43:27 +0800841void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843
844 /* TBD */
845
846 return;
847}
848
Zhang Rui9e89dde2006-12-07 20:56:16 +0800849static int acpi_bus_get_perf_flags(struct acpi_device *device)
850{
851 device->performance.state = ACPI_STATE_UNKNOWN;
852 return 0;
853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100856acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
857 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100859 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
860 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100862 acpi_status status;
863 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100865 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return AE_BAD_PARAMETER;
867
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100868 /* _PRW */
869 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
870 if (ACPI_FAILURE(status)) {
871 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
872 return status;
873 }
874
875 package = (union acpi_object *)buffer.pointer;
876
877 if (!package || (package->package.count < 2)) {
878 status = AE_BAD_DATA;
879 goto out;
880 }
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100883 if (!element) {
884 status = AE_BAD_DATA;
885 goto out;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (element->type == ACPI_TYPE_PACKAGE) {
888 if ((element->package.count < 2) ||
889 (element->package.elements[0].type !=
890 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100891 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
892 status = AE_BAD_DATA;
893 goto out;
894 }
895 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100897 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 (u32) element->package.elements[1].integer.value;
899 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100900 wakeup->gpe_device = NULL;
901 wakeup->gpe_number = element->integer.value;
902 } else {
903 status = AE_BAD_DATA;
904 goto out;
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 element = &(package->package.elements[1]);
908 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100909 status = AE_BAD_DATA;
910 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100912 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100915 status = AE_NO_MEMORY;
916 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100918 wakeup->resources.count = package->package.count - 2;
919 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100921 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
922 status = AE_BAD_DATA;
923 goto out;
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100926 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
Lin Mingbba63a22010-12-13 13:39:17 +0800929 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200930
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100931 out:
932 kfree(buffer.pointer);
933
934 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100937static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200939 struct acpi_device_id button_device_ids[] = {
Thomas Renninger29b71a12007-07-23 14:43:51 +0200940 {"PNP0C0C", 0},
Zhang Ruib7e38302012-12-04 23:23:16 +0100941 {"PNP0C0D", 0},
Thomas Renninger29b71a12007-07-23 14:43:51 +0200942 {"PNP0C0E", 0},
943 {"", 0},
944 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100945 acpi_status status;
946 acpi_event_status event_status;
947
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100948 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100949
950 /* Power button, Lid switch always enable wakeup */
951 if (!acpi_match_device_ids(device, button_device_ids)) {
952 device->wakeup.flags.run_wake = 1;
Zhang Ruib7e38302012-12-04 23:23:16 +0100953 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
954 /* Do not use Lid/sleep button for S5 wakeup */
955 if (device->wakeup.sleep_state == ACPI_STATE_S5)
956 device->wakeup.sleep_state = ACPI_STATE_S4;
957 }
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100958 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100959 return;
960 }
961
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200962 status = acpi_get_gpe_status(device->wakeup.gpe_device,
963 device->wakeup.gpe_number,
964 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100965 if (status == AE_OK)
966 device->wakeup.flags.run_wake =
967 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
968}
969
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100970static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100971{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100972 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100973 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100974 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200975
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100976 /* Presence of _PRW indicates wake capable */
977 status = acpi_get_handle(device->handle, "_PRW", &temp);
978 if (ACPI_FAILURE(status))
979 return;
980
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100981 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
982 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (ACPI_FAILURE(status)) {
984 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100985 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200989 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100990 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800991 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
992 * system for the ACPI device with the _PRW object.
993 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
994 * So it is necessary to call _DSW object first. Only when it is not
995 * present will the _PSW object used.
996 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200997 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
998 if (psw_error)
999 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1000 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001003static void acpi_bus_add_power_resource(acpi_handle handle);
1004
Zhang Rui9e89dde2006-12-07 20:56:16 +08001005static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Zhang Rui9e89dde2006-12-07 20:56:16 +08001007 acpi_status status = 0;
1008 acpi_handle handle = NULL;
1009 u32 i = 0;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001013 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 */
Zhang Rui9e89dde2006-12-07 20:56:16 +08001015 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001017 device->power.flags.explicit_get = 1;
1018 status = acpi_get_handle(device->handle, "_IRC", &handle);
1019 if (ACPI_SUCCESS(status))
1020 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +08001023 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
Lin Ming1cc0c992012-04-23 09:03:49 +08001025 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +08001026 struct acpi_device_power_state *ps = &device->power.states[i];
1027 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Zhang Rui9e89dde2006-12-07 20:56:16 +08001029 /* Evaluate "_PRx" to se if power resources are referenced */
1030 acpi_evaluate_reference(device->handle, object_name, NULL,
1031 &ps->resources);
1032 if (ps->resources.count) {
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001033 int j;
1034
Zhang Rui9e89dde2006-12-07 20:56:16 +08001035 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001036 for (j = 0; j < ps->resources.count; j++)
1037 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Zhang Rui9e89dde2006-12-07 20:56:16 +08001040 /* Evaluate "_PSx" to see if we can do explicit sets */
1041 object_name[2] = 'S';
1042 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +08001043 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001044 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001045
Lin Ming1cc0c992012-04-23 09:03:49 +08001046 /*
1047 * State is valid if there are means to put the device into it.
1048 * D3hot is only valid if _PR3 present.
1049 */
1050 if (ps->resources.count ||
Aaron Lu1399dfc2012-11-21 23:33:40 +01001051 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) {
Zhang Rui9e89dde2006-12-07 20:56:16 +08001052 ps->flags.valid = 1;
Aaron Lu1399dfc2012-11-21 23:33:40 +01001053 ps->flags.os_accessible = 1;
1054 }
Zhang Rui9e89dde2006-12-07 20:56:16 +08001055
1056 ps->power = -1; /* Unknown - driver assigned */
1057 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Zhang Rui9e89dde2006-12-07 20:56:16 +08001060 /* Set defaults for D0 and D3 states (always valid) */
1061 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1062 device->power.states[ACPI_STATE_D0].power = 100;
1063 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1064 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001066 /* Set D3cold's explicit_set flag if _PS3 exists. */
1067 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1068 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1069
Aaron Lu1399dfc2012-11-21 23:33:40 +01001070 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1071 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1072 device->power.flags.power_resources)
1073 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1074
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +01001075 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 return 0;
1078}
1079
Len Brown4be44fc2005-08-05 00:44:28 -04001080static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Len Brown4be44fc2005-08-05 00:44:28 -04001082 acpi_status status = AE_OK;
1083 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 /* Presence of _STA indicates 'dynamic_status' */
1087 status = acpi_get_handle(device->handle, "_STA", &temp);
1088 if (ACPI_SUCCESS(status))
1089 device->flags.dynamic_status = 1;
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /* Presence of _RMV indicates 'removable' */
1092 status = acpi_get_handle(device->handle, "_RMV", &temp);
1093 if (ACPI_SUCCESS(status))
1094 device->flags.removable = 1;
1095
1096 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1097 status = acpi_get_handle(device->handle, "_EJD", &temp);
1098 if (ACPI_SUCCESS(status))
1099 device->flags.ejectable = 1;
1100 else {
1101 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1102 if (ACPI_SUCCESS(status))
1103 device->flags.ejectable = 1;
1104 }
1105
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +02001106 /* Power resources cannot be power manageable. */
1107 if (device->device_type == ACPI_BUS_TYPE_POWER)
1108 return 0;
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1111 status = acpi_get_handle(device->handle, "_PS0", &temp);
1112 if (ACPI_FAILURE(status))
1113 status = acpi_get_handle(device->handle, "_PR0", &temp);
1114 if (ACPI_SUCCESS(status))
1115 device->flags.power_manageable = 1;
1116
Joe Perches3c5f9be42008-02-03 17:06:17 +02001117 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Patrick Mocheld550d982006-06-27 00:41:40 -04001119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001122static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Len Brown4be44fc2005-08-05 00:44:28 -04001124 char bus_id[5] = { '?', 0 };
1125 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1126 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /*
1129 * Bus ID
1130 * ------
1131 * The device's Bus ID is simply the object name.
1132 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1133 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001134 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001136 return;
1137 }
1138
1139 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 case ACPI_BUS_TYPE_POWER_BUTTON:
1141 strcpy(device->pnp.bus_id, "PWRF");
1142 break;
1143 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1144 strcpy(device->pnp.bus_id, "SLPF");
1145 break;
1146 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001147 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 /* Clean up trailing underscores (if any) */
1149 for (i = 3; i > 1; i--) {
1150 if (bus_id[i] == '_')
1151 bus_id[i] = '\0';
1152 else
1153 break;
1154 }
1155 strcpy(device->pnp.bus_id, bus_id);
1156 break;
1157 }
1158}
1159
Zhang Rui54735262007-01-11 02:09:09 -05001160/*
1161 * acpi_bay_match - see if a device is an ejectable driver bay
1162 *
1163 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1164 * then we can safely call it an ejectable drive bay
1165 */
1166static int acpi_bay_match(struct acpi_device *device){
1167 acpi_status status;
1168 acpi_handle handle;
1169 acpi_handle tmp;
1170 acpi_handle phandle;
1171
1172 handle = device->handle;
1173
1174 status = acpi_get_handle(handle, "_EJ0", &tmp);
1175 if (ACPI_FAILURE(status))
1176 return -ENODEV;
1177
1178 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1179 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1180 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1181 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1182 return 0;
1183
1184 if (acpi_get_parent(handle, &phandle))
1185 return -ENODEV;
1186
1187 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1188 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1189 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1190 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1191 return 0;
1192
1193 return -ENODEV;
1194}
1195
Frank Seidel3620f2f2007-12-07 13:20:34 +01001196/*
1197 * acpi_dock_match - see if a device has a _DCK method
1198 */
1199static int acpi_dock_match(struct acpi_device *device)
1200{
1201 acpi_handle tmp;
1202 return acpi_get_handle(device->handle, "_DCK", &tmp);
1203}
1204
Thomas Renninger620e1122010-10-01 10:54:00 +02001205const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001206{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001207 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001208
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001209 if (list_empty(&device->pnp.ids))
1210 return dummy_hid;
1211
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001212 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1213 return hid->id;
1214}
1215EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001216
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001217static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1218{
1219 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001220
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001221 id = kmalloc(sizeof(*id), GFP_KERNEL);
1222 if (!id)
1223 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001224
Thomas Meyer581de592011-08-06 11:32:56 +02001225 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001226 if (!id->id) {
1227 kfree(id);
1228 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001229 }
1230
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001231 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001232}
1233
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001234/*
1235 * Old IBM workstations have a DSDT bug wherein the SMBus object
1236 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1237 * prefix. Work around this.
1238 */
1239static int acpi_ibm_smbus_match(struct acpi_device *device)
1240{
1241 acpi_handle h_dummy;
1242 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1243 int result;
1244
1245 if (!dmi_name_in_vendors("IBM"))
1246 return -ENODEV;
1247
1248 /* Look for SMBS object */
1249 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1250 if (result)
1251 return result;
1252
1253 if (strcmp("SMBS", path.pointer)) {
1254 result = -ENODEV;
1255 goto out;
1256 }
1257
1258 /* Does it have the necessary (but misnamed) methods? */
1259 result = -ENODEV;
1260 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1261 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1262 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1263 result = 0;
1264out:
1265 kfree(path.pointer);
1266 return result;
1267}
1268
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001269static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Len Brown4be44fc2005-08-05 00:44:28 -04001271 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001272 struct acpi_device_info *info;
Lv Zheng78e25fe2012-10-31 02:25:24 +00001273 struct acpi_pnp_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001274 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001276 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001278 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001279 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001280 break;
1281 }
1282
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001283 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001285 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return;
1287 }
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001290 acpi_add_id(device, info->hardware_id.string);
1291 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001292 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001293 for (i = 0; i < cid_list->count; i++)
1294 acpi_add_id(device, cid_list->ids[i].string);
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (info->valid & ACPI_VALID_ADR) {
1297 device->pnp.bus_address = info->address;
1298 device->flags.bus_address = 1;
1299 }
Lv Zhengccf78042012-10-30 14:41:07 +01001300 if (info->valid & ACPI_VALID_UID)
1301 device->pnp.unique_id = kstrdup(info->unique_id.string,
1302 GFP_KERNEL);
Zhang Ruiae843332006-12-07 20:57:10 +08001303
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001304 kfree(info);
1305
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001306 /*
1307 * Some devices don't reliably have _HIDs & _CIDs, so add
1308 * synthetic HIDs to make sure drivers can find them.
1309 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001310 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001311 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001312 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001313 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001314 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001315 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001316 else if (!acpi_ibm_smbus_match(device))
1317 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001318 else if (!acpi_device_hid(device) &&
1319 ACPI_IS_ROOT_DEVICE(device->parent)) {
1320 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1321 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1322 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1323 }
Zhang Rui54735262007-01-11 02:09:09 -05001324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 break;
1326 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001327 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 break;
1329 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001330 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001333 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 break;
1335 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001336 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 break;
1338 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001339 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 break;
1341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001344static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001346 acpi_status status;
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /*
1349 * Context
1350 * -------
1351 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001352 * resolutions from handle->device very efficient. Fixed hardware
1353 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001355 if (!device->handle)
1356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001358 status = acpi_attach_data(device->handle,
1359 acpi_bus_data_handler, device);
1360 if (ACPI_SUCCESS(status))
1361 return 0;
1362
1363 printk(KERN_ERR PREFIX "Error attaching device data\n");
1364 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001367static int acpi_add_single_object(struct acpi_device **child,
1368 acpi_handle handle, int type,
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +01001369 unsigned long long sta, bool match_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001371 int result;
1372 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001373 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Burman Yan36bcbec2006-12-19 12:56:11 -08001375 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001377 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001378 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001381 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001382 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001384 device->parent = acpi_bus_get_parent(handle);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001385 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001386
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001387 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 /*
1390 * Flags
1391 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001392 * Note that we only look for object handles -- cannot evaluate objects
1393 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 */
1395 result = acpi_bus_get_flags(device);
1396 if (result)
1397 goto end;
1398
1399 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 * Initialize Device
1401 * -----------------
1402 * TBD: Synch with Core's enumeration/initialization process.
1403 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001404 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /*
1407 * Power Management
1408 * ----------------
1409 */
1410 if (device->flags.power_manageable) {
1411 result = acpi_bus_get_power_flags(device);
1412 if (result)
1413 goto end;
1414 }
1415
Len Brown4be44fc2005-08-05 00:44:28 -04001416 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 * Wakeup device management
1418 *-----------------------
1419 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001420 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /*
1423 * Performance Management
1424 * ----------------------
1425 */
1426 if (device->flags.performance_manageable) {
1427 result = acpi_bus_get_perf_flags(device);
1428 if (result)
1429 goto end;
1430 }
1431
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001432 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001433 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +01001435 device->flags.match_driver = match_driver;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001436 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Alex Chiang0c526d92009-05-14 08:31:37 -06001438end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001439 if (!result) {
1440 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1441 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1442 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1443 (char *) buffer.pointer,
1444 device->parent ? dev_name(&device->parent->dev) :
1445 "(null)"));
1446 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001448 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001449 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Patrick Mocheld550d982006-06-27 00:41:40 -04001451 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001454#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1455 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1456
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001457static void acpi_bus_add_power_resource(acpi_handle handle)
1458{
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001459 struct acpi_device *device = NULL;
1460
1461 acpi_bus_get_device(handle, &device);
1462 if (!device)
1463 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +01001464 ACPI_STA_DEFAULT, true);
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001465}
1466
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001467static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1468 unsigned long long *sta)
1469{
1470 acpi_status status;
1471 acpi_object_type acpi_type;
1472
1473 status = acpi_get_type(handle, &acpi_type);
1474 if (ACPI_FAILURE(status))
1475 return -ENODEV;
1476
1477 switch (acpi_type) {
1478 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1479 case ACPI_TYPE_DEVICE:
1480 *type = ACPI_BUS_TYPE_DEVICE;
1481 status = acpi_bus_get_status_handle(handle, sta);
1482 if (ACPI_FAILURE(status))
1483 return -ENODEV;
1484 break;
1485 case ACPI_TYPE_PROCESSOR:
1486 *type = ACPI_BUS_TYPE_PROCESSOR;
1487 status = acpi_bus_get_status_handle(handle, sta);
1488 if (ACPI_FAILURE(status))
1489 return -ENODEV;
1490 break;
1491 case ACPI_TYPE_THERMAL:
1492 *type = ACPI_BUS_TYPE_THERMAL;
1493 *sta = ACPI_STA_DEFAULT;
1494 break;
1495 case ACPI_TYPE_POWER:
1496 *type = ACPI_BUS_TYPE_POWER;
1497 *sta = ACPI_STA_DEFAULT;
1498 break;
1499 default:
1500 return -ENODEV;
1501 }
1502
1503 return 0;
1504}
1505
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001506static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1507 void *not_used, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001509 struct acpi_device *device = NULL;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001510 int type;
1511 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001512 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001513 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001514
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001515 acpi_bus_get_device(handle, &device);
1516 if (device)
1517 goto out;
1518
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001519 result = acpi_bus_type_and_status(handle, &type, &sta);
1520 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001521 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001523 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001524 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001525 struct acpi_device_wakeup wakeup;
1526 acpi_handle temp;
1527
1528 status = acpi_get_handle(handle, "_PRW", &temp);
1529 if (ACPI_SUCCESS(status))
1530 acpi_bus_extract_wakeup_device_power_package(handle,
1531 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001532 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Rafael J. Wysockif95988d2013-01-07 21:17:02 +01001535 acpi_add_single_object(&device, handle, type, sta,
1536 type == ACPI_BUS_TYPE_POWER);
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001537 if (!device)
1538 return AE_CTRL_DEPTH;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001539
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +01001540 device->flags.match_driver = true;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001541
Rafael J. Wysocki4002bf32012-12-21 00:36:44 +01001542 out:
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001543 if (!*return_value)
1544 *return_value = device;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001545
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001546 return AE_OK;
1547}
1548
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001549static acpi_status acpi_bus_device_attach(acpi_handle handle, u32 lvl_not_used,
1550 void *not_used, void **ret_not_used)
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001551{
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001552 acpi_status status = AE_OK;
1553 struct acpi_device *device;
1554 unsigned long long sta_not_used;
1555 int type_not_used;
1556
1557 /*
1558 * Ignore errors ignored by acpi_bus_check_add() to avoid terminating
1559 * namespace walks prematurely.
1560 */
1561 if (acpi_bus_type_and_status(handle, &type_not_used, &sta_not_used))
1562 return AE_OK;
1563
1564 if (acpi_bus_get_device(handle, &device))
1565 return AE_CTRL_DEPTH;
1566
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001567 if (!acpi_match_device_ids(device, acpi_platform_device_ids)) {
1568 /* This is a known good platform device. */
1569 acpi_create_platform_device(device);
Mika Westerbergabe99212013-01-11 22:08:09 +01001570 } else if (device_attach(&device->dev) < 0) {
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001571 status = AE_CTRL_DEPTH;
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001572 }
1573 return status;
1574}
1575
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001576/**
1577 * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
1578 * @handle: Root of the namespace scope to scan.
1579 *
1580 * Scan a given ACPI tree (probably recently hot-plugged) and create and add
1581 * found devices.
1582 *
1583 * If no devices were found, -ENODEV is returned, but it does not mean that
1584 * there has been a real error. There just have been no suitable ACPI objects
1585 * in the table trunk from which the kernel could create a device and add an
1586 * appropriate driver.
1587 */
1588int acpi_bus_scan(acpi_handle handle)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001589{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001590 void *device = NULL;
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001591 int error = 0;
1592
1593 mutex_lock(&acpi_scan_lock);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001594
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001595 if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001596 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysockie3863092012-12-21 00:36:47 +01001597 acpi_bus_check_add, NULL, NULL, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001598
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001599 if (!device)
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001600 error = -ENODEV;
1601 else if (ACPI_SUCCESS(acpi_bus_device_attach(handle, 0, NULL, NULL)))
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001602 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Rafael J. Wysocki0fc300b2012-12-21 00:36:41 +01001603 acpi_bus_device_attach, NULL, NULL, NULL);
Rafael J. Wysocki805d410f2012-12-21 00:36:39 +01001604
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001605 mutex_unlock(&acpi_scan_lock);
1606 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001608EXPORT_SYMBOL(acpi_bus_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001610static acpi_status acpi_bus_device_detach(acpi_handle handle, u32 lvl_not_used,
1611 void *not_used, void **ret_not_used)
1612{
1613 struct acpi_device *device = NULL;
1614
1615 if (!acpi_bus_get_device(handle, &device)) {
1616 device->removal_type = ACPI_BUS_REMOVAL_EJECT;
1617 device_release_driver(&device->dev);
1618 }
1619 return AE_OK;
1620}
1621
1622static acpi_status acpi_bus_remove(acpi_handle handle, u32 lvl_not_used,
1623 void *not_used, void **ret_not_used)
1624{
1625 struct acpi_device *device = NULL;
1626
1627 if (!acpi_bus_get_device(handle, &device))
1628 acpi_device_unregister(device);
1629
1630 return AE_OK;
1631}
1632
Rafael J. Wysockibfee26d2013-01-26 00:27:44 +01001633void acpi_bus_trim(struct acpi_device *start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001635 mutex_lock(&acpi_scan_lock);
1636
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001637 /*
Rafael J. Wysocki05404d82013-01-15 13:24:13 +01001638 * Execute acpi_bus_device_detach() as a post-order callback to detach
1639 * all ACPI drivers from the device nodes being removed.
1640 */
1641 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1642 acpi_bus_device_detach, NULL, NULL);
1643 acpi_bus_device_detach(start->handle, 0, NULL, NULL);
1644 /*
Rafael J. Wysockicecdb192013-01-15 13:24:02 +01001645 * Execute acpi_bus_remove() as a post-order callback to remove device
1646 * nodes in the given namespace scope.
1647 */
1648 acpi_walk_namespace(ACPI_TYPE_ANY, start->handle, ACPI_UINT32_MAX, NULL,
1649 acpi_bus_remove, NULL, NULL);
1650 acpi_bus_remove(start->handle, 0, NULL, NULL);
Rafael J. Wysockic511cc12013-01-27 21:17:29 +01001651
1652 mutex_unlock(&acpi_scan_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
Kristen Accardiceaba662006-02-23 17:56:01 -08001654EXPORT_SYMBOL_GPL(acpi_bus_trim);
1655
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001656static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Len Brown4be44fc2005-08-05 00:44:28 -04001658 int result = 0;
1659 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 /*
1662 * Enumerate all fixed-feature devices.
1663 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001664 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001665 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001666 ACPI_BUS_TYPE_POWER_BUTTON,
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +01001667 ACPI_STA_DEFAULT, true);
Daniel Drakec10d7a132012-05-10 00:08:43 +01001668 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001671 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001672 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001673 ACPI_BUS_TYPE_SLEEP_BUTTON,
Rafael J. Wysocki209d3b12012-12-21 00:36:48 +01001674 ACPI_STA_DEFAULT, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Patrick Mocheld550d982006-06-27 00:41:40 -04001677 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679
Bjorn Helgaase747f272009-03-24 16:49:43 -06001680int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Patrick Mochel5b327262006-05-10 10:33:00 -04001684 result = bus_register(&acpi_bus_type);
1685 if (result) {
1686 /* We don't want to quit even if we failed to add suspend/resume */
1687 printk(KERN_ERR PREFIX "Could not register bus type\n");
1688 }
1689
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001690 acpi_power_init();
Rafael J. Wysocki92ef2a22012-12-21 00:36:40 +01001691 acpi_pci_root_init();
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 * Enumerate devices in the ACPI namespace.
1695 */
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001696 result = acpi_bus_scan(ACPI_ROOT_OBJECT);
1697 if (result)
1698 return result;
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001699
Rafael J. Wysocki0cd6ac52012-12-21 00:36:49 +01001700 result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (result)
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001702 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +01001704 result = acpi_bus_scan_fixed();
1705 if (result) {
1706 acpi_device_unregister(acpi_root);
1707 return result;
1708 }
1709
1710 acpi_update_all_gpes();
1711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}