utils.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <acpi/acpi_bus.h>
  31. #include <acpi/acpi_drivers.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. if (!element) {
  89. return AE_BAD_DATA;
  90. }
  91. switch (element->type) {
  92. case ACPI_TYPE_INTEGER:
  93. switch (format_string[i]) {
  94. case 'N':
  95. size_required += sizeof(u64);
  96. tail_offset += sizeof(u64);
  97. break;
  98. case 'S':
  99. size_required +=
  100. sizeof(char *) + sizeof(u64) +
  101. sizeof(char);
  102. tail_offset += sizeof(char *);
  103. break;
  104. default:
  105. printk(KERN_WARNING PREFIX "Invalid package element"
  106. " [%d]: got number, expecing"
  107. " [%c]\n",
  108. i, format_string[i]);
  109. return AE_BAD_DATA;
  110. break;
  111. }
  112. break;
  113. case ACPI_TYPE_STRING:
  114. case ACPI_TYPE_BUFFER:
  115. switch (format_string[i]) {
  116. case 'S':
  117. size_required +=
  118. sizeof(char *) +
  119. (element->string.length * sizeof(char)) +
  120. sizeof(char);
  121. tail_offset += sizeof(char *);
  122. break;
  123. case 'B':
  124. size_required +=
  125. sizeof(u8 *) +
  126. (element->buffer.length * sizeof(u8));
  127. tail_offset += sizeof(u8 *);
  128. break;
  129. default:
  130. printk(KERN_WARNING PREFIX "Invalid package element"
  131. " [%d] got string/buffer,"
  132. " expecing [%c]\n",
  133. i, format_string[i]);
  134. return AE_BAD_DATA;
  135. break;
  136. }
  137. break;
  138. case ACPI_TYPE_PACKAGE:
  139. default:
  140. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  141. "Found unsupported element at index=%d\n",
  142. i));
  143. /* TBD: handle nested packages... */
  144. return AE_SUPPORT;
  145. break;
  146. }
  147. }
  148. /*
  149. * Validate output buffer.
  150. */
  151. if (buffer->length < size_required) {
  152. buffer->length = size_required;
  153. return AE_BUFFER_OVERFLOW;
  154. } else if (buffer->length != size_required || !buffer->pointer) {
  155. return AE_BAD_PARAMETER;
  156. }
  157. head = buffer->pointer;
  158. tail = buffer->pointer + tail_offset;
  159. /*
  160. * Extract package data.
  161. */
  162. for (i = 0; i < format_count; i++) {
  163. u8 **pointer = NULL;
  164. union acpi_object *element = &(package->package.elements[i]);
  165. if (!element) {
  166. return AE_BAD_DATA;
  167. }
  168. switch (element->type) {
  169. case ACPI_TYPE_INTEGER:
  170. switch (format_string[i]) {
  171. case 'N':
  172. *((u64 *) head) =
  173. element->integer.value;
  174. head += sizeof(u64);
  175. break;
  176. case 'S':
  177. pointer = (u8 **) head;
  178. *pointer = tail;
  179. *((u64 *) tail) =
  180. element->integer.value;
  181. head += sizeof(u64 *);
  182. tail += sizeof(u64);
  183. /* NULL terminate string */
  184. *tail = (char)0;
  185. tail += sizeof(char);
  186. break;
  187. default:
  188. /* Should never get here */
  189. break;
  190. }
  191. break;
  192. case ACPI_TYPE_STRING:
  193. case ACPI_TYPE_BUFFER:
  194. switch (format_string[i]) {
  195. case 'S':
  196. pointer = (u8 **) head;
  197. *pointer = tail;
  198. memcpy(tail, element->string.pointer,
  199. element->string.length);
  200. head += sizeof(char *);
  201. tail += element->string.length * sizeof(char);
  202. /* NULL terminate string */
  203. *tail = (char)0;
  204. tail += sizeof(char);
  205. break;
  206. case 'B':
  207. pointer = (u8 **) head;
  208. *pointer = tail;
  209. memcpy(tail, element->buffer.pointer,
  210. element->buffer.length);
  211. head += sizeof(u8 *);
  212. tail += element->buffer.length * sizeof(u8);
  213. break;
  214. default:
  215. /* Should never get here */
  216. break;
  217. }
  218. break;
  219. case ACPI_TYPE_PACKAGE:
  220. /* TBD: handle nested packages... */
  221. default:
  222. /* Should never get here */
  223. break;
  224. }
  225. }
  226. return AE_OK;
  227. }
  228. EXPORT_SYMBOL(acpi_extract_package);
  229. acpi_status
  230. acpi_evaluate_integer(acpi_handle handle,
  231. acpi_string pathname,
  232. struct acpi_object_list *arguments, unsigned long long *data)
  233. {
  234. acpi_status status = AE_OK;
  235. union acpi_object element;
  236. struct acpi_buffer buffer = { 0, NULL };
  237. if (!data)
  238. return AE_BAD_PARAMETER;
  239. buffer.length = sizeof(union acpi_object);
  240. buffer.pointer = &element;
  241. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  242. if (ACPI_FAILURE(status)) {
  243. acpi_util_eval_error(handle, pathname, status);
  244. return status;
  245. }
  246. if (element.type != ACPI_TYPE_INTEGER) {
  247. acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
  248. return AE_BAD_DATA;
  249. }
  250. *data = element.integer.value;
  251. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
  252. return AE_OK;
  253. }
  254. EXPORT_SYMBOL(acpi_evaluate_integer);
  255. acpi_status
  256. acpi_evaluate_reference(acpi_handle handle,
  257. acpi_string pathname,
  258. struct acpi_object_list *arguments,
  259. struct acpi_handle_list *list)
  260. {
  261. acpi_status status = AE_OK;
  262. union acpi_object *package = NULL;
  263. union acpi_object *element = NULL;
  264. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  265. u32 i = 0;
  266. if (!list) {
  267. return AE_BAD_PARAMETER;
  268. }
  269. /* Evaluate object. */
  270. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  271. if (ACPI_FAILURE(status))
  272. goto end;
  273. package = buffer.pointer;
  274. if ((buffer.length == 0) || !package) {
  275. printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
  276. (unsigned)buffer.length, package);
  277. status = AE_BAD_DATA;
  278. acpi_util_eval_error(handle, pathname, status);
  279. goto end;
  280. }
  281. if (package->type != ACPI_TYPE_PACKAGE) {
  282. printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
  283. package->type);
  284. status = AE_BAD_DATA;
  285. acpi_util_eval_error(handle, pathname, status);
  286. goto end;
  287. }
  288. if (!package->package.count) {
  289. printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
  290. package);
  291. status = AE_BAD_DATA;
  292. acpi_util_eval_error(handle, pathname, status);
  293. goto end;
  294. }
  295. if (package->package.count > ACPI_MAX_HANDLES) {
  296. return AE_NO_MEMORY;
  297. }
  298. list->count = package->package.count;
  299. /* Extract package data. */
  300. for (i = 0; i < list->count; i++) {
  301. element = &(package->package.elements[i]);
  302. if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
  303. status = AE_BAD_DATA;
  304. printk(KERN_ERR PREFIX
  305. "Expecting a [Reference] package element, found type %X\n",
  306. element->type);
  307. acpi_util_eval_error(handle, pathname, status);
  308. break;
  309. }
  310. if (!element->reference.handle) {
  311. printk(KERN_WARNING PREFIX "Invalid reference in"
  312. " package %s\n", pathname);
  313. status = AE_NULL_ENTRY;
  314. break;
  315. }
  316. /* Get the acpi_handle. */
  317. list->handles[i] = element->reference.handle;
  318. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
  319. list->handles[i]));
  320. }
  321. end:
  322. if (ACPI_FAILURE(status)) {
  323. list->count = 0;
  324. //kfree(list->handles);
  325. }
  326. kfree(buffer.pointer);
  327. return status;
  328. }
  329. EXPORT_SYMBOL(acpi_evaluate_reference);