blob: 3db115acea50d764d3f6a2cbf1f6d8855d70e4b0 [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);
Shaohua Li90905892009-04-07 10:24:29 +080055DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056LIST_HEAD(acpi_wakeup_device_list);
57
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080058struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080059 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080060 unsigned int instance_no;
61 struct list_head node;
62};
Thomas Renninger29b71a12007-07-23 14:43:51 +020063
64/*
65 * Creates hid/cid(s) string needed for modalias and uevent
66 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
67 * char *modalias: "acpi:IBM0001:ACPI0001"
68*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020069static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
70 int size)
71{
Thomas Renninger29b71a12007-07-23 14:43:51 +020072 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080073 int count;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060074 struct acpi_hardware_id *id;
Thomas Renninger29b71a12007-07-23 14:43:51 +020075
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +020076 if (list_empty(&acpi_dev->pnp.ids))
77 return 0;
78
Zhang Rui5c9fcb52008-03-20 16:40:32 +080079 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020080 size -= len;
81
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -060082 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
83 count = snprintf(&modalias[len], size, "%s:", id->id);
Zhang Rui5c9fcb52008-03-20 16:40:32 +080084 if (count < 0 || count >= size)
85 return -EINVAL;
86 len += count;
87 size -= count;
88 }
89
Thomas Renninger29b71a12007-07-23 14:43:51 +020090 modalias[len] = '\0';
91 return len;
92}
93
94static ssize_t
95acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
96 struct acpi_device *acpi_dev = to_acpi_device(dev);
97 int len;
98
99 /* Device has no HID and no CID or string is >1024 */
100 len = create_modalias(acpi_dev, buf, 1024);
101 if (len <= 0)
102 return 0;
103 buf[len++] = '\n';
104 return len;
105}
106static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
107
Toshi Kanic4753e52012-05-23 20:25:20 -0600108/**
109 * acpi_bus_hot_remove_device: hot-remove a device and its children
110 * @context: struct acpi_eject_event pointer (freed in this func)
111 *
112 * Hot-remove a device and its children. This function frees up the
113 * memory space passed by arg context, so that the caller may call
114 * this function asynchronously through acpi_os_hotplug_execute().
115 */
116void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Toshi Kanic4753e52012-05-23 20:25:20 -0600118 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
Zhang Rui26d46862008-04-29 02:35:48 -0400119 struct acpi_device *device;
Toshi Kanic4753e52012-05-23 20:25:20 -0600120 acpi_handle handle = ej_event->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 struct acpi_object_list arg_list;
122 union acpi_object arg;
123 acpi_status status = AE_OK;
Toshi Kanic4753e52012-05-23 20:25:20 -0600124 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Zhang Rui26d46862008-04-29 02:35:48 -0400126 if (acpi_bus_get_device(handle, &device))
Toshi Kanic4753e52012-05-23 20:25:20 -0600127 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Zhang Rui26d46862008-04-29 02:35:48 -0400129 if (!device)
Toshi Kanic4753e52012-05-23 20:25:20 -0600130 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400131
132 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100133 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400134
135 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800136 printk(KERN_ERR PREFIX
137 "Removing device failed\n");
Toshi Kanic4753e52012-05-23 20:25:20 -0600138 goto err_out;
Zhang Rui26d46862008-04-29 02:35:48 -0400139 }
140
141 /* power off device */
142 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
143 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800144 printk(KERN_WARNING PREFIX
145 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400146
147 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 arg_list.count = 1;
149 arg_list.pointer = &arg;
150 arg.type = ACPI_TYPE_INTEGER;
151 arg.integer.value = 0;
152 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
153 }
154
155 arg_list.count = 1;
156 arg_list.pointer = &arg;
157 arg.type = ACPI_TYPE_INTEGER;
158 arg.integer.value = 1;
159
160 /*
161 * TBD: _EJD support.
162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Toshi Kanic4753e52012-05-23 20:25:20 -0600164 if (ACPI_FAILURE(status)) {
165 if (status != AE_NOT_FOUND)
166 printk(KERN_WARNING PREFIX
167 "Eject device failed\n");
168 goto err_out;
169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Toshi Kanic4753e52012-05-23 20:25:20 -0600171 kfree(context);
172 return;
173
174err_out:
175 /* Inform firmware the hot-remove operation has completed w/ error */
176 (void) acpi_evaluate_hotplug_ost(handle,
177 ej_event->event, ost_code, NULL);
178 kfree(context);
Zhang Ruic8d72a52009-06-22 11:31:16 +0800179 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800183acpi_eject_store(struct device *d, struct device_attribute *attr,
184 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Len Brown4be44fc2005-08-05 00:44:28 -0400186 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400187 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400188 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800189 struct acpi_device *acpi_device = to_acpi_device(d);
Toshi Kanic4753e52012-05-23 20:25:20 -0600190 struct acpi_eject_event *ej_event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if ((!count) || (buf[0] != '1')) {
193 return -EINVAL;
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800196 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 ret = -ENODEV;
198 goto err;
199 }
200#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800201 status = acpi_get_type(acpi_device->handle, &type);
202 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 ret = -ENODEV;
204 goto err;
205 }
206
Toshi Kanic4753e52012-05-23 20:25:20 -0600207 ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
208 if (!ej_event) {
209 ret = -ENOMEM;
210 goto err;
211 }
212
213 ej_event->handle = acpi_device->handle;
214 if (acpi_device->flags.eject_pending) {
215 /* event originated from ACPI eject notification */
216 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
217 acpi_device->flags.eject_pending = 0;
218 } else {
219 /* event originated from user */
220 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
221 (void) acpi_evaluate_hotplug_ost(ej_event->handle,
222 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
223 }
224
225 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
Alok N Kataria74523c92008-06-13 12:54:24 -0400226err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800230static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800232static ssize_t
233acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
234 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Bjorn Helgaasea8d82f2009-09-21 13:35:09 -0600236 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800237}
238static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
239
240static ssize_t
241acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
242 struct acpi_device *acpi_dev = to_acpi_device(dev);
243 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
244 int result;
245
246 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600247 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800248 goto end;
249
250 result = sprintf(buf, "%s\n", (char*)path.pointer);
251 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600252end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800253 return result;
254}
255static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
256
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600257/* sysfs file that shows description text from the ACPI _STR method */
258static ssize_t description_show(struct device *dev,
259 struct device_attribute *attr,
260 char *buf) {
261 struct acpi_device *acpi_dev = to_acpi_device(dev);
262 int result;
263
264 if (acpi_dev->pnp.str_obj == NULL)
265 return 0;
266
267 /*
268 * The _STR object contains a Unicode identifier for a device.
269 * We need to convert to utf-8 so it can be displayed.
270 */
271 result = utf16s_to_utf8s(
272 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
273 acpi_dev->pnp.str_obj->buffer.length,
274 UTF16_LITTLE_ENDIAN, buf,
275 PAGE_SIZE);
276
277 buf[result++] = '\n';
278
279 return result;
280}
281static DEVICE_ATTR(description, 0444, description_show, NULL);
282
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800283static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600285 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800286 acpi_status status;
287 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800288 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800290 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800291 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800292 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600293 if (dev->handle) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800294 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600295 if (result)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800296 goto end;
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200299 if (!list_empty(&dev->pnp.ids)) {
300 result = device_create_file(&dev->dev, &dev_attr_hid);
301 if (result)
302 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200304 result = device_create_file(&dev->dev, &dev_attr_modalias);
305 if (result)
306 goto end;
307 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200308
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600309 /*
310 * If device has _STR, 'description' file is created
311 */
312 status = acpi_get_handle(dev->handle, "_STR", &temp);
313 if (ACPI_SUCCESS(status)) {
314 status = acpi_evaluate_object(dev->handle, "_STR",
315 NULL, &buffer);
316 if (ACPI_FAILURE(status))
317 buffer.pointer = NULL;
318 dev->pnp.str_obj = buffer.pointer;
319 result = device_create_file(&dev->dev, &dev_attr_description);
320 if (result)
321 goto end;
322 }
323
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800324 /*
325 * If device has _EJ0, 'eject' file is created that is used to trigger
326 * hot-removal function from userland.
327 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800328 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
329 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800330 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600331end:
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800332 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800333}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800335static void acpi_device_remove_files(struct acpi_device *dev)
336{
337 acpi_status status;
338 acpi_handle temp;
339
340 /*
Lance Ortizd1efe3c2012-10-02 12:43:23 -0600341 * If device has _STR, remove 'description' file
342 */
343 status = acpi_get_handle(dev->handle, "_STR", &temp);
344 if (ACPI_SUCCESS(status)) {
345 kfree(dev->pnp.str_obj);
346 device_remove_file(&dev->dev, &dev_attr_description);
347 }
348 /*
349 * If device has _EJ0, remove 'eject' file.
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800350 */
351 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
352 if (ACPI_SUCCESS(status))
353 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800354
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600355 device_remove_file(&dev->dev, &dev_attr_modalias);
356 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600357 if (dev->handle)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800358 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800361 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200363
Mika Westerbergcf761af2012-10-31 22:44:41 +0100364static const struct acpi_device_id *__acpi_match_device(
365 struct acpi_device *device, const struct acpi_device_id *ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200366{
367 const struct acpi_device_id *id;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600368 struct acpi_hardware_id *hwid;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200369
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800370 /*
371 * If the device is not present, it is unnecessary to load device
372 * driver for it.
373 */
374 if (!device->status.present)
Mika Westerbergcf761af2012-10-31 22:44:41 +0100375 return NULL;
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800376
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600377 for (id = ids; id->id[0]; id++)
378 list_for_each_entry(hwid, &device->pnp.ids, list)
379 if (!strcmp((char *) id->id, hwid->id))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100380 return id;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200381
Mika Westerbergcf761af2012-10-31 22:44:41 +0100382 return NULL;
383}
384
385/**
386 * acpi_match_device - Match a struct device against a given list of ACPI IDs
387 * @ids: Array of struct acpi_device_id object to match against.
388 * @dev: The device structure to match.
389 *
390 * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
391 * object for that handle and use that object to match against a given list of
392 * device IDs.
393 *
394 * Return a pointer to the first matching ID on success or %NULL on failure.
395 */
396const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
397 const struct device *dev)
398{
399 struct acpi_device *adev;
400
Rafael J. Wysocki95f8a082012-11-21 00:21:50 +0100401 if (!ids || !ACPI_HANDLE(dev)
402 || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev)))
Mika Westerbergcf761af2012-10-31 22:44:41 +0100403 return NULL;
404
405 return __acpi_match_device(adev, ids);
406}
407EXPORT_SYMBOL_GPL(acpi_match_device);
408
409int acpi_match_device_ids(struct acpi_device *device,
410 const struct acpi_device_id *ids)
411{
412 return __acpi_match_device(device, ids) ? 0 : -ENOENT;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200413}
414EXPORT_SYMBOL(acpi_match_device_ids);
415
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600416static void acpi_free_ids(struct acpi_device *device)
417{
418 struct acpi_hardware_id *id, *tmp;
419
420 list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
421 kfree(id->id);
422 kfree(id);
423 }
424}
425
Patrick Mochel1890a972006-12-07 20:56:31 +0800426static void acpi_device_release(struct device *dev)
427{
428 struct acpi_device *acpi_dev = to_acpi_device(dev);
429
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -0600430 acpi_free_ids(acpi_dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800431 kfree(acpi_dev);
432}
433
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800434static int acpi_bus_match(struct device *dev, struct device_driver *drv)
435{
436 struct acpi_device *acpi_dev = to_acpi_device(dev);
437 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
438
Thomas Renninger29b71a12007-07-23 14:43:51 +0200439 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800440}
441
Kay Sievers7eff2e72007-08-14 15:15:12 +0200442static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800443{
444 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200445 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800446
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +0200447 if (list_empty(&acpi_dev->pnp.ids))
448 return 0;
449
Kay Sievers7eff2e72007-08-14 15:15:12 +0200450 if (add_uevent_var(env, "MODALIAS="))
451 return -ENOMEM;
452 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
453 sizeof(env->buf) - env->buflen);
454 if (len >= (sizeof(env->buf) - env->buflen))
455 return -ENOMEM;
456 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458}
459
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000460static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
461{
462 struct acpi_device *device = data;
463
464 device->driver->ops.notify(device, event);
465}
466
467static acpi_status acpi_device_notify_fixed(void *data)
468{
469 struct acpi_device *device = data;
470
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000471 /* Fixed hardware devices have no handles */
472 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000473 return AE_OK;
474}
475
476static int acpi_device_install_notify_handler(struct acpi_device *device)
477{
478 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000479
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000480 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000481 status =
482 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
483 acpi_device_notify_fixed,
484 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000485 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000486 status =
487 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
488 acpi_device_notify_fixed,
489 device);
490 else
491 status = acpi_install_notify_handler(device->handle,
492 ACPI_DEVICE_NOTIFY,
493 acpi_device_notify,
494 device);
495
496 if (ACPI_FAILURE(status))
497 return -EINVAL;
498 return 0;
499}
500
501static void acpi_device_remove_notify_handler(struct acpi_device *device)
502{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000503 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000504 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
505 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000506 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000507 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
508 acpi_device_notify_fixed);
509 else
510 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
511 acpi_device_notify);
512}
513
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800514static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
515static int acpi_start_single_object(struct acpi_device *);
516static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800517{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800518 struct acpi_device *acpi_dev = to_acpi_device(dev);
519 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
520 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800522 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
523 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800524 if (acpi_dev->bus_ops.acpi_op_start)
525 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000526
527 if (acpi_drv->ops.notify) {
528 ret = acpi_device_install_notify_handler(acpi_dev);
529 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000530 if (acpi_drv->ops.remove)
531 acpi_drv->ops.remove(acpi_dev,
532 acpi_dev->removal_type);
533 return ret;
534 }
535 }
536
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800537 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
538 "Found driver [%s] for device [%s]\n",
539 acpi_drv->name, acpi_dev->pnp.bus_id));
540 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800541 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800542 return ret;
543}
544
545static int acpi_device_remove(struct device * dev)
546{
547 struct acpi_device *acpi_dev = to_acpi_device(dev);
548 struct acpi_driver *acpi_drv = acpi_dev->driver;
549
550 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000551 if (acpi_drv->ops.notify)
552 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800553 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800554 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800555 }
556 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700557 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800558
559 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800560 return 0;
561}
562
David Brownell55955aa2007-05-08 00:28:35 -0700563struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800564 .name = "acpi",
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800565 .match = acpi_bus_match,
566 .probe = acpi_device_probe,
567 .remove = acpi_device_remove,
568 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569};
570
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000571static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800573 int result;
574 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
575 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /*
578 * Linkage
579 * -------
580 * Link this device to its parent and siblings.
581 */
582 INIT_LIST_HEAD(&device->children);
583 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 INIT_LIST_HEAD(&device->wakeup_list);
Lan Tianyu1033f902012-08-17 14:44:09 +0800585 INIT_LIST_HEAD(&device->physical_node_list);
586 mutex_init(&device->physical_node_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800588 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
589 if (!new_bus_id) {
590 printk(KERN_ERR PREFIX "Memory allocation error\n");
591 return -ENOMEM;
592 }
593
Shaohua Li90905892009-04-07 10:24:29 +0800594 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800595 /*
596 * Find suitable bus_id and instance number in acpi_bus_id_list
597 * If failed, create one and link it into acpi_bus_id_list
598 */
599 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600600 if (!strcmp(acpi_device_bus_id->bus_id,
601 acpi_device_hid(device))) {
602 acpi_device_bus_id->instance_no++;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800603 found = 1;
604 kfree(new_bus_id);
605 break;
606 }
607 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600608 if (!found) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800609 acpi_device_bus_id = new_bus_id;
Bjorn Helgaas1131b932009-09-21 13:35:29 -0600610 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800611 acpi_device_bus_id->instance_no = 0;
612 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
613 }
Kay Sievers07944692008-10-30 01:18:59 +0100614 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 +0800615
Len Brown33b57152008-12-15 22:09:26 -0500616 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (device->wakeup.flags.valid)
620 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800621 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Patrick Mochel1890a972006-12-07 20:56:31 +0800623 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000624 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800625 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800626 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600627 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600628 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600629 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800630 goto end;
631 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800632
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800633 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600634 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100635 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
636 dev_name(&device->dev));
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800637
Li Shaohua96333572006-12-07 20:56:46 +0800638 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800639 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600640end:
Shaohua Li90905892009-04-07 10:24:29 +0800641 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500642 if (device->parent)
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800643 list_del(&device->node);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800644 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800645 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800646 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649static void acpi_device_unregister(struct acpi_device *device, int type)
650{
Shaohua Li90905892009-04-07 10:24:29 +0800651 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500652 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800656 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800659
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800660 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800661 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Zhang Rui9e89dde2006-12-07 20:56:16 +0800664/* --------------------------------------------------------------------------
665 Driver Management
666 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500668 * acpi_bus_driver_init - add a device to a driver
669 * @device: the device to add and initialize
670 * @driver: driver for the device
671 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600672 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800673 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
675static int
Len Brown4be44fc2005-08-05 00:44:28 -0400676acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Len Brown4be44fc2005-08-05 00:44:28 -0400678 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400681 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400684 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 result = driver->ops.add(device);
687 if (result) {
688 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700689 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400690 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
692
693 device->driver = driver;
694
695 /*
696 * TBD - Configuration Management: Assign resources to device based
697 * upon possible configuration and currently allocated resources.
698 */
699
Len Brown4be44fc2005-08-05 00:44:28 -0400700 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
701 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400702 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700703}
704
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400705static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700706{
707 int result = 0;
708 struct acpi_driver *driver;
709
Rajesh Shah3fb02732005-04-28 00:25:52 -0700710
711 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400712 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (driver->ops.start) {
715 result = driver->ops.start(device);
716 if (result && driver->ops.remove)
717 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
Patrick Mocheld550d982006-06-27 00:41:40 -0400720 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500724 * acpi_bus_register_driver - register a driver with the ACPI bus
725 * @driver: driver being registered
726 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500728 * devices that match the driver's criteria and binds. Returns zero for
729 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Len Brown4be44fc2005-08-05 00:44:28 -0400731int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Patrick Mochel1890a972006-12-07 20:56:31 +0800733 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400736 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800737 driver->drv.name = driver->name;
738 driver->drv.bus = &acpi_bus_type;
739 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Patrick Mochel1890a972006-12-07 20:56:31 +0800741 ret = driver_register(&driver->drv);
742 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Len Brown4be44fc2005-08-05 00:44:28 -0400745EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500748 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
749 * @driver: driver to unregister
750 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * Unregisters a driver with the ACPI bus. Searches the namespace for all
752 * devices that match the driver's criteria and unbinds.
753 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400754void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Patrick Mochel1890a972006-12-07 20:56:31 +0800756 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
Len Brown4be44fc2005-08-05 00:44:28 -0400758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759EXPORT_SYMBOL(acpi_bus_unregister_driver);
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761/* --------------------------------------------------------------------------
762 Device Enumeration
763 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000764static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
765{
766 acpi_status status;
767 int ret;
768 struct acpi_device *device;
769
770 /*
771 * Fixed hardware devices do not appear in the namespace and do not
772 * have handles, but we fabricate acpi_devices for them, so we have
773 * to deal with them specially.
774 */
775 if (handle == NULL)
776 return acpi_root;
777
778 do {
779 status = acpi_get_parent(handle, &handle);
780 if (status == AE_NULL_ENTRY)
781 return NULL;
782 if (ACPI_FAILURE(status))
783 return acpi_root;
784
785 ret = acpi_bus_get_device(handle, &device);
786 if (ret == 0)
787 return device;
788 } while (1);
789}
790
Len Brownc8f7a622006-07-09 17:22:28 -0400791acpi_status
792acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
793{
794 acpi_status status;
795 acpi_handle tmp;
796 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
797 union acpi_object *obj;
798
799 status = acpi_get_handle(handle, "_EJD", &tmp);
800 if (ACPI_FAILURE(status))
801 return status;
802
803 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
804 if (ACPI_SUCCESS(status)) {
805 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100806 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
807 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400808 kfree(buffer.pointer);
809 }
810 return status;
811}
812EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
813
Bob Moore8e4319c2009-06-29 13:43:27 +0800814void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816
817 /* TBD */
818
819 return;
820}
821
Zhang Rui9e89dde2006-12-07 20:56:16 +0800822static int acpi_bus_get_perf_flags(struct acpi_device *device)
823{
824 device->performance.state = ACPI_STATE_UNKNOWN;
825 return 0;
826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828static acpi_status
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100829acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
830 struct acpi_device_wakeup *wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100832 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
833 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 union acpi_object *element = NULL;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100835 acpi_status status;
836 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100838 if (!wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return AE_BAD_PARAMETER;
840
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100841 /* _PRW */
842 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
843 if (ACPI_FAILURE(status)) {
844 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
845 return status;
846 }
847
848 package = (union acpi_object *)buffer.pointer;
849
850 if (!package || (package->package.count < 2)) {
851 status = AE_BAD_DATA;
852 goto out;
853 }
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 element = &(package->package.elements[0]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100856 if (!element) {
857 status = AE_BAD_DATA;
858 goto out;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (element->type == ACPI_TYPE_PACKAGE) {
861 if ((element->package.count < 2) ||
862 (element->package.elements[0].type !=
863 ACPI_TYPE_LOCAL_REFERENCE)
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100864 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
865 status = AE_BAD_DATA;
866 goto out;
867 }
868 wakeup->gpe_device =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 element->package.elements[0].reference.handle;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100870 wakeup->gpe_number =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 (u32) element->package.elements[1].integer.value;
872 } else if (element->type == ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100873 wakeup->gpe_device = NULL;
874 wakeup->gpe_number = element->integer.value;
875 } else {
876 status = AE_BAD_DATA;
877 goto out;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 element = &(package->package.elements[1]);
881 if (element->type != ACPI_TYPE_INTEGER) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100882 status = AE_BAD_DATA;
883 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100885 wakeup->sleep_state = element->integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100888 status = AE_NO_MEMORY;
889 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100891 wakeup->resources.count = package->package.count - 2;
892 for (i = 0; i < wakeup->resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 element = &(package->package.elements[i + 2]);
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100894 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
895 status = AE_BAD_DATA;
896 goto out;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100899 wakeup->resources.handles[i] = element->reference.handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901
Lin Mingbba63a22010-12-13 13:39:17 +0800902 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
Rafael J. Wysocki98746472010-07-08 00:43:36 +0200903
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100904 out:
905 kfree(buffer.pointer);
906
907 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100910static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Thomas Renninger29b71a12007-07-23 14:43:51 +0200912 struct acpi_device_id button_device_ids[] = {
913 {"PNP0C0D", 0},
914 {"PNP0C0C", 0},
915 {"PNP0C0E", 0},
916 {"", 0},
917 };
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100918 acpi_status status;
919 acpi_event_status event_status;
920
Rafael J. Wysockib67ea762010-02-17 23:44:09 +0100921 device->wakeup.flags.notifier_present = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100922
923 /* Power button, Lid switch always enable wakeup */
924 if (!acpi_match_device_ids(device, button_device_ids)) {
925 device->wakeup.flags.run_wake = 1;
Rafael J. Wysockif2b56bc2011-01-06 23:34:22 +0100926 device_set_wakeup_capable(&device->dev, true);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100927 return;
928 }
929
Rafael J. Wysockie8e18c92010-07-08 00:42:51 +0200930 status = acpi_get_gpe_status(device->wakeup.gpe_device,
931 device->wakeup.gpe_number,
932 &event_status);
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100933 if (status == AE_OK)
934 device->wakeup.flags.run_wake =
935 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
936}
937
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100938static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100939{
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100940 acpi_handle temp;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100941 acpi_status status = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100942 int psw_error;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200943
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100944 /* Presence of _PRW indicates wake capable */
945 status = acpi_get_handle(device->handle, "_PRW", &temp);
946 if (ACPI_FAILURE(status))
947 return;
948
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +0100949 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
950 &device->wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (ACPI_FAILURE(status)) {
952 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +0100953 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200957 device->wakeup.prepare_count = 0;
Rafael J. Wysockif5177092010-02-17 23:41:49 +0100958 acpi_bus_set_run_wake_flags(device);
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800959 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
960 * system for the ACPI device with the _PRW object.
961 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
962 * So it is necessary to call _DSW object first. Only when it is not
963 * present will the _PSW object used.
964 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200965 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
966 if (psw_error)
967 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
968 "error in _DSW or _PSW evaluation\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Rafael J. Wysockibf325f92010-11-25 00:10:44 +0100971static void acpi_bus_add_power_resource(acpi_handle handle);
972
Zhang Rui9e89dde2006-12-07 20:56:16 +0800973static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800975 acpi_status status = 0;
976 acpi_handle handle = NULL;
977 u32 i = 0;
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800981 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800983 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800985 device->power.flags.explicit_get = 1;
986 status = acpi_get_handle(device->handle, "_IRC", &handle);
987 if (ACPI_SUCCESS(status))
988 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800991 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
Lin Ming1cc0c992012-04-23 09:03:49 +0800993 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800994 struct acpi_device_power_state *ps = &device->power.states[i];
995 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Zhang Rui9e89dde2006-12-07 20:56:16 +0800997 /* Evaluate "_PRx" to se if power resources are referenced */
998 acpi_evaluate_reference(device->handle, object_name, NULL,
999 &ps->resources);
1000 if (ps->resources.count) {
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001001 int j;
1002
Zhang Rui9e89dde2006-12-07 20:56:16 +08001003 device->power.flags.power_resources = 1;
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001004 for (j = 0; j < ps->resources.count; j++)
1005 acpi_bus_add_power_resource(ps->resources.handles[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Zhang Rui9e89dde2006-12-07 20:56:16 +08001008 /* Evaluate "_PSx" to see if we can do explicit sets */
1009 object_name[2] = 'S';
1010 status = acpi_get_handle(device->handle, object_name, &handle);
Alex He37239972012-02-21 16:58:10 +08001011 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +08001012 ps->flags.explicit_set = 1;
Zhang Rui9e89dde2006-12-07 20:56:16 +08001013
Lin Ming1cc0c992012-04-23 09:03:49 +08001014 /*
1015 * State is valid if there are means to put the device into it.
1016 * D3hot is only valid if _PR3 present.
1017 */
1018 if (ps->resources.count ||
Aaron Lu1399dfc2012-11-21 23:33:40 +01001019 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) {
Zhang Rui9e89dde2006-12-07 20:56:16 +08001020 ps->flags.valid = 1;
Aaron Lu1399dfc2012-11-21 23:33:40 +01001021 ps->flags.os_accessible = 1;
1022 }
Zhang Rui9e89dde2006-12-07 20:56:16 +08001023
1024 ps->power = -1; /* Unknown - driver assigned */
1025 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Zhang Rui9e89dde2006-12-07 20:56:16 +08001028 /* Set defaults for D0 and D3 states (always valid) */
1029 device->power.states[ACPI_STATE_D0].flags.valid = 1;
1030 device->power.states[ACPI_STATE_D0].power = 100;
1031 device->power.states[ACPI_STATE_D3].flags.valid = 1;
1032 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Rafael J. Wysocki5c7dd712012-05-18 00:39:35 +02001034 /* Set D3cold's explicit_set flag if _PS3 exists. */
1035 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1036 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1037
Aaron Lu1399dfc2012-11-21 23:33:40 +01001038 /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1039 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1040 device->power.flags.power_resources)
1041 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1042
Rafael J. Wysockiade3e7f2010-11-25 00:08:36 +01001043 acpi_bus_init_power(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 return 0;
1046}
1047
Len Brown4be44fc2005-08-05 00:44:28 -04001048static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Len Brown4be44fc2005-08-05 00:44:28 -04001050 acpi_status status = AE_OK;
1051 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 /* Presence of _STA indicates 'dynamic_status' */
1055 status = acpi_get_handle(device->handle, "_STA", &temp);
1056 if (ACPI_SUCCESS(status))
1057 device->flags.dynamic_status = 1;
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 /* Presence of _RMV indicates 'removable' */
1060 status = acpi_get_handle(device->handle, "_RMV", &temp);
1061 if (ACPI_SUCCESS(status))
1062 device->flags.removable = 1;
1063
1064 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1065 status = acpi_get_handle(device->handle, "_EJD", &temp);
1066 if (ACPI_SUCCESS(status))
1067 device->flags.ejectable = 1;
1068 else {
1069 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1070 if (ACPI_SUCCESS(status))
1071 device->flags.ejectable = 1;
1072 }
1073
1074 /* Presence of _LCK indicates 'lockable' */
1075 status = acpi_get_handle(device->handle, "_LCK", &temp);
1076 if (ACPI_SUCCESS(status))
1077 device->flags.lockable = 1;
1078
Rafael J. Wysocki7bed50c2011-04-26 11:33:18 +02001079 /* Power resources cannot be power manageable. */
1080 if (device->device_type == ACPI_BUS_TYPE_POWER)
1081 return 0;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 /* Presence of _PS0|_PR0 indicates 'power manageable' */
1084 status = acpi_get_handle(device->handle, "_PS0", &temp);
1085 if (ACPI_FAILURE(status))
1086 status = acpi_get_handle(device->handle, "_PR0", &temp);
1087 if (ACPI_SUCCESS(status))
1088 device->flags.power_manageable = 1;
1089
Joe Perches3c5f9be42008-02-03 17:06:17 +02001090 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Patrick Mocheld550d982006-06-27 00:41:40 -04001092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001095static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Len Brown4be44fc2005-08-05 00:44:28 -04001097 char bus_id[5] = { '?', 0 };
1098 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1099 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 /*
1102 * Bus ID
1103 * ------
1104 * The device's Bus ID is simply the object name.
1105 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1106 */
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001107 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001109 return;
1110 }
1111
1112 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 case ACPI_BUS_TYPE_POWER_BUTTON:
1114 strcpy(device->pnp.bus_id, "PWRF");
1115 break;
1116 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1117 strcpy(device->pnp.bus_id, "SLPF");
1118 break;
1119 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001120 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /* Clean up trailing underscores (if any) */
1122 for (i = 3; i > 1; i--) {
1123 if (bus_id[i] == '_')
1124 bus_id[i] = '\0';
1125 else
1126 break;
1127 }
1128 strcpy(device->pnp.bus_id, bus_id);
1129 break;
1130 }
1131}
1132
Zhang Rui54735262007-01-11 02:09:09 -05001133/*
1134 * acpi_bay_match - see if a device is an ejectable driver bay
1135 *
1136 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1137 * then we can safely call it an ejectable drive bay
1138 */
1139static int acpi_bay_match(struct acpi_device *device){
1140 acpi_status status;
1141 acpi_handle handle;
1142 acpi_handle tmp;
1143 acpi_handle phandle;
1144
1145 handle = device->handle;
1146
1147 status = acpi_get_handle(handle, "_EJ0", &tmp);
1148 if (ACPI_FAILURE(status))
1149 return -ENODEV;
1150
1151 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1152 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1153 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1154 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1155 return 0;
1156
1157 if (acpi_get_parent(handle, &phandle))
1158 return -ENODEV;
1159
1160 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1161 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1162 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1163 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1164 return 0;
1165
1166 return -ENODEV;
1167}
1168
Frank Seidel3620f2f2007-12-07 13:20:34 +01001169/*
1170 * acpi_dock_match - see if a device has a _DCK method
1171 */
1172static int acpi_dock_match(struct acpi_device *device)
1173{
1174 acpi_handle tmp;
1175 return acpi_get_handle(device->handle, "_DCK", &tmp);
1176}
1177
Thomas Renninger620e1122010-10-01 10:54:00 +02001178const char *acpi_device_hid(struct acpi_device *device)
Bob Moore15b8dd52009-06-29 13:39:29 +08001179{
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001180 struct acpi_hardware_id *hid;
Bob Moore15b8dd52009-06-29 13:39:29 +08001181
Thomas Renninger2b2ae7c2010-10-01 10:53:59 +02001182 if (list_empty(&device->pnp.ids))
1183 return dummy_hid;
1184
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001185 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1186 return hid->id;
1187}
1188EXPORT_SYMBOL(acpi_device_hid);
Bob Moore15b8dd52009-06-29 13:39:29 +08001189
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001190static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1191{
1192 struct acpi_hardware_id *id;
Bob Moore15b8dd52009-06-29 13:39:29 +08001193
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001194 id = kmalloc(sizeof(*id), GFP_KERNEL);
1195 if (!id)
1196 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001197
Thomas Meyer581de592011-08-06 11:32:56 +02001198 id->id = kstrdup(dev_id, GFP_KERNEL);
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001199 if (!id->id) {
1200 kfree(id);
1201 return;
Bob Moore15b8dd52009-06-29 13:39:29 +08001202 }
1203
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001204 list_add_tail(&id->list, &device->pnp.ids);
Bob Moore15b8dd52009-06-29 13:39:29 +08001205}
1206
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001207/*
1208 * Old IBM workstations have a DSDT bug wherein the SMBus object
1209 * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1210 * prefix. Work around this.
1211 */
1212static int acpi_ibm_smbus_match(struct acpi_device *device)
1213{
1214 acpi_handle h_dummy;
1215 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1216 int result;
1217
1218 if (!dmi_name_in_vendors("IBM"))
1219 return -ENODEV;
1220
1221 /* Look for SMBS object */
1222 result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1223 if (result)
1224 return result;
1225
1226 if (strcmp("SMBS", path.pointer)) {
1227 result = -ENODEV;
1228 goto out;
1229 }
1230
1231 /* Does it have the necessary (but misnamed) methods? */
1232 result = -ENODEV;
1233 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1234 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1235 ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1236 result = 0;
1237out:
1238 kfree(path.pointer);
1239 return result;
1240}
1241
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001242static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Len Brown4be44fc2005-08-05 00:44:28 -04001244 acpi_status status;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001245 struct acpi_device_info *info;
1246 struct acpica_device_id_list *cid_list;
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001247 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001249 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001251 if (ACPI_IS_ROOT_DEVICE(device)) {
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001252 acpi_add_id(device, ACPI_SYSTEM_HID);
Bjorn Helgaas859ac9a42009-09-21 19:29:50 +00001253 break;
1254 }
1255
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001256 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001258 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return;
1260 }
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (info->valid & ACPI_VALID_HID)
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001263 acpi_add_id(device, info->hardware_id.string);
1264 if (info->valid & ACPI_VALID_CID) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001265 cid_list = &info->compatible_id_list;
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001266 for (i = 0; i < cid_list->count; i++)
1267 acpi_add_id(device, cid_list->ids[i].string);
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (info->valid & ACPI_VALID_ADR) {
1270 device->pnp.bus_address = info->address;
1271 device->flags.bus_address = 1;
1272 }
Zhang Ruiae843332006-12-07 20:57:10 +08001273
Bjorn Helgaasa83893ae2009-10-02 11:03:12 -04001274 kfree(info);
1275
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001276 /*
1277 * Some devices don't reliably have _HIDs & _CIDs, so add
1278 * synthetic HIDs to make sure drivers can find them.
1279 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001280 if (acpi_is_video_device(device))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001281 acpi_add_id(device, ACPI_VIDEO_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001282 else if (ACPI_SUCCESS(acpi_bay_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001283 acpi_add_id(device, ACPI_BAY_HID);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001284 else if (ACPI_SUCCESS(acpi_dock_match(device)))
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001285 acpi_add_id(device, ACPI_DOCK_HID);
Darrick J. Wong222e82a2010-03-24 14:38:37 +01001286 else if (!acpi_ibm_smbus_match(device))
1287 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
Bjorn Helgaasb7b30de2010-03-24 10:44:33 -06001288 else if (!acpi_device_hid(device) &&
1289 ACPI_IS_ROOT_DEVICE(device->parent)) {
1290 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1291 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1292 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1293 }
Zhang Rui54735262007-01-11 02:09:09 -05001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 break;
1296 case ACPI_BUS_TYPE_POWER:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001297 acpi_add_id(device, ACPI_POWER_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 break;
1299 case ACPI_BUS_TYPE_PROCESSOR:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001300 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 case ACPI_BUS_TYPE_THERMAL:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001303 acpi_add_id(device, ACPI_THERMAL_HID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 break;
1305 case ACPI_BUS_TYPE_POWER_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001306 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 break;
1308 case ACPI_BUS_TYPE_SLEEP_BUTTON:
Bjorn Helgaas57f36742009-09-21 13:35:40 -06001309 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 break;
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001314static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001316 acpi_status status;
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /*
1319 * Context
1320 * -------
1321 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001322 * resolutions from handle->device very efficient. Fixed hardware
1323 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001325 if (!device->handle)
1326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001328 status = acpi_attach_data(device->handle,
1329 acpi_bus_data_handler, device);
1330 if (ACPI_SUCCESS(status))
1331 return 0;
1332
1333 printk(KERN_ERR PREFIX "Error attaching device data\n");
1334 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Len Brown4be44fc2005-08-05 00:44:28 -04001337static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001340 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Li Shaohua96333572006-12-07 20:56:46 +08001342 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001343 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Rui Zhang2786f6e2006-12-21 02:21:13 -05001348 /*
1349 * unbind _ADR-Based Devices when hot removal
1350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (dev->flags.bus_address) {
1352 if ((dev->parent) && (dev->parent->ops.unbind))
1353 dev->parent->ops.unbind(dev);
1354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1356
Patrick Mocheld550d982006-06-27 00:41:40 -04001357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001360static int acpi_add_single_object(struct acpi_device **child,
1361 acpi_handle handle, int type,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001362 unsigned long long sta,
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001363 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001365 int result;
1366 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001367 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Burman Yan36bcbec2006-12-19 12:56:11 -08001369 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001371 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001372 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Bjorn Helgaas7f47fa62009-09-21 13:35:19 -06001375 INIT_LIST_HEAD(&device->pnp.ids);
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001376 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001378 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001379 device->bus_ops = *ops; /* workround for not call .start */
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001380 STRUCT_TO_INT(device->status) = sta;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001381
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001382 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /*
1385 * Flags
1386 * -----
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001387 * Note that we only look for object handles -- cannot evaluate objects
1388 * until we know the device is present and properly initialized.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 */
1390 result = acpi_bus_get_flags(device);
1391 if (result)
1392 goto end;
1393
1394 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 * Initialize Device
1396 * -----------------
1397 * TBD: Synch with Core's enumeration/initialization process.
1398 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001399 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /*
1402 * Power Management
1403 * ----------------
1404 */
1405 if (device->flags.power_manageable) {
1406 result = acpi_bus_get_power_flags(device);
1407 if (result)
1408 goto end;
1409 }
1410
Len Brown4be44fc2005-08-05 00:44:28 -04001411 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 * Wakeup device management
1413 *-----------------------
1414 */
Rafael J. Wysockid57d09a2011-01-06 23:41:27 +01001415 acpi_bus_get_wakeup_device_flags(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /*
1418 * Performance Management
1419 * ----------------------
1420 */
1421 if (device->flags.performance_manageable) {
1422 result = acpi_bus_get_perf_flags(device);
1423 if (result)
1424 goto end;
1425 }
1426
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001427 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001428 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001430 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001433 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 */
1435 if (device->flags.bus_address) {
1436 if (device->parent && device->parent->ops.bind)
1437 device->parent->ops.bind(device);
1438 }
1439
Alex Chiang0c526d92009-05-14 08:31:37 -06001440end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001441 if (!result) {
1442 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1443 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1444 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1445 (char *) buffer.pointer,
1446 device->parent ? dev_name(&device->parent->dev) :
1447 "(null)"));
1448 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001450 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001451 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Patrick Mocheld550d982006-06-27 00:41:40 -04001453 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001456#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1457 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1458
Rafael J. Wysockibf325f92010-11-25 00:10:44 +01001459static void acpi_bus_add_power_resource(acpi_handle handle)
1460{
1461 struct acpi_bus_ops ops = {
1462 .acpi_op_add = 1,
1463 .acpi_op_start = 1,
1464 };
1465 struct acpi_device *device = NULL;
1466
1467 acpi_bus_get_device(handle, &device);
1468 if (!device)
1469 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1470 ACPI_STA_DEFAULT, &ops);
1471}
1472
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001473static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1474 unsigned long long *sta)
1475{
1476 acpi_status status;
1477 acpi_object_type acpi_type;
1478
1479 status = acpi_get_type(handle, &acpi_type);
1480 if (ACPI_FAILURE(status))
1481 return -ENODEV;
1482
1483 switch (acpi_type) {
1484 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1485 case ACPI_TYPE_DEVICE:
1486 *type = ACPI_BUS_TYPE_DEVICE;
1487 status = acpi_bus_get_status_handle(handle, sta);
1488 if (ACPI_FAILURE(status))
1489 return -ENODEV;
1490 break;
1491 case ACPI_TYPE_PROCESSOR:
1492 *type = ACPI_BUS_TYPE_PROCESSOR;
1493 status = acpi_bus_get_status_handle(handle, sta);
1494 if (ACPI_FAILURE(status))
1495 return -ENODEV;
1496 break;
1497 case ACPI_TYPE_THERMAL:
1498 *type = ACPI_BUS_TYPE_THERMAL;
1499 *sta = ACPI_STA_DEFAULT;
1500 break;
1501 case ACPI_TYPE_POWER:
1502 *type = ACPI_BUS_TYPE_POWER;
1503 *sta = ACPI_STA_DEFAULT;
1504 break;
1505 default:
1506 return -ENODEV;
1507 }
1508
1509 return 0;
1510}
1511
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001512static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1513 void *context, void **return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001515 struct acpi_bus_ops *ops = context;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001516 int type;
1517 unsigned long long sta;
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001518 struct acpi_device *device;
1519 acpi_status status;
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001520 int result;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001521
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001522 result = acpi_bus_type_and_status(handle, &type, &sta);
1523 if (result)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001524 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001526 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001527 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
Rafael J. Wysocki86e4e202011-01-06 23:40:00 +01001528 struct acpi_device_wakeup wakeup;
1529 acpi_handle temp;
1530
1531 status = acpi_get_handle(handle, "_PRW", &temp);
1532 if (ACPI_SUCCESS(status))
1533 acpi_bus_extract_wakeup_device_power_package(handle,
1534 &wakeup);
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001535 return AE_CTRL_DEPTH;
Rafael J. Wysockib581a7f2010-12-17 22:34:01 +01001536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001538 /*
1539 * We may already have an acpi_device from a previous enumeration. If
1540 * so, we needn't add it again, but we may still have to start it.
1541 */
1542 device = NULL;
1543 acpi_bus_get_device(handle, &device);
Mika Westerberg91e56872012-10-31 22:45:02 +01001544 if (ops->acpi_op_add && !device) {
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001545 acpi_add_single_object(&device, handle, type, sta, ops);
Mika Westerberg91e56872012-10-31 22:45:02 +01001546 /* Is the device a known good platform device? */
1547 if (device
1548 && !acpi_match_device_ids(device, acpi_platform_device_ids))
1549 acpi_create_platform_device(device);
1550 }
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001551
Bjorn Helgaase3b87f82009-09-21 19:30:11 +00001552 if (!device)
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001553 return AE_CTRL_DEPTH;
1554
1555 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1556 status = acpi_start_single_object(device);
1557 if (ACPI_FAILURE(status))
1558 return AE_CTRL_DEPTH;
1559 }
1560
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001561 if (!*return_value)
1562 *return_value = device;
1563 return AE_OK;
1564}
1565
1566static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1567 struct acpi_device **child)
1568{
1569 acpi_status status;
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001570 void *device = NULL;
1571
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001572 status = acpi_bus_check_add(handle, 0, ops, &device);
1573 if (ACPI_SUCCESS(status))
1574 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
Lin Ming22635762009-11-13 10:06:08 +08001575 acpi_bus_check_add, NULL, ops, &device);
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001576
1577 if (child)
1578 *child = device;
Thomas Renninger77796882010-01-29 17:48:52 +01001579
1580 if (device)
1581 return 0;
1582 else
1583 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Thomas Renninger77796882010-01-29 17:48:52 +01001586/*
1587 * acpi_bus_add and acpi_bus_start
1588 *
1589 * scan a given ACPI tree and (probably recently hot-plugged)
1590 * create and add or starts found devices.
1591 *
1592 * If no devices were found -ENODEV is returned which does not
1593 * mean that this is a real error, there just have been no suitable
1594 * ACPI objects in the table trunk from which the kernel could create
1595 * a device and add/start an appropriate driver.
1596 */
1597
Rajesh Shah3fb02732005-04-28 00:25:52 -07001598int
Len Brown4be44fc2005-08-05 00:44:28 -04001599acpi_bus_add(struct acpi_device **child,
1600 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001601{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001602 struct acpi_bus_ops ops;
1603
Li Shaohuac4168bf2006-12-07 20:56:41 +08001604 memset(&ops, 0, sizeof(ops));
1605 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001606
Thomas Renninger77796882010-01-29 17:48:52 +01001607 return acpi_bus_scan(handle, &ops, child);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001608}
1609EXPORT_SYMBOL(acpi_bus_add);
1610
Len Brown4be44fc2005-08-05 00:44:28 -04001611int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001612{
Rajesh Shah3fb02732005-04-28 00:25:52 -07001613 struct acpi_bus_ops ops;
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001614 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001615
Thomas Renningerd2f66502010-01-29 17:48:51 +01001616 if (!device)
1617 return -EINVAL;
1618
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001619 memset(&ops, 0, sizeof(ops));
1620 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001621
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001622 result = acpi_bus_scan(device->handle, &ops, NULL);
1623
Lin Ming3a378982010-12-13 13:36:15 +08001624 acpi_update_all_gpes();
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001625
1626 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001627}
1628EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Kristen Accardiceaba662006-02-23 17:56:01 -08001630int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Len Brown4be44fc2005-08-05 00:44:28 -04001632 acpi_status status;
1633 struct acpi_device *parent, *child;
1634 acpi_handle phandle, chandle;
1635 acpi_object_type type;
1636 u32 level = 1;
1637 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Len Brown4be44fc2005-08-05 00:44:28 -04001639 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 phandle = start->handle;
1641 child = chandle = NULL;
1642
1643 while ((level > 0) && parent && (!err)) {
1644 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001645 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 /*
1648 * If this scope is exhausted then move our way back up.
1649 */
1650 if (ACPI_FAILURE(status)) {
1651 level--;
1652 chandle = phandle;
1653 acpi_get_parent(phandle, &phandle);
1654 child = parent;
1655 parent = parent->parent;
1656
1657 if (level == 0)
1658 err = acpi_bus_remove(child, rmdevice);
1659 else
1660 err = acpi_bus_remove(child, 1);
1661
1662 continue;
1663 }
1664
1665 status = acpi_get_type(chandle, &type);
1666 if (ACPI_FAILURE(status)) {
1667 continue;
1668 }
1669 /*
1670 * If there is a device corresponding to chandle then
1671 * parse it (depth-first).
1672 */
1673 if (acpi_bus_get_device(chandle, &child) == 0) {
1674 level++;
1675 phandle = chandle;
1676 chandle = NULL;
1677 parent = child;
1678 }
1679 continue;
1680 }
1681 return err;
1682}
Kristen Accardiceaba662006-02-23 17:56:01 -08001683EXPORT_SYMBOL_GPL(acpi_bus_trim);
1684
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001685static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Len Brown4be44fc2005-08-05 00:44:28 -04001687 int result = 0;
1688 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001689 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Li Shaohuac4168bf2006-12-07 20:56:41 +08001691 memset(&ops, 0, sizeof(ops));
1692 ops.acpi_op_add = 1;
1693 ops.acpi_op_start = 1;
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
1696 * Enumerate all fixed-feature devices.
1697 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001698 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001699 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001700 ACPI_BUS_TYPE_POWER_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001701 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001702 &ops);
Daniel Drakec10d7a12012-05-10 00:08:43 +01001703 device_init_wakeup(&device->dev, true);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001706 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001707 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001708 ACPI_BUS_TYPE_SLEEP_BUTTON,
Bjorn Helgaas778cbc12009-09-21 19:30:06 +00001709 ACPI_STA_DEFAULT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001710 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Patrick Mocheld550d982006-06-27 00:41:40 -04001713 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714}
1715
Bjorn Helgaase747f272009-03-24 16:49:43 -06001716int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
1718 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001719 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Li Shaohuac4168bf2006-12-07 20:56:41 +08001721 memset(&ops, 0, sizeof(ops));
1722 ops.acpi_op_add = 1;
1723 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Patrick Mochel5b327262006-05-10 10:33:00 -04001725 result = bus_register(&acpi_bus_type);
1726 if (result) {
1727 /* We don't want to quit even if we failed to add suspend/resume */
1728 printk(KERN_ERR PREFIX "Could not register bus type\n");
1729 }
1730
Rafael J. Wysocki97d9a9e2010-11-25 00:10:02 +01001731 acpi_power_init();
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * Enumerate devices in the ACPI namespace.
1735 */
Bjorn Helgaas51a85fa2009-09-21 19:29:56 +00001736 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001737
Li Shaohuac4168bf2006-12-07 20:56:41 +08001738 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001739 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 if (result)
1742 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
Rafael J. Wysockia2100802010-09-16 00:30:43 +02001743 else
Lin Ming3a378982010-12-13 13:36:15 +08001744 acpi_update_all_gpes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Patrick Mocheld550d982006-06-27 00:41:40 -04001746 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}