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