utils.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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 <[email protected]>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <[email protected]>
  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_address(acpi_handle handle, u32 *addr)
  246. {
  247. unsigned long long adr;
  248. acpi_status status;
  249. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
  250. if (ACPI_FAILURE(status))
  251. return -ENODATA;
  252. *addr = (u32)adr;
  253. return 0;
  254. }
  255. EXPORT_SYMBOL(acpi_get_local_address);
  256. #define ACPI_MAX_SUB_BUF_SIZE 9
  257. const char *acpi_get_subsystem_id(acpi_handle handle)
  258. {
  259. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  260. union acpi_object *obj;
  261. acpi_status status;
  262. const char *sub;
  263. size_t len;
  264. status = acpi_evaluate_object(handle, METHOD_NAME__SUB, NULL, &buffer);
  265. if (ACPI_FAILURE(status)) {
  266. acpi_handle_debug(handle, "Reading ACPI _SUB failed: %#x\n", status);
  267. return ERR_PTR(-ENODATA);
  268. }
  269. obj = buffer.pointer;
  270. if (obj->type == ACPI_TYPE_STRING) {
  271. len = strlen(obj->string.pointer);
  272. if (len < ACPI_MAX_SUB_BUF_SIZE && len > 0) {
  273. sub = kstrdup(obj->string.pointer, GFP_KERNEL);
  274. if (!sub)
  275. sub = ERR_PTR(-ENOMEM);
  276. } else {
  277. acpi_handle_err(handle, "ACPI _SUB Length %zu is Invalid\n", len);
  278. sub = ERR_PTR(-ENODATA);
  279. }
  280. } else {
  281. acpi_handle_warn(handle, "Warning ACPI _SUB did not return a string\n");
  282. sub = ERR_PTR(-ENODATA);
  283. }
  284. acpi_os_free(buffer.pointer);
  285. return sub;
  286. }
  287. EXPORT_SYMBOL_GPL(acpi_get_subsystem_id);
  288. acpi_status
  289. acpi_evaluate_reference(acpi_handle handle,
  290. acpi_string pathname,
  291. struct acpi_object_list *arguments,
  292. struct acpi_handle_list *list)
  293. {
  294. acpi_status status = AE_OK;
  295. union acpi_object *package = NULL;
  296. union acpi_object *element = NULL;
  297. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  298. u32 i = 0;
  299. if (!list) {
  300. return AE_BAD_PARAMETER;
  301. }
  302. /* Evaluate object. */
  303. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  304. if (ACPI_FAILURE(status))
  305. goto end;
  306. package = buffer.pointer;
  307. if ((buffer.length == 0) || !package) {
  308. status = AE_BAD_DATA;
  309. acpi_util_eval_error(handle, pathname, status);
  310. goto end;
  311. }
  312. if (package->type != ACPI_TYPE_PACKAGE) {
  313. status = AE_BAD_DATA;
  314. acpi_util_eval_error(handle, pathname, status);
  315. goto end;
  316. }
  317. if (!package->package.count) {
  318. status = AE_BAD_DATA;
  319. acpi_util_eval_error(handle, pathname, status);
  320. goto end;
  321. }
  322. if (package->package.count > ACPI_MAX_HANDLES) {
  323. kfree(package);
  324. return AE_NO_MEMORY;
  325. }
  326. list->count = package->package.count;
  327. /* Extract package data. */
  328. for (i = 0; i < list->count; i++) {
  329. element = &(package->package.elements[i]);
  330. if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
  331. status = AE_BAD_DATA;
  332. acpi_util_eval_error(handle, pathname, status);
  333. break;
  334. }
  335. if (!element->reference.handle) {
  336. status = AE_NULL_ENTRY;
  337. acpi_util_eval_error(handle, pathname, status);
  338. break;
  339. }
  340. /* Get the acpi_handle. */
  341. list->handles[i] = element->reference.handle;
  342. acpi_handle_debug(list->handles[i], "Found in reference list\n");
  343. }
  344. end:
  345. if (ACPI_FAILURE(status)) {
  346. list->count = 0;
  347. //kfree(list->handles);
  348. }
  349. kfree(buffer.pointer);
  350. return status;
  351. }
  352. EXPORT_SYMBOL(acpi_evaluate_reference);
  353. acpi_status
  354. acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
  355. {
  356. acpi_status status;
  357. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  358. union acpi_object *output;
  359. status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
  360. if (ACPI_FAILURE(status))
  361. return status;
  362. output = buffer.pointer;
  363. if (!output || output->type != ACPI_TYPE_PACKAGE
  364. || !output->package.count
  365. || output->package.elements[0].type != ACPI_TYPE_BUFFER
  366. || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
  367. status = AE_TYPE;
  368. goto out;
  369. }
  370. status = acpi_decode_pld_buffer(
  371. output->package.elements[0].buffer.pointer,
  372. output->package.elements[0].buffer.length,
  373. pld);
  374. out:
  375. kfree(buffer.pointer);
  376. return status;
  377. }
  378. EXPORT_SYMBOL(acpi_get_physical_device_location);
  379. /**
  380. * acpi_evaluate_ost: Evaluate _OST for hotplug operations
  381. * @handle: ACPI device handle
  382. * @source_event: source event code
  383. * @status_code: status code
  384. * @status_buf: optional detailed information (NULL if none)
  385. *
  386. * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
  387. * must call this function when evaluating _OST for hotplug operations.
  388. * When the platform does not support _OST, this function has no effect.
  389. */
  390. acpi_status
  391. acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
  392. struct acpi_buffer *status_buf)
  393. {
  394. union acpi_object params[3] = {
  395. {.type = ACPI_TYPE_INTEGER,},
  396. {.type = ACPI_TYPE_INTEGER,},
  397. {.type = ACPI_TYPE_BUFFER,}
  398. };
  399. struct acpi_object_list arg_list = {3, params};
  400. params[0].integer.value = source_event;
  401. params[1].integer.value = status_code;
  402. if (status_buf != NULL) {
  403. params[2].buffer.pointer = status_buf->pointer;
  404. params[2].buffer.length = status_buf->length;
  405. } else {
  406. params[2].buffer.pointer = NULL;
  407. params[2].buffer.length = 0;
  408. }
  409. return acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
  410. }
  411. EXPORT_SYMBOL(acpi_evaluate_ost);
  412. /**
  413. * acpi_handle_path: Return the object path of handle
  414. * @handle: ACPI device handle
  415. *
  416. * Caller must free the returned buffer
  417. */
  418. static char *acpi_handle_path(acpi_handle handle)
  419. {
  420. struct acpi_buffer buffer = {
  421. .length = ACPI_ALLOCATE_BUFFER,
  422. .pointer = NULL
  423. };
  424. if (in_interrupt() ||
  425. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
  426. return NULL;
  427. return buffer.pointer;
  428. }
  429. /**
  430. * acpi_handle_printk: Print message with ACPI prefix and object path
  431. * @level: log level
  432. * @handle: ACPI device handle
  433. * @fmt: format string
  434. *
  435. * This function is called through acpi_handle_<level> macros and prints
  436. * a message with ACPI prefix and object path. This function acquires
  437. * the global namespace mutex to obtain an object path. In interrupt
  438. * context, it shows the object path as <n/a>.
  439. */
  440. void
  441. acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
  442. {
  443. struct va_format vaf;
  444. va_list args;
  445. const char *path;
  446. va_start(args, fmt);
  447. vaf.fmt = fmt;
  448. vaf.va = &args;
  449. path = acpi_handle_path(handle);
  450. printk("%sACPI: %s: %pV", level, path ? path : "<n/a>" , &vaf);
  451. va_end(args);
  452. kfree(path);
  453. }
  454. EXPORT_SYMBOL(acpi_handle_printk);
  455. #if defined(CONFIG_DYNAMIC_DEBUG)
  456. /**
  457. * __acpi_handle_debug: pr_debug with ACPI prefix and object path
  458. * @descriptor: Dynamic Debug descriptor
  459. * @handle: ACPI device handle
  460. * @fmt: format string
  461. *
  462. * This function is called through acpi_handle_debug macro and debug
  463. * prints a message with ACPI prefix and object path. This function
  464. * acquires the global namespace mutex to obtain an object path. In
  465. * interrupt context, it shows the object path as <n/a>.
  466. */
  467. void
  468. __acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle,
  469. const char *fmt, ...)
  470. {
  471. struct va_format vaf;
  472. va_list args;
  473. const char *path;
  474. va_start(args, fmt);
  475. vaf.fmt = fmt;
  476. vaf.va = &args;
  477. path = acpi_handle_path(handle);
  478. __dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf);
  479. va_end(args);
  480. kfree(path);
  481. }
  482. EXPORT_SYMBOL(__acpi_handle_debug);
  483. #endif
  484. /**
  485. * acpi_evaluation_failure_warn - Log evaluation failure warning.
  486. * @handle: Parent object handle.
  487. * @name: Name of the object whose evaluation has failed.
  488. * @status: Status value returned by the failing object evaluation.
  489. */
  490. void acpi_evaluation_failure_warn(acpi_handle handle, const char *name,
  491. acpi_status status)
  492. {
  493. acpi_handle_warn(handle, "%s evaluation failed: %s\n", name,
  494. acpi_format_exception(status));
  495. }
  496. EXPORT_SYMBOL_GPL(acpi_evaluation_failure_warn);
  497. /**
  498. * acpi_has_method: Check whether @handle has a method named @name
  499. * @handle: ACPI device handle
  500. * @name: name of object or method
  501. *
  502. * Check whether @handle has a method named @name.
  503. */
  504. bool acpi_has_method(acpi_handle handle, char *name)
  505. {
  506. acpi_handle tmp;
  507. return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
  508. }
  509. EXPORT_SYMBOL(acpi_has_method);
  510. acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
  511. u64 arg)
  512. {
  513. union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
  514. struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
  515. obj.integer.value = arg;
  516. return acpi_evaluate_object(handle, method, &arg_list, NULL);
  517. }
  518. EXPORT_SYMBOL(acpi_execute_simple_method);
  519. /**
  520. * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
  521. * @handle: ACPI device handle
  522. *
  523. * Evaluate device's _EJ0 method for hotplug operations.
  524. */
  525. acpi_status acpi_evaluate_ej0(acpi_handle handle)
  526. {
  527. acpi_status status;
  528. status = acpi_execute_simple_method(handle, "_EJ0", 1);
  529. if (status == AE_NOT_FOUND)
  530. acpi_handle_warn(handle, "No _EJ0 support for device\n");
  531. else if (ACPI_FAILURE(status))
  532. acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
  533. return status;
  534. }
  535. /**
  536. * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
  537. * @handle: ACPI device handle
  538. * @lock: lock device if non-zero, otherwise unlock device
  539. *
  540. * Evaluate device's _LCK method if present to lock/unlock device
  541. */
  542. acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
  543. {
  544. acpi_status status;
  545. status = acpi_execute_simple_method(handle, "_LCK", !!lock);
  546. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  547. if (lock)
  548. acpi_handle_warn(handle,
  549. "Locking device failed (0x%x)\n", status);
  550. else
  551. acpi_handle_warn(handle,
  552. "Unlocking device failed (0x%x)\n", status);
  553. }
  554. return status;
  555. }
  556. /**
  557. * acpi_evaluate_reg: Evaluate _REG method to register OpRegion presence
  558. * @handle: ACPI device handle
  559. * @space_id: ACPI address space id to register OpRegion presence for
  560. * @function: Parameter to pass to _REG one of ACPI_REG_CONNECT or
  561. * ACPI_REG_DISCONNECT
  562. *
  563. * Evaluate device's _REG method to register OpRegion presence.
  564. */
  565. acpi_status acpi_evaluate_reg(acpi_handle handle, u8 space_id, u32 function)
  566. {
  567. struct acpi_object_list arg_list;
  568. union acpi_object params[2];
  569. params[0].type = ACPI_TYPE_INTEGER;
  570. params[0].integer.value = space_id;
  571. params[1].type = ACPI_TYPE_INTEGER;
  572. params[1].integer.value = function;
  573. arg_list.count = 2;
  574. arg_list.pointer = params;
  575. return acpi_evaluate_object(handle, "_REG", &arg_list, NULL);
  576. }
  577. EXPORT_SYMBOL(acpi_evaluate_reg);
  578. /**
  579. * acpi_evaluate_dsm - evaluate device's _DSM method
  580. * @handle: ACPI device handle
  581. * @guid: GUID of requested functions, should be 16 bytes
  582. * @rev: revision number of requested function
  583. * @func: requested function number
  584. * @argv4: the function specific parameter
  585. *
  586. * Evaluate device's _DSM method with specified GUID, revision id and
  587. * function number. Caller needs to free the returned object.
  588. *
  589. * Though ACPI defines the fourth parameter for _DSM should be a package,
  590. * some old BIOSes do expect a buffer or an integer etc.
  591. */
  592. union acpi_object *
  593. acpi_evaluate_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 func,
  594. union acpi_object *argv4)
  595. {
  596. acpi_status ret;
  597. struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
  598. union acpi_object params[4];
  599. struct acpi_object_list input = {
  600. .count = 4,
  601. .pointer = params,
  602. };
  603. params[0].type = ACPI_TYPE_BUFFER;
  604. params[0].buffer.length = 16;
  605. params[0].buffer.pointer = (u8 *)guid;
  606. params[1].type = ACPI_TYPE_INTEGER;
  607. params[1].integer.value = rev;
  608. params[2].type = ACPI_TYPE_INTEGER;
  609. params[2].integer.value = func;
  610. if (argv4) {
  611. params[3] = *argv4;
  612. } else {
  613. params[3].type = ACPI_TYPE_PACKAGE;
  614. params[3].package.count = 0;
  615. params[3].package.elements = NULL;
  616. }
  617. ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
  618. if (ACPI_SUCCESS(ret))
  619. return (union acpi_object *)buf.pointer;
  620. if (ret != AE_NOT_FOUND)
  621. acpi_handle_warn(handle,
  622. "failed to evaluate _DSM %pUb (0x%x)\n", guid, ret);
  623. return NULL;
  624. }
  625. EXPORT_SYMBOL(acpi_evaluate_dsm);
  626. /**
  627. * acpi_check_dsm - check if _DSM method supports requested functions.
  628. * @handle: ACPI device handle
  629. * @guid: GUID of requested functions, should be 16 bytes at least
  630. * @rev: revision number of requested functions
  631. * @funcs: bitmap of requested functions
  632. *
  633. * Evaluate device's _DSM method to check whether it supports requested
  634. * functions. Currently only support 64 functions at maximum, should be
  635. * enough for now.
  636. */
  637. bool acpi_check_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 funcs)
  638. {
  639. int i;
  640. u64 mask = 0;
  641. union acpi_object *obj;
  642. if (funcs == 0)
  643. return false;
  644. obj = acpi_evaluate_dsm(handle, guid, rev, 0, NULL);
  645. if (!obj)
  646. return false;
  647. /* For compatibility, old BIOSes may return an integer */
  648. if (obj->type == ACPI_TYPE_INTEGER)
  649. mask = obj->integer.value;
  650. else if (obj->type == ACPI_TYPE_BUFFER)
  651. for (i = 0; i < obj->buffer.length && i < 8; i++)
  652. mask |= (((u64)obj->buffer.pointer[i]) << (i * 8));
  653. ACPI_FREE(obj);
  654. /*
  655. * Bit 0 indicates whether there's support for any functions other than
  656. * function 0 for the specified GUID and revision.
  657. */
  658. if ((mask & 0x1) && (mask & funcs) == funcs)
  659. return true;
  660. return false;
  661. }
  662. EXPORT_SYMBOL(acpi_check_dsm);
  663. /**
  664. * acpi_dev_hid_uid_match - Match device by supplied HID and UID
  665. * @adev: ACPI device to match.
  666. * @hid2: Hardware ID of the device.
  667. * @uid2: Unique ID of the device, pass NULL to not check _UID.
  668. *
  669. * Matches HID and UID in @adev with given @hid2 and @uid2.
  670. * Returns true if matches.
  671. */
  672. bool acpi_dev_hid_uid_match(struct acpi_device *adev,
  673. const char *hid2, const char *uid2)
  674. {
  675. const char *hid1 = acpi_device_hid(adev);
  676. const char *uid1 = acpi_device_uid(adev);
  677. if (strcmp(hid1, hid2))
  678. return false;
  679. if (!uid2)
  680. return true;
  681. return uid1 && !strcmp(uid1, uid2);
  682. }
  683. EXPORT_SYMBOL(acpi_dev_hid_uid_match);
  684. /**
  685. * acpi_dev_uid_to_integer - treat ACPI device _UID as integer
  686. * @adev: ACPI device to get _UID from
  687. * @integer: output buffer for integer
  688. *
  689. * Considers _UID as integer and converts it to @integer.
  690. *
  691. * Returns 0 on success, or negative error code otherwise.
  692. */
  693. int acpi_dev_uid_to_integer(struct acpi_device *adev, u64 *integer)
  694. {
  695. const char *uid;
  696. if (!adev)
  697. return -ENODEV;
  698. uid = acpi_device_uid(adev);
  699. if (!uid)
  700. return -ENODATA;
  701. return kstrtou64(uid, 0, integer);
  702. }
  703. EXPORT_SYMBOL(acpi_dev_uid_to_integer);
  704. /**
  705. * acpi_dev_found - Detect presence of a given ACPI device in the namespace.
  706. * @hid: Hardware ID of the device.
  707. *
  708. * Return %true if the device was present at the moment of invocation.
  709. * Note that if the device is pluggable, it may since have disappeared.
  710. *
  711. * For this function to work, acpi_bus_scan() must have been executed
  712. * which happens in the subsys_initcall() subsection. Hence, do not
  713. * call from a subsys_initcall() or earlier (use acpi_get_devices()
  714. * instead). Calling from module_init() is fine (which is synonymous
  715. * with device_initcall()).
  716. */
  717. bool acpi_dev_found(const char *hid)
  718. {
  719. struct acpi_device_bus_id *acpi_device_bus_id;
  720. bool found = false;
  721. mutex_lock(&acpi_device_lock);
  722. list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
  723. if (!strcmp(acpi_device_bus_id->bus_id, hid)) {
  724. found = true;
  725. break;
  726. }
  727. mutex_unlock(&acpi_device_lock);
  728. return found;
  729. }
  730. EXPORT_SYMBOL(acpi_dev_found);
  731. struct acpi_dev_match_info {
  732. struct acpi_device_id hid[2];
  733. const char *uid;
  734. s64 hrv;
  735. };
  736. static int acpi_dev_match_cb(struct device *dev, const void *data)
  737. {
  738. struct acpi_device *adev = to_acpi_device(dev);
  739. const struct acpi_dev_match_info *match = data;
  740. unsigned long long hrv;
  741. acpi_status status;
  742. if (acpi_match_device_ids(adev, match->hid))
  743. return 0;
  744. if (match->uid && (!adev->pnp.unique_id ||
  745. strcmp(adev->pnp.unique_id, match->uid)))
  746. return 0;
  747. if (match->hrv == -1)
  748. return 1;
  749. status = acpi_evaluate_integer(adev->handle, "_HRV", NULL, &hrv);
  750. if (ACPI_FAILURE(status))
  751. return 0;
  752. return hrv == match->hrv;
  753. }
  754. /**
  755. * acpi_dev_present - Detect that a given ACPI device is present
  756. * @hid: Hardware ID of the device.
  757. * @uid: Unique ID of the device, pass NULL to not check _UID
  758. * @hrv: Hardware Revision of the device, pass -1 to not check _HRV
  759. *
  760. * Return %true if a matching device was present at the moment of invocation.
  761. * Note that if the device is pluggable, it may since have disappeared.
  762. *
  763. * Note that unlike acpi_dev_found() this function checks the status
  764. * of the device. So for devices which are present in the DSDT, but
  765. * which are disabled (their _STA callback returns 0) this function
  766. * will return false.
  767. *
  768. * For this function to work, acpi_bus_scan() must have been executed
  769. * which happens in the subsys_initcall() subsection. Hence, do not
  770. * call from a subsys_initcall() or earlier (use acpi_get_devices()
  771. * instead). Calling from module_init() is fine (which is synonymous
  772. * with device_initcall()).
  773. */
  774. bool acpi_dev_present(const char *hid, const char *uid, s64 hrv)
  775. {
  776. struct acpi_dev_match_info match = {};
  777. struct device *dev;
  778. strscpy(match.hid[0].id, hid, sizeof(match.hid[0].id));
  779. match.uid = uid;
  780. match.hrv = hrv;
  781. dev = bus_find_device(&acpi_bus_type, NULL, &match, acpi_dev_match_cb);
  782. put_device(dev);
  783. return !!dev;
  784. }
  785. EXPORT_SYMBOL(acpi_dev_present);
  786. /**
  787. * acpi_dev_get_next_match_dev - Return the next match of ACPI device
  788. * @adev: Pointer to the previous ACPI device matching this @hid, @uid and @hrv
  789. * @hid: Hardware ID of the device.
  790. * @uid: Unique ID of the device, pass NULL to not check _UID
  791. * @hrv: Hardware Revision of the device, pass -1 to not check _HRV
  792. *
  793. * Return the next match of ACPI device if another matching device was present
  794. * at the moment of invocation, or NULL otherwise.
  795. *
  796. * The caller is responsible for invoking acpi_dev_put() on the returned device.
  797. * On the other hand the function invokes acpi_dev_put() on the given @adev
  798. * assuming that its reference counter had been increased beforehand.
  799. *
  800. * See additional information in acpi_dev_present() as well.
  801. */
  802. struct acpi_device *
  803. acpi_dev_get_next_match_dev(struct acpi_device *adev, const char *hid, const char *uid, s64 hrv)
  804. {
  805. struct device *start = adev ? &adev->dev : NULL;
  806. struct acpi_dev_match_info match = {};
  807. struct device *dev;
  808. strscpy(match.hid[0].id, hid, sizeof(match.hid[0].id));
  809. match.uid = uid;
  810. match.hrv = hrv;
  811. dev = bus_find_device(&acpi_bus_type, start, &match, acpi_dev_match_cb);
  812. acpi_dev_put(adev);
  813. return dev ? to_acpi_device(dev) : NULL;
  814. }
  815. EXPORT_SYMBOL(acpi_dev_get_next_match_dev);
  816. /**
  817. * acpi_dev_get_first_match_dev - Return the first match of ACPI device
  818. * @hid: Hardware ID of the device.
  819. * @uid: Unique ID of the device, pass NULL to not check _UID
  820. * @hrv: Hardware Revision of the device, pass -1 to not check _HRV
  821. *
  822. * Return the first match of ACPI device if a matching device was present
  823. * at the moment of invocation, or NULL otherwise.
  824. *
  825. * The caller is responsible for invoking acpi_dev_put() on the returned device.
  826. *
  827. * See additional information in acpi_dev_present() as well.
  828. */
  829. struct acpi_device *
  830. acpi_dev_get_first_match_dev(const char *hid, const char *uid, s64 hrv)
  831. {
  832. return acpi_dev_get_next_match_dev(NULL, hid, uid, hrv);
  833. }
  834. EXPORT_SYMBOL(acpi_dev_get_first_match_dev);
  835. /**
  836. * acpi_reduced_hardware - Return if this is an ACPI-reduced-hw machine
  837. *
  838. * Return true when running on an ACPI-reduced-hw machine, false otherwise.
  839. */
  840. bool acpi_reduced_hardware(void)
  841. {
  842. return acpi_gbl_reduced_hardware;
  843. }
  844. EXPORT_SYMBOL_GPL(acpi_reduced_hardware);
  845. /*
  846. * acpi_backlight= handling, this is done here rather then in video_detect.c
  847. * because __setup cannot be used in modules.
  848. */
  849. char acpi_video_backlight_string[16];
  850. EXPORT_SYMBOL(acpi_video_backlight_string);
  851. static int __init acpi_backlight(char *str)
  852. {
  853. strscpy(acpi_video_backlight_string, str,
  854. sizeof(acpi_video_backlight_string));
  855. return 1;
  856. }
  857. __setup("acpi_backlight=", acpi_backlight);
  858. /**
  859. * acpi_match_platform_list - Check if the system matches with a given list
  860. * @plat: pointer to acpi_platform_list table terminated by a NULL entry
  861. *
  862. * Return the matched index if the system is found in the platform list.
  863. * Otherwise, return a negative error code.
  864. */
  865. int acpi_match_platform_list(const struct acpi_platform_list *plat)
  866. {
  867. struct acpi_table_header hdr;
  868. int idx = 0;
  869. if (acpi_disabled)
  870. return -ENODEV;
  871. for (; plat->oem_id[0]; plat++, idx++) {
  872. if (ACPI_FAILURE(acpi_get_table_header(plat->table, 0, &hdr)))
  873. continue;
  874. if (strncmp(plat->oem_id, hdr.oem_id, ACPI_OEM_ID_SIZE))
  875. continue;
  876. if (strncmp(plat->oem_table_id, hdr.oem_table_id, ACPI_OEM_TABLE_ID_SIZE))
  877. continue;
  878. if ((plat->pred == all_versions) ||
  879. (plat->pred == less_than_or_equal && hdr.oem_revision <= plat->oem_revision) ||
  880. (plat->pred == greater_than_or_equal && hdr.oem_revision >= plat->oem_revision) ||
  881. (plat->pred == equal && hdr.oem_revision == plat->oem_revision))
  882. return idx;
  883. }
  884. return -ENODEV;
  885. }
  886. EXPORT_SYMBOL(acpi_match_platform_list);