property.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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(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 UUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
  24. static const u8 prp_uuid[16] = {
  25. 0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d,
  26. 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01
  27. };
  28. /* ACPI _DSD data subnodes UUID: dbb8e3e6-5886-4ba6-8795-1319f52a966b */
  29. static const u8 ads_uuid[16] = {
  30. 0xe6, 0xe3, 0xb8, 0xdb, 0x86, 0x58, 0xa6, 0x4b,
  31. 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b
  32. };
  33. static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
  34. const union acpi_object *desc,
  35. struct acpi_device_data *data,
  36. struct fwnode_handle *parent);
  37. static bool acpi_extract_properties(const union acpi_object *desc,
  38. struct acpi_device_data *data);
  39. static bool acpi_nondev_subnode_extract(const union acpi_object *desc,
  40. acpi_handle handle,
  41. const union acpi_object *link,
  42. struct list_head *list,
  43. struct fwnode_handle *parent)
  44. {
  45. struct acpi_data_node *dn;
  46. bool result;
  47. dn = kzalloc(sizeof(*dn), GFP_KERNEL);
  48. if (!dn)
  49. return false;
  50. dn->name = link->package.elements[0].string.pointer;
  51. dn->fwnode.type = FWNODE_ACPI_DATA;
  52. dn->parent = parent;
  53. INIT_LIST_HEAD(&dn->data.subnodes);
  54. result = acpi_extract_properties(desc, &dn->data);
  55. if (handle) {
  56. acpi_handle scope;
  57. acpi_status status;
  58. /*
  59. * The scope for the subnode object lookup is the one of the
  60. * namespace node (device) containing the object that has
  61. * returned the package. That is, it's the scope of that
  62. * object's parent.
  63. */
  64. status = acpi_get_parent(handle, &scope);
  65. if (ACPI_SUCCESS(status)
  66. && acpi_enumerate_nondev_subnodes(scope, desc, &dn->data,
  67. &dn->fwnode))
  68. result = true;
  69. } else if (acpi_enumerate_nondev_subnodes(NULL, desc, &dn->data,
  70. &dn->fwnode)) {
  71. result = true;
  72. }
  73. if (result) {
  74. dn->handle = handle;
  75. dn->data.pointer = desc;
  76. list_add_tail(&dn->sibling, list);
  77. return true;
  78. }
  79. kfree(dn);
  80. acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n");
  81. return false;
  82. }
  83. static bool acpi_nondev_subnode_data_ok(acpi_handle handle,
  84. const union acpi_object *link,
  85. struct list_head *list,
  86. struct fwnode_handle *parent)
  87. {
  88. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
  89. acpi_status status;
  90. status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf,
  91. ACPI_TYPE_PACKAGE);
  92. if (ACPI_FAILURE(status))
  93. return false;
  94. if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list,
  95. parent))
  96. return true;
  97. ACPI_FREE(buf.pointer);
  98. return false;
  99. }
  100. static bool acpi_nondev_subnode_ok(acpi_handle scope,
  101. const union acpi_object *link,
  102. struct list_head *list,
  103. struct fwnode_handle *parent)
  104. {
  105. acpi_handle handle;
  106. acpi_status status;
  107. if (!scope)
  108. return false;
  109. status = acpi_get_handle(scope, link->package.elements[1].string.pointer,
  110. &handle);
  111. if (ACPI_FAILURE(status))
  112. return false;
  113. return acpi_nondev_subnode_data_ok(handle, link, list, parent);
  114. }
  115. static int acpi_add_nondev_subnodes(acpi_handle scope,
  116. const union acpi_object *links,
  117. struct list_head *list,
  118. struct fwnode_handle *parent)
  119. {
  120. bool ret = false;
  121. int i;
  122. for (i = 0; i < links->package.count; i++) {
  123. const union acpi_object *link, *desc;
  124. acpi_handle handle;
  125. bool result;
  126. link = &links->package.elements[i];
  127. /* Only two elements allowed. */
  128. if (link->package.count != 2)
  129. continue;
  130. /* The first one must be a string. */
  131. if (link->package.elements[0].type != ACPI_TYPE_STRING)
  132. continue;
  133. /* The second one may be a string, a reference or a package. */
  134. switch (link->package.elements[1].type) {
  135. case ACPI_TYPE_STRING:
  136. result = acpi_nondev_subnode_ok(scope, link, list,
  137. parent);
  138. break;
  139. case ACPI_TYPE_LOCAL_REFERENCE:
  140. handle = link->package.elements[1].reference.handle;
  141. result = acpi_nondev_subnode_data_ok(handle, link, list,
  142. parent);
  143. break;
  144. case ACPI_TYPE_PACKAGE:
  145. desc = &link->package.elements[1];
  146. result = acpi_nondev_subnode_extract(desc, NULL, link,
  147. list, parent);
  148. break;
  149. default:
  150. result = false;
  151. break;
  152. }
  153. ret = ret || result;
  154. }
  155. return ret;
  156. }
  157. static bool acpi_enumerate_nondev_subnodes(acpi_handle scope,
  158. const union acpi_object *desc,
  159. struct acpi_device_data *data,
  160. struct fwnode_handle *parent)
  161. {
  162. int i;
  163. /* Look for the ACPI data subnodes UUID. */
  164. for (i = 0; i < desc->package.count; i += 2) {
  165. const union acpi_object *uuid, *links;
  166. uuid = &desc->package.elements[i];
  167. links = &desc->package.elements[i + 1];
  168. /*
  169. * The first element must be a UUID and the second one must be
  170. * a package.
  171. */
  172. if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
  173. || links->type != ACPI_TYPE_PACKAGE)
  174. break;
  175. if (memcmp(uuid->buffer.pointer, ads_uuid, sizeof(ads_uuid)))
  176. continue;
  177. return acpi_add_nondev_subnodes(scope, links, &data->subnodes,
  178. parent);
  179. }
  180. return false;
  181. }
  182. static bool acpi_property_value_ok(const union acpi_object *value)
  183. {
  184. int j;
  185. /*
  186. * The value must be an integer, a string, a reference, or a package
  187. * whose every element must be an integer, a string, or a reference.
  188. */
  189. switch (value->type) {
  190. case ACPI_TYPE_INTEGER:
  191. case ACPI_TYPE_STRING:
  192. case ACPI_TYPE_LOCAL_REFERENCE:
  193. return true;
  194. case ACPI_TYPE_PACKAGE:
  195. for (j = 0; j < value->package.count; j++)
  196. switch (value->package.elements[j].type) {
  197. case ACPI_TYPE_INTEGER:
  198. case ACPI_TYPE_STRING:
  199. case ACPI_TYPE_LOCAL_REFERENCE:
  200. continue;
  201. default:
  202. return false;
  203. }
  204. return true;
  205. }
  206. return false;
  207. }
  208. static bool acpi_properties_format_valid(const union acpi_object *properties)
  209. {
  210. int i;
  211. for (i = 0; i < properties->package.count; i++) {
  212. const union acpi_object *property;
  213. property = &properties->package.elements[i];
  214. /*
  215. * Only two elements allowed, the first one must be a string and
  216. * the second one has to satisfy certain conditions.
  217. */
  218. if (property->package.count != 2
  219. || property->package.elements[0].type != ACPI_TYPE_STRING
  220. || !acpi_property_value_ok(&property->package.elements[1]))
  221. return false;
  222. }
  223. return true;
  224. }
  225. static void acpi_init_of_compatible(struct acpi_device *adev)
  226. {
  227. const union acpi_object *of_compatible;
  228. int ret;
  229. ret = acpi_data_get_property_array(&adev->data, "compatible",
  230. ACPI_TYPE_STRING, &of_compatible);
  231. if (ret) {
  232. ret = acpi_dev_get_property(adev, "compatible",
  233. ACPI_TYPE_STRING, &of_compatible);
  234. if (ret) {
  235. if (adev->parent
  236. && adev->parent->flags.of_compatible_ok)
  237. goto out;
  238. return;
  239. }
  240. }
  241. adev->data.of_compatible = of_compatible;
  242. out:
  243. adev->flags.of_compatible_ok = 1;
  244. }
  245. static bool acpi_extract_properties(const union acpi_object *desc,
  246. struct acpi_device_data *data)
  247. {
  248. int i;
  249. if (desc->package.count % 2)
  250. return false;
  251. /* Look for the device properties UUID. */
  252. for (i = 0; i < desc->package.count; i += 2) {
  253. const union acpi_object *uuid, *properties;
  254. uuid = &desc->package.elements[i];
  255. properties = &desc->package.elements[i + 1];
  256. /*
  257. * The first element must be a UUID and the second one must be
  258. * a package.
  259. */
  260. if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
  261. || properties->type != ACPI_TYPE_PACKAGE)
  262. break;
  263. if (memcmp(uuid->buffer.pointer, prp_uuid, sizeof(prp_uuid)))
  264. continue;
  265. /*
  266. * We found the matching UUID. 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. /*
  284. * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
  285. * Device Tree compatible properties for this device.
  286. */
  287. list_for_each_entry(hwid, &adev->pnp.ids, list) {
  288. if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
  289. acpi_of = true;
  290. break;
  291. }
  292. }
  293. status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
  294. ACPI_TYPE_PACKAGE);
  295. if (ACPI_FAILURE(status))
  296. goto out;
  297. if (acpi_extract_properties(buf.pointer, &adev->data)) {
  298. adev->data.pointer = buf.pointer;
  299. if (acpi_of)
  300. acpi_init_of_compatible(adev);
  301. }
  302. if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer,
  303. &adev->data, acpi_fwnode_handle(adev)))
  304. adev->data.pointer = buf.pointer;
  305. if (!adev->data.pointer) {
  306. acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n");
  307. ACPI_FREE(buf.pointer);
  308. }
  309. out:
  310. if (acpi_of && !adev->flags.of_compatible_ok)
  311. acpi_handle_info(adev->handle,
  312. ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
  313. }
  314. static void acpi_destroy_nondev_subnodes(struct list_head *list)
  315. {
  316. struct acpi_data_node *dn, *next;
  317. if (list_empty(list))
  318. return;
  319. list_for_each_entry_safe_reverse(dn, next, list, sibling) {
  320. acpi_destroy_nondev_subnodes(&dn->data.subnodes);
  321. wait_for_completion(&dn->kobj_done);
  322. list_del(&dn->sibling);
  323. ACPI_FREE((void *)dn->data.pointer);
  324. kfree(dn);
  325. }
  326. }
  327. void acpi_free_properties(struct acpi_device *adev)
  328. {
  329. acpi_destroy_nondev_subnodes(&adev->data.subnodes);
  330. ACPI_FREE((void *)adev->data.pointer);
  331. adev->data.of_compatible = NULL;
  332. adev->data.pointer = NULL;
  333. adev->data.properties = NULL;
  334. }
  335. /**
  336. * acpi_data_get_property - return an ACPI property with given name
  337. * @data: ACPI device deta object to get the property from
  338. * @name: Name of the property
  339. * @type: Expected property type
  340. * @obj: Location to store the property value (if not %NULL)
  341. *
  342. * Look up a property with @name and store a pointer to the resulting ACPI
  343. * object at the location pointed to by @obj if found.
  344. *
  345. * Callers must not attempt to free the returned objects. These objects will be
  346. * freed by the ACPI core automatically during the removal of @data.
  347. *
  348. * Return: %0 if property with @name has been found (success),
  349. * %-EINVAL if the arguments are invalid,
  350. * %-EINVAL if the property doesn't exist,
  351. * %-EPROTO if the property value type doesn't match @type.
  352. */
  353. static int acpi_data_get_property(struct acpi_device_data *data,
  354. const char *name, acpi_object_type type,
  355. const union acpi_object **obj)
  356. {
  357. const union acpi_object *properties;
  358. int i;
  359. if (!data || !name)
  360. return -EINVAL;
  361. if (!data->pointer || !data->properties)
  362. return -EINVAL;
  363. properties = data->properties;
  364. for (i = 0; i < properties->package.count; i++) {
  365. const union acpi_object *propname, *propvalue;
  366. const union acpi_object *property;
  367. property = &properties->package.elements[i];
  368. propname = &property->package.elements[0];
  369. propvalue = &property->package.elements[1];
  370. if (!strcmp(name, propname->string.pointer)) {
  371. if (type != ACPI_TYPE_ANY && propvalue->type != type)
  372. return -EPROTO;
  373. if (obj)
  374. *obj = propvalue;
  375. return 0;
  376. }
  377. }
  378. return -EINVAL;
  379. }
  380. /**
  381. * acpi_dev_get_property - return an ACPI property with given name.
  382. * @adev: ACPI device to get the property from.
  383. * @name: Name of the property.
  384. * @type: Expected property type.
  385. * @obj: Location to store the property value (if not %NULL).
  386. */
  387. int acpi_dev_get_property(struct acpi_device *adev, const char *name,
  388. acpi_object_type type, const union acpi_object **obj)
  389. {
  390. return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL;
  391. }
  392. EXPORT_SYMBOL_GPL(acpi_dev_get_property);
  393. static struct acpi_device_data *acpi_device_data_of_node(struct fwnode_handle *fwnode)
  394. {
  395. if (fwnode->type == FWNODE_ACPI) {
  396. struct acpi_device *adev = to_acpi_device_node(fwnode);
  397. return &adev->data;
  398. } else if (fwnode->type == FWNODE_ACPI_DATA) {
  399. struct acpi_data_node *dn = to_acpi_data_node(fwnode);
  400. return &dn->data;
  401. }
  402. return NULL;
  403. }
  404. /**
  405. * acpi_node_prop_get - return an ACPI property with given name.
  406. * @fwnode: Firmware node to get the property from.
  407. * @propname: Name of the property.
  408. * @valptr: Location to store a pointer to the property value (if not %NULL).
  409. */
  410. int acpi_node_prop_get(struct fwnode_handle *fwnode, const char *propname,
  411. void **valptr)
  412. {
  413. return acpi_data_get_property(acpi_device_data_of_node(fwnode),
  414. propname, ACPI_TYPE_ANY,
  415. (const union acpi_object **)valptr);
  416. }
  417. /**
  418. * acpi_data_get_property_array - return an ACPI array property with given name
  419. * @adev: ACPI data object to get the property from
  420. * @name: Name of the property
  421. * @type: Expected type of array elements
  422. * @obj: Location to store a pointer to the property value (if not NULL)
  423. *
  424. * Look up an array property with @name and store a pointer to the resulting
  425. * ACPI object at the location pointed to by @obj if found.
  426. *
  427. * Callers must not attempt to free the returned objects. Those objects will be
  428. * freed by the ACPI core automatically during the removal of @data.
  429. *
  430. * Return: %0 if array property (package) with @name has been found (success),
  431. * %-EINVAL if the arguments are invalid,
  432. * %-EINVAL if the property doesn't exist,
  433. * %-EPROTO if the property is not a package or the type of its elements
  434. * doesn't match @type.
  435. */
  436. static int acpi_data_get_property_array(struct acpi_device_data *data,
  437. const char *name,
  438. acpi_object_type type,
  439. const union acpi_object **obj)
  440. {
  441. const union acpi_object *prop;
  442. int ret, i;
  443. ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop);
  444. if (ret)
  445. return ret;
  446. if (type != ACPI_TYPE_ANY) {
  447. /* Check that all elements are of correct type. */
  448. for (i = 0; i < prop->package.count; i++)
  449. if (prop->package.elements[i].type != type)
  450. return -EPROTO;
  451. }
  452. if (obj)
  453. *obj = prop;
  454. return 0;
  455. }
  456. /**
  457. * __acpi_node_get_property_reference - returns handle to the referenced object
  458. * @fwnode: Firmware node to get the property from
  459. * @propname: Name of the property
  460. * @index: Index of the reference to return
  461. * @num_args: Maximum number of arguments after each reference
  462. * @args: Location to store the returned reference with optional arguments
  463. *
  464. * Find property with @name, verifify that it is a package containing at least
  465. * one object reference and if so, store the ACPI device object pointer to the
  466. * target object in @args->adev. If the reference includes arguments, store
  467. * them in the @args->args[] array.
  468. *
  469. * If there's more than one reference in the property value package, @index is
  470. * used to select the one to return.
  471. *
  472. * It is possible to leave holes in the property value set like in the
  473. * example below:
  474. *
  475. * Package () {
  476. * "cs-gpios",
  477. * Package () {
  478. * ^GPIO, 19, 0, 0,
  479. * ^GPIO, 20, 0, 0,
  480. * 0,
  481. * ^GPIO, 21, 0, 0,
  482. * }
  483. * }
  484. *
  485. * Calling this function with index %2 return %-ENOENT and with index %3
  486. * returns the last entry. If the property does not contain any more values
  487. * %-ENODATA is returned. The NULL entry must be single integer and
  488. * preferably contain value %0.
  489. *
  490. * Return: %0 on success, negative error code on failure.
  491. */
  492. int __acpi_node_get_property_reference(struct fwnode_handle *fwnode,
  493. const char *propname, size_t index, size_t num_args,
  494. struct acpi_reference_args *args)
  495. {
  496. const union acpi_object *element, *end;
  497. const union acpi_object *obj;
  498. struct acpi_device_data *data;
  499. struct acpi_device *device;
  500. int ret, idx = 0;
  501. data = acpi_device_data_of_node(fwnode);
  502. if (!data)
  503. return -EINVAL;
  504. ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj);
  505. if (ret)
  506. return ret;
  507. /*
  508. * The simplest case is when the value is a single reference. Just
  509. * return that reference then.
  510. */
  511. if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
  512. if (index)
  513. return -EINVAL;
  514. ret = acpi_bus_get_device(obj->reference.handle, &device);
  515. if (ret)
  516. return ret;
  517. args->adev = device;
  518. args->nargs = 0;
  519. return 0;
  520. }
  521. /*
  522. * If it is not a single reference, then it is a package of
  523. * references followed by number of ints as follows:
  524. *
  525. * Package () { REF, INT, REF, INT, INT }
  526. *
  527. * The index argument is then used to determine which reference
  528. * the caller wants (along with the arguments).
  529. */
  530. if (obj->type != ACPI_TYPE_PACKAGE || index >= obj->package.count)
  531. return -EPROTO;
  532. element = obj->package.elements;
  533. end = element + obj->package.count;
  534. while (element < end) {
  535. u32 nargs, i;
  536. if (element->type == ACPI_TYPE_LOCAL_REFERENCE) {
  537. ret = acpi_bus_get_device(element->reference.handle,
  538. &device);
  539. if (ret)
  540. return -ENODEV;
  541. nargs = 0;
  542. element++;
  543. /* assume following integer elements are all args */
  544. for (i = 0; element + i < end && i < num_args; i++) {
  545. int type = element[i].type;
  546. if (type == ACPI_TYPE_INTEGER)
  547. nargs++;
  548. else if (type == ACPI_TYPE_LOCAL_REFERENCE)
  549. break;
  550. else
  551. return -EPROTO;
  552. }
  553. if (nargs > MAX_ACPI_REFERENCE_ARGS)
  554. return -EPROTO;
  555. if (idx == index) {
  556. args->adev = device;
  557. args->nargs = nargs;
  558. for (i = 0; i < nargs; i++)
  559. args->args[i] = element[i].integer.value;
  560. return 0;
  561. }
  562. element += nargs;
  563. } else if (element->type == ACPI_TYPE_INTEGER) {
  564. if (idx == index)
  565. return -ENOENT;
  566. element++;
  567. } else {
  568. return -EPROTO;
  569. }
  570. idx++;
  571. }
  572. return -ENODATA;
  573. }
  574. EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference);
  575. static int acpi_data_prop_read_single(struct acpi_device_data *data,
  576. const char *propname,
  577. enum dev_prop_type proptype, void *val)
  578. {
  579. const union acpi_object *obj;
  580. int ret;
  581. if (!val)
  582. return -EINVAL;
  583. if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
  584. ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj);
  585. if (ret)
  586. return ret;
  587. switch (proptype) {
  588. case DEV_PROP_U8:
  589. if (obj->integer.value > U8_MAX)
  590. return -EOVERFLOW;
  591. *(u8 *)val = obj->integer.value;
  592. break;
  593. case DEV_PROP_U16:
  594. if (obj->integer.value > U16_MAX)
  595. return -EOVERFLOW;
  596. *(u16 *)val = obj->integer.value;
  597. break;
  598. case DEV_PROP_U32:
  599. if (obj->integer.value > U32_MAX)
  600. return -EOVERFLOW;
  601. *(u32 *)val = obj->integer.value;
  602. break;
  603. default:
  604. *(u64 *)val = obj->integer.value;
  605. break;
  606. }
  607. } else if (proptype == DEV_PROP_STRING) {
  608. ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj);
  609. if (ret)
  610. return ret;
  611. *(char **)val = obj->string.pointer;
  612. return 1;
  613. } else {
  614. ret = -EINVAL;
  615. }
  616. return ret;
  617. }
  618. int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
  619. enum dev_prop_type proptype, void *val)
  620. {
  621. int ret;
  622. if (!adev)
  623. return -EINVAL;
  624. ret = acpi_data_prop_read_single(&adev->data, propname, proptype, val);
  625. if (ret < 0 || proptype != ACPI_TYPE_STRING)
  626. return ret;
  627. return 0;
  628. }
  629. static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
  630. size_t nval)
  631. {
  632. int i;
  633. for (i = 0; i < nval; i++) {
  634. if (items[i].type != ACPI_TYPE_INTEGER)
  635. return -EPROTO;
  636. if (items[i].integer.value > U8_MAX)
  637. return -EOVERFLOW;
  638. val[i] = items[i].integer.value;
  639. }
  640. return 0;
  641. }
  642. static int acpi_copy_property_array_u16(const union acpi_object *items,
  643. u16 *val, size_t nval)
  644. {
  645. int i;
  646. for (i = 0; i < nval; i++) {
  647. if (items[i].type != ACPI_TYPE_INTEGER)
  648. return -EPROTO;
  649. if (items[i].integer.value > U16_MAX)
  650. return -EOVERFLOW;
  651. val[i] = items[i].integer.value;
  652. }
  653. return 0;
  654. }
  655. static int acpi_copy_property_array_u32(const union acpi_object *items,
  656. u32 *val, size_t nval)
  657. {
  658. int i;
  659. for (i = 0; i < nval; i++) {
  660. if (items[i].type != ACPI_TYPE_INTEGER)
  661. return -EPROTO;
  662. if (items[i].integer.value > U32_MAX)
  663. return -EOVERFLOW;
  664. val[i] = items[i].integer.value;
  665. }
  666. return 0;
  667. }
  668. static int acpi_copy_property_array_u64(const union acpi_object *items,
  669. u64 *val, size_t nval)
  670. {
  671. int i;
  672. for (i = 0; i < nval; i++) {
  673. if (items[i].type != ACPI_TYPE_INTEGER)
  674. return -EPROTO;
  675. val[i] = items[i].integer.value;
  676. }
  677. return 0;
  678. }
  679. static int acpi_copy_property_array_string(const union acpi_object *items,
  680. char **val, size_t nval)
  681. {
  682. int i;
  683. for (i = 0; i < nval; i++) {
  684. if (items[i].type != ACPI_TYPE_STRING)
  685. return -EPROTO;
  686. val[i] = items[i].string.pointer;
  687. }
  688. return nval;
  689. }
  690. static int acpi_data_prop_read(struct acpi_device_data *data,
  691. const char *propname,
  692. enum dev_prop_type proptype,
  693. void *val, size_t nval)
  694. {
  695. const union acpi_object *obj;
  696. const union acpi_object *items;
  697. int ret;
  698. if (val && nval == 1) {
  699. ret = acpi_data_prop_read_single(data, propname, proptype, val);
  700. if (ret >= 0)
  701. return ret;
  702. }
  703. ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj);
  704. if (ret)
  705. return ret;
  706. if (!val)
  707. return obj->package.count;
  708. if (proptype != DEV_PROP_STRING && nval > obj->package.count)
  709. return -EOVERFLOW;
  710. else if (nval <= 0)
  711. return -EINVAL;
  712. items = obj->package.elements;
  713. switch (proptype) {
  714. case DEV_PROP_U8:
  715. ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
  716. break;
  717. case DEV_PROP_U16:
  718. ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
  719. break;
  720. case DEV_PROP_U32:
  721. ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
  722. break;
  723. case DEV_PROP_U64:
  724. ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
  725. break;
  726. case DEV_PROP_STRING:
  727. ret = acpi_copy_property_array_string(
  728. items, (char **)val,
  729. min_t(u32, nval, obj->package.count));
  730. break;
  731. default:
  732. ret = -EINVAL;
  733. break;
  734. }
  735. return ret;
  736. }
  737. int acpi_dev_prop_read(struct acpi_device *adev, const char *propname,
  738. enum dev_prop_type proptype, void *val, size_t nval)
  739. {
  740. return adev ? acpi_data_prop_read(&adev->data, propname, proptype, val, nval) : -EINVAL;
  741. }
  742. /**
  743. * acpi_node_prop_read - retrieve the value of an ACPI property with given name.
  744. * @fwnode: Firmware node to get the property from.
  745. * @propname: Name of the property.
  746. * @proptype: Expected property type.
  747. * @val: Location to store the property value (if not %NULL).
  748. * @nval: Size of the array pointed to by @val.
  749. *
  750. * If @val is %NULL, return the number of array elements comprising the value
  751. * of the property. Otherwise, read at most @nval values to the array at the
  752. * location pointed to by @val.
  753. */
  754. int acpi_node_prop_read(struct fwnode_handle *fwnode, const char *propname,
  755. enum dev_prop_type proptype, void *val, size_t nval)
  756. {
  757. return acpi_data_prop_read(acpi_device_data_of_node(fwnode),
  758. propname, proptype, val, nval);
  759. }
  760. /**
  761. * acpi_get_next_subnode - Return the next child node handle for a fwnode
  762. * @fwnode: Firmware node to find the next child node for.
  763. * @child: Handle to one of the device's child nodes or a null handle.
  764. */
  765. struct fwnode_handle *acpi_get_next_subnode(struct fwnode_handle *fwnode,
  766. struct fwnode_handle *child)
  767. {
  768. struct acpi_device *adev = to_acpi_device_node(fwnode);
  769. struct list_head *head, *next;
  770. if (!child || child->type == FWNODE_ACPI) {
  771. if (adev)
  772. head = &adev->children;
  773. else
  774. goto nondev;
  775. if (list_empty(head))
  776. goto nondev;
  777. if (child) {
  778. adev = to_acpi_device_node(child);
  779. next = adev->node.next;
  780. if (next == head) {
  781. child = NULL;
  782. goto nondev;
  783. }
  784. adev = list_entry(next, struct acpi_device, node);
  785. } else {
  786. adev = list_first_entry(head, struct acpi_device, node);
  787. }
  788. return acpi_fwnode_handle(adev);
  789. }
  790. nondev:
  791. if (!child || child->type == FWNODE_ACPI_DATA) {
  792. struct acpi_data_node *data = to_acpi_data_node(fwnode);
  793. struct acpi_data_node *dn;
  794. if (adev)
  795. head = &adev->data.subnodes;
  796. else if (data)
  797. head = &data->data.subnodes;
  798. else
  799. return NULL;
  800. if (list_empty(head))
  801. return NULL;
  802. if (child) {
  803. dn = to_acpi_data_node(child);
  804. next = dn->sibling.next;
  805. if (next == head)
  806. return NULL;
  807. dn = list_entry(next, struct acpi_data_node, sibling);
  808. } else {
  809. dn = list_first_entry(head, struct acpi_data_node, sibling);
  810. }
  811. return &dn->fwnode;
  812. }
  813. return NULL;
  814. }
  815. /**
  816. * acpi_node_get_parent - Return parent fwnode of this fwnode
  817. * @fwnode: Firmware node whose parent to get
  818. *
  819. * Returns parent node of an ACPI device or data firmware node or %NULL if
  820. * not available.
  821. */
  822. struct fwnode_handle *acpi_node_get_parent(struct fwnode_handle *fwnode)
  823. {
  824. if (is_acpi_data_node(fwnode)) {
  825. /* All data nodes have parent pointer so just return that */
  826. return to_acpi_data_node(fwnode)->parent;
  827. } else if (is_acpi_device_node(fwnode)) {
  828. acpi_handle handle, parent_handle;
  829. handle = to_acpi_device_node(fwnode)->handle;
  830. if (ACPI_SUCCESS(acpi_get_parent(handle, &parent_handle))) {
  831. struct acpi_device *adev;
  832. if (!acpi_bus_get_device(parent_handle, &adev))
  833. return acpi_fwnode_handle(adev);
  834. }
  835. }
  836. return NULL;
  837. }
  838. /**
  839. * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node
  840. * @fwnode: Pointer to the parent firmware node
  841. * @prev: Previous endpoint node or %NULL to get the first
  842. *
  843. * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns
  844. * %NULL if there is no next endpoint, ERR_PTR() in case of error. In case
  845. * of success the next endpoint is returned.
  846. */
  847. struct fwnode_handle *acpi_graph_get_next_endpoint(struct fwnode_handle *fwnode,
  848. struct fwnode_handle *prev)
  849. {
  850. struct fwnode_handle *port = NULL;
  851. struct fwnode_handle *endpoint;
  852. if (!prev) {
  853. do {
  854. port = fwnode_get_next_child_node(fwnode, port);
  855. /* Ports must have port property */
  856. if (fwnode_property_present(port, "port"))
  857. break;
  858. } while (port);
  859. } else {
  860. port = fwnode_get_parent(prev);
  861. }
  862. if (!port)
  863. return NULL;
  864. endpoint = fwnode_get_next_child_node(port, prev);
  865. while (!endpoint) {
  866. port = fwnode_get_next_child_node(fwnode, port);
  867. if (!port)
  868. break;
  869. if (fwnode_property_present(port, "port"))
  870. endpoint = fwnode_get_next_child_node(port, NULL);
  871. }
  872. if (endpoint) {
  873. /* Endpoints must have "endpoint" property */
  874. if (!fwnode_property_present(endpoint, "endpoint"))
  875. return ERR_PTR(-EPROTO);
  876. }
  877. return endpoint;
  878. }
  879. /**
  880. * acpi_graph_get_child_prop_value - Return a child with a given property value
  881. * @fwnode: device fwnode
  882. * @prop_name: The name of the property to look for
  883. * @val: the desired property value
  884. *
  885. * Return the port node corresponding to a given port number. Returns
  886. * the child node on success, NULL otherwise.
  887. */
  888. static struct fwnode_handle *acpi_graph_get_child_prop_value(
  889. struct fwnode_handle *fwnode, const char *prop_name, unsigned int val)
  890. {
  891. struct fwnode_handle *child;
  892. fwnode_for_each_child_node(fwnode, child) {
  893. u32 nr;
  894. if (!fwnode_property_read_u32(fwnode, prop_name, &nr))
  895. continue;
  896. if (val == nr)
  897. return child;
  898. }
  899. return NULL;
  900. }
  901. /**
  902. * acpi_graph_get_remote_enpoint - Parses and returns remote end of an endpoint
  903. * @fwnode: Endpoint firmware node pointing to a remote device
  904. * @parent: Firmware node of remote port parent is filled here if not %NULL
  905. * @port: Firmware node of remote port is filled here if not %NULL
  906. * @endpoint: Firmware node of remote endpoint is filled here if not %NULL
  907. *
  908. * Function parses remote end of ACPI firmware remote endpoint and fills in
  909. * fields requested by the caller. Returns %0 in case of success and
  910. * negative errno otherwise.
  911. */
  912. int acpi_graph_get_remote_endpoint(struct fwnode_handle *fwnode,
  913. struct fwnode_handle **parent,
  914. struct fwnode_handle **port,
  915. struct fwnode_handle **endpoint)
  916. {
  917. unsigned int port_nr, endpoint_nr;
  918. struct acpi_reference_args args;
  919. int ret;
  920. memset(&args, 0, sizeof(args));
  921. ret = acpi_node_get_property_reference(fwnode, "remote-endpoint", 0,
  922. &args);
  923. if (ret)
  924. return ret;
  925. /*
  926. * Always require two arguments with the reference: port and
  927. * endpoint indices.
  928. */
  929. if (args.nargs != 2)
  930. return -EPROTO;
  931. fwnode = acpi_fwnode_handle(args.adev);
  932. port_nr = args.args[0];
  933. endpoint_nr = args.args[1];
  934. if (parent)
  935. *parent = fwnode;
  936. if (!port && !endpoint)
  937. return 0;
  938. fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr);
  939. if (!fwnode)
  940. return -EPROTO;
  941. if (port)
  942. *port = fwnode;
  943. if (!endpoint)
  944. return 0;
  945. fwnode = acpi_graph_get_child_prop_value(fwnode, "endpoint",
  946. endpoint_nr);
  947. if (!fwnode)
  948. return -EPROTO;
  949. *endpoint = fwnode;
  950. return 0;
  951. }