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