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