property.c 33 KB

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