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