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