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