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