Linux 4.1.16
[linux/fpc-iii.git] / drivers / acpi / property.c
blob0d083736e25babf920140d9b0c88a84b47d6d729
1 /*
2 * ACPI device specific properties support.
4 * Copyright (C) 2014, Intel Corporation
5 * All rights reserved.
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>
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.
16 #include <linux/acpi.h>
17 #include <linux/device.h>
18 #include <linux/export.h>
20 #include "internal.h"
22 /* ACPI _DSD device properties UUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
23 static const u8 prp_uuid[16] = {
24 0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d,
25 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01
28 static bool acpi_property_value_ok(const union acpi_object *value)
30 int j;
33 * The value must be an integer, a string, a reference, or a package
34 * whose every element must be an integer, a string, or a reference.
36 switch (value->type) {
37 case ACPI_TYPE_INTEGER:
38 case ACPI_TYPE_STRING:
39 case ACPI_TYPE_LOCAL_REFERENCE:
40 return true;
42 case ACPI_TYPE_PACKAGE:
43 for (j = 0; j < value->package.count; j++)
44 switch (value->package.elements[j].type) {
45 case ACPI_TYPE_INTEGER:
46 case ACPI_TYPE_STRING:
47 case ACPI_TYPE_LOCAL_REFERENCE:
48 continue;
50 default:
51 return false;
54 return true;
56 return false;
59 static bool acpi_properties_format_valid(const union acpi_object *properties)
61 int i;
63 for (i = 0; i < properties->package.count; i++) {
64 const union acpi_object *property;
66 property = &properties->package.elements[i];
68 * Only two elements allowed, the first one must be a string and
69 * the second one has to satisfy certain conditions.
71 if (property->package.count != 2
72 || property->package.elements[0].type != ACPI_TYPE_STRING
73 || !acpi_property_value_ok(&property->package.elements[1]))
74 return false;
76 return true;
79 static void acpi_init_of_compatible(struct acpi_device *adev)
81 const union acpi_object *of_compatible;
82 struct acpi_hardware_id *hwid;
83 bool acpi_of = false;
84 int ret;
87 * Check if the special PRP0001 ACPI ID is present and in that
88 * case we fill in Device Tree compatible properties for this
89 * device.
91 list_for_each_entry(hwid, &adev->pnp.ids, list) {
92 if (!strcmp(hwid->id, "PRP0001")) {
93 acpi_of = true;
94 break;
98 if (!acpi_of)
99 return;
101 ret = acpi_dev_get_property_array(adev, "compatible", ACPI_TYPE_STRING,
102 &of_compatible);
103 if (ret) {
104 ret = acpi_dev_get_property(adev, "compatible",
105 ACPI_TYPE_STRING, &of_compatible);
106 if (ret) {
107 acpi_handle_warn(adev->handle,
108 "PRP0001 requires compatible property\n");
109 return;
112 adev->data.of_compatible = of_compatible;
115 void acpi_init_properties(struct acpi_device *adev)
117 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
118 const union acpi_object *desc;
119 acpi_status status;
120 int i;
122 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
123 ACPI_TYPE_PACKAGE);
124 if (ACPI_FAILURE(status))
125 return;
127 desc = buf.pointer;
128 if (desc->package.count % 2)
129 goto fail;
131 /* Look for the device properties UUID. */
132 for (i = 0; i < desc->package.count; i += 2) {
133 const union acpi_object *uuid, *properties;
135 uuid = &desc->package.elements[i];
136 properties = &desc->package.elements[i + 1];
139 * The first element must be a UUID and the second one must be
140 * a package.
142 if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
143 || properties->type != ACPI_TYPE_PACKAGE)
144 break;
146 if (memcmp(uuid->buffer.pointer, prp_uuid, sizeof(prp_uuid)))
147 continue;
150 * We found the matching UUID. Now validate the format of the
151 * package immediately following it.
153 if (!acpi_properties_format_valid(properties))
154 break;
156 adev->data.pointer = buf.pointer;
157 adev->data.properties = properties;
159 acpi_init_of_compatible(adev);
160 return;
163 fail:
164 dev_warn(&adev->dev, "Returned _DSD data is not valid, skipping\n");
165 ACPI_FREE(buf.pointer);
168 void acpi_free_properties(struct acpi_device *adev)
170 ACPI_FREE((void *)adev->data.pointer);
171 adev->data.of_compatible = NULL;
172 adev->data.pointer = NULL;
173 adev->data.properties = NULL;
177 * acpi_dev_get_property - return an ACPI property with given name
178 * @adev: ACPI device to get property
179 * @name: Name of the property
180 * @type: Expected property type
181 * @obj: Location to store the property value (if not %NULL)
183 * Look up a property with @name and store a pointer to the resulting ACPI
184 * object at the location pointed to by @obj if found.
186 * Callers must not attempt to free the returned objects. These objects will be
187 * freed by the ACPI core automatically during the removal of @adev.
189 * Return: %0 if property with @name has been found (success),
190 * %-EINVAL if the arguments are invalid,
191 * %-ENODATA if the property doesn't exist,
192 * %-EPROTO if the property value type doesn't match @type.
194 int acpi_dev_get_property(struct acpi_device *adev, const char *name,
195 acpi_object_type type, const union acpi_object **obj)
197 const union acpi_object *properties;
198 int i;
200 if (!adev || !name)
201 return -EINVAL;
203 if (!adev->data.pointer || !adev->data.properties)
204 return -ENODATA;
206 properties = adev->data.properties;
207 for (i = 0; i < properties->package.count; i++) {
208 const union acpi_object *propname, *propvalue;
209 const union acpi_object *property;
211 property = &properties->package.elements[i];
213 propname = &property->package.elements[0];
214 propvalue = &property->package.elements[1];
216 if (!strcmp(name, propname->string.pointer)) {
217 if (type != ACPI_TYPE_ANY && propvalue->type != type)
218 return -EPROTO;
219 else if (obj)
220 *obj = propvalue;
222 return 0;
225 return -ENODATA;
227 EXPORT_SYMBOL_GPL(acpi_dev_get_property);
230 * acpi_dev_get_property_array - return an ACPI array property with given name
231 * @adev: ACPI device to get property
232 * @name: Name of the property
233 * @type: Expected type of array elements
234 * @obj: Location to store a pointer to the property value (if not NULL)
236 * Look up an array property with @name and store a pointer to the resulting
237 * ACPI object at the location pointed to by @obj if found.
239 * Callers must not attempt to free the returned objects. Those objects will be
240 * freed by the ACPI core automatically during the removal of @adev.
242 * Return: %0 if array property (package) with @name has been found (success),
243 * %-EINVAL if the arguments are invalid,
244 * %-ENODATA if the property doesn't exist,
245 * %-EPROTO if the property is not a package or the type of its elements
246 * doesn't match @type.
248 int acpi_dev_get_property_array(struct acpi_device *adev, const char *name,
249 acpi_object_type type,
250 const union acpi_object **obj)
252 const union acpi_object *prop;
253 int ret, i;
255 ret = acpi_dev_get_property(adev, name, ACPI_TYPE_PACKAGE, &prop);
256 if (ret)
257 return ret;
259 if (type != ACPI_TYPE_ANY) {
260 /* Check that all elements are of correct type. */
261 for (i = 0; i < prop->package.count; i++)
262 if (prop->package.elements[i].type != type)
263 return -EPROTO;
265 if (obj)
266 *obj = prop;
268 return 0;
270 EXPORT_SYMBOL_GPL(acpi_dev_get_property_array);
273 * acpi_dev_get_property_reference - returns handle to the referenced object
274 * @adev: ACPI device to get property
275 * @name: Name of the property
276 * @index: Index of the reference to return
277 * @args: Location to store the returned reference with optional arguments
279 * Find property with @name, verifify that it is a package containing at least
280 * one object reference and if so, store the ACPI device object pointer to the
281 * target object in @args->adev. If the reference includes arguments, store
282 * them in the @args->args[] array.
284 * If there's more than one reference in the property value package, @index is
285 * used to select the one to return.
287 * Return: %0 on success, negative error code on failure.
289 int acpi_dev_get_property_reference(struct acpi_device *adev,
290 const char *name, size_t index,
291 struct acpi_reference_args *args)
293 const union acpi_object *element, *end;
294 const union acpi_object *obj;
295 struct acpi_device *device;
296 int ret, idx = 0;
298 ret = acpi_dev_get_property(adev, name, ACPI_TYPE_ANY, &obj);
299 if (ret)
300 return ret;
303 * The simplest case is when the value is a single reference. Just
304 * return that reference then.
306 if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
307 if (index)
308 return -EINVAL;
310 ret = acpi_bus_get_device(obj->reference.handle, &device);
311 if (ret)
312 return ret;
314 args->adev = device;
315 args->nargs = 0;
316 return 0;
320 * If it is not a single reference, then it is a package of
321 * references followed by number of ints as follows:
323 * Package () { REF, INT, REF, INT, INT }
325 * The index argument is then used to determine which reference
326 * the caller wants (along with the arguments).
328 if (obj->type != ACPI_TYPE_PACKAGE || index >= obj->package.count)
329 return -EPROTO;
331 element = obj->package.elements;
332 end = element + obj->package.count;
334 while (element < end) {
335 u32 nargs, i;
337 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
338 return -EPROTO;
340 ret = acpi_bus_get_device(element->reference.handle, &device);
341 if (ret)
342 return -ENODEV;
344 element++;
345 nargs = 0;
347 /* assume following integer elements are all args */
348 for (i = 0; element + i < end; i++) {
349 int type = element[i].type;
351 if (type == ACPI_TYPE_INTEGER)
352 nargs++;
353 else if (type == ACPI_TYPE_LOCAL_REFERENCE)
354 break;
355 else
356 return -EPROTO;
359 if (idx++ == index) {
360 args->adev = device;
361 args->nargs = nargs;
362 for (i = 0; i < nargs; i++)
363 args->args[i] = element[i].integer.value;
365 return 0;
368 element += nargs;
371 return -EPROTO;
373 EXPORT_SYMBOL_GPL(acpi_dev_get_property_reference);
375 int acpi_dev_prop_get(struct acpi_device *adev, const char *propname,
376 void **valptr)
378 return acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
379 (const union acpi_object **)valptr);
382 int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
383 enum dev_prop_type proptype, void *val)
385 const union acpi_object *obj;
386 int ret;
388 if (!val)
389 return -EINVAL;
391 if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
392 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_INTEGER, &obj);
393 if (ret)
394 return ret;
396 switch (proptype) {
397 case DEV_PROP_U8:
398 if (obj->integer.value > U8_MAX)
399 return -EOVERFLOW;
400 *(u8 *)val = obj->integer.value;
401 break;
402 case DEV_PROP_U16:
403 if (obj->integer.value > U16_MAX)
404 return -EOVERFLOW;
405 *(u16 *)val = obj->integer.value;
406 break;
407 case DEV_PROP_U32:
408 if (obj->integer.value > U32_MAX)
409 return -EOVERFLOW;
410 *(u32 *)val = obj->integer.value;
411 break;
412 default:
413 *(u64 *)val = obj->integer.value;
414 break;
416 } else if (proptype == DEV_PROP_STRING) {
417 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_STRING, &obj);
418 if (ret)
419 return ret;
421 *(char **)val = obj->string.pointer;
422 } else {
423 ret = -EINVAL;
425 return ret;
428 static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
429 size_t nval)
431 int i;
433 for (i = 0; i < nval; i++) {
434 if (items[i].type != ACPI_TYPE_INTEGER)
435 return -EPROTO;
436 if (items[i].integer.value > U8_MAX)
437 return -EOVERFLOW;
439 val[i] = items[i].integer.value;
441 return 0;
444 static int acpi_copy_property_array_u16(const union acpi_object *items,
445 u16 *val, size_t nval)
447 int i;
449 for (i = 0; i < nval; i++) {
450 if (items[i].type != ACPI_TYPE_INTEGER)
451 return -EPROTO;
452 if (items[i].integer.value > U16_MAX)
453 return -EOVERFLOW;
455 val[i] = items[i].integer.value;
457 return 0;
460 static int acpi_copy_property_array_u32(const union acpi_object *items,
461 u32 *val, size_t nval)
463 int i;
465 for (i = 0; i < nval; i++) {
466 if (items[i].type != ACPI_TYPE_INTEGER)
467 return -EPROTO;
468 if (items[i].integer.value > U32_MAX)
469 return -EOVERFLOW;
471 val[i] = items[i].integer.value;
473 return 0;
476 static int acpi_copy_property_array_u64(const union acpi_object *items,
477 u64 *val, size_t nval)
479 int i;
481 for (i = 0; i < nval; i++) {
482 if (items[i].type != ACPI_TYPE_INTEGER)
483 return -EPROTO;
485 val[i] = items[i].integer.value;
487 return 0;
490 static int acpi_copy_property_array_string(const union acpi_object *items,
491 char **val, size_t nval)
493 int i;
495 for (i = 0; i < nval; i++) {
496 if (items[i].type != ACPI_TYPE_STRING)
497 return -EPROTO;
499 val[i] = items[i].string.pointer;
501 return 0;
504 int acpi_dev_prop_read(struct acpi_device *adev, const char *propname,
505 enum dev_prop_type proptype, void *val, size_t nval)
507 const union acpi_object *obj;
508 const union acpi_object *items;
509 int ret;
511 if (val && nval == 1) {
512 ret = acpi_dev_prop_read_single(adev, propname, proptype, val);
513 if (!ret)
514 return ret;
517 ret = acpi_dev_get_property_array(adev, propname, ACPI_TYPE_ANY, &obj);
518 if (ret)
519 return ret;
521 if (!val)
522 return obj->package.count;
523 else if (nval <= 0)
524 return -EINVAL;
526 if (nval > obj->package.count)
527 return -EOVERFLOW;
529 items = obj->package.elements;
530 switch (proptype) {
531 case DEV_PROP_U8:
532 ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
533 break;
534 case DEV_PROP_U16:
535 ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
536 break;
537 case DEV_PROP_U32:
538 ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
539 break;
540 case DEV_PROP_U64:
541 ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
542 break;
543 case DEV_PROP_STRING:
544 ret = acpi_copy_property_array_string(items, (char **)val, nval);
545 break;
546 default:
547 ret = -EINVAL;
548 break;
550 return ret;