property.c 35 KB

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