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