2 * scan.c - support for transforming the ACPI namespace into individual objects
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/acpi.h>
10 #include <acpi/acpi_drivers.h>
11 #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
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
;
18 #define ACPI_BUS_CLASS "system_bus"
19 #define ACPI_BUS_HID "ACPI_BUS"
20 #define ACPI_BUS_DEVICE_NAME "System Bus"
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
);
27 struct acpi_device_bus_id
{
29 unsigned int instance_no
;
30 struct list_head node
;
32 static int acpi_eject_operation(acpi_handle handle
, int lockable
)
34 struct acpi_object_list arg_list
;
35 union acpi_object arg
;
36 acpi_status status
= AE_OK
;
44 arg_list
.pointer
= &arg
;
45 arg
.type
= ACPI_TYPE_INTEGER
;
46 arg
.integer
.value
= 0;
47 acpi_evaluate_object(handle
, "_LCK", &arg_list
, NULL
);
51 arg_list
.pointer
= &arg
;
52 arg
.type
= ACPI_TYPE_INTEGER
;
53 arg
.integer
.value
= 1;
59 status
= acpi_evaluate_object(handle
, "_EJ0", &arg_list
, NULL
);
60 if (ACPI_FAILURE(status
)) {
68 acpi_eject_store(struct device
*d
, struct device_attribute
*attr
,
69 const char *buf
, size_t count
)
76 acpi_object_type type
= 0;
77 struct acpi_device
*acpi_device
= to_acpi_device(d
);
79 if ((!count
) || (buf
[0] != '1')) {
83 if (acpi_device
->driver
== NULL
) {
88 status
= acpi_get_type(acpi_device
->handle
, &type
);
89 if (ACPI_FAILURE(status
) || (!acpi_device
->flags
.ejectable
)) {
94 islockable
= acpi_device
->flags
.lockable
;
95 handle
= acpi_device
->handle
;
97 result
= acpi_bus_trim(acpi_device
, 1);
100 result
= acpi_eject_operation(handle
, islockable
);
109 static DEVICE_ATTR(eject
, 0200, NULL
, acpi_eject_store
);
112 acpi_device_hid_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
113 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
115 return sprintf(buf
, "%s\n", acpi_dev
->pnp
.hardware_id
);
117 static DEVICE_ATTR(hid
, 0444, acpi_device_hid_show
, NULL
);
120 acpi_device_path_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
) {
121 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
122 struct acpi_buffer path
= {ACPI_ALLOCATE_BUFFER
, NULL
};
125 result
= acpi_get_name(acpi_dev
->handle
, ACPI_FULL_PATHNAME
, &path
);
129 result
= sprintf(buf
, "%s\n", (char*)path
.pointer
);
134 static DEVICE_ATTR(path
, 0444, acpi_device_path_show
, NULL
);
136 static int acpi_device_setup_files(struct acpi_device
*dev
)
143 * Devices gotten from FADT don't have a "path" attribute
146 result
= device_create_file(&dev
->dev
, &dev_attr_path
);
151 if(dev
->flags
.hardware_id
) {
152 result
= device_create_file(&dev
->dev
, &dev_attr_hid
);
158 * If device has _EJ0, 'eject' file is created that is used to trigger
159 * hot-removal function from userland.
161 status
= acpi_get_handle(dev
->handle
, "_EJ0", &temp
);
162 if (ACPI_SUCCESS(status
))
163 result
= device_create_file(&dev
->dev
, &dev_attr_eject
);
168 static void acpi_device_remove_files(struct acpi_device
*dev
)
174 * If device has _EJ0, 'eject' file is created that is used to trigger
175 * hot-removal function from userland.
177 status
= acpi_get_handle(dev
->handle
, "_EJ0", &temp
);
178 if (ACPI_SUCCESS(status
))
179 device_remove_file(&dev
->dev
, &dev_attr_eject
);
181 if(dev
->flags
.hardware_id
)
182 device_remove_file(&dev
->dev
, &dev_attr_hid
);
184 device_remove_file(&dev
->dev
, &dev_attr_path
);
186 /* --------------------------------------------------------------------------
188 -------------------------------------------------------------------------- */
189 static void acpi_device_release(struct device
*dev
)
191 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
193 kfree(acpi_dev
->pnp
.cid_list
);
197 static int acpi_device_suspend(struct device
*dev
, pm_message_t state
)
199 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
200 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
202 if (acpi_drv
&& acpi_drv
->ops
.suspend
)
203 return acpi_drv
->ops
.suspend(acpi_dev
, state
);
207 static int acpi_device_resume(struct device
*dev
)
209 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
210 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
212 if (acpi_drv
&& acpi_drv
->ops
.resume
)
213 return acpi_drv
->ops
.resume(acpi_dev
);
217 static int acpi_bus_match(struct device
*dev
, struct device_driver
*drv
)
219 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
220 struct acpi_driver
*acpi_drv
= to_acpi_driver(drv
);
222 return !acpi_match_ids(acpi_dev
, acpi_drv
->ids
);
225 static int acpi_device_uevent(struct device
*dev
, char **envp
, int num_envp
,
226 char *buffer
, int buffer_size
)
228 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
229 int i
= 0, length
= 0, ret
= 0;
231 if (acpi_dev
->flags
.hardware_id
)
232 ret
= add_uevent_var(envp
, num_envp
, &i
,
233 buffer
, buffer_size
, &length
,
234 "HWID=%s", acpi_dev
->pnp
.hardware_id
);
237 if (acpi_dev
->flags
.compatible_ids
) {
239 struct acpi_compatible_id_list
*cid_list
;
241 cid_list
= acpi_dev
->pnp
.cid_list
;
243 for (j
= 0; j
< cid_list
->count
; j
++) {
244 ret
= add_uevent_var(envp
, num_envp
, &i
, buffer
,
245 buffer_size
, &length
, "COMPTID=%s",
246 cid_list
->id
[j
].value
);
256 static int acpi_bus_driver_init(struct acpi_device
*, struct acpi_driver
*);
257 static int acpi_start_single_object(struct acpi_device
*);
258 static int acpi_device_probe(struct device
* dev
)
260 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
261 struct acpi_driver
*acpi_drv
= to_acpi_driver(dev
->driver
);
264 ret
= acpi_bus_driver_init(acpi_dev
, acpi_drv
);
266 if (acpi_dev
->bus_ops
.acpi_op_start
)
267 acpi_start_single_object(acpi_dev
);
268 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
269 "Found driver [%s] for device [%s]\n",
270 acpi_drv
->name
, acpi_dev
->pnp
.bus_id
));
276 static int acpi_device_remove(struct device
* dev
)
278 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
279 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
282 if (acpi_drv
->ops
.stop
)
283 acpi_drv
->ops
.stop(acpi_dev
, acpi_dev
->removal_type
);
284 if (acpi_drv
->ops
.remove
)
285 acpi_drv
->ops
.remove(acpi_dev
, acpi_dev
->removal_type
);
287 acpi_dev
->driver
= NULL
;
288 acpi_driver_data(dev
) = NULL
;
294 static void acpi_device_shutdown(struct device
*dev
)
296 struct acpi_device
*acpi_dev
= to_acpi_device(dev
);
297 struct acpi_driver
*acpi_drv
= acpi_dev
->driver
;
299 if (acpi_drv
&& acpi_drv
->ops
.shutdown
)
300 acpi_drv
->ops
.shutdown(acpi_dev
);
305 struct bus_type acpi_bus_type
= {
307 .suspend
= acpi_device_suspend
,
308 .resume
= acpi_device_resume
,
309 .shutdown
= acpi_device_shutdown
,
310 .match
= acpi_bus_match
,
311 .probe
= acpi_device_probe
,
312 .remove
= acpi_device_remove
,
313 .uevent
= acpi_device_uevent
,
316 static int acpi_device_register(struct acpi_device
*device
,
317 struct acpi_device
*parent
)
320 struct acpi_device_bus_id
*acpi_device_bus_id
, *new_bus_id
;
325 * Link this device to its parent and siblings.
327 INIT_LIST_HEAD(&device
->children
);
328 INIT_LIST_HEAD(&device
->node
);
329 INIT_LIST_HEAD(&device
->g_list
);
330 INIT_LIST_HEAD(&device
->wakeup_list
);
332 new_bus_id
= kzalloc(sizeof(struct acpi_device_bus_id
), GFP_KERNEL
);
334 printk(KERN_ERR PREFIX
"Memory allocation error\n");
338 spin_lock(&acpi_device_lock
);
340 * Find suitable bus_id and instance number in acpi_bus_id_list
341 * If failed, create one and link it into acpi_bus_id_list
343 list_for_each_entry(acpi_device_bus_id
, &acpi_bus_id_list
, node
) {
344 if(!strcmp(acpi_device_bus_id
->bus_id
, device
->flags
.hardware_id
? device
->pnp
.hardware_id
: "device")) {
345 acpi_device_bus_id
->instance_no
++;
352 acpi_device_bus_id
= new_bus_id
;
353 strcpy(acpi_device_bus_id
->bus_id
, device
->flags
.hardware_id
? device
->pnp
.hardware_id
: "device");
354 acpi_device_bus_id
->instance_no
= 0;
355 list_add_tail(&acpi_device_bus_id
->node
, &acpi_bus_id_list
);
357 sprintf(device
->dev
.bus_id
, "%s:%02x", acpi_device_bus_id
->bus_id
, acpi_device_bus_id
->instance_no
);
359 if (device
->parent
) {
360 list_add_tail(&device
->node
, &device
->parent
->children
);
361 list_add_tail(&device
->g_list
, &device
->parent
->g_list
);
363 list_add_tail(&device
->g_list
, &acpi_device_list
);
364 if (device
->wakeup
.flags
.valid
)
365 list_add_tail(&device
->wakeup_list
, &acpi_wakeup_device_list
);
366 spin_unlock(&acpi_device_lock
);
369 device
->dev
.parent
= &parent
->dev
;
370 device
->dev
.bus
= &acpi_bus_type
;
371 device_initialize(&device
->dev
);
372 device
->dev
.release
= &acpi_device_release
;
373 result
= device_add(&device
->dev
);
375 printk("Error adding device %s", device
->dev
.bus_id
);
379 result
= acpi_device_setup_files(device
);
381 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error creating sysfs interface for device %s\n", device
->dev
.bus_id
));
383 device
->removal_type
= ACPI_BUS_REMOVAL_NORMAL
;
386 spin_lock(&acpi_device_lock
);
387 if (device
->parent
) {
388 list_del(&device
->node
);
389 list_del(&device
->g_list
);
391 list_del(&device
->g_list
);
392 list_del(&device
->wakeup_list
);
393 spin_unlock(&acpi_device_lock
);
397 static void acpi_device_unregister(struct acpi_device
*device
, int type
)
399 spin_lock(&acpi_device_lock
);
400 if (device
->parent
) {
401 list_del(&device
->node
);
402 list_del(&device
->g_list
);
404 list_del(&device
->g_list
);
406 list_del(&device
->wakeup_list
);
407 spin_unlock(&acpi_device_lock
);
409 acpi_detach_data(device
->handle
, acpi_bus_data_handler
);
411 acpi_device_remove_files(device
);
412 device_unregister(&device
->dev
);
415 /* --------------------------------------------------------------------------
417 -------------------------------------------------------------------------- */
419 * acpi_bus_driver_init - add a device to a driver
420 * @device: the device to add and initialize
421 * @driver: driver for the device
423 * Used to initialize a device via its device driver. Called whenever a
424 * driver is bound to a device. Invokes the driver's add() ops.
427 acpi_bus_driver_init(struct acpi_device
*device
, struct acpi_driver
*driver
)
432 if (!device
|| !driver
)
435 if (!driver
->ops
.add
)
438 result
= driver
->ops
.add(device
);
440 device
->driver
= NULL
;
441 acpi_driver_data(device
) = NULL
;
445 device
->driver
= driver
;
448 * TBD - Configuration Management: Assign resources to device based
449 * upon possible configuration and currently allocated resources.
452 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
453 "Driver successfully bound to device\n"));
457 static int acpi_start_single_object(struct acpi_device
*device
)
460 struct acpi_driver
*driver
;
463 if (!(driver
= device
->driver
))
466 if (driver
->ops
.start
) {
467 result
= driver
->ops
.start(device
);
468 if (result
&& driver
->ops
.remove
)
469 driver
->ops
.remove(device
, ACPI_BUS_REMOVAL_NORMAL
);
476 * acpi_bus_register_driver - register a driver with the ACPI bus
477 * @driver: driver being registered
479 * Registers a driver with the ACPI bus. Searches the namespace for all
480 * devices that match the driver's criteria and binds. Returns zero for
481 * success or a negative error status for failure.
483 int acpi_bus_register_driver(struct acpi_driver
*driver
)
489 driver
->drv
.name
= driver
->name
;
490 driver
->drv
.bus
= &acpi_bus_type
;
491 driver
->drv
.owner
= driver
->owner
;
493 ret
= driver_register(&driver
->drv
);
497 EXPORT_SYMBOL(acpi_bus_register_driver
);
500 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
501 * @driver: driver to unregister
503 * Unregisters a driver with the ACPI bus. Searches the namespace for all
504 * devices that match the driver's criteria and unbinds.
506 void acpi_bus_unregister_driver(struct acpi_driver
*driver
)
508 driver_unregister(&driver
->drv
);
511 EXPORT_SYMBOL(acpi_bus_unregister_driver
);
513 /* --------------------------------------------------------------------------
515 -------------------------------------------------------------------------- */
517 acpi_bus_get_ejd(acpi_handle handle
, acpi_handle
*ejd
)
521 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
522 union acpi_object
*obj
;
524 status
= acpi_get_handle(handle
, "_EJD", &tmp
);
525 if (ACPI_FAILURE(status
))
528 status
= acpi_evaluate_object(handle
, "_EJD", NULL
, &buffer
);
529 if (ACPI_SUCCESS(status
)) {
530 obj
= buffer
.pointer
;
531 status
= acpi_get_handle(NULL
, obj
->string
.pointer
, ejd
);
532 kfree(buffer
.pointer
);
536 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd
);
538 void acpi_bus_data_handler(acpi_handle handle
, u32 function
, void *context
)
546 int acpi_match_ids(struct acpi_device
*device
, char *ids
)
548 if (device
->flags
.hardware_id
)
549 if (strstr(ids
, device
->pnp
.hardware_id
))
552 if (device
->flags
.compatible_ids
) {
553 struct acpi_compatible_id_list
*cid_list
= device
->pnp
.cid_list
;
556 /* compare multiple _CID entries against driver ids */
557 for (i
= 0; i
< cid_list
->count
; i
++) {
558 if (strstr(ids
, cid_list
->id
[i
].value
))
565 static int acpi_bus_get_perf_flags(struct acpi_device
*device
)
567 device
->performance
.state
= ACPI_STATE_UNKNOWN
;
572 acpi_bus_extract_wakeup_device_power_package(struct acpi_device
*device
,
573 union acpi_object
*package
)
576 union acpi_object
*element
= NULL
;
578 if (!device
|| !package
|| (package
->package
.count
< 2))
579 return AE_BAD_PARAMETER
;
581 element
= &(package
->package
.elements
[0]);
583 return AE_BAD_PARAMETER
;
584 if (element
->type
== ACPI_TYPE_PACKAGE
) {
585 if ((element
->package
.count
< 2) ||
586 (element
->package
.elements
[0].type
!=
587 ACPI_TYPE_LOCAL_REFERENCE
)
588 || (element
->package
.elements
[1].type
!= ACPI_TYPE_INTEGER
))
590 device
->wakeup
.gpe_device
=
591 element
->package
.elements
[0].reference
.handle
;
592 device
->wakeup
.gpe_number
=
593 (u32
) element
->package
.elements
[1].integer
.value
;
594 } else if (element
->type
== ACPI_TYPE_INTEGER
) {
595 device
->wakeup
.gpe_number
= element
->integer
.value
;
599 element
= &(package
->package
.elements
[1]);
600 if (element
->type
!= ACPI_TYPE_INTEGER
) {
603 device
->wakeup
.sleep_state
= element
->integer
.value
;
605 if ((package
->package
.count
- 2) > ACPI_MAX_HANDLES
) {
608 device
->wakeup
.resources
.count
= package
->package
.count
- 2;
609 for (i
= 0; i
< device
->wakeup
.resources
.count
; i
++) {
610 element
= &(package
->package
.elements
[i
+ 2]);
611 if (element
->type
!= ACPI_TYPE_ANY
) {
615 device
->wakeup
.resources
.handles
[i
] = element
->reference
.handle
;
621 static int acpi_bus_get_wakeup_device_flags(struct acpi_device
*device
)
623 acpi_status status
= 0;
624 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
625 union acpi_object
*package
= NULL
;
629 status
= acpi_evaluate_object(device
->handle
, "_PRW", NULL
, &buffer
);
630 if (ACPI_FAILURE(status
)) {
631 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PRW"));
635 package
= (union acpi_object
*)buffer
.pointer
;
636 status
= acpi_bus_extract_wakeup_device_power_package(device
, package
);
637 if (ACPI_FAILURE(status
)) {
638 ACPI_EXCEPTION((AE_INFO
, status
, "Extracting _PRW package"));
642 kfree(buffer
.pointer
);
644 device
->wakeup
.flags
.valid
= 1;
645 /* Power button, Lid switch always enable wakeup */
646 if (!acpi_match_ids(device
, "PNP0C0D,PNP0C0C,PNP0C0E"))
647 device
->wakeup
.flags
.run_wake
= 1;
650 if (ACPI_FAILURE(status
))
651 device
->flags
.wake_capable
= 0;
655 static int acpi_bus_get_power_flags(struct acpi_device
*device
)
657 acpi_status status
= 0;
658 acpi_handle handle
= NULL
;
663 * Power Management Flags
665 status
= acpi_get_handle(device
->handle
, "_PSC", &handle
);
666 if (ACPI_SUCCESS(status
))
667 device
->power
.flags
.explicit_get
= 1;
668 status
= acpi_get_handle(device
->handle
, "_IRC", &handle
);
669 if (ACPI_SUCCESS(status
))
670 device
->power
.flags
.inrush_current
= 1;
673 * Enumerate supported power management states
675 for (i
= ACPI_STATE_D0
; i
<= ACPI_STATE_D3
; i
++) {
676 struct acpi_device_power_state
*ps
= &device
->power
.states
[i
];
677 char object_name
[5] = { '_', 'P', 'R', '0' + i
, '\0' };
679 /* Evaluate "_PRx" to se if power resources are referenced */
680 acpi_evaluate_reference(device
->handle
, object_name
, NULL
,
682 if (ps
->resources
.count
) {
683 device
->power
.flags
.power_resources
= 1;
687 /* Evaluate "_PSx" to see if we can do explicit sets */
688 object_name
[2] = 'S';
689 status
= acpi_get_handle(device
->handle
, object_name
, &handle
);
690 if (ACPI_SUCCESS(status
)) {
691 ps
->flags
.explicit_set
= 1;
695 /* State is valid if we have some power control */
696 if (ps
->resources
.count
|| ps
->flags
.explicit_set
)
699 ps
->power
= -1; /* Unknown - driver assigned */
700 ps
->latency
= -1; /* Unknown - driver assigned */
703 /* Set defaults for D0 and D3 states (always valid) */
704 device
->power
.states
[ACPI_STATE_D0
].flags
.valid
= 1;
705 device
->power
.states
[ACPI_STATE_D0
].power
= 100;
706 device
->power
.states
[ACPI_STATE_D3
].flags
.valid
= 1;
707 device
->power
.states
[ACPI_STATE_D3
].power
= 0;
709 /* TBD: System wake support and resource requirements. */
711 device
->power
.state
= ACPI_STATE_UNKNOWN
;
716 static int acpi_bus_get_flags(struct acpi_device
*device
)
718 acpi_status status
= AE_OK
;
719 acpi_handle temp
= NULL
;
722 /* Presence of _STA indicates 'dynamic_status' */
723 status
= acpi_get_handle(device
->handle
, "_STA", &temp
);
724 if (ACPI_SUCCESS(status
))
725 device
->flags
.dynamic_status
= 1;
727 /* Presence of _CID indicates 'compatible_ids' */
728 status
= acpi_get_handle(device
->handle
, "_CID", &temp
);
729 if (ACPI_SUCCESS(status
))
730 device
->flags
.compatible_ids
= 1;
732 /* Presence of _RMV indicates 'removable' */
733 status
= acpi_get_handle(device
->handle
, "_RMV", &temp
);
734 if (ACPI_SUCCESS(status
))
735 device
->flags
.removable
= 1;
737 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
738 status
= acpi_get_handle(device
->handle
, "_EJD", &temp
);
739 if (ACPI_SUCCESS(status
))
740 device
->flags
.ejectable
= 1;
742 status
= acpi_get_handle(device
->handle
, "_EJ0", &temp
);
743 if (ACPI_SUCCESS(status
))
744 device
->flags
.ejectable
= 1;
747 /* Presence of _LCK indicates 'lockable' */
748 status
= acpi_get_handle(device
->handle
, "_LCK", &temp
);
749 if (ACPI_SUCCESS(status
))
750 device
->flags
.lockable
= 1;
752 /* Presence of _PS0|_PR0 indicates 'power manageable' */
753 status
= acpi_get_handle(device
->handle
, "_PS0", &temp
);
754 if (ACPI_FAILURE(status
))
755 status
= acpi_get_handle(device
->handle
, "_PR0", &temp
);
756 if (ACPI_SUCCESS(status
))
757 device
->flags
.power_manageable
= 1;
759 /* Presence of _PRW indicates wake capable */
760 status
= acpi_get_handle(device
->handle
, "_PRW", &temp
);
761 if (ACPI_SUCCESS(status
))
762 device
->flags
.wake_capable
= 1;
764 /* TBD: Peformance management */
769 static void acpi_device_get_busid(struct acpi_device
*device
,
770 acpi_handle handle
, int type
)
772 char bus_id
[5] = { '?', 0 };
773 struct acpi_buffer buffer
= { sizeof(bus_id
), bus_id
};
779 * The device's Bus ID is simply the object name.
780 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
783 case ACPI_BUS_TYPE_SYSTEM
:
784 strcpy(device
->pnp
.bus_id
, "ACPI");
786 case ACPI_BUS_TYPE_POWER_BUTTON
:
787 strcpy(device
->pnp
.bus_id
, "PWRF");
789 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
790 strcpy(device
->pnp
.bus_id
, "SLPF");
793 acpi_get_name(handle
, ACPI_SINGLE_NAME
, &buffer
);
794 /* Clean up trailing underscores (if any) */
795 for (i
= 3; i
> 1; i
--) {
796 if (bus_id
[i
] == '_')
801 strcpy(device
->pnp
.bus_id
, bus_id
);
807 acpi_video_bus_match(struct acpi_device
*device
)
809 acpi_handle h_dummy1
;
810 acpi_handle h_dummy2
;
811 acpi_handle h_dummy3
;
817 /* Since there is no HID, CID for ACPI Video drivers, we have
818 * to check well known required nodes for each feature we support.
821 /* Does this device able to support video switching ? */
822 if (ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_DOD", &h_dummy1
)) &&
823 ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_DOS", &h_dummy2
)))
826 /* Does this device able to retrieve a video ROM ? */
827 if (ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_ROM", &h_dummy1
)))
830 /* Does this device able to configure which video head to be POSTed ? */
831 if (ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_VPO", &h_dummy1
)) &&
832 ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_GPD", &h_dummy2
)) &&
833 ACPI_SUCCESS(acpi_get_handle(device
->handle
, "_SPD", &h_dummy3
)))
840 * acpi_bay_match - see if a device is an ejectable driver bay
842 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
843 * then we can safely call it an ejectable drive bay
845 static int acpi_bay_match(struct acpi_device
*device
){
851 handle
= device
->handle
;
853 status
= acpi_get_handle(handle
, "_EJ0", &tmp
);
854 if (ACPI_FAILURE(status
))
857 if ((ACPI_SUCCESS(acpi_get_handle(handle
, "_GTF", &tmp
))) ||
858 (ACPI_SUCCESS(acpi_get_handle(handle
, "_GTM", &tmp
))) ||
859 (ACPI_SUCCESS(acpi_get_handle(handle
, "_STM", &tmp
))) ||
860 (ACPI_SUCCESS(acpi_get_handle(handle
, "_SDD", &tmp
))))
863 if (acpi_get_parent(handle
, &phandle
))
866 if ((ACPI_SUCCESS(acpi_get_handle(phandle
, "_GTF", &tmp
))) ||
867 (ACPI_SUCCESS(acpi_get_handle(phandle
, "_GTM", &tmp
))) ||
868 (ACPI_SUCCESS(acpi_get_handle(phandle
, "_STM", &tmp
))) ||
869 (ACPI_SUCCESS(acpi_get_handle(phandle
, "_SDD", &tmp
))))
875 static void acpi_device_set_id(struct acpi_device
*device
,
876 struct acpi_device
*parent
, acpi_handle handle
,
879 struct acpi_device_info
*info
;
880 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
883 struct acpi_compatible_id_list
*cid_list
= NULL
;
887 case ACPI_BUS_TYPE_DEVICE
:
888 status
= acpi_get_object_info(handle
, &buffer
);
889 if (ACPI_FAILURE(status
)) {
890 printk("%s: Error reading device info\n", __FUNCTION__
);
894 info
= buffer
.pointer
;
895 if (info
->valid
& ACPI_VALID_HID
)
896 hid
= info
->hardware_id
.value
;
897 if (info
->valid
& ACPI_VALID_UID
)
898 uid
= info
->unique_id
.value
;
899 if (info
->valid
& ACPI_VALID_CID
)
900 cid_list
= &info
->compatibility_id
;
901 if (info
->valid
& ACPI_VALID_ADR
) {
902 device
->pnp
.bus_address
= info
->address
;
903 device
->flags
.bus_address
= 1;
906 if(!(info
->valid
& (ACPI_VALID_HID
| ACPI_VALID_CID
))){
907 status
= acpi_video_bus_match(device
);
908 if(ACPI_SUCCESS(status
))
909 hid
= ACPI_VIDEO_HID
;
911 status
= acpi_bay_match(device
);
912 if (ACPI_SUCCESS(status
))
916 case ACPI_BUS_TYPE_POWER
:
917 hid
= ACPI_POWER_HID
;
919 case ACPI_BUS_TYPE_PROCESSOR
:
920 hid
= ACPI_PROCESSOR_HID
;
922 case ACPI_BUS_TYPE_SYSTEM
:
923 hid
= ACPI_SYSTEM_HID
;
925 case ACPI_BUS_TYPE_THERMAL
:
926 hid
= ACPI_THERMAL_HID
;
928 case ACPI_BUS_TYPE_POWER_BUTTON
:
929 hid
= ACPI_BUTTON_HID_POWERF
;
931 case ACPI_BUS_TYPE_SLEEP_BUTTON
:
932 hid
= ACPI_BUTTON_HID_SLEEPF
;
939 * Fix for the system root bus device -- the only root-level device.
941 if (((acpi_handle
)parent
== ACPI_ROOT_OBJECT
) && (type
== ACPI_BUS_TYPE_DEVICE
)) {
943 strcpy(device
->pnp
.device_name
, ACPI_BUS_DEVICE_NAME
);
944 strcpy(device
->pnp
.device_class
, ACPI_BUS_CLASS
);
948 strcpy(device
->pnp
.hardware_id
, hid
);
949 device
->flags
.hardware_id
= 1;
952 strcpy(device
->pnp
.unique_id
, uid
);
953 device
->flags
.unique_id
= 1;
956 device
->pnp
.cid_list
= kmalloc(cid_list
->size
, GFP_KERNEL
);
957 if (device
->pnp
.cid_list
)
958 memcpy(device
->pnp
.cid_list
, cid_list
, cid_list
->size
);
960 printk(KERN_ERR
"Memory allocation error\n");
963 kfree(buffer
.pointer
);
966 static int acpi_device_set_context(struct acpi_device
*device
, int type
)
968 acpi_status status
= AE_OK
;
973 * Attach this 'struct acpi_device' to the ACPI object. This makes
974 * resolutions from handle->device very efficient. Note that we need
975 * to be careful with fixed-feature devices as they all attach to the
978 if (type
!= ACPI_BUS_TYPE_POWER_BUTTON
&&
979 type
!= ACPI_BUS_TYPE_SLEEP_BUTTON
) {
980 status
= acpi_attach_data(device
->handle
,
981 acpi_bus_data_handler
, device
);
983 if (ACPI_FAILURE(status
)) {
984 printk("Error attaching device data\n");
991 static int acpi_bus_remove(struct acpi_device
*dev
, int rmdevice
)
996 dev
->removal_type
= ACPI_BUS_REMOVAL_EJECT
;
997 device_release_driver(&dev
->dev
);
1003 * unbind _ADR-Based Devices when hot removal
1005 if (dev
->flags
.bus_address
) {
1006 if ((dev
->parent
) && (dev
->parent
->ops
.unbind
))
1007 dev
->parent
->ops
.unbind(dev
);
1009 acpi_device_unregister(dev
, ACPI_BUS_REMOVAL_EJECT
);
1015 acpi_add_single_object(struct acpi_device
**child
,
1016 struct acpi_device
*parent
, acpi_handle handle
, int type
,
1017 struct acpi_bus_ops
*ops
)
1020 struct acpi_device
*device
= NULL
;
1026 device
= kzalloc(sizeof(struct acpi_device
), GFP_KERNEL
);
1028 printk(KERN_ERR PREFIX
"Memory allocation error\n");
1032 device
->handle
= handle
;
1033 device
->parent
= parent
;
1034 device
->bus_ops
= *ops
; /* workround for not call .start */
1037 acpi_device_get_busid(device
, handle
, type
);
1042 * Get prior to calling acpi_bus_get_status() so we know whether
1043 * or not _STA is present. Note that we only look for object
1044 * handles -- cannot evaluate objects until we know the device is
1045 * present and properly initialized.
1047 result
= acpi_bus_get_flags(device
);
1054 * See if the device is present. We always assume that non-Device
1055 * and non-Processor objects (e.g. thermal zones, power resources,
1056 * etc.) are present, functioning, etc. (at least when parent object
1057 * is present). Note that _STA has a different meaning for some
1058 * objects (e.g. power resources) so we need to be careful how we use
1062 case ACPI_BUS_TYPE_PROCESSOR
:
1063 case ACPI_BUS_TYPE_DEVICE
:
1064 result
= acpi_bus_get_status(device
);
1065 if (ACPI_FAILURE(result
) || !device
->status
.present
) {
1071 STRUCT_TO_INT(device
->status
) =
1072 ACPI_STA_DEVICE_PRESENT
| ACPI_STA_DEVICE_ENABLED
|
1073 ACPI_STA_DEVICE_UI
| ACPI_STA_DEVICE_FUNCTIONING
;
1080 * TBD: Synch with Core's enumeration/initialization process.
1084 * Hardware ID, Unique ID, & Bus Address
1085 * -------------------------------------
1087 acpi_device_set_id(device
, parent
, handle
, type
);
1093 if (device
->flags
.power_manageable
) {
1094 result
= acpi_bus_get_power_flags(device
);
1100 * Wakeup device management
1101 *-----------------------
1103 if (device
->flags
.wake_capable
) {
1104 result
= acpi_bus_get_wakeup_device_flags(device
);
1110 * Performance Management
1111 * ----------------------
1113 if (device
->flags
.performance_manageable
) {
1114 result
= acpi_bus_get_perf_flags(device
);
1119 if ((result
= acpi_device_set_context(device
, type
)))
1122 result
= acpi_device_register(device
, parent
);
1125 * Bind _ADR-Based Devices when hot add
1127 if (device
->flags
.bus_address
) {
1128 if (device
->parent
&& device
->parent
->ops
.bind
)
1129 device
->parent
->ops
.bind(device
);
1136 kfree(device
->pnp
.cid_list
);
1143 static int acpi_bus_scan(struct acpi_device
*start
, struct acpi_bus_ops
*ops
)
1145 acpi_status status
= AE_OK
;
1146 struct acpi_device
*parent
= NULL
;
1147 struct acpi_device
*child
= NULL
;
1148 acpi_handle phandle
= NULL
;
1149 acpi_handle chandle
= NULL
;
1150 acpi_object_type type
= 0;
1158 phandle
= start
->handle
;
1161 * Parse through the ACPI namespace, identify all 'devices', and
1162 * create a new 'struct acpi_device' for each.
1164 while ((level
> 0) && parent
) {
1166 status
= acpi_get_next_object(ACPI_TYPE_ANY
, phandle
,
1170 * If this scope is exhausted then move our way back up.
1172 if (ACPI_FAILURE(status
)) {
1175 acpi_get_parent(phandle
, &phandle
);
1177 parent
= parent
->parent
;
1181 status
= acpi_get_type(chandle
, &type
);
1182 if (ACPI_FAILURE(status
))
1186 * If this is a scope object then parse it (depth-first).
1188 if (type
== ACPI_TYPE_LOCAL_SCOPE
) {
1196 * We're only interested in objects that we consider 'devices'.
1199 case ACPI_TYPE_DEVICE
:
1200 type
= ACPI_BUS_TYPE_DEVICE
;
1202 case ACPI_TYPE_PROCESSOR
:
1203 type
= ACPI_BUS_TYPE_PROCESSOR
;
1205 case ACPI_TYPE_THERMAL
:
1206 type
= ACPI_BUS_TYPE_THERMAL
;
1208 case ACPI_TYPE_POWER
:
1209 type
= ACPI_BUS_TYPE_POWER
;
1215 if (ops
->acpi_op_add
)
1216 status
= acpi_add_single_object(&child
, parent
,
1217 chandle
, type
, ops
);
1219 status
= acpi_bus_get_device(chandle
, &child
);
1221 if (ACPI_FAILURE(status
))
1224 if (ops
->acpi_op_start
&& !(ops
->acpi_op_add
)) {
1225 status
= acpi_start_single_object(child
);
1226 if (ACPI_FAILURE(status
))
1231 * If the device is present, enabled, and functioning then
1232 * parse its scope (depth-first). Note that we need to
1233 * represent absent devices to facilitate PnP notifications
1234 * -- but only the subtree head (not all of its children,
1235 * which will be enumerated when the parent is inserted).
1237 * TBD: Need notifications and other detection mechanisms
1238 * in place before we can fully implement this.
1240 if (child
->status
.present
) {
1241 status
= acpi_get_next_object(ACPI_TYPE_ANY
, chandle
,
1243 if (ACPI_SUCCESS(status
)) {
1256 acpi_bus_add(struct acpi_device
**child
,
1257 struct acpi_device
*parent
, acpi_handle handle
, int type
)
1260 struct acpi_bus_ops ops
;
1262 memset(&ops
, 0, sizeof(ops
));
1263 ops
.acpi_op_add
= 1;
1265 result
= acpi_add_single_object(child
, parent
, handle
, type
, &ops
);
1267 result
= acpi_bus_scan(*child
, &ops
);
1272 EXPORT_SYMBOL(acpi_bus_add
);
1274 int acpi_bus_start(struct acpi_device
*device
)
1277 struct acpi_bus_ops ops
;
1283 result
= acpi_start_single_object(device
);
1285 memset(&ops
, 0, sizeof(ops
));
1286 ops
.acpi_op_start
= 1;
1287 result
= acpi_bus_scan(device
, &ops
);
1292 EXPORT_SYMBOL(acpi_bus_start
);
1294 int acpi_bus_trim(struct acpi_device
*start
, int rmdevice
)
1297 struct acpi_device
*parent
, *child
;
1298 acpi_handle phandle
, chandle
;
1299 acpi_object_type type
;
1304 phandle
= start
->handle
;
1305 child
= chandle
= NULL
;
1307 while ((level
> 0) && parent
&& (!err
)) {
1308 status
= acpi_get_next_object(ACPI_TYPE_ANY
, phandle
,
1312 * If this scope is exhausted then move our way back up.
1314 if (ACPI_FAILURE(status
)) {
1317 acpi_get_parent(phandle
, &phandle
);
1319 parent
= parent
->parent
;
1322 err
= acpi_bus_remove(child
, rmdevice
);
1324 err
= acpi_bus_remove(child
, 1);
1329 status
= acpi_get_type(chandle
, &type
);
1330 if (ACPI_FAILURE(status
)) {
1334 * If there is a device corresponding to chandle then
1335 * parse it (depth-first).
1337 if (acpi_bus_get_device(chandle
, &child
) == 0) {
1347 EXPORT_SYMBOL_GPL(acpi_bus_trim
);
1350 static int acpi_bus_scan_fixed(struct acpi_device
*root
)
1353 struct acpi_device
*device
= NULL
;
1354 struct acpi_bus_ops ops
;
1359 memset(&ops
, 0, sizeof(ops
));
1360 ops
.acpi_op_add
= 1;
1361 ops
.acpi_op_start
= 1;
1364 * Enumerate all fixed-feature devices.
1366 if ((acpi_gbl_FADT
.flags
& ACPI_FADT_POWER_BUTTON
) == 0) {
1367 result
= acpi_add_single_object(&device
, acpi_root
,
1369 ACPI_BUS_TYPE_POWER_BUTTON
,
1373 if ((acpi_gbl_FADT
.flags
& ACPI_FADT_SLEEP_BUTTON
) == 0) {
1374 result
= acpi_add_single_object(&device
, acpi_root
,
1376 ACPI_BUS_TYPE_SLEEP_BUTTON
,
1383 static int __init
acpi_scan_init(void)
1386 struct acpi_bus_ops ops
;
1392 memset(&ops
, 0, sizeof(ops
));
1393 ops
.acpi_op_add
= 1;
1394 ops
.acpi_op_start
= 1;
1396 result
= bus_register(&acpi_bus_type
);
1398 /* We don't want to quit even if we failed to add suspend/resume */
1399 printk(KERN_ERR PREFIX
"Could not register bus type\n");
1403 * Create the root device in the bus's device tree
1405 result
= acpi_add_single_object(&acpi_root
, NULL
, ACPI_ROOT_OBJECT
,
1406 ACPI_BUS_TYPE_SYSTEM
, &ops
);
1411 * Enumerate devices in the ACPI namespace.
1413 result
= acpi_bus_scan_fixed(acpi_root
);
1415 result
= acpi_bus_scan(acpi_root
, &ops
);
1418 acpi_device_unregister(acpi_root
, ACPI_BUS_REMOVAL_NORMAL
);
1424 subsys_initcall(acpi_scan_init
);