utils.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/hardirq.h>
  31. #include <linux/acpi.h>
  32. #include "internal.h"
  33. #define _COMPONENT ACPI_BUS_COMPONENT
  34. ACPI_MODULE_NAME("utils");
  35. /* --------------------------------------------------------------------------
  36. Object Evaluation Helpers
  37. -------------------------------------------------------------------------- */
  38. static void
  39. acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
  40. {
  41. #ifdef ACPI_DEBUG_OUTPUT
  42. char prefix[80] = {'\0'};
  43. struct acpi_buffer buffer = {sizeof(prefix), prefix};
  44. acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);
  45. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",
  46. (char *) prefix, p, acpi_format_exception(s)));
  47. #else
  48. return;
  49. #endif
  50. }
  51. acpi_status
  52. acpi_extract_package(union acpi_object *package,
  53. struct acpi_buffer *format, struct acpi_buffer *buffer)
  54. {
  55. u32 size_required = 0;
  56. u32 tail_offset = 0;
  57. char *format_string = NULL;
  58. u32 format_count = 0;
  59. u32 i = 0;
  60. u8 *head = NULL;
  61. u8 *tail = NULL;
  62. if (!package || (package->type != ACPI_TYPE_PACKAGE)
  63. || (package->package.count < 1)) {
  64. printk(KERN_WARNING PREFIX "Invalid package argument\n");
  65. return AE_BAD_PARAMETER;
  66. }
  67. if (!format || !format->pointer || (format->length < 1)) {
  68. printk(KERN_WARNING PREFIX "Invalid format argument\n");
  69. return AE_BAD_PARAMETER;
  70. }
  71. if (!buffer) {
  72. printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
  73. return AE_BAD_PARAMETER;
  74. }
  75. format_count = (format->length / sizeof(char)) - 1;
  76. if (format_count > package->package.count) {
  77. printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
  78. " than exist in package [%d].\n",
  79. format_count, package->package.count);
  80. return AE_BAD_DATA;
  81. }
  82. format_string = format->pointer;
  83. /*
  84. * Calculate size_required.
  85. */
  86. for (i = 0; i < format_count; i++) {
  87. union acpi_object *element = &(package->package.elements[i]);
  88. switch (element->type) {
  89. case ACPI_TYPE_INTEGER:
  90. switch (format_string[i]) {
  91. case 'N':
  92. size_required += sizeof(u64);
  93. tail_offset += sizeof(u64);
  94. break;
  95. case 'S':
  96. size_required +=
  97. sizeof(char *) + sizeof(u64) +
  98. sizeof(char);
  99. tail_offset += sizeof(char *);
  100. break;
  101. default:
  102. printk(KERN_WARNING PREFIX "Invalid package element"
  103. " [%d]: got number, expecting"
  104. " [%c]\n",
  105. i, format_string[i]);
  106. return AE_BAD_DATA;
  107. break;
  108. }
  109. break;
  110. case ACPI_TYPE_STRING:
  111. case ACPI_TYPE_BUFFER:
  112. switch (format_string[i]) {
  113. case 'S':
  114. size_required +=
  115. sizeof(char *) +
  116. (element->string.length * sizeof(char)) +
  117. sizeof(char);
  118. tail_offset += sizeof(char *);
  119. break;
  120. case 'B':
  121. size_required +=
  122. sizeof(u8 *) +
  123. (element->buffer.length * sizeof(u8));
  124. tail_offset += sizeof(u8 *);
  125. break;
  126. default:
  127. printk(KERN_WARNING PREFIX "Invalid package element"
  128. " [%d] got string/buffer,"
  129. " expecting [%c]\n",
  130. i, format_string[i]);
  131. return AE_BAD_DATA;
  132. break;
  133. }
  134. break;
  135. case ACPI_TYPE_PACKAGE:
  136. default:
  137. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  138. "Found unsupported element at index=%d\n",
  139. i));
  140. /* TBD: handle nested packages... */
  141. return AE_SUPPORT;
  142. break;
  143. }
  144. }
  145. /*
  146. * Validate output buffer.
  147. */
  148. if (buffer->length == ACPI_ALLOCATE_BUFFER) {
  149. buffer->pointer = ACPI_ALLOCATE(size_required);
  150. if (!buffer->pointer)
  151. return AE_NO_MEMORY;
  152. buffer->length = size_required;
  153. memset(buffer->pointer, 0, size_required);
  154. } else {
  155. if (buffer->length < size_required) {
  156. buffer->length = size_required;
  157. return AE_BUFFER_OVERFLOW;
  158. } else if (buffer->length != size_required ||
  159. !buffer->pointer) {
  160. return AE_BAD_PARAMETER;
  161. }
  162. }
  163. head = buffer->pointer;
  164. tail = buffer->pointer + tail_offset;
  165. /*
  166. * Extract package data.
  167. */
  168. for (i = 0; i < format_count; i++) {
  169. u8 **pointer = NULL;
  170. union acpi_object *element = &(package->package.elements[i]);
  171. if (!element) {
  172. return AE_BAD_DATA;
  173. }
  174. switch (element->type) {
  175. case ACPI_TYPE_INTEGER:
  176. switch (format_string[i]) {
  177. case 'N':
  178. *((u64 *) head) =
  179. element->integer.value;
  180. head += sizeof(u64);
  181. break;
  182. case 'S':
  183. pointer = (u8 **) head;
  184. *pointer = tail;
  185. *((u64 *) tail) =
  186. element->integer.value;
  187. head += sizeof(u64 *);
  188. tail += sizeof(u64);
  189. /* NULL terminate string */
  190. *tail = (char)0;
  191. tail += sizeof(char);
  192. break;
  193. default:
  194. /* Should never get here */
  195. break;
  196. }
  197. break;
  198. case ACPI_TYPE_STRING:
  199. case ACPI_TYPE_BUFFER:
  200. switch (format_string[i]) {
  201. case 'S':
  202. pointer = (u8 **) head;
  203. *pointer = tail;
  204. memcpy(tail, element->string.pointer,
  205. element->string.length);
  206. head += sizeof(char *);
  207. tail += element->string.length * sizeof(char);
  208. /* NULL terminate string */
  209. *tail = (char)0;
  210. tail += sizeof(char);
  211. break;
  212. case 'B':
  213. pointer = (u8 **) head;
  214. *pointer = tail;
  215. memcpy(tail, element->buffer.pointer,
  216. element->buffer.length);
  217. head += sizeof(u8 *);
  218. tail += element->buffer.length * sizeof(u8);
  219. break;
  220. default:
  221. /* Should never get here */
  222. break;
  223. }
  224. break;
  225. case ACPI_TYPE_PACKAGE:
  226. /* TBD: handle nested packages... */
  227. default:
  228. /* Should never get here */
  229. break;
  230. }
  231. }
  232. return AE_OK;
  233. }
  234. EXPORT_SYMBOL(acpi_extract_package);
  235. acpi_status
  236. acpi_evaluate_integer(acpi_handle handle,
  237. acpi_string pathname,
  238. struct acpi_object_list *arguments, unsigned long long *data)
  239. {
  240. acpi_status status = AE_OK;
  241. union acpi_object element;
  242. struct acpi_buffer buffer = { 0, NULL };
  243. if (!data)
  244. return AE_BAD_PARAMETER;
  245. buffer.length = sizeof(union acpi_object);
  246. buffer.pointer = &element;
  247. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  248. if (ACPI_FAILURE(status)) {
  249. acpi_util_eval_error(handle, pathname, status);
  250. return status;
  251. }
  252. if (element.type != ACPI_TYPE_INTEGER) {
  253. acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
  254. return AE_BAD_DATA;
  255. }
  256. *data = element.integer.value;
  257. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
  258. return AE_OK;
  259. }
  260. EXPORT_SYMBOL(acpi_evaluate_integer);
  261. acpi_status
  262. acpi_evaluate_reference(acpi_handle handle,
  263. acpi_string pathname,
  264. struct acpi_object_list *arguments,
  265. struct acpi_handle_list *list)
  266. {
  267. acpi_status status = AE_OK;
  268. union acpi_object *package = NULL;
  269. union acpi_object *element = NULL;
  270. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  271. u32 i = 0;
  272. if (!list) {
  273. return AE_BAD_PARAMETER;
  274. }
  275. /* Evaluate object. */
  276. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  277. if (ACPI_FAILURE(status))
  278. goto end;
  279. package = buffer.pointer;
  280. if ((buffer.length == 0) || !package) {
  281. printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
  282. (unsigned)buffer.length, package);
  283. status = AE_BAD_DATA;
  284. acpi_util_eval_error(handle, pathname, status);
  285. goto end;
  286. }
  287. if (package->type != ACPI_TYPE_PACKAGE) {
  288. printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
  289. package->type);
  290. status = AE_BAD_DATA;
  291. acpi_util_eval_error(handle, pathname, status);
  292. goto end;
  293. }
  294. if (!package->package.count) {
  295. printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
  296. package);
  297. status = AE_BAD_DATA;
  298. acpi_util_eval_error(handle, pathname, status);
  299. goto end;
  300. }
  301. if (package->package.count > ACPI_MAX_HANDLES) {
  302. return AE_NO_MEMORY;
  303. }
  304. list->count = package->package.count;
  305. /* Extract package data. */
  306. for (i = 0; i < list->count; i++) {
  307. element = &(package->package.elements[i]);
  308. if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
  309. status = AE_BAD_DATA;
  310. printk(KERN_ERR PREFIX
  311. "Expecting a [Reference] package element, found type %X\n",
  312. element->type);
  313. acpi_util_eval_error(handle, pathname, status);
  314. break;
  315. }
  316. if (!element->reference.handle) {
  317. printk(KERN_WARNING PREFIX "Invalid reference in"
  318. " package %s\n", pathname);
  319. status = AE_NULL_ENTRY;
  320. break;
  321. }
  322. /* Get the acpi_handle. */
  323. list->handles[i] = element->reference.handle;
  324. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
  325. list->handles[i]));
  326. }
  327. end:
  328. if (ACPI_FAILURE(status)) {
  329. list->count = 0;
  330. //kfree(list->handles);
  331. }
  332. kfree(buffer.pointer);
  333. return status;
  334. }
  335. EXPORT_SYMBOL(acpi_evaluate_reference);
  336. acpi_status
  337. acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
  338. {
  339. acpi_status status;
  340. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  341. union acpi_object *output;
  342. status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
  343. if (ACPI_FAILURE(status))
  344. return status;
  345. output = buffer.pointer;
  346. if (!output || output->type != ACPI_TYPE_PACKAGE
  347. || !output->package.count
  348. || output->package.elements[0].type != ACPI_TYPE_BUFFER
  349. || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
  350. status = AE_TYPE;
  351. goto out;
  352. }
  353. status = acpi_decode_pld_buffer(
  354. output->package.elements[0].buffer.pointer,
  355. output->package.elements[0].buffer.length,
  356. pld);
  357. out:
  358. kfree(buffer.pointer);
  359. return status;
  360. }
  361. EXPORT_SYMBOL(acpi_get_physical_device_location);
  362. /**
  363. * acpi_evaluate_hotplug_ost: Evaluate _OST for hotplug operations
  364. * @handle: ACPI device handle
  365. * @source_event: source event code
  366. * @status_code: status code
  367. * @status_buf: optional detailed information (NULL if none)
  368. *
  369. * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
  370. * must call this function when evaluating _OST for hotplug operations.
  371. * When the platform does not support _OST, this function has no effect.
  372. */
  373. acpi_status
  374. acpi_evaluate_hotplug_ost(acpi_handle handle, u32 source_event,
  375. u32 status_code, struct acpi_buffer *status_buf)
  376. {
  377. #ifdef ACPI_HOTPLUG_OST
  378. union acpi_object params[3] = {
  379. {.type = ACPI_TYPE_INTEGER,},
  380. {.type = ACPI_TYPE_INTEGER,},
  381. {.type = ACPI_TYPE_BUFFER,}
  382. };
  383. struct acpi_object_list arg_list = {3, params};
  384. acpi_status status;
  385. params[0].integer.value = source_event;
  386. params[1].integer.value = status_code;
  387. if (status_buf != NULL) {
  388. params[2].buffer.pointer = status_buf->pointer;
  389. params[2].buffer.length = status_buf->length;
  390. } else {
  391. params[2].buffer.pointer = NULL;
  392. params[2].buffer.length = 0;
  393. }
  394. status = acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
  395. return status;
  396. #else
  397. return AE_OK;
  398. #endif
  399. }
  400. EXPORT_SYMBOL(acpi_evaluate_hotplug_ost);
  401. /**
  402. * acpi_handle_printk: Print message with ACPI prefix and object path
  403. *
  404. * This function is called through acpi_handle_<level> macros and prints
  405. * a message with ACPI prefix and object path. This function acquires
  406. * the global namespace mutex to obtain an object path. In interrupt
  407. * context, it shows the object path as <n/a>.
  408. */
  409. void
  410. acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
  411. {
  412. struct va_format vaf;
  413. va_list args;
  414. struct acpi_buffer buffer = {
  415. .length = ACPI_ALLOCATE_BUFFER,
  416. .pointer = NULL
  417. };
  418. const char *path;
  419. va_start(args, fmt);
  420. vaf.fmt = fmt;
  421. vaf.va = &args;
  422. if (in_interrupt() ||
  423. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
  424. path = "<n/a>";
  425. else
  426. path = buffer.pointer;
  427. printk("%sACPI: %s: %pV", level, path, &vaf);
  428. va_end(args);
  429. kfree(buffer.pointer);
  430. }
  431. EXPORT_SYMBOL(acpi_handle_printk);
  432. /**
  433. * acpi_has_method: Check whether @handle has a method named @name
  434. * @handle: ACPI device handle
  435. * @name: name of object or method
  436. *
  437. * Check whether @handle has a method named @name.
  438. */
  439. bool acpi_has_method(acpi_handle handle, char *name)
  440. {
  441. acpi_handle tmp;
  442. return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
  443. }
  444. EXPORT_SYMBOL(acpi_has_method);
  445. acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
  446. u64 arg)
  447. {
  448. union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
  449. struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
  450. obj.integer.value = arg;
  451. return acpi_evaluate_object(handle, method, &arg_list, NULL);
  452. }
  453. EXPORT_SYMBOL(acpi_execute_simple_method);
  454. /**
  455. * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
  456. * @handle: ACPI device handle
  457. *
  458. * Evaluate device's _EJ0 method for hotplug operations.
  459. */
  460. acpi_status acpi_evaluate_ej0(acpi_handle handle)
  461. {
  462. acpi_status status;
  463. status = acpi_execute_simple_method(handle, "_EJ0", 1);
  464. if (status == AE_NOT_FOUND)
  465. acpi_handle_warn(handle, "No _EJ0 support for device\n");
  466. else if (ACPI_FAILURE(status))
  467. acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
  468. return status;
  469. }
  470. /**
  471. * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
  472. * @handle: ACPI device handle
  473. * @lock: lock device if non-zero, otherwise unlock device
  474. *
  475. * Evaluate device's _LCK method if present to lock/unlock device
  476. */
  477. acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
  478. {
  479. acpi_status status;
  480. status = acpi_execute_simple_method(handle, "_LCK", !!lock);
  481. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  482. if (lock)
  483. acpi_handle_warn(handle,
  484. "Locking device failed (0x%x)\n", status);
  485. else
  486. acpi_handle_warn(handle,
  487. "Unlocking device failed (0x%x)\n", status);
  488. }
  489. return status;
  490. }
  491. /**
  492. * acpi_evaluate_dsm - evaluate device's _DSM method
  493. * @handle: ACPI device handle
  494. * @uuid: UUID of requested functions, should be 16 bytes
  495. * @rev: revision number of requested function
  496. * @func: requested function number
  497. * @argv4: the function specific parameter
  498. *
  499. * Evaluate device's _DSM method with specified UUID, revision id and
  500. * function number. Caller needs to free the returned object.
  501. *
  502. * Though ACPI defines the fourth parameter for _DSM should be a package,
  503. * some old BIOSes do expect a buffer or an integer etc.
  504. */
  505. union acpi_object *
  506. acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
  507. union acpi_object *argv4)
  508. {
  509. acpi_status ret;
  510. struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
  511. union acpi_object params[4];
  512. struct acpi_object_list input = {
  513. .count = 4,
  514. .pointer = params,
  515. };
  516. params[0].type = ACPI_TYPE_BUFFER;
  517. params[0].buffer.length = 16;
  518. params[0].buffer.pointer = (char *)uuid;
  519. params[1].type = ACPI_TYPE_INTEGER;
  520. params[1].integer.value = rev;
  521. params[2].type = ACPI_TYPE_INTEGER;
  522. params[2].integer.value = func;
  523. if (argv4) {
  524. params[3] = *argv4;
  525. } else {
  526. params[3].type = ACPI_TYPE_PACKAGE;
  527. params[3].package.count = 0;
  528. params[3].package.elements = NULL;
  529. }
  530. ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
  531. if (ACPI_SUCCESS(ret))
  532. return (union acpi_object *)buf.pointer;
  533. if (ret != AE_NOT_FOUND)
  534. acpi_handle_warn(handle,
  535. "failed to evaluate _DSM (0x%x)\n", ret);
  536. return NULL;
  537. }
  538. EXPORT_SYMBOL(acpi_evaluate_dsm);
  539. /**
  540. * acpi_check_dsm - check if _DSM method supports requested functions.
  541. * @handle: ACPI device handle
  542. * @uuid: UUID of requested functions, should be 16 bytes at least
  543. * @rev: revision number of requested functions
  544. * @funcs: bitmap of requested functions
  545. * @exclude: excluding special value, used to support i915 and nouveau
  546. *
  547. * Evaluate device's _DSM method to check whether it supports requested
  548. * functions. Currently only support 64 functions at maximum, should be
  549. * enough for now.
  550. */
  551. bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
  552. {
  553. int i;
  554. u64 mask = 0;
  555. union acpi_object *obj;
  556. if (funcs == 0)
  557. return false;
  558. obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
  559. if (!obj)
  560. return false;
  561. /* For compatibility, old BIOSes may return an integer */
  562. if (obj->type == ACPI_TYPE_INTEGER)
  563. mask = obj->integer.value;
  564. else if (obj->type == ACPI_TYPE_BUFFER)
  565. for (i = 0; i < obj->buffer.length && i < 8; i++)
  566. mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
  567. ACPI_FREE(obj);
  568. /*
  569. * Bit 0 indicates whether there's support for any functions other than
  570. * function 0 for the specified UUID and revision.
  571. */
  572. if ((mask & 0x1) && (mask & funcs) == funcs)
  573. return true;
  574. return false;
  575. }
  576. EXPORT_SYMBOL(acpi_check_dsm);