- Update Xen patches to 2.6.36-rc3 and c/s 1029.
[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         acpi_gpe_can_wake(device->wakeup.gpe_device, device->wakeup.gpe_number);
764
765         return AE_OK;
766 }
767
768 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
769 {
770         struct acpi_device_id button_device_ids[] = {
771                 {"PNP0C0D", 0},
772                 {"PNP0C0C", 0},
773                 {"PNP0C0E", 0},
774                 {"", 0},
775         };
776         acpi_status status;
777         acpi_event_status event_status;
778
779         device->wakeup.run_wake_count = 0;
780         device->wakeup.flags.notifier_present = 0;
781
782         /* Power button, Lid switch always enable wakeup */
783         if (!acpi_match_device_ids(device, button_device_ids)) {
784                 device->wakeup.flags.run_wake = 1;
785                 device->wakeup.flags.always_enabled = 1;
786                 return;
787         }
788
789         status = acpi_get_gpe_status(device->wakeup.gpe_device,
790                                         device->wakeup.gpe_number,
791                                                 &event_status);
792         if (status == AE_OK)
793                 device->wakeup.flags.run_wake =
794                                 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
795 }
796
797 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
798 {
799         acpi_status status = 0;
800         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
801         union acpi_object *package = NULL;
802         int psw_error;
803
804         /* _PRW */
805         status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
806         if (ACPI_FAILURE(status)) {
807                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
808                 goto end;
809         }
810
811         package = (union acpi_object *)buffer.pointer;
812         status = acpi_bus_extract_wakeup_device_power_package(device, package);
813         if (ACPI_FAILURE(status)) {
814                 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
815                 goto end;
816         }
817
818         kfree(buffer.pointer);
819
820         device->wakeup.flags.valid = 1;
821         device->wakeup.prepare_count = 0;
822         acpi_bus_set_run_wake_flags(device);
823         /* Call _PSW/_DSW object to disable its ability to wake the sleeping
824          * system for the ACPI device with the _PRW object.
825          * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
826          * So it is necessary to call _DSW object first. Only when it is not
827          * present will the _PSW object used.
828          */
829         psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
830         if (psw_error)
831                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
832                                 "error in _DSW or _PSW evaluation\n"));
833
834 end:
835         if (ACPI_FAILURE(status))
836                 device->flags.wake_capable = 0;
837         return 0;
838 }
839
840 static int acpi_bus_get_power_flags(struct acpi_device *device)
841 {
842         acpi_status status = 0;
843         acpi_handle handle = NULL;
844         u32 i = 0;
845
846
847         /*
848          * Power Management Flags
849          */
850         status = acpi_get_handle(device->handle, "_PSC", &handle);
851         if (ACPI_SUCCESS(status))
852                 device->power.flags.explicit_get = 1;
853         status = acpi_get_handle(device->handle, "_IRC", &handle);
854         if (ACPI_SUCCESS(status))
855                 device->power.flags.inrush_current = 1;
856
857         /*
858          * Enumerate supported power management states
859          */
860         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
861                 struct acpi_device_power_state *ps = &device->power.states[i];
862                 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
863
864                 /* Evaluate "_PRx" to se if power resources are referenced */
865                 acpi_evaluate_reference(device->handle, object_name, NULL,
866                                         &ps->resources);
867                 if (ps->resources.count) {
868                         device->power.flags.power_resources = 1;
869                         ps->flags.valid = 1;
870                 }
871
872                 /* Evaluate "_PSx" to see if we can do explicit sets */
873                 object_name[2] = 'S';
874                 status = acpi_get_handle(device->handle, object_name, &handle);
875                 if (ACPI_SUCCESS(status)) {
876                         ps->flags.explicit_set = 1;
877                         ps->flags.valid = 1;
878                 }
879
880                 /* State is valid if we have some power control */
881                 if (ps->resources.count || ps->flags.explicit_set)
882                         ps->flags.valid = 1;
883
884                 ps->power = -1; /* Unknown - driver assigned */
885                 ps->latency = -1;       /* Unknown - driver assigned */
886         }
887
888         /* Set defaults for D0 and D3 states (always valid) */
889         device->power.states[ACPI_STATE_D0].flags.valid = 1;
890         device->power.states[ACPI_STATE_D0].power = 100;
891         device->power.states[ACPI_STATE_D3].flags.valid = 1;
892         device->power.states[ACPI_STATE_D3].power = 0;
893
894         /* TBD: System wake support and resource requirements. */
895
896         device->power.state = ACPI_STATE_UNKNOWN;
897         acpi_bus_get_power(device->handle, &(device->power.state));
898
899         return 0;
900 }
901
902 static int acpi_bus_get_flags(struct acpi_device *device)
903 {
904         acpi_status status = AE_OK;
905         acpi_handle temp = NULL;
906
907
908         /* Presence of _STA indicates 'dynamic_status' */
909         status = acpi_get_handle(device->handle, "_STA", &temp);
910         if (ACPI_SUCCESS(status))
911                 device->flags.dynamic_status = 1;
912
913         /* Presence of _RMV indicates 'removable' */
914         status = acpi_get_handle(device->handle, "_RMV", &temp);
915         if (ACPI_SUCCESS(status))
916                 device->flags.removable = 1;
917
918         /* Presence of _EJD|_EJ0 indicates 'ejectable' */
919         status = acpi_get_handle(device->handle, "_EJD", &temp);
920         if (ACPI_SUCCESS(status))
921                 device->flags.ejectable = 1;
922         else {
923                 status = acpi_get_handle(device->handle, "_EJ0", &temp);
924                 if (ACPI_SUCCESS(status))
925                         device->flags.ejectable = 1;
926         }
927
928         /* Presence of _LCK indicates 'lockable' */
929         status = acpi_get_handle(device->handle, "_LCK", &temp);
930         if (ACPI_SUCCESS(status))
931                 device->flags.lockable = 1;
932
933         /* Presence of _PS0|_PR0 indicates 'power manageable' */
934         status = acpi_get_handle(device->handle, "_PS0", &temp);
935         if (ACPI_FAILURE(status))
936                 status = acpi_get_handle(device->handle, "_PR0", &temp);
937         if (ACPI_SUCCESS(status))
938                 device->flags.power_manageable = 1;
939
940         /* Presence of _PRW indicates wake capable */
941         status = acpi_get_handle(device->handle, "_PRW", &temp);
942         if (ACPI_SUCCESS(status))
943                 device->flags.wake_capable = 1;
944
945         /* TBD: Performance management */
946
947         return 0;
948 }
949
950 static void acpi_device_get_busid(struct acpi_device *device)
951 {
952         char bus_id[5] = { '?', 0 };
953         struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
954         int i = 0;
955
956         /*
957          * Bus ID
958          * ------
959          * The device's Bus ID is simply the object name.
960          * TBD: Shouldn't this value be unique (within the ACPI namespace)?
961          */
962         if (ACPI_IS_ROOT_DEVICE(device)) {
963                 strcpy(device->pnp.bus_id, "ACPI");
964                 return;
965         }
966
967         switch (device->device_type) {
968         case ACPI_BUS_TYPE_POWER_BUTTON:
969                 strcpy(device->pnp.bus_id, "PWRF");
970                 break;
971         case ACPI_BUS_TYPE_SLEEP_BUTTON:
972                 strcpy(device->pnp.bus_id, "SLPF");
973                 break;
974         default:
975                 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
976                 /* Clean up trailing underscores (if any) */
977                 for (i = 3; i > 1; i--) {
978                         if (bus_id[i] == '_')
979                                 bus_id[i] = '\0';
980                         else
981                                 break;
982                 }
983                 strcpy(device->pnp.bus_id, bus_id);
984                 break;
985         }
986 }
987
988 /*
989  * acpi_bay_match - see if a device is an ejectable driver bay
990  *
991  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
992  * then we can safely call it an ejectable drive bay
993  */
994 static int acpi_bay_match(struct acpi_device *device){
995         acpi_status status;
996         acpi_handle handle;
997         acpi_handle tmp;
998         acpi_handle phandle;
999
1000         handle = device->handle;
1001
1002         status = acpi_get_handle(handle, "_EJ0", &tmp);
1003         if (ACPI_FAILURE(status))
1004                 return -ENODEV;
1005
1006         if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1007                 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1008                 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1009                 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1010                 return 0;
1011
1012         if (acpi_get_parent(handle, &phandle))
1013                 return -ENODEV;
1014
1015         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1016                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1017                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1018                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1019                 return 0;
1020
1021         return -ENODEV;
1022 }
1023
1024 /*
1025  * acpi_dock_match - see if a device has a _DCK method
1026  */
1027 static int acpi_dock_match(struct acpi_device *device)
1028 {
1029         acpi_handle tmp;
1030         return acpi_get_handle(device->handle, "_DCK", &tmp);
1031 }
1032
1033 char *acpi_device_hid(struct acpi_device *device)
1034 {
1035         struct acpi_hardware_id *hid;
1036
1037         hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1038         return hid->id;
1039 }
1040 EXPORT_SYMBOL(acpi_device_hid);
1041
1042 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1043 {
1044         struct acpi_hardware_id *id;
1045
1046         id = kmalloc(sizeof(*id), GFP_KERNEL);
1047         if (!id)
1048                 return;
1049
1050         id->id = kmalloc(strlen(dev_id) + 1, GFP_KERNEL);
1051         if (!id->id) {
1052                 kfree(id);
1053                 return;
1054         }
1055
1056         strcpy(id->id, dev_id);
1057         list_add_tail(&id->list, &device->pnp.ids);
1058 }
1059
1060 /*
1061  * Old IBM workstations have a DSDT bug wherein the SMBus object
1062  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1063  * prefix.  Work around this.
1064  */
1065 static int acpi_ibm_smbus_match(struct acpi_device *device)
1066 {
1067         acpi_handle h_dummy;
1068         struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1069         int result;
1070
1071         if (!dmi_name_in_vendors("IBM"))
1072                 return -ENODEV;
1073
1074         /* Look for SMBS object */
1075         result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1076         if (result)
1077                 return result;
1078
1079         if (strcmp("SMBS", path.pointer)) {
1080                 result = -ENODEV;
1081                 goto out;
1082         }
1083
1084         /* Does it have the necessary (but misnamed) methods? */
1085         result = -ENODEV;
1086         if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1087             ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1088             ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1089                 result = 0;
1090 out:
1091         kfree(path.pointer);
1092         return result;
1093 }
1094
1095 static void acpi_device_set_id(struct acpi_device *device)
1096 {
1097         acpi_status status;
1098         struct acpi_device_info *info;
1099         struct acpica_device_id_list *cid_list;
1100         int i;
1101
1102         switch (device->device_type) {
1103         case ACPI_BUS_TYPE_DEVICE:
1104                 if (ACPI_IS_ROOT_DEVICE(device)) {
1105                         acpi_add_id(device, ACPI_SYSTEM_HID);
1106                         break;
1107                 }
1108
1109                 status = acpi_get_object_info(device->handle, &info);
1110                 if (ACPI_FAILURE(status)) {
1111                         printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1112                         return;
1113                 }
1114
1115                 if (info->valid & ACPI_VALID_HID)
1116                         acpi_add_id(device, info->hardware_id.string);
1117                 if (info->valid & ACPI_VALID_CID) {
1118                         cid_list = &info->compatible_id_list;
1119                         for (i = 0; i < cid_list->count; i++)
1120                                 acpi_add_id(device, cid_list->ids[i].string);
1121                 }
1122 #ifdef CONFIG_PCI_GUESTDEV
1123                 if (info->valid & ACPI_VALID_UID)
1124                         device->pnp.unique_id = kstrdup(info->unique_id.string,
1125                                                         GFP_KERNEL);
1126 #endif
1127                 if (info->valid & ACPI_VALID_ADR) {
1128                         device->pnp.bus_address = info->address;
1129                         device->flags.bus_address = 1;
1130                 }
1131
1132                 kfree(info);
1133
1134                 /*
1135                  * Some devices don't reliably have _HIDs & _CIDs, so add
1136                  * synthetic HIDs to make sure drivers can find them.
1137                  */
1138                 if (acpi_is_video_device(device))
1139                         acpi_add_id(device, ACPI_VIDEO_HID);
1140                 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1141                         acpi_add_id(device, ACPI_BAY_HID);
1142                 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1143                         acpi_add_id(device, ACPI_DOCK_HID);
1144                 else if (!acpi_ibm_smbus_match(device))
1145                         acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1146                 else if (!acpi_device_hid(device) &&
1147                          ACPI_IS_ROOT_DEVICE(device->parent)) {
1148                         acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1149                         strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1150                         strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1151                 }
1152
1153                 break;
1154         case ACPI_BUS_TYPE_POWER:
1155                 acpi_add_id(device, ACPI_POWER_HID);
1156                 break;
1157         case ACPI_BUS_TYPE_PROCESSOR:
1158                 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1159                 break;
1160         case ACPI_BUS_TYPE_THERMAL:
1161                 acpi_add_id(device, ACPI_THERMAL_HID);
1162                 break;
1163         case ACPI_BUS_TYPE_POWER_BUTTON:
1164                 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1165                 break;
1166         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1167                 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1168                 break;
1169         }
1170
1171         /*
1172          * We build acpi_devices for some objects that don't have _HID or _CID,
1173          * e.g., PCI bridges and slots.  Drivers can't bind to these objects,
1174          * but we do use them indirectly by traversing the acpi_device tree.
1175          * This generic ID isn't useful for driver binding, but it provides
1176          * the useful property that "every acpi_device has an ID."
1177          */
1178         if (list_empty(&device->pnp.ids))
1179                 acpi_add_id(device, "device");
1180 }
1181
1182 static int acpi_device_set_context(struct acpi_device *device)
1183 {
1184         acpi_status status;
1185
1186         /*
1187          * Context
1188          * -------
1189          * Attach this 'struct acpi_device' to the ACPI object.  This makes
1190          * resolutions from handle->device very efficient.  Fixed hardware
1191          * devices have no handles, so we skip them.
1192          */
1193         if (!device->handle)
1194                 return 0;
1195
1196         status = acpi_attach_data(device->handle,
1197                                   acpi_bus_data_handler, device);
1198         if (ACPI_SUCCESS(status))
1199                 return 0;
1200
1201         printk(KERN_ERR PREFIX "Error attaching device data\n");
1202         return -ENODEV;
1203 }
1204
1205 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1206 {
1207         if (!dev)
1208                 return -EINVAL;
1209
1210         dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1211         device_release_driver(&dev->dev);
1212
1213         if (!rmdevice)
1214                 return 0;
1215
1216         /*
1217          * unbind _ADR-Based Devices when hot removal
1218          */
1219         if (dev->flags.bus_address) {
1220                 if ((dev->parent) && (dev->parent->ops.unbind))
1221                         dev->parent->ops.unbind(dev);
1222         }
1223         acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1224
1225         return 0;
1226 }
1227
1228 static int acpi_add_single_object(struct acpi_device **child,
1229                                   acpi_handle handle, int type,
1230                                   unsigned long long sta,
1231                                   struct acpi_bus_ops *ops)
1232 {
1233         int result;
1234         struct acpi_device *device;
1235         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1236
1237         device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1238         if (!device) {
1239                 printk(KERN_ERR PREFIX "Memory allocation error\n");
1240                 return -ENOMEM;
1241         }
1242
1243         INIT_LIST_HEAD(&device->pnp.ids);
1244         device->device_type = type;
1245         device->handle = handle;
1246         device->parent = acpi_bus_get_parent(handle);
1247         device->bus_ops = *ops; /* workround for not call .start */
1248         STRUCT_TO_INT(device->status) = sta;
1249
1250         acpi_device_get_busid(device);
1251
1252         /*
1253          * Flags
1254          * -----
1255          * Note that we only look for object handles -- cannot evaluate objects
1256          * until we know the device is present and properly initialized.
1257          */
1258         result = acpi_bus_get_flags(device);
1259         if (result)
1260                 goto end;
1261
1262         /*
1263          * Initialize Device
1264          * -----------------
1265          * TBD: Synch with Core's enumeration/initialization process.
1266          */
1267         acpi_device_set_id(device);
1268
1269         /*
1270          * Power Management
1271          * ----------------
1272          */
1273         if (device->flags.power_manageable) {
1274                 result = acpi_bus_get_power_flags(device);
1275                 if (result)
1276                         goto end;
1277         }
1278
1279         /*
1280          * Wakeup device management
1281          *-----------------------
1282          */
1283         if (device->flags.wake_capable) {
1284                 result = acpi_bus_get_wakeup_device_flags(device);
1285                 if (result)
1286                         goto end;
1287         }
1288
1289         /*
1290          * Performance Management
1291          * ----------------------
1292          */
1293         if (device->flags.performance_manageable) {
1294                 result = acpi_bus_get_perf_flags(device);
1295                 if (result)
1296                         goto end;
1297         }
1298
1299         if ((result = acpi_device_set_context(device)))
1300                 goto end;
1301
1302         result = acpi_device_register(device);
1303
1304         /*
1305          * Bind _ADR-Based Devices when hot add
1306          */
1307         if (device->flags.bus_address) {
1308                 if (device->parent && device->parent->ops.bind)
1309                         device->parent->ops.bind(device);
1310         }
1311
1312 end:
1313         if (!result) {
1314                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1315                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1316                         "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1317                          (char *) buffer.pointer,
1318                          device->parent ? dev_name(&device->parent->dev) :
1319                                           "(null)"));
1320                 kfree(buffer.pointer);
1321                 *child = device;
1322         } else
1323                 acpi_device_release(&device->dev);
1324
1325         return result;
1326 }
1327
1328 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1329                           ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING)
1330
1331 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1332                                     unsigned long long *sta)
1333 {
1334         acpi_status status;
1335         acpi_object_type acpi_type;
1336
1337         status = acpi_get_type(handle, &acpi_type);
1338         if (ACPI_FAILURE(status))
1339                 return -ENODEV;
1340
1341         switch (acpi_type) {
1342         case ACPI_TYPE_ANY:             /* for ACPI_ROOT_OBJECT */
1343         case ACPI_TYPE_DEVICE:
1344                 *type = ACPI_BUS_TYPE_DEVICE;
1345                 status = acpi_bus_get_status_handle(handle, sta);
1346                 if (ACPI_FAILURE(status))
1347                         return -ENODEV;
1348                 break;
1349         case ACPI_TYPE_PROCESSOR:
1350                 *type = ACPI_BUS_TYPE_PROCESSOR;
1351                 status = acpi_bus_get_status_handle(handle, sta);
1352                 if (ACPI_FAILURE(status))
1353                         return -ENODEV;
1354                 break;
1355         case ACPI_TYPE_THERMAL:
1356                 *type = ACPI_BUS_TYPE_THERMAL;
1357                 *sta = ACPI_STA_DEFAULT;
1358                 break;
1359         case ACPI_TYPE_POWER:
1360                 *type = ACPI_BUS_TYPE_POWER;
1361                 *sta = ACPI_STA_DEFAULT;
1362                 break;
1363         default:
1364                 return -ENODEV;
1365         }
1366
1367         return 0;
1368 }
1369
1370 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1371                                       void *context, void **return_value)
1372 {
1373         struct acpi_bus_ops *ops = context;
1374         int type;
1375         unsigned long long sta;
1376         struct acpi_device *device;
1377         acpi_status status;
1378         int result;
1379
1380         result = acpi_bus_type_and_status(handle, &type, &sta);
1381         if (result)
1382                 return AE_OK;
1383
1384         if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1385             !(sta & ACPI_STA_DEVICE_FUNCTIONING))
1386                 return AE_CTRL_DEPTH;
1387
1388         /*
1389          * We may already have an acpi_device from a previous enumeration.  If
1390          * so, we needn't add it again, but we may still have to start it.
1391          */
1392         device = NULL;
1393         acpi_bus_get_device(handle, &device);
1394         if (ops->acpi_op_add && !device)
1395                 acpi_add_single_object(&device, handle, type, sta, ops);
1396
1397         if (!device)
1398                 return AE_CTRL_DEPTH;
1399
1400         if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1401                 status = acpi_start_single_object(device);
1402                 if (ACPI_FAILURE(status))
1403                         return AE_CTRL_DEPTH;
1404         }
1405
1406         if (!*return_value)
1407                 *return_value = device;
1408         return AE_OK;
1409 }
1410
1411 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1412                          struct acpi_device **child)
1413 {
1414         acpi_status status;
1415         void *device = NULL;
1416
1417         status = acpi_bus_check_add(handle, 0, ops, &device);
1418         if (ACPI_SUCCESS(status))
1419                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1420                                     acpi_bus_check_add, NULL, ops, &device);
1421
1422         if (child)
1423                 *child = device;
1424
1425         if (device)
1426                 return 0;
1427         else
1428                 return -ENODEV;
1429 }
1430
1431 /*
1432  * acpi_bus_add and acpi_bus_start
1433  *
1434  * scan a given ACPI tree and (probably recently hot-plugged)
1435  * create and add or starts found devices.
1436  *
1437  * If no devices were found -ENODEV is returned which does not
1438  * mean that this is a real error, there just have been no suitable
1439  * ACPI objects in the table trunk from which the kernel could create
1440  * a device and add/start an appropriate driver.
1441  */
1442
1443 int
1444 acpi_bus_add(struct acpi_device **child,
1445              struct acpi_device *parent, acpi_handle handle, int type)
1446 {
1447         struct acpi_bus_ops ops;
1448
1449         memset(&ops, 0, sizeof(ops));
1450         ops.acpi_op_add = 1;
1451
1452         return acpi_bus_scan(handle, &ops, child);
1453 }
1454 EXPORT_SYMBOL(acpi_bus_add);
1455
1456 int acpi_bus_start(struct acpi_device *device)
1457 {
1458         struct acpi_bus_ops ops;
1459
1460         if (!device)
1461                 return -EINVAL;
1462
1463         memset(&ops, 0, sizeof(ops));
1464         ops.acpi_op_start = 1;
1465
1466         return acpi_bus_scan(device->handle, &ops, NULL);
1467 }
1468 EXPORT_SYMBOL(acpi_bus_start);
1469
1470 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1471 {
1472         acpi_status status;
1473         struct acpi_device *parent, *child;
1474         acpi_handle phandle, chandle;
1475         acpi_object_type type;
1476         u32 level = 1;
1477         int err = 0;
1478
1479         parent = start;
1480         phandle = start->handle;
1481         child = chandle = NULL;
1482
1483         while ((level > 0) && parent && (!err)) {
1484                 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1485                                               chandle, &chandle);
1486
1487                 /*
1488                  * If this scope is exhausted then move our way back up.
1489                  */
1490                 if (ACPI_FAILURE(status)) {
1491                         level--;
1492                         chandle = phandle;
1493                         acpi_get_parent(phandle, &phandle);
1494                         child = parent;
1495                         parent = parent->parent;
1496
1497                         if (level == 0)
1498                                 err = acpi_bus_remove(child, rmdevice);
1499                         else
1500                                 err = acpi_bus_remove(child, 1);
1501
1502                         continue;
1503                 }
1504
1505                 status = acpi_get_type(chandle, &type);
1506                 if (ACPI_FAILURE(status)) {
1507                         continue;
1508                 }
1509                 /*
1510                  * If there is a device corresponding to chandle then
1511                  * parse it (depth-first).
1512                  */
1513                 if (acpi_bus_get_device(chandle, &child) == 0) {
1514                         level++;
1515                         phandle = chandle;
1516                         chandle = NULL;
1517                         parent = child;
1518                 }
1519                 continue;
1520         }
1521         return err;
1522 }
1523 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1524
1525 static int acpi_bus_scan_fixed(void)
1526 {
1527         int result = 0;
1528         struct acpi_device *device = NULL;
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         /*
1536          * Enumerate all fixed-feature devices.
1537          */
1538         if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1539                 result = acpi_add_single_object(&device, NULL,
1540                                                 ACPI_BUS_TYPE_POWER_BUTTON,
1541                                                 ACPI_STA_DEFAULT,
1542                                                 &ops);
1543         }
1544
1545         if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1546                 result = acpi_add_single_object(&device, NULL,
1547                                                 ACPI_BUS_TYPE_SLEEP_BUTTON,
1548                                                 ACPI_STA_DEFAULT,
1549                                                 &ops);
1550         }
1551
1552         return result;
1553 }
1554
1555 int __init acpi_scan_init(void)
1556 {
1557         int result;
1558         struct acpi_bus_ops ops;
1559
1560         memset(&ops, 0, sizeof(ops));
1561         ops.acpi_op_add = 1;
1562         ops.acpi_op_start = 1;
1563
1564         result = bus_register(&acpi_bus_type);
1565         if (result) {
1566                 /* We don't want to quit even if we failed to add suspend/resume */
1567                 printk(KERN_ERR PREFIX "Could not register bus type\n");
1568         }
1569
1570         /*
1571          * Enumerate devices in the ACPI namespace.
1572          */
1573         result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1574
1575         if (!result)
1576                 result = acpi_bus_scan_fixed();
1577
1578         if (result)
1579                 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1580
1581         return result;
1582 }