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 | f52fd66 | 2007-02-12 22:42:12 -0500 | [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" |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 19 | #define ACPI_BUS_HID "LNXSYBUS" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #define ACPI_BUS_DEVICE_NAME "System Bus" |
| 21 | |
| 22 | static LIST_HEAD(acpi_device_list); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 23 | static LIST_HEAD(acpi_bus_id_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | DEFINE_SPINLOCK(acpi_device_lock); |
| 25 | LIST_HEAD(acpi_wakeup_device_list); |
| 26 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 27 | struct acpi_device_bus_id{ |
Zhang Rui | bb09585 | 2007-01-04 15:03:18 +0800 | [diff] [blame] | 28 | char bus_id[15]; |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 29 | unsigned int instance_no; |
| 30 | struct list_head node; |
| 31 | }; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Creates hid/cid(s) string needed for modalias and uevent |
| 35 | * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get: |
| 36 | * char *modalias: "acpi:IBM0001:ACPI0001" |
| 37 | */ |
Adrian Bunk | b3e572d | 2007-08-14 23:22:35 +0200 | [diff] [blame] | 38 | static int create_modalias(struct acpi_device *acpi_dev, char *modalias, |
| 39 | int size) |
| 40 | { |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 41 | int len; |
Zhang Rui | 5c9fcb5 | 2008-03-20 16:40:32 +0800 | [diff] [blame] | 42 | int count; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 43 | |
Zhang Rui | 5c9fcb5 | 2008-03-20 16:40:32 +0800 | [diff] [blame] | 44 | if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids) |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 45 | return -ENODEV; |
| 46 | |
Zhang Rui | 5c9fcb5 | 2008-03-20 16:40:32 +0800 | [diff] [blame] | 47 | len = snprintf(modalias, size, "acpi:"); |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 48 | size -= len; |
| 49 | |
Zhang Rui | 5c9fcb5 | 2008-03-20 16:40:32 +0800 | [diff] [blame] | 50 | if (acpi_dev->flags.hardware_id) { |
| 51 | count = snprintf(&modalias[len], size, "%s:", |
| 52 | acpi_dev->pnp.hardware_id); |
| 53 | if (count < 0 || count >= size) |
| 54 | return -EINVAL; |
| 55 | len += count; |
| 56 | size -= count; |
| 57 | } |
| 58 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 59 | if (acpi_dev->flags.compatible_ids) { |
| 60 | struct acpi_compatible_id_list *cid_list; |
| 61 | int i; |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 62 | |
| 63 | cid_list = acpi_dev->pnp.cid_list; |
| 64 | for (i = 0; i < cid_list->count; i++) { |
| 65 | count = snprintf(&modalias[len], size, "%s:", |
| 66 | cid_list->id[i].value); |
| 67 | if (count < 0 || count >= size) { |
Len Brown | 87ecd5c | 2008-01-01 14:00:00 -0500 | [diff] [blame] | 68 | printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size", |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 69 | acpi_dev->pnp.device_name, i); |
| 70 | break; |
| 71 | } |
| 72 | len += count; |
| 73 | size -= count; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | modalias[len] = '\0'; |
| 78 | return len; |
| 79 | } |
| 80 | |
| 81 | static ssize_t |
| 82 | acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) { |
| 83 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 84 | int len; |
| 85 | |
| 86 | /* Device has no HID and no CID or string is >1024 */ |
| 87 | len = create_modalias(acpi_dev, buf, 1024); |
| 88 | if (len <= 0) |
| 89 | return 0; |
| 90 | buf[len++] = '\n'; |
| 91 | return len; |
| 92 | } |
| 93 | static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); |
| 94 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 95 | static int acpi_eject_operation(acpi_handle handle, int lockable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | struct acpi_object_list arg_list; |
| 98 | union acpi_object arg; |
| 99 | acpi_status status = AE_OK; |
| 100 | |
| 101 | /* |
| 102 | * TBD: evaluate _PS3? |
| 103 | */ |
| 104 | |
| 105 | if (lockable) { |
| 106 | arg_list.count = 1; |
| 107 | arg_list.pointer = &arg; |
| 108 | arg.type = ACPI_TYPE_INTEGER; |
| 109 | arg.integer.value = 0; |
| 110 | acpi_evaluate_object(handle, "_LCK", &arg_list, NULL); |
| 111 | } |
| 112 | |
| 113 | arg_list.count = 1; |
| 114 | arg_list.pointer = &arg; |
| 115 | arg.type = ACPI_TYPE_INTEGER; |
| 116 | arg.integer.value = 1; |
| 117 | |
| 118 | /* |
| 119 | * TBD: _EJD support. |
| 120 | */ |
| 121 | |
| 122 | status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL); |
| 123 | if (ACPI_FAILURE(status)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 124 | return (-ENODEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 127 | return (0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | static ssize_t |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 131 | acpi_eject_store(struct device *d, struct device_attribute *attr, |
| 132 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 134 | int result; |
| 135 | int ret = count; |
| 136 | int islockable; |
| 137 | acpi_status status; |
| 138 | acpi_handle handle; |
| 139 | acpi_object_type type = 0; |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 140 | struct acpi_device *acpi_device = to_acpi_device(d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | if ((!count) || (buf[0] != '1')) { |
| 143 | return -EINVAL; |
| 144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | #ifndef FORCE_EJECT |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 146 | if (acpi_device->driver == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | ret = -ENODEV; |
| 148 | goto err; |
| 149 | } |
| 150 | #endif |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 151 | status = acpi_get_type(acpi_device->handle, &type); |
| 152 | if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | ret = -ENODEV; |
| 154 | goto err; |
| 155 | } |
| 156 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 157 | islockable = acpi_device->flags.lockable; |
| 158 | handle = acpi_device->handle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 160 | result = acpi_bus_trim(acpi_device, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | if (!result) |
| 163 | result = acpi_eject_operation(handle, islockable); |
| 164 | |
| 165 | if (result) { |
| 166 | ret = -EBUSY; |
| 167 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 168 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 172 | static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 174 | static ssize_t |
| 175 | acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) { |
| 176 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 178 | return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id); |
| 179 | } |
| 180 | static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL); |
| 181 | |
| 182 | static ssize_t |
| 183 | acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) { |
| 184 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 185 | struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 186 | int result; |
| 187 | |
| 188 | result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path); |
| 189 | if(result) |
| 190 | goto end; |
| 191 | |
| 192 | result = sprintf(buf, "%s\n", (char*)path.pointer); |
| 193 | kfree(path.pointer); |
| 194 | end: |
| 195 | return result; |
| 196 | } |
| 197 | static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL); |
| 198 | |
| 199 | static int acpi_device_setup_files(struct acpi_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 201 | acpi_status status; |
| 202 | acpi_handle temp; |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 203 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 205 | /* |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 206 | * Devices gotten from FADT don't have a "path" attribute |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 207 | */ |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 208 | if(dev->handle) { |
| 209 | result = device_create_file(&dev->dev, &dev_attr_path); |
| 210 | if(result) |
| 211 | goto end; |
| 212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 214 | if(dev->flags.hardware_id) { |
| 215 | result = device_create_file(&dev->dev, &dev_attr_hid); |
| 216 | if(result) |
| 217 | goto end; |
| 218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 220 | if (dev->flags.hardware_id || dev->flags.compatible_ids){ |
| 221 | result = device_create_file(&dev->dev, &dev_attr_modalias); |
| 222 | if(result) |
| 223 | goto end; |
| 224 | } |
| 225 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 226 | /* |
| 227 | * If device has _EJ0, 'eject' file is created that is used to trigger |
| 228 | * hot-removal function from userland. |
| 229 | */ |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 230 | status = acpi_get_handle(dev->handle, "_EJ0", &temp); |
| 231 | if (ACPI_SUCCESS(status)) |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 232 | result = device_create_file(&dev->dev, &dev_attr_eject); |
| 233 | end: |
| 234 | return result; |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 237 | static void acpi_device_remove_files(struct acpi_device *dev) |
| 238 | { |
| 239 | acpi_status status; |
| 240 | acpi_handle temp; |
| 241 | |
| 242 | /* |
| 243 | * If device has _EJ0, 'eject' file is created that is used to trigger |
| 244 | * hot-removal function from userland. |
| 245 | */ |
| 246 | status = acpi_get_handle(dev->handle, "_EJ0", &temp); |
| 247 | if (ACPI_SUCCESS(status)) |
| 248 | device_remove_file(&dev->dev, &dev_attr_eject); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 249 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 250 | if (dev->flags.hardware_id || dev->flags.compatible_ids) |
| 251 | device_remove_file(&dev->dev, &dev_attr_modalias); |
| 252 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 253 | if(dev->flags.hardware_id) |
| 254 | device_remove_file(&dev->dev, &dev_attr_hid); |
| 255 | if(dev->handle) |
| 256 | device_remove_file(&dev->dev, &dev_attr_path); |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | /* -------------------------------------------------------------------------- |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 259 | ACPI Bus operations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | -------------------------------------------------------------------------- */ |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 261 | |
| 262 | int acpi_match_device_ids(struct acpi_device *device, |
| 263 | const struct acpi_device_id *ids) |
| 264 | { |
| 265 | const struct acpi_device_id *id; |
| 266 | |
| 267 | if (device->flags.hardware_id) { |
| 268 | for (id = ids; id->id[0]; id++) { |
| 269 | if (!strcmp((char*)id->id, device->pnp.hardware_id)) |
| 270 | return 0; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (device->flags.compatible_ids) { |
| 275 | struct acpi_compatible_id_list *cid_list = device->pnp.cid_list; |
| 276 | int i; |
| 277 | |
| 278 | for (id = ids; id->id[0]; id++) { |
| 279 | /* compare multiple _CID entries against driver ids */ |
| 280 | for (i = 0; i < cid_list->count; i++) { |
| 281 | if (!strcmp((char*)id->id, |
| 282 | cid_list->id[i].value)) |
| 283 | return 0; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return -ENOENT; |
| 289 | } |
| 290 | EXPORT_SYMBOL(acpi_match_device_ids); |
| 291 | |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 292 | static void acpi_device_release(struct device *dev) |
| 293 | { |
| 294 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 295 | |
| 296 | kfree(acpi_dev->pnp.cid_list); |
| 297 | kfree(acpi_dev); |
| 298 | } |
| 299 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 300 | static int acpi_device_suspend(struct device *dev, pm_message_t state) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 301 | { |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 302 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 303 | struct acpi_driver *acpi_drv = acpi_dev->driver; |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 304 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 305 | if (acpi_drv && acpi_drv->ops.suspend) |
| 306 | return acpi_drv->ops.suspend(acpi_dev, state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 310 | static int acpi_device_resume(struct device *dev) |
| 311 | { |
| 312 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 313 | struct acpi_driver *acpi_drv = acpi_dev->driver; |
| 314 | |
| 315 | if (acpi_drv && acpi_drv->ops.resume) |
| 316 | return acpi_drv->ops.resume(acpi_dev); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int acpi_bus_match(struct device *dev, struct device_driver *drv) |
| 321 | { |
| 322 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 323 | struct acpi_driver *acpi_drv = to_acpi_driver(drv); |
| 324 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 325 | return !acpi_match_device_ids(acpi_dev, acpi_drv->ids); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 326 | } |
| 327 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 328 | static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 329 | { |
| 330 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 331 | int len; |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 332 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 333 | if (add_uevent_var(env, "MODALIAS=")) |
| 334 | return -ENOMEM; |
| 335 | len = create_modalias(acpi_dev, &env->buf[env->buflen - 1], |
| 336 | sizeof(env->buf) - env->buflen); |
| 337 | if (len >= (sizeof(env->buf) - env->buflen)) |
| 338 | return -ENOMEM; |
| 339 | env->buflen += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return 0; |
| 341 | } |
| 342 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 343 | static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *); |
| 344 | static int acpi_start_single_object(struct acpi_device *); |
| 345 | static int acpi_device_probe(struct device * dev) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 346 | { |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 347 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 348 | struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver); |
| 349 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 351 | ret = acpi_bus_driver_init(acpi_dev, acpi_drv); |
| 352 | if (!ret) { |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 353 | if (acpi_dev->bus_ops.acpi_op_start) |
| 354 | acpi_start_single_object(acpi_dev); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 355 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 356 | "Found driver [%s] for device [%s]\n", |
| 357 | acpi_drv->name, acpi_dev->pnp.bus_id)); |
| 358 | get_device(dev); |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 359 | } |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | static int acpi_device_remove(struct device * dev) |
| 364 | { |
| 365 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 366 | struct acpi_driver *acpi_drv = acpi_dev->driver; |
| 367 | |
| 368 | if (acpi_drv) { |
| 369 | if (acpi_drv->ops.stop) |
Li Shaohua | 9633357 | 2006-12-07 20:56:46 +0800 | [diff] [blame] | 370 | acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 371 | if (acpi_drv->ops.remove) |
Li Shaohua | 9633357 | 2006-12-07 20:56:46 +0800 | [diff] [blame] | 372 | acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type); |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 373 | } |
| 374 | acpi_dev->driver = NULL; |
| 375 | acpi_driver_data(dev) = NULL; |
| 376 | |
| 377 | put_device(dev); |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 381 | static void acpi_device_shutdown(struct device *dev) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 382 | { |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 383 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
| 384 | struct acpi_driver *acpi_drv = acpi_dev->driver; |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 385 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 386 | if (acpi_drv && acpi_drv->ops.shutdown) |
| 387 | acpi_drv->ops.shutdown(acpi_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 389 | return ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
| 391 | |
David Brownell | 55955aa | 2007-05-08 00:28:35 -0700 | [diff] [blame] | 392 | struct bus_type acpi_bus_type = { |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 393 | .name = "acpi", |
| 394 | .suspend = acpi_device_suspend, |
| 395 | .resume = acpi_device_resume, |
Patrick Mochel | 5d9464a | 2006-12-07 20:56:27 +0800 | [diff] [blame] | 396 | .shutdown = acpi_device_shutdown, |
| 397 | .match = acpi_bus_match, |
| 398 | .probe = acpi_device_probe, |
| 399 | .remove = acpi_device_remove, |
| 400 | .uevent = acpi_device_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | }; |
| 402 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 403 | static int acpi_device_register(struct acpi_device *device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | struct acpi_device *parent) |
| 405 | { |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 406 | int result; |
| 407 | struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id; |
| 408 | int found = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | /* |
| 410 | * Linkage |
| 411 | * ------- |
| 412 | * Link this device to its parent and siblings. |
| 413 | */ |
| 414 | INIT_LIST_HEAD(&device->children); |
| 415 | INIT_LIST_HEAD(&device->node); |
| 416 | INIT_LIST_HEAD(&device->g_list); |
| 417 | INIT_LIST_HEAD(&device->wakeup_list); |
| 418 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 419 | new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL); |
| 420 | if (!new_bus_id) { |
| 421 | printk(KERN_ERR PREFIX "Memory allocation error\n"); |
| 422 | return -ENOMEM; |
| 423 | } |
| 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | spin_lock(&acpi_device_lock); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 426 | /* |
| 427 | * Find suitable bus_id and instance number in acpi_bus_id_list |
| 428 | * If failed, create one and link it into acpi_bus_id_list |
| 429 | */ |
| 430 | list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) { |
Zhang Rui | bb09585 | 2007-01-04 15:03:18 +0800 | [diff] [blame] | 431 | if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) { |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 432 | acpi_device_bus_id->instance_no ++; |
| 433 | found = 1; |
| 434 | kfree(new_bus_id); |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | if(!found) { |
| 439 | acpi_device_bus_id = new_bus_id; |
Zhang Rui | bb09585 | 2007-01-04 15:03:18 +0800 | [diff] [blame] | 440 | strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device"); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 441 | acpi_device_bus_id->instance_no = 0; |
| 442 | list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list); |
| 443 | } |
| 444 | sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no); |
| 445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | if (device->parent) { |
| 447 | list_add_tail(&device->node, &device->parent->children); |
| 448 | list_add_tail(&device->g_list, &device->parent->g_list); |
| 449 | } else |
| 450 | list_add_tail(&device->g_list, &acpi_device_list); |
| 451 | if (device->wakeup.flags.valid) |
| 452 | list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); |
| 453 | spin_unlock(&acpi_device_lock); |
| 454 | |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 455 | if (device->parent) |
| 456 | device->dev.parent = &parent->dev; |
| 457 | device->dev.bus = &acpi_bus_type; |
| 458 | device_initialize(&device->dev); |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 459 | device->dev.release = &acpi_device_release; |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 460 | result = device_add(&device->dev); |
| 461 | if(result) { |
Len Brown | 87ecd5c | 2008-01-01 14:00:00 -0500 | [diff] [blame] | 462 | printk(KERN_ERR PREFIX "Error adding device %s", device->dev.bus_id); |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 463 | goto end; |
| 464 | } |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 465 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 466 | result = acpi_device_setup_files(device); |
| 467 | if(result) |
| 468 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id)); |
| 469 | |
Li Shaohua | 9633357 | 2006-12-07 20:56:46 +0800 | [diff] [blame] | 470 | device->removal_type = ACPI_BUS_REMOVAL_NORMAL; |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 471 | return 0; |
| 472 | end: |
| 473 | spin_lock(&acpi_device_lock); |
| 474 | if (device->parent) { |
| 475 | list_del(&device->node); |
| 476 | list_del(&device->g_list); |
| 477 | } else |
| 478 | list_del(&device->g_list); |
| 479 | list_del(&device->wakeup_list); |
| 480 | spin_unlock(&acpi_device_lock); |
| 481 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | static void acpi_device_unregister(struct acpi_device *device, int type) |
| 485 | { |
| 486 | spin_lock(&acpi_device_lock); |
| 487 | if (device->parent) { |
| 488 | list_del(&device->node); |
| 489 | list_del(&device->g_list); |
| 490 | } else |
| 491 | list_del(&device->g_list); |
| 492 | |
| 493 | list_del(&device->wakeup_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | spin_unlock(&acpi_device_lock); |
| 495 | |
| 496 | acpi_detach_data(device->handle, acpi_bus_data_handler); |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 497 | |
Patrick Mochel | f883d9d | 2006-12-07 20:56:38 +0800 | [diff] [blame] | 498 | acpi_device_remove_files(device); |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 499 | device_unregister(&device->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 502 | /* -------------------------------------------------------------------------- |
| 503 | Driver Management |
| 504 | -------------------------------------------------------------------------- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 506 | * acpi_bus_driver_init - add a device to a driver |
| 507 | * @device: the device to add and initialize |
| 508 | * @driver: driver for the device |
| 509 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | * Used to initialize a device via its device driver. Called whenever a |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 511 | * driver is bound to a device. Invokes the driver's add() ops. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | */ |
| 513 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 514 | acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 516 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
| 519 | if (!device || !driver) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 520 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
| 522 | if (!driver->ops.add) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 523 | return -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
| 525 | result = driver->ops.add(device); |
| 526 | if (result) { |
| 527 | device->driver = NULL; |
| 528 | acpi_driver_data(device) = NULL; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 529 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | device->driver = driver; |
| 533 | |
| 534 | /* |
| 535 | * TBD - Configuration Management: Assign resources to device based |
| 536 | * upon possible configuration and currently allocated resources. |
| 537 | */ |
| 538 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 539 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 540 | "Driver successfully bound to device\n")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 541 | return 0; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Adrian Bunk | 8713cbe | 2005-09-02 17:16:48 -0400 | [diff] [blame] | 544 | static int acpi_start_single_object(struct acpi_device *device) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 545 | { |
| 546 | int result = 0; |
| 547 | struct acpi_driver *driver; |
| 548 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 549 | |
| 550 | if (!(driver = device->driver)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 551 | return 0; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 552 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | if (driver->ops.start) { |
| 554 | result = driver->ops.start(device); |
| 555 | if (result && driver->ops.remove) |
| 556 | driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 559 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 563 | * acpi_bus_register_driver - register a driver with the ACPI bus |
| 564 | * @driver: driver being registered |
| 565 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | * Registers a driver with the ACPI bus. Searches the namespace for all |
Bjorn Helgaas | 9d9f749 | 2006-03-28 17:04:00 -0500 | [diff] [blame] | 567 | * devices that match the driver's criteria and binds. Returns zero for |
| 568 | * success or a negative error status for failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 570 | int acpi_bus_register_driver(struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | { |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 572 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | |
| 574 | if (acpi_disabled) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 575 | return -ENODEV; |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 576 | driver->drv.name = driver->name; |
| 577 | driver->drv.bus = &acpi_bus_type; |
| 578 | driver->drv.owner = driver->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 580 | ret = driver_register(&driver->drv); |
| 581 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 584 | EXPORT_SYMBOL(acpi_bus_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 587 | * acpi_bus_unregister_driver - unregisters a driver with the APIC bus |
| 588 | * @driver: driver to unregister |
| 589 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | * Unregisters a driver with the ACPI bus. Searches the namespace for all |
| 591 | * devices that match the driver's criteria and unbinds. |
| 592 | */ |
Bjorn Helgaas | 06ea8e0 | 2006-04-27 05:25:00 -0400 | [diff] [blame] | 593 | void acpi_bus_unregister_driver(struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 595 | driver_unregister(&driver->drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | EXPORT_SYMBOL(acpi_bus_unregister_driver); |
| 599 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | /* -------------------------------------------------------------------------- |
| 601 | Device Enumeration |
| 602 | -------------------------------------------------------------------------- */ |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 603 | acpi_status |
| 604 | acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd) |
| 605 | { |
| 606 | acpi_status status; |
| 607 | acpi_handle tmp; |
| 608 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 609 | union acpi_object *obj; |
| 610 | |
| 611 | status = acpi_get_handle(handle, "_EJD", &tmp); |
| 612 | if (ACPI_FAILURE(status)) |
| 613 | return status; |
| 614 | |
| 615 | status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer); |
| 616 | if (ACPI_SUCCESS(status)) { |
| 617 | obj = buffer.pointer; |
Holger Macht | 3b5fee5 | 2008-02-14 13:40:34 +0100 | [diff] [blame] | 618 | status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer, |
| 619 | ejd); |
Len Brown | c8f7a62 | 2006-07-09 17:22:28 -0400 | [diff] [blame] | 620 | kfree(buffer.pointer); |
| 621 | } |
| 622 | return status; |
| 623 | } |
| 624 | EXPORT_SYMBOL_GPL(acpi_bus_get_ejd); |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context) |
| 627 | { |
| 628 | |
| 629 | /* TBD */ |
| 630 | |
| 631 | return; |
| 632 | } |
| 633 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 634 | static int acpi_bus_get_perf_flags(struct acpi_device *device) |
| 635 | { |
| 636 | device->performance.state = ACPI_STATE_UNKNOWN; |
| 637 | return 0; |
| 638 | } |
| 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | static acpi_status |
| 641 | acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device, |
| 642 | union acpi_object *package) |
| 643 | { |
| 644 | int i = 0; |
| 645 | union acpi_object *element = NULL; |
| 646 | |
| 647 | if (!device || !package || (package->package.count < 2)) |
| 648 | return AE_BAD_PARAMETER; |
| 649 | |
| 650 | element = &(package->package.elements[0]); |
| 651 | if (!element) |
| 652 | return AE_BAD_PARAMETER; |
| 653 | if (element->type == ACPI_TYPE_PACKAGE) { |
| 654 | if ((element->package.count < 2) || |
| 655 | (element->package.elements[0].type != |
| 656 | ACPI_TYPE_LOCAL_REFERENCE) |
| 657 | || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) |
| 658 | return AE_BAD_DATA; |
| 659 | device->wakeup.gpe_device = |
| 660 | element->package.elements[0].reference.handle; |
| 661 | device->wakeup.gpe_number = |
| 662 | (u32) element->package.elements[1].integer.value; |
| 663 | } else if (element->type == ACPI_TYPE_INTEGER) { |
| 664 | device->wakeup.gpe_number = element->integer.value; |
| 665 | } else |
| 666 | return AE_BAD_DATA; |
| 667 | |
| 668 | element = &(package->package.elements[1]); |
| 669 | if (element->type != ACPI_TYPE_INTEGER) { |
| 670 | return AE_BAD_DATA; |
| 671 | } |
| 672 | device->wakeup.sleep_state = element->integer.value; |
| 673 | |
| 674 | if ((package->package.count - 2) > ACPI_MAX_HANDLES) { |
| 675 | return AE_NO_MEMORY; |
| 676 | } |
| 677 | device->wakeup.resources.count = package->package.count - 2; |
| 678 | for (i = 0; i < device->wakeup.resources.count; i++) { |
| 679 | element = &(package->package.elements[i + 2]); |
Bob Moore | cd0b224 | 2008-04-10 19:06:43 +0400 | [diff] [blame] | 680 | if (element->type != ACPI_TYPE_LOCAL_REFERENCE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
| 683 | device->wakeup.resources.handles[i] = element->reference.handle; |
| 684 | } |
| 685 | |
| 686 | return AE_OK; |
| 687 | } |
| 688 | |
| 689 | static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device) |
| 690 | { |
| 691 | acpi_status status = 0; |
| 692 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 693 | union acpi_object *package = NULL; |
Zhao Yakui | 729b2bd | 2008-03-19 13:26:54 +0800 | [diff] [blame] | 694 | union acpi_object in_arg[3]; |
| 695 | struct acpi_object_list arg_list = { 3, in_arg }; |
| 696 | acpi_status psw_status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 698 | struct acpi_device_id button_device_ids[] = { |
| 699 | {"PNP0C0D", 0}, |
| 700 | {"PNP0C0C", 0}, |
| 701 | {"PNP0C0E", 0}, |
| 702 | {"", 0}, |
| 703 | }; |
| 704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | /* _PRW */ |
| 706 | status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer); |
| 707 | if (ACPI_FAILURE(status)) { |
| 708 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW")); |
| 709 | goto end; |
| 710 | } |
| 711 | |
| 712 | package = (union acpi_object *)buffer.pointer; |
| 713 | status = acpi_bus_extract_wakeup_device_power_package(device, package); |
| 714 | if (ACPI_FAILURE(status)) { |
| 715 | ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package")); |
| 716 | goto end; |
| 717 | } |
| 718 | |
| 719 | kfree(buffer.pointer); |
| 720 | |
| 721 | device->wakeup.flags.valid = 1; |
Zhao Yakui | 729b2bd | 2008-03-19 13:26:54 +0800 | [diff] [blame] | 722 | /* Call _PSW/_DSW object to disable its ability to wake the sleeping |
| 723 | * system for the ACPI device with the _PRW object. |
| 724 | * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW. |
| 725 | * So it is necessary to call _DSW object first. Only when it is not |
| 726 | * present will the _PSW object used. |
| 727 | */ |
| 728 | /* |
| 729 | * Three agruments are needed for the _DSW object. |
| 730 | * Argument 0: enable/disable the wake capabilities |
| 731 | * When _DSW object is called to disable the wake capabilities, maybe |
| 732 | * the first argument is filled. The value of the other two agruments |
| 733 | * is meaningless. |
| 734 | */ |
| 735 | in_arg[0].type = ACPI_TYPE_INTEGER; |
| 736 | in_arg[0].integer.value = 0; |
| 737 | in_arg[1].type = ACPI_TYPE_INTEGER; |
| 738 | in_arg[1].integer.value = 0; |
| 739 | in_arg[2].type = ACPI_TYPE_INTEGER; |
| 740 | in_arg[2].integer.value = 0; |
| 741 | psw_status = acpi_evaluate_object(device->handle, "_DSW", |
| 742 | &arg_list, NULL); |
| 743 | if (ACPI_FAILURE(psw_status) && (psw_status != AE_NOT_FOUND)) |
| 744 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "error in evaluate _DSW\n")); |
| 745 | /* |
| 746 | * When the _DSW object is not present, OSPM will call _PSW object. |
| 747 | */ |
| 748 | if (psw_status == AE_NOT_FOUND) { |
| 749 | /* |
| 750 | * Only one agruments is required for the _PSW object. |
| 751 | * agrument 0: enable/disable the wake capabilities |
| 752 | */ |
| 753 | arg_list.count = 1; |
| 754 | in_arg[0].integer.value = 0; |
| 755 | psw_status = acpi_evaluate_object(device->handle, "_PSW", |
| 756 | &arg_list, NULL); |
| 757 | if (ACPI_FAILURE(psw_status) && (psw_status != AE_NOT_FOUND)) |
| 758 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "error in " |
| 759 | "evaluate _PSW\n")); |
| 760 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | /* Power button, Lid switch always enable wakeup */ |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame] | 762 | if (!acpi_match_device_ids(device, button_device_ids)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | device->wakeup.flags.run_wake = 1; |
| 764 | |
| 765 | end: |
| 766 | if (ACPI_FAILURE(status)) |
| 767 | device->flags.wake_capable = 0; |
| 768 | return 0; |
| 769 | } |
| 770 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 771 | static int acpi_bus_get_power_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | { |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 773 | acpi_status status = 0; |
| 774 | acpi_handle handle = NULL; |
| 775 | u32 i = 0; |
| 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
| 778 | /* |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 779 | * Power Management Flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | */ |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 781 | status = acpi_get_handle(device->handle, "_PSC", &handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | if (ACPI_SUCCESS(status)) |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 783 | device->power.flags.explicit_get = 1; |
| 784 | status = acpi_get_handle(device->handle, "_IRC", &handle); |
| 785 | if (ACPI_SUCCESS(status)) |
| 786 | device->power.flags.inrush_current = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
| 788 | /* |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 789 | * Enumerate supported power management states |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | */ |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 791 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) { |
| 792 | struct acpi_device_power_state *ps = &device->power.states[i]; |
| 793 | char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 795 | /* Evaluate "_PRx" to se if power resources are referenced */ |
| 796 | acpi_evaluate_reference(device->handle, object_name, NULL, |
| 797 | &ps->resources); |
| 798 | if (ps->resources.count) { |
| 799 | device->power.flags.power_resources = 1; |
| 800 | ps->flags.valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 803 | /* Evaluate "_PSx" to see if we can do explicit sets */ |
| 804 | object_name[2] = 'S'; |
| 805 | status = acpi_get_handle(device->handle, object_name, &handle); |
| 806 | if (ACPI_SUCCESS(status)) { |
| 807 | ps->flags.explicit_set = 1; |
| 808 | ps->flags.valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | } |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 810 | |
| 811 | /* State is valid if we have some power control */ |
| 812 | if (ps->resources.count || ps->flags.explicit_set) |
| 813 | ps->flags.valid = 1; |
| 814 | |
| 815 | ps->power = -1; /* Unknown - driver assigned */ |
| 816 | ps->latency = -1; /* Unknown - driver assigned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 819 | /* Set defaults for D0 and D3 states (always valid) */ |
| 820 | device->power.states[ACPI_STATE_D0].flags.valid = 1; |
| 821 | device->power.states[ACPI_STATE_D0].power = 100; |
| 822 | device->power.states[ACPI_STATE_D3].flags.valid = 1; |
| 823 | device->power.states[ACPI_STATE_D3].power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 825 | /* TBD: System wake support and resource requirements. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
Zhang Rui | 9e89dde | 2006-12-07 20:56:16 +0800 | [diff] [blame] | 827 | device->power.state = ACPI_STATE_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
| 829 | return 0; |
| 830 | } |
| 831 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 832 | static int acpi_bus_get_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 834 | acpi_status status = AE_OK; |
| 835 | acpi_handle temp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | |
| 838 | /* Presence of _STA indicates 'dynamic_status' */ |
| 839 | status = acpi_get_handle(device->handle, "_STA", &temp); |
| 840 | if (ACPI_SUCCESS(status)) |
| 841 | device->flags.dynamic_status = 1; |
| 842 | |
| 843 | /* Presence of _CID indicates 'compatible_ids' */ |
| 844 | status = acpi_get_handle(device->handle, "_CID", &temp); |
| 845 | if (ACPI_SUCCESS(status)) |
| 846 | device->flags.compatible_ids = 1; |
| 847 | |
| 848 | /* Presence of _RMV indicates 'removable' */ |
| 849 | status = acpi_get_handle(device->handle, "_RMV", &temp); |
| 850 | if (ACPI_SUCCESS(status)) |
| 851 | device->flags.removable = 1; |
| 852 | |
| 853 | /* Presence of _EJD|_EJ0 indicates 'ejectable' */ |
| 854 | status = acpi_get_handle(device->handle, "_EJD", &temp); |
| 855 | if (ACPI_SUCCESS(status)) |
| 856 | device->flags.ejectable = 1; |
| 857 | else { |
| 858 | status = acpi_get_handle(device->handle, "_EJ0", &temp); |
| 859 | if (ACPI_SUCCESS(status)) |
| 860 | device->flags.ejectable = 1; |
| 861 | } |
| 862 | |
| 863 | /* Presence of _LCK indicates 'lockable' */ |
| 864 | status = acpi_get_handle(device->handle, "_LCK", &temp); |
| 865 | if (ACPI_SUCCESS(status)) |
| 866 | device->flags.lockable = 1; |
| 867 | |
| 868 | /* Presence of _PS0|_PR0 indicates 'power manageable' */ |
| 869 | status = acpi_get_handle(device->handle, "_PS0", &temp); |
| 870 | if (ACPI_FAILURE(status)) |
| 871 | status = acpi_get_handle(device->handle, "_PR0", &temp); |
| 872 | if (ACPI_SUCCESS(status)) |
| 873 | device->flags.power_manageable = 1; |
| 874 | |
| 875 | /* Presence of _PRW indicates wake capable */ |
| 876 | status = acpi_get_handle(device->handle, "_PRW", &temp); |
| 877 | if (ACPI_SUCCESS(status)) |
| 878 | device->flags.wake_capable = 1; |
| 879 | |
Joe Perches | 3c5f9be | 2008-02-03 17:06:17 +0200 | [diff] [blame] | 880 | /* TBD: Performance management */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 882 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 885 | static void acpi_device_get_busid(struct acpi_device *device, |
| 886 | acpi_handle handle, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 888 | char bus_id[5] = { '?', 0 }; |
| 889 | struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; |
| 890 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
| 892 | /* |
| 893 | * Bus ID |
| 894 | * ------ |
| 895 | * The device's Bus ID is simply the object name. |
| 896 | * TBD: Shouldn't this value be unique (within the ACPI namespace)? |
| 897 | */ |
| 898 | switch (type) { |
| 899 | case ACPI_BUS_TYPE_SYSTEM: |
| 900 | strcpy(device->pnp.bus_id, "ACPI"); |
| 901 | break; |
| 902 | case ACPI_BUS_TYPE_POWER_BUTTON: |
| 903 | strcpy(device->pnp.bus_id, "PWRF"); |
| 904 | break; |
| 905 | case ACPI_BUS_TYPE_SLEEP_BUTTON: |
| 906 | strcpy(device->pnp.bus_id, "SLPF"); |
| 907 | break; |
| 908 | default: |
| 909 | acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); |
| 910 | /* Clean up trailing underscores (if any) */ |
| 911 | for (i = 3; i > 1; i--) { |
| 912 | if (bus_id[i] == '_') |
| 913 | bus_id[i] = '\0'; |
| 914 | else |
| 915 | break; |
| 916 | } |
| 917 | strcpy(device->pnp.bus_id, bus_id); |
| 918 | break; |
| 919 | } |
| 920 | } |
| 921 | |
Zhang Rui | ae84333 | 2006-12-07 20:57:10 +0800 | [diff] [blame] | 922 | static int |
| 923 | acpi_video_bus_match(struct acpi_device *device) |
| 924 | { |
Thomas Renninger | 66fb9d1 | 2008-04-16 20:52:02 +0200 | [diff] [blame] | 925 | acpi_handle h_dummy; |
Zhang Rui | ae84333 | 2006-12-07 20:57:10 +0800 | [diff] [blame] | 926 | |
| 927 | if (!device) |
| 928 | return -EINVAL; |
| 929 | |
| 930 | /* Since there is no HID, CID for ACPI Video drivers, we have |
| 931 | * to check well known required nodes for each feature we support. |
| 932 | */ |
| 933 | |
| 934 | /* Does this device able to support video switching ? */ |
Thomas Renninger | 66fb9d1 | 2008-04-16 20:52:02 +0200 | [diff] [blame] | 935 | if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy)) && |
| 936 | ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy))) |
Zhang Rui | ae84333 | 2006-12-07 20:57:10 +0800 | [diff] [blame] | 937 | return 0; |
| 938 | |
| 939 | /* Does this device able to retrieve a video ROM ? */ |
Thomas Renninger | 66fb9d1 | 2008-04-16 20:52:02 +0200 | [diff] [blame] | 940 | if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy))) |
Zhang Rui | ae84333 | 2006-12-07 20:57:10 +0800 | [diff] [blame] | 941 | return 0; |
| 942 | |
| 943 | /* Does this device able to configure which video head to be POSTed ? */ |
Thomas Renninger | 66fb9d1 | 2008-04-16 20:52:02 +0200 | [diff] [blame] | 944 | if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy)) && |
| 945 | ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy)) && |
| 946 | ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy))) |
Zhang Rui | ae84333 | 2006-12-07 20:57:10 +0800 | [diff] [blame] | 947 | return 0; |
| 948 | |
| 949 | return -ENODEV; |
| 950 | } |
| 951 | |
Zhang Rui | 5473526 | 2007-01-11 02:09:09 -0500 | [diff] [blame] | 952 | /* |
| 953 | * acpi_bay_match - see if a device is an ejectable driver bay |
| 954 | * |
| 955 | * If an acpi object is ejectable and has one of the ACPI ATA methods defined, |
| 956 | * then we can safely call it an ejectable drive bay |
| 957 | */ |
| 958 | static int acpi_bay_match(struct acpi_device *device){ |
| 959 | acpi_status status; |
| 960 | acpi_handle handle; |
| 961 | acpi_handle tmp; |
| 962 | acpi_handle phandle; |
| 963 | |
| 964 | handle = device->handle; |
| 965 | |
| 966 | status = acpi_get_handle(handle, "_EJ0", &tmp); |
| 967 | if (ACPI_FAILURE(status)) |
| 968 | return -ENODEV; |
| 969 | |
| 970 | if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) || |
| 971 | (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) || |
| 972 | (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) || |
| 973 | (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp)))) |
| 974 | return 0; |
| 975 | |
| 976 | if (acpi_get_parent(handle, &phandle)) |
| 977 | return -ENODEV; |
| 978 | |
| 979 | if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) || |
| 980 | (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) || |
| 981 | (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) || |
| 982 | (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp)))) |
| 983 | return 0; |
| 984 | |
| 985 | return -ENODEV; |
| 986 | } |
| 987 | |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 988 | /* |
| 989 | * acpi_dock_match - see if a device has a _DCK method |
| 990 | */ |
| 991 | static int acpi_dock_match(struct acpi_device *device) |
| 992 | { |
| 993 | acpi_handle tmp; |
| 994 | return acpi_get_handle(device->handle, "_DCK", &tmp); |
| 995 | } |
| 996 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 997 | static void acpi_device_set_id(struct acpi_device *device, |
| 998 | struct acpi_device *parent, acpi_handle handle, |
| 999 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1001 | struct acpi_device_info *info; |
| 1002 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 1003 | char *hid = NULL; |
| 1004 | char *uid = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | struct acpi_compatible_id_list *cid_list = NULL; |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1006 | const char *cid_add = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1007 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
| 1009 | switch (type) { |
| 1010 | case ACPI_BUS_TYPE_DEVICE: |
| 1011 | status = acpi_get_object_info(handle, &buffer); |
| 1012 | if (ACPI_FAILURE(status)) { |
Harvey Harrison | 96b2dd1 | 2008-03-05 18:24:51 -0800 | [diff] [blame] | 1013 | printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | info = buffer.pointer; |
| 1018 | if (info->valid & ACPI_VALID_HID) |
| 1019 | hid = info->hardware_id.value; |
| 1020 | if (info->valid & ACPI_VALID_UID) |
| 1021 | uid = info->unique_id.value; |
| 1022 | if (info->valid & ACPI_VALID_CID) |
| 1023 | cid_list = &info->compatibility_id; |
| 1024 | if (info->valid & ACPI_VALID_ADR) { |
| 1025 | device->pnp.bus_address = info->address; |
| 1026 | device->flags.bus_address = 1; |
| 1027 | } |
Zhang Rui | ae84333 | 2006-12-07 20:57:10 +0800 | [diff] [blame] | 1028 | |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1029 | /* If we have a video/bay/dock device, add our selfdefined |
| 1030 | HID to the CID list. Like that the video/bay/dock drivers |
| 1031 | will get autoloaded and the device might still match |
| 1032 | against another driver. |
| 1033 | */ |
| 1034 | if (ACPI_SUCCESS(acpi_video_bus_match(device))) |
| 1035 | cid_add = ACPI_VIDEO_HID; |
| 1036 | else if (ACPI_SUCCESS(acpi_bay_match(device))) |
| 1037 | cid_add = ACPI_BAY_HID; |
| 1038 | else if (ACPI_SUCCESS(acpi_dock_match(device))) |
| 1039 | cid_add = ACPI_DOCK_HID; |
Zhang Rui | 5473526 | 2007-01-11 02:09:09 -0500 | [diff] [blame] | 1040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | break; |
| 1042 | case ACPI_BUS_TYPE_POWER: |
| 1043 | hid = ACPI_POWER_HID; |
| 1044 | break; |
| 1045 | case ACPI_BUS_TYPE_PROCESSOR: |
| 1046 | hid = ACPI_PROCESSOR_HID; |
| 1047 | break; |
| 1048 | case ACPI_BUS_TYPE_SYSTEM: |
| 1049 | hid = ACPI_SYSTEM_HID; |
| 1050 | break; |
| 1051 | case ACPI_BUS_TYPE_THERMAL: |
| 1052 | hid = ACPI_THERMAL_HID; |
| 1053 | break; |
| 1054 | case ACPI_BUS_TYPE_POWER_BUTTON: |
| 1055 | hid = ACPI_BUTTON_HID_POWERF; |
| 1056 | break; |
| 1057 | case ACPI_BUS_TYPE_SLEEP_BUTTON: |
| 1058 | hid = ACPI_BUTTON_HID_SLEEPF; |
| 1059 | break; |
| 1060 | } |
| 1061 | |
| 1062 | /* |
| 1063 | * \_SB |
| 1064 | * ---- |
| 1065 | * Fix for the system root bus device -- the only root-level device. |
| 1066 | */ |
Bob Moore | c51a4de | 2005-11-17 13:07:00 -0500 | [diff] [blame] | 1067 | if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | hid = ACPI_BUS_HID; |
| 1069 | strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME); |
| 1070 | strcpy(device->pnp.device_class, ACPI_BUS_CLASS); |
| 1071 | } |
| 1072 | |
| 1073 | if (hid) { |
| 1074 | strcpy(device->pnp.hardware_id, hid); |
| 1075 | device->flags.hardware_id = 1; |
| 1076 | } |
| 1077 | if (uid) { |
| 1078 | strcpy(device->pnp.unique_id, uid); |
| 1079 | device->flags.unique_id = 1; |
| 1080 | } |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1081 | if (cid_list || cid_add) { |
| 1082 | struct acpi_compatible_id_list *list; |
| 1083 | int size = 0; |
| 1084 | int count = 0; |
| 1085 | |
| 1086 | if (cid_list) { |
| 1087 | size = cid_list->size; |
| 1088 | } else if (cid_add) { |
| 1089 | size = sizeof(struct acpi_compatible_id_list); |
| 1090 | cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size); |
| 1091 | if (!cid_list) { |
| 1092 | printk(KERN_ERR "Memory allocation error\n"); |
| 1093 | kfree(buffer.pointer); |
| 1094 | return; |
| 1095 | } else { |
| 1096 | cid_list->count = 0; |
| 1097 | cid_list->size = size; |
| 1098 | } |
| 1099 | } |
| 1100 | if (cid_add) |
| 1101 | size += sizeof(struct acpi_compatible_id); |
| 1102 | list = kmalloc(size, GFP_KERNEL); |
| 1103 | |
| 1104 | if (list) { |
| 1105 | if (cid_list) { |
| 1106 | memcpy(list, cid_list, cid_list->size); |
| 1107 | count = cid_list->count; |
| 1108 | } |
| 1109 | if (cid_add) { |
| 1110 | strncpy(list->id[count].value, cid_add, |
| 1111 | ACPI_MAX_CID_LENGTH); |
| 1112 | count++; |
| 1113 | device->flags.compatible_ids = 1; |
| 1114 | } |
| 1115 | list->size = size; |
| 1116 | list->count = count; |
| 1117 | device->pnp.cid_list = list; |
| 1118 | } else |
Len Brown | 87ecd5c | 2008-01-01 14:00:00 -0500 | [diff] [blame] | 1119 | printk(KERN_ERR PREFIX "Memory allocation error\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Len Brown | 02438d8 | 2006-06-30 03:19:10 -0400 | [diff] [blame] | 1122 | kfree(buffer.pointer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1125 | static int acpi_device_set_context(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | { |
| 1127 | acpi_status status = AE_OK; |
| 1128 | int result = 0; |
| 1129 | /* |
| 1130 | * Context |
| 1131 | * ------- |
| 1132 | * Attach this 'struct acpi_device' to the ACPI object. This makes |
| 1133 | * resolutions from handle->device very efficient. Note that we need |
| 1134 | * to be careful with fixed-feature devices as they all attach to the |
| 1135 | * root object. |
| 1136 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1137 | if (type != ACPI_BUS_TYPE_POWER_BUTTON && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1138 | type != ACPI_BUS_TYPE_SLEEP_BUTTON) { |
| 1139 | status = acpi_attach_data(device->handle, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1140 | acpi_bus_data_handler, device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | |
| 1142 | if (ACPI_FAILURE(status)) { |
Len Brown | 87ecd5c | 2008-01-01 14:00:00 -0500 | [diff] [blame] | 1143 | printk(KERN_ERR PREFIX "Error attaching device data\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | result = -ENODEV; |
| 1145 | } |
| 1146 | } |
| 1147 | return result; |
| 1148 | } |
| 1149 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1150 | static int acpi_bus_remove(struct acpi_device *dev, int rmdevice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | if (!dev) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1153 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
Li Shaohua | 9633357 | 2006-12-07 20:56:46 +0800 | [diff] [blame] | 1155 | dev->removal_type = ACPI_BUS_REMOVAL_EJECT; |
Patrick Mochel | 1890a97 | 2006-12-07 20:56:31 +0800 | [diff] [blame] | 1156 | device_release_driver(&dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
| 1158 | if (!rmdevice) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1159 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
Rui Zhang | 2786f6e | 2006-12-21 02:21:13 -0500 | [diff] [blame] | 1161 | /* |
| 1162 | * unbind _ADR-Based Devices when hot removal |
| 1163 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | if (dev->flags.bus_address) { |
| 1165 | if ((dev->parent) && (dev->parent->ops.unbind)) |
| 1166 | dev->parent->ops.unbind(dev); |
| 1167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT); |
| 1169 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1170 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1173 | static int |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1174 | acpi_is_child_device(struct acpi_device *device, |
| 1175 | int (*matcher)(struct acpi_device *)) |
| 1176 | { |
| 1177 | int result = -ENODEV; |
| 1178 | |
| 1179 | do { |
| 1180 | if (ACPI_SUCCESS(matcher(device))) |
| 1181 | return AE_OK; |
| 1182 | } while ((device = device->parent)); |
| 1183 | |
| 1184 | return result; |
| 1185 | } |
| 1186 | |
| 1187 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1188 | acpi_add_single_object(struct acpi_device **child, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1189 | struct acpi_device *parent, acpi_handle handle, int type, |
| 1190 | struct acpi_bus_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1192 | int result = 0; |
| 1193 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
| 1196 | if (!child) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1197 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | |
Burman Yan | 36bcbec | 2006-12-19 12:56:11 -0800 | [diff] [blame] | 1199 | device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | if (!device) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 1201 | printk(KERN_ERR PREFIX "Memory allocation error\n"); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1202 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | |
| 1205 | device->handle = handle; |
| 1206 | device->parent = parent; |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1207 | device->bus_ops = *ops; /* workround for not call .start */ |
| 1208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1210 | acpi_device_get_busid(device, handle, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | |
| 1212 | /* |
| 1213 | * Flags |
| 1214 | * ----- |
| 1215 | * Get prior to calling acpi_bus_get_status() so we know whether |
| 1216 | * or not _STA is present. Note that we only look for object |
| 1217 | * handles -- cannot evaluate objects until we know the device is |
| 1218 | * present and properly initialized. |
| 1219 | */ |
| 1220 | result = acpi_bus_get_flags(device); |
| 1221 | if (result) |
| 1222 | goto end; |
| 1223 | |
| 1224 | /* |
| 1225 | * Status |
| 1226 | * ------ |
Keiichiro Tokunaga | 6940fab | 2005-03-30 23:15:47 -0500 | [diff] [blame] | 1227 | * See if the device is present. We always assume that non-Device |
| 1228 | * and non-Processor objects (e.g. thermal zones, power resources, |
| 1229 | * etc.) are present, functioning, etc. (at least when parent object |
| 1230 | * is present). Note that _STA has a different meaning for some |
| 1231 | * objects (e.g. power resources) so we need to be careful how we use |
| 1232 | * it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | */ |
| 1234 | switch (type) { |
Keiichiro Tokunaga | 6940fab | 2005-03-30 23:15:47 -0500 | [diff] [blame] | 1235 | case ACPI_BUS_TYPE_PROCESSOR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | case ACPI_BUS_TYPE_DEVICE: |
| 1237 | result = acpi_bus_get_status(device); |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1238 | if (ACPI_FAILURE(result)) { |
| 1239 | result = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | goto end; |
| 1241 | } |
Frank Seidel | 3620f2f | 2007-12-07 13:20:34 +0100 | [diff] [blame] | 1242 | if (!device->status.present) { |
| 1243 | /* Bay and dock should be handled even if absent */ |
| 1244 | if (!ACPI_SUCCESS( |
| 1245 | acpi_is_child_device(device, acpi_bay_match)) && |
| 1246 | !ACPI_SUCCESS( |
| 1247 | acpi_is_child_device(device, acpi_dock_match))) { |
| 1248 | result = -ENODEV; |
| 1249 | goto end; |
| 1250 | } |
| 1251 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | break; |
| 1253 | default: |
Bjorn Helgaas | 0c0e892 | 2007-04-25 14:20:58 -0400 | [diff] [blame] | 1254 | STRUCT_TO_INT(device->status) = |
| 1255 | ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | |
| 1256 | ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | break; |
| 1258 | } |
| 1259 | |
| 1260 | /* |
| 1261 | * Initialize Device |
| 1262 | * ----------------- |
| 1263 | * TBD: Synch with Core's enumeration/initialization process. |
| 1264 | */ |
| 1265 | |
| 1266 | /* |
| 1267 | * Hardware ID, Unique ID, & Bus Address |
| 1268 | * ------------------------------------- |
| 1269 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1270 | acpi_device_set_id(device, parent, handle, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
| 1272 | /* |
| 1273 | * Power Management |
| 1274 | * ---------------- |
| 1275 | */ |
| 1276 | if (device->flags.power_manageable) { |
| 1277 | result = acpi_bus_get_power_flags(device); |
| 1278 | if (result) |
| 1279 | goto end; |
| 1280 | } |
| 1281 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1282 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | * Wakeup device management |
| 1284 | *----------------------- |
| 1285 | */ |
| 1286 | if (device->flags.wake_capable) { |
| 1287 | result = acpi_bus_get_wakeup_device_flags(device); |
| 1288 | if (result) |
| 1289 | goto end; |
| 1290 | } |
| 1291 | |
| 1292 | /* |
| 1293 | * Performance Management |
| 1294 | * ---------------------- |
| 1295 | */ |
| 1296 | if (device->flags.performance_manageable) { |
| 1297 | result = acpi_bus_get_perf_flags(device); |
| 1298 | if (result) |
| 1299 | goto end; |
| 1300 | } |
| 1301 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1302 | if ((result = acpi_device_set_context(device, type))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | goto end; |
| 1304 | |
Zhang Rui | e49bd2d | 2006-12-08 17:23:43 +0800 | [diff] [blame] | 1305 | result = acpi_device_register(device, parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | |
| 1307 | /* |
Rui Zhang | 2786f6e | 2006-12-21 02:21:13 -0500 | [diff] [blame] | 1308 | * Bind _ADR-Based Devices when hot add |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | */ |
| 1310 | if (device->flags.bus_address) { |
| 1311 | if (device->parent && device->parent->ops.bind) |
| 1312 | device->parent->ops.bind(device); |
| 1313 | } |
| 1314 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1315 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | if (!result) |
| 1317 | *child = device; |
| 1318 | else { |
Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 1319 | kfree(device->pnp.cid_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | kfree(device); |
| 1321 | } |
| 1322 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1323 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1326 | 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] | 1327 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1328 | acpi_status status = AE_OK; |
| 1329 | struct acpi_device *parent = NULL; |
| 1330 | struct acpi_device *child = NULL; |
| 1331 | acpi_handle phandle = NULL; |
| 1332 | acpi_handle chandle = NULL; |
| 1333 | acpi_object_type type = 0; |
| 1334 | u32 level = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | |
| 1337 | if (!start) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1338 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | |
| 1340 | parent = start; |
| 1341 | phandle = start->handle; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | /* |
| 1344 | * Parse through the ACPI namespace, identify all 'devices', and |
| 1345 | * create a new 'struct acpi_device' for each. |
| 1346 | */ |
| 1347 | while ((level > 0) && parent) { |
| 1348 | |
| 1349 | status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1350 | chandle, &chandle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | |
| 1352 | /* |
| 1353 | * If this scope is exhausted then move our way back up. |
| 1354 | */ |
| 1355 | if (ACPI_FAILURE(status)) { |
| 1356 | level--; |
| 1357 | chandle = phandle; |
| 1358 | acpi_get_parent(phandle, &phandle); |
| 1359 | if (parent->parent) |
| 1360 | parent = parent->parent; |
| 1361 | continue; |
| 1362 | } |
| 1363 | |
| 1364 | status = acpi_get_type(chandle, &type); |
| 1365 | if (ACPI_FAILURE(status)) |
| 1366 | continue; |
| 1367 | |
| 1368 | /* |
| 1369 | * If this is a scope object then parse it (depth-first). |
| 1370 | */ |
| 1371 | if (type == ACPI_TYPE_LOCAL_SCOPE) { |
| 1372 | level++; |
| 1373 | phandle = chandle; |
| 1374 | chandle = NULL; |
| 1375 | continue; |
| 1376 | } |
| 1377 | |
| 1378 | /* |
| 1379 | * We're only interested in objects that we consider 'devices'. |
| 1380 | */ |
| 1381 | switch (type) { |
| 1382 | case ACPI_TYPE_DEVICE: |
| 1383 | type = ACPI_BUS_TYPE_DEVICE; |
| 1384 | break; |
| 1385 | case ACPI_TYPE_PROCESSOR: |
| 1386 | type = ACPI_BUS_TYPE_PROCESSOR; |
| 1387 | break; |
| 1388 | case ACPI_TYPE_THERMAL: |
| 1389 | type = ACPI_BUS_TYPE_THERMAL; |
| 1390 | break; |
| 1391 | case ACPI_TYPE_POWER: |
| 1392 | type = ACPI_BUS_TYPE_POWER; |
| 1393 | break; |
| 1394 | default: |
| 1395 | continue; |
| 1396 | } |
| 1397 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1398 | if (ops->acpi_op_add) |
| 1399 | status = acpi_add_single_object(&child, parent, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1400 | chandle, type, ops); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1401 | else |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1402 | status = acpi_bus_get_device(chandle, &child); |
| 1403 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1404 | if (ACPI_FAILURE(status)) |
| 1405 | continue; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1406 | |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1407 | if (ops->acpi_op_start && !(ops->acpi_op_add)) { |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1408 | status = acpi_start_single_object(child); |
| 1409 | if (ACPI_FAILURE(status)) |
| 1410 | continue; |
| 1411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
| 1413 | /* |
| 1414 | * If the device is present, enabled, and functioning then |
| 1415 | * parse its scope (depth-first). Note that we need to |
| 1416 | * represent absent devices to facilitate PnP notifications |
| 1417 | * -- but only the subtree head (not all of its children, |
| 1418 | * which will be enumerated when the parent is inserted). |
| 1419 | * |
| 1420 | * TBD: Need notifications and other detection mechanisms |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1421 | * in place before we can fully implement this. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | */ |
| 1423 | if (child->status.present) { |
| 1424 | status = acpi_get_next_object(ACPI_TYPE_ANY, chandle, |
| 1425 | NULL, NULL); |
| 1426 | if (ACPI_SUCCESS(status)) { |
| 1427 | level++; |
| 1428 | phandle = chandle; |
| 1429 | chandle = NULL; |
| 1430 | parent = child; |
| 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1435 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1438 | int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1439 | acpi_bus_add(struct acpi_device **child, |
| 1440 | struct acpi_device *parent, acpi_handle handle, int type) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1441 | { |
| 1442 | int result; |
| 1443 | struct acpi_bus_ops ops; |
| 1444 | |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1445 | memset(&ops, 0, sizeof(ops)); |
| 1446 | ops.acpi_op_add = 1; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1447 | |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1448 | result = acpi_add_single_object(child, parent, handle, type, &ops); |
| 1449 | if (!result) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1450 | result = acpi_bus_scan(*child, &ops); |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1451 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1452 | return result; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1453 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1454 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1455 | EXPORT_SYMBOL(acpi_bus_add); |
| 1456 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1457 | int acpi_bus_start(struct acpi_device *device) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1458 | { |
| 1459 | int result; |
| 1460 | struct acpi_bus_ops ops; |
| 1461 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1462 | |
| 1463 | if (!device) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1464 | return -EINVAL; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1465 | |
| 1466 | result = acpi_start_single_object(device); |
| 1467 | if (!result) { |
| 1468 | memset(&ops, 0, sizeof(ops)); |
| 1469 | ops.acpi_op_start = 1; |
| 1470 | result = acpi_bus_scan(device, &ops); |
| 1471 | } |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1472 | return result; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1473 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1474 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1475 | EXPORT_SYMBOL(acpi_bus_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
Kristen Accardi | ceaba66 | 2006-02-23 17:56:01 -0800 | [diff] [blame] | 1477 | int acpi_bus_trim(struct acpi_device *start, int rmdevice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1479 | acpi_status status; |
| 1480 | struct acpi_device *parent, *child; |
| 1481 | acpi_handle phandle, chandle; |
| 1482 | acpi_object_type type; |
| 1483 | u32 level = 1; |
| 1484 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1486 | parent = start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | phandle = start->handle; |
| 1488 | child = chandle = NULL; |
| 1489 | |
| 1490 | while ((level > 0) && parent && (!err)) { |
| 1491 | status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1492 | chandle, &chandle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
| 1494 | /* |
| 1495 | * If this scope is exhausted then move our way back up. |
| 1496 | */ |
| 1497 | if (ACPI_FAILURE(status)) { |
| 1498 | level--; |
| 1499 | chandle = phandle; |
| 1500 | acpi_get_parent(phandle, &phandle); |
| 1501 | child = parent; |
| 1502 | parent = parent->parent; |
| 1503 | |
| 1504 | if (level == 0) |
| 1505 | err = acpi_bus_remove(child, rmdevice); |
| 1506 | else |
| 1507 | err = acpi_bus_remove(child, 1); |
| 1508 | |
| 1509 | continue; |
| 1510 | } |
| 1511 | |
| 1512 | status = acpi_get_type(chandle, &type); |
| 1513 | if (ACPI_FAILURE(status)) { |
| 1514 | continue; |
| 1515 | } |
| 1516 | /* |
| 1517 | * If there is a device corresponding to chandle then |
| 1518 | * parse it (depth-first). |
| 1519 | */ |
| 1520 | if (acpi_bus_get_device(chandle, &child) == 0) { |
| 1521 | level++; |
| 1522 | phandle = chandle; |
| 1523 | chandle = NULL; |
| 1524 | parent = child; |
| 1525 | } |
| 1526 | continue; |
| 1527 | } |
| 1528 | return err; |
| 1529 | } |
Kristen Accardi | ceaba66 | 2006-02-23 17:56:01 -0800 | [diff] [blame] | 1530 | EXPORT_SYMBOL_GPL(acpi_bus_trim); |
| 1531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1533 | static int acpi_bus_scan_fixed(struct acpi_device *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1535 | int result = 0; |
| 1536 | struct acpi_device *device = NULL; |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1537 | struct acpi_bus_ops ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
| 1539 | if (!root) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1540 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1542 | memset(&ops, 0, sizeof(ops)); |
| 1543 | ops.acpi_op_add = 1; |
| 1544 | ops.acpi_op_start = 1; |
| 1545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | /* |
| 1547 | * Enumerate all fixed-feature devices. |
| 1548 | */ |
Alexey Starikovskiy | cee324b | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 1549 | if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) { |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1550 | result = acpi_add_single_object(&device, acpi_root, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1551 | NULL, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1552 | ACPI_BUS_TYPE_POWER_BUTTON, |
| 1553 | &ops); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1554 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | |
Alexey Starikovskiy | cee324b | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 1556 | if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) { |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1557 | result = acpi_add_single_object(&device, acpi_root, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1558 | NULL, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1559 | ACPI_BUS_TYPE_SLEEP_BUTTON, |
| 1560 | &ops); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1561 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1563 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
Alexey Starikovskiy | c04209a | 2008-01-01 14:12:55 -0500 | [diff] [blame] | 1566 | int __init acpi_boot_ec_enable(void); |
| 1567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | static int __init acpi_scan_init(void) |
| 1569 | { |
| 1570 | int result; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1571 | struct acpi_bus_ops ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | |
| 1574 | if (acpi_disabled) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1575 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1577 | memset(&ops, 0, sizeof(ops)); |
| 1578 | ops.acpi_op_add = 1; |
| 1579 | ops.acpi_op_start = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | |
Patrick Mochel | 5b32726 | 2006-05-10 10:33:00 -0400 | [diff] [blame] | 1581 | result = bus_register(&acpi_bus_type); |
| 1582 | if (result) { |
| 1583 | /* We don't want to quit even if we failed to add suspend/resume */ |
| 1584 | printk(KERN_ERR PREFIX "Could not register bus type\n"); |
| 1585 | } |
| 1586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | /* |
| 1588 | * Create the root device in the bus's device tree |
| 1589 | */ |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1590 | result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT, |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1591 | ACPI_BUS_TYPE_SYSTEM, &ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | if (result) |
| 1593 | goto Done; |
| 1594 | |
| 1595 | /* |
| 1596 | * Enumerate devices in the ACPI namespace. |
| 1597 | */ |
| 1598 | result = acpi_bus_scan_fixed(acpi_root); |
Alexey Starikovskiy | c04209a | 2008-01-01 14:12:55 -0500 | [diff] [blame] | 1599 | |
| 1600 | /* EC region might be needed at bus_scan, so enable it now */ |
| 1601 | acpi_boot_ec_enable(); |
| 1602 | |
Li Shaohua | c4168bf | 2006-12-07 20:56:41 +0800 | [diff] [blame] | 1603 | if (!result) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1604 | result = acpi_bus_scan(acpi_root, &ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | |
| 1606 | if (result) |
| 1607 | acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL); |
| 1608 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1609 | Done: |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1610 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | subsys_initcall(acpi_scan_init); |