utils.c 23 KB

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