blob: 671bbe68b96cf6ada4d96bf96d15fac03a5d4200 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/acpi.h>
Alok N Kataria74523c92008-06-13 12:54:24 -040010#include <linux/signal.h>
11#include <linux/kthread.h>
Darrick J. Wong222e82a2010-03-24 14:38:37 +010012#include <linux/dmi.h>
Lance Ortizd1efe3c2012-10-02 12:43:23 -060013#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060017#include "internal.h"
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050020ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040022extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020025#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define ACPI_BUS_DEVICE_NAME "System Bus"
27
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +000028#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
Thomas Renninger620e1122010-10-01 10:54:00 +020030static const char *dummy_hid = "device";
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020031
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 Westerberg91e56872012-10-31 22:45:02 +010040 { }
41};
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2d2006-12-08 17:23:43 +080044static LIST_HEAD(acpi_bus_id_list);
Shaohua Li90905892009-04-07 10:24:29 +080045DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046LIST_HEAD(acpi_wakeup_device_list);
47
Zhang Ruie49bd2d2006-12-08 17:23:43 +080048struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080049 char bus_id[15];
Zhang Ruie49bd2d2006-12-08 17:23:43 +080050 unsigned int instance_no;
51 struct list_head node;
52};
Thomas Renninger29b71a12007-07-23 14:43:51 +020053
54/*
55 * Creates hid/cid(s) string needed for modalias and uevent
56 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
57 * char *modalias: "acpi:IBM0001:ACPI0001"
58*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020059static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
60 int size)
61{
Thomas Renninger29b71a12007-07-23 14:43:51 +020062 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080063 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060064 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020065
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020066 if (list_empty(&acpi_dev->pnp.ids))
67 return 0;
68
Zhang Rui5c9fcb52008-03-20 16:40:32 +080069 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020070 size -= len;
71
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060072 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
73 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080074 if (count < 0 || count >= size)
75 return -EINVAL;
76 len += count;
77 size -= count;
78 }
79
Thomas Renninger29b71a12007-07-23 14:43:51 +020080 modalias[len] = '\0';
81 return len;
82}
83
84static ssize_t
85acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
86 struct acpi_device *acpi_dev = to_acpi_device(dev);
87 int len;
88
89 /* Device has no HID and no CID or string is >1024 */
90 len = create_modalias(acpi_dev, buf, 1024);
91 if (len <= 0)
92 return 0;
93 buf[len++] = '\n';
94 return len;
95}
96static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
97
Toshi Kanic4753e52012-05-23 20:25:20 -060098/**
99 * acpi_bus_hot_remove_device: hot-remove a device and its children
100 * @context: struct acpi_eject_event pointer (freed in this func)
101 *
102 * Hot-remove a device and its children. This function frees up the
103 * memory space passed by arg context, so that the caller may call
104 * this function asynchronously through acpi_os_hotplug_execute().
105 */
106void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Toshi Kanic4753e52012-05-23 20:25:20 -0600108 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
Zhang Rui26d46862008-04-29 02:35:48 -0400109 struct acpi_device *device;
Toshi Kanic4753e52012-05-23 20:25:20 -0600110 acpi_handle handle = ej_event->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 struct acpi_object_list arg_list;
112 union acpi_object arg;
113 acpi_status status = AE_OK;
Toshi Kanic4753e52012-05-23 20:25:20 -0600114 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Zhang Rui26d46862008-04-29 02:35:48 -0400116 if (acpi_bus_get_device(handle, &device))
Toshi Kanic4753e52012-05-23 20:25:20 -0600117 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Zhang Rui26d46862008-04-29 02:35:48 -0400119 if (!device)
Toshi Kanic4753e52012-05-23 20:25:20 -0600120 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400121
122 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100123 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400124
125 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800126 printk(KERN_ERR PREFIX
127 "Removing device failed\n");
Toshi Kanic4753e52012-05-23 20:25:20 -0600128 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400129 }
130
131 /* power off device */
132 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
133 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800134 printk(KERN_WARNING PREFIX
135 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400136
137 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 arg_list.count = 1;
139 arg_list.pointer = &arg;
140 arg.type = ACPI_TYPE_INTEGER;
141 arg.integer.value = 0;
142 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
143 }
144
145 arg_list.count = 1;
146 arg_list.pointer = &arg;
147 arg.type = ACPI_TYPE_INTEGER;
148 arg.integer.value = 1;
149
150 /*
151 * TBD: _EJD support.
152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600154 if (ACPI_FAILURE(status)) {
155 if (status != AE_NOT_FOUND)
156 printk(KERN_WARNING PREFIX
157 "Eject device failed\n");
158 goto err_out;
159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Toshi Kanic4753e52012-05-23 20:25:20 -0600161 kfree(context);
162 return;
163
164err_out:
165 /* Inform firmware the hot-remove operation has completed w/ error */
166 (void) acpi_evaluate_hotplug_ost(handle,
167 ej_event->event, ost_code, NULL);
168 kfree(context);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800169 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800173acpi_eject_store(struct device *d, struct device_attribute *attr,
174 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Len Brown4be44fc2005-08-05 00:44:28 -0400176 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400177 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400178 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800179 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600180 struct acpi_eject_event *ej_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if ((!count) || (buf[0] != '1')) {
183 return -EINVAL;
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800186 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 ret = -ENODEV;
188 goto err;
189 }
190#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800191 status = acpi_get_type(acpi_device->handle, &type);
192 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 ret = -ENODEV;
194 goto err;
195 }
196
Toshi Kanic4753e52012-05-23 20:25:20 -0600197 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
198 if (!ej_event) {
199 ret = -ENOMEM;
200 goto err;
201 }
202
203 ej_event->handle = acpi_device->handle;
204 if (acpi_device->flags.eject_pending) {
205 /* event originated from ACPI eject notification */
206 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
207 acpi_device->flags.eject_pending = 0;
208 } else {
209 /* event originated from user */
210 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
211 (void) acpi_evaluate_hotplug_ost(ej_event->handle,
212 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
213 }
214
215 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
Alok N Kataria74523c92008-06-13 12:54:24 -0400216err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800220static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800222static ssize_t
223acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
224 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600226 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800227}
228static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
229
230static ssize_t
231acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
232 struct acpi_device *acpi_dev = to_acpi_device(dev);
233 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
234 int result;
235
236 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600237 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800238 goto end;
239
240 result = sprintf(buf, "%s\n", (char*)path.pointer);
241 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600242end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800243 return result;
244}
245static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
246
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600247/* sysfs file that shows description text from the ACPI _STR method */
248static ssize_t description_show(struct device *dev,
249 struct device_attribute *attr,
250 char *buf) {
251 struct acpi_device *acpi_dev = to_acpi_device(dev);
252 int result;
253
254 if (acpi_dev->pnp.str_obj == NULL)
255 return 0;
256
257 /*
258 * The _STR object contains a Unicode identifier for a device.
259 * We need to convert to utf-8 so it can be displayed.
260 */
261 result = utf16s_to_utf8s(
262 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
263 acpi_dev->pnp.str_obj->buffer.length,
264 UTF16_LITTLE_ENDIAN, buf,
265 PAGE_SIZE);
266
267 buf[result++] = '\n';
268
269 return result;
270}
271static DEVICE_ATTR(description, 0444, description_show, NULL);
272
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800273static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600275 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800276 acpi_status status;
277 acpi_handle temp;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800278 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800280 /*
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800281 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800282 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600283 if (dev->handle) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800284 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600285 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800286 goto end;
287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200289 if (!list_empty(&dev->pnp.ids)) {
290 result = device_create_file(&dev->dev, &dev_attr_hid);
291 if (result)
292 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200294 result = device_create_file(&dev->dev, &dev_attr_modalias);
295 if (result)
296 goto end;
297 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200298
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600299 /*
300 * If device has _STR, 'description' file is created
301 */
302 status = acpi_get_handle(dev->handle, "_STR", &temp);
303 if (ACPI_SUCCESS(status)) {
304 status = acpi_evaluate_object(dev->handle, "_STR",
305 NULL, &buffer);
306 if (ACPI_FAILURE(status))
307 buffer.pointer = NULL;
308 dev->pnp.str_obj = buffer.pointer;
309 result = device_create_file(&dev->dev, &dev_attr_description);
310 if (result)
311 goto end;
312 }
313
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800314 /*
315 * If device has _EJ0, 'eject' file is created that is used to trigger
316 * hot-removal function from userland.
317 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800318 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
319 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800320 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600321end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800322 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800323}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800325static void acpi_device_remove_files(struct acpi_device *dev)
326{
327 acpi_status status;
328 acpi_handle temp;
329
330 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600331 * If device has _STR, remove 'description' file
332 */
333 status = acpi_get_handle(dev->handle, "_STR", &temp);
334 if (ACPI_SUCCESS(status)) {
335 kfree(dev->pnp.str_obj);
336 device_remove_file(&dev->dev, &dev_attr_description);
337 }
338 /*
339 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800340 */
341 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
342 if (ACPI_SUCCESS(status))
343 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800344
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600345 device_remove_file(&dev->dev, &dev_attr_modalias);
346 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600347 if (dev->handle)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800348 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800349}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800351 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200353
Mika Westerbergcf761af2012-10-31 22:44:41 +0100354static const struct acpi_device_id *__acpi_match_device(
355 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200356{
357 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600358 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200359
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800360 /*
361 * If the device is not present, it is unnecessary to load device
362 * driver for it.
363 */
364 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100365 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800366
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600367 for (id = ids; id->id[0]; id++)
368 list_for_each_entry(hwid, &device->pnp.ids, list)
369 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100370 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200371
Mika Westerbergcf761af2012-10-31 22:44:41 +0100372 return NULL;
373}
374
375/**
376 * acpi_match_device - Match a struct device against a given list of ACPI IDs
377 * @ids: Array of struct acpi_device_id object to match against.
378 * @dev: The device structure to match.
379 *
380 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
381 * object for that handle and use that object to match against a given list of
382 * device IDs.
383 *
384 * Return a pointer to the first matching ID on success or %NULL on failure.
385 */
386const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
387 const struct device *dev)
388{
389 struct acpi_device *adev;
390
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100391 if (!ids || !ACPI_HANDLE(dev)
392 || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev)))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100393 return NULL;
394
395 return __acpi_match_device(adev, ids);
396}
397EXPORT_SYMBOL_GPL(acpi_match_device);
398
399int acpi_match_device_ids(struct acpi_device *device,
400 const struct acpi_device_id *ids)
401{
402 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200403}
404EXPORT_SYMBOL(acpi_match_device_ids);
405
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600406static void acpi_free_ids(struct acpi_device *device)
407{
408 struct acpi_hardware_id *id, *tmp;
409
410 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
411 kfree(id->id);
412 kfree(id);
413 }
414}
415
Patrick Mochel1890a972006-12-07 20:56:31 +0800416static void acpi_device_release(struct device *dev)
417{
418 struct acpi_device *acpi_dev = to_acpi_device(dev);
419
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600420 acpi_free_ids(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800421 kfree(acpi_dev);
422}
423
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800424static int acpi_bus_match(struct device *dev, struct device_driver *drv)
425{
426 struct acpi_device *acpi_dev = to_acpi_device(dev);
427 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
428
Thomas Renninger29b71a12007-07-23 14:43:51 +0200429 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800430}
431
Kay Sievers7eff2e72007-08-14 15:15:12 +0200432static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800433{
434 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200435 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800436
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200437 if (list_empty(&acpi_dev->pnp.ids))
438 return 0;
439
Kay Sievers7eff2e72007-08-14 15:15:12 +0200440 if (add_uevent_var(env, "MODALIAS="))
441 return -ENOMEM;
442 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
443 sizeof(env->buf) - env->buflen);
444 if (len >= (sizeof(env->buf) - env->buflen))
445 return -ENOMEM;
446 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
448}
449
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000450static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
451{
452 struct acpi_device *device = data;
453
454 device->driver->ops.notify(device, event);
455}
456
457static acpi_status acpi_device_notify_fixed(void *data)
458{
459 struct acpi_device *device = data;
460
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000461 /* Fixed hardware devices have no handles */
462 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000463 return AE_OK;
464}
465
466static int acpi_device_install_notify_handler(struct acpi_device *device)
467{
468 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000469
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000470 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000471 status =
472 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
473 acpi_device_notify_fixed,
474 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000475 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000476 status =
477 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
478 acpi_device_notify_fixed,
479 device);
480 else
481 status = acpi_install_notify_handler(device->handle,
482 ACPI_DEVICE_NOTIFY,
483 acpi_device_notify,
484 device);
485
486 if (ACPI_FAILURE(status))
487 return -EINVAL;
488 return 0;
489}
490
491static void acpi_device_remove_notify_handler(struct acpi_device *device)
492{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000493 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000494 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
495 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000496 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000497 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
498 acpi_device_notify_fixed);
499 else
500 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
501 acpi_device_notify);
502}
503
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800504static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
505static int acpi_start_single_object(struct acpi_device *);
506static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800507{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800508 struct acpi_device *acpi_dev = to_acpi_device(dev);
509 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
510 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800512 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
513 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800514 if (acpi_dev->bus_ops.acpi_op_start)
515 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000516
517 if (acpi_drv->ops.notify) {
518 ret = acpi_device_install_notify_handler(acpi_dev);
519 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000520 if (acpi_drv->ops.remove)
521 acpi_drv->ops.remove(acpi_dev,
522 acpi_dev->removal_type);
523 return ret;
524 }
525 }
526
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800527 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
528 "Found driver [%s] for device [%s]\n",
529 acpi_drv->name, acpi_dev->pnp.bus_id));
530 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800531 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800532 return ret;
533}
534
535static int acpi_device_remove(struct device * dev)
536{
537 struct acpi_device *acpi_dev = to_acpi_device(dev);
538 struct acpi_driver *acpi_drv = acpi_dev->driver;
539
540 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000541 if (acpi_drv->ops.notify)
542 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800543 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800544 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800545 }
546 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700547 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800548
549 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800550 return 0;
551}
552
David Brownell55955aa2007-05-08 00:28:35 -0700553struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800554 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800555 .match = acpi_bus_match,
556 .probe = acpi_device_probe,
557 .remove = acpi_device_remove,
558 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559};
560
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000561static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800563 int result;
564 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
565 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /*
568 * Linkage
569 * -------
570 * Link this device to its parent and siblings.
571 */
572 INIT_LIST_HEAD(&device->children);
573 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800575 INIT_LIST_HEAD(&device->physical_node_list);
576 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800578 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
579 if (!new_bus_id) {
580 printk(KERN_ERR PREFIX "Memory allocation error\n");
581 return -ENOMEM;
582 }
583
Shaohua Li90905892009-04-07 10:24:29 +0800584 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800585 /*
586 * Find suitable bus_id and instance number in acpi_bus_id_list
587 * If failed, create one and link it into acpi_bus_id_list
588 */
589 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600590 if (!strcmp(acpi_device_bus_id->bus_id,
591 acpi_device_hid(device))) {
592 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800593 found = 1;
594 kfree(new_bus_id);
595 break;
596 }
597 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600598 if (!found) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800599 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600600 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800601 acpi_device_bus_id->instance_no = 0;
602 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
603 }
Kay Sievers07944692008-10-30 01:18:59 +0100604 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800605
Len Brown33b57152008-12-15 22:09:26 -0500606 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (device->wakeup.flags.valid)
610 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800611 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Patrick Mochel1890a972006-12-07 20:56:31 +0800613 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000614 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800615 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800616 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600617 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600618 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600619 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800620 goto end;
621 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800622
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800623 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600624 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100625 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
626 dev_name(&device->dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800627
Li Shaohua96333572006-12-07 20:56:46 +0800628 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800629 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600630end:
Shaohua Li90905892009-04-07 10:24:29 +0800631 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500632 if (device->parent)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800633 list_del(&device->node);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800634 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800635 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800636 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639static void acpi_device_unregister(struct acpi_device *device, int type)
640{
Shaohua Li90905892009-04-07 10:24:29 +0800641 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500642 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800646 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800649
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800650 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800651 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Zhang Rui9e89dde2006-12-07 20:56:16 +0800654/* --------------------------------------------------------------------------
655 Driver Management
656 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500658 * acpi_bus_driver_init - add a device to a driver
659 * @device: the device to add and initialize
660 * @driver: driver for the device
661 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600662 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800663 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
665static int
Len Brown4be44fc2005-08-05 00:44:28 -0400666acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Len Brown4be44fc2005-08-05 00:44:28 -0400668 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400671 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400674 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 result = driver->ops.add(device);
677 if (result) {
678 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700679 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400680 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 device->driver = driver;
684
685 /*
686 * TBD - Configuration Management: Assign resources to device based
687 * upon possible configuration and currently allocated resources.
688 */
689
Len Brown4be44fc2005-08-05 00:44:28 -0400690 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
691 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400692 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700693}
694
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400695static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700696{
697 int result = 0;
698 struct acpi_driver *driver;
699
Rajesh Shah3fb02732005-04-28 00:25:52 -0700700
701 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400702 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (driver->ops.start) {
705 result = driver->ops.start(device);
706 if (result && driver->ops.remove)
707 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Patrick Mocheld550d982006-06-27 00:41:40 -0400710 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500714 * acpi_bus_register_driver - register a driver with the ACPI bus
715 * @driver: driver being registered
716 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500718 * devices that match the driver's criteria and binds. Returns zero for
719 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Len Brown4be44fc2005-08-05 00:44:28 -0400721int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Patrick Mochel1890a972006-12-07 20:56:31 +0800723 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400726 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800727 driver->drv.name = driver->name;
728 driver->drv.bus = &acpi_bus_type;
729 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Patrick Mochel1890a972006-12-07 20:56:31 +0800731 ret = driver_register(&driver->drv);
732 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Len Brown4be44fc2005-08-05 00:44:28 -0400735EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500738 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
739 * @driver: driver to unregister
740 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * Unregisters a driver with the ACPI bus. Searches the namespace for all
742 * devices that match the driver's criteria and unbinds.
743 */
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400744void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Patrick Mochel1890a972006-12-07 20:56:31 +0800746 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
Len Brown4be44fc2005-08-05 00:44:28 -0400748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749EXPORT_SYMBOL(acpi_bus_unregister_driver);
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751/* --------------------------------------------------------------------------
752 Device Enumeration
753 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000754static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
755{
756 acpi_status status;
757 int ret;
758 struct acpi_device *device;
759
760 /*
761 * Fixed hardware devices do not appear in the namespace and do not
762 * have handles, but we fabricate acpi_devices for them, so we have
763 * to deal with them specially.
764 */
765 if (handle == NULL)
766 return acpi_root;
767
768 do {
769 status = acpi_get_parent(handle, &handle);
770 if (status == AE_NULL_ENTRY)
771 return NULL;
772 if (ACPI_FAILURE(status))
773 return acpi_root;
774
775 ret = acpi_bus_get_device(handle, &device);
776 if (ret == 0)
777 return device;
778 } while (1);
779}
780
Len Brownc8f7a622006-07-09 17:22:28 -0400781acpi_status
782acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
783{
784 acpi_status status;
785 acpi_handle tmp;
786 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
787 union acpi_object *obj;
788
789 status = acpi_get_handle(handle, "_EJD", &tmp);
790 if (ACPI_FAILURE(status))
791 return status;
792
793 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
794 if (ACPI_SUCCESS(status)) {
795 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100796 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
797 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400798 kfree(buffer.pointer);
799 }
800 return status;
801}
802EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
803
Bob Moore8e4319c2009-06-29 13:43:27 +0800804void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806
807 /* TBD */
808
809 return;
810}
811
Zhang Rui9e89dde2006-12-07 20:56:16 +0800812static int acpi_bus_get_perf_flags(struct acpi_device *device)
813{
814 device->performance.state = ACPI_STATE_UNKNOWN;
815 return 0;
816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100819acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
820 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100822 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
823 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100825 acpi_status status;
826 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100828 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return AE_BAD_PARAMETER;
830
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100831 /* _PRW */
832 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
833 if (ACPI_FAILURE(status)) {
834 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
835 return status;
836 }
837
838 package = (union acpi_object *)buffer.pointer;
839
840 if (!package || (package->package.count < 2)) {
841 status = AE_BAD_DATA;
842 goto out;
843 }
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100846 if (!element) {
847 status = AE_BAD_DATA;
848 goto out;
849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (element->type == ACPI_TYPE_PACKAGE) {
851 if ((element->package.count < 2) ||
852 (element->package.elements[0].type !=
853 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100854 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
855 status = AE_BAD_DATA;
856 goto out;
857 }
858 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100860 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 (u32) element->package.elements[1].integer.value;
862 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100863 wakeup->gpe_device = NULL;
864 wakeup->gpe_number = element->integer.value;
865 } else {
866 status = AE_BAD_DATA;
867 goto out;
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 element = &(package->package.elements[1]);
871 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100872 status = AE_BAD_DATA;
873 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100875 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100878 status = AE_NO_MEMORY;
879 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100881 wakeup->resources.count = package->package.count - 2;
882 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100884 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
885 status = AE_BAD_DATA;
886 goto out;
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100889 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
Lin Mingbba63a22010-12-13 13:39:17 +0800892 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200893
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100894 out:
895 kfree(buffer.pointer);
896
897 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898}
899
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100900static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200902 struct acpi_device_id button_device_ids[] = {
903 {"PNP0C0D", 0},
904 {"PNP0C0C", 0},
905 {"PNP0C0E", 0},
906 {"", 0},
907 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100908 acpi_status status;
909 acpi_event_status event_status;
910
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100911 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100912
913 /* Power button, Lid switch always enable wakeup */
914 if (!acpi_match_device_ids(device, button_device_ids)) {
915 device->wakeup.flags.run_wake = 1;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100916 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100917 return;
918 }
919
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200920 status = acpi_get_gpe_status(device->wakeup.gpe_device,
921 device->wakeup.gpe_number,
922 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100923 if (status == AE_OK)
924 device->wakeup.flags.run_wake =
925 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
926}
927
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100928static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100929{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100930 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100931 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100932 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200933
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100934 /* Presence of _PRW indicates wake capable */
935 status = acpi_get_handle(device->handle, "_PRW", &temp);
936 if (ACPI_FAILURE(status))
937 return;
938
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100939 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
940 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (ACPI_FAILURE(status)) {
942 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100943 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200947 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100948 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800949 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
950 * system for the ACPI device with the _PRW object.
951 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
952 * So it is necessary to call _DSW object first. Only when it is not
953 * present will the _PSW object used.
954 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200955 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
956 if (psw_error)
957 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
958 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
Rafael J. Wysockibf325f952010-11-25 00:10:44 +0100961static void acpi_bus_add_power_resource(acpi_handle handle);
962
Zhang Rui9e89dde2006-12-07 20:56:16 +0800963static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800965 acpi_status status = 0;
966 acpi_handle handle = NULL;
967 u32 i = 0;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800971 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800973 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800975 device->power.flags.explicit_get = 1;
976 status = acpi_get_handle(device->handle, "_IRC", &handle);
977 if (ACPI_SUCCESS(status))
978 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800981 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 */
Lin Ming1cc0c992012-04-23 09:03:49 +0800983 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800984 struct acpi_device_power_state *ps = &device->power.states[i];
985 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Zhang Rui9e89dde2006-12-07 20:56:16 +0800987 /* Evaluate "_PRx" to se if power resources are referenced */
988 acpi_evaluate_reference(device->handle, object_name, NULL,
989 &ps->resources);
990 if (ps->resources.count) {
Rafael J. Wysockibf325f952010-11-25 00:10:44 +0100991 int j;
992
Zhang Rui9e89dde2006-12-07 20:56:16 +0800993 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f952010-11-25 00:10:44 +0100994 for (j = 0; j < ps->resources.count; j++)
995 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Zhang Rui9e89dde2006-12-07 20:56:16 +0800998 /* Evaluate "_PSx" to see if we can do explicit sets */
999 object_name[2] = 'S';
1000 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +08001001 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001002 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001003
Lin Ming1cc0c992012-04-23 09:03:49 +08001004 /*
1005 * State is valid if there are means to put the device into it.
1006 * D3hot is only valid if _PR3 present.
1007 */
1008 if (ps->resources.count ||
1009 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001010 ps->flags.valid = 1;
1011
1012 ps->power = -1; /* Unknown - driver assigned */
1013 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Zhang Rui9e89dde2006-12-07 20:56:16 +08001016 /* Set defaults for D0 and D3 states (always valid) */
1017 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1018 device->power.states[ACPI_STATE_D0].power = 100;
1019 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1020 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001022 /* Set D3cold's explicit_set flag if _PS3 exists. */
1023 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1024 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1025
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +01001026 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 return 0;
1029}
1030
Len Brown4be44fc2005-08-05 00:44:28 -04001031static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
Len Brown4be44fc2005-08-05 00:44:28 -04001033 acpi_status status = AE_OK;
1034 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /* Presence of _STA indicates 'dynamic_status' */
1038 status = acpi_get_handle(device->handle, "_STA", &temp);
1039 if (ACPI_SUCCESS(status))
1040 device->flags.dynamic_status = 1;
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* Presence of _RMV indicates 'removable' */
1043 status = acpi_get_handle(device->handle, "_RMV", &temp);
1044 if (ACPI_SUCCESS(status))
1045 device->flags.removable = 1;
1046
1047 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1048 status = acpi_get_handle(device->handle, "_EJD", &temp);
1049 if (ACPI_SUCCESS(status))
1050 device->flags.ejectable = 1;
1051 else {
1052 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1053 if (ACPI_SUCCESS(status))
1054 device->flags.ejectable = 1;
1055 }
1056
1057 /* Presence of _LCK indicates 'lockable' */
1058 status = acpi_get_handle(device->handle, "_LCK", &temp);
1059 if (ACPI_SUCCESS(status))
1060 device->flags.lockable = 1;
1061
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +02001062 /* Power resources cannot be power manageable. */
1063 if (device->device_type == ACPI_BUS_TYPE_POWER)
1064 return 0;
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1067 status = acpi_get_handle(device->handle, "_PS0", &temp);
1068 if (ACPI_FAILURE(status))
1069 status = acpi_get_handle(device->handle, "_PR0", &temp);
1070 if (ACPI_SUCCESS(status))
1071 device->flags.power_manageable = 1;
1072
Joe Perches3c5f9be42008-02-03 17:06:17 +02001073 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Patrick Mocheld550d982006-06-27 00:41:40 -04001075 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001078static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Len Brown4be44fc2005-08-05 00:44:28 -04001080 char bus_id[5] = { '?', 0 };
1081 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1082 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /*
1085 * Bus ID
1086 * ------
1087 * The device's Bus ID is simply the object name.
1088 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1089 */
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001090 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001092 return;
1093 }
1094
1095 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 case ACPI_BUS_TYPE_POWER_BUTTON:
1097 strcpy(device->pnp.bus_id, "PWRF");
1098 break;
1099 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1100 strcpy(device->pnp.bus_id, "SLPF");
1101 break;
1102 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001103 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /* Clean up trailing underscores (if any) */
1105 for (i = 3; i > 1; i--) {
1106 if (bus_id[i] == '_')
1107 bus_id[i] = '\0';
1108 else
1109 break;
1110 }
1111 strcpy(device->pnp.bus_id, bus_id);
1112 break;
1113 }
1114}
1115
Zhang Rui54735262007-01-11 02:09:09 -05001116/*
1117 * acpi_bay_match - see if a device is an ejectable driver bay
1118 *
1119 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1120 * then we can safely call it an ejectable drive bay
1121 */
1122static int acpi_bay_match(struct acpi_device *device){
1123 acpi_status status;
1124 acpi_handle handle;
1125 acpi_handle tmp;
1126 acpi_handle phandle;
1127
1128 handle = device->handle;
1129
1130 status = acpi_get_handle(handle, "_EJ0", &tmp);
1131 if (ACPI_FAILURE(status))
1132 return -ENODEV;
1133
1134 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1135 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1136 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1137 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1138 return 0;
1139
1140 if (acpi_get_parent(handle, &phandle))
1141 return -ENODEV;
1142
1143 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1144 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1145 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1146 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1147 return 0;
1148
1149 return -ENODEV;
1150}
1151
Frank Seidel3620f2f2007-12-07 13:20:34 +01001152/*
1153 * acpi_dock_match - see if a device has a _DCK method
1154 */
1155static int acpi_dock_match(struct acpi_device *device)
1156{
1157 acpi_handle tmp;
1158 return acpi_get_handle(device->handle, "_DCK", &tmp);
1159}
1160
Thomas Renninger620e1122010-10-01 10:54:00 +02001161const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001162{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001163 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001164
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001165 if (list_empty(&device->pnp.ids))
1166 return dummy_hid;
1167
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001168 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1169 return hid->id;
1170}
1171EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001172
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001173static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1174{
1175 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001176
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001177 id = kmalloc(sizeof(*id), GFP_KERNEL);
1178 if (!id)
1179 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001180
Thomas Meyer581de592011-08-06 11:32:56 +02001181 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001182 if (!id->id) {
1183 kfree(id);
1184 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001185 }
1186
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001187 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001188}
1189
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001190/*
1191 * Old IBM workstations have a DSDT bug wherein the SMBus object
1192 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1193 * prefix. Work around this.
1194 */
1195static int acpi_ibm_smbus_match(struct acpi_device *device)
1196{
1197 acpi_handle h_dummy;
1198 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1199 int result;
1200
1201 if (!dmi_name_in_vendors("IBM"))
1202 return -ENODEV;
1203
1204 /* Look for SMBS object */
1205 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1206 if (result)
1207 return result;
1208
1209 if (strcmp("SMBS", path.pointer)) {
1210 result = -ENODEV;
1211 goto out;
1212 }
1213
1214 /* Does it have the necessary (but misnamed) methods? */
1215 result = -ENODEV;
1216 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1217 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1218 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1219 result = 0;
1220out:
1221 kfree(path.pointer);
1222 return result;
1223}
1224
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001225static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Len Brown4be44fc2005-08-05 00:44:28 -04001227 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001228 struct acpi_device_info *info;
1229 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001230 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001232 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001234 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001235 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001236 break;
1237 }
1238
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001239 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001241 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return;
1243 }
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001246 acpi_add_id(device, info->hardware_id.string);
1247 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001248 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001249 for (i = 0; i < cid_list->count; i++)
1250 acpi_add_id(device, cid_list->ids[i].string);
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (info->valid & ACPI_VALID_ADR) {
1253 device->pnp.bus_address = info->address;
1254 device->flags.bus_address = 1;
1255 }
Zhang Ruiae843332006-12-07 20:57:10 +08001256
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001257 kfree(info);
1258
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001259 /*
1260 * Some devices don't reliably have _HIDs & _CIDs, so add
1261 * synthetic HIDs to make sure drivers can find them.
1262 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001263 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001264 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001265 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001266 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001267 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001268 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001269 else if (!acpi_ibm_smbus_match(device))
1270 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001271 else if (!acpi_device_hid(device) &&
1272 ACPI_IS_ROOT_DEVICE(device->parent)) {
1273 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1274 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1275 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1276 }
Zhang Rui54735262007-01-11 02:09:09 -05001277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
1279 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001280 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 break;
1282 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001283 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001286 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
1288 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001289 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 break;
1291 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001292 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 break;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001297static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001299 acpi_status status;
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /*
1302 * Context
1303 * -------
1304 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001305 * resolutions from handle->device very efficient. Fixed hardware
1306 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001308 if (!device->handle)
1309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001311 status = acpi_attach_data(device->handle,
1312 acpi_bus_data_handler, device);
1313 if (ACPI_SUCCESS(status))
1314 return 0;
1315
1316 printk(KERN_ERR PREFIX "Error attaching device data\n");
1317 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
Len Brown4be44fc2005-08-05 00:44:28 -04001320static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001323 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Li Shaohua96333572006-12-07 20:56:46 +08001325 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001326 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Rui Zhang2786f6e2006-12-21 02:21:13 -05001331 /*
1332 * unbind _ADR-Based Devices when hot removal
1333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (dev->flags.bus_address) {
1335 if ((dev->parent) && (dev->parent->ops.unbind))
1336 dev->parent->ops.unbind(dev);
1337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1339
Patrick Mocheld550d982006-06-27 00:41:40 -04001340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001343static int acpi_add_single_object(struct acpi_device **child,
1344 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001345 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001346 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001348 int result;
1349 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001350 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Burman Yan36bcbec2006-12-19 12:56:11 -08001352 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001354 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001355 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001358 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001359 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001361 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001362 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001363 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001364
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001365 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 /*
1368 * Flags
1369 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001370 * Note that we only look for object handles -- cannot evaluate objects
1371 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 */
1373 result = acpi_bus_get_flags(device);
1374 if (result)
1375 goto end;
1376
1377 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * Initialize Device
1379 * -----------------
1380 * TBD: Synch with Core's enumeration/initialization process.
1381 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001382 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /*
1385 * Power Management
1386 * ----------------
1387 */
1388 if (device->flags.power_manageable) {
1389 result = acpi_bus_get_power_flags(device);
1390 if (result)
1391 goto end;
1392 }
1393
Len Brown4be44fc2005-08-05 00:44:28 -04001394 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 * Wakeup device management
1396 *-----------------------
1397 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001398 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 /*
1401 * Performance Management
1402 * ----------------------
1403 */
1404 if (device->flags.performance_manageable) {
1405 result = acpi_bus_get_perf_flags(device);
1406 if (result)
1407 goto end;
1408 }
1409
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001410 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001411 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001413 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001416 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
1418 if (device->flags.bus_address) {
1419 if (device->parent && device->parent->ops.bind)
1420 device->parent->ops.bind(device);
1421 }
1422
Alex Chiang0c526d92009-05-14 08:31:37 -06001423end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001424 if (!result) {
1425 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1426 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1427 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1428 (char *) buffer.pointer,
1429 device->parent ? dev_name(&device->parent->dev) :
1430 "(null)"));
1431 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001433 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001434 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Patrick Mocheld550d982006-06-27 00:41:40 -04001436 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001439#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1440 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1441
Rafael J. Wysockibf325f952010-11-25 00:10:44 +01001442static void acpi_bus_add_power_resource(acpi_handle handle)
1443{
1444 struct acpi_bus_ops ops = {
1445 .acpi_op_add = 1,
1446 .acpi_op_start = 1,
1447 };
1448 struct acpi_device *device = NULL;
1449
1450 acpi_bus_get_device(handle, &device);
1451 if (!device)
1452 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1453 ACPI_STA_DEFAULT, &ops);
1454}
1455
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001456static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1457 unsigned long long *sta)
1458{
1459 acpi_status status;
1460 acpi_object_type acpi_type;
1461
1462 status = acpi_get_type(handle, &acpi_type);
1463 if (ACPI_FAILURE(status))
1464 return -ENODEV;
1465
1466 switch (acpi_type) {
1467 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1468 case ACPI_TYPE_DEVICE:
1469 *type = ACPI_BUS_TYPE_DEVICE;
1470 status = acpi_bus_get_status_handle(handle, sta);
1471 if (ACPI_FAILURE(status))
1472 return -ENODEV;
1473 break;
1474 case ACPI_TYPE_PROCESSOR:
1475 *type = ACPI_BUS_TYPE_PROCESSOR;
1476 status = acpi_bus_get_status_handle(handle, sta);
1477 if (ACPI_FAILURE(status))
1478 return -ENODEV;
1479 break;
1480 case ACPI_TYPE_THERMAL:
1481 *type = ACPI_BUS_TYPE_THERMAL;
1482 *sta = ACPI_STA_DEFAULT;
1483 break;
1484 case ACPI_TYPE_POWER:
1485 *type = ACPI_BUS_TYPE_POWER;
1486 *sta = ACPI_STA_DEFAULT;
1487 break;
1488 default:
1489 return -ENODEV;
1490 }
1491
1492 return 0;
1493}
1494
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001495static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1496 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001498 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001499 int type;
1500 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001501 struct acpi_device *device;
1502 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001503 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001504
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001505 result = acpi_bus_type_and_status(handle, &type, &sta);
1506 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001507 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001509 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001510 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001511 struct acpi_device_wakeup wakeup;
1512 acpi_handle temp;
1513
1514 status = acpi_get_handle(handle, "_PRW", &temp);
1515 if (ACPI_SUCCESS(status))
1516 acpi_bus_extract_wakeup_device_power_package(handle,
1517 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001518 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001521 /*
1522 * We may already have an acpi_device from a previous enumeration. If
1523 * so, we needn't add it again, but we may still have to start it.
1524 */
1525 device = NULL;
1526 acpi_bus_get_device(handle, &device);
Mika Westerberg91e56872012-10-31 22:45:02 +01001527 if (ops->acpi_op_add && !device) {
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001528 acpi_add_single_object(&device, handle, type, sta, ops);
Mika Westerberg91e56872012-10-31 22:45:02 +01001529 /* Is the device a known good platform device? */
1530 if (device
1531 && !acpi_match_device_ids(device, acpi_platform_device_ids))
1532 acpi_create_platform_device(device);
1533 }
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001534
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001535 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001536 return AE_CTRL_DEPTH;
1537
1538 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1539 status = acpi_start_single_object(device);
1540 if (ACPI_FAILURE(status))
1541 return AE_CTRL_DEPTH;
1542 }
1543
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001544 if (!*return_value)
1545 *return_value = device;
1546 return AE_OK;
1547}
1548
1549static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1550 struct acpi_device **child)
1551{
1552 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001553 void *device = NULL;
1554
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001555 status = acpi_bus_check_add(handle, 0, ops, &device);
1556 if (ACPI_SUCCESS(status))
1557 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001558 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001559
1560 if (child)
1561 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001562
1563 if (device)
1564 return 0;
1565 else
1566 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Thomas Renninger77796882010-01-29 17:48:52 +01001569/*
1570 * acpi_bus_add and acpi_bus_start
1571 *
1572 * scan a given ACPI tree and (probably recently hot-plugged)
1573 * create and add or starts found devices.
1574 *
1575 * If no devices were found -ENODEV is returned which does not
1576 * mean that this is a real error, there just have been no suitable
1577 * ACPI objects in the table trunk from which the kernel could create
1578 * a device and add/start an appropriate driver.
1579 */
1580
Rajesh Shah3fb02732005-04-28 00:25:52 -07001581int
Len Brown4be44fc2005-08-05 00:44:28 -04001582acpi_bus_add(struct acpi_device **child,
1583 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001584{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001585 struct acpi_bus_ops ops;
1586
Li Shaohuac4168bf2006-12-07 20:56:41 +08001587 memset(&ops, 0, sizeof(ops));
1588 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001589
Thomas Renninger77796882010-01-29 17:48:52 +01001590 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001591}
1592EXPORT_SYMBOL(acpi_bus_add);
1593
Len Brown4be44fc2005-08-05 00:44:28 -04001594int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001595{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001596 struct acpi_bus_ops ops;
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001597 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001598
Thomas Renningerd2f66502010-01-29 17:48:51 +01001599 if (!device)
1600 return -EINVAL;
1601
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001602 memset(&ops, 0, sizeof(ops));
1603 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001604
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001605 result = acpi_bus_scan(device->handle, &ops, NULL);
1606
Lin Ming3a378982010-12-13 13:36:15 +08001607 acpi_update_all_gpes();
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001608
1609 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001610}
1611EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Kristen Accardiceaba662006-02-23 17:56:01 -08001613int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Len Brown4be44fc2005-08-05 00:44:28 -04001615 acpi_status status;
1616 struct acpi_device *parent, *child;
1617 acpi_handle phandle, chandle;
1618 acpi_object_type type;
1619 u32 level = 1;
1620 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Len Brown4be44fc2005-08-05 00:44:28 -04001622 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 phandle = start->handle;
1624 child = chandle = NULL;
1625
1626 while ((level > 0) && parent && (!err)) {
1627 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001628 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630 /*
1631 * If this scope is exhausted then move our way back up.
1632 */
1633 if (ACPI_FAILURE(status)) {
1634 level--;
1635 chandle = phandle;
1636 acpi_get_parent(phandle, &phandle);
1637 child = parent;
1638 parent = parent->parent;
1639
1640 if (level == 0)
1641 err = acpi_bus_remove(child, rmdevice);
1642 else
1643 err = acpi_bus_remove(child, 1);
1644
1645 continue;
1646 }
1647
1648 status = acpi_get_type(chandle, &type);
1649 if (ACPI_FAILURE(status)) {
1650 continue;
1651 }
1652 /*
1653 * If there is a device corresponding to chandle then
1654 * parse it (depth-first).
1655 */
1656 if (acpi_bus_get_device(chandle, &child) == 0) {
1657 level++;
1658 phandle = chandle;
1659 chandle = NULL;
1660 parent = child;
1661 }
1662 continue;
1663 }
1664 return err;
1665}
Kristen Accardiceaba662006-02-23 17:56:01 -08001666EXPORT_SYMBOL_GPL(acpi_bus_trim);
1667
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001668static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Len Brown4be44fc2005-08-05 00:44:28 -04001670 int result = 0;
1671 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001672 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Li Shaohuac4168bf2006-12-07 20:56:41 +08001674 memset(&ops, 0, sizeof(ops));
1675 ops.acpi_op_add = 1;
1676 ops.acpi_op_start = 1;
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /*
1679 * Enumerate all fixed-feature devices.
1680 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001681 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001682 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001683 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001684 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001685 &ops);
Daniel Drakec10d7a12012-05-10 00:08:43 +01001686 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001689 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001690 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001691 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001692 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001693 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Patrick Mocheld550d982006-06-27 00:41:40 -04001696 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
Bjorn Helgaase747f272009-03-24 16:49:43 -06001699int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
1701 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001702 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Li Shaohuac4168bf2006-12-07 20:56:41 +08001704 memset(&ops, 0, sizeof(ops));
1705 ops.acpi_op_add = 1;
1706 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Patrick Mochel5b327262006-05-10 10:33:00 -04001708 result = bus_register(&acpi_bus_type);
1709 if (result) {
1710 /* We don't want to quit even if we failed to add suspend/resume */
1711 printk(KERN_ERR PREFIX "Could not register bus type\n");
1712 }
1713
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001714 acpi_power_init();
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 * Enumerate devices in the ACPI namespace.
1718 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001719 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001720
Li Shaohuac4168bf2006-12-07 20:56:41 +08001721 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001722 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 if (result)
1725 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001726 else
Lin Ming3a378982010-12-13 13:36:15 +08001727 acpi_update_all_gpes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Patrick Mocheld550d982006-06-27 00:41:40 -04001729 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}