blob: 9efe3e9dbf21834df5b5dd8321e206d46b3ca54f [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>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04007#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/acpi.h>
9
10#include <acpi/acpi_drivers.h>
11#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#define _COMPONENT ACPI_BUS_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040014ACPI_MODULE_NAME("scan")
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040016extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#define ACPI_BUS_CLASS "system_bus"
19#define ACPI_BUS_HID "ACPI_BUS"
20#define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
21#define ACPI_BUS_DEVICE_NAME "System Bus"
22
23static LIST_HEAD(acpi_device_list);
24DEFINE_SPINLOCK(acpi_device_lock);
25LIST_HEAD(acpi_wakeup_device_list);
26
Len Brown4be44fc2005-08-05 00:44:28 -040027static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
29 struct acpi_object_list arg_list;
30 union acpi_object arg;
31 acpi_status status = AE_OK;
32
33 /*
34 * TBD: evaluate _PS3?
35 */
36
37 if (lockable) {
38 arg_list.count = 1;
39 arg_list.pointer = &arg;
40 arg.type = ACPI_TYPE_INTEGER;
41 arg.integer.value = 0;
42 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
43 }
44
45 arg_list.count = 1;
46 arg_list.pointer = &arg;
47 arg.type = ACPI_TYPE_INTEGER;
48 arg.integer.value = 1;
49
50 /*
51 * TBD: _EJD support.
52 */
53
54 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
55 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -040056 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 }
58
Len Brown4be44fc2005-08-05 00:44:28 -040059 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +080063acpi_eject_store(struct device *d, struct device_attribute *attr,
64 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Len Brown4be44fc2005-08-05 00:44:28 -040066 int result;
67 int ret = count;
68 int islockable;
69 acpi_status status;
70 acpi_handle handle;
71 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +080072 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 if ((!count) || (buf[0] != '1')) {
75 return -EINVAL;
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +080078 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 ret = -ENODEV;
80 goto err;
81 }
82#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +080083 status = acpi_get_type(acpi_device->handle, &type);
84 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 ret = -ENODEV;
86 goto err;
87 }
88
Patrick Mochelf883d9d2006-12-07 20:56:38 +080089 islockable = acpi_device->flags.lockable;
90 handle = acpi_device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Patrick Mochelf883d9d2006-12-07 20:56:38 +080092 result = acpi_bus_trim(acpi_device, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 if (!result)
95 result = acpi_eject_operation(handle, islockable);
96
97 if (result) {
98 ret = -EBUSY;
99 }
Len Brown4be44fc2005-08-05 00:44:28 -0400100 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return ret;
102}
103
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800104static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
105
106static void acpi_device_setup_files(struct acpi_device *dev)
107{
108 acpi_status status;
109 acpi_handle temp;
110
111 /*
112 * If device has _EJ0, 'eject' file is created that is used to trigger
113 * hot-removal function from userland.
114 */
115 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
116 if (ACPI_SUCCESS(status))
117 device_create_file(&dev->dev, &dev_attr_eject);
118}
119
120static void acpi_device_remove_files(struct acpi_device *dev)
121{
122 acpi_status status;
123 acpi_handle temp;
124
125 /*
126 * If device has _EJ0, 'eject' file is created that is used to trigger
127 * hot-removal function from userland.
128 */
129 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
130 if (ACPI_SUCCESS(status))
131 device_remove_file(&dev->dev, &dev_attr_eject);
132}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800134 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 -------------------------------------------------------------------------- */
Patrick Mochel1890a972006-12-07 20:56:31 +0800136static void acpi_device_release(struct device *dev)
137{
138 struct acpi_device *acpi_dev = to_acpi_device(dev);
139
140 kfree(acpi_dev->pnp.cid_list);
141 kfree(acpi_dev);
142}
143
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800144static int acpi_device_suspend(struct device *dev, pm_message_t state)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800145{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800146 struct acpi_device *acpi_dev = to_acpi_device(dev);
147 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800148
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800149 if (acpi_drv && acpi_drv->ops.suspend)
150 return acpi_drv->ops.suspend(acpi_dev, state);
151 return 0;
152}
153
154static int acpi_device_resume(struct device *dev)
155{
156 struct acpi_device *acpi_dev = to_acpi_device(dev);
157 struct acpi_driver *acpi_drv = acpi_dev->driver;
158
159 if (acpi_drv && acpi_drv->ops.resume)
160 return acpi_drv->ops.resume(acpi_dev);
161 return 0;
162}
163
164static int acpi_bus_match(struct device *dev, struct device_driver *drv)
165{
166 struct acpi_device *acpi_dev = to_acpi_device(dev);
167 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
168
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800169 return !acpi_match_ids(acpi_dev, acpi_drv->ids);
170}
171
172static int acpi_device_uevent(struct device *dev, char **envp, int num_envp,
173 char *buffer, int buffer_size)
174{
175 struct acpi_device *acpi_dev = to_acpi_device(dev);
176 int i = 0, length = 0, ret = 0;
177
178 if (acpi_dev->flags.hardware_id)
179 ret = add_uevent_var(envp, num_envp, &i,
180 buffer, buffer_size, &length,
181 "HWID=%s", acpi_dev->pnp.hardware_id);
182 if (ret)
183 return -ENOMEM;
184 if (acpi_dev->flags.compatible_ids) {
185 int j;
186 struct acpi_compatible_id_list *cid_list;
187
188 cid_list = acpi_dev->pnp.cid_list;
189
190 for (j = 0; j < cid_list->count; j++) {
191 ret = add_uevent_var(envp, num_envp, &i, buffer,
192 buffer_size, &length, "COMPTID=%s",
193 cid_list->id[j].value);
194 if (ret)
195 return -ENOMEM;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800196 }
197 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800198
199 envp[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return 0;
201}
202
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800203static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
204static int acpi_start_single_object(struct acpi_device *);
205static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800206{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800207 struct acpi_device *acpi_dev = to_acpi_device(dev);
208 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
209 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800211 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
212 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800213 if (acpi_dev->bus_ops.acpi_op_start)
214 acpi_start_single_object(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800215 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
216 "Found driver [%s] for device [%s]\n",
217 acpi_drv->name, acpi_dev->pnp.bus_id));
218 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800219 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800220 return ret;
221}
222
223static int acpi_device_remove(struct device * dev)
224{
225 struct acpi_device *acpi_dev = to_acpi_device(dev);
226 struct acpi_driver *acpi_drv = acpi_dev->driver;
227
228 if (acpi_drv) {
229 if (acpi_drv->ops.stop)
Li Shaohua96333572006-12-07 20:56:46 +0800230 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800231 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800232 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800233 }
234 acpi_dev->driver = NULL;
235 acpi_driver_data(dev) = NULL;
236
237 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800238 return 0;
239}
240
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800241static void acpi_device_shutdown(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800242{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800243 struct acpi_device *acpi_dev = to_acpi_device(dev);
244 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800245
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800246 if (acpi_drv && acpi_drv->ops.shutdown)
247 acpi_drv->ops.shutdown(acpi_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800249 return ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Zhang Rui9e89dde2006-12-07 20:56:16 +0800252static struct bus_type acpi_bus_type = {
253 .name = "acpi",
254 .suspend = acpi_device_suspend,
255 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800256 .shutdown = acpi_device_shutdown,
257 .match = acpi_bus_match,
258 .probe = acpi_device_probe,
259 .remove = acpi_device_remove,
260 .uevent = acpi_device_uevent,
Zhang Rui9e89dde2006-12-07 20:56:16 +0800261};
262
263static void acpi_device_register(struct acpi_device *device,
264 struct acpi_device *parent)
265{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800266 /*
267 * Linkage
268 * -------
269 * Link this device to its parent and siblings.
270 */
271 INIT_LIST_HEAD(&device->children);
272 INIT_LIST_HEAD(&device->node);
273 INIT_LIST_HEAD(&device->g_list);
274 INIT_LIST_HEAD(&device->wakeup_list);
275
276 spin_lock(&acpi_device_lock);
277 if (device->parent) {
278 list_add_tail(&device->node, &device->parent->children);
279 list_add_tail(&device->g_list, &device->parent->g_list);
280 } else
281 list_add_tail(&device->g_list, &acpi_device_list);
282 if (device->wakeup.flags.valid)
283 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
284 spin_unlock(&acpi_device_lock);
285
Patrick Mochel1890a972006-12-07 20:56:31 +0800286 if (device->parent)
287 device->dev.parent = &parent->dev;
288 device->dev.bus = &acpi_bus_type;
289 device_initialize(&device->dev);
290 sprintf(device->dev.bus_id, "%s", device->pnp.bus_id);
291 device->dev.release = &acpi_device_release;
292 device_add(&device->dev);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800293
294 acpi_device_setup_files(device);
Li Shaohua96333572006-12-07 20:56:46 +0800295 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800296}
297
298static void acpi_device_unregister(struct acpi_device *device, int type)
299{
300 spin_lock(&acpi_device_lock);
301 if (device->parent) {
302 list_del(&device->node);
303 list_del(&device->g_list);
304 } else
305 list_del(&device->g_list);
306
307 list_del(&device->wakeup_list);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800308 spin_unlock(&acpi_device_lock);
309
310 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800311
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800312 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800313 device_unregister(&device->dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800314}
315
316/* --------------------------------------------------------------------------
317 Driver Management
318 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500320 * acpi_bus_driver_init - add a device to a driver
321 * @device: the device to add and initialize
322 * @driver: driver for the device
323 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800325 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
327static int
Len Brown4be44fc2005-08-05 00:44:28 -0400328acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Len Brown4be44fc2005-08-05 00:44:28 -0400330 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400334 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400337 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 result = driver->ops.add(device);
340 if (result) {
341 device->driver = NULL;
342 acpi_driver_data(device) = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400343 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
346 device->driver = driver;
347
348 /*
349 * TBD - Configuration Management: Assign resources to device based
350 * upon possible configuration and currently allocated resources.
351 */
352
Len Brown4be44fc2005-08-05 00:44:28 -0400353 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
354 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400355 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700356}
357
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400358static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700359{
360 int result = 0;
361 struct acpi_driver *driver;
362
Rajesh Shah3fb02732005-04-28 00:25:52 -0700363
364 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400365 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (driver->ops.start) {
368 result = driver->ops.start(device);
369 if (result && driver->ops.remove)
370 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
Patrick Mocheld550d982006-06-27 00:41:40 -0400373 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500377 * acpi_bus_register_driver - register a driver with the ACPI bus
378 * @driver: driver being registered
379 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500381 * devices that match the driver's criteria and binds. Returns zero for
382 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
Len Brown4be44fc2005-08-05 00:44:28 -0400384int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Patrick Mochel1890a972006-12-07 20:56:31 +0800386 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400389 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800390 driver->drv.name = driver->name;
391 driver->drv.bus = &acpi_bus_type;
392 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Patrick Mochel1890a972006-12-07 20:56:31 +0800394 ret = driver_register(&driver->drv);
395 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Len Brown4be44fc2005-08-05 00:44:28 -0400398EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500401 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
402 * @driver: driver to unregister
403 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 * Unregisters a driver with the ACPI bus. Searches the namespace for all
405 * devices that match the driver's criteria and unbinds.
406 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400407void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Patrick Mochel1890a972006-12-07 20:56:31 +0800409 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
Len Brown4be44fc2005-08-05 00:44:28 -0400411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412EXPORT_SYMBOL(acpi_bus_unregister_driver);
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/* --------------------------------------------------------------------------
415 Device Enumeration
416 -------------------------------------------------------------------------- */
Len Brownc8f7a622006-07-09 17:22:28 -0400417acpi_status
418acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
419{
420 acpi_status status;
421 acpi_handle tmp;
422 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
423 union acpi_object *obj;
424
425 status = acpi_get_handle(handle, "_EJD", &tmp);
426 if (ACPI_FAILURE(status))
427 return status;
428
429 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
430 if (ACPI_SUCCESS(status)) {
431 obj = buffer.pointer;
432 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
433 kfree(buffer.pointer);
434 }
435 return status;
436}
437EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
438
Zhang Rui9e89dde2006-12-07 20:56:16 +0800439void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
440{
441
442 /* TBD */
443
444 return;
445}
446
447int acpi_match_ids(struct acpi_device *device, char *ids)
448{
449 if (device->flags.hardware_id)
450 if (strstr(ids, device->pnp.hardware_id))
451 return 0;
452
453 if (device->flags.compatible_ids) {
454 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
455 int i;
456
457 /* compare multiple _CID entries against driver ids */
458 for (i = 0; i < cid_list->count; i++) {
459 if (strstr(ids, cid_list->id[i].value))
460 return 0;
461 }
462 }
463 return -ENOENT;
464}
465
466static int acpi_bus_get_perf_flags(struct acpi_device *device)
467{
468 device->performance.state = ACPI_STATE_UNKNOWN;
469 return 0;
470}
471
472static acpi_status
473acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
474 union acpi_object *package)
475{
476 int i = 0;
477 union acpi_object *element = NULL;
478
479 if (!device || !package || (package->package.count < 2))
480 return AE_BAD_PARAMETER;
481
482 element = &(package->package.elements[0]);
483 if (!element)
484 return AE_BAD_PARAMETER;
485 if (element->type == ACPI_TYPE_PACKAGE) {
486 if ((element->package.count < 2) ||
487 (element->package.elements[0].type !=
488 ACPI_TYPE_LOCAL_REFERENCE)
489 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
490 return AE_BAD_DATA;
491 device->wakeup.gpe_device =
492 element->package.elements[0].reference.handle;
493 device->wakeup.gpe_number =
494 (u32) element->package.elements[1].integer.value;
495 } else if (element->type == ACPI_TYPE_INTEGER) {
496 device->wakeup.gpe_number = element->integer.value;
497 } else
498 return AE_BAD_DATA;
499
500 element = &(package->package.elements[1]);
501 if (element->type != ACPI_TYPE_INTEGER) {
502 return AE_BAD_DATA;
503 }
504 device->wakeup.sleep_state = element->integer.value;
505
506 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
507 return AE_NO_MEMORY;
508 }
509 device->wakeup.resources.count = package->package.count - 2;
510 for (i = 0; i < device->wakeup.resources.count; i++) {
511 element = &(package->package.elements[i + 2]);
512 if (element->type != ACPI_TYPE_ANY) {
513 return AE_BAD_DATA;
514 }
515
516 device->wakeup.resources.handles[i] = element->reference.handle;
517 }
518
519 return AE_OK;
520}
521
522static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
523{
524 acpi_status status = 0;
525 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
526 union acpi_object *package = NULL;
527
528
529 /* _PRW */
530 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
531 if (ACPI_FAILURE(status)) {
532 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
533 goto end;
534 }
535
536 package = (union acpi_object *)buffer.pointer;
537 status = acpi_bus_extract_wakeup_device_power_package(device, package);
538 if (ACPI_FAILURE(status)) {
539 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
540 goto end;
541 }
542
543 kfree(buffer.pointer);
544
545 device->wakeup.flags.valid = 1;
546 /* Power button, Lid switch always enable wakeup */
547 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
548 device->wakeup.flags.run_wake = 1;
549
550 end:
551 if (ACPI_FAILURE(status))
552 device->flags.wake_capable = 0;
553 return 0;
554}
555
556static int acpi_bus_get_power_flags(struct acpi_device *device)
557{
558 acpi_status status = 0;
559 acpi_handle handle = NULL;
560 u32 i = 0;
561
562
563 /*
564 * Power Management Flags
565 */
566 status = acpi_get_handle(device->handle, "_PSC", &handle);
567 if (ACPI_SUCCESS(status))
568 device->power.flags.explicit_get = 1;
569 status = acpi_get_handle(device->handle, "_IRC", &handle);
570 if (ACPI_SUCCESS(status))
571 device->power.flags.inrush_current = 1;
572
573 /*
574 * Enumerate supported power management states
575 */
576 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
577 struct acpi_device_power_state *ps = &device->power.states[i];
578 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
579
580 /* Evaluate "_PRx" to se if power resources are referenced */
581 acpi_evaluate_reference(device->handle, object_name, NULL,
582 &ps->resources);
583 if (ps->resources.count) {
584 device->power.flags.power_resources = 1;
585 ps->flags.valid = 1;
586 }
587
588 /* Evaluate "_PSx" to see if we can do explicit sets */
589 object_name[2] = 'S';
590 status = acpi_get_handle(device->handle, object_name, &handle);
591 if (ACPI_SUCCESS(status)) {
592 ps->flags.explicit_set = 1;
593 ps->flags.valid = 1;
594 }
595
596 /* State is valid if we have some power control */
597 if (ps->resources.count || ps->flags.explicit_set)
598 ps->flags.valid = 1;
599
600 ps->power = -1; /* Unknown - driver assigned */
601 ps->latency = -1; /* Unknown - driver assigned */
602 }
603
604 /* Set defaults for D0 and D3 states (always valid) */
605 device->power.states[ACPI_STATE_D0].flags.valid = 1;
606 device->power.states[ACPI_STATE_D0].power = 100;
607 device->power.states[ACPI_STATE_D3].flags.valid = 1;
608 device->power.states[ACPI_STATE_D3].power = 0;
609
610 /* TBD: System wake support and resource requirements. */
611
612 device->power.state = ACPI_STATE_UNKNOWN;
613
614 return 0;
615}
Len Brownc8f7a622006-07-09 17:22:28 -0400616
Len Brown4be44fc2005-08-05 00:44:28 -0400617static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Len Brown4be44fc2005-08-05 00:44:28 -0400619 acpi_status status = AE_OK;
620 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 /* Presence of _STA indicates 'dynamic_status' */
624 status = acpi_get_handle(device->handle, "_STA", &temp);
625 if (ACPI_SUCCESS(status))
626 device->flags.dynamic_status = 1;
627
628 /* Presence of _CID indicates 'compatible_ids' */
629 status = acpi_get_handle(device->handle, "_CID", &temp);
630 if (ACPI_SUCCESS(status))
631 device->flags.compatible_ids = 1;
632
633 /* Presence of _RMV indicates 'removable' */
634 status = acpi_get_handle(device->handle, "_RMV", &temp);
635 if (ACPI_SUCCESS(status))
636 device->flags.removable = 1;
637
638 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
639 status = acpi_get_handle(device->handle, "_EJD", &temp);
640 if (ACPI_SUCCESS(status))
641 device->flags.ejectable = 1;
642 else {
643 status = acpi_get_handle(device->handle, "_EJ0", &temp);
644 if (ACPI_SUCCESS(status))
645 device->flags.ejectable = 1;
646 }
647
648 /* Presence of _LCK indicates 'lockable' */
649 status = acpi_get_handle(device->handle, "_LCK", &temp);
650 if (ACPI_SUCCESS(status))
651 device->flags.lockable = 1;
652
653 /* Presence of _PS0|_PR0 indicates 'power manageable' */
654 status = acpi_get_handle(device->handle, "_PS0", &temp);
655 if (ACPI_FAILURE(status))
656 status = acpi_get_handle(device->handle, "_PR0", &temp);
657 if (ACPI_SUCCESS(status))
658 device->flags.power_manageable = 1;
659
660 /* Presence of _PRW indicates wake capable */
661 status = acpi_get_handle(device->handle, "_PRW", &temp);
662 if (ACPI_SUCCESS(status))
663 device->flags.wake_capable = 1;
664
665 /* TBD: Peformance management */
666
Patrick Mocheld550d982006-06-27 00:41:40 -0400667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Len Brown4be44fc2005-08-05 00:44:28 -0400670static void acpi_device_get_busid(struct acpi_device *device,
671 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Len Brown4be44fc2005-08-05 00:44:28 -0400673 char bus_id[5] = { '?', 0 };
674 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
675 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /*
678 * Bus ID
679 * ------
680 * The device's Bus ID is simply the object name.
681 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
682 */
683 switch (type) {
684 case ACPI_BUS_TYPE_SYSTEM:
685 strcpy(device->pnp.bus_id, "ACPI");
686 break;
687 case ACPI_BUS_TYPE_POWER_BUTTON:
688 strcpy(device->pnp.bus_id, "PWRF");
689 break;
690 case ACPI_BUS_TYPE_SLEEP_BUTTON:
691 strcpy(device->pnp.bus_id, "SLPF");
692 break;
693 default:
694 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
695 /* Clean up trailing underscores (if any) */
696 for (i = 3; i > 1; i--) {
697 if (bus_id[i] == '_')
698 bus_id[i] = '\0';
699 else
700 break;
701 }
702 strcpy(device->pnp.bus_id, bus_id);
703 break;
704 }
705}
706
Zhang Ruiae843332006-12-07 20:57:10 +0800707static int
708acpi_video_bus_match(struct acpi_device *device)
709{
710 acpi_handle h_dummy1;
711 acpi_handle h_dummy2;
712 acpi_handle h_dummy3;
713
714
715 if (!device)
716 return -EINVAL;
717
718 /* Since there is no HID, CID for ACPI Video drivers, we have
719 * to check well known required nodes for each feature we support.
720 */
721
722 /* Does this device able to support video switching ? */
723 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
724 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
725 return 0;
726
727 /* Does this device able to retrieve a video ROM ? */
728 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
729 return 0;
730
731 /* Does this device able to configure which video head to be POSTed ? */
732 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
733 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
734 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
735 return 0;
736
737 return -ENODEV;
738}
739
740static int acpi_pci_bridge_match(struct acpi_device *device)
741{
742 acpi_status status;
743 acpi_handle handle;
744
745 /* pci bridge has _PRT but isn't PNP0A03 */
746 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
747 if (ACPI_FAILURE(status))
748 return -ENODEV;
749 if (!acpi_match_ids(device, "PNP0A03"))
750 return -ENODEV;
751 return 0;
752}
753
Len Brown4be44fc2005-08-05 00:44:28 -0400754static void acpi_device_set_id(struct acpi_device *device,
755 struct acpi_device *parent, acpi_handle handle,
756 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Len Brown4be44fc2005-08-05 00:44:28 -0400758 struct acpi_device_info *info;
759 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
760 char *hid = NULL;
761 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400763 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 switch (type) {
766 case ACPI_BUS_TYPE_DEVICE:
767 status = acpi_get_object_info(handle, &buffer);
768 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400769 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return;
771 }
772
773 info = buffer.pointer;
774 if (info->valid & ACPI_VALID_HID)
775 hid = info->hardware_id.value;
776 if (info->valid & ACPI_VALID_UID)
777 uid = info->unique_id.value;
778 if (info->valid & ACPI_VALID_CID)
779 cid_list = &info->compatibility_id;
780 if (info->valid & ACPI_VALID_ADR) {
781 device->pnp.bus_address = info->address;
782 device->flags.bus_address = 1;
783 }
Zhang Ruiae843332006-12-07 20:57:10 +0800784
785 if(!(info->valid & (ACPI_VALID_HID | ACPI_VALID_CID))){
786 status = acpi_video_bus_match(device);
787 if(ACPI_SUCCESS(status))
788 hid = ACPI_VIDEO_HID;
789
790 status = acpi_pci_bridge_match(device);
791 if(ACPI_SUCCESS(status))
792 hid = ACPI_PCI_BRIDGE_HID;
793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 break;
795 case ACPI_BUS_TYPE_POWER:
796 hid = ACPI_POWER_HID;
797 break;
798 case ACPI_BUS_TYPE_PROCESSOR:
799 hid = ACPI_PROCESSOR_HID;
800 break;
801 case ACPI_BUS_TYPE_SYSTEM:
802 hid = ACPI_SYSTEM_HID;
803 break;
804 case ACPI_BUS_TYPE_THERMAL:
805 hid = ACPI_THERMAL_HID;
806 break;
807 case ACPI_BUS_TYPE_POWER_BUTTON:
808 hid = ACPI_BUTTON_HID_POWERF;
809 break;
810 case ACPI_BUS_TYPE_SLEEP_BUTTON:
811 hid = ACPI_BUTTON_HID_SLEEPF;
812 break;
813 }
814
815 /*
816 * \_SB
817 * ----
818 * Fix for the system root bus device -- the only root-level device.
819 */
Bob Moorec51a4de2005-11-17 13:07:00 -0500820 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 hid = ACPI_BUS_HID;
822 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
823 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
824 }
825
826 if (hid) {
827 strcpy(device->pnp.hardware_id, hid);
828 device->flags.hardware_id = 1;
829 }
830 if (uid) {
831 strcpy(device->pnp.unique_id, uid);
832 device->flags.unique_id = 1;
833 }
834 if (cid_list) {
835 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
836 if (device->pnp.cid_list)
837 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
838 else
839 printk(KERN_ERR "Memory allocation error\n");
840 }
841
Len Brown02438d82006-06-30 03:19:10 -0400842 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Len Brown4be44fc2005-08-05 00:44:28 -0400845static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 acpi_status status = AE_OK;
848 int result = 0;
849 /*
850 * Context
851 * -------
852 * Attach this 'struct acpi_device' to the ACPI object. This makes
853 * resolutions from handle->device very efficient. Note that we need
854 * to be careful with fixed-feature devices as they all attach to the
855 * root object.
856 */
Len Brown4be44fc2005-08-05 00:44:28 -0400857 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
859 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400860 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 if (ACPI_FAILURE(status)) {
863 printk("Error attaching device data\n");
864 result = -ENODEV;
865 }
866 }
867 return result;
868}
869
Len Brown4be44fc2005-08-05 00:44:28 -0400870static void acpi_device_get_debug_info(struct acpi_device *device,
871 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873#ifdef CONFIG_ACPI_DEBUG_OUTPUT
Len Brown4be44fc2005-08-05 00:44:28 -0400874 char *type_string = NULL;
875 char name[80] = { '?', '\0' };
876 struct acpi_buffer buffer = { sizeof(name), name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 switch (type) {
879 case ACPI_BUS_TYPE_DEVICE:
880 type_string = "Device";
881 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
882 break;
883 case ACPI_BUS_TYPE_POWER:
884 type_string = "Power Resource";
885 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
886 break;
887 case ACPI_BUS_TYPE_PROCESSOR:
888 type_string = "Processor";
889 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
890 break;
891 case ACPI_BUS_TYPE_SYSTEM:
892 type_string = "System";
893 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
894 break;
895 case ACPI_BUS_TYPE_THERMAL:
896 type_string = "Thermal Zone";
897 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
898 break;
899 case ACPI_BUS_TYPE_POWER_BUTTON:
900 type_string = "Power Button";
901 sprintf(name, "PWRB");
902 break;
903 case ACPI_BUS_TYPE_SLEEP_BUTTON:
904 type_string = "Sleep Button";
905 sprintf(name, "SLPB");
906 break;
907 }
908
909 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
Len Brown4be44fc2005-08-05 00:44:28 -0400910#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Len Brown4be44fc2005-08-05 00:44:28 -0400913static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -0400916 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Li Shaohua96333572006-12-07 20:56:46 +0800918 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +0800919 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -0400922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
925
Patrick Mocheld550d982006-06-27 00:41:40 -0400926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Rajesh Shah3fb02732005-04-28 00:25:52 -0700929static int
Len Brown4be44fc2005-08-05 00:44:28 -0400930acpi_add_single_object(struct acpi_device **child,
Li Shaohuac4168bf2006-12-07 20:56:41 +0800931 struct acpi_device *parent, acpi_handle handle, int type,
932 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Len Brown4be44fc2005-08-05 00:44:28 -0400934 int result = 0;
935 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 if (!child)
Patrick Mocheld550d982006-06-27 00:41:40 -0400939 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
942 if (!device) {
Len Brown64684632006-06-26 23:41:38 -0400943 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400944 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 memset(device, 0, sizeof(struct acpi_device));
947
948 device->handle = handle;
949 device->parent = parent;
Li Shaohuac4168bf2006-12-07 20:56:41 +0800950 device->bus_ops = *ops; /* workround for not call .start */
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Len Brown4be44fc2005-08-05 00:44:28 -0400953 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 /*
956 * Flags
957 * -----
958 * Get prior to calling acpi_bus_get_status() so we know whether
959 * or not _STA is present. Note that we only look for object
960 * handles -- cannot evaluate objects until we know the device is
961 * present and properly initialized.
962 */
963 result = acpi_bus_get_flags(device);
964 if (result)
965 goto end;
966
967 /*
968 * Status
969 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -0500970 * See if the device is present. We always assume that non-Device
971 * and non-Processor objects (e.g. thermal zones, power resources,
972 * etc.) are present, functioning, etc. (at least when parent object
973 * is present). Note that _STA has a different meaning for some
974 * objects (e.g. power resources) so we need to be careful how we use
975 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 */
977 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -0500978 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 case ACPI_BUS_TYPE_DEVICE:
980 result = acpi_bus_get_status(device);
981 if (ACPI_FAILURE(result) || !device->status.present) {
982 result = -ENOENT;
983 goto end;
984 }
985 break;
986 default:
987 STRUCT_TO_INT(device->status) = 0x0F;
988 break;
989 }
990
991 /*
992 * Initialize Device
993 * -----------------
994 * TBD: Synch with Core's enumeration/initialization process.
995 */
996
997 /*
998 * Hardware ID, Unique ID, & Bus Address
999 * -------------------------------------
1000 */
Len Brown4be44fc2005-08-05 00:44:28 -04001001 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 /*
1004 * Power Management
1005 * ----------------
1006 */
1007 if (device->flags.power_manageable) {
1008 result = acpi_bus_get_power_flags(device);
1009 if (result)
1010 goto end;
1011 }
1012
Len Brown4be44fc2005-08-05 00:44:28 -04001013 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 * Wakeup device management
1015 *-----------------------
1016 */
1017 if (device->flags.wake_capable) {
1018 result = acpi_bus_get_wakeup_device_flags(device);
1019 if (result)
1020 goto end;
1021 }
1022
1023 /*
1024 * Performance Management
1025 * ----------------------
1026 */
1027 if (device->flags.performance_manageable) {
1028 result = acpi_bus_get_perf_flags(device);
1029 if (result)
1030 goto end;
1031 }
1032
Len Brown4be44fc2005-08-05 00:44:28 -04001033 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 goto end;
1035
Len Brown4be44fc2005-08-05 00:44:28 -04001036 acpi_device_get_debug_info(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Len Brown4be44fc2005-08-05 00:44:28 -04001038 acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Len Brown4be44fc2005-08-05 00:44:28 -04001040 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (!result)
1042 *child = device;
1043 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001044 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 kfree(device);
1046 }
1047
Patrick Mocheld550d982006-06-27 00:41:40 -04001048 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Len Brown4be44fc2005-08-05 00:44:28 -04001051static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Len Brown4be44fc2005-08-05 00:44:28 -04001053 acpi_status status = AE_OK;
1054 struct acpi_device *parent = NULL;
1055 struct acpi_device *child = NULL;
1056 acpi_handle phandle = NULL;
1057 acpi_handle chandle = NULL;
1058 acpi_object_type type = 0;
1059 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (!start)
Patrick Mocheld550d982006-06-27 00:41:40 -04001063 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 parent = start;
1066 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /*
1069 * Parse through the ACPI namespace, identify all 'devices', and
1070 * create a new 'struct acpi_device' for each.
1071 */
1072 while ((level > 0) && parent) {
1073
1074 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001075 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /*
1078 * If this scope is exhausted then move our way back up.
1079 */
1080 if (ACPI_FAILURE(status)) {
1081 level--;
1082 chandle = phandle;
1083 acpi_get_parent(phandle, &phandle);
1084 if (parent->parent)
1085 parent = parent->parent;
1086 continue;
1087 }
1088
1089 status = acpi_get_type(chandle, &type);
1090 if (ACPI_FAILURE(status))
1091 continue;
1092
1093 /*
1094 * If this is a scope object then parse it (depth-first).
1095 */
1096 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1097 level++;
1098 phandle = chandle;
1099 chandle = NULL;
1100 continue;
1101 }
1102
1103 /*
1104 * We're only interested in objects that we consider 'devices'.
1105 */
1106 switch (type) {
1107 case ACPI_TYPE_DEVICE:
1108 type = ACPI_BUS_TYPE_DEVICE;
1109 break;
1110 case ACPI_TYPE_PROCESSOR:
1111 type = ACPI_BUS_TYPE_PROCESSOR;
1112 break;
1113 case ACPI_TYPE_THERMAL:
1114 type = ACPI_BUS_TYPE_THERMAL;
1115 break;
1116 case ACPI_TYPE_POWER:
1117 type = ACPI_BUS_TYPE_POWER;
1118 break;
1119 default:
1120 continue;
1121 }
1122
Rajesh Shah3fb02732005-04-28 00:25:52 -07001123 if (ops->acpi_op_add)
1124 status = acpi_add_single_object(&child, parent,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001125 chandle, type, ops);
Len Brown4be44fc2005-08-05 00:44:28 -04001126 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001127 status = acpi_bus_get_device(chandle, &child);
1128
Len Brown4be44fc2005-08-05 00:44:28 -04001129 if (ACPI_FAILURE(status))
1130 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001131
Li Shaohuac4168bf2006-12-07 20:56:41 +08001132 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001133 status = acpi_start_single_object(child);
1134 if (ACPI_FAILURE(status))
1135 continue;
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /*
1139 * If the device is present, enabled, and functioning then
1140 * parse its scope (depth-first). Note that we need to
1141 * represent absent devices to facilitate PnP notifications
1142 * -- but only the subtree head (not all of its children,
1143 * which will be enumerated when the parent is inserted).
1144 *
1145 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001146 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 */
1148 if (child->status.present) {
1149 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1150 NULL, NULL);
1151 if (ACPI_SUCCESS(status)) {
1152 level++;
1153 phandle = chandle;
1154 chandle = NULL;
1155 parent = child;
1156 }
1157 }
1158 }
1159
Patrick Mocheld550d982006-06-27 00:41:40 -04001160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Rajesh Shah3fb02732005-04-28 00:25:52 -07001163int
Len Brown4be44fc2005-08-05 00:44:28 -04001164acpi_bus_add(struct acpi_device **child,
1165 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001166{
1167 int result;
1168 struct acpi_bus_ops ops;
1169
Li Shaohuac4168bf2006-12-07 20:56:41 +08001170 memset(&ops, 0, sizeof(ops));
1171 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001172
Li Shaohuac4168bf2006-12-07 20:56:41 +08001173 result = acpi_add_single_object(child, parent, handle, type, &ops);
1174 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001175 result = acpi_bus_scan(*child, &ops);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001176
Patrick Mocheld550d982006-06-27 00:41:40 -04001177 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001178}
Len Brown4be44fc2005-08-05 00:44:28 -04001179
Rajesh Shah3fb02732005-04-28 00:25:52 -07001180EXPORT_SYMBOL(acpi_bus_add);
1181
Len Brown4be44fc2005-08-05 00:44:28 -04001182int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001183{
1184 int result;
1185 struct acpi_bus_ops ops;
1186
Rajesh Shah3fb02732005-04-28 00:25:52 -07001187
1188 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001189 return -EINVAL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001190
1191 result = acpi_start_single_object(device);
1192 if (!result) {
1193 memset(&ops, 0, sizeof(ops));
1194 ops.acpi_op_start = 1;
1195 result = acpi_bus_scan(device, &ops);
1196 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001197 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001198}
Len Brown4be44fc2005-08-05 00:44:28 -04001199
Rajesh Shah3fb02732005-04-28 00:25:52 -07001200EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Kristen Accardiceaba662006-02-23 17:56:01 -08001202int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Len Brown4be44fc2005-08-05 00:44:28 -04001204 acpi_status status;
1205 struct acpi_device *parent, *child;
1206 acpi_handle phandle, chandle;
1207 acpi_object_type type;
1208 u32 level = 1;
1209 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Len Brown4be44fc2005-08-05 00:44:28 -04001211 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 phandle = start->handle;
1213 child = chandle = NULL;
1214
1215 while ((level > 0) && parent && (!err)) {
1216 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001217 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /*
1220 * If this scope is exhausted then move our way back up.
1221 */
1222 if (ACPI_FAILURE(status)) {
1223 level--;
1224 chandle = phandle;
1225 acpi_get_parent(phandle, &phandle);
1226 child = parent;
1227 parent = parent->parent;
1228
1229 if (level == 0)
1230 err = acpi_bus_remove(child, rmdevice);
1231 else
1232 err = acpi_bus_remove(child, 1);
1233
1234 continue;
1235 }
1236
1237 status = acpi_get_type(chandle, &type);
1238 if (ACPI_FAILURE(status)) {
1239 continue;
1240 }
1241 /*
1242 * If there is a device corresponding to chandle then
1243 * parse it (depth-first).
1244 */
1245 if (acpi_bus_get_device(chandle, &child) == 0) {
1246 level++;
1247 phandle = chandle;
1248 chandle = NULL;
1249 parent = child;
1250 }
1251 continue;
1252 }
1253 return err;
1254}
Kristen Accardiceaba662006-02-23 17:56:01 -08001255EXPORT_SYMBOL_GPL(acpi_bus_trim);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Len Brown4be44fc2005-08-05 00:44:28 -04001258static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Len Brown4be44fc2005-08-05 00:44:28 -04001260 int result = 0;
1261 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001262 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -04001265 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Li Shaohuac4168bf2006-12-07 20:56:41 +08001267 memset(&ops, 0, sizeof(ops));
1268 ops.acpi_op_add = 1;
1269 ops.acpi_op_start = 1;
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
1272 * Enumerate all fixed-feature devices.
1273 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001274 if (acpi_fadt.pwr_button == 0) {
1275 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001276 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001277 ACPI_BUS_TYPE_POWER_BUTTON,
1278 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Rajesh Shah3fb02732005-04-28 00:25:52 -07001281 if (acpi_fadt.sleep_button == 0) {
1282 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001283 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001284 ACPI_BUS_TYPE_SLEEP_BUTTON,
1285 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Patrick Mocheld550d982006-06-27 00:41:40 -04001288 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291static int __init acpi_scan_init(void)
1292{
1293 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001294 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Li Shaohuac4168bf2006-12-07 20:56:41 +08001300 memset(&ops, 0, sizeof(ops));
1301 ops.acpi_op_add = 1;
1302 ops.acpi_op_start = 1;
1303
Patrick Mochel5b327262006-05-10 10:33:00 -04001304 result = bus_register(&acpi_bus_type);
1305 if (result) {
1306 /* We don't want to quit even if we failed to add suspend/resume */
1307 printk(KERN_ERR PREFIX "Could not register bus type\n");
1308 }
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 /*
1311 * Create the root device in the bus's device tree
1312 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001313 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001314 ACPI_BUS_TYPE_SYSTEM, &ops);
Patrick Mochel5b327262006-05-10 10:33:00 -04001315 if (result)
1316 goto Done;
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /*
1319 * Enumerate devices in the ACPI namespace.
1320 */
1321 result = acpi_bus_scan_fixed(acpi_root);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001322 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001323 result = acpi_bus_scan(acpi_root, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 if (result)
1326 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1327
Len Brown4be44fc2005-08-05 00:44:28 -04001328 Done:
Patrick Mocheld550d982006-06-27 00:41:40 -04001329 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
1332subsys_initcall(acpi_scan_init);