Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Randy Dunlap | 9b6d97b | 2006-07-12 02:08:00 -0400 | [diff] [blame] | 7 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #define _COMPONENT ACPI_BUS_COMPONENT |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 14 | ACPI_MODULE_NAME("scan") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #define STRUCT_TO_INT(s) (*((int*)&s)) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 16 | extern struct acpi_device *acpi_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 27 | static int acpi_eject_operation(acpi_handle handle, int lockable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 56 | return (-ENODEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 59 | return (0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static ssize_t |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 63 | acpi_eject_store(struct device *d, struct device_attribute *attr, |
| 64 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 66 | int result; |
| 67 | int ret = count; |
| 68 | int islockable; |
| 69 | acpi_status status; |
| 70 | acpi_handle handle; |
| 71 | acpi_object_type type = 0; |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 72 | struct acpi_device *acpi_device = to_acpi_device(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | if ((!count) || (buf[0] != '1')) { |
| 75 | return -EINVAL; |
| 76 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #ifndef FORCE_EJECT |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 78 | if (acpi_device->driver == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | ret = -ENODEV; |
| 80 | goto err; |
| 81 | } |
| 82 | #endif |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 83 | status = acpi_get_type(acpi_device->handle, &type); |
| 84 | if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | ret = -ENODEV; |
| 86 | goto err; |
| 87 | } |
| 88 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 89 | islockable = acpi_device->flags.lockable; |
| 90 | handle = acpi_device->handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 92 | result = acpi_bus_trim(acpi_device, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | if (!result) |
| 95 | result = acpi_eject_operation(handle, islockable); |
| 96 | |
| 97 | if (result) { |
| 98 | ret = -EBUSY; |
| 99 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 100 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return ret; |
| 102 | } |
| 103 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | /* -------------------------------------------------------------------------- |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 134 | ACPI Bus operations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | -------------------------------------------------------------------------- */ |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 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 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 144 | static int acpi_device_suspend(struct device *dev, pm_message_t state) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 145 | { |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 146 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 147 | struct acpi_driver *acpi_drv = acpi_dev->driver; |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 148 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 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 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 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; |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 196 | } |
| 197 | } |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 198 | |
| 199 | envp[i] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 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) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 206 | { |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 207 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 208 | struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver); |
| 209 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 211 | ret = acpi_bus_driver_init(acpi_dev, acpi_drv); |
| 212 | if (!ret) { |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 213 | if (acpi_dev->bus_ops.acpi_op_start) |
| 214 | acpi_start_single_object(acpi_dev); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 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); |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 219 | } |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 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) |
Li Shaohua | 9633357 | 2006-12-07 20:56:46 +0800 | [diff] [blame] | 230 | acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 231 | if (acpi_drv->ops.remove) |
Li Shaohua | 9633357 | 2006-12-07 20:56:46 +0800 | [diff] [blame] | 232 | acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 233 | } |
| 234 | acpi_dev->driver = NULL; |
| 235 | acpi_driver_data(dev) = NULL; |
| 236 | |
| 237 | put_device(dev); |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 241 | static void acpi_device_shutdown(struct device *dev) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 242 | { |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 243 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 244 | struct acpi_driver *acpi_drv = acpi_dev->driver; |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 245 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 246 | if (acpi_drv && acpi_drv->ops.shutdown) |
| 247 | acpi_drv->ops.shutdown(acpi_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 249 | return ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 252 | static struct bus_type acpi_bus_type = { |
| 253 | .name = "acpi", |
| 254 | .suspend = acpi_device_suspend, |
| 255 | .resume = acpi_device_resume, |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 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, |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 261 | }; |
| 262 | |
| 263 | static void acpi_device_register(struct acpi_device *device, |
| 264 | struct acpi_device *parent) |
| 265 | { |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 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 | |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 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); |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 293 | |
| 294 | acpi_device_setup_files(device); |
Li Shaohua | 9633357 | 2006-12-07 20:56:46 +0800 | [diff] [blame] | 295 | device->removal_type = ACPI_BUS_REMOVAL_NORMAL; |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 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); |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 308 | spin_unlock(&acpi_device_lock); |
| 309 | |
| 310 | acpi_detach_data(device->handle, acpi_bus_data_handler); |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 311 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 312 | acpi_device_remove_files(device); |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 313 | device_unregister(&device->dev); |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /* -------------------------------------------------------------------------- |
| 317 | Driver Management |
| 318 | -------------------------------------------------------------------------- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | * Used to initialize a device via its device driver. Called whenever a |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 325 | * driver is bound to a device. Invokes the driver's add() ops. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | */ |
| 327 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 328 | acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 330 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | if (!device || !driver) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 334 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | if (!driver->ops.add) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 337 | return -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | result = driver->ops.add(device); |
| 340 | if (result) { |
| 341 | device->driver = NULL; |
| 342 | acpi_driver_data(device) = NULL; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 343 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 353 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 354 | "Driver successfully bound to device\n")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 355 | return 0; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Adrian Bunk | 8713cbe | 2005-09-02 17:16:48 -0400 | [diff] [blame] | 358 | static int acpi_start_single_object(struct acpi_device *device) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 359 | { |
| 360 | int result = 0; |
| 361 | struct acpi_driver *driver; |
| 362 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 363 | |
| 364 | if (!(driver = device->driver)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 365 | return 0; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 373 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 377 | * acpi_bus_register_driver - register a driver with the ACPI bus |
| 378 | * @driver: driver being registered |
| 379 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | * Registers a driver with the ACPI bus. Searches the namespace for all |
Bjorn Helgaas | 9d9f749 | 2006-03-28 17:04:00 -0500 | [diff] [blame] | 381 | * devices that match the driver's criteria and binds. Returns zero for |
| 382 | * success or a negative error status for failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 384 | int acpi_bus_register_driver(struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 386 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
| 388 | if (acpi_disabled) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 389 | return -ENODEV; |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 390 | driver->drv.name = driver->name; |
| 391 | driver->drv.bus = &acpi_bus_type; |
| 392 | driver->drv.owner = driver->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 394 | ret = driver_register(&driver->drv); |
| 395 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 398 | EXPORT_SYMBOL(acpi_bus_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
| 400 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 401 | * acpi_bus_unregister_driver - unregisters a driver with the APIC bus |
| 402 | * @driver: driver to unregister |
| 403 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | * Unregisters a driver with the ACPI bus. Searches the namespace for all |
| 405 | * devices that match the driver's criteria and unbinds. |
| 406 | */ |
Bjorn Helgaas | 06ea8e08 | 2006-04-27 05:25:00 -0400 | [diff] [blame] | 407 | void acpi_bus_unregister_driver(struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | { |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 409 | driver_unregister(&driver->drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | EXPORT_SYMBOL(acpi_bus_unregister_driver); |
| 413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | /* -------------------------------------------------------------------------- |
| 415 | Device Enumeration |
| 416 | -------------------------------------------------------------------------- */ |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 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 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 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 | } |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 616 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 617 | static int acpi_bus_get_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 619 | acpi_status status = AE_OK; |
| 620 | acpi_handle temp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 667 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 670 | static void acpi_device_get_busid(struct acpi_device *device, |
| 671 | acpi_handle handle, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 673 | char bus_id[5] = { '?', 0 }; |
| 674 | struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; |
| 675 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Zhang Rui | ae84333 | 2006-12-07 20:57:10 +0800 | [diff] [blame^] | 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 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 754 | static void acpi_device_set_id(struct acpi_device *device, |
| 755 | struct acpi_device *parent, acpi_handle handle, |
| 756 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 758 | struct acpi_device_info *info; |
| 759 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 760 | char *hid = NULL; |
| 761 | char *uid = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | struct acpi_compatible_id_list *cid_list = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 763 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | |
| 765 | switch (type) { |
| 766 | case ACPI_BUS_TYPE_DEVICE: |
| 767 | status = acpi_get_object_info(handle, &buffer); |
| 768 | if (ACPI_FAILURE(status)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 769 | printk("%s: Error reading device info\n", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Zhang Rui | ae84333 | 2006-12-07 20:57:10 +0800 | [diff] [blame^] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Bob Moore | c51a4de | 2005-11-17 13:07:00 -0500 | [diff] [blame] | 820 | if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Len Brown | 02438d8 | 2006-06-30 03:19:10 -0400 | [diff] [blame] | 842 | kfree(buffer.pointer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 845 | static int acpi_device_set_context(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 857 | if (type != ACPI_BUS_TYPE_POWER_BUTTON && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | type != ACPI_BUS_TYPE_SLEEP_BUTTON) { |
| 859 | status = acpi_attach_data(device->handle, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 860 | acpi_bus_data_handler, device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
| 862 | if (ACPI_FAILURE(status)) { |
| 863 | printk("Error attaching device data\n"); |
| 864 | result = -ENODEV; |
| 865 | } |
| 866 | } |
| 867 | return result; |
| 868 | } |
| 869 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 870 | static void acpi_device_get_debug_info(struct acpi_device *device, |
| 871 | acpi_handle handle, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
| 873 | #ifdef CONFIG_ACPI_DEBUG_OUTPUT |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 874 | char *type_string = NULL; |
| 875 | char name[80] = { '?', '\0' }; |
| 876 | struct acpi_buffer buffer = { sizeof(name), name }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 910 | #endif /*CONFIG_ACPI_DEBUG_OUTPUT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 913 | static int acpi_bus_remove(struct acpi_device *dev, int rmdevice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | if (!dev) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 916 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
Li Shaohua | 9633357 | 2006-12-07 20:56:46 +0800 | [diff] [blame] | 918 | dev->removal_type = ACPI_BUS_REMOVAL_EJECT; |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 919 | device_release_driver(&dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | |
| 921 | if (!rmdevice) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 922 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT); |
| 925 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 926 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 929 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 930 | acpi_add_single_object(struct acpi_device **child, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 931 | struct acpi_device *parent, acpi_handle handle, int type, |
| 932 | struct acpi_bus_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 934 | int result = 0; |
| 935 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | |
| 938 | if (!child) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 939 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
| 941 | device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL); |
| 942 | if (!device) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 943 | printk(KERN_ERR PREFIX "Memory allocation error\n"); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 944 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | } |
| 946 | memset(device, 0, sizeof(struct acpi_device)); |
| 947 | |
| 948 | device->handle = handle; |
| 949 | device->parent = parent; |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 950 | device->bus_ops = *ops; /* workround for not call .start */ |
| 951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 953 | acpi_device_get_busid(device, handle, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * ------ |
Keiichiro Tokunaga | 6940fab | 2005-03-30 23:15:47 -0500 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | */ |
| 977 | switch (type) { |
Keiichiro Tokunaga | 6940fab | 2005-03-30 23:15:47 -0500 | [diff] [blame] | 978 | case ACPI_BUS_TYPE_PROCESSOR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1001 | acpi_device_set_id(device, parent, handle, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1013 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1033 | if ((result = acpi_device_set_context(device, type))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | goto end; |
| 1035 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1036 | acpi_device_get_debug_info(device, handle, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1038 | acpi_device_register(device, parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1040 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | if (!result) |
| 1042 | *child = device; |
| 1043 | else { |
Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 1044 | kfree(device->pnp.cid_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | kfree(device); |
| 1046 | } |
| 1047 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1048 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1051 | static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | |
| 1062 | if (!start) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1063 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
| 1065 | parent = start; |
| 1066 | phandle = start->handle; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1075 | chandle, &chandle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1123 | if (ops->acpi_op_add) |
| 1124 | status = acpi_add_single_object(&child, parent, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1125 | chandle, type, ops); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1126 | else |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1127 | status = acpi_bus_get_device(chandle, &child); |
| 1128 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1129 | if (ACPI_FAILURE(status)) |
| 1130 | continue; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1131 | |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1132 | if (ops->acpi_op_start && !(ops->acpi_op_add)) { |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1133 | status = acpi_start_single_object(child); |
| 1134 | if (ACPI_FAILURE(status)) |
| 1135 | continue; |
| 1136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1146 | * in place before we can fully implement this. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1160 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1163 | int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1164 | acpi_bus_add(struct acpi_device **child, |
| 1165 | struct acpi_device *parent, acpi_handle handle, int type) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1166 | { |
| 1167 | int result; |
| 1168 | struct acpi_bus_ops ops; |
| 1169 | |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1170 | memset(&ops, 0, sizeof(ops)); |
| 1171 | ops.acpi_op_add = 1; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1172 | |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1173 | result = acpi_add_single_object(child, parent, handle, type, &ops); |
| 1174 | if (!result) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1175 | result = acpi_bus_scan(*child, &ops); |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1176 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1177 | return result; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1178 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1179 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1180 | EXPORT_SYMBOL(acpi_bus_add); |
| 1181 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1182 | int acpi_bus_start(struct acpi_device *device) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1183 | { |
| 1184 | int result; |
| 1185 | struct acpi_bus_ops ops; |
| 1186 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1187 | |
| 1188 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1189 | return -EINVAL; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 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 | } |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1197 | return result; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1198 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1199 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1200 | EXPORT_SYMBOL(acpi_bus_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
Kristen Accardi | ceaba66 | 2006-02-23 17:56:01 -0800 | [diff] [blame] | 1202 | int acpi_bus_trim(struct acpi_device *start, int rmdevice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1211 | parent = start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1217 | chandle, &chandle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Kristen Accardi | ceaba66 | 2006-02-23 17:56:01 -0800 | [diff] [blame] | 1255 | EXPORT_SYMBOL_GPL(acpi_bus_trim); |
| 1256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1258 | static int acpi_bus_scan_fixed(struct acpi_device *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1260 | int result = 0; |
| 1261 | struct acpi_device *device = NULL; |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1262 | struct acpi_bus_ops ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | |
| 1264 | if (!root) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1265 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1267 | memset(&ops, 0, sizeof(ops)); |
| 1268 | ops.acpi_op_add = 1; |
| 1269 | ops.acpi_op_start = 1; |
| 1270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | /* |
| 1272 | * Enumerate all fixed-feature devices. |
| 1273 | */ |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1274 | if (acpi_fadt.pwr_button == 0) { |
| 1275 | result = acpi_add_single_object(&device, acpi_root, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1276 | NULL, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1277 | ACPI_BUS_TYPE_POWER_BUTTON, |
| 1278 | &ops); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1279 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1281 | if (acpi_fadt.sleep_button == 0) { |
| 1282 | result = acpi_add_single_object(&device, acpi_root, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1283 | NULL, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1284 | ACPI_BUS_TYPE_SLEEP_BUTTON, |
| 1285 | &ops); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1288 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | static int __init acpi_scan_init(void) |
| 1292 | { |
| 1293 | int result; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1294 | struct acpi_bus_ops ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | |
| 1297 | if (acpi_disabled) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1298 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1300 | memset(&ops, 0, sizeof(ops)); |
| 1301 | ops.acpi_op_add = 1; |
| 1302 | ops.acpi_op_start = 1; |
| 1303 | |
Patrick Mochel | 5b32726 | 2006-05-10 10:33:00 -0400 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | /* |
| 1311 | * Create the root device in the bus's device tree |
| 1312 | */ |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1313 | result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1314 | ACPI_BUS_TYPE_SYSTEM, &ops); |
Patrick Mochel | 5b32726 | 2006-05-10 10:33:00 -0400 | [diff] [blame] | 1315 | if (result) |
| 1316 | goto Done; |
| 1317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | /* |
| 1319 | * Enumerate devices in the ACPI namespace. |
| 1320 | */ |
| 1321 | result = acpi_bus_scan_fixed(acpi_root); |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1322 | if (!result) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1323 | result = acpi_bus_scan(acpi_root, &ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | |
| 1325 | if (result) |
| 1326 | acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL); |
| 1327 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1328 | Done: |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1329 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | subsys_initcall(acpi_scan_init); |