utils.c 24 KB

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