property.c 34 KB

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