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