ACPI: convert to sysfs framework
[deliverable/linux.git] / drivers / acpi / scan.c
CommitLineData
1da177e4
LT
1/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
9b6d97b6 7#include <linux/kernel.h>
1da177e4
LT
8#include <linux/acpi.h>
9
10#include <acpi/acpi_drivers.h>
11#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
12
1da177e4 13#define _COMPONENT ACPI_BUS_COMPONENT
4be44fcd 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"
19#define ACPI_BUS_HID "ACPI_BUS"
20#define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
21#define ACPI_BUS_DEVICE_NAME "System Bus"
22
23static LIST_HEAD(acpi_device_list);
24DEFINE_SPINLOCK(acpi_device_lock);
25LIST_HEAD(acpi_wakeup_device_list);
26
4be44fcd 27static int acpi_eject_operation(acpi_handle handle, int lockable)
1da177e4
LT
28{
29 struct acpi_object_list arg_list;
30 union acpi_object arg;
31 acpi_status status = AE_OK;
32
33 /*
34 * TBD: evaluate _PS3?
35 */
36
37 if (lockable) {
38 arg_list.count = 1;
39 arg_list.pointer = &arg;
40 arg.type = ACPI_TYPE_INTEGER;
41 arg.integer.value = 0;
42 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
43 }
44
45 arg_list.count = 1;
46 arg_list.pointer = &arg;
47 arg.type = ACPI_TYPE_INTEGER;
48 arg.integer.value = 1;
49
50 /*
51 * TBD: _EJD support.
52 */
53
54 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
55 if (ACPI_FAILURE(status)) {
4be44fcd 56 return (-ENODEV);
1da177e4
LT
57 }
58
4be44fcd 59 return (0);
1da177e4
LT
60}
61
1da177e4 62static ssize_t
f883d9db
PM
63acpi_eject_store(struct device *d, struct device_attribute *attr,
64 const char *buf, size_t count)
1da177e4 65{
4be44fcd
LB
66 int result;
67 int ret = count;
68 int islockable;
69 acpi_status status;
70 acpi_handle handle;
71 acpi_object_type type = 0;
f883d9db 72 struct acpi_device *acpi_device = to_acpi_device(d);
1da177e4
LT
73
74 if ((!count) || (buf[0] != '1')) {
75 return -EINVAL;
76 }
1da177e4 77#ifndef FORCE_EJECT
f883d9db 78 if (acpi_device->driver == NULL) {
1da177e4
LT
79 ret = -ENODEV;
80 goto err;
81 }
82#endif
f883d9db
PM
83 status = acpi_get_type(acpi_device->handle, &type);
84 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
1da177e4
LT
85 ret = -ENODEV;
86 goto err;
87 }
88
f883d9db
PM
89 islockable = acpi_device->flags.lockable;
90 handle = acpi_device->handle;
1da177e4 91
f883d9db 92 result = acpi_bus_trim(acpi_device, 1);
1da177e4
LT
93
94 if (!result)
95 result = acpi_eject_operation(handle, islockable);
96
97 if (result) {
98 ret = -EBUSY;
99 }
4be44fcd 100 err:
1da177e4
LT
101 return ret;
102}
103
f883d9db
PM
104static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
105
106static void acpi_device_setup_files(struct acpi_device *dev)
107{
108 acpi_status status;
109 acpi_handle temp;
110
111 /*
112 * If device has _EJ0, 'eject' file is created that is used to trigger
113 * hot-removal function from userland.
114 */
115 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
116 if (ACPI_SUCCESS(status))
117 device_create_file(&dev->dev, &dev_attr_eject);
118}
119
120static void acpi_device_remove_files(struct acpi_device *dev)
121{
122 acpi_status status;
123 acpi_handle temp;
124
125 /*
126 * If device has _EJ0, 'eject' file is created that is used to trigger
127 * hot-removal function from userland.
128 */
129 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
130 if (ACPI_SUCCESS(status))
131 device_remove_file(&dev->dev, &dev_attr_eject);
132}
1da177e4 133/* --------------------------------------------------------------------------
9e89dde2 134 ACPI Bus operations
1da177e4 135 -------------------------------------------------------------------------- */
1890a97a
PM
136static void acpi_device_release(struct device *dev)
137{
138 struct acpi_device *acpi_dev = to_acpi_device(dev);
139
140 kfree(acpi_dev->pnp.cid_list);
141 kfree(acpi_dev);
142}
143
5d9464a4 144static int acpi_device_suspend(struct device *dev, pm_message_t state)
1da177e4 145{
5d9464a4
PM
146 struct acpi_device *acpi_dev = to_acpi_device(dev);
147 struct acpi_driver *acpi_drv = acpi_dev->driver;
9e89dde2 148
5d9464a4
PM
149 if (acpi_drv && acpi_drv->ops.suspend)
150 return acpi_drv->ops.suspend(acpi_dev, state);
1da177e4
LT
151 return 0;
152}
153
5d9464a4 154static int acpi_device_resume(struct device *dev)
9e89dde2 155{
5d9464a4
PM
156 struct acpi_device *acpi_dev = to_acpi_device(dev);
157 struct acpi_driver *acpi_drv = acpi_dev->driver;
1da177e4 158
5d9464a4
PM
159 if (acpi_drv && acpi_drv->ops.resume)
160 return acpi_drv->ops.resume(acpi_dev);
9e89dde2
ZR
161 return 0;
162}
163
5d9464a4 164static int acpi_bus_match(struct device *dev, struct device_driver *drv)
9e89dde2 165{
5d9464a4
PM
166 struct acpi_device *acpi_dev = to_acpi_device(dev);
167 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
9e89dde2 168
5d9464a4
PM
169 if (acpi_drv->ops.match)
170 return !acpi_drv->ops.match(acpi_dev, acpi_drv);
171 return !acpi_match_ids(acpi_dev, acpi_drv->ids);
172}
173
174static int acpi_device_uevent(struct device *dev, char **envp, int num_envp,
175 char *buffer, int buffer_size)
176{
177 struct acpi_device *acpi_dev = to_acpi_device(dev);
178 int i = 0, length = 0, ret = 0;
179
180 if (acpi_dev->flags.hardware_id)
181 ret = add_uevent_var(envp, num_envp, &i,
182 buffer, buffer_size, &length,
183 "HWID=%s", acpi_dev->pnp.hardware_id);
184 if (ret)
185 return -ENOMEM;
186 if (acpi_dev->flags.compatible_ids) {
187 int j;
188 struct acpi_compatible_id_list *cid_list;
189
190 cid_list = acpi_dev->pnp.cid_list;
191
192 for (j = 0; j < cid_list->count; j++) {
193 ret = add_uevent_var(envp, num_envp, &i, buffer,
194 buffer_size, &length, "COMPTID=%s",
195 cid_list->id[j].value);
196 if (ret)
197 return -ENOMEM;
9e89dde2
ZR
198 }
199 }
5d9464a4
PM
200
201 envp[i] = NULL;
9e89dde2
ZR
202 return 0;
203}
204
5d9464a4
PM
205static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
206static int acpi_start_single_object(struct acpi_device *);
207static int acpi_device_probe(struct device * dev)
9e89dde2 208{
5d9464a4
PM
209 struct acpi_device *acpi_dev = to_acpi_device(dev);
210 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
211 int ret;
212
213 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
214 if (!ret) {
215 acpi_start_single_object(acpi_dev);
216 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
217 "Found driver [%s] for device [%s]\n",
218 acpi_drv->name, acpi_dev->pnp.bus_id));
219 get_device(dev);
220 }
221 return ret;
222}
9e89dde2 223
5d9464a4
PM
224static int acpi_device_remove(struct device * dev)
225{
226 struct acpi_device *acpi_dev = to_acpi_device(dev);
227 struct acpi_driver *acpi_drv = acpi_dev->driver;
228
229 if (acpi_drv) {
230 if (acpi_drv->ops.stop)
231 acpi_drv->ops.stop(acpi_dev, ACPI_BUS_REMOVAL_NORMAL);
232 if (acpi_drv->ops.remove)
233 acpi_drv->ops.remove(acpi_dev, ACPI_BUS_REMOVAL_NORMAL);
234 }
235 acpi_dev->driver = NULL;
236 acpi_driver_data(dev) = NULL;
237
238 put_device(dev);
9e89dde2
ZR
239 return 0;
240}
1da177e4 241
5d9464a4 242static void acpi_device_shutdown(struct device *dev)
1da177e4 243{
5d9464a4
PM
244 struct acpi_device *acpi_dev = to_acpi_device(dev);
245 struct acpi_driver *acpi_drv = acpi_dev->driver;
246
247 if (acpi_drv && acpi_drv->ops.shutdown)
248 acpi_drv->ops.shutdown(acpi_dev);
249
250 return ;
1da177e4
LT
251}
252
9e89dde2
ZR
253static struct bus_type acpi_bus_type = {
254 .name = "acpi",
255 .suspend = acpi_device_suspend,
256 .resume = acpi_device_resume,
5d9464a4
PM
257 .shutdown = acpi_device_shutdown,
258 .match = acpi_bus_match,
259 .probe = acpi_device_probe,
260 .remove = acpi_device_remove,
261 .uevent = acpi_device_uevent,
9e89dde2
ZR
262};
263
264static void acpi_device_register(struct acpi_device *device,
265 struct acpi_device *parent)
266{
9e89dde2
ZR
267 /*
268 * Linkage
269 * -------
270 * Link this device to its parent and siblings.
271 */
272 INIT_LIST_HEAD(&device->children);
273 INIT_LIST_HEAD(&device->node);
274 INIT_LIST_HEAD(&device->g_list);
275 INIT_LIST_HEAD(&device->wakeup_list);
276
277 spin_lock(&acpi_device_lock);
278 if (device->parent) {
279 list_add_tail(&device->node, &device->parent->children);
280 list_add_tail(&device->g_list, &device->parent->g_list);
281 } else
282 list_add_tail(&device->g_list, &acpi_device_list);
283 if (device->wakeup.flags.valid)
284 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
285 spin_unlock(&acpi_device_lock);
286
1890a97a
PM
287 if (device->parent)
288 device->dev.parent = &parent->dev;
289 device->dev.bus = &acpi_bus_type;
290 device_initialize(&device->dev);
291 sprintf(device->dev.bus_id, "%s", device->pnp.bus_id);
292 device->dev.release = &acpi_device_release;
293 device_add(&device->dev);
f883d9db
PM
294
295 acpi_device_setup_files(device);
9e89dde2
ZR
296}
297
298static void acpi_device_unregister(struct acpi_device *device, int type)
299{
300 spin_lock(&acpi_device_lock);
301 if (device->parent) {
302 list_del(&device->node);
303 list_del(&device->g_list);
304 } else
305 list_del(&device->g_list);
306
307 list_del(&device->wakeup_list);
308
309 spin_unlock(&acpi_device_lock);
310
311 acpi_detach_data(device->handle, acpi_bus_data_handler);
1890a97a 312
f883d9db 313 acpi_device_remove_files(device);
1890a97a 314 device_unregister(&device->dev);
9e89dde2
ZR
315}
316
317/* --------------------------------------------------------------------------
318 Driver Management
319 -------------------------------------------------------------------------- */
1da177e4 320/**
d758a8fa
RD
321 * acpi_bus_driver_init - add a device to a driver
322 * @device: the device to add and initialize
323 * @driver: driver for the device
324 *
1da177e4 325 * Used to initialize a device via its device driver. Called whenever a
1890a97a 326 * driver is bound to a device. Invokes the driver's add() ops.
1da177e4
LT
327 */
328static int
4be44fcd 329acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
1da177e4 330{
4be44fcd 331 int result = 0;
1da177e4 332
1da177e4
LT
333
334 if (!device || !driver)
d550d98d 335 return -EINVAL;
1da177e4
LT
336
337 if (!driver->ops.add)
d550d98d 338 return -ENOSYS;
1da177e4
LT
339
340 result = driver->ops.add(device);
341 if (result) {
342 device->driver = NULL;
343 acpi_driver_data(device) = NULL;
d550d98d 344 return result;
1da177e4
LT
345 }
346
347 device->driver = driver;
348
349 /*
350 * TBD - Configuration Management: Assign resources to device based
351 * upon possible configuration and currently allocated resources.
352 */
353
4be44fcd
LB
354 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
355 "Driver successfully bound to device\n"));
d550d98d 356 return 0;
3fb02738
RS
357}
358
8713cbef 359static int acpi_start_single_object(struct acpi_device *device)
3fb02738
RS
360{
361 int result = 0;
362 struct acpi_driver *driver;
363
3fb02738
RS
364
365 if (!(driver = device->driver))
d550d98d 366 return 0;
3fb02738 367
1da177e4
LT
368 if (driver->ops.start) {
369 result = driver->ops.start(device);
370 if (result && driver->ops.remove)
371 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
1da177e4
LT
372 }
373
d550d98d 374 return result;
1da177e4
LT
375}
376
9e89dde2
ZR
377/**
378 * acpi_bus_register_driver - register a driver with the ACPI bus
379 * @driver: driver being registered
380 *
381 * Registers a driver with the ACPI bus. Searches the namespace for all
382 * devices that match the driver's criteria and binds. Returns zero for
383 * success or a negative error status for failure.
384 */
385int acpi_bus_register_driver(struct acpi_driver *driver)
386{
1890a97a 387 int ret;
9e89dde2
ZR
388
389 if (acpi_disabled)
390 return -ENODEV;
1890a97a
PM
391 driver->drv.name = driver->name;
392 driver->drv.bus = &acpi_bus_type;
393 driver->drv.owner = driver->owner;
9e89dde2 394
1890a97a
PM
395 ret = driver_register(&driver->drv);
396 return ret;
9e89dde2
ZR
397}
398
399EXPORT_SYMBOL(acpi_bus_register_driver);
400
401/**
402 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
403 * @driver: driver to unregister
404 *
405 * Unregisters a driver with the ACPI bus. Searches the namespace for all
406 * devices that match the driver's criteria and unbinds.
407 */
408void acpi_bus_unregister_driver(struct acpi_driver *driver)
409{
1890a97a 410 driver_unregister(&driver->drv);
9e89dde2
ZR
411}
412
413EXPORT_SYMBOL(acpi_bus_unregister_driver);
414
9e89dde2
ZR
415/* --------------------------------------------------------------------------
416 Device Enumeration
417 -------------------------------------------------------------------------- */
418acpi_status
419acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
420{
421 acpi_status status;
422 acpi_handle tmp;
423 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
424 union acpi_object *obj;
425
426 status = acpi_get_handle(handle, "_EJD", &tmp);
427 if (ACPI_FAILURE(status))
428 return status;
429
430 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
431 if (ACPI_SUCCESS(status)) {
432 obj = buffer.pointer;
433 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
434 kfree(buffer.pointer);
435 }
436 return status;
437}
438EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
439
440void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
441{
442
443 /* TBD */
444
445 return;
446}
447
448int acpi_match_ids(struct acpi_device *device, char *ids)
449{
450 if (device->flags.hardware_id)
451 if (strstr(ids, device->pnp.hardware_id))
452 return 0;
453
454 if (device->flags.compatible_ids) {
455 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
456 int i;
457
458 /* compare multiple _CID entries against driver ids */
459 for (i = 0; i < cid_list->count; i++) {
460 if (strstr(ids, cid_list->id[i].value))
461 return 0;
462 }
463 }
464 return -ENOENT;
465}
466
467static int acpi_bus_get_perf_flags(struct acpi_device *device)
468{
469 device->performance.state = ACPI_STATE_UNKNOWN;
470 return 0;
471}
472
473static acpi_status
474acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
475 union acpi_object *package)
476{
477 int i = 0;
478 union acpi_object *element = NULL;
479
480 if (!device || !package || (package->package.count < 2))
481 return AE_BAD_PARAMETER;
482
483 element = &(package->package.elements[0]);
484 if (!element)
485 return AE_BAD_PARAMETER;
486 if (element->type == ACPI_TYPE_PACKAGE) {
487 if ((element->package.count < 2) ||
488 (element->package.elements[0].type !=
489 ACPI_TYPE_LOCAL_REFERENCE)
490 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
491 return AE_BAD_DATA;
492 device->wakeup.gpe_device =
493 element->package.elements[0].reference.handle;
494 device->wakeup.gpe_number =
495 (u32) element->package.elements[1].integer.value;
496 } else if (element->type == ACPI_TYPE_INTEGER) {
497 device->wakeup.gpe_number = element->integer.value;
498 } else
499 return AE_BAD_DATA;
500
501 element = &(package->package.elements[1]);
502 if (element->type != ACPI_TYPE_INTEGER) {
503 return AE_BAD_DATA;
504 }
505 device->wakeup.sleep_state = element->integer.value;
506
507 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
508 return AE_NO_MEMORY;
509 }
510 device->wakeup.resources.count = package->package.count - 2;
511 for (i = 0; i < device->wakeup.resources.count; i++) {
512 element = &(package->package.elements[i + 2]);
513 if (element->type != ACPI_TYPE_ANY) {
514 return AE_BAD_DATA;
515 }
516
517 device->wakeup.resources.handles[i] = element->reference.handle;
518 }
519
520 return AE_OK;
1da177e4
LT
521}
522
9e89dde2 523static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1da177e4 524{
9e89dde2
ZR
525 acpi_status status = 0;
526 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
527 union acpi_object *package = NULL;
1da177e4 528
1da177e4 529
9e89dde2
ZR
530 /* _PRW */
531 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
532 if (ACPI_FAILURE(status)) {
533 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
534 goto end;
535 }
1da177e4 536
9e89dde2
ZR
537 package = (union acpi_object *)buffer.pointer;
538 status = acpi_bus_extract_wakeup_device_power_package(device, package);
539 if (ACPI_FAILURE(status)) {
540 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
541 goto end;
542 }
1da177e4 543
9e89dde2 544 kfree(buffer.pointer);
1da177e4 545
9e89dde2
ZR
546 device->wakeup.flags.valid = 1;
547 /* Power button, Lid switch always enable wakeup */
548 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
549 device->wakeup.flags.run_wake = 1;
1a365616 550
9e89dde2
ZR
551 end:
552 if (ACPI_FAILURE(status))
553 device->flags.wake_capable = 0;
554 return 0;
1da177e4 555}
4be44fcd 556
9e89dde2 557static int acpi_bus_get_power_flags(struct acpi_device *device)
1da177e4 558{
9e89dde2
ZR
559 acpi_status status = 0;
560 acpi_handle handle = NULL;
561 u32 i = 0;
1da177e4 562
1da177e4 563
9e89dde2
ZR
564 /*
565 * Power Management Flags
566 */
567 status = acpi_get_handle(device->handle, "_PSC", &handle);
568 if (ACPI_SUCCESS(status))
569 device->power.flags.explicit_get = 1;
570 status = acpi_get_handle(device->handle, "_IRC", &handle);
571 if (ACPI_SUCCESS(status))
572 device->power.flags.inrush_current = 1;
1da177e4 573
9e89dde2
ZR
574 /*
575 * Enumerate supported power management states
576 */
577 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
578 struct acpi_device_power_state *ps = &device->power.states[i];
579 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
580
581 /* Evaluate "_PRx" to se if power resources are referenced */
582 acpi_evaluate_reference(device->handle, object_name, NULL,
583 &ps->resources);
584 if (ps->resources.count) {
585 device->power.flags.power_resources = 1;
586 ps->flags.valid = 1;
1da177e4 587 }
1da177e4 588
9e89dde2
ZR
589 /* Evaluate "_PSx" to see if we can do explicit sets */
590 object_name[2] = 'S';
591 status = acpi_get_handle(device->handle, object_name, &handle);
592 if (ACPI_SUCCESS(status)) {
593 ps->flags.explicit_set = 1;
594 ps->flags.valid = 1;
595 }
1da177e4 596
9e89dde2
ZR
597 /* State is valid if we have some power control */
598 if (ps->resources.count || ps->flags.explicit_set)
599 ps->flags.valid = 1;
1da177e4 600
9e89dde2
ZR
601 ps->power = -1; /* Unknown - driver assigned */
602 ps->latency = -1; /* Unknown - driver assigned */
603 }
c8f7a62c 604
9e89dde2
ZR
605 /* Set defaults for D0 and D3 states (always valid) */
606 device->power.states[ACPI_STATE_D0].flags.valid = 1;
607 device->power.states[ACPI_STATE_D0].power = 100;
608 device->power.states[ACPI_STATE_D3].flags.valid = 1;
609 device->power.states[ACPI_STATE_D3].power = 0;
c8f7a62c 610
9e89dde2
ZR
611 /* TBD: System wake support and resource requirements. */
612
613 device->power.state = ACPI_STATE_UNKNOWN;
c8f7a62c 614
9e89dde2
ZR
615 return 0;
616}
c8f7a62c 617
4be44fcd 618static int acpi_bus_get_flags(struct acpi_device *device)
1da177e4 619{
4be44fcd
LB
620 acpi_status status = AE_OK;
621 acpi_handle temp = NULL;
1da177e4 622
1da177e4
LT
623
624 /* Presence of _STA indicates 'dynamic_status' */
625 status = acpi_get_handle(device->handle, "_STA", &temp);
626 if (ACPI_SUCCESS(status))
627 device->flags.dynamic_status = 1;
628
629 /* Presence of _CID indicates 'compatible_ids' */
630 status = acpi_get_handle(device->handle, "_CID", &temp);
631 if (ACPI_SUCCESS(status))
632 device->flags.compatible_ids = 1;
633
634 /* Presence of _RMV indicates 'removable' */
635 status = acpi_get_handle(device->handle, "_RMV", &temp);
636 if (ACPI_SUCCESS(status))
637 device->flags.removable = 1;
638
639 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
640 status = acpi_get_handle(device->handle, "_EJD", &temp);
641 if (ACPI_SUCCESS(status))
642 device->flags.ejectable = 1;
643 else {
644 status = acpi_get_handle(device->handle, "_EJ0", &temp);
645 if (ACPI_SUCCESS(status))
646 device->flags.ejectable = 1;
647 }
648
649 /* Presence of _LCK indicates 'lockable' */
650 status = acpi_get_handle(device->handle, "_LCK", &temp);
651 if (ACPI_SUCCESS(status))
652 device->flags.lockable = 1;
653
654 /* Presence of _PS0|_PR0 indicates 'power manageable' */
655 status = acpi_get_handle(device->handle, "_PS0", &temp);
656 if (ACPI_FAILURE(status))
657 status = acpi_get_handle(device->handle, "_PR0", &temp);
658 if (ACPI_SUCCESS(status))
659 device->flags.power_manageable = 1;
660
661 /* Presence of _PRW indicates wake capable */
662 status = acpi_get_handle(device->handle, "_PRW", &temp);
663 if (ACPI_SUCCESS(status))
664 device->flags.wake_capable = 1;
665
666 /* TBD: Peformance management */
667
d550d98d 668 return 0;
1da177e4
LT
669}
670
4be44fcd
LB
671static void acpi_device_get_busid(struct acpi_device *device,
672 acpi_handle handle, int type)
1da177e4 673{
4be44fcd
LB
674 char bus_id[5] = { '?', 0 };
675 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
676 int i = 0;
1da177e4
LT
677
678 /*
679 * Bus ID
680 * ------
681 * The device's Bus ID is simply the object name.
682 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
683 */
684 switch (type) {
685 case ACPI_BUS_TYPE_SYSTEM:
686 strcpy(device->pnp.bus_id, "ACPI");
687 break;
688 case ACPI_BUS_TYPE_POWER_BUTTON:
689 strcpy(device->pnp.bus_id, "PWRF");
690 break;
691 case ACPI_BUS_TYPE_SLEEP_BUTTON:
692 strcpy(device->pnp.bus_id, "SLPF");
693 break;
694 default:
695 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
696 /* Clean up trailing underscores (if any) */
697 for (i = 3; i > 1; i--) {
698 if (bus_id[i] == '_')
699 bus_id[i] = '\0';
700 else
701 break;
702 }
703 strcpy(device->pnp.bus_id, bus_id);
704 break;
705 }
706}
707
4be44fcd
LB
708static void acpi_device_set_id(struct acpi_device *device,
709 struct acpi_device *parent, acpi_handle handle,
710 int type)
1da177e4 711{
4be44fcd
LB
712 struct acpi_device_info *info;
713 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
714 char *hid = NULL;
715 char *uid = NULL;
1da177e4 716 struct acpi_compatible_id_list *cid_list = NULL;
4be44fcd 717 acpi_status status;
1da177e4
LT
718
719 switch (type) {
720 case ACPI_BUS_TYPE_DEVICE:
721 status = acpi_get_object_info(handle, &buffer);
722 if (ACPI_FAILURE(status)) {
4be44fcd 723 printk("%s: Error reading device info\n", __FUNCTION__);
1da177e4
LT
724 return;
725 }
726
727 info = buffer.pointer;
728 if (info->valid & ACPI_VALID_HID)
729 hid = info->hardware_id.value;
730 if (info->valid & ACPI_VALID_UID)
731 uid = info->unique_id.value;
732 if (info->valid & ACPI_VALID_CID)
733 cid_list = &info->compatibility_id;
734 if (info->valid & ACPI_VALID_ADR) {
735 device->pnp.bus_address = info->address;
736 device->flags.bus_address = 1;
737 }
738 break;
739 case ACPI_BUS_TYPE_POWER:
740 hid = ACPI_POWER_HID;
741 break;
742 case ACPI_BUS_TYPE_PROCESSOR:
743 hid = ACPI_PROCESSOR_HID;
744 break;
745 case ACPI_BUS_TYPE_SYSTEM:
746 hid = ACPI_SYSTEM_HID;
747 break;
748 case ACPI_BUS_TYPE_THERMAL:
749 hid = ACPI_THERMAL_HID;
750 break;
751 case ACPI_BUS_TYPE_POWER_BUTTON:
752 hid = ACPI_BUTTON_HID_POWERF;
753 break;
754 case ACPI_BUS_TYPE_SLEEP_BUTTON:
755 hid = ACPI_BUTTON_HID_SLEEPF;
756 break;
757 }
758
759 /*
760 * \_SB
761 * ----
762 * Fix for the system root bus device -- the only root-level device.
763 */
c51a4de8 764 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
1da177e4
LT
765 hid = ACPI_BUS_HID;
766 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
767 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
768 }
769
770 if (hid) {
771 strcpy(device->pnp.hardware_id, hid);
772 device->flags.hardware_id = 1;
773 }
774 if (uid) {
775 strcpy(device->pnp.unique_id, uid);
776 device->flags.unique_id = 1;
777 }
778 if (cid_list) {
779 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
780 if (device->pnp.cid_list)
781 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
782 else
783 printk(KERN_ERR "Memory allocation error\n");
784 }
785
02438d87 786 kfree(buffer.pointer);
1da177e4
LT
787}
788
4be44fcd 789static int acpi_device_set_context(struct acpi_device *device, int type)
1da177e4
LT
790{
791 acpi_status status = AE_OK;
792 int result = 0;
793 /*
794 * Context
795 * -------
796 * Attach this 'struct acpi_device' to the ACPI object. This makes
797 * resolutions from handle->device very efficient. Note that we need
798 * to be careful with fixed-feature devices as they all attach to the
799 * root object.
800 */
4be44fcd 801 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
1da177e4
LT
802 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
803 status = acpi_attach_data(device->handle,
4be44fcd 804 acpi_bus_data_handler, device);
1da177e4
LT
805
806 if (ACPI_FAILURE(status)) {
807 printk("Error attaching device data\n");
808 result = -ENODEV;
809 }
810 }
811 return result;
812}
813
4be44fcd
LB
814static void acpi_device_get_debug_info(struct acpi_device *device,
815 acpi_handle handle, int type)
1da177e4
LT
816{
817#ifdef CONFIG_ACPI_DEBUG_OUTPUT
4be44fcd
LB
818 char *type_string = NULL;
819 char name[80] = { '?', '\0' };
820 struct acpi_buffer buffer = { sizeof(name), name };
1da177e4
LT
821
822 switch (type) {
823 case ACPI_BUS_TYPE_DEVICE:
824 type_string = "Device";
825 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
826 break;
827 case ACPI_BUS_TYPE_POWER:
828 type_string = "Power Resource";
829 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
830 break;
831 case ACPI_BUS_TYPE_PROCESSOR:
832 type_string = "Processor";
833 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
834 break;
835 case ACPI_BUS_TYPE_SYSTEM:
836 type_string = "System";
837 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
838 break;
839 case ACPI_BUS_TYPE_THERMAL:
840 type_string = "Thermal Zone";
841 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
842 break;
843 case ACPI_BUS_TYPE_POWER_BUTTON:
844 type_string = "Power Button";
845 sprintf(name, "PWRB");
846 break;
847 case ACPI_BUS_TYPE_SLEEP_BUTTON:
848 type_string = "Sleep Button";
849 sprintf(name, "SLPB");
850 break;
851 }
852
853 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
4be44fcd 854#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
1da177e4
LT
855}
856
4be44fcd 857static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1da177e4 858{
1da177e4 859 if (!dev)
d550d98d 860 return -EINVAL;
1da177e4 861
1890a97a 862 device_release_driver(&dev->dev);
1da177e4
LT
863
864 if (!rmdevice)
d550d98d 865 return 0;
1da177e4
LT
866
867 if (dev->flags.bus_address) {
868 if ((dev->parent) && (dev->parent->ops.unbind))
869 dev->parent->ops.unbind(dev);
870 }
4be44fcd 871
1da177e4
LT
872 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
873
d550d98d 874 return 0;
1da177e4
LT
875}
876
3fb02738 877static int
4be44fcd
LB
878acpi_add_single_object(struct acpi_device **child,
879 struct acpi_device *parent, acpi_handle handle, int type)
1da177e4 880{
4be44fcd
LB
881 int result = 0;
882 struct acpi_device *device = NULL;
1da177e4 883
1da177e4
LT
884
885 if (!child)
d550d98d 886 return -EINVAL;
1da177e4
LT
887
888 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
889 if (!device) {
6468463a 890 printk(KERN_ERR PREFIX "Memory allocation error\n");
d550d98d 891 return -ENOMEM;
1da177e4
LT
892 }
893 memset(device, 0, sizeof(struct acpi_device));
894
895 device->handle = handle;
896 device->parent = parent;
897
4be44fcd 898 acpi_device_get_busid(device, handle, type);
1da177e4
LT
899
900 /*
901 * Flags
902 * -----
903 * Get prior to calling acpi_bus_get_status() so we know whether
904 * or not _STA is present. Note that we only look for object
905 * handles -- cannot evaluate objects until we know the device is
906 * present and properly initialized.
907 */
908 result = acpi_bus_get_flags(device);
909 if (result)
910 goto end;
911
912 /*
913 * Status
914 * ------
6940faba
KT
915 * See if the device is present. We always assume that non-Device
916 * and non-Processor objects (e.g. thermal zones, power resources,
917 * etc.) are present, functioning, etc. (at least when parent object
918 * is present). Note that _STA has a different meaning for some
919 * objects (e.g. power resources) so we need to be careful how we use
920 * it.
1da177e4
LT
921 */
922 switch (type) {
6940faba 923 case ACPI_BUS_TYPE_PROCESSOR:
1da177e4
LT
924 case ACPI_BUS_TYPE_DEVICE:
925 result = acpi_bus_get_status(device);
926 if (ACPI_FAILURE(result) || !device->status.present) {
927 result = -ENOENT;
928 goto end;
929 }
930 break;
931 default:
932 STRUCT_TO_INT(device->status) = 0x0F;
933 break;
934 }
935
936 /*
937 * Initialize Device
938 * -----------------
939 * TBD: Synch with Core's enumeration/initialization process.
940 */
941
942 /*
943 * Hardware ID, Unique ID, & Bus Address
944 * -------------------------------------
945 */
4be44fcd 946 acpi_device_set_id(device, parent, handle, type);
1da177e4
LT
947
948 /*
949 * Power Management
950 * ----------------
951 */
952 if (device->flags.power_manageable) {
953 result = acpi_bus_get_power_flags(device);
954 if (result)
955 goto end;
956 }
957
4be44fcd 958 /*
1da177e4
LT
959 * Wakeup device management
960 *-----------------------
961 */
962 if (device->flags.wake_capable) {
963 result = acpi_bus_get_wakeup_device_flags(device);
964 if (result)
965 goto end;
966 }
967
968 /*
969 * Performance Management
970 * ----------------------
971 */
972 if (device->flags.performance_manageable) {
973 result = acpi_bus_get_perf_flags(device);
974 if (result)
975 goto end;
976 }
977
4be44fcd 978 if ((result = acpi_device_set_context(device, type)))
1da177e4
LT
979 goto end;
980
4be44fcd 981 acpi_device_get_debug_info(device, handle, type);
1da177e4 982
4be44fcd 983 acpi_device_register(device, parent);
1da177e4
LT
984
985 /*
986 * Bind _ADR-Based Devices
987 * -----------------------
988 * If there's a a bus address (_ADR) then we utilize the parent's
989 * 'bind' function (if exists) to bind the ACPI- and natively-
990 * enumerated device representations.
991 */
992 if (device->flags.bus_address) {
993 if (device->parent && device->parent->ops.bind)
994 device->parent->ops.bind(device);
995 }
996
4be44fcd 997 end:
1da177e4
LT
998 if (!result)
999 *child = device;
1000 else {
6044ec88 1001 kfree(device->pnp.cid_list);
1da177e4
LT
1002 kfree(device);
1003 }
1004
d550d98d 1005 return result;
1da177e4 1006}
1da177e4 1007
4be44fcd 1008static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1da177e4 1009{
4be44fcd
LB
1010 acpi_status status = AE_OK;
1011 struct acpi_device *parent = NULL;
1012 struct acpi_device *child = NULL;
1013 acpi_handle phandle = NULL;
1014 acpi_handle chandle = NULL;
1015 acpi_object_type type = 0;
1016 u32 level = 1;
1da177e4 1017
1da177e4
LT
1018
1019 if (!start)
d550d98d 1020 return -EINVAL;
1da177e4
LT
1021
1022 parent = start;
1023 phandle = start->handle;
4be44fcd 1024
1da177e4
LT
1025 /*
1026 * Parse through the ACPI namespace, identify all 'devices', and
1027 * create a new 'struct acpi_device' for each.
1028 */
1029 while ((level > 0) && parent) {
1030
1031 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
4be44fcd 1032 chandle, &chandle);
1da177e4
LT
1033
1034 /*
1035 * If this scope is exhausted then move our way back up.
1036 */
1037 if (ACPI_FAILURE(status)) {
1038 level--;
1039 chandle = phandle;
1040 acpi_get_parent(phandle, &phandle);
1041 if (parent->parent)
1042 parent = parent->parent;
1043 continue;
1044 }
1045
1046 status = acpi_get_type(chandle, &type);
1047 if (ACPI_FAILURE(status))
1048 continue;
1049
1050 /*
1051 * If this is a scope object then parse it (depth-first).
1052 */
1053 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1054 level++;
1055 phandle = chandle;
1056 chandle = NULL;
1057 continue;
1058 }
1059
1060 /*
1061 * We're only interested in objects that we consider 'devices'.
1062 */
1063 switch (type) {
1064 case ACPI_TYPE_DEVICE:
1065 type = ACPI_BUS_TYPE_DEVICE;
1066 break;
1067 case ACPI_TYPE_PROCESSOR:
1068 type = ACPI_BUS_TYPE_PROCESSOR;
1069 break;
1070 case ACPI_TYPE_THERMAL:
1071 type = ACPI_BUS_TYPE_THERMAL;
1072 break;
1073 case ACPI_TYPE_POWER:
1074 type = ACPI_BUS_TYPE_POWER;
1075 break;
1076 default:
1077 continue;
1078 }
1079
3fb02738
RS
1080 if (ops->acpi_op_add)
1081 status = acpi_add_single_object(&child, parent,
4be44fcd
LB
1082 chandle, type);
1083 else
3fb02738
RS
1084 status = acpi_bus_get_device(chandle, &child);
1085
4be44fcd
LB
1086 if (ACPI_FAILURE(status))
1087 continue;
3fb02738
RS
1088
1089 if (ops->acpi_op_start) {
1090 status = acpi_start_single_object(child);
1091 if (ACPI_FAILURE(status))
1092 continue;
1093 }
1da177e4
LT
1094
1095 /*
1096 * If the device is present, enabled, and functioning then
1097 * parse its scope (depth-first). Note that we need to
1098 * represent absent devices to facilitate PnP notifications
1099 * -- but only the subtree head (not all of its children,
1100 * which will be enumerated when the parent is inserted).
1101 *
1102 * TBD: Need notifications and other detection mechanisms
4be44fcd 1103 * in place before we can fully implement this.
1da177e4
LT
1104 */
1105 if (child->status.present) {
1106 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1107 NULL, NULL);
1108 if (ACPI_SUCCESS(status)) {
1109 level++;
1110 phandle = chandle;
1111 chandle = NULL;
1112 parent = child;
1113 }
1114 }
1115 }
1116
d550d98d 1117 return 0;
1da177e4 1118}
1da177e4 1119
3fb02738 1120int
4be44fcd
LB
1121acpi_bus_add(struct acpi_device **child,
1122 struct acpi_device *parent, acpi_handle handle, int type)
3fb02738
RS
1123{
1124 int result;
1125 struct acpi_bus_ops ops;
1126
3fb02738
RS
1127
1128 result = acpi_add_single_object(child, parent, handle, type);
1129 if (!result) {
1130 memset(&ops, 0, sizeof(ops));
1131 ops.acpi_op_add = 1;
1132 result = acpi_bus_scan(*child, &ops);
1133 }
d550d98d 1134 return result;
3fb02738 1135}
4be44fcd 1136
3fb02738
RS
1137EXPORT_SYMBOL(acpi_bus_add);
1138
4be44fcd 1139int acpi_bus_start(struct acpi_device *device)
3fb02738
RS
1140{
1141 int result;
1142 struct acpi_bus_ops ops;
1143
3fb02738
RS
1144
1145 if (!device)
d550d98d 1146 return -EINVAL;
3fb02738
RS
1147
1148 result = acpi_start_single_object(device);
1149 if (!result) {
1150 memset(&ops, 0, sizeof(ops));
1151 ops.acpi_op_start = 1;
1152 result = acpi_bus_scan(device, &ops);
1153 }
d550d98d 1154 return result;
3fb02738 1155}
4be44fcd 1156
3fb02738 1157EXPORT_SYMBOL(acpi_bus_start);
1da177e4 1158
ceaba663 1159int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1da177e4 1160{
4be44fcd
LB
1161 acpi_status status;
1162 struct acpi_device *parent, *child;
1163 acpi_handle phandle, chandle;
1164 acpi_object_type type;
1165 u32 level = 1;
1166 int err = 0;
1167
1168 parent = start;
1da177e4
LT
1169 phandle = start->handle;
1170 child = chandle = NULL;
1171
1172 while ((level > 0) && parent && (!err)) {
1173 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
4be44fcd 1174 chandle, &chandle);
1da177e4
LT
1175
1176 /*
1177 * If this scope is exhausted then move our way back up.
1178 */
1179 if (ACPI_FAILURE(status)) {
1180 level--;
1181 chandle = phandle;
1182 acpi_get_parent(phandle, &phandle);
1183 child = parent;
1184 parent = parent->parent;
1185
1186 if (level == 0)
1187 err = acpi_bus_remove(child, rmdevice);
1188 else
1189 err = acpi_bus_remove(child, 1);
1190
1191 continue;
1192 }
1193
1194 status = acpi_get_type(chandle, &type);
1195 if (ACPI_FAILURE(status)) {
1196 continue;
1197 }
1198 /*
1199 * If there is a device corresponding to chandle then
1200 * parse it (depth-first).
1201 */
1202 if (acpi_bus_get_device(chandle, &child) == 0) {
1203 level++;
1204 phandle = chandle;
1205 chandle = NULL;
1206 parent = child;
1207 }
1208 continue;
1209 }
1210 return err;
1211}
ceaba663
KA
1212EXPORT_SYMBOL_GPL(acpi_bus_trim);
1213
1da177e4 1214
4be44fcd 1215static int acpi_bus_scan_fixed(struct acpi_device *root)
1da177e4 1216{
4be44fcd
LB
1217 int result = 0;
1218 struct acpi_device *device = NULL;
1da177e4 1219
1da177e4
LT
1220
1221 if (!root)
d550d98d 1222 return -ENODEV;
1da177e4
LT
1223
1224 /*
1225 * Enumerate all fixed-feature devices.
1226 */
3fb02738
RS
1227 if (acpi_fadt.pwr_button == 0) {
1228 result = acpi_add_single_object(&device, acpi_root,
4be44fcd
LB
1229 NULL,
1230 ACPI_BUS_TYPE_POWER_BUTTON);
3fb02738
RS
1231 if (!result)
1232 result = acpi_start_single_object(device);
1233 }
1da177e4 1234
3fb02738
RS
1235 if (acpi_fadt.sleep_button == 0) {
1236 result = acpi_add_single_object(&device, acpi_root,
4be44fcd
LB
1237 NULL,
1238 ACPI_BUS_TYPE_SLEEP_BUTTON);
3fb02738
RS
1239 if (!result)
1240 result = acpi_start_single_object(device);
1241 }
1da177e4 1242
d550d98d 1243 return result;
1da177e4
LT
1244}
1245
1da177e4
LT
1246static int __init acpi_scan_init(void)
1247{
1248 int result;
3fb02738 1249 struct acpi_bus_ops ops;
1da177e4 1250
1da177e4
LT
1251
1252 if (acpi_disabled)
d550d98d 1253 return 0;
1da177e4 1254
5b327265
PM
1255 result = bus_register(&acpi_bus_type);
1256 if (result) {
1257 /* We don't want to quit even if we failed to add suspend/resume */
1258 printk(KERN_ERR PREFIX "Could not register bus type\n");
1259 }
1260
1da177e4
LT
1261 /*
1262 * Create the root device in the bus's device tree
1263 */
3fb02738 1264 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
4be44fcd 1265 ACPI_BUS_TYPE_SYSTEM);
1da177e4
LT
1266 if (result)
1267 goto Done;
1268
3fb02738 1269 result = acpi_start_single_object(acpi_root);
5b327265
PM
1270 if (result)
1271 goto Done;
1272
1da177e4
LT
1273 /*
1274 * Enumerate devices in the ACPI namespace.
1275 */
1276 result = acpi_bus_scan_fixed(acpi_root);
3fb02738
RS
1277 if (!result) {
1278 memset(&ops, 0, sizeof(ops));
1279 ops.acpi_op_add = 1;
1280 ops.acpi_op_start = 1;
1281 result = acpi_bus_scan(acpi_root, &ops);
1282 }
1da177e4
LT
1283
1284 if (result)
1285 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1286
4be44fcd 1287 Done:
d550d98d 1288 return result;
1da177e4
LT
1289}
1290
1291subsys_initcall(acpi_scan_init);
This page took 0.333065 seconds and 5 git commands to generate.