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