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