1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * ACPI device specific properties support.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2014, Intel Corporation
6*4882a593Smuzhiyun * All rights reserved.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
9*4882a593Smuzhiyun * Darren Hart <dvhart@linux.intel.com>
10*4882a593Smuzhiyun * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/acpi.h>
14*4882a593Smuzhiyun #include <linux/device.h>
15*4882a593Smuzhiyun #include <linux/export.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include "internal.h"
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun static int acpi_data_get_property_array(const struct acpi_device_data *data,
20*4882a593Smuzhiyun const char *name,
21*4882a593Smuzhiyun acpi_object_type type,
22*4882a593Smuzhiyun const union acpi_object **obj);
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun * The GUIDs here are made equivalent to each other in order to avoid extra
26*4882a593Smuzhiyun * complexity in the properties handling code, with the caveat that the
27*4882a593Smuzhiyun * kernel will accept certain combinations of GUID and properties that are
28*4882a593Smuzhiyun * not defined without a warning. For instance if any of the properties
29*4882a593Smuzhiyun * from different GUID appear in a property list of another, it will be
30*4882a593Smuzhiyun * accepted by the kernel. Firmware validation tools should catch these.
31*4882a593Smuzhiyun */
32*4882a593Smuzhiyun static const guid_t prp_guids[] = {
33*4882a593Smuzhiyun /* ACPI _DSD device properties GUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
34*4882a593Smuzhiyun GUID_INIT(0xdaffd814, 0x6eba, 0x4d8c,
35*4882a593Smuzhiyun 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01),
36*4882a593Smuzhiyun /* Hotplug in D3 GUID: 6211e2c0-58a3-4af3-90e1-927a4e0c55a4 */
37*4882a593Smuzhiyun GUID_INIT(0x6211e2c0, 0x58a3, 0x4af3,
38*4882a593Smuzhiyun 0x90, 0xe1, 0x92, 0x7a, 0x4e, 0x0c, 0x55, 0xa4),
39*4882a593Smuzhiyun /* External facing port GUID: efcc06cc-73ac-4bc3-bff0-76143807c389 */
40*4882a593Smuzhiyun GUID_INIT(0xefcc06cc, 0x73ac, 0x4bc3,
41*4882a593Smuzhiyun 0xbf, 0xf0, 0x76, 0x14, 0x38, 0x07, 0xc3, 0x89),
42*4882a593Smuzhiyun /* Thunderbolt GUID for IMR_VALID: c44d002f-69f9-4e7d-a904-a7baabdf43f7 */
43*4882a593Smuzhiyun GUID_INIT(0xc44d002f, 0x69f9, 0x4e7d,
44*4882a593Smuzhiyun 0xa9, 0x04, 0xa7, 0xba, 0xab, 0xdf, 0x43, 0xf7),
45*4882a593Smuzhiyun /* Thunderbolt GUID for WAKE_SUPPORTED: 6c501103-c189-4296-ba72-9bf5a26ebe5d */
46*4882a593Smuzhiyun GUID_INIT(0x6c501103, 0xc189, 0x4296,
47*4882a593Smuzhiyun 0xba, 0x72, 0x9b, 0xf5, 0xa2, 0x6e, 0xbe, 0x5d),
48*4882a593Smuzhiyun /* Storage device needs D3 GUID: 5025030f-842f-4ab4-a561-99a5189762d0 */
49*4882a593Smuzhiyun GUID_INIT(0x5025030f, 0x842f, 0x4ab4,
50*4882a593Smuzhiyun 0xa5, 0x61, 0x99, 0xa5, 0x18, 0x97, 0x62, 0xd0),
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /* ACPI _DSD data subnodes GUID: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
54*4882a593Smuzhiyun static const guid_t ads_guid =
55*4882a593Smuzhiyun GUID_INIT(0xdbb8e3e6, 0x5886, 0x4ba6,
56*4882a593Smuzhiyun 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
59*4882a593Smuzhiyun const union acpi_object *desc,
60*4882a593Smuzhiyun struct acpi_device_data *data,
61*4882a593Smuzhiyun struct fwnode_handle *parent);
62*4882a593Smuzhiyun static bool acpi_extract_properties(const union acpi_object *desc,
63*4882a593Smuzhiyun struct acpi_device_data *data);
64*4882a593Smuzhiyun
acpi_nondev_subnode_extract(const union acpi_object * desc,acpi_handle handle,const union acpi_object * link,struct list_head * list,struct fwnode_handle * parent)65*4882a593Smuzhiyun static bool acpi_nondev_subnode_extract(const union acpi_object *desc,
66*4882a593Smuzhiyun acpi_handle handle,
67*4882a593Smuzhiyun const union acpi_object *link,
68*4882a593Smuzhiyun struct list_head *list,
69*4882a593Smuzhiyun struct fwnode_handle *parent)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun struct acpi_data_node *dn;
72*4882a593Smuzhiyun bool result;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun dn = kzalloc(sizeof(*dn), GFP_KERNEL);
75*4882a593Smuzhiyun if (!dn)
76*4882a593Smuzhiyun return false;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun dn->name = link->package.elements[0].string.pointer;
79*4882a593Smuzhiyun fwnode_init(&dn->fwnode, &acpi_data_fwnode_ops);
80*4882a593Smuzhiyun dn->parent = parent;
81*4882a593Smuzhiyun INIT_LIST_HEAD(&dn->data.properties);
82*4882a593Smuzhiyun INIT_LIST_HEAD(&dn->data.subnodes);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun result = acpi_extract_properties(desc, &dn->data);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun if (handle) {
87*4882a593Smuzhiyun acpi_handle scope;
88*4882a593Smuzhiyun acpi_status status;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /*
91*4882a593Smuzhiyun * The scope for the subnode object lookup is the one of the
92*4882a593Smuzhiyun * namespace node (device) containing the object that has
93*4882a593Smuzhiyun * returned the package. That is, it's the scope of that
94*4882a593Smuzhiyun * object's parent.
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun status = acpi_get_parent(handle, &scope);
97*4882a593Smuzhiyun if (ACPI_SUCCESS(status)
98*4882a593Smuzhiyun && acpi_enumerate_nondev_subnodes(scope, desc, &dn->data,
99*4882a593Smuzhiyun &dn->fwnode))
100*4882a593Smuzhiyun result = true;
101*4882a593Smuzhiyun } else if (acpi_enumerate_nondev_subnodes(NULL, desc, &dn->data,
102*4882a593Smuzhiyun &dn->fwnode)) {
103*4882a593Smuzhiyun result = true;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun if (result) {
107*4882a593Smuzhiyun dn->handle = handle;
108*4882a593Smuzhiyun dn->data.pointer = desc;
109*4882a593Smuzhiyun list_add_tail(&dn->sibling, list);
110*4882a593Smuzhiyun return true;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun kfree(dn);
114*4882a593Smuzhiyun acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
115*4882a593Smuzhiyun return false;
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun
acpi_nondev_subnode_data_ok(acpi_handle handle,const union acpi_object * link,struct list_head * list,struct fwnode_handle * parent)118*4882a593Smuzhiyun static bool acpi_nondev_subnode_data_ok(acpi_handle handle,
119*4882a593Smuzhiyun const union acpi_object *link,
120*4882a593Smuzhiyun struct list_head *list,
121*4882a593Smuzhiyun struct fwnode_handle *parent)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
124*4882a593Smuzhiyun acpi_status status;
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
127*4882a593Smuzhiyun ACPI_TYPE_PACKAGE);
128*4882a593Smuzhiyun if (ACPI_FAILURE(status))
129*4882a593Smuzhiyun return false;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list,
132*4882a593Smuzhiyun parent))
133*4882a593Smuzhiyun return true;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun ACPI_FREE(buf.pointer);
136*4882a593Smuzhiyun return false;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
acpi_nondev_subnode_ok(acpi_handle scope,const union acpi_object * link,struct list_head * list,struct fwnode_handle * parent)139*4882a593Smuzhiyun static bool acpi_nondev_subnode_ok(acpi_handle scope,
140*4882a593Smuzhiyun const union acpi_object *link,
141*4882a593Smuzhiyun struct list_head *list,
142*4882a593Smuzhiyun struct fwnode_handle *parent)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun acpi_handle handle;
145*4882a593Smuzhiyun acpi_status status;
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun if (!scope)
148*4882a593Smuzhiyun return false;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
151*4882a593Smuzhiyun &handle);
152*4882a593Smuzhiyun if (ACPI_FAILURE(status))
153*4882a593Smuzhiyun return false;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun return acpi_nondev_subnode_data_ok(handle, link, list, parent);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
acpi_add_nondev_subnodes(acpi_handle scope,const union acpi_object * links,struct list_head * list,struct fwnode_handle * parent)158*4882a593Smuzhiyun static bool acpi_add_nondev_subnodes(acpi_handle scope,
159*4882a593Smuzhiyun const union acpi_object *links,
160*4882a593Smuzhiyun struct list_head *list,
161*4882a593Smuzhiyun struct fwnode_handle *parent)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun bool ret = false;
164*4882a593Smuzhiyun int i;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun for (i = 0; i < links->package.count; i++) {
167*4882a593Smuzhiyun const union acpi_object *link, *desc;
168*4882a593Smuzhiyun acpi_handle handle;
169*4882a593Smuzhiyun bool result;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun link = &links->package.elements[i];
172*4882a593Smuzhiyun /* Only two elements allowed. */
173*4882a593Smuzhiyun if (link->package.count != 2)
174*4882a593Smuzhiyun continue;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /* The first one must be a string. */
177*4882a593Smuzhiyun if (link->package.elements[0].type != ACPI_TYPE_STRING)
178*4882a593Smuzhiyun continue;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /* The second one may be a string, a reference or a package. */
181*4882a593Smuzhiyun switch (link->package.elements[1].type) {
182*4882a593Smuzhiyun case ACPI_TYPE_STRING:
183*4882a593Smuzhiyun result = acpi_nondev_subnode_ok(scope, link, list,
184*4882a593Smuzhiyun parent);
185*4882a593Smuzhiyun break;
186*4882a593Smuzhiyun case ACPI_TYPE_LOCAL_REFERENCE:
187*4882a593Smuzhiyun handle = link->package.elements[1].reference.handle;
188*4882a593Smuzhiyun result = acpi_nondev_subnode_data_ok(handle, link, list,
189*4882a593Smuzhiyun parent);
190*4882a593Smuzhiyun break;
191*4882a593Smuzhiyun case ACPI_TYPE_PACKAGE:
192*4882a593Smuzhiyun desc = &link->package.elements[1];
193*4882a593Smuzhiyun result = acpi_nondev_subnode_extract(desc, NULL, link,
194*4882a593Smuzhiyun list, parent);
195*4882a593Smuzhiyun break;
196*4882a593Smuzhiyun default:
197*4882a593Smuzhiyun result = false;
198*4882a593Smuzhiyun break;
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun ret = ret || result;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun return ret;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
acpi_enumerate_nondev_subnodes(acpi_handle scope,const union acpi_object * desc,struct acpi_device_data * data,struct fwnode_handle * parent)206*4882a593Smuzhiyun static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
207*4882a593Smuzhiyun const union acpi_object *desc,
208*4882a593Smuzhiyun struct acpi_device_data *data,
209*4882a593Smuzhiyun struct fwnode_handle *parent)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun int i;
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /* Look for the ACPI data subnodes GUID. */
214*4882a593Smuzhiyun for (i = 0; i < desc->package.count; i += 2) {
215*4882a593Smuzhiyun const union acpi_object *guid, *links;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun guid = &desc->package.elements[i];
218*4882a593Smuzhiyun links = &desc->package.elements[i + 1];
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /*
221*4882a593Smuzhiyun * The first element must be a GUID and the second one must be
222*4882a593Smuzhiyun * a package.
223*4882a593Smuzhiyun */
224*4882a593Smuzhiyun if (guid->type != ACPI_TYPE_BUFFER ||
225*4882a593Smuzhiyun guid->buffer.length != 16 ||
226*4882a593Smuzhiyun links->type != ACPI_TYPE_PACKAGE)
227*4882a593Smuzhiyun break;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun if (!guid_equal((guid_t *)guid->buffer.pointer, &ads_guid))
230*4882a593Smuzhiyun continue;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun return acpi_add_nondev_subnodes(scope, links, &data->subnodes,
233*4882a593Smuzhiyun parent);
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun return false;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
acpi_property_value_ok(const union acpi_object * value)239*4882a593Smuzhiyun static bool acpi_property_value_ok(const union acpi_object *value)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun int j;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /*
244*4882a593Smuzhiyun * The value must be an integer, a string, a reference, or a package
245*4882a593Smuzhiyun * whose every element must be an integer, a string, or a reference.
246*4882a593Smuzhiyun */
247*4882a593Smuzhiyun switch (value->type) {
248*4882a593Smuzhiyun case ACPI_TYPE_INTEGER:
249*4882a593Smuzhiyun case ACPI_TYPE_STRING:
250*4882a593Smuzhiyun case ACPI_TYPE_LOCAL_REFERENCE:
251*4882a593Smuzhiyun return true;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun case ACPI_TYPE_PACKAGE:
254*4882a593Smuzhiyun for (j = 0; j < value->package.count; j++)
255*4882a593Smuzhiyun switch (value->package.elements[j].type) {
256*4882a593Smuzhiyun case ACPI_TYPE_INTEGER:
257*4882a593Smuzhiyun case ACPI_TYPE_STRING:
258*4882a593Smuzhiyun case ACPI_TYPE_LOCAL_REFERENCE:
259*4882a593Smuzhiyun continue;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun default:
262*4882a593Smuzhiyun return false;
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun return true;
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun return false;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun
acpi_properties_format_valid(const union acpi_object * properties)270*4882a593Smuzhiyun static bool acpi_properties_format_valid(const union acpi_object *properties)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun int i;
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun for (i = 0; i < properties->package.count; i++) {
275*4882a593Smuzhiyun const union acpi_object *property;
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun property = &properties->package.elements[i];
278*4882a593Smuzhiyun /*
279*4882a593Smuzhiyun * Only two elements allowed, the first one must be a string and
280*4882a593Smuzhiyun * the second one has to satisfy certain conditions.
281*4882a593Smuzhiyun */
282*4882a593Smuzhiyun if (property->package.count != 2
283*4882a593Smuzhiyun || property->package.elements[0].type != ACPI_TYPE_STRING
284*4882a593Smuzhiyun || !acpi_property_value_ok(&property->package.elements[1]))
285*4882a593Smuzhiyun return false;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun return true;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
acpi_init_of_compatible(struct acpi_device * adev)290*4882a593Smuzhiyun static void acpi_init_of_compatible(struct acpi_device *adev)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun const union acpi_object *of_compatible;
293*4882a593Smuzhiyun int ret;
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun ret = acpi_data_get_property_array(&adev->data, "compatible",
296*4882a593Smuzhiyun ACPI_TYPE_STRING, &of_compatible);
297*4882a593Smuzhiyun if (ret) {
298*4882a593Smuzhiyun ret = acpi_dev_get_property(adev, "compatible",
299*4882a593Smuzhiyun ACPI_TYPE_STRING, &of_compatible);
300*4882a593Smuzhiyun if (ret) {
301*4882a593Smuzhiyun if (adev->parent
302*4882a593Smuzhiyun && adev->parent->flags.of_compatible_ok)
303*4882a593Smuzhiyun goto out;
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun return;
306*4882a593Smuzhiyun }
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun adev->data.of_compatible = of_compatible;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun out:
311*4882a593Smuzhiyun adev->flags.of_compatible_ok = 1;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun
acpi_is_property_guid(const guid_t * guid)314*4882a593Smuzhiyun static bool acpi_is_property_guid(const guid_t *guid)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun int i;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(prp_guids); i++) {
319*4882a593Smuzhiyun if (guid_equal(guid, &prp_guids[i]))
320*4882a593Smuzhiyun return true;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun return false;
324*4882a593Smuzhiyun }
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun struct acpi_device_properties *
acpi_data_add_props(struct acpi_device_data * data,const guid_t * guid,const union acpi_object * properties)327*4882a593Smuzhiyun acpi_data_add_props(struct acpi_device_data *data, const guid_t *guid,
328*4882a593Smuzhiyun const union acpi_object *properties)
329*4882a593Smuzhiyun {
330*4882a593Smuzhiyun struct acpi_device_properties *props;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun props = kzalloc(sizeof(*props), GFP_KERNEL);
333*4882a593Smuzhiyun if (props) {
334*4882a593Smuzhiyun INIT_LIST_HEAD(&props->list);
335*4882a593Smuzhiyun props->guid = guid;
336*4882a593Smuzhiyun props->properties = properties;
337*4882a593Smuzhiyun list_add_tail(&props->list, &data->properties);
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun return props;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
acpi_extract_properties(const union acpi_object * desc,struct acpi_device_data * data)343*4882a593Smuzhiyun static bool acpi_extract_properties(const union acpi_object *desc,
344*4882a593Smuzhiyun struct acpi_device_data *data)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun int i;
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun if (desc->package.count % 2)
349*4882a593Smuzhiyun return false;
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun /* Look for the device properties GUID. */
352*4882a593Smuzhiyun for (i = 0; i < desc->package.count; i += 2) {
353*4882a593Smuzhiyun const union acpi_object *guid, *properties;
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun guid = &desc->package.elements[i];
356*4882a593Smuzhiyun properties = &desc->package.elements[i + 1];
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun /*
359*4882a593Smuzhiyun * The first element must be a GUID and the second one must be
360*4882a593Smuzhiyun * a package.
361*4882a593Smuzhiyun */
362*4882a593Smuzhiyun if (guid->type != ACPI_TYPE_BUFFER ||
363*4882a593Smuzhiyun guid->buffer.length != 16 ||
364*4882a593Smuzhiyun properties->type != ACPI_TYPE_PACKAGE)
365*4882a593Smuzhiyun break;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun if (!acpi_is_property_guid((guid_t *)guid->buffer.pointer))
368*4882a593Smuzhiyun continue;
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun /*
371*4882a593Smuzhiyun * We found the matching GUID. Now validate the format of the
372*4882a593Smuzhiyun * package immediately following it.
373*4882a593Smuzhiyun */
374*4882a593Smuzhiyun if (!acpi_properties_format_valid(properties))
375*4882a593Smuzhiyun continue;
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun acpi_data_add_props(data, (const guid_t *)guid->buffer.pointer,
378*4882a593Smuzhiyun properties);
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun return !list_empty(&data->properties);
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun
acpi_init_properties(struct acpi_device * adev)384*4882a593Smuzhiyun void acpi_init_properties(struct acpi_device *adev)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
387*4882a593Smuzhiyun struct acpi_hardware_id *hwid;
388*4882a593Smuzhiyun acpi_status status;
389*4882a593Smuzhiyun bool acpi_of = false;
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun INIT_LIST_HEAD(&adev->data.properties);
392*4882a593Smuzhiyun INIT_LIST_HEAD(&adev->data.subnodes);
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun if (!adev->handle)
395*4882a593Smuzhiyun return;
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /*
398*4882a593Smuzhiyun * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
399*4882a593Smuzhiyun * Device Tree compatible properties for this device.
400*4882a593Smuzhiyun */
401*4882a593Smuzhiyun list_for_each_entry(hwid, &adev->pnp.ids, list) {
402*4882a593Smuzhiyun if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
403*4882a593Smuzhiyun acpi_of = true;
404*4882a593Smuzhiyun break;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
409*4882a593Smuzhiyun ACPI_TYPE_PACKAGE);
410*4882a593Smuzhiyun if (ACPI_FAILURE(status))
411*4882a593Smuzhiyun goto out;
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun if (acpi_extract_properties(buf.pointer, &adev->data)) {
414*4882a593Smuzhiyun adev->data.pointer = buf.pointer;
415*4882a593Smuzhiyun if (acpi_of)
416*4882a593Smuzhiyun acpi_init_of_compatible(adev);
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
419*4882a593Smuzhiyun &adev->data, acpi_fwnode_handle(adev)))
420*4882a593Smuzhiyun adev->data.pointer = buf.pointer;
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun if (!adev->data.pointer) {
423*4882a593Smuzhiyun acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
424*4882a593Smuzhiyun ACPI_FREE(buf.pointer);
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun out:
428*4882a593Smuzhiyun if (acpi_of && !adev->flags.of_compatible_ok)
429*4882a593Smuzhiyun acpi_handle_info(adev->handle,
430*4882a593Smuzhiyun ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun if (!adev->data.pointer)
433*4882a593Smuzhiyun acpi_extract_apple_properties(adev);
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun
acpi_free_device_properties(struct list_head * list)436*4882a593Smuzhiyun static void acpi_free_device_properties(struct list_head *list)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun struct acpi_device_properties *props, *tmp;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun list_for_each_entry_safe(props, tmp, list, list) {
441*4882a593Smuzhiyun list_del(&props->list);
442*4882a593Smuzhiyun kfree(props);
443*4882a593Smuzhiyun }
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun
acpi_destroy_nondev_subnodes(struct list_head * list)446*4882a593Smuzhiyun static void acpi_destroy_nondev_subnodes(struct list_head *list)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun struct acpi_data_node *dn, *next;
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun if (list_empty(list))
451*4882a593Smuzhiyun return;
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun list_for_each_entry_safe_reverse(dn, next, list, sibling) {
454*4882a593Smuzhiyun acpi_destroy_nondev_subnodes(&dn->data.subnodes);
455*4882a593Smuzhiyun wait_for_completion(&dn->kobj_done);
456*4882a593Smuzhiyun list_del(&dn->sibling);
457*4882a593Smuzhiyun ACPI_FREE((void *)dn->data.pointer);
458*4882a593Smuzhiyun acpi_free_device_properties(&dn->data.properties);
459*4882a593Smuzhiyun kfree(dn);
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun
acpi_free_properties(struct acpi_device * adev)463*4882a593Smuzhiyun void acpi_free_properties(struct acpi_device *adev)
464*4882a593Smuzhiyun {
465*4882a593Smuzhiyun acpi_destroy_nondev_subnodes(&adev->data.subnodes);
466*4882a593Smuzhiyun ACPI_FREE((void *)adev->data.pointer);
467*4882a593Smuzhiyun adev->data.of_compatible = NULL;
468*4882a593Smuzhiyun adev->data.pointer = NULL;
469*4882a593Smuzhiyun acpi_free_device_properties(&adev->data.properties);
470*4882a593Smuzhiyun }
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun /**
473*4882a593Smuzhiyun * acpi_data_get_property - return an ACPI property with given name
474*4882a593Smuzhiyun * @data: ACPI device deta object to get the property from
475*4882a593Smuzhiyun * @name: Name of the property
476*4882a593Smuzhiyun * @type: Expected property type
477*4882a593Smuzhiyun * @obj: Location to store the property value (if not %NULL)
478*4882a593Smuzhiyun *
479*4882a593Smuzhiyun * Look up a property with @name and store a pointer to the resulting ACPI
480*4882a593Smuzhiyun * object at the location pointed to by @obj if found.
481*4882a593Smuzhiyun *
482*4882a593Smuzhiyun * Callers must not attempt to free the returned objects. These objects will be
483*4882a593Smuzhiyun * freed by the ACPI core automatically during the removal of @data.
484*4882a593Smuzhiyun *
485*4882a593Smuzhiyun * Return: %0 if property with @name has been found (success),
486*4882a593Smuzhiyun * %-EINVAL if the arguments are invalid,
487*4882a593Smuzhiyun * %-EINVAL if the property doesn't exist,
488*4882a593Smuzhiyun * %-EPROTO if the property value type doesn't match @type.
489*4882a593Smuzhiyun */
acpi_data_get_property(const struct acpi_device_data * data,const char * name,acpi_object_type type,const union acpi_object ** obj)490*4882a593Smuzhiyun static int acpi_data_get_property(const struct acpi_device_data *data,
491*4882a593Smuzhiyun const char *name, acpi_object_type type,
492*4882a593Smuzhiyun const union acpi_object **obj)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun const struct acpi_device_properties *props;
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun if (!data || !name)
497*4882a593Smuzhiyun return -EINVAL;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun if (!data->pointer || list_empty(&data->properties))
500*4882a593Smuzhiyun return -EINVAL;
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun list_for_each_entry(props, &data->properties, list) {
503*4882a593Smuzhiyun const union acpi_object *properties;
504*4882a593Smuzhiyun unsigned int i;
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun properties = props->properties;
507*4882a593Smuzhiyun for (i = 0; i < properties->package.count; i++) {
508*4882a593Smuzhiyun const union acpi_object *propname, *propvalue;
509*4882a593Smuzhiyun const union acpi_object *property;
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun property = &properties->package.elements[i];
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun propname = &property->package.elements[0];
514*4882a593Smuzhiyun propvalue = &property->package.elements[1];
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun if (!strcmp(name, propname->string.pointer)) {
517*4882a593Smuzhiyun if (type != ACPI_TYPE_ANY &&
518*4882a593Smuzhiyun propvalue->type != type)
519*4882a593Smuzhiyun return -EPROTO;
520*4882a593Smuzhiyun if (obj)
521*4882a593Smuzhiyun *obj = propvalue;
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun return 0;
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun }
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun return -EINVAL;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun /**
531*4882a593Smuzhiyun * acpi_dev_get_property - return an ACPI property with given name.
532*4882a593Smuzhiyun * @adev: ACPI device to get the property from.
533*4882a593Smuzhiyun * @name: Name of the property.
534*4882a593Smuzhiyun * @type: Expected property type.
535*4882a593Smuzhiyun * @obj: Location to store the property value (if not %NULL).
536*4882a593Smuzhiyun */
acpi_dev_get_property(const struct acpi_device * adev,const char * name,acpi_object_type type,const union acpi_object ** obj)537*4882a593Smuzhiyun int acpi_dev_get_property(const struct acpi_device *adev, const char *name,
538*4882a593Smuzhiyun acpi_object_type type, const union acpi_object **obj)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(acpi_dev_get_property);
543*4882a593Smuzhiyun
544*4882a593Smuzhiyun static const struct acpi_device_data *
acpi_device_data_of_node(const struct fwnode_handle * fwnode)545*4882a593Smuzhiyun acpi_device_data_of_node(const struct fwnode_handle *fwnode)
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun if (is_acpi_device_node(fwnode)) {
548*4882a593Smuzhiyun const struct acpi_device *adev = to_acpi_device_node(fwnode);
549*4882a593Smuzhiyun return &adev->data;
550*4882a593Smuzhiyun } else if (is_acpi_data_node(fwnode)) {
551*4882a593Smuzhiyun const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
552*4882a593Smuzhiyun return &dn->data;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun return NULL;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun /**
558*4882a593Smuzhiyun * acpi_node_prop_get - return an ACPI property with given name.
559*4882a593Smuzhiyun * @fwnode: Firmware node to get the property from.
560*4882a593Smuzhiyun * @propname: Name of the property.
561*4882a593Smuzhiyun * @valptr: Location to store a pointer to the property value (if not %NULL).
562*4882a593Smuzhiyun */
acpi_node_prop_get(const struct fwnode_handle * fwnode,const char * propname,void ** valptr)563*4882a593Smuzhiyun int acpi_node_prop_get(const struct fwnode_handle *fwnode,
564*4882a593Smuzhiyun const char *propname, void **valptr)
565*4882a593Smuzhiyun {
566*4882a593Smuzhiyun return acpi_data_get_property(acpi_device_data_of_node(fwnode),
567*4882a593Smuzhiyun propname, ACPI_TYPE_ANY,
568*4882a593Smuzhiyun (const union acpi_object **)valptr);
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun /**
572*4882a593Smuzhiyun * acpi_data_get_property_array - return an ACPI array property with given name
573*4882a593Smuzhiyun * @adev: ACPI data object to get the property from
574*4882a593Smuzhiyun * @name: Name of the property
575*4882a593Smuzhiyun * @type: Expected type of array elements
576*4882a593Smuzhiyun * @obj: Location to store a pointer to the property value (if not NULL)
577*4882a593Smuzhiyun *
578*4882a593Smuzhiyun * Look up an array property with @name and store a pointer to the resulting
579*4882a593Smuzhiyun * ACPI object at the location pointed to by @obj if found.
580*4882a593Smuzhiyun *
581*4882a593Smuzhiyun * Callers must not attempt to free the returned objects. Those objects will be
582*4882a593Smuzhiyun * freed by the ACPI core automatically during the removal of @data.
583*4882a593Smuzhiyun *
584*4882a593Smuzhiyun * Return: %0 if array property (package) with @name has been found (success),
585*4882a593Smuzhiyun * %-EINVAL if the arguments are invalid,
586*4882a593Smuzhiyun * %-EINVAL if the property doesn't exist,
587*4882a593Smuzhiyun * %-EPROTO if the property is not a package or the type of its elements
588*4882a593Smuzhiyun * doesn't match @type.
589*4882a593Smuzhiyun */
acpi_data_get_property_array(const struct acpi_device_data * data,const char * name,acpi_object_type type,const union acpi_object ** obj)590*4882a593Smuzhiyun static int acpi_data_get_property_array(const struct acpi_device_data *data,
591*4882a593Smuzhiyun const char *name,
592*4882a593Smuzhiyun acpi_object_type type,
593*4882a593Smuzhiyun const union acpi_object **obj)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun const union acpi_object *prop;
596*4882a593Smuzhiyun int ret, i;
597*4882a593Smuzhiyun
598*4882a593Smuzhiyun ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
599*4882a593Smuzhiyun if (ret)
600*4882a593Smuzhiyun return ret;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun if (type != ACPI_TYPE_ANY) {
603*4882a593Smuzhiyun /* Check that all elements are of correct type. */
604*4882a593Smuzhiyun for (i = 0; i < prop->package.count; i++)
605*4882a593Smuzhiyun if (prop->package.elements[i].type != type)
606*4882a593Smuzhiyun return -EPROTO;
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun if (obj)
609*4882a593Smuzhiyun *obj = prop;
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun return 0;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun static struct fwnode_handle *
acpi_fwnode_get_named_child_node(const struct fwnode_handle * fwnode,const char * childname)615*4882a593Smuzhiyun acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
616*4882a593Smuzhiyun const char *childname)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun struct fwnode_handle *child;
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun fwnode_for_each_child_node(fwnode, child) {
621*4882a593Smuzhiyun if (is_acpi_data_node(child)) {
622*4882a593Smuzhiyun if (acpi_data_node_match(child, childname))
623*4882a593Smuzhiyun return child;
624*4882a593Smuzhiyun continue;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun if (!strncmp(acpi_device_bid(to_acpi_device_node(child)),
628*4882a593Smuzhiyun childname, ACPI_NAMESEG_SIZE))
629*4882a593Smuzhiyun return child;
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun return NULL;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun /**
636*4882a593Smuzhiyun * __acpi_node_get_property_reference - returns handle to the referenced object
637*4882a593Smuzhiyun * @fwnode: Firmware node to get the property from
638*4882a593Smuzhiyun * @propname: Name of the property
639*4882a593Smuzhiyun * @index: Index of the reference to return
640*4882a593Smuzhiyun * @num_args: Maximum number of arguments after each reference
641*4882a593Smuzhiyun * @args: Location to store the returned reference with optional arguments
642*4882a593Smuzhiyun *
643*4882a593Smuzhiyun * Find property with @name, verifify that it is a package containing at least
644*4882a593Smuzhiyun * one object reference and if so, store the ACPI device object pointer to the
645*4882a593Smuzhiyun * target object in @args->adev. If the reference includes arguments, store
646*4882a593Smuzhiyun * them in the @args->args[] array.
647*4882a593Smuzhiyun *
648*4882a593Smuzhiyun * If there's more than one reference in the property value package, @index is
649*4882a593Smuzhiyun * used to select the one to return.
650*4882a593Smuzhiyun *
651*4882a593Smuzhiyun * It is possible to leave holes in the property value set like in the
652*4882a593Smuzhiyun * example below:
653*4882a593Smuzhiyun *
654*4882a593Smuzhiyun * Package () {
655*4882a593Smuzhiyun * "cs-gpios",
656*4882a593Smuzhiyun * Package () {
657*4882a593Smuzhiyun * ^GPIO, 19, 0, 0,
658*4882a593Smuzhiyun * ^GPIO, 20, 0, 0,
659*4882a593Smuzhiyun * 0,
660*4882a593Smuzhiyun * ^GPIO, 21, 0, 0,
661*4882a593Smuzhiyun * }
662*4882a593Smuzhiyun * }
663*4882a593Smuzhiyun *
664*4882a593Smuzhiyun * Calling this function with index %2 or index %3 return %-ENOENT. If the
665*4882a593Smuzhiyun * property does not contain any more values %-ENOENT is returned. The NULL
666*4882a593Smuzhiyun * entry must be single integer and preferably contain value %0.
667*4882a593Smuzhiyun *
668*4882a593Smuzhiyun * Return: %0 on success, negative error code on failure.
669*4882a593Smuzhiyun */
__acpi_node_get_property_reference(const struct fwnode_handle * fwnode,const char * propname,size_t index,size_t num_args,struct fwnode_reference_args * args)670*4882a593Smuzhiyun int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode,
671*4882a593Smuzhiyun const char *propname, size_t index, size_t num_args,
672*4882a593Smuzhiyun struct fwnode_reference_args *args)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun const union acpi_object *element, *end;
675*4882a593Smuzhiyun const union acpi_object *obj;
676*4882a593Smuzhiyun const struct acpi_device_data *data;
677*4882a593Smuzhiyun struct acpi_device *device;
678*4882a593Smuzhiyun int ret, idx = 0;
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun data = acpi_device_data_of_node(fwnode);
681*4882a593Smuzhiyun if (!data)
682*4882a593Smuzhiyun return -ENOENT;
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
685*4882a593Smuzhiyun if (ret)
686*4882a593Smuzhiyun return ret == -EINVAL ? -ENOENT : -EINVAL;
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun /*
689*4882a593Smuzhiyun * The simplest case is when the value is a single reference. Just
690*4882a593Smuzhiyun * return that reference then.
691*4882a593Smuzhiyun */
692*4882a593Smuzhiyun if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
693*4882a593Smuzhiyun if (index)
694*4882a593Smuzhiyun return -ENOENT;
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun ret = acpi_bus_get_device(obj->reference.handle, &device);
697*4882a593Smuzhiyun if (ret)
698*4882a593Smuzhiyun return ret == -ENODEV ? -EINVAL : ret;
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun args->fwnode = acpi_fwnode_handle(device);
701*4882a593Smuzhiyun args->nargs = 0;
702*4882a593Smuzhiyun return 0;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun /*
706*4882a593Smuzhiyun * If it is not a single reference, then it is a package of
707*4882a593Smuzhiyun * references followed by number of ints as follows:
708*4882a593Smuzhiyun *
709*4882a593Smuzhiyun * Package () { REF, INT, REF, INT, INT }
710*4882a593Smuzhiyun *
711*4882a593Smuzhiyun * The index argument is then used to determine which reference
712*4882a593Smuzhiyun * the caller wants (along with the arguments).
713*4882a593Smuzhiyun */
714*4882a593Smuzhiyun if (obj->type != ACPI_TYPE_PACKAGE)
715*4882a593Smuzhiyun return -EINVAL;
716*4882a593Smuzhiyun if (index >= obj->package.count)
717*4882a593Smuzhiyun return -ENOENT;
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun element = obj->package.elements;
720*4882a593Smuzhiyun end = element + obj->package.count;
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun while (element < end) {
723*4882a593Smuzhiyun u32 nargs, i;
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun if (element->type == ACPI_TYPE_LOCAL_REFERENCE) {
726*4882a593Smuzhiyun struct fwnode_handle *ref_fwnode;
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun ret = acpi_bus_get_device(element->reference.handle,
729*4882a593Smuzhiyun &device);
730*4882a593Smuzhiyun if (ret)
731*4882a593Smuzhiyun return -EINVAL;
732*4882a593Smuzhiyun
733*4882a593Smuzhiyun nargs = 0;
734*4882a593Smuzhiyun element++;
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun /*
737*4882a593Smuzhiyun * Find the referred data extension node under the
738*4882a593Smuzhiyun * referred device node.
739*4882a593Smuzhiyun */
740*4882a593Smuzhiyun for (ref_fwnode = acpi_fwnode_handle(device);
741*4882a593Smuzhiyun element < end && element->type == ACPI_TYPE_STRING;
742*4882a593Smuzhiyun element++) {
743*4882a593Smuzhiyun ref_fwnode = acpi_fwnode_get_named_child_node(
744*4882a593Smuzhiyun ref_fwnode, element->string.pointer);
745*4882a593Smuzhiyun if (!ref_fwnode)
746*4882a593Smuzhiyun return -EINVAL;
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun /* assume following integer elements are all args */
750*4882a593Smuzhiyun for (i = 0; element + i < end && i < num_args; i++) {
751*4882a593Smuzhiyun int type = element[i].type;
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun if (type == ACPI_TYPE_INTEGER)
754*4882a593Smuzhiyun nargs++;
755*4882a593Smuzhiyun else if (type == ACPI_TYPE_LOCAL_REFERENCE)
756*4882a593Smuzhiyun break;
757*4882a593Smuzhiyun else
758*4882a593Smuzhiyun return -EINVAL;
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun
761*4882a593Smuzhiyun if (nargs > NR_FWNODE_REFERENCE_ARGS)
762*4882a593Smuzhiyun return -EINVAL;
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun if (idx == index) {
765*4882a593Smuzhiyun args->fwnode = ref_fwnode;
766*4882a593Smuzhiyun args->nargs = nargs;
767*4882a593Smuzhiyun for (i = 0; i < nargs; i++)
768*4882a593Smuzhiyun args->args[i] = element[i].integer.value;
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun return 0;
771*4882a593Smuzhiyun }
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun element += nargs;
774*4882a593Smuzhiyun } else if (element->type == ACPI_TYPE_INTEGER) {
775*4882a593Smuzhiyun if (idx == index)
776*4882a593Smuzhiyun return -ENOENT;
777*4882a593Smuzhiyun element++;
778*4882a593Smuzhiyun } else {
779*4882a593Smuzhiyun return -EINVAL;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun idx++;
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun
785*4882a593Smuzhiyun return -ENOENT;
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
788*4882a593Smuzhiyun
acpi_data_prop_read_single(const struct acpi_device_data * data,const char * propname,enum dev_prop_type proptype,void * val)789*4882a593Smuzhiyun static int acpi_data_prop_read_single(const struct acpi_device_data *data,
790*4882a593Smuzhiyun const char *propname,
791*4882a593Smuzhiyun enum dev_prop_type proptype, void *val)
792*4882a593Smuzhiyun {
793*4882a593Smuzhiyun const union acpi_object *obj;
794*4882a593Smuzhiyun int ret;
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
797*4882a593Smuzhiyun ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
798*4882a593Smuzhiyun if (ret)
799*4882a593Smuzhiyun return ret;
800*4882a593Smuzhiyun
801*4882a593Smuzhiyun switch (proptype) {
802*4882a593Smuzhiyun case DEV_PROP_U8:
803*4882a593Smuzhiyun if (obj->integer.value > U8_MAX)
804*4882a593Smuzhiyun return -EOVERFLOW;
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun if (val)
807*4882a593Smuzhiyun *(u8 *)val = obj->integer.value;
808*4882a593Smuzhiyun
809*4882a593Smuzhiyun break;
810*4882a593Smuzhiyun case DEV_PROP_U16:
811*4882a593Smuzhiyun if (obj->integer.value > U16_MAX)
812*4882a593Smuzhiyun return -EOVERFLOW;
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun if (val)
815*4882a593Smuzhiyun *(u16 *)val = obj->integer.value;
816*4882a593Smuzhiyun
817*4882a593Smuzhiyun break;
818*4882a593Smuzhiyun case DEV_PROP_U32:
819*4882a593Smuzhiyun if (obj->integer.value > U32_MAX)
820*4882a593Smuzhiyun return -EOVERFLOW;
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun if (val)
823*4882a593Smuzhiyun *(u32 *)val = obj->integer.value;
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun break;
826*4882a593Smuzhiyun default:
827*4882a593Smuzhiyun if (val)
828*4882a593Smuzhiyun *(u64 *)val = obj->integer.value;
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun break;
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun if (!val)
834*4882a593Smuzhiyun return 1;
835*4882a593Smuzhiyun } else if (proptype == DEV_PROP_STRING) {
836*4882a593Smuzhiyun ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
837*4882a593Smuzhiyun if (ret)
838*4882a593Smuzhiyun return ret;
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun if (val)
841*4882a593Smuzhiyun *(char **)val = obj->string.pointer;
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun return 1;
844*4882a593Smuzhiyun } else {
845*4882a593Smuzhiyun ret = -EINVAL;
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun return ret;
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun
acpi_dev_prop_read_single(struct acpi_device * adev,const char * propname,enum dev_prop_type proptype,void * val)850*4882a593Smuzhiyun int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
851*4882a593Smuzhiyun enum dev_prop_type proptype, void *val)
852*4882a593Smuzhiyun {
853*4882a593Smuzhiyun int ret;
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun if (!adev || !val)
856*4882a593Smuzhiyun return -EINVAL;
857*4882a593Smuzhiyun
858*4882a593Smuzhiyun ret = acpi_data_prop_read_single(&adev->data, propname, proptype, val);
859*4882a593Smuzhiyun if (ret < 0 || proptype != ACPI_TYPE_STRING)
860*4882a593Smuzhiyun return ret;
861*4882a593Smuzhiyun return 0;
862*4882a593Smuzhiyun }
863*4882a593Smuzhiyun
acpi_copy_property_array_u8(const union acpi_object * items,u8 * val,size_t nval)864*4882a593Smuzhiyun static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
865*4882a593Smuzhiyun size_t nval)
866*4882a593Smuzhiyun {
867*4882a593Smuzhiyun int i;
868*4882a593Smuzhiyun
869*4882a593Smuzhiyun for (i = 0; i < nval; i++) {
870*4882a593Smuzhiyun if (items[i].type != ACPI_TYPE_INTEGER)
871*4882a593Smuzhiyun return -EPROTO;
872*4882a593Smuzhiyun if (items[i].integer.value > U8_MAX)
873*4882a593Smuzhiyun return -EOVERFLOW;
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun val[i] = items[i].integer.value;
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun return 0;
878*4882a593Smuzhiyun }
879*4882a593Smuzhiyun
acpi_copy_property_array_u16(const union acpi_object * items,u16 * val,size_t nval)880*4882a593Smuzhiyun static int acpi_copy_property_array_u16(const union acpi_object *items,
881*4882a593Smuzhiyun u16 *val, size_t nval)
882*4882a593Smuzhiyun {
883*4882a593Smuzhiyun int i;
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun for (i = 0; i < nval; i++) {
886*4882a593Smuzhiyun if (items[i].type != ACPI_TYPE_INTEGER)
887*4882a593Smuzhiyun return -EPROTO;
888*4882a593Smuzhiyun if (items[i].integer.value > U16_MAX)
889*4882a593Smuzhiyun return -EOVERFLOW;
890*4882a593Smuzhiyun
891*4882a593Smuzhiyun val[i] = items[i].integer.value;
892*4882a593Smuzhiyun }
893*4882a593Smuzhiyun return 0;
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun
acpi_copy_property_array_u32(const union acpi_object * items,u32 * val,size_t nval)896*4882a593Smuzhiyun static int acpi_copy_property_array_u32(const union acpi_object *items,
897*4882a593Smuzhiyun u32 *val, size_t nval)
898*4882a593Smuzhiyun {
899*4882a593Smuzhiyun int i;
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun for (i = 0; i < nval; i++) {
902*4882a593Smuzhiyun if (items[i].type != ACPI_TYPE_INTEGER)
903*4882a593Smuzhiyun return -EPROTO;
904*4882a593Smuzhiyun if (items[i].integer.value > U32_MAX)
905*4882a593Smuzhiyun return -EOVERFLOW;
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun val[i] = items[i].integer.value;
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun return 0;
910*4882a593Smuzhiyun }
911*4882a593Smuzhiyun
acpi_copy_property_array_u64(const union acpi_object * items,u64 * val,size_t nval)912*4882a593Smuzhiyun static int acpi_copy_property_array_u64(const union acpi_object *items,
913*4882a593Smuzhiyun u64 *val, size_t nval)
914*4882a593Smuzhiyun {
915*4882a593Smuzhiyun int i;
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun for (i = 0; i < nval; i++) {
918*4882a593Smuzhiyun if (items[i].type != ACPI_TYPE_INTEGER)
919*4882a593Smuzhiyun return -EPROTO;
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun val[i] = items[i].integer.value;
922*4882a593Smuzhiyun }
923*4882a593Smuzhiyun return 0;
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun
acpi_copy_property_array_string(const union acpi_object * items,char ** val,size_t nval)926*4882a593Smuzhiyun static int acpi_copy_property_array_string(const union acpi_object *items,
927*4882a593Smuzhiyun char **val, size_t nval)
928*4882a593Smuzhiyun {
929*4882a593Smuzhiyun int i;
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun for (i = 0; i < nval; i++) {
932*4882a593Smuzhiyun if (items[i].type != ACPI_TYPE_STRING)
933*4882a593Smuzhiyun return -EPROTO;
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun val[i] = items[i].string.pointer;
936*4882a593Smuzhiyun }
937*4882a593Smuzhiyun return nval;
938*4882a593Smuzhiyun }
939*4882a593Smuzhiyun
acpi_data_prop_read(const struct acpi_device_data * data,const char * propname,enum dev_prop_type proptype,void * val,size_t nval)940*4882a593Smuzhiyun static int acpi_data_prop_read(const struct acpi_device_data *data,
941*4882a593Smuzhiyun const char *propname,
942*4882a593Smuzhiyun enum dev_prop_type proptype,
943*4882a593Smuzhiyun void *val, size_t nval)
944*4882a593Smuzhiyun {
945*4882a593Smuzhiyun const union acpi_object *obj;
946*4882a593Smuzhiyun const union acpi_object *items;
947*4882a593Smuzhiyun int ret;
948*4882a593Smuzhiyun
949*4882a593Smuzhiyun if (nval == 1 || !val) {
950*4882a593Smuzhiyun ret = acpi_data_prop_read_single(data, propname, proptype, val);
951*4882a593Smuzhiyun /*
952*4882a593Smuzhiyun * The overflow error means that the property is there and it is
953*4882a593Smuzhiyun * single-value, but its type does not match, so return.
954*4882a593Smuzhiyun */
955*4882a593Smuzhiyun if (ret >= 0 || ret == -EOVERFLOW)
956*4882a593Smuzhiyun return ret;
957*4882a593Smuzhiyun
958*4882a593Smuzhiyun /*
959*4882a593Smuzhiyun * Reading this property as a single-value one failed, but its
960*4882a593Smuzhiyun * value may still be represented as one-element array, so
961*4882a593Smuzhiyun * continue.
962*4882a593Smuzhiyun */
963*4882a593Smuzhiyun }
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
966*4882a593Smuzhiyun if (ret)
967*4882a593Smuzhiyun return ret;
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun if (!val)
970*4882a593Smuzhiyun return obj->package.count;
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun if (proptype != DEV_PROP_STRING && nval > obj->package.count)
973*4882a593Smuzhiyun return -EOVERFLOW;
974*4882a593Smuzhiyun else if (nval <= 0)
975*4882a593Smuzhiyun return -EINVAL;
976*4882a593Smuzhiyun
977*4882a593Smuzhiyun items = obj->package.elements;
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun switch (proptype) {
980*4882a593Smuzhiyun case DEV_PROP_U8:
981*4882a593Smuzhiyun ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
982*4882a593Smuzhiyun break;
983*4882a593Smuzhiyun case DEV_PROP_U16:
984*4882a593Smuzhiyun ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
985*4882a593Smuzhiyun break;
986*4882a593Smuzhiyun case DEV_PROP_U32:
987*4882a593Smuzhiyun ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
988*4882a593Smuzhiyun break;
989*4882a593Smuzhiyun case DEV_PROP_U64:
990*4882a593Smuzhiyun ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
991*4882a593Smuzhiyun break;
992*4882a593Smuzhiyun case DEV_PROP_STRING:
993*4882a593Smuzhiyun ret = acpi_copy_property_array_string(
994*4882a593Smuzhiyun items, (char **)val,
995*4882a593Smuzhiyun min_t(u32, nval, obj->package.count));
996*4882a593Smuzhiyun break;
997*4882a593Smuzhiyun default:
998*4882a593Smuzhiyun ret = -EINVAL;
999*4882a593Smuzhiyun break;
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun return ret;
1002*4882a593Smuzhiyun }
1003*4882a593Smuzhiyun
acpi_dev_prop_read(const struct acpi_device * adev,const char * propname,enum dev_prop_type proptype,void * val,size_t nval)1004*4882a593Smuzhiyun int acpi_dev_prop_read(const struct acpi_device *adev, const char *propname,
1005*4882a593Smuzhiyun enum dev_prop_type proptype, void *val, size_t nval)
1006*4882a593Smuzhiyun {
1007*4882a593Smuzhiyun return adev ? acpi_data_prop_read(&adev->data, propname, proptype, val, nval) : -EINVAL;
1008*4882a593Smuzhiyun }
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun /**
1011*4882a593Smuzhiyun * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
1012*4882a593Smuzhiyun * @fwnode: Firmware node to get the property from.
1013*4882a593Smuzhiyun * @propname: Name of the property.
1014*4882a593Smuzhiyun * @proptype: Expected property type.
1015*4882a593Smuzhiyun * @val: Location to store the property value (if not %NULL).
1016*4882a593Smuzhiyun * @nval: Size of the array pointed to by @val.
1017*4882a593Smuzhiyun *
1018*4882a593Smuzhiyun * If @val is %NULL, return the number of array elements comprising the value
1019*4882a593Smuzhiyun * of the property. Otherwise, read at most @nval values to the array at the
1020*4882a593Smuzhiyun * location pointed to by @val.
1021*4882a593Smuzhiyun */
acpi_node_prop_read(const struct fwnode_handle * fwnode,const char * propname,enum dev_prop_type proptype,void * val,size_t nval)1022*4882a593Smuzhiyun int acpi_node_prop_read(const struct fwnode_handle *fwnode,
1023*4882a593Smuzhiyun const char *propname, enum dev_prop_type proptype,
1024*4882a593Smuzhiyun void *val, size_t nval)
1025*4882a593Smuzhiyun {
1026*4882a593Smuzhiyun return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
1027*4882a593Smuzhiyun propname, proptype, val, nval);
1028*4882a593Smuzhiyun }
1029*4882a593Smuzhiyun
1030*4882a593Smuzhiyun /**
1031*4882a593Smuzhiyun * acpi_get_next_subnode - Return the next child node handle for a fwnode
1032*4882a593Smuzhiyun * @fwnode: Firmware node to find the next child node for.
1033*4882a593Smuzhiyun * @child: Handle to one of the device's child nodes or a null handle.
1034*4882a593Smuzhiyun */
acpi_get_next_subnode(const struct fwnode_handle * fwnode,struct fwnode_handle * child)1035*4882a593Smuzhiyun struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode,
1036*4882a593Smuzhiyun struct fwnode_handle *child)
1037*4882a593Smuzhiyun {
1038*4882a593Smuzhiyun const struct acpi_device *adev = to_acpi_device_node(fwnode);
1039*4882a593Smuzhiyun const struct list_head *head;
1040*4882a593Smuzhiyun struct list_head *next;
1041*4882a593Smuzhiyun
1042*4882a593Smuzhiyun if (!child || is_acpi_device_node(child)) {
1043*4882a593Smuzhiyun struct acpi_device *child_adev;
1044*4882a593Smuzhiyun
1045*4882a593Smuzhiyun if (adev)
1046*4882a593Smuzhiyun head = &adev->children;
1047*4882a593Smuzhiyun else
1048*4882a593Smuzhiyun goto nondev;
1049*4882a593Smuzhiyun
1050*4882a593Smuzhiyun if (list_empty(head))
1051*4882a593Smuzhiyun goto nondev;
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun if (child) {
1054*4882a593Smuzhiyun adev = to_acpi_device_node(child);
1055*4882a593Smuzhiyun next = adev->node.next;
1056*4882a593Smuzhiyun if (next == head) {
1057*4882a593Smuzhiyun child = NULL;
1058*4882a593Smuzhiyun goto nondev;
1059*4882a593Smuzhiyun }
1060*4882a593Smuzhiyun child_adev = list_entry(next, struct acpi_device, node);
1061*4882a593Smuzhiyun } else {
1062*4882a593Smuzhiyun child_adev = list_first_entry(head, struct acpi_device,
1063*4882a593Smuzhiyun node);
1064*4882a593Smuzhiyun }
1065*4882a593Smuzhiyun return acpi_fwnode_handle(child_adev);
1066*4882a593Smuzhiyun }
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun nondev:
1069*4882a593Smuzhiyun if (!child || is_acpi_data_node(child)) {
1070*4882a593Smuzhiyun const struct acpi_data_node *data = to_acpi_data_node(fwnode);
1071*4882a593Smuzhiyun struct acpi_data_node *dn;
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun /*
1074*4882a593Smuzhiyun * We can have a combination of device and data nodes, e.g. with
1075*4882a593Smuzhiyun * hierarchical _DSD properties. Make sure the adev pointer is
1076*4882a593Smuzhiyun * restored before going through data nodes, otherwise we will
1077*4882a593Smuzhiyun * be looking for data_nodes below the last device found instead
1078*4882a593Smuzhiyun * of the common fwnode shared by device_nodes and data_nodes.
1079*4882a593Smuzhiyun */
1080*4882a593Smuzhiyun adev = to_acpi_device_node(fwnode);
1081*4882a593Smuzhiyun if (adev)
1082*4882a593Smuzhiyun head = &adev->data.subnodes;
1083*4882a593Smuzhiyun else if (data)
1084*4882a593Smuzhiyun head = &data->data.subnodes;
1085*4882a593Smuzhiyun else
1086*4882a593Smuzhiyun return NULL;
1087*4882a593Smuzhiyun
1088*4882a593Smuzhiyun if (list_empty(head))
1089*4882a593Smuzhiyun return NULL;
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun if (child) {
1092*4882a593Smuzhiyun dn = to_acpi_data_node(child);
1093*4882a593Smuzhiyun next = dn->sibling.next;
1094*4882a593Smuzhiyun if (next == head)
1095*4882a593Smuzhiyun return NULL;
1096*4882a593Smuzhiyun
1097*4882a593Smuzhiyun dn = list_entry(next, struct acpi_data_node, sibling);
1098*4882a593Smuzhiyun } else {
1099*4882a593Smuzhiyun dn = list_first_entry(head, struct acpi_data_node, sibling);
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun return &dn->fwnode;
1102*4882a593Smuzhiyun }
1103*4882a593Smuzhiyun return NULL;
1104*4882a593Smuzhiyun }
1105*4882a593Smuzhiyun
1106*4882a593Smuzhiyun /**
1107*4882a593Smuzhiyun * acpi_node_get_parent - Return parent fwnode of this fwnode
1108*4882a593Smuzhiyun * @fwnode: Firmware node whose parent to get
1109*4882a593Smuzhiyun *
1110*4882a593Smuzhiyun * Returns parent node of an ACPI device or data firmware node or %NULL if
1111*4882a593Smuzhiyun * not available.
1112*4882a593Smuzhiyun */
acpi_node_get_parent(const struct fwnode_handle * fwnode)1113*4882a593Smuzhiyun struct fwnode_handle *acpi_node_get_parent(const struct fwnode_handle *fwnode)
1114*4882a593Smuzhiyun {
1115*4882a593Smuzhiyun if (is_acpi_data_node(fwnode)) {
1116*4882a593Smuzhiyun /* All data nodes have parent pointer so just return that */
1117*4882a593Smuzhiyun return to_acpi_data_node(fwnode)->parent;
1118*4882a593Smuzhiyun } else if (is_acpi_device_node(fwnode)) {
1119*4882a593Smuzhiyun struct device *dev = to_acpi_device_node(fwnode)->dev.parent;
1120*4882a593Smuzhiyun
1121*4882a593Smuzhiyun if (dev)
1122*4882a593Smuzhiyun return acpi_fwnode_handle(to_acpi_device(dev));
1123*4882a593Smuzhiyun }
1124*4882a593Smuzhiyun
1125*4882a593Smuzhiyun return NULL;
1126*4882a593Smuzhiyun }
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun /*
1129*4882a593Smuzhiyun * Return true if the node is an ACPI graph node. Called on either ports
1130*4882a593Smuzhiyun * or endpoints.
1131*4882a593Smuzhiyun */
is_acpi_graph_node(struct fwnode_handle * fwnode,const char * str)1132*4882a593Smuzhiyun static bool is_acpi_graph_node(struct fwnode_handle *fwnode,
1133*4882a593Smuzhiyun const char *str)
1134*4882a593Smuzhiyun {
1135*4882a593Smuzhiyun unsigned int len = strlen(str);
1136*4882a593Smuzhiyun const char *name;
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun if (!len || !is_acpi_data_node(fwnode))
1139*4882a593Smuzhiyun return false;
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun name = to_acpi_data_node(fwnode)->name;
1142*4882a593Smuzhiyun
1143*4882a593Smuzhiyun return (fwnode_property_present(fwnode, "reg") &&
1144*4882a593Smuzhiyun !strncmp(name, str, len) && name[len] == '@') ||
1145*4882a593Smuzhiyun fwnode_property_present(fwnode, str);
1146*4882a593Smuzhiyun }
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun /**
1149*4882a593Smuzhiyun * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
1150*4882a593Smuzhiyun * @fwnode: Pointer to the parent firmware node
1151*4882a593Smuzhiyun * @prev: Previous endpoint node or %NULL to get the first
1152*4882a593Smuzhiyun *
1153*4882a593Smuzhiyun * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
1154*4882a593Smuzhiyun * %NULL if there is no next endpoint or in case of error. In case of success
1155*4882a593Smuzhiyun * the next endpoint is returned.
1156*4882a593Smuzhiyun */
acpi_graph_get_next_endpoint(const struct fwnode_handle * fwnode,struct fwnode_handle * prev)1157*4882a593Smuzhiyun static struct fwnode_handle *acpi_graph_get_next_endpoint(
1158*4882a593Smuzhiyun const struct fwnode_handle *fwnode, struct fwnode_handle *prev)
1159*4882a593Smuzhiyun {
1160*4882a593Smuzhiyun struct fwnode_handle *port = NULL;
1161*4882a593Smuzhiyun struct fwnode_handle *endpoint;
1162*4882a593Smuzhiyun
1163*4882a593Smuzhiyun if (!prev) {
1164*4882a593Smuzhiyun do {
1165*4882a593Smuzhiyun port = fwnode_get_next_child_node(fwnode, port);
1166*4882a593Smuzhiyun /*
1167*4882a593Smuzhiyun * The names of the port nodes begin with "port@"
1168*4882a593Smuzhiyun * followed by the number of the port node and they also
1169*4882a593Smuzhiyun * have a "reg" property that also has the number of the
1170*4882a593Smuzhiyun * port node. For compatibility reasons a node is also
1171*4882a593Smuzhiyun * recognised as a port node from the "port" property.
1172*4882a593Smuzhiyun */
1173*4882a593Smuzhiyun if (is_acpi_graph_node(port, "port"))
1174*4882a593Smuzhiyun break;
1175*4882a593Smuzhiyun } while (port);
1176*4882a593Smuzhiyun } else {
1177*4882a593Smuzhiyun port = fwnode_get_parent(prev);
1178*4882a593Smuzhiyun }
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun if (!port)
1181*4882a593Smuzhiyun return NULL;
1182*4882a593Smuzhiyun
1183*4882a593Smuzhiyun endpoint = fwnode_get_next_child_node(port, prev);
1184*4882a593Smuzhiyun while (!endpoint) {
1185*4882a593Smuzhiyun port = fwnode_get_next_child_node(fwnode, port);
1186*4882a593Smuzhiyun if (!port)
1187*4882a593Smuzhiyun break;
1188*4882a593Smuzhiyun if (is_acpi_graph_node(port, "port"))
1189*4882a593Smuzhiyun endpoint = fwnode_get_next_child_node(port, NULL);
1190*4882a593Smuzhiyun }
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun /*
1193*4882a593Smuzhiyun * The names of the endpoint nodes begin with "endpoint@" followed by
1194*4882a593Smuzhiyun * the number of the endpoint node and they also have a "reg" property
1195*4882a593Smuzhiyun * that also has the number of the endpoint node. For compatibility
1196*4882a593Smuzhiyun * reasons a node is also recognised as an endpoint node from the
1197*4882a593Smuzhiyun * "endpoint" property.
1198*4882a593Smuzhiyun */
1199*4882a593Smuzhiyun if (!is_acpi_graph_node(endpoint, "endpoint"))
1200*4882a593Smuzhiyun return NULL;
1201*4882a593Smuzhiyun
1202*4882a593Smuzhiyun return endpoint;
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun
1205*4882a593Smuzhiyun /**
1206*4882a593Smuzhiyun * acpi_graph_get_child_prop_value - Return a child with a given property value
1207*4882a593Smuzhiyun * @fwnode: device fwnode
1208*4882a593Smuzhiyun * @prop_name: The name of the property to look for
1209*4882a593Smuzhiyun * @val: the desired property value
1210*4882a593Smuzhiyun *
1211*4882a593Smuzhiyun * Return the port node corresponding to a given port number. Returns
1212*4882a593Smuzhiyun * the child node on success, NULL otherwise.
1213*4882a593Smuzhiyun */
acpi_graph_get_child_prop_value(const struct fwnode_handle * fwnode,const char * prop_name,unsigned int val)1214*4882a593Smuzhiyun static struct fwnode_handle *acpi_graph_get_child_prop_value(
1215*4882a593Smuzhiyun const struct fwnode_handle *fwnode, const char *prop_name,
1216*4882a593Smuzhiyun unsigned int val)
1217*4882a593Smuzhiyun {
1218*4882a593Smuzhiyun struct fwnode_handle *child;
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun fwnode_for_each_child_node(fwnode, child) {
1221*4882a593Smuzhiyun u32 nr;
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun if (fwnode_property_read_u32(child, prop_name, &nr))
1224*4882a593Smuzhiyun continue;
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun if (val == nr)
1227*4882a593Smuzhiyun return child;
1228*4882a593Smuzhiyun }
1229*4882a593Smuzhiyun
1230*4882a593Smuzhiyun return NULL;
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun
1234*4882a593Smuzhiyun /**
1235*4882a593Smuzhiyun * acpi_graph_get_remote_endpoint - Parses and returns remote end of an endpoint
1236*4882a593Smuzhiyun * @fwnode: Endpoint firmware node pointing to a remote device
1237*4882a593Smuzhiyun * @endpoint: Firmware node of remote endpoint is filled here if not %NULL
1238*4882a593Smuzhiyun *
1239*4882a593Smuzhiyun * Returns the remote endpoint corresponding to @__fwnode. NULL on error.
1240*4882a593Smuzhiyun */
1241*4882a593Smuzhiyun static struct fwnode_handle *
acpi_graph_get_remote_endpoint(const struct fwnode_handle * __fwnode)1242*4882a593Smuzhiyun acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode)
1243*4882a593Smuzhiyun {
1244*4882a593Smuzhiyun struct fwnode_handle *fwnode;
1245*4882a593Smuzhiyun unsigned int port_nr, endpoint_nr;
1246*4882a593Smuzhiyun struct fwnode_reference_args args;
1247*4882a593Smuzhiyun int ret;
1248*4882a593Smuzhiyun
1249*4882a593Smuzhiyun memset(&args, 0, sizeof(args));
1250*4882a593Smuzhiyun ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0,
1251*4882a593Smuzhiyun &args);
1252*4882a593Smuzhiyun if (ret)
1253*4882a593Smuzhiyun return NULL;
1254*4882a593Smuzhiyun
1255*4882a593Smuzhiyun /* Direct endpoint reference? */
1256*4882a593Smuzhiyun if (!is_acpi_device_node(args.fwnode))
1257*4882a593Smuzhiyun return args.nargs ? NULL : args.fwnode;
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun /*
1260*4882a593Smuzhiyun * Always require two arguments with the reference: port and
1261*4882a593Smuzhiyun * endpoint indices.
1262*4882a593Smuzhiyun */
1263*4882a593Smuzhiyun if (args.nargs != 2)
1264*4882a593Smuzhiyun return NULL;
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun fwnode = args.fwnode;
1267*4882a593Smuzhiyun port_nr = args.args[0];
1268*4882a593Smuzhiyun endpoint_nr = args.args[1];
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
1271*4882a593Smuzhiyun
1272*4882a593Smuzhiyun return acpi_graph_get_child_prop_value(fwnode, "endpoint", endpoint_nr);
1273*4882a593Smuzhiyun }
1274*4882a593Smuzhiyun
acpi_fwnode_device_is_available(const struct fwnode_handle * fwnode)1275*4882a593Smuzhiyun static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
1276*4882a593Smuzhiyun {
1277*4882a593Smuzhiyun if (!is_acpi_device_node(fwnode))
1278*4882a593Smuzhiyun return false;
1279*4882a593Smuzhiyun
1280*4882a593Smuzhiyun return acpi_device_is_present(to_acpi_device_node(fwnode));
1281*4882a593Smuzhiyun }
1282*4882a593Smuzhiyun
acpi_fwnode_property_present(const struct fwnode_handle * fwnode,const char * propname)1283*4882a593Smuzhiyun static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode,
1284*4882a593Smuzhiyun const char *propname)
1285*4882a593Smuzhiyun {
1286*4882a593Smuzhiyun return !acpi_node_prop_get(fwnode, propname, NULL);
1287*4882a593Smuzhiyun }
1288*4882a593Smuzhiyun
1289*4882a593Smuzhiyun static int
acpi_fwnode_property_read_int_array(const struct fwnode_handle * fwnode,const char * propname,unsigned int elem_size,void * val,size_t nval)1290*4882a593Smuzhiyun acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
1291*4882a593Smuzhiyun const char *propname,
1292*4882a593Smuzhiyun unsigned int elem_size, void *val,
1293*4882a593Smuzhiyun size_t nval)
1294*4882a593Smuzhiyun {
1295*4882a593Smuzhiyun enum dev_prop_type type;
1296*4882a593Smuzhiyun
1297*4882a593Smuzhiyun switch (elem_size) {
1298*4882a593Smuzhiyun case sizeof(u8):
1299*4882a593Smuzhiyun type = DEV_PROP_U8;
1300*4882a593Smuzhiyun break;
1301*4882a593Smuzhiyun case sizeof(u16):
1302*4882a593Smuzhiyun type = DEV_PROP_U16;
1303*4882a593Smuzhiyun break;
1304*4882a593Smuzhiyun case sizeof(u32):
1305*4882a593Smuzhiyun type = DEV_PROP_U32;
1306*4882a593Smuzhiyun break;
1307*4882a593Smuzhiyun case sizeof(u64):
1308*4882a593Smuzhiyun type = DEV_PROP_U64;
1309*4882a593Smuzhiyun break;
1310*4882a593Smuzhiyun default:
1311*4882a593Smuzhiyun return -ENXIO;
1312*4882a593Smuzhiyun }
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun return acpi_node_prop_read(fwnode, propname, type, val, nval);
1315*4882a593Smuzhiyun }
1316*4882a593Smuzhiyun
1317*4882a593Smuzhiyun static int
acpi_fwnode_property_read_string_array(const struct fwnode_handle * fwnode,const char * propname,const char ** val,size_t nval)1318*4882a593Smuzhiyun acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
1319*4882a593Smuzhiyun const char *propname, const char **val,
1320*4882a593Smuzhiyun size_t nval)
1321*4882a593Smuzhiyun {
1322*4882a593Smuzhiyun return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
1323*4882a593Smuzhiyun val, nval);
1324*4882a593Smuzhiyun }
1325*4882a593Smuzhiyun
1326*4882a593Smuzhiyun static int
acpi_fwnode_get_reference_args(const struct fwnode_handle * fwnode,const char * prop,const char * nargs_prop,unsigned int args_count,unsigned int index,struct fwnode_reference_args * args)1327*4882a593Smuzhiyun acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
1328*4882a593Smuzhiyun const char *prop, const char *nargs_prop,
1329*4882a593Smuzhiyun unsigned int args_count, unsigned int index,
1330*4882a593Smuzhiyun struct fwnode_reference_args *args)
1331*4882a593Smuzhiyun {
1332*4882a593Smuzhiyun return __acpi_node_get_property_reference(fwnode, prop, index,
1333*4882a593Smuzhiyun args_count, args);
1334*4882a593Smuzhiyun }
1335*4882a593Smuzhiyun
acpi_fwnode_get_name(const struct fwnode_handle * fwnode)1336*4882a593Smuzhiyun static const char *acpi_fwnode_get_name(const struct fwnode_handle *fwnode)
1337*4882a593Smuzhiyun {
1338*4882a593Smuzhiyun const struct acpi_device *adev;
1339*4882a593Smuzhiyun struct fwnode_handle *parent;
1340*4882a593Smuzhiyun
1341*4882a593Smuzhiyun /* Is this the root node? */
1342*4882a593Smuzhiyun parent = fwnode_get_parent(fwnode);
1343*4882a593Smuzhiyun if (!parent)
1344*4882a593Smuzhiyun return "\\";
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun fwnode_handle_put(parent);
1347*4882a593Smuzhiyun
1348*4882a593Smuzhiyun if (is_acpi_data_node(fwnode)) {
1349*4882a593Smuzhiyun const struct acpi_data_node *dn = to_acpi_data_node(fwnode);
1350*4882a593Smuzhiyun
1351*4882a593Smuzhiyun return dn->name;
1352*4882a593Smuzhiyun }
1353*4882a593Smuzhiyun
1354*4882a593Smuzhiyun adev = to_acpi_device_node(fwnode);
1355*4882a593Smuzhiyun if (WARN_ON(!adev))
1356*4882a593Smuzhiyun return NULL;
1357*4882a593Smuzhiyun
1358*4882a593Smuzhiyun return acpi_device_bid(adev);
1359*4882a593Smuzhiyun }
1360*4882a593Smuzhiyun
1361*4882a593Smuzhiyun static const char *
acpi_fwnode_get_name_prefix(const struct fwnode_handle * fwnode)1362*4882a593Smuzhiyun acpi_fwnode_get_name_prefix(const struct fwnode_handle *fwnode)
1363*4882a593Smuzhiyun {
1364*4882a593Smuzhiyun struct fwnode_handle *parent;
1365*4882a593Smuzhiyun
1366*4882a593Smuzhiyun /* Is this the root node? */
1367*4882a593Smuzhiyun parent = fwnode_get_parent(fwnode);
1368*4882a593Smuzhiyun if (!parent)
1369*4882a593Smuzhiyun return "";
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun /* Is this 2nd node from the root? */
1372*4882a593Smuzhiyun parent = fwnode_get_next_parent(parent);
1373*4882a593Smuzhiyun if (!parent)
1374*4882a593Smuzhiyun return "";
1375*4882a593Smuzhiyun
1376*4882a593Smuzhiyun fwnode_handle_put(parent);
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun /* ACPI device or data node. */
1379*4882a593Smuzhiyun return ".";
1380*4882a593Smuzhiyun }
1381*4882a593Smuzhiyun
1382*4882a593Smuzhiyun static struct fwnode_handle *
acpi_fwnode_get_parent(struct fwnode_handle * fwnode)1383*4882a593Smuzhiyun acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
1384*4882a593Smuzhiyun {
1385*4882a593Smuzhiyun return acpi_node_get_parent(fwnode);
1386*4882a593Smuzhiyun }
1387*4882a593Smuzhiyun
acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle * fwnode,struct fwnode_endpoint * endpoint)1388*4882a593Smuzhiyun static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
1389*4882a593Smuzhiyun struct fwnode_endpoint *endpoint)
1390*4882a593Smuzhiyun {
1391*4882a593Smuzhiyun struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
1392*4882a593Smuzhiyun
1393*4882a593Smuzhiyun endpoint->local_fwnode = fwnode;
1394*4882a593Smuzhiyun
1395*4882a593Smuzhiyun if (fwnode_property_read_u32(port_fwnode, "reg", &endpoint->port))
1396*4882a593Smuzhiyun fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
1397*4882a593Smuzhiyun if (fwnode_property_read_u32(fwnode, "reg", &endpoint->id))
1398*4882a593Smuzhiyun fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun return 0;
1401*4882a593Smuzhiyun }
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun static const void *
acpi_fwnode_device_get_match_data(const struct fwnode_handle * fwnode,const struct device * dev)1404*4882a593Smuzhiyun acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode,
1405*4882a593Smuzhiyun const struct device *dev)
1406*4882a593Smuzhiyun {
1407*4882a593Smuzhiyun return acpi_device_get_match_data(dev);
1408*4882a593Smuzhiyun }
1409*4882a593Smuzhiyun
1410*4882a593Smuzhiyun #define DECLARE_ACPI_FWNODE_OPS(ops) \
1411*4882a593Smuzhiyun const struct fwnode_operations ops = { \
1412*4882a593Smuzhiyun .device_is_available = acpi_fwnode_device_is_available, \
1413*4882a593Smuzhiyun .device_get_match_data = acpi_fwnode_device_get_match_data, \
1414*4882a593Smuzhiyun .property_present = acpi_fwnode_property_present, \
1415*4882a593Smuzhiyun .property_read_int_array = \
1416*4882a593Smuzhiyun acpi_fwnode_property_read_int_array, \
1417*4882a593Smuzhiyun .property_read_string_array = \
1418*4882a593Smuzhiyun acpi_fwnode_property_read_string_array, \
1419*4882a593Smuzhiyun .get_parent = acpi_node_get_parent, \
1420*4882a593Smuzhiyun .get_next_child_node = acpi_get_next_subnode, \
1421*4882a593Smuzhiyun .get_named_child_node = acpi_fwnode_get_named_child_node, \
1422*4882a593Smuzhiyun .get_name = acpi_fwnode_get_name, \
1423*4882a593Smuzhiyun .get_name_prefix = acpi_fwnode_get_name_prefix, \
1424*4882a593Smuzhiyun .get_reference_args = acpi_fwnode_get_reference_args, \
1425*4882a593Smuzhiyun .graph_get_next_endpoint = \
1426*4882a593Smuzhiyun acpi_graph_get_next_endpoint, \
1427*4882a593Smuzhiyun .graph_get_remote_endpoint = \
1428*4882a593Smuzhiyun acpi_graph_get_remote_endpoint, \
1429*4882a593Smuzhiyun .graph_get_port_parent = acpi_fwnode_get_parent, \
1430*4882a593Smuzhiyun .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
1431*4882a593Smuzhiyun }; \
1432*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(ops)
1433*4882a593Smuzhiyun
1434*4882a593Smuzhiyun DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops);
1435*4882a593Smuzhiyun DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops);
1436*4882a593Smuzhiyun const struct fwnode_operations acpi_static_fwnode_ops;
1437*4882a593Smuzhiyun
is_acpi_device_node(const struct fwnode_handle * fwnode)1438*4882a593Smuzhiyun bool is_acpi_device_node(const struct fwnode_handle *fwnode)
1439*4882a593Smuzhiyun {
1440*4882a593Smuzhiyun return !IS_ERR_OR_NULL(fwnode) &&
1441*4882a593Smuzhiyun fwnode->ops == &acpi_device_fwnode_ops;
1442*4882a593Smuzhiyun }
1443*4882a593Smuzhiyun EXPORT_SYMBOL(is_acpi_device_node);
1444*4882a593Smuzhiyun
is_acpi_data_node(const struct fwnode_handle * fwnode)1445*4882a593Smuzhiyun bool is_acpi_data_node(const struct fwnode_handle *fwnode)
1446*4882a593Smuzhiyun {
1447*4882a593Smuzhiyun return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &acpi_data_fwnode_ops;
1448*4882a593Smuzhiyun }
1449*4882a593Smuzhiyun EXPORT_SYMBOL(is_acpi_data_node);
1450