property.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. /* ACPI _DSD device properties UUID: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */
  20. static const u8 prp_uuid[16] = {
  21. 0x14, 0xd8, 0xff, 0xda, 0xba, 0x6e, 0x8c, 0x4d,
  22. 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01
  23. };
  24. static bool acpi_property_value_ok(const union acpi_object *value)
  25. {
  26. int j;
  27. /*
  28. * The value must be an integer, a string, a reference, or a package
  29. * whose every element must be an integer, a string, or a reference.
  30. */
  31. switch (value->type) {
  32. case ACPI_TYPE_INTEGER:
  33. case ACPI_TYPE_STRING:
  34. case ACPI_TYPE_LOCAL_REFERENCE:
  35. return true;
  36. case ACPI_TYPE_PACKAGE:
  37. for (j = 0; j < value->package.count; j++)
  38. switch (value->package.elements[j].type) {
  39. case ACPI_TYPE_INTEGER:
  40. case ACPI_TYPE_STRING:
  41. case ACPI_TYPE_LOCAL_REFERENCE:
  42. continue;
  43. default:
  44. return false;
  45. }
  46. return true;
  47. }
  48. return false;
  49. }
  50. static bool acpi_properties_format_valid(const union acpi_object *properties)
  51. {
  52. int i;
  53. for (i = 0; i < properties->package.count; i++) {
  54. const union acpi_object *property;
  55. property = &properties->package.elements[i];
  56. /*
  57. * Only two elements allowed, the first one must be a string and
  58. * the second one has to satisfy certain conditions.
  59. */
  60. if (property->package.count != 2
  61. || property->package.elements[0].type != ACPI_TYPE_STRING
  62. || !acpi_property_value_ok(&property->package.elements[1]))
  63. return false;
  64. }
  65. return true;
  66. }
  67. static void acpi_init_of_compatible(struct acpi_device *adev)
  68. {
  69. const union acpi_object *of_compatible;
  70. int ret;
  71. ret = acpi_dev_get_property_array(adev, "compatible", ACPI_TYPE_STRING,
  72. &of_compatible);
  73. if (ret) {
  74. ret = acpi_dev_get_property(adev, "compatible",
  75. ACPI_TYPE_STRING, &of_compatible);
  76. if (ret) {
  77. if (adev->parent
  78. && adev->parent->flags.of_compatible_ok)
  79. goto out;
  80. return;
  81. }
  82. }
  83. adev->data.of_compatible = of_compatible;
  84. out:
  85. adev->flags.of_compatible_ok = 1;
  86. }
  87. void acpi_init_properties(struct acpi_device *adev)
  88. {
  89. struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
  90. bool acpi_of = false;
  91. struct acpi_hardware_id *hwid;
  92. const union acpi_object *desc;
  93. acpi_status status;
  94. int i;
  95. /*
  96. * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in
  97. * Device Tree compatible properties for this device.
  98. */
  99. list_for_each_entry(hwid, &adev->pnp.ids, list) {
  100. if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) {
  101. acpi_of = true;
  102. break;
  103. }
  104. }
  105. status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf,
  106. ACPI_TYPE_PACKAGE);
  107. if (ACPI_FAILURE(status))
  108. goto out;
  109. desc = buf.pointer;
  110. if (desc->package.count % 2)
  111. goto fail;
  112. /* Look for the device properties UUID. */
  113. for (i = 0; i < desc->package.count; i += 2) {
  114. const union acpi_object *uuid, *properties;
  115. uuid = &desc->package.elements[i];
  116. properties = &desc->package.elements[i + 1];
  117. /*
  118. * The first element must be a UUID and the second one must be
  119. * a package.
  120. */
  121. if (uuid->type != ACPI_TYPE_BUFFER || uuid->buffer.length != 16
  122. || properties->type != ACPI_TYPE_PACKAGE)
  123. break;
  124. if (memcmp(uuid->buffer.pointer, prp_uuid, sizeof(prp_uuid)))
  125. continue;
  126. /*
  127. * We found the matching UUID. Now validate the format of the
  128. * package immediately following it.
  129. */
  130. if (!acpi_properties_format_valid(properties))
  131. break;
  132. adev->data.pointer = buf.pointer;
  133. adev->data.properties = properties;
  134. if (acpi_of)
  135. acpi_init_of_compatible(adev);
  136. goto out;
  137. }
  138. fail:
  139. dev_dbg(&adev->dev, "Returned _DSD data is not valid, skipping\n");
  140. ACPI_FREE(buf.pointer);
  141. out:
  142. if (acpi_of && !adev->flags.of_compatible_ok)
  143. acpi_handle_info(adev->handle,
  144. ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n");
  145. }
  146. void acpi_free_properties(struct acpi_device *adev)
  147. {
  148. ACPI_FREE((void *)adev->data.pointer);
  149. adev->data.of_compatible = NULL;
  150. adev->data.pointer = NULL;
  151. adev->data.properties = NULL;
  152. }
  153. /**
  154. * acpi_dev_get_property - return an ACPI property with given name
  155. * @adev: ACPI device to get property
  156. * @name: Name of the property
  157. * @type: Expected property type
  158. * @obj: Location to store the property value (if not %NULL)
  159. *
  160. * Look up a property with @name and store a pointer to the resulting ACPI
  161. * object at the location pointed to by @obj if found.
  162. *
  163. * Callers must not attempt to free the returned objects. These objects will be
  164. * freed by the ACPI core automatically during the removal of @adev.
  165. *
  166. * Return: %0 if property with @name has been found (success),
  167. * %-EINVAL if the arguments are invalid,
  168. * %-ENODATA if the property doesn't exist,
  169. * %-EPROTO if the property value type doesn't match @type.
  170. */
  171. int acpi_dev_get_property(struct acpi_device *adev, const char *name,
  172. acpi_object_type type, const union acpi_object **obj)
  173. {
  174. const union acpi_object *properties;
  175. int i;
  176. if (!adev || !name)
  177. return -EINVAL;
  178. if (!adev->data.pointer || !adev->data.properties)
  179. return -ENODATA;
  180. properties = adev->data.properties;
  181. for (i = 0; i < properties->package.count; i++) {
  182. const union acpi_object *propname, *propvalue;
  183. const union acpi_object *property;
  184. property = &properties->package.elements[i];
  185. propname = &property->package.elements[0];
  186. propvalue = &property->package.elements[1];
  187. if (!strcmp(name, propname->string.pointer)) {
  188. if (type != ACPI_TYPE_ANY && propvalue->type != type)
  189. return -EPROTO;
  190. else if (obj)
  191. *obj = propvalue;
  192. return 0;
  193. }
  194. }
  195. return -ENODATA;
  196. }
  197. EXPORT_SYMBOL_GPL(acpi_dev_get_property);
  198. /**
  199. * acpi_dev_get_property_array - return an ACPI array property with given name
  200. * @adev: ACPI device to get property
  201. * @name: Name of the property
  202. * @type: Expected type of array elements
  203. * @obj: Location to store a pointer to the property value (if not NULL)
  204. *
  205. * Look up an array property with @name and store a pointer to the resulting
  206. * ACPI object at the location pointed to by @obj if found.
  207. *
  208. * Callers must not attempt to free the returned objects. Those objects will be
  209. * freed by the ACPI core automatically during the removal of @adev.
  210. *
  211. * Return: %0 if array property (package) with @name has been found (success),
  212. * %-EINVAL if the arguments are invalid,
  213. * %-ENODATA if the property doesn't exist,
  214. * %-EPROTO if the property is not a package or the type of its elements
  215. * doesn't match @type.
  216. */
  217. int acpi_dev_get_property_array(struct acpi_device *adev, const char *name,
  218. acpi_object_type type,
  219. const union acpi_object **obj)
  220. {
  221. const union acpi_object *prop;
  222. int ret, i;
  223. ret = acpi_dev_get_property(adev, name, ACPI_TYPE_PACKAGE, &prop);
  224. if (ret)
  225. return ret;
  226. if (type != ACPI_TYPE_ANY) {
  227. /* Check that all elements are of correct type. */
  228. for (i = 0; i < prop->package.count; i++)
  229. if (prop->package.elements[i].type != type)
  230. return -EPROTO;
  231. }
  232. if (obj)
  233. *obj = prop;
  234. return 0;
  235. }
  236. EXPORT_SYMBOL_GPL(acpi_dev_get_property_array);
  237. /**
  238. * acpi_dev_get_property_reference - returns handle to the referenced object
  239. * @adev: ACPI device to get property
  240. * @name: Name of the property
  241. * @index: Index of the reference to return
  242. * @args: Location to store the returned reference with optional arguments
  243. *
  244. * Find property with @name, verifify that it is a package containing at least
  245. * one object reference and if so, store the ACPI device object pointer to the
  246. * target object in @args->adev. If the reference includes arguments, store
  247. * them in the @args->args[] array.
  248. *
  249. * If there's more than one reference in the property value package, @index is
  250. * used to select the one to return.
  251. *
  252. * Return: %0 on success, negative error code on failure.
  253. */
  254. int acpi_dev_get_property_reference(struct acpi_device *adev,
  255. const char *name, size_t index,
  256. struct acpi_reference_args *args)
  257. {
  258. const union acpi_object *element, *end;
  259. const union acpi_object *obj;
  260. struct acpi_device *device;
  261. int ret, idx = 0;
  262. ret = acpi_dev_get_property(adev, name, ACPI_TYPE_ANY, &obj);
  263. if (ret)
  264. return ret;
  265. /*
  266. * The simplest case is when the value is a single reference. Just
  267. * return that reference then.
  268. */
  269. if (obj->type == ACPI_TYPE_LOCAL_REFERENCE) {
  270. if (index)
  271. return -EINVAL;
  272. ret = acpi_bus_get_device(obj->reference.handle, &device);
  273. if (ret)
  274. return ret;
  275. args->adev = device;
  276. args->nargs = 0;
  277. return 0;
  278. }
  279. /*
  280. * If it is not a single reference, then it is a package of
  281. * references followed by number of ints as follows:
  282. *
  283. * Package () { REF, INT, REF, INT, INT }
  284. *
  285. * The index argument is then used to determine which reference
  286. * the caller wants (along with the arguments).
  287. */
  288. if (obj->type != ACPI_TYPE_PACKAGE || index >= obj->package.count)
  289. return -EPROTO;
  290. element = obj->package.elements;
  291. end = element + obj->package.count;
  292. while (element < end) {
  293. u32 nargs, i;
  294. if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
  295. return -EPROTO;
  296. ret = acpi_bus_get_device(element->reference.handle, &device);
  297. if (ret)
  298. return -ENODEV;
  299. element++;
  300. nargs = 0;
  301. /* assume following integer elements are all args */
  302. for (i = 0; element + i < end; i++) {
  303. int type = element[i].type;
  304. if (type == ACPI_TYPE_INTEGER)
  305. nargs++;
  306. else if (type == ACPI_TYPE_LOCAL_REFERENCE)
  307. break;
  308. else
  309. return -EPROTO;
  310. }
  311. if (idx++ == index) {
  312. args->adev = device;
  313. args->nargs = nargs;
  314. for (i = 0; i < nargs; i++)
  315. args->args[i] = element[i].integer.value;
  316. return 0;
  317. }
  318. element += nargs;
  319. }
  320. return -EPROTO;
  321. }
  322. EXPORT_SYMBOL_GPL(acpi_dev_get_property_reference);
  323. int acpi_dev_prop_get(struct acpi_device *adev, const char *propname,
  324. void **valptr)
  325. {
  326. return acpi_dev_get_property(adev, propname, ACPI_TYPE_ANY,
  327. (const union acpi_object **)valptr);
  328. }
  329. int acpi_dev_prop_read_single(struct acpi_device *adev, const char *propname,
  330. enum dev_prop_type proptype, void *val)
  331. {
  332. const union acpi_object *obj;
  333. int ret;
  334. if (!val)
  335. return -EINVAL;
  336. if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) {
  337. ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_INTEGER, &obj);
  338. if (ret)
  339. return ret;
  340. switch (proptype) {
  341. case DEV_PROP_U8:
  342. if (obj->integer.value > U8_MAX)
  343. return -EOVERFLOW;
  344. *(u8 *)val = obj->integer.value;
  345. break;
  346. case DEV_PROP_U16:
  347. if (obj->integer.value > U16_MAX)
  348. return -EOVERFLOW;
  349. *(u16 *)val = obj->integer.value;
  350. break;
  351. case DEV_PROP_U32:
  352. if (obj->integer.value > U32_MAX)
  353. return -EOVERFLOW;
  354. *(u32 *)val = obj->integer.value;
  355. break;
  356. default:
  357. *(u64 *)val = obj->integer.value;
  358. break;
  359. }
  360. } else if (proptype == DEV_PROP_STRING) {
  361. ret = acpi_dev_get_property(adev, propname, ACPI_TYPE_STRING, &obj);
  362. if (ret)
  363. return ret;
  364. *(char **)val = obj->string.pointer;
  365. } else {
  366. ret = -EINVAL;
  367. }
  368. return ret;
  369. }
  370. static int acpi_copy_property_array_u8(const union acpi_object *items, u8 *val,
  371. size_t nval)
  372. {
  373. int i;
  374. for (i = 0; i < nval; i++) {
  375. if (items[i].type != ACPI_TYPE_INTEGER)
  376. return -EPROTO;
  377. if (items[i].integer.value > U8_MAX)
  378. return -EOVERFLOW;
  379. val[i] = items[i].integer.value;
  380. }
  381. return 0;
  382. }
  383. static int acpi_copy_property_array_u16(const union acpi_object *items,
  384. u16 *val, size_t nval)
  385. {
  386. int i;
  387. for (i = 0; i < nval; i++) {
  388. if (items[i].type != ACPI_TYPE_INTEGER)
  389. return -EPROTO;
  390. if (items[i].integer.value > U16_MAX)
  391. return -EOVERFLOW;
  392. val[i] = items[i].integer.value;
  393. }
  394. return 0;
  395. }
  396. static int acpi_copy_property_array_u32(const union acpi_object *items,
  397. u32 *val, size_t nval)
  398. {
  399. int i;
  400. for (i = 0; i < nval; i++) {
  401. if (items[i].type != ACPI_TYPE_INTEGER)
  402. return -EPROTO;
  403. if (items[i].integer.value > U32_MAX)
  404. return -EOVERFLOW;
  405. val[i] = items[i].integer.value;
  406. }
  407. return 0;
  408. }
  409. static int acpi_copy_property_array_u64(const union acpi_object *items,
  410. u64 *val, size_t nval)
  411. {
  412. int i;
  413. for (i = 0; i < nval; i++) {
  414. if (items[i].type != ACPI_TYPE_INTEGER)
  415. return -EPROTO;
  416. val[i] = items[i].integer.value;
  417. }
  418. return 0;
  419. }
  420. static int acpi_copy_property_array_string(const union acpi_object *items,
  421. char **val, size_t nval)
  422. {
  423. int i;
  424. for (i = 0; i < nval; i++) {
  425. if (items[i].type != ACPI_TYPE_STRING)
  426. return -EPROTO;
  427. val[i] = items[i].string.pointer;
  428. }
  429. return 0;
  430. }
  431. int acpi_dev_prop_read(struct acpi_device *adev, const char *propname,
  432. enum dev_prop_type proptype, void *val, size_t nval)
  433. {
  434. const union acpi_object *obj;
  435. const union acpi_object *items;
  436. int ret;
  437. if (val && nval == 1) {
  438. ret = acpi_dev_prop_read_single(adev, propname, proptype, val);
  439. if (!ret)
  440. return ret;
  441. }
  442. ret = acpi_dev_get_property_array(adev, propname, ACPI_TYPE_ANY, &obj);
  443. if (ret)
  444. return ret;
  445. if (!val)
  446. return obj->package.count;
  447. else if (nval <= 0)
  448. return -EINVAL;
  449. if (nval > obj->package.count)
  450. return -EOVERFLOW;
  451. items = obj->package.elements;
  452. switch (proptype) {
  453. case DEV_PROP_U8:
  454. ret = acpi_copy_property_array_u8(items, (u8 *)val, nval);
  455. break;
  456. case DEV_PROP_U16:
  457. ret = acpi_copy_property_array_u16(items, (u16 *)val, nval);
  458. break;
  459. case DEV_PROP_U32:
  460. ret = acpi_copy_property_array_u32(items, (u32 *)val, nval);
  461. break;
  462. case DEV_PROP_U64:
  463. ret = acpi_copy_property_array_u64(items, (u64 *)val, nval);
  464. break;
  465. case DEV_PROP_STRING:
  466. ret = acpi_copy_property_array_string(items, (char **)val, nval);
  467. break;
  468. default:
  469. ret = -EINVAL;
  470. break;
  471. }
  472. return ret;
  473. }