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