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> |
| 7 | #include <linux/acpi.h> |
| 8 | |
| 9 | #include <acpi/acpi_drivers.h> |
| 10 | #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #define _COMPONENT ACPI_BUS_COMPONENT |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 13 | ACPI_MODULE_NAME("scan") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #define STRUCT_TO_INT(s) (*((int*)&s)) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 15 | extern struct acpi_device *acpi_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | #define ACPI_BUS_CLASS "system_bus" |
| 18 | #define ACPI_BUS_HID "ACPI_BUS" |
| 19 | #define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver" |
| 20 | #define ACPI_BUS_DEVICE_NAME "System Bus" |
| 21 | |
| 22 | static LIST_HEAD(acpi_device_list); |
| 23 | DEFINE_SPINLOCK(acpi_device_lock); |
| 24 | LIST_HEAD(acpi_wakeup_device_list); |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 27 | static void acpi_device_release(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 29 | struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj); |
Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 30 | kfree(dev->pnp.cid_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | kfree(dev); |
| 32 | } |
| 33 | |
| 34 | struct acpi_device_attribute { |
| 35 | struct attribute attr; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 36 | ssize_t(*show) (struct acpi_device *, char *); |
| 37 | ssize_t(*store) (struct acpi_device *, const char *, size_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | typedef void acpi_device_sysfs_files(struct kobject *, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 41 | const struct attribute *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | static void setup_sys_fs_device_files(struct acpi_device *dev, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 44 | acpi_device_sysfs_files * func); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | #define create_sysfs_device_files(dev) \ |
| 47 | setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file) |
| 48 | #define remove_sysfs_device_files(dev) \ |
| 49 | setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file) |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #define to_acpi_device(n) container_of(n, struct acpi_device, kobj) |
| 52 | #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr); |
| 53 | |
| 54 | static ssize_t acpi_device_attr_show(struct kobject *kobj, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 55 | struct attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | struct acpi_device *device = to_acpi_device(kobj); |
| 58 | struct acpi_device_attribute *attribute = to_handle_attr(attr); |
Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 59 | return attribute->show ? attribute->show(device, buf) : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | static ssize_t acpi_device_attr_store(struct kobject *kobj, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 62 | struct attribute *attr, const char *buf, |
| 63 | size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
| 65 | struct acpi_device *device = to_acpi_device(kobj); |
| 66 | struct acpi_device_attribute *attribute = to_handle_attr(attr); |
Dmitry Torokhov | 70f2817 | 2005-04-29 01:27:34 -0500 | [diff] [blame] | 67 | return attribute->store ? attribute->store(device, buf, len) : -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static struct sysfs_ops acpi_device_sysfs_ops = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 71 | .show = acpi_device_attr_show, |
| 72 | .store = acpi_device_attr_store, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static struct kobj_type ktype_acpi_ns = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 76 | .sysfs_ops = &acpi_device_sysfs_ops, |
| 77 | .release = acpi_device_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 80 | static int namespace_uevent(struct kset *kset, struct kobject *kobj, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | char **envp, int num_envp, char *buffer, |
| 82 | int buffer_size) |
| 83 | { |
| 84 | struct acpi_device *dev = to_acpi_device(kobj); |
| 85 | int i = 0; |
| 86 | int len = 0; |
| 87 | |
| 88 | if (!dev->driver) |
| 89 | return 0; |
| 90 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 91 | if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, |
| 92 | "PHYSDEVDRIVER=%s", dev->driver->name)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return -ENOMEM; |
| 94 | |
| 95 | envp[i] = NULL; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 100 | static struct kset_uevent_ops namespace_uevent_ops = { |
| 101 | .uevent = &namespace_uevent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | static struct kset acpi_namespace_kset = { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 105 | .kobj = { |
| 106 | .name = "namespace", |
| 107 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .subsys = &acpi_subsys, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 109 | .ktype = &ktype_acpi_ns, |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 110 | .uevent_ops = &namespace_uevent_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 113 | static void acpi_device_register(struct acpi_device *device, |
| 114 | struct acpi_device *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
| 116 | /* |
| 117 | * Linkage |
| 118 | * ------- |
| 119 | * Link this device to its parent and siblings. |
| 120 | */ |
| 121 | INIT_LIST_HEAD(&device->children); |
| 122 | INIT_LIST_HEAD(&device->node); |
| 123 | INIT_LIST_HEAD(&device->g_list); |
| 124 | INIT_LIST_HEAD(&device->wakeup_list); |
| 125 | |
| 126 | spin_lock(&acpi_device_lock); |
| 127 | if (device->parent) { |
| 128 | list_add_tail(&device->node, &device->parent->children); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 129 | list_add_tail(&device->g_list, &device->parent->g_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } else |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 131 | list_add_tail(&device->g_list, &acpi_device_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if (device->wakeup.flags.valid) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 133 | list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | spin_unlock(&acpi_device_lock); |
| 135 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 136 | strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | if (parent) |
| 138 | device->kobj.parent = &parent->kobj; |
| 139 | device->kobj.ktype = &ktype_acpi_ns; |
| 140 | device->kobj.kset = &acpi_namespace_kset; |
| 141 | kobject_register(&device->kobj); |
| 142 | create_sysfs_device_files(device); |
| 143 | } |
| 144 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 145 | static int acpi_device_unregister(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
| 147 | spin_lock(&acpi_device_lock); |
| 148 | if (device->parent) { |
| 149 | list_del(&device->node); |
| 150 | list_del(&device->g_list); |
| 151 | } else |
| 152 | list_del(&device->g_list); |
| 153 | |
| 154 | list_del(&device->wakeup_list); |
| 155 | |
| 156 | spin_unlock(&acpi_device_lock); |
| 157 | |
| 158 | acpi_detach_data(device->handle, acpi_bus_data_handler); |
| 159 | remove_sysfs_device_files(device); |
| 160 | kobject_unregister(&device->kobj); |
| 161 | return 0; |
| 162 | } |
| 163 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 164 | void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
| 166 | ACPI_FUNCTION_TRACE("acpi_bus_data_handler"); |
| 167 | |
| 168 | /* TBD */ |
| 169 | |
| 170 | return_VOID; |
| 171 | } |
| 172 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 173 | static int acpi_bus_get_power_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 175 | acpi_status status = 0; |
| 176 | acpi_handle handle = NULL; |
| 177 | u32 i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags"); |
| 180 | |
| 181 | /* |
| 182 | * Power Management Flags |
| 183 | */ |
| 184 | status = acpi_get_handle(device->handle, "_PSC", &handle); |
| 185 | if (ACPI_SUCCESS(status)) |
| 186 | device->power.flags.explicit_get = 1; |
| 187 | status = acpi_get_handle(device->handle, "_IRC", &handle); |
| 188 | if (ACPI_SUCCESS(status)) |
| 189 | device->power.flags.inrush_current = 1; |
| 190 | |
| 191 | /* |
| 192 | * Enumerate supported power management states |
| 193 | */ |
| 194 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) { |
| 195 | struct acpi_device_power_state *ps = &device->power.states[i]; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 196 | char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | /* Evaluate "_PRx" to se if power resources are referenced */ |
| 199 | acpi_evaluate_reference(device->handle, object_name, NULL, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 200 | &ps->resources); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | if (ps->resources.count) { |
| 202 | device->power.flags.power_resources = 1; |
| 203 | ps->flags.valid = 1; |
| 204 | } |
| 205 | |
| 206 | /* Evaluate "_PSx" to see if we can do explicit sets */ |
| 207 | object_name[2] = 'S'; |
| 208 | status = acpi_get_handle(device->handle, object_name, &handle); |
| 209 | if (ACPI_SUCCESS(status)) { |
| 210 | ps->flags.explicit_set = 1; |
| 211 | ps->flags.valid = 1; |
| 212 | } |
| 213 | |
| 214 | /* State is valid if we have some power control */ |
| 215 | if (ps->resources.count || ps->flags.explicit_set) |
| 216 | ps->flags.valid = 1; |
| 217 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 218 | ps->power = -1; /* Unknown - driver assigned */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | ps->latency = -1; /* Unknown - driver assigned */ |
| 220 | } |
| 221 | |
| 222 | /* Set defaults for D0 and D3 states (always valid) */ |
| 223 | device->power.states[ACPI_STATE_D0].flags.valid = 1; |
| 224 | device->power.states[ACPI_STATE_D0].power = 100; |
| 225 | device->power.states[ACPI_STATE_D3].flags.valid = 1; |
| 226 | device->power.states[ACPI_STATE_D3].power = 0; |
| 227 | |
| 228 | /* TBD: System wake support and resource requirements. */ |
| 229 | |
| 230 | device->power.state = ACPI_STATE_UNKNOWN; |
| 231 | |
| 232 | return_VALUE(0); |
| 233 | } |
| 234 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 235 | int acpi_match_ids(struct acpi_device *device, char *ids) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | if (device->flags.hardware_id) |
| 238 | if (strstr(ids, device->pnp.hardware_id)) |
Bjorn Helgaas | 1a36561 | 2006-03-28 17:04:00 -0500 | [diff] [blame^] | 239 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | if (device->flags.compatible_ids) { |
| 242 | struct acpi_compatible_id_list *cid_list = device->pnp.cid_list; |
| 243 | int i; |
| 244 | |
| 245 | /* compare multiple _CID entries against driver ids */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 246 | for (i = 0; i < cid_list->count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | if (strstr(ids, cid_list->id[i].value)) |
Bjorn Helgaas | 1a36561 | 2006-03-28 17:04:00 -0500 | [diff] [blame^] | 248 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
Bjorn Helgaas | 1a36561 | 2006-03-28 17:04:00 -0500 | [diff] [blame^] | 251 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 255 | acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device, |
| 256 | union acpi_object *package) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 258 | int i = 0; |
| 259 | union acpi_object *element = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | if (!device || !package || (package->package.count < 2)) |
| 262 | return AE_BAD_PARAMETER; |
| 263 | |
| 264 | element = &(package->package.elements[0]); |
| 265 | if (!element) |
| 266 | return AE_BAD_PARAMETER; |
| 267 | if (element->type == ACPI_TYPE_PACKAGE) { |
| 268 | if ((element->package.count < 2) || |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 269 | (element->package.elements[0].type != |
| 270 | ACPI_TYPE_LOCAL_REFERENCE) |
| 271 | || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | return AE_BAD_DATA; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 273 | device->wakeup.gpe_device = |
| 274 | element->package.elements[0].reference.handle; |
| 275 | device->wakeup.gpe_number = |
| 276 | (u32) element->package.elements[1].integer.value; |
| 277 | } else if (element->type == ACPI_TYPE_INTEGER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | device->wakeup.gpe_number = element->integer.value; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 279 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return AE_BAD_DATA; |
| 281 | |
| 282 | element = &(package->package.elements[1]); |
| 283 | if (element->type != ACPI_TYPE_INTEGER) { |
| 284 | return AE_BAD_DATA; |
| 285 | } |
| 286 | device->wakeup.sleep_state = element->integer.value; |
| 287 | |
| 288 | if ((package->package.count - 2) > ACPI_MAX_HANDLES) { |
| 289 | return AE_NO_MEMORY; |
| 290 | } |
| 291 | device->wakeup.resources.count = package->package.count - 2; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 292 | for (i = 0; i < device->wakeup.resources.count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | element = &(package->package.elements[i + 2]); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 294 | if (element->type != ACPI_TYPE_ANY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | return AE_BAD_DATA; |
| 296 | } |
| 297 | |
| 298 | device->wakeup.resources.handles[i] = element->reference.handle; |
| 299 | } |
| 300 | |
| 301 | return AE_OK; |
| 302 | } |
| 303 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 304 | static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 306 | acpi_status status = 0; |
| 307 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 308 | union acpi_object *package = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
| 310 | ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags"); |
| 311 | |
| 312 | /* _PRW */ |
| 313 | status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer); |
| 314 | if (ACPI_FAILURE(status)) { |
| 315 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n")); |
| 316 | goto end; |
| 317 | } |
| 318 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 319 | package = (union acpi_object *)buffer.pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | status = acpi_bus_extract_wakeup_device_power_package(device, package); |
| 321 | if (ACPI_FAILURE(status)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 322 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
| 323 | "Error extracting _PRW package\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | goto end; |
| 325 | } |
| 326 | |
| 327 | acpi_os_free(buffer.pointer); |
| 328 | |
| 329 | device->wakeup.flags.valid = 1; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 330 | /* Power button, Lid switch always enable wakeup */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E")) |
| 332 | device->wakeup.flags.run_wake = 1; |
| 333 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 334 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | if (ACPI_FAILURE(status)) |
| 336 | device->flags.wake_capable = 0; |
| 337 | return_VALUE(0); |
| 338 | } |
| 339 | |
| 340 | /* -------------------------------------------------------------------------- |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 341 | ACPI sysfs device file support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | -------------------------------------------------------------------------- */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 343 | static ssize_t acpi_eject_store(struct acpi_device *device, |
| 344 | const char *buf, size_t count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \ |
| 347 | static struct acpi_device_attribute acpi_device_attr_##_name = \ |
| 348 | __ATTR(_name, _mode, _show, _store) |
| 349 | |
| 350 | ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store); |
| 351 | |
| 352 | /** |
| 353 | * setup_sys_fs_device_files - sets up the device files under device namespace |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 354 | * @dev: acpi_device object |
| 355 | * @func: function pointer to create or destroy the device file |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | */ |
| 357 | static void |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 358 | setup_sys_fs_device_files(struct acpi_device *dev, |
| 359 | acpi_device_sysfs_files * func) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 361 | acpi_status status; |
| 362 | acpi_handle temp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
| 364 | /* |
| 365 | * If device has _EJ0, 'eject' file is created that is used to trigger |
| 366 | * hot-removal function from userland. |
| 367 | */ |
| 368 | status = acpi_get_handle(dev->handle, "_EJ0", &temp); |
| 369 | if (ACPI_SUCCESS(status)) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 370 | (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 373 | static int acpi_eject_operation(acpi_handle handle, int lockable) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
| 375 | struct acpi_object_list arg_list; |
| 376 | union acpi_object arg; |
| 377 | acpi_status status = AE_OK; |
| 378 | |
| 379 | /* |
| 380 | * TBD: evaluate _PS3? |
| 381 | */ |
| 382 | |
| 383 | if (lockable) { |
| 384 | arg_list.count = 1; |
| 385 | arg_list.pointer = &arg; |
| 386 | arg.type = ACPI_TYPE_INTEGER; |
| 387 | arg.integer.value = 0; |
| 388 | acpi_evaluate_object(handle, "_LCK", &arg_list, NULL); |
| 389 | } |
| 390 | |
| 391 | arg_list.count = 1; |
| 392 | arg_list.pointer = &arg; |
| 393 | arg.type = ACPI_TYPE_INTEGER; |
| 394 | arg.integer.value = 1; |
| 395 | |
| 396 | /* |
| 397 | * TBD: _EJD support. |
| 398 | */ |
| 399 | |
| 400 | status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL); |
| 401 | if (ACPI_FAILURE(status)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 402 | return (-ENODEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 405 | return (0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | static ssize_t |
| 409 | acpi_eject_store(struct acpi_device *device, const char *buf, size_t count) |
| 410 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 411 | int result; |
| 412 | int ret = count; |
| 413 | int islockable; |
| 414 | acpi_status status; |
| 415 | acpi_handle handle; |
| 416 | acpi_object_type type = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
| 418 | if ((!count) || (buf[0] != '1')) { |
| 419 | return -EINVAL; |
| 420 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | #ifndef FORCE_EJECT |
| 422 | if (device->driver == NULL) { |
| 423 | ret = -ENODEV; |
| 424 | goto err; |
| 425 | } |
| 426 | #endif |
| 427 | status = acpi_get_type(device->handle, &type); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 428 | if (ACPI_FAILURE(status) || (!device->flags.ejectable)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | ret = -ENODEV; |
| 430 | goto err; |
| 431 | } |
| 432 | |
| 433 | islockable = device->flags.lockable; |
| 434 | handle = device->handle; |
| 435 | |
| 436 | if (type == ACPI_TYPE_PROCESSOR) |
| 437 | result = acpi_bus_trim(device, 0); |
| 438 | else |
| 439 | result = acpi_bus_trim(device, 1); |
| 440 | |
| 441 | if (!result) |
| 442 | result = acpi_eject_operation(handle, islockable); |
| 443 | |
| 444 | if (result) { |
| 445 | ret = -EBUSY; |
| 446 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 447 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | return ret; |
| 449 | } |
| 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | /* -------------------------------------------------------------------------- |
| 452 | Performance Management |
| 453 | -------------------------------------------------------------------------- */ |
| 454 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 455 | static int acpi_bus_get_perf_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
| 457 | device->performance.state = ACPI_STATE_UNKNOWN; |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | /* -------------------------------------------------------------------------- |
| 462 | Driver Management |
| 463 | -------------------------------------------------------------------------- */ |
| 464 | |
| 465 | static LIST_HEAD(acpi_bus_drivers); |
| 466 | static DECLARE_MUTEX(acpi_bus_drivers_lock); |
| 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 469 | * acpi_bus_match - match device IDs to driver's supported IDs |
| 470 | * @device: the device that we are trying to match to a driver |
| 471 | * @driver: driver whose device id table is being checked |
| 472 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it |
| 474 | * matches the specified driver's criteria. |
| 475 | */ |
| 476 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 477 | acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | { |
| 479 | if (driver && driver->ops.match) |
| 480 | return driver->ops.match(device, driver); |
| 481 | return acpi_match_ids(device, driver->ids); |
| 482 | } |
| 483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 485 | * acpi_bus_driver_init - add a device to a driver |
| 486 | * @device: the device to add and initialize |
| 487 | * @driver: driver for the device |
| 488 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | * Used to initialize a device via its device driver. Called whenever a |
| 490 | * driver is bound to a device. Invokes the driver's add() and start() ops. |
| 491 | */ |
| 492 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 493 | acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 495 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | |
| 497 | ACPI_FUNCTION_TRACE("acpi_bus_driver_init"); |
| 498 | |
| 499 | if (!device || !driver) |
| 500 | return_VALUE(-EINVAL); |
| 501 | |
| 502 | if (!driver->ops.add) |
| 503 | return_VALUE(-ENOSYS); |
| 504 | |
| 505 | result = driver->ops.add(device); |
| 506 | if (result) { |
| 507 | device->driver = NULL; |
| 508 | acpi_driver_data(device) = NULL; |
| 509 | return_VALUE(result); |
| 510 | } |
| 511 | |
| 512 | device->driver = driver; |
| 513 | |
| 514 | /* |
| 515 | * TBD - Configuration Management: Assign resources to device based |
| 516 | * upon possible configuration and currently allocated resources. |
| 517 | */ |
| 518 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 519 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 520 | "Driver successfully bound to device\n")); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 521 | return_VALUE(0); |
| 522 | } |
| 523 | |
Adrian Bunk | 8713cbe | 2005-09-02 17:16:48 -0400 | [diff] [blame] | 524 | static int acpi_start_single_object(struct acpi_device *device) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 525 | { |
| 526 | int result = 0; |
| 527 | struct acpi_driver *driver; |
| 528 | |
| 529 | ACPI_FUNCTION_TRACE("acpi_start_single_object"); |
| 530 | |
| 531 | if (!(driver = device->driver)) |
| 532 | return_VALUE(0); |
| 533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (driver->ops.start) { |
| 535 | result = driver->ops.start(device); |
| 536 | if (result && driver->ops.remove) |
| 537 | driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 540 | return_VALUE(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 543 | static int acpi_driver_attach(struct acpi_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 545 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | int count = 0; |
| 547 | |
| 548 | ACPI_FUNCTION_TRACE("acpi_driver_attach"); |
| 549 | |
| 550 | spin_lock(&acpi_device_lock); |
| 551 | list_for_each_safe(node, next, &acpi_device_list) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 552 | struct acpi_device *dev = |
| 553 | container_of(node, struct acpi_device, g_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
| 555 | if (dev->driver || !dev->status.present) |
| 556 | continue; |
| 557 | spin_unlock(&acpi_device_lock); |
| 558 | |
| 559 | if (!acpi_bus_match(dev, drv)) { |
| 560 | if (!acpi_bus_driver_init(dev, drv)) { |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 561 | acpi_start_single_object(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | atomic_inc(&drv->references); |
| 563 | count++; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 564 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 565 | "Found driver [%s] for device [%s]\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | drv->name, dev->pnp.bus_id)); |
| 567 | } |
| 568 | } |
| 569 | spin_lock(&acpi_device_lock); |
| 570 | } |
| 571 | spin_unlock(&acpi_device_lock); |
| 572 | return_VALUE(count); |
| 573 | } |
| 574 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 575 | static int acpi_driver_detach(struct acpi_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 577 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | ACPI_FUNCTION_TRACE("acpi_driver_detach"); |
| 580 | |
| 581 | spin_lock(&acpi_device_lock); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 582 | list_for_each_safe(node, next, &acpi_device_list) { |
| 583 | struct acpi_device *dev = |
| 584 | container_of(node, struct acpi_device, g_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | if (dev->driver == drv) { |
| 587 | spin_unlock(&acpi_device_lock); |
| 588 | if (drv->ops.remove) |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 589 | drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | spin_lock(&acpi_device_lock); |
| 591 | dev->driver = NULL; |
| 592 | dev->driver_data = NULL; |
| 593 | atomic_dec(&drv->references); |
| 594 | } |
| 595 | } |
| 596 | spin_unlock(&acpi_device_lock); |
| 597 | return_VALUE(0); |
| 598 | } |
| 599 | |
| 600 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 601 | * acpi_bus_register_driver - register a driver with the ACPI bus |
| 602 | * @driver: driver being registered |
| 603 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | * Registers a driver with the ACPI bus. Searches the namespace for all |
| 605 | * devices that match the driver's criteria and binds. Returns the |
| 606 | * number of devices that were claimed by the driver, or a negative |
| 607 | * error status for failure. |
| 608 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 609 | int acpi_bus_register_driver(struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
| 611 | int count; |
| 612 | |
| 613 | ACPI_FUNCTION_TRACE("acpi_bus_register_driver"); |
| 614 | |
| 615 | if (acpi_disabled) |
| 616 | return_VALUE(-ENODEV); |
| 617 | |
| 618 | if (!driver) |
| 619 | return_VALUE(-EINVAL); |
| 620 | |
| 621 | spin_lock(&acpi_device_lock); |
| 622 | list_add_tail(&driver->node, &acpi_bus_drivers); |
| 623 | spin_unlock(&acpi_device_lock); |
| 624 | count = acpi_driver_attach(driver); |
| 625 | |
| 626 | return_VALUE(count); |
| 627 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 629 | EXPORT_SYMBOL(acpi_bus_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
| 631 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 632 | * acpi_bus_unregister_driver - unregisters a driver with the APIC bus |
| 633 | * @driver: driver to unregister |
| 634 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | * Unregisters a driver with the ACPI bus. Searches the namespace for all |
| 636 | * devices that match the driver's criteria and unbinds. |
| 637 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 638 | int acpi_bus_unregister_driver(struct acpi_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | ACPI_FUNCTION_TRACE("acpi_bus_unregister_driver"); |
| 641 | |
Bjorn Helgaas | 1a36561 | 2006-03-28 17:04:00 -0500 | [diff] [blame^] | 642 | if (!driver) |
| 643 | return_VALUE(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Bjorn Helgaas | 1a36561 | 2006-03-28 17:04:00 -0500 | [diff] [blame^] | 645 | acpi_driver_detach(driver); |
| 646 | |
| 647 | if (!atomic_read(&driver->references)) { |
| 648 | spin_lock(&acpi_device_lock); |
| 649 | list_del_init(&driver->node); |
| 650 | spin_unlock(&acpi_device_lock); |
| 651 | } |
| 652 | return_VALUE(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | EXPORT_SYMBOL(acpi_bus_unregister_driver); |
| 656 | |
| 657 | /** |
Randy Dunlap | d758a8f | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 658 | * acpi_bus_find_driver - check if there is a driver installed for the device |
| 659 | * @device: device that we are trying to find a supporting driver for |
| 660 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | * Parses the list of registered drivers looking for a driver applicable for |
| 662 | * the specified device. |
| 663 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 664 | static int acpi_bus_find_driver(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 666 | int result = 0; |
| 667 | struct list_head *node, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | |
| 669 | ACPI_FUNCTION_TRACE("acpi_bus_find_driver"); |
| 670 | |
| 671 | spin_lock(&acpi_device_lock); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 672 | list_for_each_safe(node, next, &acpi_bus_drivers) { |
| 673 | struct acpi_driver *driver = |
| 674 | container_of(node, struct acpi_driver, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
| 676 | atomic_inc(&driver->references); |
| 677 | spin_unlock(&acpi_device_lock); |
| 678 | if (!acpi_bus_match(device, driver)) { |
| 679 | result = acpi_bus_driver_init(device, driver); |
| 680 | if (!result) |
| 681 | goto Done; |
| 682 | } |
| 683 | atomic_dec(&driver->references); |
| 684 | spin_lock(&acpi_device_lock); |
| 685 | } |
| 686 | spin_unlock(&acpi_device_lock); |
| 687 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 688 | Done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | return_VALUE(result); |
| 690 | } |
| 691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | /* -------------------------------------------------------------------------- |
| 693 | Device Enumeration |
| 694 | -------------------------------------------------------------------------- */ |
| 695 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 696 | static int acpi_bus_get_flags(struct acpi_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 698 | acpi_status status = AE_OK; |
| 699 | acpi_handle temp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | |
| 701 | ACPI_FUNCTION_TRACE("acpi_bus_get_flags"); |
| 702 | |
| 703 | /* Presence of _STA indicates 'dynamic_status' */ |
| 704 | status = acpi_get_handle(device->handle, "_STA", &temp); |
| 705 | if (ACPI_SUCCESS(status)) |
| 706 | device->flags.dynamic_status = 1; |
| 707 | |
| 708 | /* Presence of _CID indicates 'compatible_ids' */ |
| 709 | status = acpi_get_handle(device->handle, "_CID", &temp); |
| 710 | if (ACPI_SUCCESS(status)) |
| 711 | device->flags.compatible_ids = 1; |
| 712 | |
| 713 | /* Presence of _RMV indicates 'removable' */ |
| 714 | status = acpi_get_handle(device->handle, "_RMV", &temp); |
| 715 | if (ACPI_SUCCESS(status)) |
| 716 | device->flags.removable = 1; |
| 717 | |
| 718 | /* Presence of _EJD|_EJ0 indicates 'ejectable' */ |
| 719 | status = acpi_get_handle(device->handle, "_EJD", &temp); |
| 720 | if (ACPI_SUCCESS(status)) |
| 721 | device->flags.ejectable = 1; |
| 722 | else { |
| 723 | status = acpi_get_handle(device->handle, "_EJ0", &temp); |
| 724 | if (ACPI_SUCCESS(status)) |
| 725 | device->flags.ejectable = 1; |
| 726 | } |
| 727 | |
| 728 | /* Presence of _LCK indicates 'lockable' */ |
| 729 | status = acpi_get_handle(device->handle, "_LCK", &temp); |
| 730 | if (ACPI_SUCCESS(status)) |
| 731 | device->flags.lockable = 1; |
| 732 | |
| 733 | /* Presence of _PS0|_PR0 indicates 'power manageable' */ |
| 734 | status = acpi_get_handle(device->handle, "_PS0", &temp); |
| 735 | if (ACPI_FAILURE(status)) |
| 736 | status = acpi_get_handle(device->handle, "_PR0", &temp); |
| 737 | if (ACPI_SUCCESS(status)) |
| 738 | device->flags.power_manageable = 1; |
| 739 | |
| 740 | /* Presence of _PRW indicates wake capable */ |
| 741 | status = acpi_get_handle(device->handle, "_PRW", &temp); |
| 742 | if (ACPI_SUCCESS(status)) |
| 743 | device->flags.wake_capable = 1; |
| 744 | |
| 745 | /* TBD: Peformance management */ |
| 746 | |
| 747 | return_VALUE(0); |
| 748 | } |
| 749 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 750 | static void acpi_device_get_busid(struct acpi_device *device, |
| 751 | acpi_handle handle, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 753 | char bus_id[5] = { '?', 0 }; |
| 754 | struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; |
| 755 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | |
| 757 | /* |
| 758 | * Bus ID |
| 759 | * ------ |
| 760 | * The device's Bus ID is simply the object name. |
| 761 | * TBD: Shouldn't this value be unique (within the ACPI namespace)? |
| 762 | */ |
| 763 | switch (type) { |
| 764 | case ACPI_BUS_TYPE_SYSTEM: |
| 765 | strcpy(device->pnp.bus_id, "ACPI"); |
| 766 | break; |
| 767 | case ACPI_BUS_TYPE_POWER_BUTTON: |
| 768 | strcpy(device->pnp.bus_id, "PWRF"); |
| 769 | break; |
| 770 | case ACPI_BUS_TYPE_SLEEP_BUTTON: |
| 771 | strcpy(device->pnp.bus_id, "SLPF"); |
| 772 | break; |
| 773 | default: |
| 774 | acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); |
| 775 | /* Clean up trailing underscores (if any) */ |
| 776 | for (i = 3; i > 1; i--) { |
| 777 | if (bus_id[i] == '_') |
| 778 | bus_id[i] = '\0'; |
| 779 | else |
| 780 | break; |
| 781 | } |
| 782 | strcpy(device->pnp.bus_id, bus_id); |
| 783 | break; |
| 784 | } |
| 785 | } |
| 786 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 787 | static void acpi_device_set_id(struct acpi_device *device, |
| 788 | struct acpi_device *parent, acpi_handle handle, |
| 789 | int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 791 | struct acpi_device_info *info; |
| 792 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 793 | char *hid = NULL; |
| 794 | char *uid = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | struct acpi_compatible_id_list *cid_list = NULL; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 796 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
| 798 | switch (type) { |
| 799 | case ACPI_BUS_TYPE_DEVICE: |
| 800 | status = acpi_get_object_info(handle, &buffer); |
| 801 | if (ACPI_FAILURE(status)) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 802 | printk("%s: Error reading device info\n", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | return; |
| 804 | } |
| 805 | |
| 806 | info = buffer.pointer; |
| 807 | if (info->valid & ACPI_VALID_HID) |
| 808 | hid = info->hardware_id.value; |
| 809 | if (info->valid & ACPI_VALID_UID) |
| 810 | uid = info->unique_id.value; |
| 811 | if (info->valid & ACPI_VALID_CID) |
| 812 | cid_list = &info->compatibility_id; |
| 813 | if (info->valid & ACPI_VALID_ADR) { |
| 814 | device->pnp.bus_address = info->address; |
| 815 | device->flags.bus_address = 1; |
| 816 | } |
| 817 | break; |
| 818 | case ACPI_BUS_TYPE_POWER: |
| 819 | hid = ACPI_POWER_HID; |
| 820 | break; |
| 821 | case ACPI_BUS_TYPE_PROCESSOR: |
| 822 | hid = ACPI_PROCESSOR_HID; |
| 823 | break; |
| 824 | case ACPI_BUS_TYPE_SYSTEM: |
| 825 | hid = ACPI_SYSTEM_HID; |
| 826 | break; |
| 827 | case ACPI_BUS_TYPE_THERMAL: |
| 828 | hid = ACPI_THERMAL_HID; |
| 829 | break; |
| 830 | case ACPI_BUS_TYPE_POWER_BUTTON: |
| 831 | hid = ACPI_BUTTON_HID_POWERF; |
| 832 | break; |
| 833 | case ACPI_BUS_TYPE_SLEEP_BUTTON: |
| 834 | hid = ACPI_BUTTON_HID_SLEEPF; |
| 835 | break; |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * \_SB |
| 840 | * ---- |
| 841 | * Fix for the system root bus device -- the only root-level device. |
| 842 | */ |
Bob Moore | c51a4de | 2005-11-17 13:07:00 -0500 | [diff] [blame] | 843 | if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | hid = ACPI_BUS_HID; |
| 845 | strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME); |
| 846 | strcpy(device->pnp.device_class, ACPI_BUS_CLASS); |
| 847 | } |
| 848 | |
| 849 | if (hid) { |
| 850 | strcpy(device->pnp.hardware_id, hid); |
| 851 | device->flags.hardware_id = 1; |
| 852 | } |
| 853 | if (uid) { |
| 854 | strcpy(device->pnp.unique_id, uid); |
| 855 | device->flags.unique_id = 1; |
| 856 | } |
| 857 | if (cid_list) { |
| 858 | device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL); |
| 859 | if (device->pnp.cid_list) |
| 860 | memcpy(device->pnp.cid_list, cid_list, cid_list->size); |
| 861 | else |
| 862 | printk(KERN_ERR "Memory allocation error\n"); |
| 863 | } |
| 864 | |
| 865 | acpi_os_free(buffer.pointer); |
| 866 | } |
| 867 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 868 | static int acpi_device_set_context(struct acpi_device *device, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | { |
| 870 | acpi_status status = AE_OK; |
| 871 | int result = 0; |
| 872 | /* |
| 873 | * Context |
| 874 | * ------- |
| 875 | * Attach this 'struct acpi_device' to the ACPI object. This makes |
| 876 | * resolutions from handle->device very efficient. Note that we need |
| 877 | * to be careful with fixed-feature devices as they all attach to the |
| 878 | * root object. |
| 879 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 880 | if (type != ACPI_BUS_TYPE_POWER_BUTTON && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | type != ACPI_BUS_TYPE_SLEEP_BUTTON) { |
| 882 | status = acpi_attach_data(device->handle, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 883 | acpi_bus_data_handler, device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | if (ACPI_FAILURE(status)) { |
| 886 | printk("Error attaching device data\n"); |
| 887 | result = -ENODEV; |
| 888 | } |
| 889 | } |
| 890 | return result; |
| 891 | } |
| 892 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 893 | static void acpi_device_get_debug_info(struct acpi_device *device, |
| 894 | acpi_handle handle, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | { |
| 896 | #ifdef CONFIG_ACPI_DEBUG_OUTPUT |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 897 | char *type_string = NULL; |
| 898 | char name[80] = { '?', '\0' }; |
| 899 | struct acpi_buffer buffer = { sizeof(name), name }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | |
| 901 | switch (type) { |
| 902 | case ACPI_BUS_TYPE_DEVICE: |
| 903 | type_string = "Device"; |
| 904 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 905 | break; |
| 906 | case ACPI_BUS_TYPE_POWER: |
| 907 | type_string = "Power Resource"; |
| 908 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 909 | break; |
| 910 | case ACPI_BUS_TYPE_PROCESSOR: |
| 911 | type_string = "Processor"; |
| 912 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 913 | break; |
| 914 | case ACPI_BUS_TYPE_SYSTEM: |
| 915 | type_string = "System"; |
| 916 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 917 | break; |
| 918 | case ACPI_BUS_TYPE_THERMAL: |
| 919 | type_string = "Thermal Zone"; |
| 920 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); |
| 921 | break; |
| 922 | case ACPI_BUS_TYPE_POWER_BUTTON: |
| 923 | type_string = "Power Button"; |
| 924 | sprintf(name, "PWRB"); |
| 925 | break; |
| 926 | case ACPI_BUS_TYPE_SLEEP_BUTTON: |
| 927 | type_string = "Sleep Button"; |
| 928 | sprintf(name, "SLPB"); |
| 929 | break; |
| 930 | } |
| 931 | |
| 932 | printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 933 | #endif /*CONFIG_ACPI_DEBUG_OUTPUT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 936 | static int acpi_bus_remove(struct acpi_device *dev, int rmdevice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 938 | int result = 0; |
| 939 | struct acpi_driver *driver; |
| 940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | ACPI_FUNCTION_TRACE("acpi_bus_remove"); |
| 942 | |
| 943 | if (!dev) |
| 944 | return_VALUE(-EINVAL); |
| 945 | |
| 946 | driver = dev->driver; |
| 947 | |
| 948 | if ((driver) && (driver->ops.remove)) { |
| 949 | |
| 950 | if (driver->ops.stop) { |
| 951 | result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT); |
| 952 | if (result) |
| 953 | return_VALUE(result); |
| 954 | } |
| 955 | |
| 956 | result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT); |
| 957 | if (result) { |
| 958 | return_VALUE(result); |
| 959 | } |
| 960 | |
| 961 | atomic_dec(&dev->driver->references); |
| 962 | dev->driver = NULL; |
| 963 | acpi_driver_data(dev) = NULL; |
| 964 | } |
| 965 | |
| 966 | if (!rmdevice) |
| 967 | return_VALUE(0); |
| 968 | |
| 969 | if (dev->flags.bus_address) { |
| 970 | if ((dev->parent) && (dev->parent->ops.unbind)) |
| 971 | dev->parent->ops.unbind(dev); |
| 972 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT); |
| 975 | |
| 976 | return_VALUE(0); |
| 977 | } |
| 978 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 979 | static int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 980 | acpi_add_single_object(struct acpi_device **child, |
| 981 | struct acpi_device *parent, acpi_handle handle, int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 983 | int result = 0; |
| 984 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 986 | ACPI_FUNCTION_TRACE("acpi_add_single_object"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | |
| 988 | if (!child) |
| 989 | return_VALUE(-EINVAL); |
| 990 | |
| 991 | device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL); |
| 992 | if (!device) { |
| 993 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n")); |
| 994 | return_VALUE(-ENOMEM); |
| 995 | } |
| 996 | memset(device, 0, sizeof(struct acpi_device)); |
| 997 | |
| 998 | device->handle = handle; |
| 999 | device->parent = parent; |
| 1000 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1001 | acpi_device_get_busid(device, handle, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
| 1003 | /* |
| 1004 | * Flags |
| 1005 | * ----- |
| 1006 | * Get prior to calling acpi_bus_get_status() so we know whether |
| 1007 | * or not _STA is present. Note that we only look for object |
| 1008 | * handles -- cannot evaluate objects until we know the device is |
| 1009 | * present and properly initialized. |
| 1010 | */ |
| 1011 | result = acpi_bus_get_flags(device); |
| 1012 | if (result) |
| 1013 | goto end; |
| 1014 | |
| 1015 | /* |
| 1016 | * Status |
| 1017 | * ------ |
Keiichiro Tokunaga | 6940fab | 2005-03-30 23:15:47 -0500 | [diff] [blame] | 1018 | * See if the device is present. We always assume that non-Device |
| 1019 | * and non-Processor objects (e.g. thermal zones, power resources, |
| 1020 | * etc.) are present, functioning, etc. (at least when parent object |
| 1021 | * is present). Note that _STA has a different meaning for some |
| 1022 | * objects (e.g. power resources) so we need to be careful how we use |
| 1023 | * it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | */ |
| 1025 | switch (type) { |
Keiichiro Tokunaga | 6940fab | 2005-03-30 23:15:47 -0500 | [diff] [blame] | 1026 | case ACPI_BUS_TYPE_PROCESSOR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | case ACPI_BUS_TYPE_DEVICE: |
| 1028 | result = acpi_bus_get_status(device); |
| 1029 | if (ACPI_FAILURE(result) || !device->status.present) { |
| 1030 | result = -ENOENT; |
| 1031 | goto end; |
| 1032 | } |
| 1033 | break; |
| 1034 | default: |
| 1035 | STRUCT_TO_INT(device->status) = 0x0F; |
| 1036 | break; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * Initialize Device |
| 1041 | * ----------------- |
| 1042 | * TBD: Synch with Core's enumeration/initialization process. |
| 1043 | */ |
| 1044 | |
| 1045 | /* |
| 1046 | * Hardware ID, Unique ID, & Bus Address |
| 1047 | * ------------------------------------- |
| 1048 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1049 | acpi_device_set_id(device, parent, handle, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
| 1051 | /* |
| 1052 | * Power Management |
| 1053 | * ---------------- |
| 1054 | */ |
| 1055 | if (device->flags.power_manageable) { |
| 1056 | result = acpi_bus_get_power_flags(device); |
| 1057 | if (result) |
| 1058 | goto end; |
| 1059 | } |
| 1060 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1061 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | * Wakeup device management |
| 1063 | *----------------------- |
| 1064 | */ |
| 1065 | if (device->flags.wake_capable) { |
| 1066 | result = acpi_bus_get_wakeup_device_flags(device); |
| 1067 | if (result) |
| 1068 | goto end; |
| 1069 | } |
| 1070 | |
| 1071 | /* |
| 1072 | * Performance Management |
| 1073 | * ---------------------- |
| 1074 | */ |
| 1075 | if (device->flags.performance_manageable) { |
| 1076 | result = acpi_bus_get_perf_flags(device); |
| 1077 | if (result) |
| 1078 | goto end; |
| 1079 | } |
| 1080 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1081 | if ((result = acpi_device_set_context(device, type))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | goto end; |
| 1083 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1084 | acpi_device_get_debug_info(device, handle, type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1086 | acpi_device_register(device, parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
| 1088 | /* |
| 1089 | * Bind _ADR-Based Devices |
| 1090 | * ----------------------- |
| 1091 | * If there's a a bus address (_ADR) then we utilize the parent's |
| 1092 | * 'bind' function (if exists) to bind the ACPI- and natively- |
| 1093 | * enumerated device representations. |
| 1094 | */ |
| 1095 | if (device->flags.bus_address) { |
| 1096 | if (device->parent && device->parent->ops.bind) |
| 1097 | device->parent->ops.bind(device); |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * Locate & Attach Driver |
| 1102 | * ---------------------- |
| 1103 | * If there's a hardware id (_HID) or compatible ids (_CID) we check |
| 1104 | * to see if there's a driver installed for this kind of device. Note |
| 1105 | * that drivers can install before or after a device is enumerated. |
| 1106 | * |
| 1107 | * TBD: Assumes LDM provides driver hot-plug capability. |
| 1108 | */ |
Thomas Renninger | bd7ce5b | 2005-10-03 10:39:00 -0700 | [diff] [blame] | 1109 | acpi_bus_find_driver(device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1111 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | if (!result) |
| 1113 | *child = device; |
| 1114 | else { |
Jesper Juhl | 6044ec8 | 2005-11-07 01:01:32 -0800 | [diff] [blame] | 1115 | kfree(device->pnp.cid_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | kfree(device); |
| 1117 | } |
| 1118 | |
| 1119 | return_VALUE(result); |
| 1120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1122 | 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] | 1123 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1124 | acpi_status status = AE_OK; |
| 1125 | struct acpi_device *parent = NULL; |
| 1126 | struct acpi_device *child = NULL; |
| 1127 | acpi_handle phandle = NULL; |
| 1128 | acpi_handle chandle = NULL; |
| 1129 | acpi_object_type type = 0; |
| 1130 | u32 level = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | |
| 1132 | ACPI_FUNCTION_TRACE("acpi_bus_scan"); |
| 1133 | |
| 1134 | if (!start) |
| 1135 | return_VALUE(-EINVAL); |
| 1136 | |
| 1137 | parent = start; |
| 1138 | phandle = start->handle; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | /* |
| 1141 | * Parse through the ACPI namespace, identify all 'devices', and |
| 1142 | * create a new 'struct acpi_device' for each. |
| 1143 | */ |
| 1144 | while ((level > 0) && parent) { |
| 1145 | |
| 1146 | status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1147 | chandle, &chandle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | |
| 1149 | /* |
| 1150 | * If this scope is exhausted then move our way back up. |
| 1151 | */ |
| 1152 | if (ACPI_FAILURE(status)) { |
| 1153 | level--; |
| 1154 | chandle = phandle; |
| 1155 | acpi_get_parent(phandle, &phandle); |
| 1156 | if (parent->parent) |
| 1157 | parent = parent->parent; |
| 1158 | continue; |
| 1159 | } |
| 1160 | |
| 1161 | status = acpi_get_type(chandle, &type); |
| 1162 | if (ACPI_FAILURE(status)) |
| 1163 | continue; |
| 1164 | |
| 1165 | /* |
| 1166 | * If this is a scope object then parse it (depth-first). |
| 1167 | */ |
| 1168 | if (type == ACPI_TYPE_LOCAL_SCOPE) { |
| 1169 | level++; |
| 1170 | phandle = chandle; |
| 1171 | chandle = NULL; |
| 1172 | continue; |
| 1173 | } |
| 1174 | |
| 1175 | /* |
| 1176 | * We're only interested in objects that we consider 'devices'. |
| 1177 | */ |
| 1178 | switch (type) { |
| 1179 | case ACPI_TYPE_DEVICE: |
| 1180 | type = ACPI_BUS_TYPE_DEVICE; |
| 1181 | break; |
| 1182 | case ACPI_TYPE_PROCESSOR: |
| 1183 | type = ACPI_BUS_TYPE_PROCESSOR; |
| 1184 | break; |
| 1185 | case ACPI_TYPE_THERMAL: |
| 1186 | type = ACPI_BUS_TYPE_THERMAL; |
| 1187 | break; |
| 1188 | case ACPI_TYPE_POWER: |
| 1189 | type = ACPI_BUS_TYPE_POWER; |
| 1190 | break; |
| 1191 | default: |
| 1192 | continue; |
| 1193 | } |
| 1194 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1195 | if (ops->acpi_op_add) |
| 1196 | status = acpi_add_single_object(&child, parent, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1197 | chandle, type); |
| 1198 | else |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1199 | status = acpi_bus_get_device(chandle, &child); |
| 1200 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1201 | if (ACPI_FAILURE(status)) |
| 1202 | continue; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1203 | |
| 1204 | if (ops->acpi_op_start) { |
| 1205 | status = acpi_start_single_object(child); |
| 1206 | if (ACPI_FAILURE(status)) |
| 1207 | continue; |
| 1208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | |
| 1210 | /* |
| 1211 | * If the device is present, enabled, and functioning then |
| 1212 | * parse its scope (depth-first). Note that we need to |
| 1213 | * represent absent devices to facilitate PnP notifications |
| 1214 | * -- but only the subtree head (not all of its children, |
| 1215 | * which will be enumerated when the parent is inserted). |
| 1216 | * |
| 1217 | * TBD: Need notifications and other detection mechanisms |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1218 | * in place before we can fully implement this. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | */ |
| 1220 | if (child->status.present) { |
| 1221 | status = acpi_get_next_object(ACPI_TYPE_ANY, chandle, |
| 1222 | NULL, NULL); |
| 1223 | if (ACPI_SUCCESS(status)) { |
| 1224 | level++; |
| 1225 | phandle = chandle; |
| 1226 | chandle = NULL; |
| 1227 | parent = child; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | return_VALUE(0); |
| 1233 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1235 | int |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1236 | acpi_bus_add(struct acpi_device **child, |
| 1237 | struct acpi_device *parent, acpi_handle handle, int type) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1238 | { |
| 1239 | int result; |
| 1240 | struct acpi_bus_ops ops; |
| 1241 | |
| 1242 | ACPI_FUNCTION_TRACE("acpi_bus_add"); |
| 1243 | |
| 1244 | result = acpi_add_single_object(child, parent, handle, type); |
| 1245 | if (!result) { |
| 1246 | memset(&ops, 0, sizeof(ops)); |
| 1247 | ops.acpi_op_add = 1; |
| 1248 | result = acpi_bus_scan(*child, &ops); |
| 1249 | } |
| 1250 | return_VALUE(result); |
| 1251 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1252 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1253 | EXPORT_SYMBOL(acpi_bus_add); |
| 1254 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1255 | int acpi_bus_start(struct acpi_device *device) |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1256 | { |
| 1257 | int result; |
| 1258 | struct acpi_bus_ops ops; |
| 1259 | |
| 1260 | ACPI_FUNCTION_TRACE("acpi_bus_start"); |
| 1261 | |
| 1262 | if (!device) |
| 1263 | return_VALUE(-EINVAL); |
| 1264 | |
| 1265 | result = acpi_start_single_object(device); |
| 1266 | if (!result) { |
| 1267 | memset(&ops, 0, sizeof(ops)); |
| 1268 | ops.acpi_op_start = 1; |
| 1269 | result = acpi_bus_scan(device, &ops); |
| 1270 | } |
| 1271 | return_VALUE(result); |
| 1272 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1273 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1274 | EXPORT_SYMBOL(acpi_bus_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | |
Kristen Accardi | ceaba66 | 2006-02-23 17:56:01 -0800 | [diff] [blame] | 1276 | int acpi_bus_trim(struct acpi_device *start, int rmdevice) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1278 | acpi_status status; |
| 1279 | struct acpi_device *parent, *child; |
| 1280 | acpi_handle phandle, chandle; |
| 1281 | acpi_object_type type; |
| 1282 | u32 level = 1; |
| 1283 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1285 | parent = start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | phandle = start->handle; |
| 1287 | child = chandle = NULL; |
| 1288 | |
| 1289 | while ((level > 0) && parent && (!err)) { |
| 1290 | status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1291 | chandle, &chandle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | |
| 1293 | /* |
| 1294 | * If this scope is exhausted then move our way back up. |
| 1295 | */ |
| 1296 | if (ACPI_FAILURE(status)) { |
| 1297 | level--; |
| 1298 | chandle = phandle; |
| 1299 | acpi_get_parent(phandle, &phandle); |
| 1300 | child = parent; |
| 1301 | parent = parent->parent; |
| 1302 | |
| 1303 | if (level == 0) |
| 1304 | err = acpi_bus_remove(child, rmdevice); |
| 1305 | else |
| 1306 | err = acpi_bus_remove(child, 1); |
| 1307 | |
| 1308 | continue; |
| 1309 | } |
| 1310 | |
| 1311 | status = acpi_get_type(chandle, &type); |
| 1312 | if (ACPI_FAILURE(status)) { |
| 1313 | continue; |
| 1314 | } |
| 1315 | /* |
| 1316 | * If there is a device corresponding to chandle then |
| 1317 | * parse it (depth-first). |
| 1318 | */ |
| 1319 | if (acpi_bus_get_device(chandle, &child) == 0) { |
| 1320 | level++; |
| 1321 | phandle = chandle; |
| 1322 | chandle = NULL; |
| 1323 | parent = child; |
| 1324 | } |
| 1325 | continue; |
| 1326 | } |
| 1327 | return err; |
| 1328 | } |
Kristen Accardi | ceaba66 | 2006-02-23 17:56:01 -0800 | [diff] [blame] | 1329 | EXPORT_SYMBOL_GPL(acpi_bus_trim); |
| 1330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1332 | static int acpi_bus_scan_fixed(struct acpi_device *root) |
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 | int result = 0; |
| 1335 | struct acpi_device *device = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | |
| 1337 | ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed"); |
| 1338 | |
| 1339 | if (!root) |
| 1340 | return_VALUE(-ENODEV); |
| 1341 | |
| 1342 | /* |
| 1343 | * Enumerate all fixed-feature devices. |
| 1344 | */ |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1345 | if (acpi_fadt.pwr_button == 0) { |
| 1346 | result = acpi_add_single_object(&device, acpi_root, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1347 | NULL, |
| 1348 | ACPI_BUS_TYPE_POWER_BUTTON); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1349 | if (!result) |
| 1350 | result = acpi_start_single_object(device); |
| 1351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1353 | if (acpi_fadt.sleep_button == 0) { |
| 1354 | result = acpi_add_single_object(&device, acpi_root, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1355 | NULL, |
| 1356 | ACPI_BUS_TYPE_SLEEP_BUTTON); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1357 | if (!result) |
| 1358 | result = acpi_start_single_object(device); |
| 1359 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | |
| 1361 | return_VALUE(result); |
| 1362 | } |
| 1363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | static int __init acpi_scan_init(void) |
| 1365 | { |
| 1366 | int result; |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1367 | struct acpi_bus_ops ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | |
| 1369 | ACPI_FUNCTION_TRACE("acpi_scan_init"); |
| 1370 | |
| 1371 | if (acpi_disabled) |
| 1372 | return_VALUE(0); |
| 1373 | |
| 1374 | kset_register(&acpi_namespace_kset); |
| 1375 | |
| 1376 | /* |
| 1377 | * Create the root device in the bus's device tree |
| 1378 | */ |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1379 | result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1380 | ACPI_BUS_TYPE_SYSTEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | if (result) |
| 1382 | goto Done; |
| 1383 | |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1384 | result = acpi_start_single_object(acpi_root); |
| 1385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | /* |
| 1387 | * Enumerate devices in the ACPI namespace. |
| 1388 | */ |
| 1389 | result = acpi_bus_scan_fixed(acpi_root); |
Rajesh Shah | 3fb0273 | 2005-04-28 00:25:52 -0700 | [diff] [blame] | 1390 | if (!result) { |
| 1391 | memset(&ops, 0, sizeof(ops)); |
| 1392 | ops.acpi_op_add = 1; |
| 1393 | ops.acpi_op_start = 1; |
| 1394 | result = acpi_bus_scan(acpi_root, &ops); |
| 1395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | |
| 1397 | if (result) |
| 1398 | acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL); |
| 1399 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 1400 | Done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | return_VALUE(result); |
| 1402 | } |
| 1403 | |
| 1404 | subsys_initcall(acpi_scan_init); |