/drivers/acpi/utils.c

http://github.com/mirrors/linux · C · 901 lines · 592 code · 123 blank · 186 comment · 109 complexity · c2ed8a57f46d530877444b2fcd881fde MD5 · raw file

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