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