utils.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * acpi_utils.c - ACPI Utility Functions ($Revision: 10 $)
  4. *
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. */
  8. #define pr_fmt(fmt) "ACPI: utils: " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include <linux/init.h>
  13. #include <linux/types.h>
  14. #include <linux/hardirq.h>
  15. #include <linux/acpi.h>
  16. #include <linux/dynamic_debug.h>
  17. #include "internal.h"
  18. #include "sleep.h"
  19. /* --------------------------------------------------------------------------
  20. Object Evaluation Helpers
  21. -------------------------------------------------------------------------- */
  22. static void acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
  23. {
  24. acpi_handle_debug(h, "Evaluate [%s]: %s\n", p, acpi_format_exception(s));
  25. }
  26. acpi_status
  27. acpi_extract_package(union acpi_object *package,
  28. struct acpi_buffer *format, struct acpi_buffer *buffer)
  29. {
  30. u32 size_required = 0;
  31. u32 tail_offset = 0;
  32. char *format_string = NULL;
  33. u32 format_count = 0;
  34. u32 i = 0;
  35. u8 *head = NULL;
  36. u8 *tail = NULL;
  37. if (!package || (package->type != ACPI_TYPE_PACKAGE)
  38. || (package->package.count < 1)) {
  39. pr_debug("Invalid package argument\n");
  40. return AE_BAD_PARAMETER;
  41. }
  42. if (!format || !format->pointer || (format->length < 1)) {
  43. pr_debug("Invalid format argument\n");
  44. return AE_BAD_PARAMETER;
  45. }
  46. if (!buffer) {
  47. pr_debug("Invalid buffer argument\n");
  48. return AE_BAD_PARAMETER;
  49. }
  50. format_count = (format->length / sizeof(char)) - 1;
  51. if (format_count > package->package.count) {
  52. pr_debug("Format specifies more objects [%d] than present [%d]\n",
  53. format_count, package->package.count);
  54. return AE_BAD_DATA;
  55. }
  56. format_string = format->pointer;
  57. /*
  58. * Calculate size_required.
  59. */
  60. for (i = 0; i < format_count; i++) {
  61. union acpi_object *element = &(package->package.elements[i]);
  62. switch (element->type) {
  63. case ACPI_TYPE_INTEGER:
  64. switch (format_string[i]) {
  65. case 'N':
  66. size_required += sizeof(u64);
  67. tail_offset += sizeof(u64);
  68. break;
  69. case 'S':
  70. size_required +=
  71. sizeof(char *) + sizeof(u64) +
  72. sizeof(char);
  73. tail_offset += sizeof(char *);
  74. break;
  75. default:
  76. pr_debug("Invalid package element [%d]: got number, expected [%c]\n",
  77. i, format_string[i]);
  78. return AE_BAD_DATA;
  79. }
  80. break;
  81. case ACPI_TYPE_STRING:
  82. case ACPI_TYPE_BUFFER:
  83. switch (format_string[i]) {
  84. case 'S':
  85. size_required +=
  86. sizeof(char *) +
  87. (element->string.length * sizeof(char)) +
  88. sizeof(char);
  89. tail_offset += sizeof(char *);
  90. break;
  91. case 'B':
  92. size_required +=
  93. sizeof(u8 *) + element->buffer.length;
  94. tail_offset += sizeof(u8 *);
  95. break;
  96. default:
  97. pr_debug("Invalid package element [%d] got string/buffer, expected [%c]\n",
  98. i, format_string[i]);
  99. return AE_BAD_DATA;
  100. }
  101. break;
  102. case ACPI_TYPE_LOCAL_REFERENCE:
  103. switch (format_string[i]) {
  104. case 'R':
  105. size_required += sizeof(void *);
  106. tail_offset += sizeof(void *);
  107. break;
  108. default:
  109. pr_debug("Invalid package element [%d] got reference, expected [%c]\n",
  110. i, format_string[i]);
  111. return AE_BAD_DATA;
  112. }
  113. break;
  114. case ACPI_TYPE_PACKAGE:
  115. default:
  116. pr_debug("Unsupported element at index=%d\n", i);
  117. /* TBD: handle nested packages... */
  118. return AE_SUPPORT;
  119. }
  120. }
  121. /*
  122. * Validate output buffer.
  123. */
  124. if (buffer->length == ACPI_ALLOCATE_BUFFER) {
  125. buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required);
  126. if (!buffer->pointer)
  127. return AE_NO_MEMORY;
  128. buffer->length = size_required;
  129. } else {
  130. if (buffer->length < size_required) {
  131. buffer->length = size_required;
  132. return AE_BUFFER_OVERFLOW;
  133. } else if (buffer->length != size_required ||
  134. !buffer->pointer) {
  135. return AE_BAD_PARAMETER;
  136. }
  137. }
  138. head = buffer->pointer;
  139. tail = buffer->pointer + tail_offset;
  140. /*
  141. * Extract package data.
  142. */
  143. for (i = 0; i < format_count; i++) {
  144. u8 **pointer = NULL;
  145. union acpi_object *element = &(package->package.elements[i]);
  146. switch (element->type) {
  147. case ACPI_TYPE_INTEGER:
  148. switch (format_string[i]) {
  149. case 'N':
  150. *((u64 *) head) =
  151. element->integer.value;
  152. head += sizeof(u64);
  153. break;
  154. case 'S':
  155. pointer = (u8 **) head;
  156. *pointer = tail;
  157. *((u64 *) tail) =
  158. element->integer.value;
  159. head += sizeof(u64 *);
  160. tail += sizeof(u64);
  161. /* NULL terminate string */
  162. *tail = (char)0;
  163. tail += sizeof(char);
  164. break;
  165. default:
  166. /* Should never get here */
  167. break;
  168. }
  169. break;
  170. case ACPI_TYPE_STRING:
  171. case ACPI_TYPE_BUFFER:
  172. switch (format_string[i]) {
  173. case 'S':
  174. pointer = (u8 **) head;
  175. *pointer = tail;
  176. memcpy(tail, element->string.pointer,
  177. element->string.length);
  178. head += sizeof(char *);
  179. tail += element->string.length * sizeof(char);
  180. /* NULL terminate string */
  181. *tail = (char)0;
  182. tail += sizeof(char);
  183. break;
  184. case 'B':
  185. pointer = (u8 **) head;
  186. *pointer = tail;
  187. memcpy(tail, element->buffer.pointer,
  188. element->buffer.length);
  189. head += sizeof(u8 *);
  190. tail += element->buffer.length;
  191. break;
  192. default:
  193. /* Should never get here */
  194. break;
  195. }
  196. break;
  197. case ACPI_TYPE_LOCAL_REFERENCE:
  198. switch (format_string[i]) {
  199. case 'R':
  200. *(void **)head =
  201. (void *)element->reference.handle;
  202. head += sizeof(void *);
  203. break;
  204. default:
  205. /* Should never get here */
  206. break;
  207. }
  208. break;
  209. case ACPI_TYPE_PACKAGE:
  210. /* TBD: handle nested packages... */
  211. default:
  212. /* Should never get here */
  213. break;
  214. }
  215. }
  216. return AE_OK;
  217. }
  218. EXPORT_SYMBOL(acpi_extract_package);
  219. acpi_status
  220. acpi_evaluate_integer(acpi_handle handle,
  221. acpi_string pathname,
  222. struct acpi_object_list *arguments, unsigned long long *data)
  223. {
  224. acpi_status status = AE_OK;
  225. union acpi_object element;
  226. struct acpi_buffer buffer = { 0, NULL };
  227. if (!data)
  228. return AE_BAD_PARAMETER;
  229. buffer.length = sizeof(union acpi_object);
  230. buffer.pointer = &element;
  231. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  232. if (ACPI_FAILURE(status)) {
  233. acpi_util_eval_error(handle, pathname, status);
  234. return status;
  235. }
  236. if (element.type != ACPI_TYPE_INTEGER) {
  237. acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
  238. return AE_BAD_DATA;
  239. }
  240. *data = element.integer.value;
  241. acpi_handle_debug(handle, "Return value [%llu]\n", *data);
  242. return AE_OK;
  243. }
  244. EXPORT_SYMBOL(acpi_evaluate_integer);
  245. int acpi_get_local_u64_address(acpi_handle handle, u64 *addr)
  246. {
  247. acpi_status status;
  248. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, addr);
  249. if (ACPI_FAILURE(status))
  250. return -ENODATA;
  251. return 0;
  252. }
  253. EXPORT_SYMBOL(acpi_get_local_u64_address);
  254. int acpi_get_local_address(acpi_handle handle, u32 *addr)
  255. {
  256. u64 adr;
  257. int ret;
  258. ret = acpi_get_local_u64_address(handle, &adr);
  259. if (ret < 0)
  260. return ret;
  261. *addr = (u32)adr;
  262. return 0;
  263. }
  264. EXPORT_SYMBOL(acpi_get_local_address);
  265. #define ACPI_MAX_SUB_BUF_SIZE 9
  266. const char *acpi_get_subsystem_id(acpi_handle handle)
  267. {
  268. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  269. union acpi_object *obj;
  270. acpi_status status;
  271. const char *sub;
  272. size_t len;
  273. status = acpi_evaluate_object(handle, METHOD_NAME__SUB, NULL, &buffer);
  274. if (ACPI_FAILURE(status)) {
  275. acpi_handle_debug(handle, "Reading ACPI _SUB failed: %#x\n", status);
  276. return ERR_PTR(-ENODATA);
  277. }
  278. obj = buffer.pointer;
  279. if (obj->type == ACPI_TYPE_STRING) {
  280. len = strlen(obj->string.pointer);
  281. if (len < ACPI_MAX_SUB_BUF_SIZE && len > 0) {
  282. sub = kstrdup(obj->string.pointer, GFP_KERNEL);
  283. if (!sub)
  284. sub = ERR_PTR(-ENOMEM);
  285. } else {
  286. acpi_handle_err(handle, "ACPI _SUB Length %zu is Invalid\n", len);
  287. sub = ERR_PTR(-ENODATA);
  288. }
  289. } else {
  290. acpi_handle_warn(handle, "Warning ACPI _SUB did not return a string\n");
  291. sub = ERR_PTR(-ENODATA);
  292. }
  293. acpi_os_free(buffer.pointer);
  294. return sub;
  295. }
  296. EXPORT_SYMBOL_GPL(acpi_get_subsystem_id);
  297. bool acpi_evaluate_reference(acpi_handle handle, acpi_string pathname,
  298. struct acpi_object_list *arguments,
  299. struct acpi_handle_list *list)
  300. {
  301. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  302. union acpi_object *package;
  303. acpi_status status;
  304. bool ret = false;
  305. u32 i;
  306. if (!list)
  307. return false;
  308. /* Evaluate object. */
  309. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  310. if (ACPI_FAILURE(status))
  311. goto end;
  312. package = buffer.pointer;
  313. if (buffer.length == 0 || !package ||
  314. package->type != ACPI_TYPE_PACKAGE || !package->package.count)
  315. goto err;
  316. list->count = package->package.count;
  317. list->handles = kzalloc_objs(*list->handles, list->count);
  318. if (!list->handles)
  319. goto err_clear;
  320. /* Extract package data. */
  321. for (i = 0; i < list->count; i++) {
  322. union acpi_object *element = &(package->package.elements[i]);
  323. if (element->type != ACPI_TYPE_LOCAL_REFERENCE ||
  324. !element->reference.handle)
  325. goto err_free;
  326. /* Get the acpi_handle. */
  327. list->handles[i] = element->reference.handle;
  328. acpi_handle_debug(list->handles[i], "Found in reference list\n");
  329. }
  330. ret = true;
  331. end:
  332. kfree(buffer.pointer);
  333. return ret;
  334. err_free:
  335. kfree(list->handles);
  336. list->handles = NULL;
  337. err_clear:
  338. list->count = 0;
  339. err:
  340. acpi_util_eval_error(handle, pathname, status);
  341. goto end;
  342. }
  343. EXPORT_SYMBOL(acpi_evaluate_reference);
  344. /**
  345. * acpi_handle_list_equal - Check if two ACPI handle lists are the same
  346. * @list1: First list to compare.
  347. * @list2: Second list to compare.
  348. *
  349. * Return true if the given ACPI handle lists are of the same size and
  350. * contain the same ACPI handles in the same order. Otherwise, return false.
  351. */
  352. bool acpi_handle_list_equal(struct acpi_handle_list *list1,
  353. struct acpi_handle_list *list2)
  354. {
  355. return list1->count == list2->count &&
  356. !memcmp(list1->handles, list2->handles,
  357. list1->count * sizeof(*list1->handles));
  358. }
  359. EXPORT_SYMBOL_GPL(acpi_handle_list_equal);
  360. /**
  361. * acpi_handle_list_replace - Replace one ACPI handle list with another
  362. * @dst: ACPI handle list to replace.
  363. * @src: Source ACPI handle list.
  364. *
  365. * Free the handles table in @dst, move the handles table from @src to @dst,
  366. * copy count from @src to @dst and clear @src.
  367. */
  368. void acpi_handle_list_replace(struct acpi_handle_list *dst,
  369. struct acpi_handle_list *src)
  370. {
  371. if (dst->count)
  372. kfree(dst->handles);
  373. dst->count = src->count;
  374. dst->handles = src->handles;
  375. src->handles = NULL;
  376. src->count = 0;
  377. }
  378. EXPORT_SYMBOL_GPL(acpi_handle_list_replace);
  379. /**
  380. * acpi_handle_list_free - Free the handles table in an ACPI handle list
  381. * @list: ACPI handle list to free.
  382. *
  383. * Free the handles table in @list and clear its count field.
  384. */
  385. void acpi_handle_list_free(struct acpi_handle_list *list)
  386. {
  387. if (!list->count)
  388. return;
  389. kfree(list->handles);
  390. list->count = 0;
  391. }
  392. EXPORT_SYMBOL_GPL(acpi_handle_list_free);
  393. /**
  394. * acpi_device_dep - Check ACPI device dependency
  395. * @target: ACPI handle of the target ACPI device.
  396. * @match: ACPI handle to look up in the target's _DEP list.
  397. *
  398. * Return true if @match is present in the list returned by _DEP for
  399. * @target or false otherwise.
  400. */
  401. bool acpi_device_dep(acpi_handle target, acpi_handle match)
  402. {
  403. struct acpi_handle_list dep_devices;
  404. bool ret = false;
  405. int i;
  406. if (!acpi_has_method(target, "_DEP"))
  407. return false;
  408. if (!acpi_evaluate_reference(target, "_DEP", NULL, &dep_devices)) {
  409. acpi_handle_debug(target, "Failed to evaluate _DEP.\n");
  410. return false;
  411. }
  412. for (i = 0; i < dep_devices.count; i++) {
  413. if (dep_devices.handles[i] == match) {
  414. ret = true;
  415. break;
  416. }
  417. }
  418. acpi_handle_list_free(&dep_devices);
  419. return ret;
  420. }
  421. EXPORT_SYMBOL_GPL(acpi_device_dep);
  422. bool
  423. acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
  424. {
  425. acpi_status status;
  426. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  427. union acpi_object *output;
  428. status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
  429. if (ACPI_FAILURE(status))
  430. return false;
  431. output = buffer.pointer;
  432. if (!output || output->type != ACPI_TYPE_PACKAGE
  433. || !output->package.count
  434. || output->package.elements[0].type != ACPI_TYPE_BUFFER
  435. || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
  436. status = AE_TYPE;
  437. goto out;
  438. }
  439. status = acpi_decode_pld_buffer(
  440. output->package.elements[0].buffer.pointer,
  441. output->package.elements[0].buffer.length,
  442. pld);
  443. out:
  444. kfree(buffer.pointer);
  445. return ACPI_SUCCESS(status);
  446. }
  447. EXPORT_SYMBOL(acpi_get_physical_device_location);
  448. /**
  449. * acpi_evaluate_ost: Evaluate _OST for hotplug operations
  450. * @handle: ACPI device handle
  451. * @source_event: source event code
  452. * @status_code: status code
  453. * @status_buf: optional detailed information (NULL if none)
  454. *
  455. * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
  456. * must call this function when evaluating _OST for hotplug operations.
  457. * When the platform does not support _OST, this function has no effect.
  458. */
  459. acpi_status
  460. acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
  461. struct acpi_buffer *status_buf)
  462. {
  463. union acpi_object params[3] = {
  464. {.type = ACPI_TYPE_INTEGER,},
  465. {.type = ACPI_TYPE_INTEGER,},
  466. {.type = ACPI_TYPE_BUFFER,}
  467. };
  468. struct acpi_object_list arg_list = {3, params};
  469. params[0].integer.value = source_event;
  470. params[1].integer.value = status_code;
  471. if (status_buf != NULL) {
  472. params[2].buffer.pointer = status_buf->pointer;
  473. params[2].buffer.length = status_buf->length;
  474. } else {
  475. params[2].buffer.pointer = NULL;
  476. params[2].buffer.length = 0;
  477. }
  478. return acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
  479. }
  480. EXPORT_SYMBOL(acpi_evaluate_ost);
  481. /**
  482. * acpi_handle_path: Return the object path of handle
  483. * @handle: ACPI device handle
  484. *
  485. * Caller must free the returned buffer
  486. */
  487. char *acpi_handle_path(acpi_handle handle)
  488. {
  489. struct acpi_buffer buffer = {
  490. .length = ACPI_ALLOCATE_BUFFER,
  491. .pointer = NULL
  492. };
  493. if (in_interrupt() ||
  494. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
  495. return NULL;
  496. return buffer.pointer;
  497. }
  498. /**
  499. * acpi_handle_printk: Print message with ACPI prefix and object path
  500. * @level: log level
  501. * @handle: ACPI device handle
  502. * @fmt: format string
  503. *
  504. * This function is called through acpi_handle_<level> macros and prints
  505. * a message with ACPI prefix and object path. This function acquires
  506. * the global namespace mutex to obtain an object path. In interrupt
  507. * context, it shows the object path as <n/a>.
  508. */
  509. void
  510. acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
  511. {
  512. struct va_format vaf;
  513. va_list args;
  514. const char *path;
  515. va_start(args, fmt);
  516. vaf.fmt = fmt;
  517. vaf.va = &args;
  518. path = acpi_handle_path(handle);
  519. printk("%sACPI: %s: %pV", level, path ? path : "<n/a>", &vaf);
  520. va_end(args);
  521. kfree(path);
  522. }
  523. EXPORT_SYMBOL(acpi_handle_printk);
  524. #if defined(CONFIG_DYNAMIC_DEBUG)
  525. /**
  526. * __acpi_handle_debug: pr_debug with ACPI prefix and object path
  527. * @descriptor: Dynamic Debug descriptor
  528. * @handle: ACPI device handle
  529. * @fmt: format string
  530. *
  531. * This function is called through acpi_handle_debug macro and debug
  532. * prints a message with ACPI prefix and object path. This function
  533. * acquires the global namespace mutex to obtain an object path. In
  534. * interrupt context, it shows the object path as <n/a>.
  535. */
  536. void
  537. __acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle,
  538. const char *fmt, ...)
  539. {
  540. struct va_format vaf;
  541. va_list args;
  542. const char *path;
  543. va_start(args, fmt);
  544. vaf.fmt = fmt;
  545. vaf.va = &args;
  546. path = acpi_handle_path(handle);
  547. __dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf);
  548. va_end(args);
  549. kfree(path);
  550. }
  551. EXPORT_SYMBOL(__acpi_handle_debug);
  552. #endif
  553. /**
  554. * acpi_evaluation_failure_warn - Log evaluation failure warning.
  555. * @handle: Parent object handle.
  556. * @name: Name of the object whose evaluation has failed.
  557. * @status: Status value returned by the failing object evaluation.
  558. */
  559. void acpi_evaluation_failure_warn(acpi_handle handle, const char *name,
  560. acpi_status status)
  561. {
  562. acpi_handle_warn(handle, "%s evaluation failed: %s\n", name,
  563. acpi_format_exception(status));
  564. }
  565. EXPORT_SYMBOL_GPL(acpi_evaluation_failure_warn);
  566. /**
  567. * acpi_has_method: Check whether @handle has a method named @name
  568. * @handle: ACPI device handle
  569. * @name: name of object or method
  570. *
  571. * Check whether @handle has a method named @name.
  572. */
  573. bool acpi_has_method(acpi_handle handle, char *name)
  574. {
  575. acpi_handle tmp;
  576. return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
  577. }
  578. EXPORT_SYMBOL(acpi_has_method);
  579. acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
  580. u64 arg)
  581. {
  582. union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
  583. struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
  584. obj.integer.value = arg;
  585. return acpi_evaluate_object(handle, method, &arg_list, NULL);
  586. }
  587. EXPORT_SYMBOL(acpi_execute_simple_method);
  588. /**
  589. * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
  590. * @handle: ACPI device handle
  591. *
  592. * Evaluate device's _EJ0 method for hotplug operations.
  593. */
  594. acpi_status acpi_evaluate_ej0(acpi_handle handle)
  595. {
  596. acpi_status status;
  597. status = acpi_execute_simple_method(handle, "_EJ0", 1);
  598. if (status == AE_NOT_FOUND)
  599. acpi_handle_warn(handle, "No _EJ0 support for device\n");
  600. else if (ACPI_FAILURE(status))
  601. acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
  602. return status;
  603. }
  604. /**
  605. * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
  606. * @handle: ACPI device handle
  607. * @lock: lock device if non-zero, otherwise unlock device
  608. *
  609. * Evaluate device's _LCK method if present to lock/unlock device
  610. */
  611. acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
  612. {
  613. acpi_status status;
  614. status = acpi_execute_simple_method(handle, "_LCK", !!lock);
  615. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  616. if (lock)
  617. acpi_handle_warn(handle,
  618. "Locking device failed (0x%x)\n", status);
  619. else
  620. acpi_handle_warn(handle,
  621. "Unlocking device failed (0x%x)\n", status);
  622. }
  623. return status;
  624. }
  625. /**
  626. * acpi_evaluate_reg: Evaluate _REG method to register OpRegion presence
  627. * @handle: ACPI device handle
  628. * @space_id: ACPI address space id to register OpRegion presence for
  629. * @function: Parameter to pass to _REG one of ACPI_REG_CONNECT or
  630. * ACPI_REG_DISCONNECT
  631. *
  632. * Evaluate device's _REG method to register OpRegion presence.
  633. */
  634. acpi_status acpi_evaluate_reg(acpi_handle handle, u8 space_id, u32 function)
  635. {
  636. struct acpi_object_list arg_list;
  637. union acpi_object params[2];
  638. params[0].type = ACPI_TYPE_INTEGER;
  639. params[0].integer.value = space_id;
  640. params[1].type = ACPI_TYPE_INTEGER;
  641. params[1].integer.value = function;
  642. arg_list.count = 2;
  643. arg_list.pointer = params;
  644. return acpi_evaluate_object(handle, "_REG", &arg_list, NULL);
  645. }
  646. EXPORT_SYMBOL(acpi_evaluate_reg);
  647. /**
  648. * acpi_evaluate_dsm - evaluate device's _DSM method
  649. * @handle: ACPI device handle
  650. * @guid: GUID of requested functions, should be 16 bytes
  651. * @rev: revision number of requested function
  652. * @func: requested function number
  653. * @argv4: the function specific parameter
  654. *
  655. * Evaluate device's _DSM method with specified GUID, revision id and
  656. * function number. Caller needs to free the returned object.
  657. *
  658. * Though ACPI defines the fourth parameter for _DSM should be a package,
  659. * some old BIOSes do expect a buffer or an integer etc.
  660. */
  661. union acpi_object *
  662. acpi_evaluate_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 func,
  663. union acpi_object *argv4)
  664. {
  665. acpi_status ret;
  666. struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
  667. union acpi_object params[4];
  668. struct acpi_object_list input = {
  669. .count = 4,
  670. .pointer = params,
  671. };
  672. params[0].type = ACPI_TYPE_BUFFER;
  673. params[0].buffer.length = 16;
  674. params[0].buffer.pointer = (u8 *)guid;
  675. params[1].type = ACPI_TYPE_INTEGER;
  676. params[1].integer.value = rev;
  677. params[2].type = ACPI_TYPE_INTEGER;
  678. params[2].integer.value = func;
  679. if (argv4) {
  680. params[3] = *argv4;
  681. } else {
  682. params[3].type = ACPI_TYPE_PACKAGE;
  683. params[3].package.count = 0;
  684. params[3].package.elements = NULL;
  685. }
  686. ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
  687. if (ACPI_SUCCESS(ret))
  688. return (union acpi_object *)buf.pointer;
  689. if (ret != AE_NOT_FOUND)
  690. acpi_handle_warn(handle,
  691. "failed to evaluate _DSM %pUb rev:%lld func:%lld (0x%x)\n",
  692. guid, rev, func, ret);
  693. return NULL;
  694. }
  695. EXPORT_SYMBOL(acpi_evaluate_dsm);
  696. /**
  697. * acpi_check_dsm - check if _DSM method supports requested functions.
  698. * @handle: ACPI device handle
  699. * @guid: GUID of requested functions, should be 16 bytes at least
  700. * @rev: revision number of requested functions
  701. * @funcs: bitmap of requested functions
  702. *
  703. * Evaluate device's _DSM method to check whether it supports requested
  704. * functions. Currently only support 64 functions at maximum, should be
  705. * enough for now.
  706. */
  707. bool acpi_check_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 funcs)
  708. {
  709. int i;
  710. u64 mask = 0;
  711. union acpi_object *obj;
  712. if (funcs == 0)
  713. return false;
  714. obj = acpi_evaluate_dsm(handle, guid, rev, 0, NULL);
  715. if (!obj)
  716. return false;
  717. /* For compatibility, old BIOSes may return an integer */
  718. if (obj->type == ACPI_TYPE_INTEGER)
  719. mask = obj->integer.value;
  720. else if (obj->type == ACPI_TYPE_BUFFER)
  721. for (i = 0; i < obj->buffer.length && i < 8; i++)
  722. mask |= (((u64)obj->buffer.pointer[i]) << (i * 8));
  723. ACPI_FREE(obj);
  724. /*
  725. * Bit 0 indicates whether there's support for any functions other than
  726. * function 0 for the specified GUID and revision.
  727. */
  728. if ((mask & 0x1) && (mask & funcs) == funcs)
  729. return true;
  730. return false;
  731. }
  732. EXPORT_SYMBOL(acpi_check_dsm);
  733. /**
  734. * acpi_dev_uid_to_integer - treat ACPI device _UID as integer
  735. * @adev: ACPI device to get _UID from
  736. * @integer: output buffer for integer
  737. *
  738. * Considers _UID as integer and converts it to @integer.
  739. *
  740. * Returns 0 on success, or negative error code otherwise.
  741. */
  742. int acpi_dev_uid_to_integer(struct acpi_device *adev, u64 *integer)
  743. {
  744. const char *uid;
  745. if (!adev)
  746. return -ENODEV;
  747. uid = acpi_device_uid(adev);
  748. if (!uid)
  749. return -ENODATA;
  750. return kstrtou64(uid, 0, integer);
  751. }
  752. EXPORT_SYMBOL(acpi_dev_uid_to_integer);
  753. /**
  754. * acpi_dev_found - Detect presence of a given ACPI device in the namespace.
  755. * @hid: Hardware ID of the device.
  756. *
  757. * Return %true if the device was present at the moment of invocation.
  758. * Note that if the device is pluggable, it may since have disappeared.
  759. *
  760. * For this function to work, acpi_bus_scan() must have been executed
  761. * which happens in the subsys_initcall() subsection. Hence, do not
  762. * call from a subsys_initcall() or earlier (use acpi_get_devices()
  763. * instead). Calling from module_init() is fine (which is synonymous
  764. * with device_initcall()).
  765. */
  766. bool acpi_dev_found(const char *hid)
  767. {
  768. struct acpi_device_bus_id *acpi_device_bus_id;
  769. bool found = false;
  770. mutex_lock(&acpi_device_lock);
  771. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
  772. if (!strcmp(acpi_device_bus_id->bus_id, hid)) {
  773. found = true;
  774. break;
  775. }
  776. mutex_unlock(&acpi_device_lock);
  777. return found;
  778. }
  779. EXPORT_SYMBOL(acpi_dev_found);
  780. struct acpi_dev_match_info {
  781. struct acpi_device_id hid[2];
  782. const char *uid;
  783. s64 hrv;
  784. };
  785. static int acpi_dev_match_cb(struct device *dev, const void *data)
  786. {
  787. struct acpi_device *adev = to_acpi_device(dev);
  788. const struct acpi_dev_match_info *match = data;
  789. unsigned long long hrv;
  790. acpi_status status;
  791. if (acpi_match_device_ids(adev, match->hid))
  792. return 0;
  793. if (match->uid && !acpi_dev_uid_match(adev, match->uid))
  794. return 0;
  795. if (match->hrv == -1)
  796. return 1;
  797. status = acpi_evaluate_integer(adev->handle, "_HRV", NULL, &hrv);
  798. if (ACPI_FAILURE(status))
  799. return 0;
  800. return hrv == match->hrv;
  801. }
  802. /**
  803. * acpi_dev_present - Detect that a given ACPI device is present
  804. * @hid: Hardware ID of the device.
  805. * @uid: Unique ID of the device, pass NULL to not check _UID
  806. * @hrv: Hardware Revision of the device, pass -1 to not check _HRV
  807. *
  808. * Return %true if a matching device was present at the moment of invocation.
  809. * Note that if the device is pluggable, it may since have disappeared.
  810. *
  811. * Note that unlike acpi_dev_found() this function checks the status
  812. * of the device. So for devices which are present in the DSDT, but
  813. * which are disabled (their _STA callback returns 0) this function
  814. * will return false.
  815. *
  816. * For this function to work, acpi_bus_scan() must have been executed
  817. * which happens in the subsys_initcall() subsection. Hence, do not
  818. * call from a subsys_initcall() or earlier (use acpi_get_devices()
  819. * instead). Calling from module_init() is fine (which is synonymous
  820. * with device_initcall()).
  821. */
  822. bool acpi_dev_present(const char *hid, const char *uid, s64 hrv)
  823. {
  824. struct acpi_dev_match_info match = {};
  825. struct device *dev;
  826. strscpy(match.hid[0].id, hid, sizeof(match.hid[0].id));
  827. match.uid = uid;
  828. match.hrv = hrv;
  829. dev = bus_find_device(&acpi_bus_type, NULL, &match, acpi_dev_match_cb);
  830. put_device(dev);
  831. return !!dev;
  832. }
  833. EXPORT_SYMBOL(acpi_dev_present);
  834. /**
  835. * acpi_dev_get_next_match_dev - Return the next match of ACPI device
  836. * @adev: Pointer to the previous ACPI device matching this @hid, @uid and @hrv
  837. * @hid: Hardware ID of the device.
  838. * @uid: Unique ID of the device, pass NULL to not check _UID
  839. * @hrv: Hardware Revision of the device, pass -1 to not check _HRV
  840. *
  841. * Return the next match of ACPI device if another matching device was present
  842. * at the moment of invocation, or NULL otherwise.
  843. *
  844. * The caller is responsible for invoking acpi_dev_put() on the returned device.
  845. * On the other hand the function invokes acpi_dev_put() on the given @adev
  846. * assuming that its reference counter had been increased beforehand.
  847. *
  848. * See additional information in acpi_dev_present() as well.
  849. */
  850. struct acpi_device *
  851. acpi_dev_get_next_match_dev(struct acpi_device *adev, const char *hid, const char *uid, s64 hrv)
  852. {
  853. struct device *start = adev ? &adev->dev : NULL;
  854. struct acpi_dev_match_info match = {};
  855. struct device *dev;
  856. strscpy(match.hid[0].id, hid, sizeof(match.hid[0].id));
  857. match.uid = uid;
  858. match.hrv = hrv;
  859. dev = bus_find_device(&acpi_bus_type, start, &match, acpi_dev_match_cb);
  860. acpi_dev_put(adev);
  861. return dev ? to_acpi_device(dev) : NULL;
  862. }
  863. EXPORT_SYMBOL(acpi_dev_get_next_match_dev);
  864. /**
  865. * acpi_dev_get_first_match_dev - Return the first match of ACPI device
  866. * @hid: Hardware ID of the device.
  867. * @uid: Unique ID of the device, pass NULL to not check _UID
  868. * @hrv: Hardware Revision of the device, pass -1 to not check _HRV
  869. *
  870. * Return the first match of ACPI device if a matching device was present
  871. * at the moment of invocation, or NULL otherwise.
  872. *
  873. * The caller is responsible for invoking acpi_dev_put() on the returned device.
  874. *
  875. * See additional information in acpi_dev_present() as well.
  876. */
  877. struct acpi_device *
  878. acpi_dev_get_first_match_dev(const char *hid, const char *uid, s64 hrv)
  879. {
  880. return acpi_dev_get_next_match_dev(NULL, hid, uid, hrv);
  881. }
  882. EXPORT_SYMBOL(acpi_dev_get_first_match_dev);
  883. /**
  884. * acpi_reduced_hardware - Return if this is an ACPI-reduced-hw machine
  885. *
  886. * Return true when running on an ACPI-reduced-hw machine, false otherwise.
  887. */
  888. bool acpi_reduced_hardware(void)
  889. {
  890. return acpi_gbl_reduced_hardware;
  891. }
  892. EXPORT_SYMBOL_GPL(acpi_reduced_hardware);
  893. /*
  894. * acpi_backlight= handling, this is done here rather then in video_detect.c
  895. * because __setup cannot be used in modules.
  896. */
  897. char acpi_video_backlight_string[16];
  898. EXPORT_SYMBOL(acpi_video_backlight_string);
  899. static int __init acpi_backlight(char *str)
  900. {
  901. strscpy(acpi_video_backlight_string, str,
  902. sizeof(acpi_video_backlight_string));
  903. return 1;
  904. }
  905. __setup("acpi_backlight=", acpi_backlight);
  906. /**
  907. * acpi_match_platform_list - Check if the system matches with a given list
  908. * @plat: pointer to acpi_platform_list table terminated by a NULL entry
  909. *
  910. * Return the matched index if the system is found in the platform list.
  911. * Otherwise, return a negative error code.
  912. */
  913. int acpi_match_platform_list(const struct acpi_platform_list *plat)
  914. {
  915. struct acpi_table_header hdr;
  916. int idx = 0;
  917. if (acpi_disabled)
  918. return -ENODEV;
  919. for (; plat->oem_id[0]; plat++, idx++) {
  920. if (ACPI_FAILURE(acpi_get_table_header(plat->table, 0, &hdr)))
  921. continue;
  922. if (strncmp(plat->oem_id, hdr.oem_id, ACPI_OEM_ID_SIZE))
  923. continue;
  924. if (strncmp(plat->oem_table_id, hdr.oem_table_id, ACPI_OEM_TABLE_ID_SIZE))
  925. continue;
  926. if ((plat->pred == all_versions) ||
  927. (plat->pred == less_than_or_equal && hdr.oem_revision <= plat->oem_revision) ||
  928. (plat->pred == greater_than_or_equal && hdr.oem_revision >= plat->oem_revision) ||
  929. (plat->pred == equal && hdr.oem_revision == plat->oem_revision))
  930. return idx;
  931. }
  932. return -ENODEV;
  933. }
  934. EXPORT_SYMBOL(acpi_match_platform_list);