ACPI / property: Expose data-only subnodes via sysfs
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / property.c
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
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
26 };
27 /* ACPI _DSD data subnodes UUID: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
28 static const u8 ads_uuid[16] = {
29         0xe6, 0xe3, 0xb8, 0xdb, 0x86, 0x58, 0xa6, 0x4b,
30         0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b
31 };
32
33 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
34                                            const union acpi_object *desc,
35                                            struct acpi_device_data *data);
36 static bool acpi_extract_properties(const union acpi_object *desc,
37                                     struct acpi_device_data *data);
38
39 static bool acpi_nondev_subnode_ok(acpi_handle scope,
40                                    const union acpi_object *link,
41                                    struct list_head *list)
42 {
43         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
44         struct acpi_data_node *dn;
45         acpi_handle handle;
46         acpi_status status;
47
48         dn = kzalloc(sizeof(*dn), GFP_KERNEL);
49         if (!dn)
50                 return false;
51
52         dn->name = link->package.elements[0].string.pointer;
53         dn->fwnode.type = FWNODE_ACPI_DATA;
54         INIT_LIST_HEAD(&dn->data.subnodes);
55
56         status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
57                                  &handle);
58         if (ACPI_FAILURE(status))
59                 goto fail;
60
61         status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
62                                             ACPI_TYPE_PACKAGE);
63         if (ACPI_FAILURE(status))
64                 goto fail;
65
66         if (acpi_extract_properties(buf.pointer, &dn->data))
67                 dn->handle = handle;
68
69         if (acpi_enumerate_nondev_subnodes(scope, buf.pointer, &dn->data))
70                 dn->handle = handle;
71
72         if (dn->handle) {
73                 dn->data.pointer = buf.pointer;
74                 list_add_tail(&dn->sibling, list);
75                 return true;
76         }
77
78         acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
79
80  fail:
81         ACPI_FREE(buf.pointer);
82         kfree(dn);
83         return false;
84 }
85
86 static int acpi_add_nondev_subnodes(acpi_handle scope,
87                                     const union acpi_object *links,
88                                     struct list_head *list)
89 {
90         bool ret = false;
91         int i;
92
93         for (i = 0; i < links->package.count; i++) {
94                 const union acpi_object *link;
95
96                 link = &links->package.elements[i];
97                 /* Only two elements allowed, both must be strings. */
98                 if (link->package.count == 2
99                     && link->package.elements[0].type == ACPI_TYPE_STRING
100                     && link->package.elements[1].type == ACPI_TYPE_STRING
101                     && acpi_nondev_subnode_ok(scope, link, list))
102                         ret = true;
103         }
104
105         return ret;
106 }
107
108 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
109                                            const union acpi_object *desc,
110                                            struct acpi_device_data *data)
111 {
112         int i;
113
114         /* Look for the ACPI data subnodes UUID. */
115         for (i = 0; i < desc->package.count; i += 2) {
116                 const union acpi_object *uuid, *links;
117
118                 uuid = &desc->package.elements[i];
119                 links = &desc->package.elements[i + 1];
120
121                 /*
122                  * The first element must be a UUID and the second one must be
123                  * a package.
124                  */
125                 if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
126                     || links->type != ACPI_TYPE_PACKAGE)
127                         break;
128
129                 if (memcmp(uuid->buffer.pointer, ads_uuid, sizeof(ads_uuid)))
130                         continue;
131
132                 return acpi_add_nondev_subnodes(scope, links, &data->subnodes);
133         }
134
135         return false;
136 }
137
138 static bool acpi_property_value_ok(const union acpi_object *value)
139 {
140         int j;
141
142         /*
143          * The value must be an integer, a string, a reference, or a package
144          * whose every element must be an integer, a string, or a reference.
145          */
146         switch (value->type) {
147         case ACPI_TYPE_INTEGER:
148         case ACPI_TYPE_STRING:
149         case ACPI_TYPE_LOCAL_REFERENCE:
150                 return true;
151
152         case ACPI_TYPE_PACKAGE:
153                 for (j = 0; j < value->package.count; j++)
154                         switch (value->package.elements[j].type) {
155                         case ACPI_TYPE_INTEGER:
156                         case ACPI_TYPE_STRING:
157                         case ACPI_TYPE_LOCAL_REFERENCE:
158                                 continue;
159
160                         default:
161                                 return false;
162                         }
163
164                 return true;
165         }
166         return false;
167 }
168
169 static bool acpi_properties_format_valid(const union acpi_object *properties)
170 {
171         int i;
172
173         for (i = 0; i < properties->package.count; i++) {
174                 const union acpi_object *property;
175
176                 property = &properties->package.elements[i];
177                 /*
178                  * Only two elements allowed, the first one must be a string and
179                  * the second one has to satisfy certain conditions.
180                  */
181                 if (property->package.count != 2
182                     || property->package.elements[0].type != ACPI_TYPE_STRING
183                     || !acpi_property_value_ok(&property->package.elements[1]))
184                         return false;
185         }
186         return true;
187 }
188
189 static void acpi_init_of_compatible(struct acpi_device *adev)
190 {
191         const union acpi_object *of_compatible;
192         int ret;
193
194         ret = acpi_dev_get_property_array(adev, "compatible", ACPI_TYPE_STRING,
195                                           &of_compatible);
196         if (ret) {
197                 ret = acpi_dev_get_property(adev, "compatible",
198                                             ACPI_TYPE_STRING, &of_compatible);
199                 if (ret) {
200                         if (adev->parent
201                             && adev->parent->flags.of_compatible_ok)
202                                 goto out;
203
204                         return;
205                 }
206         }
207         adev->data.of_compatible = of_compatible;
208
209  out:
210         adev->flags.of_compatible_ok = 1;
211 }
212
213 static bool acpi_extract_properties(const union acpi_object *desc,
214                                     struct acpi_device_data *data)
215 {
216         int i;
217
218         if (desc->package.count % 2)
219                 return false;
220
221         /* Look for the device properties UUID. */
222         for (i = 0; i < desc->package.count; i += 2) {
223                 const union acpi_object *uuid, *properties;
224
225                 uuid = &desc->package.elements[i];
226                 properties = &desc->package.elements[i + 1];
227
228                 /*
229                  * The first element must be a UUID and the second one must be
230                  * a package.
231                  */
232                 if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
233                     || properties->type != ACPI_TYPE_PACKAGE)
234                         break;
235
236                 if (memcmp(uuid->buffer.pointer, prp_uuid, sizeof(prp_uuid)))
237                         continue;
238
239                 /*
240                  * We found the matching UUID. Now validate the format of the
241                  * package immediately following it.
242                  */
243                 if (!acpi_properties_format_valid(properties))
244                         break;
245
246                 data->properties = properties;
247                 return true;
248         }
249
250         return false;
251 }
252
253 void acpi_init_properties(struct acpi_device *adev)
254 {
255         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
256         struct acpi_hardware_id *hwid;
257         acpi_status status;
258         bool acpi_of = false;
259
260         INIT_LIST_HEAD(&adev->data.subnodes);
261
262         /*
263          * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
264          * Device Tree compatible properties for this device.
265          */
266         list_for_each_entry(hwid, &adev->pnp.ids, list) {
267                 if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
268                         acpi_of = true;
269                         break;
270                 }
271         }
272
273         status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
274                                             ACPI_TYPE_PACKAGE);
275         if (ACPI_FAILURE(status))
276                 goto out;
277
278         if (acpi_extract_properties(buf.pointer, &adev->data)) {
279                 adev->data.pointer = buf.pointer;
280                 if (acpi_of)
281                         acpi_init_of_compatible(adev);
282         }
283         if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer, &adev->data))
284                 adev->data.pointer = buf.pointer;
285
286         if (!adev->data.pointer) {
287                 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
288                 ACPI_FREE(buf.pointer);
289         }
290
291  out:
292         if (acpi_of && !adev->flags.of_compatible_ok)
293                 acpi_handle_info(adev->handle,
294                          ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
295 }
296
297 static void acpi_destroy_nondev_subnodes(struct list_head *list)
298 {
299         struct acpi_data_node *dn, *next;
300
301         if (list_empty(list))
302                 return;
303
304         list_for_each_entry_safe_reverse(dn, next, list, sibling) {
305                 acpi_destroy_nondev_subnodes(&dn->data.subnodes);
306                 wait_for_completion(&dn->kobj_done);
307                 list_del(&dn->sibling);
308                 ACPI_FREE((void *)dn->data.pointer);
309                 kfree(dn);
310         }
311 }
312
313 void acpi_free_properties(struct acpi_device *adev)
314 {
315         acpi_destroy_nondev_subnodes(&adev->data.subnodes);
316         ACPI_FREE((void *)adev->data.pointer);
317         adev->data.of_compatible = NULL;
318         adev->data.pointer = NULL;
319         adev->data.properties = NULL;
320 }
321
322 /**
323  * acpi_dev_get_property - return an ACPI property with given name
324  * @adev: ACPI device to get property
325  * @name: Name of the property
326  * @type: Expected property type
327  * @obj: Location to store the property value (if not %NULL)
328  *
329  * Look up a property with @name and store a pointer to the resulting ACPI
330  * object at the location pointed to by @obj if found.
331  *
332  * Callers must not attempt to free the returned objects.  These objects will be
333  * freed by the ACPI core automatically during the removal of @adev.
334  *
335  * Return: %0 if property with @name has been found (success),
336  *         %-EINVAL if the arguments are invalid,
337  *         %-ENODATA if the property doesn't exist,
338  *         %-EPROTO if the property value type doesn't match @type.
339  */
340 int acpi_dev_get_property(struct acpi_device *adev, const char *name,
341                           acpi_object_type type, const union acpi_object **obj)
342 {
343         const union acpi_object *properties;
344         int i;
345
346         if (!adev || !name)
347                 return -EINVAL;
348
349         if (!adev->data.pointer || !adev->data.properties)
350                 return -ENODATA;
351
352         properties = adev->data.properties;
353         for (i = 0; i < properties->package.count; i++) {
354                 const union acpi_object *propname, *propvalue;
355                 const union acpi_object *property;
356
357                 property = &properties->package.elements[i];
358
359                 propname = &property->package.elements[0];
360                 propvalue = &property->package.elements[1];
361
362                 if (!strcmp(name, propname->string.pointer)) {
363                         if (type != ACPI_TYPE_ANY && propvalue->type != type)
364                                 return -EPROTO;
365                         else if (obj)
366                                 *obj = propvalue;
367
368                         return 0;
369                 }
370         }
371         return -ENODATA;
372 }
373 EXPORT_SYMBOL_GPL(acpi_dev_get_property);
374
375 /**
376  * acpi_dev_get_property_array - return an ACPI array property with given name
377  * @adev: ACPI device to get property
378  * @name: Name of the property
379  * @type: Expected type of array elements
380  * @obj: Location to store a pointer to the property value (if not NULL)
381  *
382  * Look up an array property with @name and store a pointer to the resulting
383  * ACPI object at the location pointed to by @obj if found.
384  *
385  * Callers must not attempt to free the returned objects.  Those objects will be
386  * freed by the ACPI core automatically during the removal of @adev.
387  *
388  * Return: %0 if array property (package) with @name has been found (success),
389  *         %-EINVAL if the arguments are invalid,
390  *         %-ENODATA if the property doesn't exist,
391  *         %-EPROTO if the property is not a package or the type of its elements
392  *           doesn't match @type.
393  */
394 int acpi_dev_get_property_array(struct acpi_device *adev, const char *name,
395                                 acpi_object_type type,
396                                 const union acpi_object **obj)
397 {
398         const union acpi_object *prop;
399         int ret, i;
400
401         ret = acpi_dev_get_property(adev, name, ACPI_TYPE_PACKAGE, &prop);
402         if (ret)
403                 return ret;
404
405         if (type != ACPI_TYPE_ANY) {
406                 /* Check that all elements are of correct type. */
407                 for (i = 0; i < prop->package.count; i++)
408                         if (prop->package.elements[i].type != type)
409                                 return -EPROTO;
410         }
411         if (obj)
412                 *obj = prop;
413
414         return 0;
415 }
416 EXPORT_SYMBOL_GPL(acpi_dev_get_property_array);
417
418 /**
419  * acpi_dev_get_property_reference - returns handle to the referenced object
420  * @adev: ACPI device to get property
421  * @name: Name of the property
422  * @index: Index of the reference to return
423  * @args: Location to store the returned reference with optional arguments
424  *
425  * Find property with @name, verifify that it is a package containing at least
426  * one object reference and if so, store the ACPI device object pointer to the
427  * target object in @args->adev.  If the reference includes arguments, store
428  * them in the @args->args[] array.
429  *
430  * If there's more than one reference in the property value package, @index is
431  * used to select the one to return.
432  *
433  * Return: %0 on success, negative error code on failure.
434  */
435 int acpi_dev_get_property_reference(struct acpi_device *adev,
436                                     const char *name, size_t index,
437                                     struct acpi_reference_args *args)
438 {
439         const union acpi_object *element, *end;
440         const union acpi_object *obj;
441         struct acpi_device *device;
442         int ret, idx = 0;
443
444         ret = acpi_dev_get_property(adev, name, ACPI_TYPE_ANY, &obj);
445         if (ret)
446                 return ret;
447
448         /*
449          * The simplest case is when the value is a single reference.  Just
450          * return that reference then.
451          */
452         if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
453                 if (index)
454                         return -EINVAL;
455
456                 ret = acpi_bus_get_device(obj->reference.handle, &device);
457                 if (ret)
458                         return ret;
459
460                 args->adev = device;
461                 args->nargs = 0;
462                 return 0;
463         }
464
465         /*
466          * If it is not a single reference, then it is a package of
467          * references followed by number of ints as follows:
468          *
469          *  Package () { REF, INT, REF, INT, INT }
470          *
471          * The index argument is then used to determine which reference
472          * the caller wants (along with the arguments).
473          */
474         if (obj->type != ACPI_TYPE_PACKAGE || index >= obj->package.count)
475                 return -EPROTO;
476
477         element = obj->package.elements;
478         end = element + obj->package.count;
479
480         while (element < end) {
481                 u32 nargs, i;
482
483                 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
484                         return -EPROTO;
485
486                 ret = acpi_bus_get_device(element->reference.handle, &device);
487                 if (ret)
488                         return -ENODEV;
489
490                 element++;
491                 nargs = 0;
492
493                 /* assume following integer elements are all args */
494                 for (i = 0; element + i < end; i++) {
495                         int type = element[i].type;
496
497                         if (type == ACPI_TYPE_INTEGER)
498                                 nargs++;
499                         else if (type == ACPI_TYPE_LOCAL_REFERENCE)
500                                 break;
501                         else
502                                 return -EPROTO;
503                 }
504
505                 if (idx++ == index) {
506                         args->adev = device;
507                         args->nargs = nargs;
508                         for (i = 0; i < nargs; i++)
509                                 args->args[i] = element[i].integer.value;
510
511                         return 0;
512                 }
513
514                 element += nargs;
515         }
516
517         return -EPROTO;
518 }
519 EXPORT_SYMBOL_GPL(acpi_dev_get_property_reference);
520
521 int acpi_dev_prop_get(struct acpi_device *adev, const char *propname,
522                       void **valptr)
523 {
524         return acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
525                                      (const union acpi_object **)valptr);
526 }
527
528 int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
529                               enum dev_prop_type proptype, void *val)
530 {
531         const union acpi_object *obj;
532         int ret;
533
534         if (!val)
535                 return -EINVAL;
536
537         if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
538                 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_INTEGER, &obj);
539                 if (ret)
540                         return ret;
541
542                 switch (proptype) {
543                 case DEV_PROP_U8:
544                         if (obj->integer.value > U8_MAX)
545                                 return -EOVERFLOW;
546                         *(u8 *)val = obj->integer.value;
547                         break;
548                 case DEV_PROP_U16:
549                         if (obj->integer.value > U16_MAX)
550                                 return -EOVERFLOW;
551                         *(u16 *)val = obj->integer.value;
552                         break;
553                 case DEV_PROP_U32:
554                         if (obj->integer.value > U32_MAX)
555                                 return -EOVERFLOW;
556                         *(u32 *)val = obj->integer.value;
557                         break;
558                 default:
559                         *(u64 *)val = obj->integer.value;
560                         break;
561                 }
562         } else if (proptype == DEV_PROP_STRING) {
563                 ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_STRING, &obj);
564                 if (ret)
565                         return ret;
566
567                 *(char **)val = obj->string.pointer;
568         } else {
569                 ret = -EINVAL;
570         }
571         return ret;
572 }
573
574 static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
575                                        size_t nval)
576 {
577         int i;
578
579         for (i = 0; i < nval; i++) {
580                 if (items[i].type != ACPI_TYPE_INTEGER)
581                         return -EPROTO;
582                 if (items[i].integer.value > U8_MAX)
583                         return -EOVERFLOW;
584
585                 val[i] = items[i].integer.value;
586         }
587         return 0;
588 }
589
590 static int acpi_copy_property_array_u16(const union acpi_object *items,
591                                         u16 *val, size_t nval)
592 {
593         int i;
594
595         for (i = 0; i < nval; i++) {
596                 if (items[i].type != ACPI_TYPE_INTEGER)
597                         return -EPROTO;
598                 if (items[i].integer.value > U16_MAX)
599                         return -EOVERFLOW;
600
601                 val[i] = items[i].integer.value;
602         }
603         return 0;
604 }
605
606 static int acpi_copy_property_array_u32(const union acpi_object *items,
607                                         u32 *val, size_t nval)
608 {
609         int i;
610
611         for (i = 0; i < nval; i++) {
612                 if (items[i].type != ACPI_TYPE_INTEGER)
613                         return -EPROTO;
614                 if (items[i].integer.value > U32_MAX)
615                         return -EOVERFLOW;
616
617                 val[i] = items[i].integer.value;
618         }
619         return 0;
620 }
621
622 static int acpi_copy_property_array_u64(const union acpi_object *items,
623                                         u64 *val, size_t nval)
624 {
625         int i;
626
627         for (i = 0; i < nval; i++) {
628                 if (items[i].type != ACPI_TYPE_INTEGER)
629                         return -EPROTO;
630
631                 val[i] = items[i].integer.value;
632         }
633         return 0;
634 }
635
636 static int acpi_copy_property_array_string(const union acpi_object *items,
637                                            char **val, size_t nval)
638 {
639         int i;
640
641         for (i = 0; i < nval; i++) {
642                 if (items[i].type != ACPI_TYPE_STRING)
643                         return -EPROTO;
644
645                 val[i] = items[i].string.pointer;
646         }
647         return 0;
648 }
649
650 int acpi_dev_prop_read(struct acpi_device *adev, const char *propname,
651                        enum dev_prop_type proptype, void *val, size_t nval)
652 {
653         const union acpi_object *obj;
654         const union acpi_object *items;
655         int ret;
656
657         if (val && nval == 1) {
658                 ret = acpi_dev_prop_read_single(adev, propname, proptype, val);
659                 if (!ret)
660                         return ret;
661         }
662
663         ret = acpi_dev_get_property_array(adev, propname, ACPI_TYPE_ANY, &obj);
664         if (ret)
665                 return ret;
666
667         if (!val)
668                 return obj->package.count;
669
670         if (nval > obj->package.count)
671                 return -EOVERFLOW;
672         else if (nval <= 0)
673                 return -EINVAL;
674
675         items = obj->package.elements;
676
677         switch (proptype) {
678         case DEV_PROP_U8:
679                 ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
680                 break;
681         case DEV_PROP_U16:
682                 ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
683                 break;
684         case DEV_PROP_U32:
685                 ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
686                 break;
687         case DEV_PROP_U64:
688                 ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
689                 break;
690         case DEV_PROP_STRING:
691                 ret = acpi_copy_property_array_string(items, (char **)val, nval);
692                 break;
693         default:
694                 ret = -EINVAL;
695                 break;
696         }
697         return ret;
698 }