property.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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. /**
  462. * __acpi_node_get_property_reference - returns handle to the referenced object
  463. * @fwnode: Firmware node to get the property from
  464. * @propname: Name of the property
  465. * @index: Index of the reference to return
  466. * @num_args: Maximum number of arguments after each reference
  467. * @args: Location to store the returned reference with optional arguments
  468. *
  469. * Find property with @name, verifify that it is a package containing at least
  470. * one object reference and if so, store the ACPI device object pointer to the
  471. * target object in @args->adev. If the reference includes arguments, store
  472. * them in the @args->args[] array.
  473. *
  474. * If there's more than one reference in the property value package, @index is
  475. * used to select the one to return.
  476. *
  477. * It is possible to leave holes in the property value set like in the
  478. * example below:
  479. *
  480. * Package () {
  481. * "cs-gpios",
  482. * Package () {
  483. * ^GPIO, 19, 0, 0,
  484. * ^GPIO, 20, 0, 0,
  485. * 0,
  486. * ^GPIO, 21, 0, 0,
  487. * }
  488. * }
  489. *
  490. * Calling this function with index %2 return %-ENOENT and with index %3
  491. * returns the last entry. If the property does not contain any more values
  492. * %-ENODATA is returned. The NULL entry must be single integer and
  493. * preferably contain value %0.
  494. *
  495. * Return: %0 on success, negative error code on failure.
  496. */
  497. int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode,
  498. const char *propname, size_t index, size_t num_args,
  499. struct acpi_reference_args *args)
  500. {
  501. const union acpi_object *element, *end;
  502. const union acpi_object *obj;
  503. const struct acpi_device_data *data;
  504. struct acpi_device *device;
  505. int ret, idx = 0;
  506. data = acpi_device_data_of_node(fwnode);
  507. if (!data)
  508. return -EINVAL;
  509. ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
  510. if (ret)
  511. return ret;
  512. /*
  513. * The simplest case is when the value is a single reference. Just
  514. * return that reference then.
  515. */
  516. if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
  517. if (index)
  518. return -EINVAL;
  519. ret = acpi_bus_get_device(obj->reference.handle, &device);
  520. if (ret)
  521. return ret;
  522. args->adev = device;
  523. args->nargs = 0;
  524. return 0;
  525. }
  526. /*
  527. * If it is not a single reference, then it is a package of
  528. * references followed by number of ints as follows:
  529. *
  530. * Package () { REF, INT, REF, INT, INT }
  531. *
  532. * The index argument is then used to determine which reference
  533. * the caller wants (along with the arguments).
  534. */
  535. if (obj->type != ACPI_TYPE_PACKAGE || index >= obj->package.count)
  536. return -EPROTO;
  537. element = obj->package.elements;
  538. end = element + obj->package.count;
  539. while (element < end) {
  540. u32 nargs, i;
  541. if (element->type == ACPI_TYPE_LOCAL_REFERENCE) {
  542. ret = acpi_bus_get_device(element->reference.handle,
  543. &device);
  544. if (ret)
  545. return -ENODEV;
  546. nargs = 0;
  547. element++;
  548. /* assume following integer elements are all args */
  549. for (i = 0; element + i < end && i < num_args; i++) {
  550. int type = element[i].type;
  551. if (type == ACPI_TYPE_INTEGER)
  552. nargs++;
  553. else if (type == ACPI_TYPE_LOCAL_REFERENCE)
  554. break;
  555. else
  556. return -EPROTO;
  557. }
  558. if (nargs > MAX_ACPI_REFERENCE_ARGS)
  559. return -EPROTO;
  560. if (idx == index) {
  561. args->adev = device;
  562. args->nargs = nargs;
  563. for (i = 0; i < nargs; i++)
  564. args->args[i] = element[i].integer.value;
  565. return 0;
  566. }
  567. element += nargs;
  568. } else if (element->type == ACPI_TYPE_INTEGER) {
  569. if (idx == index)
  570. return -ENOENT;
  571. element++;
  572. } else {
  573. return -EPROTO;
  574. }
  575. idx++;
  576. }
  577. return -ENODATA;
  578. }
  579. EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
  580. static int acpi_data_prop_read_single(const struct acpi_device_data *data,
  581. const char *propname,
  582. enum dev_prop_type proptype, void *val)
  583. {
  584. const union acpi_object *obj;
  585. int ret;
  586. if (!val)
  587. return -EINVAL;
  588. if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
  589. ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
  590. if (ret)
  591. return ret;
  592. switch (proptype) {
  593. case DEV_PROP_U8:
  594. if (obj->integer.value > U8_MAX)
  595. return -EOVERFLOW;
  596. *(u8 *)val = obj->integer.value;
  597. break;
  598. case DEV_PROP_U16:
  599. if (obj->integer.value > U16_MAX)
  600. return -EOVERFLOW;
  601. *(u16 *)val = obj->integer.value;
  602. break;
  603. case DEV_PROP_U32:
  604. if (obj->integer.value > U32_MAX)
  605. return -EOVERFLOW;
  606. *(u32 *)val = obj->integer.value;
  607. break;
  608. default:
  609. *(u64 *)val = obj->integer.value;
  610. break;
  611. }
  612. } else if (proptype == DEV_PROP_STRING) {
  613. ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
  614. if (ret)
  615. return ret;
  616. *(char **)val = obj->string.pointer;
  617. return 1;
  618. } else {
  619. ret = -EINVAL;
  620. }
  621. return ret;
  622. }
  623. int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
  624. enum dev_prop_type proptype, void *val)
  625. {
  626. int ret;
  627. if (!adev)
  628. return -EINVAL;
  629. ret = acpi_data_prop_read_single(&adev->data, propname, proptype, val);
  630. if (ret < 0 || proptype != ACPI_TYPE_STRING)
  631. return ret;
  632. return 0;
  633. }
  634. static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
  635. size_t nval)
  636. {
  637. int i;
  638. for (i = 0; i < nval; i++) {
  639. if (items[i].type != ACPI_TYPE_INTEGER)
  640. return -EPROTO;
  641. if (items[i].integer.value > U8_MAX)
  642. return -EOVERFLOW;
  643. val[i] = items[i].integer.value;
  644. }
  645. return 0;
  646. }
  647. static int acpi_copy_property_array_u16(const union acpi_object *items,
  648. u16 *val, size_t nval)
  649. {
  650. int i;
  651. for (i = 0; i < nval; i++) {
  652. if (items[i].type != ACPI_TYPE_INTEGER)
  653. return -EPROTO;
  654. if (items[i].integer.value > U16_MAX)
  655. return -EOVERFLOW;
  656. val[i] = items[i].integer.value;
  657. }
  658. return 0;
  659. }
  660. static int acpi_copy_property_array_u32(const union acpi_object *items,
  661. u32 *val, size_t nval)
  662. {
  663. int i;
  664. for (i = 0; i < nval; i++) {
  665. if (items[i].type != ACPI_TYPE_INTEGER)
  666. return -EPROTO;
  667. if (items[i].integer.value > U32_MAX)
  668. return -EOVERFLOW;
  669. val[i] = items[i].integer.value;
  670. }
  671. return 0;
  672. }
  673. static int acpi_copy_property_array_u64(const union acpi_object *items,
  674. u64 *val, size_t nval)
  675. {
  676. int i;
  677. for (i = 0; i < nval; i++) {
  678. if (items[i].type != ACPI_TYPE_INTEGER)
  679. return -EPROTO;
  680. val[i] = items[i].integer.value;
  681. }
  682. return 0;
  683. }
  684. static int acpi_copy_property_array_string(const union acpi_object *items,
  685. char **val, size_t nval)
  686. {
  687. int i;
  688. for (i = 0; i < nval; i++) {
  689. if (items[i].type != ACPI_TYPE_STRING)
  690. return -EPROTO;
  691. val[i] = items[i].string.pointer;
  692. }
  693. return nval;
  694. }
  695. static int acpi_data_prop_read(const struct acpi_device_data *data,
  696. const char *propname,
  697. enum dev_prop_type proptype,
  698. void *val, size_t nval)
  699. {
  700. const union acpi_object *obj;
  701. const union acpi_object *items;
  702. int ret;
  703. if (val && nval == 1) {
  704. ret = acpi_data_prop_read_single(data, propname, proptype, val);
  705. if (ret >= 0)
  706. return ret;
  707. }
  708. ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
  709. if (ret)
  710. return ret;
  711. if (!val)
  712. return obj->package.count;
  713. if (proptype != DEV_PROP_STRING && nval > obj->package.count)
  714. return -EOVERFLOW;
  715. else if (nval <= 0)
  716. return -EINVAL;
  717. items = obj->package.elements;
  718. switch (proptype) {
  719. case DEV_PROP_U8:
  720. ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
  721. break;
  722. case DEV_PROP_U16:
  723. ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
  724. break;
  725. case DEV_PROP_U32:
  726. ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
  727. break;
  728. case DEV_PROP_U64:
  729. ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
  730. break;
  731. case DEV_PROP_STRING:
  732. ret = acpi_copy_property_array_string(
  733. items, (char **)val,
  734. min_t(u32, nval, obj->package.count));
  735. break;
  736. default:
  737. ret = -EINVAL;
  738. break;
  739. }
  740. return ret;
  741. }
  742. int acpi_dev_prop_read(const struct acpi_device *adev, const char *propname,
  743. enum dev_prop_type proptype, void *val, size_t nval)
  744. {
  745. return adev ? acpi_data_prop_read(&adev->data, propname, proptype, val, nval) : -EINVAL;
  746. }
  747. /**
  748. * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
  749. * @fwnode: Firmware node to get the property from.
  750. * @propname: Name of the property.
  751. * @proptype: Expected property type.
  752. * @val: Location to store the property value (if not %NULL).
  753. * @nval: Size of the array pointed to by @val.
  754. *
  755. * If @val is %NULL, return the number of array elements comprising the value
  756. * of the property. Otherwise, read at most @nval values to the array at the
  757. * location pointed to by @val.
  758. */
  759. int acpi_node_prop_read(const struct fwnode_handle *fwnode,
  760. const char *propname, enum dev_prop_type proptype,
  761. void *val, size_t nval)
  762. {
  763. return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
  764. propname, proptype, val, nval);
  765. }
  766. /**
  767. * acpi_get_next_subnode - Return the next child node handle for a fwnode
  768. * @fwnode: Firmware node to find the next child node for.
  769. * @child: Handle to one of the device's child nodes or a null handle.
  770. */
  771. struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode,
  772. struct fwnode_handle *child)
  773. {
  774. const struct acpi_device *adev = to_acpi_device_node(fwnode);
  775. const struct list_head *head;
  776. struct list_head *next;
  777. if (!child || is_acpi_device_node(child)) {
  778. struct acpi_device *child_adev;
  779. if (adev)
  780. head = &adev->children;
  781. else
  782. goto nondev;
  783. if (list_empty(head))
  784. goto nondev;
  785. if (child) {
  786. adev = to_acpi_device_node(child);
  787. next = adev->node.next;
  788. if (next == head) {
  789. child = NULL;
  790. goto nondev;
  791. }
  792. child_adev = list_entry(next, struct acpi_device, node);
  793. } else {
  794. child_adev = list_first_entry(head, struct acpi_device,
  795. node);
  796. }
  797. return acpi_fwnode_handle(child_adev);
  798. }
  799. nondev:
  800. if (!child || is_acpi_data_node(child)) {
  801. const struct acpi_data_node *data = to_acpi_data_node(fwnode);
  802. struct acpi_data_node *dn;
  803. if (adev)
  804. head = &adev->data.subnodes;
  805. else if (data)
  806. head = &data->data.subnodes;
  807. else
  808. return NULL;
  809. if (list_empty(head))
  810. return NULL;
  811. if (child) {
  812. dn = to_acpi_data_node(child);
  813. next = dn->sibling.next;
  814. if (next == head)
  815. return NULL;
  816. dn = list_entry(next, struct acpi_data_node, sibling);
  817. } else {
  818. dn = list_first_entry(head, struct acpi_data_node, sibling);
  819. }
  820. return &dn->fwnode;
  821. }
  822. return NULL;
  823. }
  824. /**
  825. * acpi_node_get_parent - Return parent fwnode of this fwnode
  826. * @fwnode: Firmware node whose parent to get
  827. *
  828. * Returns parent node of an ACPI device or data firmware node or %NULL if
  829. * not available.
  830. */
  831. struct fwnode_handle *acpi_node_get_parent(const struct fwnode_handle *fwnode)
  832. {
  833. if (is_acpi_data_node(fwnode)) {
  834. /* All data nodes have parent pointer so just return that */
  835. return to_acpi_data_node(fwnode)->parent;
  836. } else if (is_acpi_device_node(fwnode)) {
  837. acpi_handle handle, parent_handle;
  838. handle = to_acpi_device_node(fwnode)->handle;
  839. if (ACPI_SUCCESS(acpi_get_parent(handle, &parent_handle))) {
  840. struct acpi_device *adev;
  841. if (!acpi_bus_get_device(parent_handle, &adev))
  842. return acpi_fwnode_handle(adev);
  843. }
  844. }
  845. return NULL;
  846. }
  847. /**
  848. * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
  849. * @fwnode: Pointer to the parent firmware node
  850. * @prev: Previous endpoint node or %NULL to get the first
  851. *
  852. * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
  853. * %NULL if there is no next endpoint, ERR_PTR() in case of error. In case
  854. * of success the next endpoint is returned.
  855. */
  856. struct fwnode_handle *acpi_graph_get_next_endpoint(
  857. const struct fwnode_handle *fwnode, struct fwnode_handle *prev)
  858. {
  859. struct fwnode_handle *port = NULL;
  860. struct fwnode_handle *endpoint;
  861. if (!prev) {
  862. do {
  863. port = fwnode_get_next_child_node(fwnode, port);
  864. /* Ports must have port property */
  865. if (fwnode_property_present(port, "port"))
  866. break;
  867. } while (port);
  868. } else {
  869. port = fwnode_get_parent(prev);
  870. }
  871. if (!port)
  872. return NULL;
  873. endpoint = fwnode_get_next_child_node(port, prev);
  874. while (!endpoint) {
  875. port = fwnode_get_next_child_node(fwnode, port);
  876. if (!port)
  877. break;
  878. if (fwnode_property_present(port, "port"))
  879. endpoint = fwnode_get_next_child_node(port, NULL);
  880. }
  881. if (endpoint) {
  882. /* Endpoints must have "endpoint" property */
  883. if (!fwnode_property_present(endpoint, "endpoint"))
  884. return ERR_PTR(-EPROTO);
  885. }
  886. return endpoint;
  887. }
  888. /**
  889. * acpi_graph_get_child_prop_value - Return a child with a given property value
  890. * @fwnode: device fwnode
  891. * @prop_name: The name of the property to look for
  892. * @val: the desired property value
  893. *
  894. * Return the port node corresponding to a given port number. Returns
  895. * the child node on success, NULL otherwise.
  896. */
  897. static struct fwnode_handle *acpi_graph_get_child_prop_value(
  898. const struct fwnode_handle *fwnode, const char *prop_name,
  899. unsigned int val)
  900. {
  901. struct fwnode_handle *child;
  902. fwnode_for_each_child_node(fwnode, child) {
  903. u32 nr;
  904. if (fwnode_property_read_u32(child, prop_name, &nr))
  905. continue;
  906. if (val == nr)
  907. return child;
  908. }
  909. return NULL;
  910. }
  911. /**
  912. * acpi_graph_get_remote_enpoint - Parses and returns remote end of an endpoint
  913. * @fwnode: Endpoint firmware node pointing to a remote device
  914. * @parent: Firmware node of remote port parent is filled here if not %NULL
  915. * @port: Firmware node of remote port is filled here if not %NULL
  916. * @endpoint: Firmware node of remote endpoint is filled here if not %NULL
  917. *
  918. * Function parses remote end of ACPI firmware remote endpoint and fills in
  919. * fields requested by the caller. Returns %0 in case of success and
  920. * negative errno otherwise.
  921. */
  922. int acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode,
  923. struct fwnode_handle **parent,
  924. struct fwnode_handle **port,
  925. struct fwnode_handle **endpoint)
  926. {
  927. struct fwnode_handle *fwnode;
  928. unsigned int port_nr, endpoint_nr;
  929. struct acpi_reference_args args;
  930. int ret;
  931. memset(&args, 0, sizeof(args));
  932. ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0,
  933. &args);
  934. if (ret)
  935. return ret;
  936. /*
  937. * Always require two arguments with the reference: port and
  938. * endpoint indices.
  939. */
  940. if (args.nargs != 2)
  941. return -EPROTO;
  942. fwnode = acpi_fwnode_handle(args.adev);
  943. port_nr = args.args[0];
  944. endpoint_nr = args.args[1];
  945. if (parent)
  946. *parent = fwnode;
  947. if (!port && !endpoint)
  948. return 0;
  949. fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
  950. if (!fwnode)
  951. return -EPROTO;
  952. if (port)
  953. *port = fwnode;
  954. if (!endpoint)
  955. return 0;
  956. fwnode = acpi_graph_get_child_prop_value(fwnode, "endpoint",
  957. endpoint_nr);
  958. if (!fwnode)
  959. return -EPROTO;
  960. *endpoint = fwnode;
  961. return 0;
  962. }
  963. static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode)
  964. {
  965. if (!is_acpi_device_node(fwnode))
  966. return false;
  967. return acpi_device_is_present(to_acpi_device_node(fwnode));
  968. }
  969. static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode,
  970. const char *propname)
  971. {
  972. return !acpi_node_prop_get(fwnode, propname, NULL);
  973. }
  974. static int
  975. acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode,
  976. const char *propname,
  977. unsigned int elem_size, void *val,
  978. size_t nval)
  979. {
  980. enum dev_prop_type type;
  981. switch (elem_size) {
  982. case sizeof(u8):
  983. type = DEV_PROP_U8;
  984. break;
  985. case sizeof(u16):
  986. type = DEV_PROP_U16;
  987. break;
  988. case sizeof(u32):
  989. type = DEV_PROP_U32;
  990. break;
  991. case sizeof(u64):
  992. type = DEV_PROP_U64;
  993. break;
  994. default:
  995. return -ENXIO;
  996. }
  997. return acpi_node_prop_read(fwnode, propname, type, val, nval);
  998. }
  999. static int
  1000. acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
  1001. const char *propname, const char **val,
  1002. size_t nval)
  1003. {
  1004. return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING,
  1005. val, nval);
  1006. }
  1007. static struct fwnode_handle *
  1008. acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
  1009. const char *childname)
  1010. {
  1011. struct fwnode_handle *child;
  1012. /*
  1013. * Find first matching named child node of this fwnode.
  1014. * For ACPI this will be a data only sub-node.
  1015. */
  1016. fwnode_for_each_child_node(fwnode, child)
  1017. if (acpi_data_node_match(child, childname))
  1018. return child;
  1019. return NULL;
  1020. }
  1021. static int
  1022. acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode,
  1023. const char *prop, const char *nargs_prop,
  1024. unsigned int args_count, unsigned int index,
  1025. struct fwnode_reference_args *args)
  1026. {
  1027. struct acpi_reference_args acpi_args;
  1028. unsigned int i;
  1029. int ret;
  1030. ret = __acpi_node_get_property_reference(fwnode, prop, index,
  1031. args_count, &acpi_args);
  1032. if (ret < 0)
  1033. return ret;
  1034. if (!args)
  1035. return 0;
  1036. args->nargs = acpi_args.nargs;
  1037. args->fwnode = acpi_fwnode_handle(acpi_args.adev);
  1038. for (i = 0; i < NR_FWNODE_REFERENCE_ARGS; i++)
  1039. args->args[i] = i < acpi_args.nargs ? acpi_args.args[i] : 0;
  1040. return 0;
  1041. }
  1042. static struct fwnode_handle *
  1043. acpi_fwnode_graph_get_next_endpoint(const struct fwnode_handle *fwnode,
  1044. struct fwnode_handle *prev)
  1045. {
  1046. struct fwnode_handle *endpoint;
  1047. endpoint = acpi_graph_get_next_endpoint(fwnode, prev);
  1048. if (IS_ERR(endpoint))
  1049. return NULL;
  1050. return endpoint;
  1051. }
  1052. static struct fwnode_handle *
  1053. acpi_fwnode_graph_get_remote_endpoint(const struct fwnode_handle *fwnode)
  1054. {
  1055. struct fwnode_handle *endpoint = NULL;
  1056. acpi_graph_get_remote_endpoint(fwnode, NULL, NULL, &endpoint);
  1057. return endpoint;
  1058. }
  1059. static struct fwnode_handle *
  1060. acpi_fwnode_get_parent(struct fwnode_handle *fwnode)
  1061. {
  1062. return acpi_node_get_parent(fwnode);
  1063. }
  1064. static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode,
  1065. struct fwnode_endpoint *endpoint)
  1066. {
  1067. struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode);
  1068. endpoint->local_fwnode = fwnode;
  1069. fwnode_property_read_u32(port_fwnode, "port", &endpoint->port);
  1070. fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id);
  1071. return 0;
  1072. }
  1073. #define DECLARE_ACPI_FWNODE_OPS(ops) \
  1074. const struct fwnode_operations ops = { \
  1075. .device_is_available = acpi_fwnode_device_is_available, \
  1076. .property_present = acpi_fwnode_property_present, \
  1077. .property_read_int_array = \
  1078. acpi_fwnode_property_read_int_array, \
  1079. .property_read_string_array = \
  1080. acpi_fwnode_property_read_string_array, \
  1081. .get_parent = acpi_node_get_parent, \
  1082. .get_next_child_node = acpi_get_next_subnode, \
  1083. .get_named_child_node = acpi_fwnode_get_named_child_node, \
  1084. .get_reference_args = acpi_fwnode_get_reference_args, \
  1085. .graph_get_next_endpoint = \
  1086. acpi_fwnode_graph_get_next_endpoint, \
  1087. .graph_get_remote_endpoint = \
  1088. acpi_fwnode_graph_get_remote_endpoint, \
  1089. .graph_get_port_parent = acpi_fwnode_get_parent, \
  1090. .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \
  1091. }; \
  1092. EXPORT_SYMBOL_GPL(ops)
  1093. DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops);
  1094. DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops);
  1095. const struct fwnode_operations acpi_static_fwnode_ops;
  1096. bool is_acpi_device_node(const struct fwnode_handle *fwnode)
  1097. {
  1098. return !IS_ERR_OR_NULL(fwnode) &&
  1099. fwnode->ops == &acpi_device_fwnode_ops;
  1100. }
  1101. EXPORT_SYMBOL(is_acpi_device_node);
  1102. bool is_acpi_data_node(const struct fwnode_handle *fwnode)
  1103. {
  1104. return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &acpi_data_fwnode_ops;
  1105. }
  1106. EXPORT_SYMBOL(is_acpi_data_node);