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