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