property.c 34 KB

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