property.c 37 KB

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