5 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
10 #include <linux/signal.h>
19 #define _COMPONENT ACPI_BUS_COMPONENT
21 #define STRUCT_TO_INT(s) (*((int*)&s))
24 #define ACPI_BUS_CLASS "system_bus"
25 #define ACPI_BUS_HID "LNXSYBUS"
26 #define ACPI_BUS_DEVICE_NAME "System Bus"
28 #define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
30 static const char *dummy_hid =
"device";
48 static int create_modalias(
struct acpi_device *acpi_dev,
char *modalias,
53 struct acpi_hardware_id *
id;
55 if (list_empty(&acpi_dev->pnp.ids))
58 len =
snprintf(modalias, size,
"acpi:");
62 count =
snprintf(&modalias[len], size,
"%s:", id->id);
63 if (count < 0 || count >= size)
75 struct acpi_device *acpi_dev = to_acpi_device(dev);
79 len = create_modalias(acpi_dev, buf, 1024);
97 struct acpi_eject_event *ej_event = (
struct acpi_eject_event *) context;
98 struct acpi_device *
device;
103 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
112 "Hot-removing device %s...\n", dev_name(&device->dev)));
116 "Removing device failed\n");
124 "Power-off device failed\n");
126 if (device->flags.lockable) {
146 "Eject device failed\n");
156 ej_event->event, ost_code,
NULL);
163 const char *buf,
size_t count)
168 struct acpi_device *acpi_device = to_acpi_device(d);
169 struct acpi_eject_event *ej_event;
171 if ((!count) || (buf[0] !=
'1')) {
175 if (acpi_device->driver ==
NULL) {
181 if (
ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
192 ej_event->handle = acpi_device->handle;
193 if (acpi_device->flags.eject_pending) {
196 acpi_device->flags.eject_pending = 0;
199 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
201 ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS,
NULL);
213 struct acpi_device *acpi_dev = to_acpi_device(dev);
221 struct acpi_device *acpi_dev = to_acpi_device(dev);
240 struct acpi_device *acpi_dev = to_acpi_device(dev);
243 if (acpi_dev->pnp.str_obj ==
NULL)
251 (
wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
252 acpi_dev->pnp.str_obj->buffer.length,
256 buf[result++] =
'\n';
262 static int acpi_device_setup_files(
struct acpi_device *dev)
278 if (!list_empty(&dev->pnp.ids)) {
297 dev->pnp.str_obj = buffer.
pointer;
314 static void acpi_device_remove_files(
struct acpi_device *dev)
324 kfree(dev->pnp.str_obj);
347 struct acpi_hardware_id *hwid;
353 if (!device->status.present)
356 for (
id = ids;
id->
id[0];
id++)
358 if (!
strcmp((
char *) id->
id, hwid->id))
365 static void acpi_free_ids(
struct acpi_device *
device)
367 struct acpi_hardware_id *
id, *
tmp;
375 static void acpi_device_release(
struct device *dev)
377 struct acpi_device *acpi_dev = to_acpi_device(dev);
379 acpi_free_ids(acpi_dev);
385 struct acpi_device *acpi_dev = to_acpi_device(dev);
386 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
393 struct acpi_device *acpi_dev = to_acpi_device(dev);
396 if (list_empty(&acpi_dev->pnp.ids))
401 len = create_modalias(acpi_dev, &env->
buf[env->
buflen - 1],
403 if (len >= (
sizeof(env->
buf) - env->
buflen))
413 device->driver->ops.notify(device, event);
416 static acpi_status acpi_device_notify_fixed(
void *data)
418 struct acpi_device *device =
data;
425 static int acpi_device_install_notify_handler(
struct acpi_device *device)
429 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
432 acpi_device_notify_fixed,
434 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
437 acpi_device_notify_fixed,
450 static void acpi_device_remove_notify_handler(
struct acpi_device *device)
452 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
454 acpi_device_notify_fixed);
455 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
457 acpi_device_notify_fixed);
463 static int acpi_bus_driver_init(
struct acpi_device *,
struct acpi_driver *);
464 static int acpi_start_single_object(
struct acpi_device *);
465 static int acpi_device_probe(
struct device * dev)
467 struct acpi_device *acpi_dev = to_acpi_device(dev);
468 struct acpi_driver *acpi_drv = to_acpi_driver(dev->
driver);
471 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
473 if (acpi_dev->bus_ops.acpi_op_start)
474 acpi_start_single_object(acpi_dev);
476 if (acpi_drv->ops.notify) {
477 ret = acpi_device_install_notify_handler(acpi_dev);
479 if (acpi_drv->ops.remove)
480 acpi_drv->ops.remove(acpi_dev,
481 acpi_dev->removal_type);
487 "Found driver [%s] for device [%s]\n",
488 acpi_drv->name, acpi_dev->pnp.bus_id));
494 static int acpi_device_remove(
struct device * dev)
496 struct acpi_device *acpi_dev = to_acpi_device(dev);
497 struct acpi_driver *acpi_drv = acpi_dev->driver;
500 if (acpi_drv->ops.notify)
501 acpi_device_remove_notify_handler(acpi_dev);
502 if (acpi_drv->ops.remove)
503 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
505 acpi_dev->driver =
NULL;
506 acpi_dev->driver_data =
NULL;
514 .match = acpi_bus_match,
515 .probe = acpi_device_probe,
516 .remove = acpi_device_remove,
517 .uevent = acpi_device_uevent,
520 static int acpi_device_register(
struct acpi_device *device)
531 INIT_LIST_HEAD(&device->children);
532 INIT_LIST_HEAD(&device->node);
533 INIT_LIST_HEAD(&device->wakeup_list);
534 INIT_LIST_HEAD(&device->physical_node_list);
537 new_bus_id = kzalloc(
sizeof(
struct acpi_device_bus_id),
GFP_KERNEL);
558 acpi_device_bus_id = new_bus_id;
568 if (device->wakeup.flags.valid)
569 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
573 device->dev.parent = &device->parent->dev;
575 device->dev.release = &acpi_device_release;
578 dev_err(&device->dev,
"Error registering device\n");
582 result = acpi_device_setup_files(device);
585 dev_name(&device->dev));
587 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
598 static void acpi_device_unregister(
struct acpi_device *device,
int type)
609 acpi_device_remove_files(device);
625 acpi_bus_driver_init(
struct acpi_device *device,
struct acpi_driver *
driver)
629 if (!device || !driver)
632 if (!driver->ops.add)
635 result = driver->ops.add(device);
637 device->driver =
NULL;
638 device->driver_data =
NULL;
650 "Driver successfully bound to device\n"));
654 static int acpi_start_single_object(
struct acpi_device *device)
657 struct acpi_driver *
driver;
660 if (!(driver = device->driver))
663 if (driver->ops.start) {
664 result = driver->ops.start(device);
665 if (result && driver->ops.remove)
666 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
686 driver->drv.name = driver->name;
688 driver->drv.owner = driver->owner;
713 static struct acpi_device *acpi_bus_get_parent(
acpi_handle handle)
717 struct acpi_device *device;
771 static int acpi_bus_get_perf_flags(
struct acpi_device *device)
778 acpi_bus_extract_wakeup_device_power_package(
acpi_handle handle,
779 struct acpi_device_wakeup *wakeup)
797 package = (union acpi_object *)buffer.pointer;
799 if (!package || (package->package.count < 2)) {
804 element = &(package->package.elements[0]);
810 if ((
element->package.count < 2) ||
818 element->package.elements[0].reference.handle;
820 (
u32)
element->package.elements[1].integer.value;
822 wakeup->gpe_device =
NULL;
823 wakeup->gpe_number =
element->integer.value;
829 element = &(package->package.elements[1]);
834 wakeup->sleep_state =
element->integer.value;
840 wakeup->resources.count = package->package.count - 2;
841 for (
i = 0;
i < wakeup->resources.count;
i++) {
842 element = &(package->package.elements[
i + 2]);
848 wakeup->resources.handles[
i] =
element->reference.handle;
859 static void acpi_bus_set_run_wake_flags(
struct acpi_device *device)
870 device->wakeup.flags.notifier_present = 0;
874 device->wakeup.flags.run_wake = 1;
880 device->wakeup.gpe_number,
883 device->wakeup.flags.run_wake =
887 static void acpi_bus_get_wakeup_device_flags(
struct acpi_device *device)
898 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
905 device->wakeup.flags.valid = 1;
906 device->wakeup.prepare_count = 0;
907 acpi_bus_set_run_wake_flags(device);
917 "error in _DSW or _PSW evaluation\n"));
920 static void acpi_bus_add_power_resource(
acpi_handle handle);
922 static int acpi_bus_get_power_flags(
struct acpi_device *device)
934 device->power.flags.explicit_get = 1;
937 device->power.flags.inrush_current = 1;
943 struct acpi_device_power_state *
ps = &device->power.states[
i];
944 char object_name[5] = {
'_',
'P',
'R',
'0' +
i,
'\0' };
949 if (ps->resources.count) {
952 device->power.flags.power_resources = 1;
953 for (j = 0; j < ps->resources.count; j++)
954 acpi_bus_add_power_resource(ps->resources.handles[j]);
958 object_name[2] =
'S';
961 ps->flags.explicit_set = 1;
967 if (ps->resources.count ||
990 static int acpi_bus_get_flags(
struct acpi_device *device)
999 device->flags.dynamic_status = 1;
1004 device->flags.removable = 1;
1009 device->flags.ejectable = 1;
1013 device->flags.ejectable = 1;
1019 device->flags.lockable = 1;
1022 if (device->device_type == ACPI_BUS_TYPE_POWER)
1030 device->flags.power_manageable = 1;
1037 static void acpi_device_get_busid(
struct acpi_device *device)
1039 char bus_id[5] = {
'?', 0 };
1040 struct acpi_buffer buffer = {
sizeof(bus_id), bus_id };
1050 strcpy(device->pnp.bus_id,
"ACPI");
1054 switch (device->device_type) {
1055 case ACPI_BUS_TYPE_POWER_BUTTON:
1056 strcpy(device->pnp.bus_id,
"PWRF");
1058 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1059 strcpy(device->pnp.bus_id,
"SLPF");
1064 for (i = 3; i > 1; i--) {
1065 if (bus_id[i] ==
'_')
1070 strcpy(device->pnp.bus_id, bus_id);
1081 static int acpi_bay_match(
struct acpi_device *device){
1087 handle = device->handle;
1114 static int acpi_dock_match(
struct acpi_device *device)
1122 struct acpi_hardware_id *hid;
1124 if (list_empty(&device->pnp.ids))
1132 static void acpi_add_id(
struct acpi_device *device,
const char *
dev_id)
1134 struct acpi_hardware_id *
id;
1154 static int acpi_ibm_smbus_match(
struct acpi_device *device)
1184 static void acpi_device_set_id(
struct acpi_device *device)
1191 switch (device->device_type) {
1192 case ACPI_BUS_TYPE_DEVICE:
1200 printk(
KERN_ERR PREFIX
"%s: Error reading device info\n", __func__);
1208 for (i = 0; i < cid_list->
count; i++)
1209 acpi_add_id(device, cid_list->
ids[i].string);
1212 device->pnp.bus_address = info->
address;
1213 device->flags.bus_address = 1;
1228 else if (!acpi_ibm_smbus_match(device))
1238 case ACPI_BUS_TYPE_POWER:
1241 case ACPI_BUS_TYPE_PROCESSOR:
1244 case ACPI_BUS_TYPE_THERMAL:
1247 case ACPI_BUS_TYPE_POWER_BUTTON:
1250 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1256 static int acpi_device_set_context(
struct acpi_device *device)
1267 if (!device->handle)
1279 static int acpi_bus_remove(
struct acpi_device *dev,
int rmdevice)
1284 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1293 if (dev->flags.bus_address) {
1294 if ((dev->parent) && (dev->parent->ops.unbind))
1295 dev->parent->ops.unbind(dev);
1297 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1302 static int acpi_add_single_object(
struct acpi_device **
child,
1304 unsigned long long sta,
1305 struct acpi_bus_ops *ops)
1308 struct acpi_device *device;
1311 device = kzalloc(
sizeof(
struct acpi_device),
GFP_KERNEL);
1317 INIT_LIST_HEAD(&device->pnp.ids);
1318 device->device_type =
type;
1320 device->parent = acpi_bus_get_parent(handle);
1321 device->bus_ops = *ops;
1324 acpi_device_get_busid(device);
1332 result = acpi_bus_get_flags(device);
1341 acpi_device_set_id(device);
1347 if (device->flags.power_manageable) {
1348 result = acpi_bus_get_power_flags(device);
1357 acpi_bus_get_wakeup_device_flags(device);
1363 if (device->flags.performance_manageable) {
1364 result = acpi_bus_get_perf_flags(device);
1369 if ((result = acpi_device_set_context(device)))
1372 result = acpi_device_register(device);
1377 if (device->flags.bus_address) {
1378 if (device->parent && device->parent->ops.bind)
1379 device->parent->ops.bind(device);
1386 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1388 device->parent ? dev_name(&device->parent->dev) :
1393 acpi_device_release(&device->dev);
1398 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1399 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1401 static void acpi_bus_add_power_resource(
acpi_handle handle)
1403 struct acpi_bus_ops ops = {
1407 struct acpi_device *device =
NULL;
1411 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1415 static int acpi_bus_type_and_status(
acpi_handle handle,
int *type,
1416 unsigned long long *sta)
1425 switch (acpi_type) {
1428 *type = ACPI_BUS_TYPE_DEVICE;
1434 *type = ACPI_BUS_TYPE_PROCESSOR;
1440 *type = ACPI_BUS_TYPE_THERMAL;
1444 *type = ACPI_BUS_TYPE_POWER;
1457 struct acpi_bus_ops *ops =
context;
1459 unsigned long long sta;
1460 struct acpi_device *device;
1464 result = acpi_bus_type_and_status(handle, &type, &sta);
1470 struct acpi_device_wakeup wakeup;
1475 acpi_bus_extract_wakeup_device_power_package(handle,
1486 if (ops->acpi_op_add && !device)
1487 acpi_add_single_object(&device, handle, type, sta, ops);
1492 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1493 status = acpi_start_single_object(device);
1499 *return_value = device;
1503 static int acpi_bus_scan(
acpi_handle handle,
struct acpi_bus_ops *ops,
1504 struct acpi_device **child)
1507 void *device =
NULL;
1509 status = acpi_bus_check_add(handle, 0, ops, &device);
1512 acpi_bus_check_add,
NULL, ops, &device);
1537 struct acpi_device *parent,
acpi_handle handle,
int type)
1539 struct acpi_bus_ops ops;
1541 memset(&ops, 0,
sizeof(ops));
1542 ops.acpi_op_add = 1;
1544 return acpi_bus_scan(handle, &ops, child);
1550 struct acpi_bus_ops ops;
1556 memset(&ops, 0,
sizeof(ops));
1557 ops.acpi_op_start = 1;
1559 result = acpi_bus_scan(device->handle, &ops,
NULL);
1570 struct acpi_device *
parent, *child;
1577 phandle = start->handle;
1578 child = chandle =
NULL;
1580 while ((level > 0) && parent && (!err)) {
1592 parent = parent->parent;
1595 err = acpi_bus_remove(child, rmdevice);
1597 err = acpi_bus_remove(child, 1);
1622 static int acpi_bus_scan_fixed(
void)
1625 struct acpi_device *device =
NULL;
1626 struct acpi_bus_ops ops;
1628 memset(&ops, 0,
sizeof(ops));
1629 ops.acpi_op_add = 1;
1630 ops.acpi_op_start = 1;
1636 result = acpi_add_single_object(&device,
NULL,
1637 ACPI_BUS_TYPE_POWER_BUTTON,
1644 result = acpi_add_single_object(&device,
NULL,
1645 ACPI_BUS_TYPE_SLEEP_BUTTON,
1656 struct acpi_bus_ops ops;
1658 memset(&ops, 0,
sizeof(ops));
1659 ops.acpi_op_add = 1;
1660 ops.acpi_op_start = 1;
1676 result = acpi_bus_scan_fixed();
1679 acpi_device_unregister(
acpi_root, ACPI_BUS_REMOVAL_NORMAL);