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