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