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