Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ACPI device specific properties support. |
| 3 | * |
| 4 | * Copyright (C) 2014, Intel Corporation |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Authors: Mika Westerberg <mika.westerberg@linux.intel.com> |
| 8 | * Darren Hart <dvhart@linux.intel.com> |
| 9 | * Rafael J. Wysocki <rafael.j.wysocki@intel.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/acpi.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/export.h> |
| 19 | |
| 20 | #include "internal.h" |
| 21 | |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 22 | static int acpi_data_get_property_array(struct acpi_device_data *data, |
| 23 | const char *name, |
| 24 | acpi_object_type type, |
| 25 | const union acpi_object **obj); |
| 26 | |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 27 | /* ACPI _DSD device properties UUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */ |
| 28 | static const u8 prp_uuid[16] = { |
| 29 | 0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d, |
| 30 | 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01 |
| 31 | }; |
Rafael J. Wysocki | 445b0eb | 2015-08-27 04:36:14 +0200 | [diff] [blame] | 32 | /* ACPI _DSD data subnodes UUID: dbb8e3e6-5886-4ba6-8795-1319f52a966b */ |
| 33 | static const u8 ads_uuid[16] = { |
| 34 | 0xe6, 0xe3, 0xb8, 0xdb, 0x86, 0x58, 0xa6, 0x4b, |
| 35 | 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b |
| 36 | }; |
| 37 | |
| 38 | static bool acpi_enumerate_nondev_subnodes(acpi_handle scope, |
| 39 | const union acpi_object *desc, |
| 40 | struct acpi_device_data *data); |
| 41 | static bool acpi_extract_properties(const union acpi_object *desc, |
| 42 | struct acpi_device_data *data); |
| 43 | |
| 44 | static bool acpi_nondev_subnode_ok(acpi_handle scope, |
| 45 | const union acpi_object *link, |
| 46 | struct list_head *list) |
| 47 | { |
| 48 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; |
| 49 | struct acpi_data_node *dn; |
| 50 | acpi_handle handle; |
| 51 | acpi_status status; |
| 52 | |
| 53 | dn = kzalloc(sizeof(*dn), GFP_KERNEL); |
| 54 | if (!dn) |
| 55 | return false; |
| 56 | |
| 57 | dn->name = link->package.elements[0].string.pointer; |
| 58 | dn->fwnode.type = FWNODE_ACPI_DATA; |
| 59 | INIT_LIST_HEAD(&dn->data.subnodes); |
| 60 | |
| 61 | status = acpi_get_handle(scope, link->package.elements[1].string.pointer, |
| 62 | &handle); |
| 63 | if (ACPI_FAILURE(status)) |
| 64 | goto fail; |
| 65 | |
| 66 | status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf, |
| 67 | ACPI_TYPE_PACKAGE); |
| 68 | if (ACPI_FAILURE(status)) |
| 69 | goto fail; |
| 70 | |
| 71 | if (acpi_extract_properties(buf.pointer, &dn->data)) |
Rafael J. Wysocki | 263b4c1 | 2015-08-27 04:37:19 +0200 | [diff] [blame] | 72 | dn->handle = handle; |
Rafael J. Wysocki | 445b0eb | 2015-08-27 04:36:14 +0200 | [diff] [blame] | 73 | |
| 74 | if (acpi_enumerate_nondev_subnodes(scope, buf.pointer, &dn->data)) |
Rafael J. Wysocki | 263b4c1 | 2015-08-27 04:37:19 +0200 | [diff] [blame] | 75 | dn->handle = handle; |
Rafael J. Wysocki | 445b0eb | 2015-08-27 04:36:14 +0200 | [diff] [blame] | 76 | |
Rafael J. Wysocki | 263b4c1 | 2015-08-27 04:37:19 +0200 | [diff] [blame] | 77 | if (dn->handle) { |
| 78 | dn->data.pointer = buf.pointer; |
Rafael J. Wysocki | 445b0eb | 2015-08-27 04:36:14 +0200 | [diff] [blame] | 79 | list_add_tail(&dn->sibling, list); |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n"); |
| 84 | |
| 85 | fail: |
| 86 | ACPI_FREE(buf.pointer); |
| 87 | kfree(dn); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | static int acpi_add_nondev_subnodes(acpi_handle scope, |
| 92 | const union acpi_object *links, |
| 93 | struct list_head *list) |
| 94 | { |
| 95 | bool ret = false; |
| 96 | int i; |
| 97 | |
| 98 | for (i = 0; i < links->package.count; i++) { |
| 99 | const union acpi_object *link; |
| 100 | |
| 101 | link = &links->package.elements[i]; |
| 102 | /* Only two elements allowed, both must be strings. */ |
| 103 | if (link->package.count == 2 |
| 104 | && link->package.elements[0].type == ACPI_TYPE_STRING |
| 105 | && link->package.elements[1].type == ACPI_TYPE_STRING |
| 106 | && acpi_nondev_subnode_ok(scope, link, list)) |
| 107 | ret = true; |
| 108 | } |
| 109 | |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | static bool acpi_enumerate_nondev_subnodes(acpi_handle scope, |
| 114 | const union acpi_object *desc, |
| 115 | struct acpi_device_data *data) |
| 116 | { |
| 117 | int i; |
| 118 | |
| 119 | /* Look for the ACPI data subnodes UUID. */ |
| 120 | for (i = 0; i < desc->package.count; i += 2) { |
| 121 | const union acpi_object *uuid, *links; |
| 122 | |
| 123 | uuid = &desc->package.elements[i]; |
| 124 | links = &desc->package.elements[i + 1]; |
| 125 | |
| 126 | /* |
| 127 | * The first element must be a UUID and the second one must be |
| 128 | * a package. |
| 129 | */ |
| 130 | if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16 |
| 131 | || links->type != ACPI_TYPE_PACKAGE) |
| 132 | break; |
| 133 | |
| 134 | if (memcmp(uuid->buffer.pointer, ads_uuid, sizeof(ads_uuid))) |
| 135 | continue; |
| 136 | |
| 137 | return acpi_add_nondev_subnodes(scope, links, &data->subnodes); |
| 138 | } |
| 139 | |
| 140 | return false; |
| 141 | } |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 142 | |
| 143 | static bool acpi_property_value_ok(const union acpi_object *value) |
| 144 | { |
| 145 | int j; |
| 146 | |
| 147 | /* |
| 148 | * The value must be an integer, a string, a reference, or a package |
| 149 | * whose every element must be an integer, a string, or a reference. |
| 150 | */ |
| 151 | switch (value->type) { |
| 152 | case ACPI_TYPE_INTEGER: |
| 153 | case ACPI_TYPE_STRING: |
| 154 | case ACPI_TYPE_LOCAL_REFERENCE: |
| 155 | return true; |
| 156 | |
| 157 | case ACPI_TYPE_PACKAGE: |
| 158 | for (j = 0; j < value->package.count; j++) |
| 159 | switch (value->package.elements[j].type) { |
| 160 | case ACPI_TYPE_INTEGER: |
| 161 | case ACPI_TYPE_STRING: |
| 162 | case ACPI_TYPE_LOCAL_REFERENCE: |
| 163 | continue; |
| 164 | |
| 165 | default: |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | static bool acpi_properties_format_valid(const union acpi_object *properties) |
| 175 | { |
| 176 | int i; |
| 177 | |
| 178 | for (i = 0; i < properties->package.count; i++) { |
| 179 | const union acpi_object *property; |
| 180 | |
| 181 | property = &properties->package.elements[i]; |
| 182 | /* |
| 183 | * Only two elements allowed, the first one must be a string and |
| 184 | * the second one has to satisfy certain conditions. |
| 185 | */ |
| 186 | if (property->package.count != 2 |
| 187 | || property->package.elements[0].type != ACPI_TYPE_STRING |
| 188 | || !acpi_property_value_ok(&property->package.elements[1])) |
| 189 | return false; |
| 190 | } |
| 191 | return true; |
| 192 | } |
| 193 | |
Mika Westerberg | 733e625 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 194 | static void acpi_init_of_compatible(struct acpi_device *adev) |
| 195 | { |
| 196 | const union acpi_object *of_compatible; |
Mika Westerberg | 733e625 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 197 | int ret; |
| 198 | |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 199 | ret = acpi_data_get_property_array(&adev->data, "compatible", |
| 200 | ACPI_TYPE_STRING, &of_compatible); |
Rafael J. Wysocki | 5c53b26 | 2015-05-05 15:43:07 +0200 | [diff] [blame] | 201 | if (ret) { |
| 202 | ret = acpi_dev_get_property(adev, "compatible", |
| 203 | ACPI_TYPE_STRING, &of_compatible); |
| 204 | if (ret) { |
| 205 | if (adev->parent |
| 206 | && adev->parent->flags.of_compatible_ok) |
| 207 | goto out; |
| 208 | |
| 209 | return; |
| 210 | } |
| 211 | } |
| 212 | adev->data.of_compatible = of_compatible; |
| 213 | |
| 214 | out: |
| 215 | adev->flags.of_compatible_ok = 1; |
| 216 | } |
| 217 | |
Rafael J. Wysocki | bd8191c | 2015-08-27 04:35:14 +0200 | [diff] [blame] | 218 | static bool acpi_extract_properties(const union acpi_object *desc, |
| 219 | struct acpi_device_data *data) |
Rafael J. Wysocki | 5c53b26 | 2015-05-05 15:43:07 +0200 | [diff] [blame] | 220 | { |
Rafael J. Wysocki | 5c53b26 | 2015-05-05 15:43:07 +0200 | [diff] [blame] | 221 | int i; |
| 222 | |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 223 | if (desc->package.count % 2) |
Rafael J. Wysocki | bd8191c | 2015-08-27 04:35:14 +0200 | [diff] [blame] | 224 | return false; |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 225 | |
| 226 | /* Look for the device properties UUID. */ |
| 227 | for (i = 0; i < desc->package.count; i += 2) { |
| 228 | const union acpi_object *uuid, *properties; |
| 229 | |
| 230 | uuid = &desc->package.elements[i]; |
| 231 | properties = &desc->package.elements[i + 1]; |
| 232 | |
| 233 | /* |
| 234 | * The first element must be a UUID and the second one must be |
| 235 | * a package. |
| 236 | */ |
| 237 | if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16 |
| 238 | || properties->type != ACPI_TYPE_PACKAGE) |
| 239 | break; |
| 240 | |
| 241 | if (memcmp(uuid->buffer.pointer, prp_uuid, sizeof(prp_uuid))) |
| 242 | continue; |
| 243 | |
| 244 | /* |
| 245 | * We found the matching UUID. Now validate the format of the |
| 246 | * package immediately following it. |
| 247 | */ |
| 248 | if (!acpi_properties_format_valid(properties)) |
| 249 | break; |
| 250 | |
Rafael J. Wysocki | bd8191c | 2015-08-27 04:35:14 +0200 | [diff] [blame] | 251 | data->properties = properties; |
| 252 | return true; |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Rafael J. Wysocki | bd8191c | 2015-08-27 04:35:14 +0200 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
| 258 | void acpi_init_properties(struct acpi_device *adev) |
| 259 | { |
| 260 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; |
| 261 | struct acpi_hardware_id *hwid; |
| 262 | acpi_status status; |
| 263 | bool acpi_of = false; |
| 264 | |
Rafael J. Wysocki | 445b0eb | 2015-08-27 04:36:14 +0200 | [diff] [blame] | 265 | INIT_LIST_HEAD(&adev->data.subnodes); |
| 266 | |
Rafael J. Wysocki | bd8191c | 2015-08-27 04:35:14 +0200 | [diff] [blame] | 267 | /* |
| 268 | * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in |
| 269 | * Device Tree compatible properties for this device. |
| 270 | */ |
| 271 | list_for_each_entry(hwid, &adev->pnp.ids, list) { |
| 272 | if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) { |
| 273 | acpi_of = true; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf, |
| 279 | ACPI_TYPE_PACKAGE); |
| 280 | if (ACPI_FAILURE(status)) |
| 281 | goto out; |
| 282 | |
| 283 | if (acpi_extract_properties(buf.pointer, &adev->data)) { |
| 284 | adev->data.pointer = buf.pointer; |
| 285 | if (acpi_of) |
| 286 | acpi_init_of_compatible(adev); |
Rafael J. Wysocki | 445b0eb | 2015-08-27 04:36:14 +0200 | [diff] [blame] | 287 | } |
| 288 | if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer, &adev->data)) |
| 289 | adev->data.pointer = buf.pointer; |
| 290 | |
| 291 | if (!adev->data.pointer) { |
Rafael J. Wysocki | bd8191c | 2015-08-27 04:35:14 +0200 | [diff] [blame] | 292 | acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n"); |
| 293 | ACPI_FREE(buf.pointer); |
| 294 | } |
Rafael J. Wysocki | 5c53b26 | 2015-05-05 15:43:07 +0200 | [diff] [blame] | 295 | |
| 296 | out: |
| 297 | if (acpi_of && !adev->flags.of_compatible_ok) |
| 298 | acpi_handle_info(adev->handle, |
Rafael J. Wysocki | ee89209 | 2015-05-22 04:24:34 +0200 | [diff] [blame] | 299 | ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n"); |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Rafael J. Wysocki | 445b0eb | 2015-08-27 04:36:14 +0200 | [diff] [blame] | 302 | static void acpi_destroy_nondev_subnodes(struct list_head *list) |
| 303 | { |
| 304 | struct acpi_data_node *dn, *next; |
| 305 | |
| 306 | if (list_empty(list)) |
| 307 | return; |
| 308 | |
| 309 | list_for_each_entry_safe_reverse(dn, next, list, sibling) { |
| 310 | acpi_destroy_nondev_subnodes(&dn->data.subnodes); |
Rafael J. Wysocki | 263b4c1 | 2015-08-27 04:37:19 +0200 | [diff] [blame] | 311 | wait_for_completion(&dn->kobj_done); |
Rafael J. Wysocki | 445b0eb | 2015-08-27 04:36:14 +0200 | [diff] [blame] | 312 | list_del(&dn->sibling); |
| 313 | ACPI_FREE((void *)dn->data.pointer); |
| 314 | kfree(dn); |
| 315 | } |
| 316 | } |
| 317 | |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 318 | void acpi_free_properties(struct acpi_device *adev) |
| 319 | { |
Rafael J. Wysocki | 445b0eb | 2015-08-27 04:36:14 +0200 | [diff] [blame] | 320 | acpi_destroy_nondev_subnodes(&adev->data.subnodes); |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 321 | ACPI_FREE((void *)adev->data.pointer); |
Mika Westerberg | 733e625 | 2014-10-21 13:33:56 +0200 | [diff] [blame] | 322 | adev->data.of_compatible = NULL; |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 323 | adev->data.pointer = NULL; |
| 324 | adev->data.properties = NULL; |
| 325 | } |
| 326 | |
| 327 | /** |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 328 | * acpi_data_get_property - return an ACPI property with given name |
| 329 | * @data: ACPI device deta object to get the property from |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 330 | * @name: Name of the property |
| 331 | * @type: Expected property type |
| 332 | * @obj: Location to store the property value (if not %NULL) |
| 333 | * |
| 334 | * Look up a property with @name and store a pointer to the resulting ACPI |
| 335 | * object at the location pointed to by @obj if found. |
| 336 | * |
| 337 | * Callers must not attempt to free the returned objects. These objects will be |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 338 | * freed by the ACPI core automatically during the removal of @data. |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 339 | * |
| 340 | * Return: %0 if property with @name has been found (success), |
| 341 | * %-EINVAL if the arguments are invalid, |
| 342 | * %-ENODATA if the property doesn't exist, |
| 343 | * %-EPROTO if the property value type doesn't match @type. |
| 344 | */ |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 345 | static int acpi_data_get_property(struct acpi_device_data *data, |
| 346 | const char *name, acpi_object_type type, |
| 347 | const union acpi_object **obj) |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 348 | { |
| 349 | const union acpi_object *properties; |
| 350 | int i; |
| 351 | |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 352 | if (!data || !name) |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 353 | return -EINVAL; |
| 354 | |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 355 | if (!data->pointer || !data->properties) |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 356 | return -ENODATA; |
| 357 | |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 358 | properties = data->properties; |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 359 | for (i = 0; i < properties->package.count; i++) { |
| 360 | const union acpi_object *propname, *propvalue; |
| 361 | const union acpi_object *property; |
| 362 | |
| 363 | property = &properties->package.elements[i]; |
| 364 | |
| 365 | propname = &property->package.elements[0]; |
| 366 | propvalue = &property->package.elements[1]; |
| 367 | |
| 368 | if (!strcmp(name, propname->string.pointer)) { |
| 369 | if (type != ACPI_TYPE_ANY && propvalue->type != type) |
| 370 | return -EPROTO; |
| 371 | else if (obj) |
| 372 | *obj = propvalue; |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | } |
| 377 | return -ENODATA; |
| 378 | } |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 379 | |
| 380 | /** |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 381 | * acpi_dev_get_property - return an ACPI property with given name. |
| 382 | * @adev: ACPI device to get the property from. |
| 383 | * @name: Name of the property. |
| 384 | * @type: Expected property type. |
| 385 | * @obj: Location to store the property value (if not %NULL). |
| 386 | */ |
| 387 | int acpi_dev_get_property(struct acpi_device *adev, const char *name, |
| 388 | acpi_object_type type, const union acpi_object **obj) |
| 389 | { |
| 390 | return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL; |
| 391 | } |
| 392 | EXPORT_SYMBOL_GPL(acpi_dev_get_property); |
| 393 | |
| 394 | static struct acpi_device_data *acpi_device_data_of_node(struct fwnode_handle *fwnode) |
| 395 | { |
| 396 | if (fwnode->type == FWNODE_ACPI) { |
| 397 | struct acpi_device *adev = to_acpi_device_node(fwnode); |
| 398 | return &adev->data; |
| 399 | } else if (fwnode->type == FWNODE_ACPI_DATA) { |
| 400 | struct acpi_data_node *dn = to_acpi_data_node(fwnode); |
| 401 | return &dn->data; |
| 402 | } |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * acpi_node_prop_get - return an ACPI property with given name. |
| 408 | * @fwnode: Firmware node to get the property from. |
| 409 | * @propname: Name of the property. |
| 410 | * @valptr: Location to store a pointer to the property value (if not %NULL). |
| 411 | */ |
| 412 | int acpi_node_prop_get(struct fwnode_handle *fwnode, const char *propname, |
| 413 | void **valptr) |
| 414 | { |
| 415 | return acpi_data_get_property(acpi_device_data_of_node(fwnode), |
| 416 | propname, ACPI_TYPE_ANY, |
| 417 | (const union acpi_object **)valptr); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * acpi_data_get_property_array - return an ACPI array property with given name |
| 422 | * @adev: ACPI data object to get the property from |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 423 | * @name: Name of the property |
| 424 | * @type: Expected type of array elements |
| 425 | * @obj: Location to store a pointer to the property value (if not NULL) |
| 426 | * |
| 427 | * Look up an array property with @name and store a pointer to the resulting |
| 428 | * ACPI object at the location pointed to by @obj if found. |
| 429 | * |
| 430 | * Callers must not attempt to free the returned objects. Those objects will be |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 431 | * freed by the ACPI core automatically during the removal of @data. |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 432 | * |
| 433 | * Return: %0 if array property (package) with @name has been found (success), |
| 434 | * %-EINVAL if the arguments are invalid, |
| 435 | * %-ENODATA if the property doesn't exist, |
| 436 | * %-EPROTO if the property is not a package or the type of its elements |
| 437 | * doesn't match @type. |
| 438 | */ |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 439 | static int acpi_data_get_property_array(struct acpi_device_data *data, |
| 440 | const char *name, |
| 441 | acpi_object_type type, |
| 442 | const union acpi_object **obj) |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 443 | { |
| 444 | const union acpi_object *prop; |
| 445 | int ret, i; |
| 446 | |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 447 | ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop); |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 448 | if (ret) |
| 449 | return ret; |
| 450 | |
| 451 | if (type != ACPI_TYPE_ANY) { |
| 452 | /* Check that all elements are of correct type. */ |
| 453 | for (i = 0; i < prop->package.count; i++) |
| 454 | if (prop->package.elements[i].type != type) |
| 455 | return -EPROTO; |
| 456 | } |
| 457 | if (obj) |
| 458 | *obj = prop; |
| 459 | |
| 460 | return 0; |
| 461 | } |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 462 | |
| 463 | /** |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame^] | 464 | * acpi_data_get_property_reference - returns handle to the referenced object |
| 465 | * @data: ACPI device data object containing the property |
| 466 | * @propname: Name of the property |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 467 | * @index: Index of the reference to return |
| 468 | * @args: Location to store the returned reference with optional arguments |
| 469 | * |
| 470 | * Find property with @name, verifify that it is a package containing at least |
| 471 | * one object reference and if so, store the ACPI device object pointer to the |
Rafael J. Wysocki | 60ba032 | 2014-11-05 00:29:07 +0100 | [diff] [blame] | 472 | * target object in @args->adev. If the reference includes arguments, store |
| 473 | * them in the @args->args[] array. |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 474 | * |
Rafael J. Wysocki | 60ba032 | 2014-11-05 00:29:07 +0100 | [diff] [blame] | 475 | * If there's more than one reference in the property value package, @index is |
| 476 | * used to select the one to return. |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 477 | * |
| 478 | * Return: %0 on success, negative error code on failure. |
| 479 | */ |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame^] | 480 | static int acpi_data_get_property_reference(struct acpi_device_data *data, |
| 481 | const char *propname, size_t index, |
| 482 | struct acpi_reference_args *args) |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 483 | { |
| 484 | const union acpi_object *element, *end; |
| 485 | const union acpi_object *obj; |
| 486 | struct acpi_device *device; |
| 487 | int ret, idx = 0; |
| 488 | |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame^] | 489 | ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj); |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 490 | if (ret) |
| 491 | return ret; |
| 492 | |
| 493 | /* |
| 494 | * The simplest case is when the value is a single reference. Just |
| 495 | * return that reference then. |
| 496 | */ |
| 497 | if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) { |
Rafael J. Wysocki | 60ba032 | 2014-11-05 00:29:07 +0100 | [diff] [blame] | 498 | if (index) |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 499 | return -EINVAL; |
| 500 | |
| 501 | ret = acpi_bus_get_device(obj->reference.handle, &device); |
| 502 | if (ret) |
| 503 | return ret; |
| 504 | |
| 505 | args->adev = device; |
| 506 | args->nargs = 0; |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * If it is not a single reference, then it is a package of |
| 512 | * references followed by number of ints as follows: |
| 513 | * |
| 514 | * Package () { REF, INT, REF, INT, INT } |
| 515 | * |
| 516 | * The index argument is then used to determine which reference |
| 517 | * the caller wants (along with the arguments). |
| 518 | */ |
| 519 | if (obj->type != ACPI_TYPE_PACKAGE || index >= obj->package.count) |
| 520 | return -EPROTO; |
| 521 | |
| 522 | element = obj->package.elements; |
| 523 | end = element + obj->package.count; |
| 524 | |
| 525 | while (element < end) { |
| 526 | u32 nargs, i; |
| 527 | |
| 528 | if (element->type != ACPI_TYPE_LOCAL_REFERENCE) |
| 529 | return -EPROTO; |
| 530 | |
| 531 | ret = acpi_bus_get_device(element->reference.handle, &device); |
| 532 | if (ret) |
| 533 | return -ENODEV; |
| 534 | |
| 535 | element++; |
| 536 | nargs = 0; |
| 537 | |
Rafael J. Wysocki | 60ba032 | 2014-11-05 00:29:07 +0100 | [diff] [blame] | 538 | /* assume following integer elements are all args */ |
| 539 | for (i = 0; element + i < end; i++) { |
| 540 | int type = element[i].type; |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 541 | |
Rafael J. Wysocki | 60ba032 | 2014-11-05 00:29:07 +0100 | [diff] [blame] | 542 | if (type == ACPI_TYPE_INTEGER) |
| 543 | nargs++; |
| 544 | else if (type == ACPI_TYPE_LOCAL_REFERENCE) |
| 545 | break; |
| 546 | else |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 547 | return -EPROTO; |
Mika Westerberg | ffdcd95 | 2014-10-21 13:33:55 +0200 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if (idx++ == index) { |
| 551 | args->adev = device; |
| 552 | args->nargs = nargs; |
| 553 | for (i = 0; i < nargs; i++) |
| 554 | args->args[i] = element[i].integer.value; |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | element += nargs; |
| 560 | } |
| 561 | |
| 562 | return -EPROTO; |
| 563 | } |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame^] | 564 | |
| 565 | /** |
| 566 | * acpi_node_get_property_reference - get a handle to the referenced object. |
| 567 | * @fwnode: Firmware node to get the property from. |
| 568 | * @propname: Name of the property. |
| 569 | * @index: Index of the reference to return. |
| 570 | * @args: Location to store the returned reference with optional arguments. |
| 571 | */ |
| 572 | int acpi_node_get_property_reference(struct fwnode_handle *fwnode, |
| 573 | const char *name, size_t index, |
| 574 | struct acpi_reference_args *args) |
| 575 | { |
| 576 | struct acpi_device_data *data = acpi_device_data_of_node(fwnode); |
| 577 | |
| 578 | return data ? acpi_data_get_property_reference(data, name, index, args) : -EINVAL; |
| 579 | } |
| 580 | EXPORT_SYMBOL_GPL(acpi_node_get_property_reference); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 581 | |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 582 | static int acpi_data_prop_read_single(struct acpi_device_data *data, |
| 583 | const char *propname, |
| 584 | enum dev_prop_type proptype, void *val) |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 585 | { |
| 586 | const union acpi_object *obj; |
| 587 | int ret; |
| 588 | |
| 589 | if (!val) |
| 590 | return -EINVAL; |
| 591 | |
| 592 | if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) { |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 593 | ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 594 | if (ret) |
| 595 | return ret; |
| 596 | |
| 597 | switch (proptype) { |
| 598 | case DEV_PROP_U8: |
| 599 | if (obj->integer.value > U8_MAX) |
| 600 | return -EOVERFLOW; |
| 601 | *(u8 *)val = obj->integer.value; |
| 602 | break; |
| 603 | case DEV_PROP_U16: |
| 604 | if (obj->integer.value > U16_MAX) |
| 605 | return -EOVERFLOW; |
| 606 | *(u16 *)val = obj->integer.value; |
| 607 | break; |
| 608 | case DEV_PROP_U32: |
| 609 | if (obj->integer.value > U32_MAX) |
| 610 | return -EOVERFLOW; |
| 611 | *(u32 *)val = obj->integer.value; |
| 612 | break; |
| 613 | default: |
| 614 | *(u64 *)val = obj->integer.value; |
| 615 | break; |
| 616 | } |
| 617 | } else if (proptype == DEV_PROP_STRING) { |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 618 | ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 619 | if (ret) |
| 620 | return ret; |
| 621 | |
| 622 | *(char **)val = obj->string.pointer; |
| 623 | } else { |
| 624 | ret = -EINVAL; |
| 625 | } |
| 626 | return ret; |
| 627 | } |
| 628 | |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 629 | int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname, |
| 630 | enum dev_prop_type proptype, void *val) |
| 631 | { |
| 632 | return adev ? acpi_data_prop_read_single(&adev->data, propname, proptype, val) : -EINVAL; |
| 633 | } |
| 634 | |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 635 | static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val, |
| 636 | size_t nval) |
| 637 | { |
| 638 | int i; |
| 639 | |
| 640 | for (i = 0; i < nval; i++) { |
| 641 | if (items[i].type != ACPI_TYPE_INTEGER) |
| 642 | return -EPROTO; |
| 643 | if (items[i].integer.value > U8_MAX) |
| 644 | return -EOVERFLOW; |
| 645 | |
| 646 | val[i] = items[i].integer.value; |
| 647 | } |
| 648 | return 0; |
| 649 | } |
| 650 | |
| 651 | static int acpi_copy_property_array_u16(const union acpi_object *items, |
| 652 | u16 *val, size_t nval) |
| 653 | { |
| 654 | int i; |
| 655 | |
| 656 | for (i = 0; i < nval; i++) { |
| 657 | if (items[i].type != ACPI_TYPE_INTEGER) |
| 658 | return -EPROTO; |
| 659 | if (items[i].integer.value > U16_MAX) |
| 660 | return -EOVERFLOW; |
| 661 | |
| 662 | val[i] = items[i].integer.value; |
| 663 | } |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | static int acpi_copy_property_array_u32(const union acpi_object *items, |
| 668 | u32 *val, size_t nval) |
| 669 | { |
| 670 | int i; |
| 671 | |
| 672 | for (i = 0; i < nval; i++) { |
| 673 | if (items[i].type != ACPI_TYPE_INTEGER) |
| 674 | return -EPROTO; |
| 675 | if (items[i].integer.value > U32_MAX) |
| 676 | return -EOVERFLOW; |
| 677 | |
| 678 | val[i] = items[i].integer.value; |
| 679 | } |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | static int acpi_copy_property_array_u64(const union acpi_object *items, |
| 684 | u64 *val, size_t nval) |
| 685 | { |
| 686 | int i; |
| 687 | |
| 688 | for (i = 0; i < nval; i++) { |
| 689 | if (items[i].type != ACPI_TYPE_INTEGER) |
| 690 | return -EPROTO; |
| 691 | |
| 692 | val[i] = items[i].integer.value; |
| 693 | } |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static int acpi_copy_property_array_string(const union acpi_object *items, |
| 698 | char **val, size_t nval) |
| 699 | { |
| 700 | int i; |
| 701 | |
| 702 | for (i = 0; i < nval; i++) { |
| 703 | if (items[i].type != ACPI_TYPE_STRING) |
| 704 | return -EPROTO; |
| 705 | |
| 706 | val[i] = items[i].string.pointer; |
| 707 | } |
| 708 | return 0; |
| 709 | } |
| 710 | |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 711 | static int acpi_data_prop_read(struct acpi_device_data *data, |
| 712 | const char *propname, |
| 713 | enum dev_prop_type proptype, |
| 714 | void *val, size_t nval) |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 715 | { |
| 716 | const union acpi_object *obj; |
| 717 | const union acpi_object *items; |
| 718 | int ret; |
| 719 | |
| 720 | if (val && nval == 1) { |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 721 | ret = acpi_data_prop_read_single(data, propname, proptype, val); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 722 | if (!ret) |
| 723 | return ret; |
| 724 | } |
| 725 | |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 726 | ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj); |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 727 | if (ret) |
| 728 | return ret; |
| 729 | |
| 730 | if (!val) |
| 731 | return obj->package.count; |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 732 | |
| 733 | if (nval > obj->package.count) |
| 734 | return -EOVERFLOW; |
Andy Shevchenko | 7dc59dc | 2015-08-10 19:56:48 +0300 | [diff] [blame] | 735 | else if (nval <= 0) |
| 736 | return -EINVAL; |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 737 | |
| 738 | items = obj->package.elements; |
Andy Shevchenko | 7dc59dc | 2015-08-10 19:56:48 +0300 | [diff] [blame] | 739 | |
Rafael J. Wysocki | b31384f | 2014-11-04 01:28:56 +0100 | [diff] [blame] | 740 | switch (proptype) { |
| 741 | case DEV_PROP_U8: |
| 742 | ret = acpi_copy_property_array_u8(items, (u8 *)val, nval); |
| 743 | break; |
| 744 | case DEV_PROP_U16: |
| 745 | ret = acpi_copy_property_array_u16(items, (u16 *)val, nval); |
| 746 | break; |
| 747 | case DEV_PROP_U32: |
| 748 | ret = acpi_copy_property_array_u32(items, (u32 *)val, nval); |
| 749 | break; |
| 750 | case DEV_PROP_U64: |
| 751 | ret = acpi_copy_property_array_u64(items, (u64 *)val, nval); |
| 752 | break; |
| 753 | case DEV_PROP_STRING: |
| 754 | ret = acpi_copy_property_array_string(items, (char **)val, nval); |
| 755 | break; |
| 756 | default: |
| 757 | ret = -EINVAL; |
| 758 | break; |
| 759 | } |
| 760 | return ret; |
| 761 | } |
Rafael J. Wysocki | 3a7a2ab | 2015-08-27 04:40:05 +0200 | [diff] [blame] | 762 | |
| 763 | int acpi_dev_prop_read(struct acpi_device *adev, const char *propname, |
| 764 | enum dev_prop_type proptype, void *val, size_t nval) |
| 765 | { |
| 766 | return adev ? acpi_data_prop_read(&adev->data, propname, proptype, val, nval) : -EINVAL; |
| 767 | } |
| 768 | |
| 769 | /** |
| 770 | * acpi_node_prop_read - retrieve the value of an ACPI property with given name. |
| 771 | * @fwnode: Firmware node to get the property from. |
| 772 | * @propname: Name of the property. |
| 773 | * @proptype: Expected property type. |
| 774 | * @val: Location to store the property value (if not %NULL). |
| 775 | * @nval: Size of the array pointed to by @val. |
| 776 | * |
| 777 | * If @val is %NULL, return the number of array elements comprising the value |
| 778 | * of the property. Otherwise, read at most @nval values to the array at the |
| 779 | * location pointed to by @val. |
| 780 | */ |
| 781 | int acpi_node_prop_read(struct fwnode_handle *fwnode, const char *propname, |
| 782 | enum dev_prop_type proptype, void *val, size_t nval) |
| 783 | { |
| 784 | return acpi_data_prop_read(acpi_device_data_of_node(fwnode), |
| 785 | propname, proptype, val, nval); |
| 786 | } |
Rafael J. Wysocki | 504a337 | 2015-08-27 04:42:33 +0200 | [diff] [blame^] | 787 | |
| 788 | /** |
| 789 | * acpi_get_next_subnode - Return the next child node handle for a device. |
| 790 | * @dev: Device to find the next child node for. |
| 791 | * @child: Handle to one of the device's child nodes or a null handle. |
| 792 | */ |
| 793 | struct fwnode_handle *acpi_get_next_subnode(struct device *dev, |
| 794 | struct fwnode_handle *child) |
| 795 | { |
| 796 | struct acpi_device *adev = ACPI_COMPANION(dev); |
| 797 | struct list_head *head, *next; |
| 798 | |
| 799 | if (!adev) |
| 800 | return NULL; |
| 801 | |
| 802 | if (!child || child->type == FWNODE_ACPI) { |
| 803 | head = &adev->children; |
| 804 | if (list_empty(head)) |
| 805 | goto nondev; |
| 806 | |
| 807 | if (child) { |
| 808 | adev = to_acpi_device_node(child); |
| 809 | next = adev->node.next; |
| 810 | if (next == head) { |
| 811 | child = NULL; |
| 812 | goto nondev; |
| 813 | } |
| 814 | adev = list_entry(next, struct acpi_device, node); |
| 815 | } else { |
| 816 | adev = list_first_entry(head, struct acpi_device, node); |
| 817 | } |
| 818 | return acpi_fwnode_handle(adev); |
| 819 | } |
| 820 | |
| 821 | nondev: |
| 822 | if (!child || child->type == FWNODE_ACPI_DATA) { |
| 823 | struct acpi_data_node *dn; |
| 824 | |
| 825 | head = &adev->data.subnodes; |
| 826 | if (list_empty(head)) |
| 827 | return NULL; |
| 828 | |
| 829 | if (child) { |
| 830 | dn = to_acpi_data_node(child); |
| 831 | next = dn->sibling.next; |
| 832 | if (next == head) |
| 833 | return NULL; |
| 834 | |
| 835 | dn = list_entry(next, struct acpi_data_node, sibling); |
| 836 | } else { |
| 837 | dn = list_first_entry(head, struct acpi_data_node, sibling); |
| 838 | } |
| 839 | return &dn->fwnode; |
| 840 | } |
| 841 | return NULL; |
| 842 | } |