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