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