Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6.git] / drivers / acpi / scan.c
1 /*
2  * scan.c - support for transforming the ACPI namespace into individual objects
3  */
4
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/acpi.h>
9 #include <linux/signal.h>
10 #include <linux/kthread.h>
11 #include <linux/dmi.h>
12
13 #include <acpi/acpi_drivers.h>
14
15 #include "internal.h"
16
17 #define _COMPONENT              ACPI_BUS_COMPONENT
18 ACPI_MODULE_NAME("scan");
19 #define STRUCT_TO_INT(s)        (*((int*)&s))
20 extern struct acpi_device *acpi_root;
21
22 #define ACPI_BUS_CLASS                  "system_bus"
23 #define ACPI_BUS_HID                    "LNXSYBUS"
24 #define ACPI_BUS_DEVICE_NAME            "System Bus"
25
26 #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
27
28 static LIST_HEAD(acpi_device_list);
29 static LIST_HEAD(acpi_bus_id_list);
30 DEFINE_MUTEX(acpi_device_lock);
31 LIST_HEAD(acpi_wakeup_device_list);
32
33 struct acpi_device_bus_id{
34         char bus_id[15];
35         unsigned int instance_no;
36         struct list_head node;
37 };
38
39 /*
40  * Creates hid/cid(s) string needed for modalias and uevent
41  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
42  * char *modalias: "acpi:IBM0001:ACPI0001"
43 */
44 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
45                            int size)
46 {
47         int len;
48         int count;
49         struct acpi_hardware_id *id;
50
51         len = snprintf(modalias, size, "acpi:");
52         size -= len;
53
54         list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
55                 count = snprintf(&modalias[len], size, "%s:", id->id);
56                 if (count < 0 || count >= size)
57                         return -EINVAL;
58                 len += count;
59                 size -= count;
60         }
61
62         modalias[len] = '\0';
63         return len;
64 }
65
66 static ssize_t
67 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
68         struct acpi_device *acpi_dev = to_acpi_device(dev);
69         int len;
70
71         /* Device has no HID and no CID or string is >1024 */
72         len = create_modalias(acpi_dev, buf, 1024);
73         if (len <= 0)
74                 return 0;
75         buf[len++] = '\n';
76         return len;
77 }
78 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
79
80 static void acpi_bus_hot_remove_device(void *context)
81 {
82         struct acpi_device *device;
83         acpi_handle handle = context;
84         struct acpi_object_list arg_list;
85         union acpi_object arg;
86         acpi_status status = AE_OK;
87
88         if (acpi_bus_get_device(handle, &device))
89                 return;
90
91         if (!device)
92                 return;
93
94         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
95                 "Hot-removing device %s...\n", dev_name(&device->dev)));
96
97         if (acpi_bus_trim(device, 1)) {
98                 printk(KERN_ERR PREFIX
99                                 "Removing device failed\n");
100                 return;
101         }
102
103         /* power off device */
104         status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
105         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
106                 printk(KERN_WARNING PREFIX
107                                 "Power-off device failed\n");
108
109         if (device->flags.lockable) {
110                 arg_list.count = 1;
111                 arg_list.pointer = &arg;
112                 arg.type = ACPI_TYPE_INTEGER;
113                 arg.integer.value = 0;
114                 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
115         }
116
117         arg_list.count = 1;
118         arg_list.pointer = &arg;
119         arg.type = ACPI_TYPE_INTEGER;
120         arg.integer.value = 1;
121
122         /*
123          * TBD: _EJD support.
124          */
125         status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
126         if (ACPI_FAILURE(status))
127                 printk(KERN_WARNING PREFIX
128                                 "Eject device failed\n");
129
130         return;
131 }
132
133 static ssize_t
134 acpi_eject_store(struct device *d, struct device_attribute *attr,
135                 const char *buf, size_t count)
136 {
137         int ret = count;
138         acpi_status status;
139         acpi_object_type type = 0;
140         struct acpi_device *acpi_device = to_acpi_device(d);
141
142         if ((!count) || (buf[0] != '1')) {
143                 return -EINVAL;
144         }
145 #ifndef FORCE_EJECT
146         if (acpi_device->driver == NULL) {
147                 ret = -ENODEV;
148                 goto err;
149         }
150 #endif
151         status = acpi_get_type(acpi_device->handle, &type);
152         if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
153                 ret = -ENODEV;
154                 goto err;
155         }
156
157         acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
158 err:
159         return ret;
160 }
161
162 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
163
164 static ssize_t
165 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
166         struct acpi_device *acpi_dev = to_acpi_device(dev);
167
168         return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
169 }
170 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
171
172 static ssize_t
173 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
174         struct acpi_device *acpi_dev = to_acpi_device(dev);
175         struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
176         int result;
177
178         result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
179         if (result)
180                 goto end;
181
182         result = sprintf(buf, "%s\n", (char*)path.pointer);
183         kfree(path.pointer);
184 end:
185         return result;
186 }
187 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
188
189 static int acpi_device_setup_files(struct acpi_device *dev)
190 {
191         acpi_status status;
192         acpi_handle temp;
193         int result = 0;
194
195         /*
196          * Devices gotten from FADT don't have a "path" attribute
197          */
198         if (dev->handle) {
199                 result = device_create_file(&dev->dev, &dev_attr_path);
200                 if (result)
201                         goto end;
202         }
203
204         result = device_create_file(&dev->dev, &dev_attr_hid);
205         if (result)
206                 goto end;
207
208         result = device_create_file(&dev->dev, &dev_attr_modalias);
209         if (result)
210                 goto end;
211
212         /*
213          * If device has _EJ0, 'eject' file is created that is used to trigger
214          * hot-removal function from userland.
215          */
216         status = acpi_get_handle(dev->handle, "_EJ0", &temp);
217         if (ACPI_SUCCESS(status))
218                 result = device_create_file(&dev->dev, &dev_attr_eject);
219 end:
220         return result;
221 }
222
223 static void acpi_device_remove_files(struct acpi_device *dev)
224 {
225         acpi_status status;
226         acpi_handle temp;
227
228         /*
229          * If device has _EJ0, 'eject' file is created that is used to trigger
230          * hot-removal function from userland.
231          */
232         status = acpi_get_handle(dev->handle, "_EJ0", &temp);
233         if (ACPI_SUCCESS(status))
234                 device_remove_file(&dev->dev, &dev_attr_eject);
235
236         device_remove_file(&dev->dev, &dev_attr_modalias);
237         device_remove_file(&dev->dev, &dev_attr_hid);
238         if (dev->handle)
239                 device_remove_file(&dev->dev, &dev_attr_path);
240 }
241 /* --------------------------------------------------------------------------
242                         ACPI Bus operations
243    -------------------------------------------------------------------------- */
244
245 int acpi_match_device_ids(struct acpi_device *device,
246                           const struct acpi_device_id *ids)
247 {
248         const struct acpi_device_id *id;
249         struct acpi_hardware_id *hwid;
250
251         /*
252          * If the device is not present, it is unnecessary to load device
253          * driver for it.
254          */
255         if (!device->status.present)
256                 return -ENODEV;
257
258         for (id = ids; id->id[0]; id++)
259                 list_for_each_entry(hwid, &device->pnp.ids, list)
260                         if (!strcmp((char *) id->id, hwid->id))
261                                 return 0;
262
263         return -ENOENT;
264 }
265 EXPORT_SYMBOL(acpi_match_device_ids);
266
267 static void acpi_free_ids(struct acpi_device *device)
268 {
269         struct acpi_hardware_id *id, *tmp;
270
271         list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
272                 kfree(id->id);
273                 kfree(id);
274         }
275 }
276
277 static void acpi_device_release(struct device *dev)
278 {
279         struct acpi_device *acpi_dev = to_acpi_device(dev);
280
281         acpi_free_ids(acpi_dev);
282         kfree(acpi_dev);
283 }
284
285 static int acpi_device_suspend(struct device *dev, pm_message_t state)
286 {
287         struct acpi_device *acpi_dev = to_acpi_device(dev);
288         struct acpi_driver *acpi_drv = acpi_dev->driver;
289
290         if (acpi_drv && acpi_drv->ops.suspend)
291                 return acpi_drv->ops.suspend(acpi_dev, state);
292         return 0;
293 }
294
295 static int acpi_device_resume(struct device *dev)
296 {
297         struct acpi_device *acpi_dev = to_acpi_device(dev);
298         struct acpi_driver *acpi_drv = acpi_dev->driver;
299
300         if (acpi_drv && acpi_drv->ops.resume)
301                 return acpi_drv->ops.resume(acpi_dev);
302         return 0;
303 }
304
305 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
306 {
307         struct acpi_device *acpi_dev = to_acpi_device(dev);
308         struct acpi_driver *acpi_drv = to_acpi_driver(drv);
309
310         return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
311 }
312
313 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
314 {
315         struct acpi_device *acpi_dev = to_acpi_device(dev);
316         int len;
317
318         if (add_uevent_var(env, "MODALIAS="))
319                 return -ENOMEM;
320         len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
321                               sizeof(env->buf) - env->buflen);
322         if (len >= (sizeof(env->buf) - env->buflen))
323                 return -ENOMEM;
324         env->buflen += len;
325         return 0;
326 }
327
328 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
329 {
330         struct acpi_device *device = data;
331
332         device->driver->ops.notify(device, event);
333 }
334
335 static acpi_status acpi_device_notify_fixed(void *data)
336 {
337         struct acpi_device *device = data;
338
339         /* Fixed hardware devices have no handles */
340         acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
341         return AE_OK;
342 }
343
344 static int acpi_device_install_notify_handler(struct acpi_device *device)
345 {
346         acpi_status status;
347
348         if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
349                 status =
350                     acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
351                                                      acpi_device_notify_fixed,
352                                                      device);
353         else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
354                 status =
355                     acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
356                                                      acpi_device_notify_fixed,
357                                                      device);
358         else
359                 status = acpi_install_notify_handler(device->handle,
360                                                      ACPI_DEVICE_NOTIFY,
361                                                      acpi_device_notify,
362                                                      device);
363
364         if (ACPI_FAILURE(status))
365                 return -EINVAL;
366         return 0;
367 }
368
369 static void acpi_device_remove_notify_handler(struct acpi_device *device)
370 {
371         if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
372                 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
373                                                 acpi_device_notify_fixed);
374         else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
375                 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
376                                                 acpi_device_notify_fixed);
377         else
378                 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
379                                            acpi_device_notify);
380 }
381
382 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
383 static int acpi_start_single_object(struct acpi_device *);
384 static int acpi_device_probe(struct device * dev)
385 {
386         struct acpi_device *acpi_dev = to_acpi_device(dev);
387         struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
388         int ret;
389
390         ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
391         if (!ret) {
392                 if (acpi_dev->bus_ops.acpi_op_start)
393                         acpi_start_single_object(acpi_dev);
394
395                 if (acpi_drv->ops.notify) {
396                         ret = acpi_device_install_notify_handler(acpi_dev);
397                         if (ret) {
398                                 if (acpi_drv->ops.remove)
399                                         acpi_drv->ops.remove(acpi_dev,
400                                                      acpi_dev->removal_type);
401                                 return ret;
402                         }
403                 }
404
405                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
406                         "Found driver [%s] for device [%s]\n",
407                         acpi_drv->name, acpi_dev->pnp.bus_id));
408                 get_device(dev);
409         }
410         return ret;
411 }
412
413 static int acpi_device_remove(struct device * dev)
414 {
415         struct acpi_device *acpi_dev = to_acpi_device(dev);
416         struct acpi_driver *acpi_drv = acpi_dev->driver;
417
418         if (acpi_drv) {
419                 if (acpi_drv->ops.notify)
420                         acpi_device_remove_notify_handler(acpi_dev);
421                 if (acpi_drv->ops.remove)
422                         acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
423         }
424         acpi_dev->driver = NULL;
425         acpi_dev->driver_data = NULL;
426
427         put_device(dev);
428         return 0;
429 }
430
431 struct bus_type acpi_bus_type = {
432         .name           = "acpi",
433         .suspend        = acpi_device_suspend,
434         .resume         = acpi_device_resume,
435         .match          = acpi_bus_match,
436         .probe          = acpi_device_probe,
437         .remove         = acpi_device_remove,
438         .uevent         = acpi_device_uevent,
439 };
440
441 static int acpi_device_register(struct acpi_device *device)
442 {
443         int result;
444         struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
445         int found = 0;
446
447         /*
448          * Linkage
449          * -------
450          * Link this device to its parent and siblings.
451          */
452         INIT_LIST_HEAD(&device->children);
453         INIT_LIST_HEAD(&device->node);
454         INIT_LIST_HEAD(&device->wakeup_list);
455
456         new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
457         if (!new_bus_id) {
458                 printk(KERN_ERR PREFIX "Memory allocation error\n");
459                 return -ENOMEM;
460         }
461
462         mutex_lock(&acpi_device_lock);
463         /*
464          * Find suitable bus_id and instance number in acpi_bus_id_list
465          * If failed, create one and link it into acpi_bus_id_list
466          */
467         list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
468                 if (!strcmp(acpi_device_bus_id->bus_id,
469                             acpi_device_hid(device))) {
470                         acpi_device_bus_id->instance_no++;
471                         found = 1;
472                         kfree(new_bus_id);
473                         break;
474                 }
475         }
476         if (!found) {
477                 acpi_device_bus_id = new_bus_id;
478                 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
479                 acpi_device_bus_id->instance_no = 0;
480                 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
481         }
482         dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
483
484         if (device->parent)
485                 list_add_tail(&device->node, &device->parent->children);
486
487         if (device->wakeup.flags.valid)
488                 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
489         mutex_unlock(&acpi_device_lock);
490
491         if (device->parent)
492                 device->dev.parent = &device->parent->dev;
493         device->dev.bus = &acpi_bus_type;
494         device->dev.release = &acpi_device_release;
495         result = device_register(&device->dev);
496         if (result) {
497                 dev_err(&device->dev, "Error registering device\n");
498                 goto end;
499         }
500
501         result = acpi_device_setup_files(device);
502         if (result)
503                 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
504                        dev_name(&device->dev));
505
506         device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
507         return 0;
508 end:
509         mutex_lock(&acpi_device_lock);
510         if (device->parent)
511                 list_del(&device->node);
512         list_del(&device->wakeup_list);
513         mutex_unlock(&acpi_device_lock);
514         return result;
515 }
516
517 static void acpi_device_unregister(struct acpi_device *device, int type)
518 {
519         mutex_lock(&acpi_device_lock);
520         if (device->parent)
521                 list_del(&device->node);
522
523         list_del(&device->wakeup_list);
524         mutex_unlock(&acpi_device_lock);
525
526         acpi_detach_data(device->handle, acpi_bus_data_handler);
527
528         acpi_device_remove_files(device);
529         device_unregister(&device->dev);
530 }
531
532 /* --------------------------------------------------------------------------
533                                  Driver Management
534    -------------------------------------------------------------------------- */
535 /**
536  * acpi_bus_driver_init - add a device to a driver
537  * @device: the device to add and initialize
538  * @driver: driver for the device
539  *
540  * Used to initialize a device via its device driver.  Called whenever a
541  * driver is bound to a device.  Invokes the driver's add() ops.
542  */
543 static int
544 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
545 {
546         int result = 0;
547
548         if (!device || !driver)
549                 return -EINVAL;
550
551         if (!driver->ops.add)
552                 return -ENOSYS;
553
554         result = driver->ops.add(device);
555         if (result) {
556                 device->driver = NULL;
557                 device->driver_data = NULL;
558                 return result;
559         }
560
561         device->driver = driver;
562
563         /*
564          * TBD - Configuration Management: Assign resources to device based
565          * upon possible configuration and currently allocated resources.
566          */
567
568         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
569                           "Driver successfully bound to device\n"));
570         return 0;
571 }
572
573 static int acpi_start_single_object(struct acpi_device *device)
574 {
575         int result = 0;
576         struct acpi_driver *driver;
577
578
579         if (!(driver = device->driver))
580                 return 0;
581
582         if (driver->ops.start) {
583                 result = driver->ops.start(device);
584                 if (result && driver->ops.remove)
585                         driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
586         }
587
588         return result;
589 }
590
591 /**
592  * acpi_bus_register_driver - register a driver with the ACPI bus
593  * @driver: driver being registered
594  *
595  * Registers a driver with the ACPI bus.  Searches the namespace for all
596  * devices that match the driver's criteria and binds.  Returns zero for
597  * success or a negative error status for failure.
598  */
599 int acpi_bus_register_driver(struct acpi_driver *driver)
600 {
601         int ret;
602
603         if (acpi_disabled)
604                 return -ENODEV;
605         driver->drv.name = driver->name;
606         driver->drv.bus = &acpi_bus_type;
607         driver->drv.owner = driver->owner;
608
609         ret = driver_register(&driver->drv);
610         return ret;
611 }
612
613 EXPORT_SYMBOL(acpi_bus_register_driver);
614
615 /**
616  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
617  * @driver: driver to unregister
618  *
619  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
620  * devices that match the driver's criteria and unbinds.
621  */
622 void acpi_bus_unregister_driver(struct acpi_driver *driver)
623 {
624         driver_unregister(&driver->drv);
625 }
626
627 EXPORT_SYMBOL(acpi_bus_unregister_driver);
628
629 /* --------------------------------------------------------------------------
630                                  Device Enumeration
631    -------------------------------------------------------------------------- */
632 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
633 {
634         acpi_status status;
635         int ret;
636         struct acpi_device *device;
637
638         /*
639          * Fixed hardware devices do not appear in the namespace and do not
640          * have handles, but we fabricate acpi_devices for them, so we have
641          * to deal with them specially.
642          */
643         if (handle == NULL)
644                 return acpi_root;
645
646         do {
647                 status = acpi_get_parent(handle, &handle);
648                 if (status == AE_NULL_ENTRY)
649                         return NULL;
650                 if (ACPI_FAILURE(status))
651                         return acpi_root;
652
653                 ret = acpi_bus_get_device(handle, &device);
654                 if (ret == 0)
655                         return device;
656         } while (1);
657 }
658
659 acpi_status
660 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
661 {
662         acpi_status status;
663         acpi_handle tmp;
664         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
665         union acpi_object *obj;
666
667         status = acpi_get_handle(handle, "_EJD", &tmp);
668         if (ACPI_FAILURE(status))
669                 return status;
670
671         status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
672         if (ACPI_SUCCESS(status)) {
673                 obj = buffer.pointer;
674                 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
675                                          ejd);
676                 kfree(buffer.pointer);
677         }
678         return status;
679 }
680 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
681
682 void acpi_bus_data_handler(acpi_handle handle, void *context)
683 {
684
685         /* TBD */
686
687         return;
688 }
689
690 static int acpi_bus_get_perf_flags(struct acpi_device *device)
691 {
692         device->performance.state = ACPI_STATE_UNKNOWN;
693         return 0;
694 }
695
696 static acpi_status
697 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
698                                              union acpi_object *package)
699 {
700         int i = 0;
701         union acpi_object *element = NULL;
702
703         if (!device || !package || (package->package.count < 2))
704                 return AE_BAD_PARAMETER;
705
706         element = &(package->package.elements[0]);
707         if (!element)
708                 return AE_BAD_PARAMETER;
709         if (element->type == ACPI_TYPE_PACKAGE) {
710                 if ((element->package.count < 2) ||
711                     (element->package.elements[0].type !=
712                      ACPI_TYPE_LOCAL_REFERENCE)
713                     || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
714                         return AE_BAD_DATA;
715                 device->wakeup.gpe_device =
716                     element->package.elements[0].reference.handle;
717                 device->wakeup.gpe_number =
718                     (u32) element->package.elements[1].integer.value;
719         } else if (element->type == ACPI_TYPE_INTEGER) {
720                 device->wakeup.gpe_number = element->integer.value;
721         } else
722                 return AE_BAD_DATA;
723
724         element = &(package->package.elements[1]);
725         if (element->type != ACPI_TYPE_INTEGER) {
726                 return AE_BAD_DATA;
727         }
728         device->wakeup.sleep_state = element->integer.value;
729
730         if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
731                 return AE_NO_MEMORY;
732         }
733         device->wakeup.resources.count = package->package.count - 2;
734         for (i = 0; i < device->wakeup.resources.count; i++) {
735                 element = &(package->package.elements[i + 2]);
736                 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
737                         return AE_BAD_DATA;
738
739                 device->wakeup.resources.handles[i] = element->reference.handle;
740         }
741
742         return AE_OK;
743 }
744
745 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
746 {
747         struct acpi_device_id button_device_ids[] = {
748                 {"PNP0C0D", 0},
749                 {"PNP0C0C", 0},
750                 {"PNP0C0E", 0},
751                 {"", 0},
752         };
753         acpi_status status;
754         acpi_event_status event_status;
755
756         device->wakeup.run_wake_count = 0;
757         device->wakeup.flags.notifier_present = 0;
758
759         /* Power button, Lid switch always enable wakeup */
760         if (!acpi_match_device_ids(device, button_device_ids)) {
761                 device->wakeup.flags.run_wake = 1;
762                 device->wakeup.flags.always_enabled = 1;
763                 return;
764         }
765
766         status = acpi_get_gpe_status(NULL, device->wakeup.gpe_number,
767                                         ACPI_NOT_ISR, &event_status);
768         if (status == AE_OK)
769                 device->wakeup.flags.run_wake =
770                                 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
771 }
772
773 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
774 {
775         acpi_status status = 0;
776         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
777         union acpi_object *package = NULL;
778         int psw_error;
779
780         /* _PRW */
781         status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
782         if (ACPI_FAILURE(status)) {
783                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
784                 goto end;
785         }
786
787         package = (union acpi_object *)buffer.pointer;
788         status = acpi_bus_extract_wakeup_device_power_package(device, package);
789         if (ACPI_FAILURE(status)) {
790                 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
791                 goto end;
792         }
793
794         kfree(buffer.pointer);
795
796         device->wakeup.flags.valid = 1;
797         device->wakeup.prepare_count = 0;
798         acpi_bus_set_run_wake_flags(device);
799         /* Call _PSW/_DSW object to disable its ability to wake the sleeping
800          * system for the ACPI device with the _PRW object.
801          * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
802          * So it is necessary to call _DSW object first. Only when it is not
803          * present will the _PSW object used.
804          */
805         psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
806         if (psw_error)
807                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
808                                 "error in _DSW or _PSW evaluation\n"));
809
810 end:
811         if (ACPI_FAILURE(status))
812                 device->flags.wake_capable = 0;
813         return 0;
814 }
815
816 static int acpi_bus_get_power_flags(struct acpi_device *device)
817 {
818         acpi_status status = 0;
819         acpi_handle handle = NULL;
820         u32 i = 0;
821
822
823         /*
824          * Power Management Flags
825          */
826         status = acpi_get_handle(device->handle, "_PSC", &handle);
827         if (ACPI_SUCCESS(status))
828                 device->power.flags.explicit_get = 1;
829         status = acpi_get_handle(device->handle, "_IRC", &handle);
830         if (ACPI_SUCCESS(status))
831                 device->power.flags.inrush_current = 1;
832
833         /*
834          * Enumerate supported power management states
835          */
836         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
837                 struct acpi_device_power_state *ps = &device->power.states[i];
838                 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
839
840                 /* Evaluate "_PRx" to se if power resources are referenced */
841                 acpi_evaluate_reference(device->handle, object_name, NULL,
842                                         &ps->resources);
843                 if (ps->resources.count) {
844                         device->power.flags.power_resources = 1;
845                         ps->flags.valid = 1;
846                 }
847
848                 /* Evaluate "_PSx" to see if we can do explicit sets */
849                 object_name[2] = 'S';
850                 status = acpi_get_handle(device->handle, object_name, &handle);
851                 if (ACPI_SUCCESS(status)) {
852                         ps->flags.explicit_set = 1;
853                         ps->flags.valid = 1;
854                 }
855
856                 /* State is valid if we have some power control */
857                 if (ps->resources.count || ps->flags.explicit_set)
858                         ps->flags.valid = 1;
859
860                 ps->power = -1; /* Unknown - driver assigned */
861                 ps->latency = -1;       /* Unknown - driver assigned */
862         }
863
864         /* Set defaults for D0 and D3 states (always valid) */
865         device->power.states[ACPI_STATE_D0].flags.valid = 1;
866         device->power.states[ACPI_STATE_D0].power = 100;
867         device->power.states[ACPI_STATE_D3].flags.valid = 1;
868         device->power.states[ACPI_STATE_D3].power = 0;
869
870         /* TBD: System wake support and resource requirements. */
871
872         device->power.state = ACPI_STATE_UNKNOWN;
873         acpi_bus_get_power(device->handle, &(device->power.state));
874
875         return 0;
876 }
877
878 static int acpi_bus_get_flags(struct acpi_device *device)
879 {
880         acpi_status status = AE_OK;
881         acpi_handle temp = NULL;
882
883
884         /* Presence of _STA indicates 'dynamic_status' */
885         status = acpi_get_handle(device->handle, "_STA", &temp);
886         if (ACPI_SUCCESS(status))
887                 device->flags.dynamic_status = 1;
888
889         /* Presence of _RMV indicates 'removable' */
890         status = acpi_get_handle(device->handle, "_RMV", &temp);
891         if (ACPI_SUCCESS(status))
892                 device->flags.removable = 1;
893
894         /* Presence of _EJD|_EJ0 indicates 'ejectable' */
895         status = acpi_get_handle(device->handle, "_EJD", &temp);
896         if (ACPI_SUCCESS(status))
897                 device->flags.ejectable = 1;
898         else {
899                 status = acpi_get_handle(device->handle, "_EJ0", &temp);
900                 if (ACPI_SUCCESS(status))
901                         device->flags.ejectable = 1;
902         }
903
904         /* Presence of _LCK indicates 'lockable' */
905         status = acpi_get_handle(device->handle, "_LCK", &temp);
906         if (ACPI_SUCCESS(status))
907                 device->flags.lockable = 1;
908
909         /* Presence of _PS0|_PR0 indicates 'power manageable' */
910         status = acpi_get_handle(device->handle, "_PS0", &temp);
911         if (ACPI_FAILURE(status))
912                 status = acpi_get_handle(device->handle, "_PR0", &temp);
913         if (ACPI_SUCCESS(status))
914                 device->flags.power_manageable = 1;
915
916         /* Presence of _PRW indicates wake capable */
917         status = acpi_get_handle(device->handle, "_PRW", &temp);
918         if (ACPI_SUCCESS(status))
919                 device->flags.wake_capable = 1;
920
921         /* TBD: Performance management */
922
923         return 0;
924 }
925
926 static void acpi_device_get_busid(struct acpi_device *device)
927 {
928         char bus_id[5] = { '?', 0 };
929         struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
930         int i = 0;
931
932         /*
933          * Bus ID
934          * ------
935          * The device's Bus ID is simply the object name.
936          * TBD: Shouldn't this value be unique (within the ACPI namespace)?
937          */
938         if (ACPI_IS_ROOT_DEVICE(device)) {
939                 strcpy(device->pnp.bus_id, "ACPI");
940                 return;
941         }
942
943         switch (device->device_type) {
944         case ACPI_BUS_TYPE_POWER_BUTTON:
945                 strcpy(device->pnp.bus_id, "PWRF");
946                 break;
947         case ACPI_BUS_TYPE_SLEEP_BUTTON:
948                 strcpy(device->pnp.bus_id, "SLPF");
949                 break;
950         default:
951                 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
952                 /* Clean up trailing underscores (if any) */
953                 for (i = 3; i > 1; i--) {
954                         if (bus_id[i] == '_')
955                                 bus_id[i] = '\0';
956                         else
957                                 break;
958                 }
959                 strcpy(device->pnp.bus_id, bus_id);
960                 break;
961         }
962 }
963
964 /*
965  * acpi_bay_match - see if a device is an ejectable driver bay
966  *
967  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
968  * then we can safely call it an ejectable drive bay
969  */
970 static int acpi_bay_match(struct acpi_device *device){
971         acpi_status status;
972         acpi_handle handle;
973         acpi_handle tmp;
974         acpi_handle phandle;
975
976         handle = device->handle;
977
978         status = acpi_get_handle(handle, "_EJ0", &tmp);
979         if (ACPI_FAILURE(status))
980                 return -ENODEV;
981
982         if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
983                 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
984                 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
985                 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
986                 return 0;
987
988         if (acpi_get_parent(handle, &phandle))
989                 return -ENODEV;
990
991         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
992                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
993                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
994                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
995                 return 0;
996
997         return -ENODEV;
998 }
999
1000 /*
1001  * acpi_dock_match - see if a device has a _DCK method
1002  */
1003 static int acpi_dock_match(struct acpi_device *device)
1004 {
1005         acpi_handle tmp;
1006         return acpi_get_handle(device->handle, "_DCK", &tmp);
1007 }
1008
1009 char *acpi_device_hid(struct acpi_device *device)
1010 {
1011         struct acpi_hardware_id *hid;
1012
1013         hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1014         return hid->id;
1015 }
1016 EXPORT_SYMBOL(acpi_device_hid);
1017
1018 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1019 {
1020         struct acpi_hardware_id *id;
1021
1022         id = kmalloc(sizeof(*id), GFP_KERNEL);
1023         if (!id)
1024                 return;
1025
1026         id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1027         if (!id->id) {
1028                 kfree(id);
1029                 return;
1030         }
1031
1032         strcpy(id->id, dev_id);
1033         list_add_tail(&id->list, &device->pnp.ids);
1034 }
1035
1036 /*
1037  * Old IBM workstations have a DSDT bug wherein the SMBus object
1038  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1039  * prefix.  Work around this.
1040  */
1041 static int acpi_ibm_smbus_match(struct acpi_device *device)
1042 {
1043         acpi_handle h_dummy;
1044         struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1045         int result;
1046
1047         if (!dmi_name_in_vendors("IBM"))
1048                 return -ENODEV;
1049
1050         /* Look for SMBS object */
1051         result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1052         if (result)
1053                 return result;
1054
1055         if (strcmp("SMBS", path.pointer)) {
1056                 result = -ENODEV;
1057                 goto out;
1058         }
1059
1060         /* Does it have the necessary (but misnamed) methods? */
1061         result = -ENODEV;
1062         if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1063             ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1064             ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1065                 result = 0;
1066 out:
1067         kfree(path.pointer);
1068         return result;
1069 }
1070
1071 static void acpi_device_set_id(struct acpi_device *device)
1072 {
1073         acpi_status status;
1074         struct acpi_device_info *info;
1075         struct acpica_device_id_list *cid_list;
1076         int i;
1077
1078         switch (device->device_type) {
1079         case ACPI_BUS_TYPE_DEVICE:
1080                 if (ACPI_IS_ROOT_DEVICE(device)) {
1081                         acpi_add_id(device, ACPI_SYSTEM_HID);
1082                         break;
1083                 } else if (ACPI_IS_ROOT_DEVICE(device->parent)) {
1084                         /* \_SB_, the only root-level namespace device */
1085                         acpi_add_id(device, ACPI_BUS_HID);
1086                         strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1087                         strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1088                         break;
1089                 }
1090
1091                 status = acpi_get_object_info(device->handle, &info);
1092                 if (ACPI_FAILURE(status)) {
1093                         printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1094                         return;
1095                 }
1096
1097                 if (info->valid & ACPI_VALID_HID)
1098                         acpi_add_id(device, info->hardware_id.string);
1099                 if (info->valid & ACPI_VALID_CID) {
1100                         cid_list = &info->compatible_id_list;
1101                         for (i = 0; i < cid_list->count; i++)
1102                                 acpi_add_id(device, cid_list->ids[i].string);
1103                 }
1104                 if (info->valid & ACPI_VALID_ADR) {
1105                         device->pnp.bus_address = info->address;
1106                         device->flags.bus_address = 1;
1107                 }
1108
1109                 kfree(info);
1110
1111                 /*
1112                  * Some devices don't reliably have _HIDs & _CIDs, so add
1113                  * synthetic HIDs to make sure drivers can find them.
1114                  */
1115                 if (acpi_is_video_device(device))
1116                         acpi_add_id(device, ACPI_VIDEO_HID);
1117                 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1118                         acpi_add_id(device, ACPI_BAY_HID);
1119                 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1120                         acpi_add_id(device, ACPI_DOCK_HID);
1121                 else if (!acpi_ibm_smbus_match(device))
1122                         acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1123
1124                 break;
1125         case ACPI_BUS_TYPE_POWER:
1126                 acpi_add_id(device, ACPI_POWER_HID);
1127                 break;
1128         case ACPI_BUS_TYPE_PROCESSOR:
1129                 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1130                 break;
1131         case ACPI_BUS_TYPE_THERMAL:
1132                 acpi_add_id(device, ACPI_THERMAL_HID);
1133                 break;
1134         case ACPI_BUS_TYPE_POWER_BUTTON:
1135                 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1136                 break;
1137         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1138                 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1139                 break;
1140         }
1141
1142         /*
1143          * We build acpi_devices for some objects that don't have _HID or _CID,
1144          * e.g., PCI bridges and slots.  Drivers can't bind to these objects,
1145          * but we do use them indirectly by traversing the acpi_device tree.
1146          * This generic ID isn't useful for driver binding, but it provides
1147          * the useful property that "every acpi_device has an ID."
1148          */
1149         if (list_empty(&device->pnp.ids))
1150                 acpi_add_id(device, "device");
1151 }
1152
1153 static int acpi_device_set_context(struct acpi_device *device)
1154 {
1155         acpi_status status;
1156
1157         /*
1158          * Context
1159          * -------
1160          * Attach this 'struct acpi_device' to the ACPI object.  This makes
1161          * resolutions from handle->device very efficient.  Fixed hardware
1162          * devices have no handles, so we skip them.
1163          */
1164         if (!device->handle)
1165                 return 0;
1166
1167         status = acpi_attach_data(device->handle,
1168                                   acpi_bus_data_handler, device);
1169         if (ACPI_SUCCESS(status))
1170                 return 0;
1171
1172         printk(KERN_ERR PREFIX "Error attaching device data\n");
1173         return -ENODEV;
1174 }
1175
1176 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1177 {
1178         if (!dev)
1179                 return -EINVAL;
1180
1181         dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1182         device_release_driver(&dev->dev);
1183
1184         if (!rmdevice)
1185                 return 0;
1186
1187         /*
1188          * unbind _ADR-Based Devices when hot removal
1189          */
1190         if (dev->flags.bus_address) {
1191                 if ((dev->parent) && (dev->parent->ops.unbind))
1192                         dev->parent->ops.unbind(dev);
1193         }
1194         acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1195
1196         return 0;
1197 }
1198
1199 static int acpi_add_single_object(struct acpi_device **child,
1200                                   acpi_handle handle, int type,
1201                                   unsigned long long sta,
1202                                   struct acpi_bus_ops *ops)
1203 {
1204         int result;
1205         struct acpi_device *device;
1206         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1207
1208         device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1209         if (!device) {
1210                 printk(KERN_ERR PREFIX "Memory allocation error\n");
1211                 return -ENOMEM;
1212         }
1213
1214         INIT_LIST_HEAD(&device->pnp.ids);
1215         device->device_type = type;
1216         device->handle = handle;
1217         device->parent = acpi_bus_get_parent(handle);
1218         device->bus_ops = *ops; /* workround for not call .start */
1219         STRUCT_TO_INT(device->status) = sta;
1220
1221         acpi_device_get_busid(device);
1222
1223         /*
1224          * Flags
1225          * -----
1226          * Note that we only look for object handles -- cannot evaluate objects
1227          * until we know the device is present and properly initialized.
1228          */
1229         result = acpi_bus_get_flags(device);
1230         if (result)
1231                 goto end;
1232
1233         /*
1234          * Initialize Device
1235          * -----------------
1236          * TBD: Synch with Core's enumeration/initialization process.
1237          */
1238         acpi_device_set_id(device);
1239
1240         /*
1241          * Power Management
1242          * ----------------
1243          */
1244         if (device->flags.power_manageable) {
1245                 result = acpi_bus_get_power_flags(device);
1246                 if (result)
1247                         goto end;
1248         }
1249
1250         /*
1251          * Wakeup device management
1252          *-----------------------
1253          */
1254         if (device->flags.wake_capable) {
1255                 result = acpi_bus_get_wakeup_device_flags(device);
1256                 if (result)
1257                         goto end;
1258         }
1259
1260         /*
1261          * Performance Management
1262          * ----------------------
1263          */
1264         if (device->flags.performance_manageable) {
1265                 result = acpi_bus_get_perf_flags(device);
1266                 if (result)
1267                         goto end;
1268         }
1269
1270         if ((result = acpi_device_set_context(device)))
1271                 goto end;
1272
1273         result = acpi_device_register(device);
1274
1275         /*
1276          * Bind _ADR-Based Devices when hot add
1277          */
1278         if (device->flags.bus_address) {
1279                 if (device->parent && device->parent->ops.bind)
1280                         device->parent->ops.bind(device);
1281         }
1282
1283 end:
1284         if (!result) {
1285                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1286                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1287                         "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1288                          (char *) buffer.pointer,
1289                          device->parent ? dev_name(&device->parent->dev) :
1290                                           "(null)"));
1291                 kfree(buffer.pointer);
1292                 *child = device;
1293         } else
1294                 acpi_device_release(&device->dev);
1295
1296         return result;
1297 }
1298
1299 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1300                           ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING)
1301
1302 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1303                                     unsigned long long *sta)
1304 {
1305         acpi_status status;
1306         acpi_object_type acpi_type;
1307
1308         status = acpi_get_type(handle, &acpi_type);
1309         if (ACPI_FAILURE(status))
1310                 return -ENODEV;
1311
1312         switch (acpi_type) {
1313         case ACPI_TYPE_ANY:             /* for ACPI_ROOT_OBJECT */
1314         case ACPI_TYPE_DEVICE:
1315                 *type = ACPI_BUS_TYPE_DEVICE;
1316                 status = acpi_bus_get_status_handle(handle, sta);
1317                 if (ACPI_FAILURE(status))
1318                         return -ENODEV;
1319                 break;
1320         case ACPI_TYPE_PROCESSOR:
1321                 *type = ACPI_BUS_TYPE_PROCESSOR;
1322                 status = acpi_bus_get_status_handle(handle, sta);
1323                 if (ACPI_FAILURE(status))
1324                         return -ENODEV;
1325                 break;
1326         case ACPI_TYPE_THERMAL:
1327                 *type = ACPI_BUS_TYPE_THERMAL;
1328                 *sta = ACPI_STA_DEFAULT;
1329                 break;
1330         case ACPI_TYPE_POWER:
1331                 *type = ACPI_BUS_TYPE_POWER;
1332                 *sta = ACPI_STA_DEFAULT;
1333                 break;
1334         default:
1335                 return -ENODEV;
1336         }
1337
1338         return 0;
1339 }
1340
1341 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1342                                       void *context, void **return_value)
1343 {
1344         struct acpi_bus_ops *ops = context;
1345         int type;
1346         unsigned long long sta;
1347         struct acpi_device *device;
1348         acpi_status status;
1349         int result;
1350
1351         result = acpi_bus_type_and_status(handle, &type, &sta);
1352         if (result)
1353                 return AE_OK;
1354
1355         if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1356             !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1357                 return AE_CTRL_DEPTH;
1358
1359         /*
1360          * We may already have an acpi_device from a previous enumeration.  If
1361          * so, we needn't add it again, but we may still have to start it.
1362          */
1363         device = NULL;
1364         acpi_bus_get_device(handle, &device);
1365         if (ops->acpi_op_add && !device)
1366                 acpi_add_single_object(&device, handle, type, sta, ops);
1367
1368         if (!device)
1369                 return AE_CTRL_DEPTH;
1370
1371         if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1372                 status = acpi_start_single_object(device);
1373                 if (ACPI_FAILURE(status))
1374                         return AE_CTRL_DEPTH;
1375         }
1376
1377         if (!*return_value)
1378                 *return_value = device;
1379         return AE_OK;
1380 }
1381
1382 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1383                          struct acpi_device **child)
1384 {
1385         acpi_status status;
1386         void *device = NULL;
1387
1388         status = acpi_bus_check_add(handle, 0, ops, &device);
1389         if (ACPI_SUCCESS(status))
1390                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1391                                     acpi_bus_check_add, NULL, ops, &device);
1392
1393         if (child)
1394                 *child = device;
1395
1396         if (device)
1397                 return 0;
1398         else
1399                 return -ENODEV;
1400 }
1401
1402 /*
1403  * acpi_bus_add and acpi_bus_start
1404  *
1405  * scan a given ACPI tree and (probably recently hot-plugged)
1406  * create and add or starts found devices.
1407  *
1408  * If no devices were found -ENODEV is returned which does not
1409  * mean that this is a real error, there just have been no suitable
1410  * ACPI objects in the table trunk from which the kernel could create
1411  * a device and add/start an appropriate driver.
1412  */
1413
1414 int
1415 acpi_bus_add(struct acpi_device **child,
1416              struct acpi_device *parent, acpi_handle handle, int type)
1417 {
1418         struct acpi_bus_ops ops;
1419
1420         memset(&ops, 0, sizeof(ops));
1421         ops.acpi_op_add = 1;
1422
1423         return acpi_bus_scan(handle, &ops, child);
1424 }
1425 EXPORT_SYMBOL(acpi_bus_add);
1426
1427 int acpi_bus_start(struct acpi_device *device)
1428 {
1429         struct acpi_bus_ops ops;
1430
1431         if (!device)
1432                 return -EINVAL;
1433
1434         memset(&ops, 0, sizeof(ops));
1435         ops.acpi_op_start = 1;
1436
1437         return acpi_bus_scan(device->handle, &ops, NULL);
1438 }
1439 EXPORT_SYMBOL(acpi_bus_start);
1440
1441 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1442 {
1443         acpi_status status;
1444         struct acpi_device *parent, *child;
1445         acpi_handle phandle, chandle;
1446         acpi_object_type type;
1447         u32 level = 1;
1448         int err = 0;
1449
1450         parent = start;
1451         phandle = start->handle;
1452         child = chandle = NULL;
1453
1454         while ((level > 0) && parent && (!err)) {
1455                 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1456                                               chandle, &chandle);
1457
1458                 /*
1459                  * If this scope is exhausted then move our way back up.
1460                  */
1461                 if (ACPI_FAILURE(status)) {
1462                         level--;
1463                         chandle = phandle;
1464                         acpi_get_parent(phandle, &phandle);
1465                         child = parent;
1466                         parent = parent->parent;
1467
1468                         if (level == 0)
1469                                 err = acpi_bus_remove(child, rmdevice);
1470                         else
1471                                 err = acpi_bus_remove(child, 1);
1472
1473                         continue;
1474                 }
1475
1476                 status = acpi_get_type(chandle, &type);
1477                 if (ACPI_FAILURE(status)) {
1478                         continue;
1479                 }
1480                 /*
1481                  * If there is a device corresponding to chandle then
1482                  * parse it (depth-first).
1483                  */
1484                 if (acpi_bus_get_device(chandle, &child) == 0) {
1485                         level++;
1486                         phandle = chandle;
1487                         chandle = NULL;
1488                         parent = child;
1489                 }
1490                 continue;
1491         }
1492         return err;
1493 }
1494 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1495
1496 static int acpi_bus_scan_fixed(void)
1497 {
1498         int result = 0;
1499         struct acpi_device *device = NULL;
1500         struct acpi_bus_ops ops;
1501
1502         memset(&ops, 0, sizeof(ops));
1503         ops.acpi_op_add = 1;
1504         ops.acpi_op_start = 1;
1505
1506         /*
1507          * Enumerate all fixed-feature devices.
1508          */
1509         if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1510                 result = acpi_add_single_object(&device, NULL,
1511                                                 ACPI_BUS_TYPE_POWER_BUTTON,
1512                                                 ACPI_STA_DEFAULT,
1513                                                 &ops);
1514         }
1515
1516         if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1517                 result = acpi_add_single_object(&device, NULL,
1518                                                 ACPI_BUS_TYPE_SLEEP_BUTTON,
1519                                                 ACPI_STA_DEFAULT,
1520                                                 &ops);
1521         }
1522
1523         return result;
1524 }
1525
1526 int __init acpi_scan_init(void)
1527 {
1528         int result;
1529         struct acpi_bus_ops ops;
1530
1531         memset(&ops, 0, sizeof(ops));
1532         ops.acpi_op_add = 1;
1533         ops.acpi_op_start = 1;
1534
1535         result = bus_register(&acpi_bus_type);
1536         if (result) {
1537                 /* We don't want to quit even if we failed to add suspend/resume */
1538                 printk(KERN_ERR PREFIX "Could not register bus type\n");
1539         }
1540
1541         /*
1542          * Enumerate devices in the ACPI namespace.
1543          */
1544         result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1545
1546         if (!result)
1547                 result = acpi_bus_scan_fixed();
1548
1549         if (result)
1550                 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1551
1552         return result;
1553 }