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