Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2.6
[deliverable/linux.git] / drivers / acpi / scan.c
CommitLineData
1da177e4
LT
1/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
9b6d97b6 7#include <linux/kernel.h>
1da177e4
LT
8#include <linux/acpi.h>
9
10#include <acpi/acpi_drivers.h>
11#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
12
1da177e4 13#define _COMPONENT ACPI_BUS_COMPONENT
f52fd66d 14ACPI_MODULE_NAME("scan");
1da177e4 15#define STRUCT_TO_INT(s) (*((int*)&s))
4be44fcd 16extern struct acpi_device *acpi_root;
1da177e4
LT
17
18#define ACPI_BUS_CLASS "system_bus"
29b71a1c 19#define ACPI_BUS_HID "LNXSYBUS"
1da177e4
LT
20#define ACPI_BUS_DEVICE_NAME "System Bus"
21
22static LIST_HEAD(acpi_device_list);
e49bd2dd 23static LIST_HEAD(acpi_bus_id_list);
1da177e4
LT
24DEFINE_SPINLOCK(acpi_device_lock);
25LIST_HEAD(acpi_wakeup_device_list);
26
e49bd2dd 27struct acpi_device_bus_id{
bb095854 28 char bus_id[15];
e49bd2dd
ZR
29 unsigned int instance_no;
30 struct list_head node;
1da177e4 31};
29b71a1c
TR
32
33/*
34 * Creates hid/cid(s) string needed for modalias and uevent
35 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
36 * char *modalias: "acpi:IBM0001:ACPI0001"
37*/
b3e572d2
AB
38static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
39 int size)
40{
29b71a1c
TR
41 int len;
42
43 if (!acpi_dev->flags.hardware_id)
44 return -ENODEV;
45
46 len = snprintf(modalias, size, "acpi:%s:",
47 acpi_dev->pnp.hardware_id);
48 if (len < 0 || len >= size)
49 return -EINVAL;
50 size -= len;
51
52 if (acpi_dev->flags.compatible_ids) {
53 struct acpi_compatible_id_list *cid_list;
54 int i;
55 int count;
56
57 cid_list = acpi_dev->pnp.cid_list;
58 for (i = 0; i < cid_list->count; i++) {
59 count = snprintf(&modalias[len], size, "%s:",
60 cid_list->id[i].value);
61 if (count < 0 || count >= size) {
87ecd5cd 62 printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
29b71a1c
TR
63 acpi_dev->pnp.device_name, i);
64 break;
65 }
66 len += count;
67 size -= count;
68 }
69 }
70
71 modalias[len] = '\0';
72 return len;
73}
74
75static ssize_t
76acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
77 struct acpi_device *acpi_dev = to_acpi_device(dev);
78 int len;
79
80 /* Device has no HID and no CID or string is >1024 */
81 len = create_modalias(acpi_dev, buf, 1024);
82 if (len <= 0)
83 return 0;
84 buf[len++] = '\n';
85 return len;
86}
87static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
88
4be44fcd 89static int acpi_eject_operation(acpi_handle handle, int lockable)
1da177e4 90{
1da177e4
LT
91 struct acpi_object_list arg_list;
92 union acpi_object arg;
93 acpi_status status = AE_OK;
1da177e4 94
1da177e4
LT
95 /*
96 * TBD: evaluate _PS3?
97 */
1da177e4 98
1da177e4
LT
99 if (lockable) {
100 arg_list.count = 1;
101 arg_list.pointer = &arg;
102 arg.type = ACPI_TYPE_INTEGER;
103 arg.integer.value = 0;
104 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
105 }
1da177e4 106
1da177e4
LT
107 arg_list.count = 1;
108 arg_list.pointer = &arg;
109 arg.type = ACPI_TYPE_INTEGER;
110 arg.integer.value = 1;
1da177e4 111
1da177e4
LT
112 /*
113 * TBD: _EJD support.
114 */
1da177e4 115
1da177e4
LT
116 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
117 if (ACPI_FAILURE(status)) {
4be44fcd 118 return (-ENODEV);
1da177e4 119 }
1da177e4 120
4be44fcd 121 return (0);
1da177e4
LT
122}
123
1da177e4 124static ssize_t
f883d9db
PM
125acpi_eject_store(struct device *d, struct device_attribute *attr,
126 const char *buf, size_t count)
1da177e4 127{
4be44fcd
LB
128 int result;
129 int ret = count;
130 int islockable;
131 acpi_status status;
132 acpi_handle handle;
133 acpi_object_type type = 0;
f883d9db 134 struct acpi_device *acpi_device = to_acpi_device(d);
1da177e4 135
1da177e4
LT
136 if ((!count) || (buf[0] != '1')) {
137 return -EINVAL;
138 }
1da177e4 139#ifndef FORCE_EJECT
f883d9db 140 if (acpi_device->driver == NULL) {
1da177e4
LT
141 ret = -ENODEV;
142 goto err;
143 }
144#endif
f883d9db
PM
145 status = acpi_get_type(acpi_device->handle, &type);
146 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
1da177e4
LT
147 ret = -ENODEV;
148 goto err;
149 }
1da177e4 150
f883d9db
PM
151 islockable = acpi_device->flags.lockable;
152 handle = acpi_device->handle;
9b6d97b6 153
f883d9db 154 result = acpi_bus_trim(acpi_device, 1);
1da177e4 155
1da177e4
LT
156 if (!result)
157 result = acpi_eject_operation(handle, islockable);
1da177e4 158
1da177e4
LT
159 if (result) {
160 ret = -EBUSY;
161 }
4be44fcd 162 err:
1da177e4 163 return ret;
1da177e4
LT
164}
165
f883d9db 166static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
1da177e4 167
e49bd2dd
ZR
168static ssize_t
169acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
170 struct acpi_device *acpi_dev = to_acpi_device(dev);
1da177e4 171
e49bd2dd 172 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
1da177e4 173}
e49bd2dd 174static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
1da177e4 175
e49bd2dd
ZR
176static ssize_t
177acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
178 struct acpi_device *acpi_dev = to_acpi_device(dev);
179 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
180 int result;
1da177e4 181
e49bd2dd
ZR
182 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
183 if(result)
184 goto end;
1da177e4 185
e49bd2dd
ZR
186 result = sprintf(buf, "%s\n", (char*)path.pointer);
187 kfree(path.pointer);
188 end:
189 return result;
1da177e4 190}
e49bd2dd 191static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
1da177e4 192
e49bd2dd 193static int acpi_device_setup_files(struct acpi_device *dev)
1da177e4 194{
f883d9db
PM
195 acpi_status status;
196 acpi_handle temp;
e49bd2dd 197 int result = 0;
1da177e4
LT
198
199 /*
e49bd2dd 200 * Devices gotten from FADT don't have a "path" attribute
1da177e4 201 */
e49bd2dd
ZR
202 if(dev->handle) {
203 result = device_create_file(&dev->dev, &dev_attr_path);
204 if(result)
205 goto end;
1da177e4
LT
206 }
207
e49bd2dd
ZR
208 if(dev->flags.hardware_id) {
209 result = device_create_file(&dev->dev, &dev_attr_hid);
210 if(result)
211 goto end;
212 }
1da177e4 213
29b71a1c
TR
214 if (dev->flags.hardware_id || dev->flags.compatible_ids){
215 result = device_create_file(&dev->dev, &dev_attr_modalias);
216 if(result)
217 goto end;
218 }
219
e49bd2dd
ZR
220 /*
221 * If device has _EJ0, 'eject' file is created that is used to trigger
222 * hot-removal function from userland.
223 */
f883d9db
PM
224 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
225 if (ACPI_SUCCESS(status))
e49bd2dd
ZR
226 result = device_create_file(&dev->dev, &dev_attr_eject);
227 end:
228 return result;
1da177e4
LT
229}
230
f883d9db 231static void acpi_device_remove_files(struct acpi_device *dev)
1da177e4 232{
f883d9db
PM
233 acpi_status status;
234 acpi_handle temp;
1da177e4 235
f883d9db
PM
236 /*
237 * If device has _EJ0, 'eject' file is created that is used to trigger
238 * hot-removal function from userland.
239 */
240 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
241 if (ACPI_SUCCESS(status))
242 device_remove_file(&dev->dev, &dev_attr_eject);
1da177e4 243
29b71a1c
TR
244 if (dev->flags.hardware_id || dev->flags.compatible_ids)
245 device_remove_file(&dev->dev, &dev_attr_modalias);
246
e49bd2dd
ZR
247 if(dev->flags.hardware_id)
248 device_remove_file(&dev->dev, &dev_attr_hid);
249 if(dev->handle)
250 device_remove_file(&dev->dev, &dev_attr_path);
1da177e4 251}
1da177e4 252/* --------------------------------------------------------------------------
9e89dde2 253 ACPI Bus operations
1da177e4 254 -------------------------------------------------------------------------- */
29b71a1c
TR
255
256int acpi_match_device_ids(struct acpi_device *device,
257 const struct acpi_device_id *ids)
258{
259 const struct acpi_device_id *id;
260
261 if (device->flags.hardware_id) {
262 for (id = ids; id->id[0]; id++) {
263 if (!strcmp((char*)id->id, device->pnp.hardware_id))
264 return 0;
265 }
266 }
267
268 if (device->flags.compatible_ids) {
269 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
270 int i;
271
272 for (id = ids; id->id[0]; id++) {
273 /* compare multiple _CID entries against driver ids */
274 for (i = 0; i < cid_list->count; i++) {
275 if (!strcmp((char*)id->id,
276 cid_list->id[i].value))
277 return 0;
278 }
279 }
280 }
281
282 return -ENOENT;
283}
284EXPORT_SYMBOL(acpi_match_device_ids);
285
1890a97a 286static void acpi_device_release(struct device *dev)
1da177e4 287{
1890a97a 288 struct acpi_device *acpi_dev = to_acpi_device(dev);
1da177e4 289
1890a97a
PM
290 kfree(acpi_dev->pnp.cid_list);
291 kfree(acpi_dev);
1da177e4
LT
292}
293
5d9464a4 294static int acpi_device_suspend(struct device *dev, pm_message_t state)
1da177e4 295{
5d9464a4
PM
296 struct acpi_device *acpi_dev = to_acpi_device(dev);
297 struct acpi_driver *acpi_drv = acpi_dev->driver;
1da177e4 298
5d9464a4
PM
299 if (acpi_drv && acpi_drv->ops.suspend)
300 return acpi_drv->ops.suspend(acpi_dev, state);
1da177e4
LT
301 return 0;
302}
1da177e4 303
5d9464a4 304static int acpi_device_resume(struct device *dev)
9e89dde2 305{
5d9464a4
PM
306 struct acpi_device *acpi_dev = to_acpi_device(dev);
307 struct acpi_driver *acpi_drv = acpi_dev->driver;
1da177e4 308
5d9464a4
PM
309 if (acpi_drv && acpi_drv->ops.resume)
310 return acpi_drv->ops.resume(acpi_dev);
d550d98d 311 return 0;
1da177e4
LT
312}
313
5d9464a4 314static int acpi_bus_match(struct device *dev, struct device_driver *drv)
9e89dde2 315{
5d9464a4
PM
316 struct acpi_device *acpi_dev = to_acpi_device(dev);
317 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1da177e4 318
29b71a1c 319 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
5d9464a4 320}
1da177e4 321
7eff2e7a 322static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
1da177e4 323{
5d9464a4 324 struct acpi_device *acpi_dev = to_acpi_device(dev);
7eff2e7a 325 int len;
5d9464a4 326
7eff2e7a
KS
327 if (add_uevent_var(env, "MODALIAS="))
328 return -ENOMEM;
329 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
330 sizeof(env->buf) - env->buflen);
331 if (len >= (sizeof(env->buf) - env->buflen))
332 return -ENOMEM;
333 env->buflen += len;
9e89dde2 334 return 0;
1da177e4
LT
335}
336
5d9464a4
PM
337static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
338static int acpi_start_single_object(struct acpi_device *);
339static int acpi_device_probe(struct device * dev)
1da177e4 340{
5d9464a4
PM
341 struct acpi_device *acpi_dev = to_acpi_device(dev);
342 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
343 int ret;
344
345 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
346 if (!ret) {
c4168bff
LS
347 if (acpi_dev->bus_ops.acpi_op_start)
348 acpi_start_single_object(acpi_dev);
5d9464a4
PM
349 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
350 "Found driver [%s] for device [%s]\n",
351 acpi_drv->name, acpi_dev->pnp.bus_id));
352 get_device(dev);
353 }
354 return ret;
355}
1da177e4 356
5d9464a4
PM
357static int acpi_device_remove(struct device * dev)
358{
359 struct acpi_device *acpi_dev = to_acpi_device(dev);
360 struct acpi_driver *acpi_drv = acpi_dev->driver;
361
362 if (acpi_drv) {
363 if (acpi_drv->ops.stop)
96333578 364 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
5d9464a4 365 if (acpi_drv->ops.remove)
96333578 366 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
1da177e4 367 }
5d9464a4
PM
368 acpi_dev->driver = NULL;
369 acpi_driver_data(dev) = NULL;
1da177e4 370
5d9464a4 371 put_device(dev);
9e89dde2
ZR
372 return 0;
373}
1da177e4 374
5d9464a4 375static void acpi_device_shutdown(struct device *dev)
1da177e4 376{
5d9464a4
PM
377 struct acpi_device *acpi_dev = to_acpi_device(dev);
378 struct acpi_driver *acpi_drv = acpi_dev->driver;
1da177e4 379
5d9464a4
PM
380 if (acpi_drv && acpi_drv->ops.shutdown)
381 acpi_drv->ops.shutdown(acpi_dev);
1da177e4 382
5d9464a4 383 return ;
1da177e4
LT
384}
385
55955aad 386struct bus_type acpi_bus_type = {
9e89dde2
ZR
387 .name = "acpi",
388 .suspend = acpi_device_suspend,
389 .resume = acpi_device_resume,
5d9464a4
PM
390 .shutdown = acpi_device_shutdown,
391 .match = acpi_bus_match,
392 .probe = acpi_device_probe,
393 .remove = acpi_device_remove,
394 .uevent = acpi_device_uevent,
9e89dde2
ZR
395};
396
e49bd2dd 397static int acpi_device_register(struct acpi_device *device,
9e89dde2 398 struct acpi_device *parent)
1da177e4 399{
4be44fcd 400 int result;
e49bd2dd
ZR
401 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
402 int found = 0;
9e89dde2
ZR
403 /*
404 * Linkage
405 * -------
406 * Link this device to its parent and siblings.
407 */
408 INIT_LIST_HEAD(&device->children);
409 INIT_LIST_HEAD(&device->node);
410 INIT_LIST_HEAD(&device->g_list);
411 INIT_LIST_HEAD(&device->wakeup_list);
1da177e4 412
e49bd2dd
ZR
413 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
414 if (!new_bus_id) {
415 printk(KERN_ERR PREFIX "Memory allocation error\n");
416 return -ENOMEM;
1da177e4 417 }
e49bd2dd 418
9e89dde2 419 spin_lock(&acpi_device_lock);
e49bd2dd
ZR
420 /*
421 * Find suitable bus_id and instance number in acpi_bus_id_list
422 * If failed, create one and link it into acpi_bus_id_list
423 */
424 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
bb095854 425 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
e49bd2dd
ZR
426 acpi_device_bus_id->instance_no ++;
427 found = 1;
428 kfree(new_bus_id);
429 break;
430 }
1da177e4 431 }
e49bd2dd
ZR
432 if(!found) {
433 acpi_device_bus_id = new_bus_id;
bb095854 434 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
e49bd2dd
ZR
435 acpi_device_bus_id->instance_no = 0;
436 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1da177e4 437 }
e49bd2dd 438 sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1da177e4 439
9e89dde2
ZR
440 if (device->parent) {
441 list_add_tail(&device->node, &device->parent->children);
442 list_add_tail(&device->g_list, &device->parent->g_list);
443 } else
444 list_add_tail(&device->g_list, &acpi_device_list);
445 if (device->wakeup.flags.valid)
446 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
447 spin_unlock(&acpi_device_lock);
1da177e4 448
1890a97a
PM
449 if (device->parent)
450 device->dev.parent = &parent->dev;
451 device->dev.bus = &acpi_bus_type;
452 device_initialize(&device->dev);
1890a97a 453 device->dev.release = &acpi_device_release;
e49bd2dd
ZR
454 result = device_add(&device->dev);
455 if(result) {
87ecd5cd 456 printk(KERN_ERR PREFIX "Error adding device %s", device->dev.bus_id);
e49bd2dd 457 goto end;
1da177e4 458 }
1da177e4 459
e49bd2dd
ZR
460 result = acpi_device_setup_files(device);
461 if(result)
462 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id));
1da177e4 463
96333578 464 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
1da177e4 465 return 0;
e49bd2dd
ZR
466 end:
467 spin_lock(&acpi_device_lock);
468 if (device->parent) {
469 list_del(&device->node);
470 list_del(&device->g_list);
471 } else
472 list_del(&device->g_list);
473 list_del(&device->wakeup_list);
474 spin_unlock(&acpi_device_lock);
475 return result;
1da177e4
LT
476}
477
9e89dde2
ZR
478static void acpi_device_unregister(struct acpi_device *device, int type)
479{
480 spin_lock(&acpi_device_lock);
481 if (device->parent) {
482 list_del(&device->node);
483 list_del(&device->g_list);
484 } else
485 list_del(&device->g_list);
1da177e4 486
9e89dde2 487 list_del(&device->wakeup_list);
9e89dde2 488 spin_unlock(&acpi_device_lock);
1da177e4 489
9e89dde2 490 acpi_detach_data(device->handle, acpi_bus_data_handler);
1890a97a 491
f883d9db 492 acpi_device_remove_files(device);
1890a97a 493 device_unregister(&device->dev);
1da177e4
LT
494}
495
9e89dde2
ZR
496/* --------------------------------------------------------------------------
497 Driver Management
498 -------------------------------------------------------------------------- */
1da177e4 499/**
d758a8fa
RD
500 * acpi_bus_driver_init - add a device to a driver
501 * @device: the device to add and initialize
502 * @driver: driver for the device
503 *
1da177e4 504 * Used to initialize a device via its device driver. Called whenever a
1890a97a 505 * driver is bound to a device. Invokes the driver's add() ops.
1da177e4
LT
506 */
507static int
4be44fcd 508acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
1da177e4 509{
4be44fcd 510 int result = 0;
1da177e4 511
1da177e4
LT
512
513 if (!device || !driver)
d550d98d 514 return -EINVAL;
1da177e4
LT
515
516 if (!driver->ops.add)
d550d98d 517 return -ENOSYS;
1da177e4
LT
518
519 result = driver->ops.add(device);
520 if (result) {
521 device->driver = NULL;
522 acpi_driver_data(device) = NULL;
d550d98d 523 return result;
1da177e4
LT
524 }
525
526 device->driver = driver;
527
528 /*
529 * TBD - Configuration Management: Assign resources to device based
530 * upon possible configuration and currently allocated resources.
531 */
532
4be44fcd
LB
533 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
534 "Driver successfully bound to device\n"));
d550d98d 535 return 0;
3fb02738
RS
536}
537
8713cbef 538static int acpi_start_single_object(struct acpi_device *device)
3fb02738
RS
539{
540 int result = 0;
541 struct acpi_driver *driver;
542
3fb02738
RS
543
544 if (!(driver = device->driver))
d550d98d 545 return 0;
3fb02738 546
1da177e4
LT
547 if (driver->ops.start) {
548 result = driver->ops.start(device);
549 if (result && driver->ops.remove)
550 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
1da177e4
LT
551 }
552
d550d98d 553 return result;
1da177e4
LT
554}
555
9e89dde2
ZR
556/**
557 * acpi_bus_register_driver - register a driver with the ACPI bus
558 * @driver: driver being registered
559 *
560 * Registers a driver with the ACPI bus. Searches the namespace for all
561 * devices that match the driver's criteria and binds. Returns zero for
562 * success or a negative error status for failure.
563 */
564int acpi_bus_register_driver(struct acpi_driver *driver)
1da177e4 565{
1890a97a 566 int ret;
1da177e4 567
9e89dde2
ZR
568 if (acpi_disabled)
569 return -ENODEV;
1890a97a
PM
570 driver->drv.name = driver->name;
571 driver->drv.bus = &acpi_bus_type;
572 driver->drv.owner = driver->owner;
1da177e4 573
1890a97a
PM
574 ret = driver_register(&driver->drv);
575 return ret;
9e89dde2 576}
1da177e4 577
9e89dde2
ZR
578EXPORT_SYMBOL(acpi_bus_register_driver);
579
580/**
581 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
582 * @driver: driver to unregister
583 *
584 * Unregisters a driver with the ACPI bus. Searches the namespace for all
585 * devices that match the driver's criteria and unbinds.
586 */
587void acpi_bus_unregister_driver(struct acpi_driver *driver)
588{
1890a97a 589 driver_unregister(&driver->drv);
9e89dde2
ZR
590}
591
592EXPORT_SYMBOL(acpi_bus_unregister_driver);
593
9e89dde2
ZR
594/* --------------------------------------------------------------------------
595 Device Enumeration
596 -------------------------------------------------------------------------- */
597acpi_status
598acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
599{
600 acpi_status status;
601 acpi_handle tmp;
602 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
603 union acpi_object *obj;
604
605 status = acpi_get_handle(handle, "_EJD", &tmp);
606 if (ACPI_FAILURE(status))
607 return status;
608
609 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
610 if (ACPI_SUCCESS(status)) {
611 obj = buffer.pointer;
3b5fee59
HM
612 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
613 ejd);
9e89dde2
ZR
614 kfree(buffer.pointer);
615 }
616 return status;
617}
618EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
619
620void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
621{
622
623 /* TBD */
624
625 return;
626}
627
9e89dde2
ZR
628static int acpi_bus_get_perf_flags(struct acpi_device *device)
629{
630 device->performance.state = ACPI_STATE_UNKNOWN;
631 return 0;
632}
633
634static acpi_status
635acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
636 union acpi_object *package)
637{
638 int i = 0;
639 union acpi_object *element = NULL;
640
641 if (!device || !package || (package->package.count < 2))
642 return AE_BAD_PARAMETER;
643
644 element = &(package->package.elements[0]);
645 if (!element)
646 return AE_BAD_PARAMETER;
647 if (element->type == ACPI_TYPE_PACKAGE) {
648 if ((element->package.count < 2) ||
649 (element->package.elements[0].type !=
650 ACPI_TYPE_LOCAL_REFERENCE)
651 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
652 return AE_BAD_DATA;
653 device->wakeup.gpe_device =
654 element->package.elements[0].reference.handle;
655 device->wakeup.gpe_number =
656 (u32) element->package.elements[1].integer.value;
657 } else if (element->type == ACPI_TYPE_INTEGER) {
658 device->wakeup.gpe_number = element->integer.value;
659 } else
660 return AE_BAD_DATA;
661
662 element = &(package->package.elements[1]);
663 if (element->type != ACPI_TYPE_INTEGER) {
664 return AE_BAD_DATA;
665 }
666 device->wakeup.sleep_state = element->integer.value;
667
668 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
669 return AE_NO_MEMORY;
670 }
671 device->wakeup.resources.count = package->package.count - 2;
672 for (i = 0; i < device->wakeup.resources.count; i++) {
673 element = &(package->package.elements[i + 2]);
674 if (element->type != ACPI_TYPE_ANY) {
675 return AE_BAD_DATA;
1da177e4 676 }
9e89dde2
ZR
677
678 device->wakeup.resources.handles[i] = element->reference.handle;
1da177e4 679 }
9e89dde2
ZR
680
681 return AE_OK;
1da177e4
LT
682}
683
9e89dde2 684static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1da177e4 685{
9e89dde2
ZR
686 acpi_status status = 0;
687 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
688 union acpi_object *package = NULL;
1da177e4 689
29b71a1c
TR
690 struct acpi_device_id button_device_ids[] = {
691 {"PNP0C0D", 0},
692 {"PNP0C0C", 0},
693 {"PNP0C0E", 0},
694 {"", 0},
695 };
696
1da177e4 697
9e89dde2
ZR
698 /* _PRW */
699 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
700 if (ACPI_FAILURE(status)) {
701 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
702 goto end;
1da177e4 703 }
1da177e4 704
9e89dde2
ZR
705 package = (union acpi_object *)buffer.pointer;
706 status = acpi_bus_extract_wakeup_device_power_package(device, package);
707 if (ACPI_FAILURE(status)) {
708 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
709 goto end;
710 }
1da177e4 711
9e89dde2 712 kfree(buffer.pointer);
1da177e4 713
9e89dde2
ZR
714 device->wakeup.flags.valid = 1;
715 /* Power button, Lid switch always enable wakeup */
29b71a1c 716 if (!acpi_match_device_ids(device, button_device_ids))
9e89dde2 717 device->wakeup.flags.run_wake = 1;
1da177e4 718
9e89dde2
ZR
719 end:
720 if (ACPI_FAILURE(status))
721 device->flags.wake_capable = 0;
d550d98d 722 return 0;
1da177e4 723}
1da177e4 724
9e89dde2 725static int acpi_bus_get_power_flags(struct acpi_device *device)
1da177e4 726{
9e89dde2
ZR
727 acpi_status status = 0;
728 acpi_handle handle = NULL;
729 u32 i = 0;
1a365616 730
4be44fcd 731
9e89dde2
ZR
732 /*
733 * Power Management Flags
734 */
735 status = acpi_get_handle(device->handle, "_PSC", &handle);
736 if (ACPI_SUCCESS(status))
737 device->power.flags.explicit_get = 1;
738 status = acpi_get_handle(device->handle, "_IRC", &handle);
739 if (ACPI_SUCCESS(status))
740 device->power.flags.inrush_current = 1;
1da177e4 741
9e89dde2
ZR
742 /*
743 * Enumerate supported power management states
744 */
745 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
746 struct acpi_device_power_state *ps = &device->power.states[i];
747 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
1da177e4 748
9e89dde2
ZR
749 /* Evaluate "_PRx" to se if power resources are referenced */
750 acpi_evaluate_reference(device->handle, object_name, NULL,
751 &ps->resources);
752 if (ps->resources.count) {
753 device->power.flags.power_resources = 1;
754 ps->flags.valid = 1;
1da177e4 755 }
1da177e4 756
9e89dde2
ZR
757 /* Evaluate "_PSx" to see if we can do explicit sets */
758 object_name[2] = 'S';
759 status = acpi_get_handle(device->handle, object_name, &handle);
760 if (ACPI_SUCCESS(status)) {
761 ps->flags.explicit_set = 1;
762 ps->flags.valid = 1;
1da177e4 763 }
1da177e4 764
9e89dde2
ZR
765 /* State is valid if we have some power control */
766 if (ps->resources.count || ps->flags.explicit_set)
767 ps->flags.valid = 1;
1da177e4 768
9e89dde2
ZR
769 ps->power = -1; /* Unknown - driver assigned */
770 ps->latency = -1; /* Unknown - driver assigned */
771 }
1da177e4 772
9e89dde2
ZR
773 /* Set defaults for D0 and D3 states (always valid) */
774 device->power.states[ACPI_STATE_D0].flags.valid = 1;
775 device->power.states[ACPI_STATE_D0].power = 100;
776 device->power.states[ACPI_STATE_D3].flags.valid = 1;
777 device->power.states[ACPI_STATE_D3].power = 0;
c8f7a62c 778
9e89dde2 779 /* TBD: System wake support and resource requirements. */
c8f7a62c 780
9e89dde2 781 device->power.state = ACPI_STATE_UNKNOWN;
c8f7a62c 782
9e89dde2
ZR
783 return 0;
784}
c8f7a62c 785
4be44fcd 786static int acpi_bus_get_flags(struct acpi_device *device)
1da177e4 787{
4be44fcd
LB
788 acpi_status status = AE_OK;
789 acpi_handle temp = NULL;
1da177e4 790
1da177e4
LT
791
792 /* Presence of _STA indicates 'dynamic_status' */
793 status = acpi_get_handle(device->handle, "_STA", &temp);
794 if (ACPI_SUCCESS(status))
795 device->flags.dynamic_status = 1;
796
797 /* Presence of _CID indicates 'compatible_ids' */
798 status = acpi_get_handle(device->handle, "_CID", &temp);
799 if (ACPI_SUCCESS(status))
800 device->flags.compatible_ids = 1;
801
802 /* Presence of _RMV indicates 'removable' */
803 status = acpi_get_handle(device->handle, "_RMV", &temp);
804 if (ACPI_SUCCESS(status))
805 device->flags.removable = 1;
806
807 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
808 status = acpi_get_handle(device->handle, "_EJD", &temp);
809 if (ACPI_SUCCESS(status))
810 device->flags.ejectable = 1;
811 else {
812 status = acpi_get_handle(device->handle, "_EJ0", &temp);
813 if (ACPI_SUCCESS(status))
814 device->flags.ejectable = 1;
815 }
816
817 /* Presence of _LCK indicates 'lockable' */
818 status = acpi_get_handle(device->handle, "_LCK", &temp);
819 if (ACPI_SUCCESS(status))
820 device->flags.lockable = 1;
821
822 /* Presence of _PS0|_PR0 indicates 'power manageable' */
823 status = acpi_get_handle(device->handle, "_PS0", &temp);
824 if (ACPI_FAILURE(status))
825 status = acpi_get_handle(device->handle, "_PR0", &temp);
826 if (ACPI_SUCCESS(status))
827 device->flags.power_manageable = 1;
828
829 /* Presence of _PRW indicates wake capable */
830 status = acpi_get_handle(device->handle, "_PRW", &temp);
831 if (ACPI_SUCCESS(status))
832 device->flags.wake_capable = 1;
833
3c5f9be4 834 /* TBD: Performance management */
1da177e4 835
d550d98d 836 return 0;
1da177e4
LT
837}
838
4be44fcd
LB
839static void acpi_device_get_busid(struct acpi_device *device,
840 acpi_handle handle, int type)
1da177e4 841{
4be44fcd
LB
842 char bus_id[5] = { '?', 0 };
843 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
844 int i = 0;
1da177e4
LT
845
846 /*
847 * Bus ID
848 * ------
849 * The device's Bus ID is simply the object name.
850 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
851 */
852 switch (type) {
853 case ACPI_BUS_TYPE_SYSTEM:
854 strcpy(device->pnp.bus_id, "ACPI");
855 break;
856 case ACPI_BUS_TYPE_POWER_BUTTON:
857 strcpy(device->pnp.bus_id, "PWRF");
858 break;
859 case ACPI_BUS_TYPE_SLEEP_BUTTON:
860 strcpy(device->pnp.bus_id, "SLPF");
861 break;
862 default:
863 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
864 /* Clean up trailing underscores (if any) */
865 for (i = 3; i > 1; i--) {
866 if (bus_id[i] == '_')
867 bus_id[i] = '\0';
868 else
869 break;
870 }
871 strcpy(device->pnp.bus_id, bus_id);
872 break;
873 }
874}
875
ae843332
ZR
876static int
877acpi_video_bus_match(struct acpi_device *device)
878{
879 acpi_handle h_dummy1;
880 acpi_handle h_dummy2;
881 acpi_handle h_dummy3;
882
883
884 if (!device)
885 return -EINVAL;
886
887 /* Since there is no HID, CID for ACPI Video drivers, we have
888 * to check well known required nodes for each feature we support.
889 */
890
891 /* Does this device able to support video switching ? */
892 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
893 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
894 return 0;
895
896 /* Does this device able to retrieve a video ROM ? */
897 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
898 return 0;
899
900 /* Does this device able to configure which video head to be POSTed ? */
901 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
902 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
903 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
904 return 0;
905
906 return -ENODEV;
907}
908
54735266
ZR
909/*
910 * acpi_bay_match - see if a device is an ejectable driver bay
911 *
912 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
913 * then we can safely call it an ejectable drive bay
914 */
915static int acpi_bay_match(struct acpi_device *device){
916 acpi_status status;
917 acpi_handle handle;
918 acpi_handle tmp;
919 acpi_handle phandle;
920
921 handle = device->handle;
922
923 status = acpi_get_handle(handle, "_EJ0", &tmp);
924 if (ACPI_FAILURE(status))
925 return -ENODEV;
926
927 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
928 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
929 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
930 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
931 return 0;
932
933 if (acpi_get_parent(handle, &phandle))
934 return -ENODEV;
935
936 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
937 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
938 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
939 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
940 return 0;
941
942 return -ENODEV;
943}
944
3620f2f2
FS
945/*
946 * acpi_dock_match - see if a device has a _DCK method
947 */
948static int acpi_dock_match(struct acpi_device *device)
949{
950 acpi_handle tmp;
951 return acpi_get_handle(device->handle, "_DCK", &tmp);
952}
953
4be44fcd
LB
954static void acpi_device_set_id(struct acpi_device *device,
955 struct acpi_device *parent, acpi_handle handle,
956 int type)
1da177e4 957{
4be44fcd
LB
958 struct acpi_device_info *info;
959 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
960 char *hid = NULL;
961 char *uid = NULL;
1da177e4 962 struct acpi_compatible_id_list *cid_list = NULL;
3620f2f2 963 const char *cid_add = NULL;
4be44fcd 964 acpi_status status;
1da177e4
LT
965
966 switch (type) {
967 case ACPI_BUS_TYPE_DEVICE:
968 status = acpi_get_object_info(handle, &buffer);
969 if (ACPI_FAILURE(status)) {
96b2dd1f 970 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1da177e4
LT
971 return;
972 }
973
974 info = buffer.pointer;
975 if (info->valid & ACPI_VALID_HID)
976 hid = info->hardware_id.value;
977 if (info->valid & ACPI_VALID_UID)
978 uid = info->unique_id.value;
979 if (info->valid & ACPI_VALID_CID)
980 cid_list = &info->compatibility_id;
981 if (info->valid & ACPI_VALID_ADR) {
982 device->pnp.bus_address = info->address;
983 device->flags.bus_address = 1;
984 }
ae843332 985
3620f2f2
FS
986 /* If we have a video/bay/dock device, add our selfdefined
987 HID to the CID list. Like that the video/bay/dock drivers
988 will get autoloaded and the device might still match
989 against another driver.
990 */
991 if (ACPI_SUCCESS(acpi_video_bus_match(device)))
992 cid_add = ACPI_VIDEO_HID;
993 else if (ACPI_SUCCESS(acpi_bay_match(device)))
994 cid_add = ACPI_BAY_HID;
995 else if (ACPI_SUCCESS(acpi_dock_match(device)))
996 cid_add = ACPI_DOCK_HID;
54735266 997
1da177e4
LT
998 break;
999 case ACPI_BUS_TYPE_POWER:
1000 hid = ACPI_POWER_HID;
1001 break;
1002 case ACPI_BUS_TYPE_PROCESSOR:
1003 hid = ACPI_PROCESSOR_HID;
1004 break;
1005 case ACPI_BUS_TYPE_SYSTEM:
1006 hid = ACPI_SYSTEM_HID;
1007 break;
1008 case ACPI_BUS_TYPE_THERMAL:
1009 hid = ACPI_THERMAL_HID;
1010 break;
1011 case ACPI_BUS_TYPE_POWER_BUTTON:
1012 hid = ACPI_BUTTON_HID_POWERF;
1013 break;
1014 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1015 hid = ACPI_BUTTON_HID_SLEEPF;
1016 break;
1017 }
1018
1019 /*
1020 * \_SB
1021 * ----
1022 * Fix for the system root bus device -- the only root-level device.
1023 */
c51a4de8 1024 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
1da177e4
LT
1025 hid = ACPI_BUS_HID;
1026 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1027 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1028 }
1029
1030 if (hid) {
1031 strcpy(device->pnp.hardware_id, hid);
1032 device->flags.hardware_id = 1;
1033 }
1034 if (uid) {
1035 strcpy(device->pnp.unique_id, uid);
1036 device->flags.unique_id = 1;
1037 }
3620f2f2
FS
1038 if (cid_list || cid_add) {
1039 struct acpi_compatible_id_list *list;
1040 int size = 0;
1041 int count = 0;
1042
1043 if (cid_list) {
1044 size = cid_list->size;
1045 } else if (cid_add) {
1046 size = sizeof(struct acpi_compatible_id_list);
1047 cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
1048 if (!cid_list) {
1049 printk(KERN_ERR "Memory allocation error\n");
1050 kfree(buffer.pointer);
1051 return;
1052 } else {
1053 cid_list->count = 0;
1054 cid_list->size = size;
1055 }
1056 }
1057 if (cid_add)
1058 size += sizeof(struct acpi_compatible_id);
1059 list = kmalloc(size, GFP_KERNEL);
1060
1061 if (list) {
1062 if (cid_list) {
1063 memcpy(list, cid_list, cid_list->size);
1064 count = cid_list->count;
1065 }
1066 if (cid_add) {
1067 strncpy(list->id[count].value, cid_add,
1068 ACPI_MAX_CID_LENGTH);
1069 count++;
1070 device->flags.compatible_ids = 1;
1071 }
1072 list->size = size;
1073 list->count = count;
1074 device->pnp.cid_list = list;
1075 } else
87ecd5cd 1076 printk(KERN_ERR PREFIX "Memory allocation error\n");
1da177e4
LT
1077 }
1078
02438d87 1079 kfree(buffer.pointer);
1da177e4
LT
1080}
1081
4be44fcd 1082static int acpi_device_set_context(struct acpi_device *device, int type)
1da177e4
LT
1083{
1084 acpi_status status = AE_OK;
1085 int result = 0;
1086 /*
1087 * Context
1088 * -------
1089 * Attach this 'struct acpi_device' to the ACPI object. This makes
1090 * resolutions from handle->device very efficient. Note that we need
1091 * to be careful with fixed-feature devices as they all attach to the
1092 * root object.
1093 */
4be44fcd 1094 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
1da177e4
LT
1095 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
1096 status = acpi_attach_data(device->handle,
4be44fcd 1097 acpi_bus_data_handler, device);
1da177e4
LT
1098
1099 if (ACPI_FAILURE(status)) {
87ecd5cd 1100 printk(KERN_ERR PREFIX "Error attaching device data\n");
1da177e4
LT
1101 result = -ENODEV;
1102 }
1103 }
1104 return result;
1105}
1106
4be44fcd 1107static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1da177e4 1108{
1da177e4 1109 if (!dev)
d550d98d 1110 return -EINVAL;
1da177e4 1111
96333578 1112 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1890a97a 1113 device_release_driver(&dev->dev);
1da177e4
LT
1114
1115 if (!rmdevice)
d550d98d 1116 return 0;
1da177e4 1117
2786f6e3
RZ
1118 /*
1119 * unbind _ADR-Based Devices when hot removal
1120 */
1da177e4
LT
1121 if (dev->flags.bus_address) {
1122 if ((dev->parent) && (dev->parent->ops.unbind))
1123 dev->parent->ops.unbind(dev);
1124 }
1da177e4
LT
1125 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1126
d550d98d 1127 return 0;
1da177e4
LT
1128}
1129
3620f2f2
FS
1130static int
1131acpi_is_child_device(struct acpi_device *device,
1132 int (*matcher)(struct acpi_device *))
1133{
1134 int result = -ENODEV;
1135
1136 do {
1137 if (ACPI_SUCCESS(matcher(device)))
1138 return AE_OK;
1139 } while ((device = device->parent));
1140
1141 return result;
1142}
1143
3fb02738 1144static int
4be44fcd 1145acpi_add_single_object(struct acpi_device **child,
c4168bff
LS
1146 struct acpi_device *parent, acpi_handle handle, int type,
1147 struct acpi_bus_ops *ops)
1da177e4 1148{
4be44fcd
LB
1149 int result = 0;
1150 struct acpi_device *device = NULL;
1da177e4 1151
1da177e4
LT
1152
1153 if (!child)
d550d98d 1154 return -EINVAL;
1da177e4 1155
36bcbec7 1156 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1da177e4 1157 if (!device) {
6468463a 1158 printk(KERN_ERR PREFIX "Memory allocation error\n");
d550d98d 1159 return -ENOMEM;
1da177e4 1160 }
1da177e4
LT
1161
1162 device->handle = handle;
1163 device->parent = parent;
c4168bff
LS
1164 device->bus_ops = *ops; /* workround for not call .start */
1165
1da177e4 1166
4be44fcd 1167 acpi_device_get_busid(device, handle, type);
1da177e4
LT
1168
1169 /*
1170 * Flags
1171 * -----
1172 * Get prior to calling acpi_bus_get_status() so we know whether
1173 * or not _STA is present. Note that we only look for object
1174 * handles -- cannot evaluate objects until we know the device is
1175 * present and properly initialized.
1176 */
1177 result = acpi_bus_get_flags(device);
1178 if (result)
1179 goto end;
1180
1181 /*
1182 * Status
1183 * ------
6940faba
KT
1184 * See if the device is present. We always assume that non-Device
1185 * and non-Processor objects (e.g. thermal zones, power resources,
1186 * etc.) are present, functioning, etc. (at least when parent object
1187 * is present). Note that _STA has a different meaning for some
1188 * objects (e.g. power resources) so we need to be careful how we use
1189 * it.
1da177e4
LT
1190 */
1191 switch (type) {
6940faba 1192 case ACPI_BUS_TYPE_PROCESSOR:
1da177e4
LT
1193 case ACPI_BUS_TYPE_DEVICE:
1194 result = acpi_bus_get_status(device);
3620f2f2
FS
1195 if (ACPI_FAILURE(result)) {
1196 result = -ENODEV;
1da177e4
LT
1197 goto end;
1198 }
3620f2f2
FS
1199 if (!device->status.present) {
1200 /* Bay and dock should be handled even if absent */
1201 if (!ACPI_SUCCESS(
1202 acpi_is_child_device(device, acpi_bay_match)) &&
1203 !ACPI_SUCCESS(
1204 acpi_is_child_device(device, acpi_dock_match))) {
1205 result = -ENODEV;
1206 goto end;
1207 }
1208 }
1da177e4
LT
1209 break;
1210 default:
0c0e8921
BH
1211 STRUCT_TO_INT(device->status) =
1212 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1213 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
1da177e4
LT
1214 break;
1215 }
1216
1217 /*
1218 * Initialize Device
1219 * -----------------
1220 * TBD: Synch with Core's enumeration/initialization process.
1221 */
1222
1223 /*
1224 * Hardware ID, Unique ID, & Bus Address
1225 * -------------------------------------
1226 */
4be44fcd 1227 acpi_device_set_id(device, parent, handle, type);
1da177e4
LT
1228
1229 /*
1230 * Power Management
1231 * ----------------
1232 */
1233 if (device->flags.power_manageable) {
1234 result = acpi_bus_get_power_flags(device);
1235 if (result)
1236 goto end;
1237 }
1238
4be44fcd 1239 /*
1da177e4
LT
1240 * Wakeup device management
1241 *-----------------------
1242 */
1243 if (device->flags.wake_capable) {
1244 result = acpi_bus_get_wakeup_device_flags(device);
1245 if (result)
1246 goto end;
1247 }
1248
1249 /*
1250 * Performance Management
1251 * ----------------------
1252 */
1253 if (device->flags.performance_manageable) {
1254 result = acpi_bus_get_perf_flags(device);
1255 if (result)
1256 goto end;
1257 }
1258
4be44fcd 1259 if ((result = acpi_device_set_context(device, type)))
1da177e4
LT
1260 goto end;
1261
e49bd2dd 1262 result = acpi_device_register(device, parent);
1da177e4
LT
1263
1264 /*
2786f6e3 1265 * Bind _ADR-Based Devices when hot add
1da177e4
LT
1266 */
1267 if (device->flags.bus_address) {
1268 if (device->parent && device->parent->ops.bind)
1269 device->parent->ops.bind(device);
1270 }
1271
4be44fcd 1272 end:
1da177e4
LT
1273 if (!result)
1274 *child = device;
1275 else {
6044ec88 1276 kfree(device->pnp.cid_list);
1da177e4
LT
1277 kfree(device);
1278 }
1279
d550d98d 1280 return result;
1da177e4 1281}
1da177e4 1282
4be44fcd 1283static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1da177e4 1284{
4be44fcd
LB
1285 acpi_status status = AE_OK;
1286 struct acpi_device *parent = NULL;
1287 struct acpi_device *child = NULL;
1288 acpi_handle phandle = NULL;
1289 acpi_handle chandle = NULL;
1290 acpi_object_type type = 0;
1291 u32 level = 1;
1da177e4 1292
1da177e4
LT
1293
1294 if (!start)
d550d98d 1295 return -EINVAL;
1da177e4
LT
1296
1297 parent = start;
1298 phandle = start->handle;
4be44fcd 1299
1da177e4
LT
1300 /*
1301 * Parse through the ACPI namespace, identify all 'devices', and
1302 * create a new 'struct acpi_device' for each.
1303 */
1304 while ((level > 0) && parent) {
1305
1306 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
4be44fcd 1307 chandle, &chandle);
1da177e4
LT
1308
1309 /*
1310 * If this scope is exhausted then move our way back up.
1311 */
1312 if (ACPI_FAILURE(status)) {
1313 level--;
1314 chandle = phandle;
1315 acpi_get_parent(phandle, &phandle);
1316 if (parent->parent)
1317 parent = parent->parent;
1318 continue;
1319 }
1320
1321 status = acpi_get_type(chandle, &type);
1322 if (ACPI_FAILURE(status))
1323 continue;
1324
1325 /*
1326 * If this is a scope object then parse it (depth-first).
1327 */
1328 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1329 level++;
1330 phandle = chandle;
1331 chandle = NULL;
1332 continue;
1333 }
1334
1335 /*
1336 * We're only interested in objects that we consider 'devices'.
1337 */
1338 switch (type) {
1339 case ACPI_TYPE_DEVICE:
1340 type = ACPI_BUS_TYPE_DEVICE;
1341 break;
1342 case ACPI_TYPE_PROCESSOR:
1343 type = ACPI_BUS_TYPE_PROCESSOR;
1344 break;
1345 case ACPI_TYPE_THERMAL:
1346 type = ACPI_BUS_TYPE_THERMAL;
1347 break;
1348 case ACPI_TYPE_POWER:
1349 type = ACPI_BUS_TYPE_POWER;
1350 break;
1351 default:
1352 continue;
1353 }
1354
3fb02738
RS
1355 if (ops->acpi_op_add)
1356 status = acpi_add_single_object(&child, parent,
c4168bff 1357 chandle, type, ops);
4be44fcd 1358 else
3fb02738
RS
1359 status = acpi_bus_get_device(chandle, &child);
1360
4be44fcd
LB
1361 if (ACPI_FAILURE(status))
1362 continue;
3fb02738 1363
c4168bff 1364 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
3fb02738
RS
1365 status = acpi_start_single_object(child);
1366 if (ACPI_FAILURE(status))
1367 continue;
1368 }
1da177e4
LT
1369
1370 /*
1371 * If the device is present, enabled, and functioning then
1372 * parse its scope (depth-first). Note that we need to
1373 * represent absent devices to facilitate PnP notifications
1374 * -- but only the subtree head (not all of its children,
1375 * which will be enumerated when the parent is inserted).
1376 *
1377 * TBD: Need notifications and other detection mechanisms
4be44fcd 1378 * in place before we can fully implement this.
1da177e4
LT
1379 */
1380 if (child->status.present) {
1381 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1382 NULL, NULL);
1383 if (ACPI_SUCCESS(status)) {
1384 level++;
1385 phandle = chandle;
1386 chandle = NULL;
1387 parent = child;
1388 }
1389 }
1390 }
1391
d550d98d 1392 return 0;
1da177e4 1393}
1da177e4 1394
3fb02738 1395int
4be44fcd
LB
1396acpi_bus_add(struct acpi_device **child,
1397 struct acpi_device *parent, acpi_handle handle, int type)
3fb02738
RS
1398{
1399 int result;
1400 struct acpi_bus_ops ops;
1401
c4168bff
LS
1402 memset(&ops, 0, sizeof(ops));
1403 ops.acpi_op_add = 1;
3fb02738 1404
c4168bff
LS
1405 result = acpi_add_single_object(child, parent, handle, type, &ops);
1406 if (!result)
3fb02738 1407 result = acpi_bus_scan(*child, &ops);
c4168bff 1408
d550d98d 1409 return result;
3fb02738 1410}
4be44fcd 1411
3fb02738
RS
1412EXPORT_SYMBOL(acpi_bus_add);
1413
4be44fcd 1414int acpi_bus_start(struct acpi_device *device)
3fb02738
RS
1415{
1416 int result;
1417 struct acpi_bus_ops ops;
1418
3fb02738
RS
1419
1420 if (!device)
d550d98d 1421 return -EINVAL;
3fb02738
RS
1422
1423 result = acpi_start_single_object(device);
1424 if (!result) {
1425 memset(&ops, 0, sizeof(ops));
1426 ops.acpi_op_start = 1;
1427 result = acpi_bus_scan(device, &ops);
1428 }
d550d98d 1429 return result;
3fb02738 1430}
4be44fcd 1431
3fb02738 1432EXPORT_SYMBOL(acpi_bus_start);
1da177e4 1433
ceaba663 1434int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1da177e4 1435{
4be44fcd
LB
1436 acpi_status status;
1437 struct acpi_device *parent, *child;
1438 acpi_handle phandle, chandle;
1439 acpi_object_type type;
1440 u32 level = 1;
1441 int err = 0;
1442
1443 parent = start;
1da177e4
LT
1444 phandle = start->handle;
1445 child = chandle = NULL;
1446
1447 while ((level > 0) && parent && (!err)) {
1448 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
4be44fcd 1449 chandle, &chandle);
1da177e4
LT
1450
1451 /*
1452 * If this scope is exhausted then move our way back up.
1453 */
1454 if (ACPI_FAILURE(status)) {
1455 level--;
1456 chandle = phandle;
1457 acpi_get_parent(phandle, &phandle);
1458 child = parent;
1459 parent = parent->parent;
1460
1461 if (level == 0)
1462 err = acpi_bus_remove(child, rmdevice);
1463 else
1464 err = acpi_bus_remove(child, 1);
1465
1466 continue;
1467 }
1468
1469 status = acpi_get_type(chandle, &type);
1470 if (ACPI_FAILURE(status)) {
1471 continue;
1472 }
1473 /*
1474 * If there is a device corresponding to chandle then
1475 * parse it (depth-first).
1476 */
1477 if (acpi_bus_get_device(chandle, &child) == 0) {
1478 level++;
1479 phandle = chandle;
1480 chandle = NULL;
1481 parent = child;
1482 }
1483 continue;
1484 }
1485 return err;
1486}
ceaba663
KA
1487EXPORT_SYMBOL_GPL(acpi_bus_trim);
1488
1da177e4 1489
4be44fcd 1490static int acpi_bus_scan_fixed(struct acpi_device *root)
1da177e4 1491{
4be44fcd
LB
1492 int result = 0;
1493 struct acpi_device *device = NULL;
c4168bff 1494 struct acpi_bus_ops ops;
1da177e4
LT
1495
1496 if (!root)
d550d98d 1497 return -ENODEV;
1da177e4 1498
c4168bff
LS
1499 memset(&ops, 0, sizeof(ops));
1500 ops.acpi_op_add = 1;
1501 ops.acpi_op_start = 1;
1502
1da177e4
LT
1503 /*
1504 * Enumerate all fixed-feature devices.
1505 */
cee324b1 1506 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
3fb02738 1507 result = acpi_add_single_object(&device, acpi_root,
4be44fcd 1508 NULL,
c4168bff
LS
1509 ACPI_BUS_TYPE_POWER_BUTTON,
1510 &ops);
3fb02738 1511 }
1da177e4 1512
cee324b1 1513 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
3fb02738 1514 result = acpi_add_single_object(&device, acpi_root,
4be44fcd 1515 NULL,
c4168bff
LS
1516 ACPI_BUS_TYPE_SLEEP_BUTTON,
1517 &ops);
3fb02738 1518 }
1da177e4 1519
d550d98d 1520 return result;
1da177e4
LT
1521}
1522
c04209a7
AS
1523int __init acpi_boot_ec_enable(void);
1524
1da177e4
LT
1525static int __init acpi_scan_init(void)
1526{
1527 int result;
3fb02738 1528 struct acpi_bus_ops ops;
1da177e4 1529
1da177e4
LT
1530
1531 if (acpi_disabled)
d550d98d 1532 return 0;
1da177e4 1533
c4168bff
LS
1534 memset(&ops, 0, sizeof(ops));
1535 ops.acpi_op_add = 1;
1536 ops.acpi_op_start = 1;
1da177e4 1537
5b327265
PM
1538 result = bus_register(&acpi_bus_type);
1539 if (result) {
1540 /* We don't want to quit even if we failed to add suspend/resume */
1541 printk(KERN_ERR PREFIX "Could not register bus type\n");
1542 }
1543
1da177e4
LT
1544 /*
1545 * Create the root device in the bus's device tree
1546 */
3fb02738 1547 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
c4168bff 1548 ACPI_BUS_TYPE_SYSTEM, &ops);
1da177e4
LT
1549 if (result)
1550 goto Done;
1551
1552 /*
1553 * Enumerate devices in the ACPI namespace.
1554 */
1555 result = acpi_bus_scan_fixed(acpi_root);
c04209a7
AS
1556
1557 /* EC region might be needed at bus_scan, so enable it now */
1558 acpi_boot_ec_enable();
1559
c4168bff 1560 if (!result)
3fb02738 1561 result = acpi_bus_scan(acpi_root, &ops);
1da177e4
LT
1562
1563 if (result)
1564 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1565
4be44fcd 1566 Done:
d550d98d 1567 return result;
1da177e4
LT
1568}
1569
1570subsys_initcall(acpi_scan_init);
This page took 0.421108 seconds and 5 git commands to generate.