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