utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/init.h>
  28. #include <linux/types.h>
  29. #include <acpi/acpi_bus.h>
  30. #include <acpi/acpi_drivers.h>
  31. #define _COMPONENT ACPI_BUS_COMPONENT
  32. ACPI_MODULE_NAME ("acpi_utils")
  33. /* --------------------------------------------------------------------------
  34. Object Evaluation Helpers
  35. -------------------------------------------------------------------------- */
  36. #ifdef ACPI_DEBUG_OUTPUT
  37. #define acpi_util_eval_error(h,p,s) {\
  38. char prefix[80] = {'\0'};\
  39. struct acpi_buffer buffer = {sizeof(prefix), prefix};\
  40. acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);\
  41. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",\
  42. (char *) prefix, p, acpi_format_exception(s))); }
  43. #else
  44. #define acpi_util_eval_error(h,p,s)
  45. #endif
  46. acpi_status
  47. acpi_extract_package (
  48. union acpi_object *package,
  49. struct acpi_buffer *format,
  50. struct acpi_buffer *buffer)
  51. {
  52. u32 size_required = 0;
  53. u32 tail_offset = 0;
  54. char *format_string = NULL;
  55. u32 format_count = 0;
  56. u32 i = 0;
  57. u8 *head = NULL;
  58. u8 *tail = NULL;
  59. ACPI_FUNCTION_TRACE("acpi_extract_package");
  60. if (!package || (package->type != ACPI_TYPE_PACKAGE) || (package->package.count < 1)) {
  61. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid 'package' argument\n"));
  62. return_ACPI_STATUS(AE_BAD_PARAMETER);
  63. }
  64. if (!format || !format->pointer || (format->length < 1)) {
  65. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid 'format' argument\n"));
  66. return_ACPI_STATUS(AE_BAD_PARAMETER);
  67. }
  68. if (!buffer) {
  69. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid 'buffer' argument\n"));
  70. return_ACPI_STATUS(AE_BAD_PARAMETER);
  71. }
  72. format_count = (format->length/sizeof(char)) - 1;
  73. if (format_count > package->package.count) {
  74. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Format specifies more objects [%d] than exist in package [%d].", format_count, package->package.count));
  75. return_ACPI_STATUS(AE_BAD_DATA);
  76. }
  77. format_string = (char*)format->pointer;
  78. /*
  79. * Calculate size_required.
  80. */
  81. for (i=0; i<format_count; i++) {
  82. union acpi_object *element = &(package->package.elements[i]);
  83. if (!element) {
  84. return_ACPI_STATUS(AE_BAD_DATA);
  85. }
  86. switch (element->type) {
  87. case ACPI_TYPE_INTEGER:
  88. switch (format_string[i]) {
  89. case 'N':
  90. size_required += sizeof(acpi_integer);
  91. tail_offset += sizeof(acpi_integer);
  92. break;
  93. case 'S':
  94. size_required += sizeof(char*) + sizeof(acpi_integer) + sizeof(char);
  95. tail_offset += sizeof(char*);
  96. break;
  97. default:
  98. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid package element [%d]: got number, expecing [%c].\n", i, format_string[i]));
  99. return_ACPI_STATUS(AE_BAD_DATA);
  100. break;
  101. }
  102. break;
  103. case ACPI_TYPE_STRING:
  104. case ACPI_TYPE_BUFFER:
  105. switch (format_string[i]) {
  106. case 'S':
  107. size_required += sizeof(char*) + (element->string.length * sizeof(char)) + sizeof(char);
  108. tail_offset += sizeof(char*);
  109. break;
  110. case 'B':
  111. size_required += sizeof(u8*) + (element->buffer.length * sizeof(u8));
  112. tail_offset += sizeof(u8*);
  113. break;
  114. default:
  115. ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid package element [%d] got string/buffer, expecing [%c].\n", i, format_string[i]));
  116. return_ACPI_STATUS(AE_BAD_DATA);
  117. break;
  118. }
  119. break;
  120. case ACPI_TYPE_PACKAGE:
  121. default:
  122. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unsupported element at index=%d\n", i));
  123. /* TBD: handle nested packages... */
  124. return_ACPI_STATUS(AE_SUPPORT);
  125. break;
  126. }
  127. }
  128. /*
  129. * Validate output buffer.
  130. */
  131. if (buffer->length < size_required) {
  132. buffer->length = size_required;
  133. return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
  134. }
  135. else if (buffer->length != size_required || !buffer->pointer) {
  136. return_ACPI_STATUS(AE_BAD_PARAMETER);
  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. if (!element) {
  147. return_ACPI_STATUS(AE_BAD_DATA);
  148. }
  149. switch (element->type) {
  150. case ACPI_TYPE_INTEGER:
  151. switch (format_string[i]) {
  152. case 'N':
  153. *((acpi_integer*)head) = element->integer.value;
  154. head += sizeof(acpi_integer);
  155. break;
  156. case 'S':
  157. pointer = (u8**)head;
  158. *pointer = tail;
  159. *((acpi_integer*)tail) = element->integer.value;
  160. head += sizeof(acpi_integer*);
  161. tail += sizeof(acpi_integer);
  162. /* NULL terminate string */
  163. *tail = (char)0;
  164. tail += sizeof(char);
  165. break;
  166. default:
  167. /* Should never get here */
  168. break;
  169. }
  170. break;
  171. case ACPI_TYPE_STRING:
  172. case ACPI_TYPE_BUFFER:
  173. switch (format_string[i]) {
  174. case 'S':
  175. pointer = (u8**)head;
  176. *pointer = tail;
  177. memcpy(tail, element->string.pointer, 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, element->buffer.length);
  188. head += sizeof(u8*);
  189. tail += element->buffer.length * sizeof(u8);
  190. break;
  191. default:
  192. /* Should never get here */
  193. break;
  194. }
  195. break;
  196. case ACPI_TYPE_PACKAGE:
  197. /* TBD: handle nested packages... */
  198. default:
  199. /* Should never get here */
  200. break;
  201. }
  202. }
  203. return_ACPI_STATUS(AE_OK);
  204. }
  205. EXPORT_SYMBOL(acpi_extract_package);
  206. acpi_status
  207. acpi_evaluate_integer (
  208. acpi_handle handle,
  209. acpi_string pathname,
  210. struct acpi_object_list *arguments,
  211. unsigned long *data)
  212. {
  213. acpi_status status = AE_OK;
  214. union acpi_object *element;
  215. struct acpi_buffer buffer = {0,NULL};
  216. ACPI_FUNCTION_TRACE("acpi_evaluate_integer");
  217. if (!data)
  218. return_ACPI_STATUS(AE_BAD_PARAMETER);
  219. element = kmalloc(sizeof(union acpi_object), GFP_KERNEL);
  220. if(!element)
  221. return_ACPI_STATUS(AE_NO_MEMORY);
  222. memset(element, 0, sizeof(union acpi_object));
  223. buffer.length = sizeof(union acpi_object);
  224. buffer.pointer = element;
  225. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  226. if (ACPI_FAILURE(status)) {
  227. acpi_util_eval_error(handle, pathname, status);
  228. return_ACPI_STATUS(status);
  229. }
  230. if (element->type != ACPI_TYPE_INTEGER) {
  231. acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
  232. return_ACPI_STATUS(AE_BAD_DATA);
  233. }
  234. *data = element->integer.value;
  235. kfree(element);
  236. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%lu]\n", *data));
  237. return_ACPI_STATUS(AE_OK);
  238. }
  239. EXPORT_SYMBOL(acpi_evaluate_integer);
  240. #if 0
  241. acpi_status
  242. acpi_evaluate_string (
  243. acpi_handle handle,
  244. acpi_string pathname,
  245. acpi_object_list *arguments,
  246. acpi_string *data)
  247. {
  248. acpi_status status = AE_OK;
  249. acpi_object *element = NULL;
  250. acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  251. ACPI_FUNCTION_TRACE("acpi_evaluate_string");
  252. if (!data)
  253. return_ACPI_STATUS(AE_BAD_PARAMETER);
  254. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  255. if (ACPI_FAILURE(status)) {
  256. acpi_util_eval_error(handle, pathname, status);
  257. return_ACPI_STATUS(status);
  258. }
  259. element = (acpi_object *) buffer.pointer;
  260. if ((element->type != ACPI_TYPE_STRING)
  261. || (element->type != ACPI_TYPE_BUFFER)
  262. || !element->string.length) {
  263. acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
  264. return_ACPI_STATUS(AE_BAD_DATA);
  265. }
  266. *data = kmalloc(element->string.length + 1, GFP_KERNEL);
  267. if (!data) {
  268. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
  269. return_VALUE(-ENOMEM);
  270. }
  271. memset(*data, 0, element->string.length + 1);
  272. memcpy(*data, element->string.pointer, element->string.length);
  273. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%s]\n", *data));
  274. acpi_os_free(buffer.pointer);
  275. return_ACPI_STATUS(AE_OK);
  276. }
  277. #endif
  278. acpi_status
  279. acpi_evaluate_reference (
  280. acpi_handle handle,
  281. acpi_string pathname,
  282. struct acpi_object_list *arguments,
  283. struct acpi_handle_list *list)
  284. {
  285. acpi_status status = AE_OK;
  286. union acpi_object *package = NULL;
  287. union acpi_object *element = NULL;
  288. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  289. u32 i = 0;
  290. ACPI_FUNCTION_TRACE("acpi_evaluate_reference");
  291. if (!list) {
  292. return_ACPI_STATUS(AE_BAD_PARAMETER);
  293. }
  294. /* Evaluate object. */
  295. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  296. if (ACPI_FAILURE(status))
  297. goto end;
  298. package = (union acpi_object *) buffer.pointer;
  299. if ((buffer.length == 0) || !package) {
  300. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  301. "No return object (len %X ptr %p)\n",
  302. (unsigned)buffer.length, package));
  303. status = AE_BAD_DATA;
  304. acpi_util_eval_error(handle, pathname, status);
  305. goto end;
  306. }
  307. if (package->type != ACPI_TYPE_PACKAGE) {
  308. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  309. "Expecting a [Package], found type %X\n",
  310. package->type));
  311. status = AE_BAD_DATA;
  312. acpi_util_eval_error(handle, pathname, status);
  313. goto end;
  314. }
  315. if (!package->package.count) {
  316. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  317. "[Package] has zero elements (%p)\n",
  318. package));
  319. status = AE_BAD_DATA;
  320. acpi_util_eval_error(handle, pathname, status);
  321. goto end;
  322. }
  323. if (package->package.count > ACPI_MAX_HANDLES) {
  324. return_ACPI_STATUS(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_ANY) {
  331. status = AE_BAD_DATA;
  332. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  333. "Expecting a [Reference] package element, found type %X\n",
  334. element->type));
  335. acpi_util_eval_error(handle, pathname, status);
  336. break;
  337. }
  338. /* Get the acpi_handle. */
  339. list->handles[i] = element->reference.handle;
  340. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
  341. list->handles[i]));
  342. }
  343. end:
  344. if (ACPI_FAILURE(status)) {
  345. list->count = 0;
  346. //kfree(list->handles);
  347. }
  348. acpi_os_free(buffer.pointer);
  349. return_ACPI_STATUS(status);
  350. }
  351. EXPORT_SYMBOL(acpi_evaluate_reference);