utils.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 <acpi/acpi_bus.h>
  33. #include <acpi/acpi_drivers.h>
  34. #include "internal.h"
  35. #define _COMPONENT ACPI_BUS_COMPONENT
  36. ACPI_MODULE_NAME("utils");
  37. /* --------------------------------------------------------------------------
  38. Object Evaluation Helpers
  39. -------------------------------------------------------------------------- */
  40. static void
  41. acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
  42. {
  43. #ifdef ACPI_DEBUG_OUTPUT
  44. char prefix[80] = {'\0'};
  45. struct acpi_buffer buffer = {sizeof(prefix), prefix};
  46. acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);
  47. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",
  48. (char *) prefix, p, acpi_format_exception(s)));
  49. #else
  50. return;
  51. #endif
  52. }
  53. acpi_status
  54. acpi_extract_package(union acpi_object *package,
  55. struct acpi_buffer *format, struct acpi_buffer *buffer)
  56. {
  57. u32 size_required = 0;
  58. u32 tail_offset = 0;
  59. char *format_string = NULL;
  60. u32 format_count = 0;
  61. u32 i = 0;
  62. u8 *head = NULL;
  63. u8 *tail = NULL;
  64. if (!package || (package->type != ACPI_TYPE_PACKAGE)
  65. || (package->package.count < 1)) {
  66. printk(KERN_WARNING PREFIX "Invalid package argument\n");
  67. return AE_BAD_PARAMETER;
  68. }
  69. if (!format || !format->pointer || (format->length < 1)) {
  70. printk(KERN_WARNING PREFIX "Invalid format argument\n");
  71. return AE_BAD_PARAMETER;
  72. }
  73. if (!buffer) {
  74. printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
  75. return AE_BAD_PARAMETER;
  76. }
  77. format_count = (format->length / sizeof(char)) - 1;
  78. if (format_count > package->package.count) {
  79. printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
  80. " than exist in package [%d].\n",
  81. format_count, package->package.count);
  82. return AE_BAD_DATA;
  83. }
  84. format_string = format->pointer;
  85. /*
  86. * Calculate size_required.
  87. */
  88. for (i = 0; i < format_count; i++) {
  89. union acpi_object *element = &(package->package.elements[i]);
  90. if (!element) {
  91. return AE_BAD_DATA;
  92. }
  93. switch (element->type) {
  94. case ACPI_TYPE_INTEGER:
  95. switch (format_string[i]) {
  96. case 'N':
  97. size_required += sizeof(u64);
  98. tail_offset += sizeof(u64);
  99. break;
  100. case 'S':
  101. size_required +=
  102. sizeof(char *) + sizeof(u64) +
  103. sizeof(char);
  104. tail_offset += sizeof(char *);
  105. break;
  106. default:
  107. printk(KERN_WARNING PREFIX "Invalid package element"
  108. " [%d]: got number, expecing"
  109. " [%c]\n",
  110. i, format_string[i]);
  111. return AE_BAD_DATA;
  112. break;
  113. }
  114. break;
  115. case ACPI_TYPE_STRING:
  116. case ACPI_TYPE_BUFFER:
  117. switch (format_string[i]) {
  118. case 'S':
  119. size_required +=
  120. sizeof(char *) +
  121. (element->string.length * sizeof(char)) +
  122. sizeof(char);
  123. tail_offset += sizeof(char *);
  124. break;
  125. case 'B':
  126. size_required +=
  127. sizeof(u8 *) +
  128. (element->buffer.length * sizeof(u8));
  129. tail_offset += sizeof(u8 *);
  130. break;
  131. default:
  132. printk(KERN_WARNING PREFIX "Invalid package element"
  133. " [%d] got string/buffer,"
  134. " expecing [%c]\n",
  135. i, format_string[i]);
  136. return AE_BAD_DATA;
  137. break;
  138. }
  139. break;
  140. case ACPI_TYPE_PACKAGE:
  141. default:
  142. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  143. "Found unsupported element at index=%d\n",
  144. i));
  145. /* TBD: handle nested packages... */
  146. return AE_SUPPORT;
  147. break;
  148. }
  149. }
  150. /*
  151. * Validate output buffer.
  152. */
  153. if (buffer->length == ACPI_ALLOCATE_BUFFER) {
  154. buffer->pointer = ACPI_ALLOCATE(size_required);
  155. if (!buffer->pointer)
  156. return AE_NO_MEMORY;
  157. buffer->length = size_required;
  158. memset(buffer->pointer, 0, size_required);
  159. } else {
  160. if (buffer->length < size_required) {
  161. buffer->length = size_required;
  162. return AE_BUFFER_OVERFLOW;
  163. } else if (buffer->length != size_required ||
  164. !buffer->pointer) {
  165. return AE_BAD_PARAMETER;
  166. }
  167. }
  168. head = buffer->pointer;
  169. tail = buffer->pointer + tail_offset;
  170. /*
  171. * Extract package data.
  172. */
  173. for (i = 0; i < format_count; i++) {
  174. u8 **pointer = NULL;
  175. union acpi_object *element = &(package->package.elements[i]);
  176. if (!element) {
  177. return AE_BAD_DATA;
  178. }
  179. switch (element->type) {
  180. case ACPI_TYPE_INTEGER:
  181. switch (format_string[i]) {
  182. case 'N':
  183. *((u64 *) head) =
  184. element->integer.value;
  185. head += sizeof(u64);
  186. break;
  187. case 'S':
  188. pointer = (u8 **) head;
  189. *pointer = tail;
  190. *((u64 *) tail) =
  191. element->integer.value;
  192. head += sizeof(u64 *);
  193. tail += sizeof(u64);
  194. /* NULL terminate string */
  195. *tail = (char)0;
  196. tail += sizeof(char);
  197. break;
  198. default:
  199. /* Should never get here */
  200. break;
  201. }
  202. break;
  203. case ACPI_TYPE_STRING:
  204. case ACPI_TYPE_BUFFER:
  205. switch (format_string[i]) {
  206. case 'S':
  207. pointer = (u8 **) head;
  208. *pointer = tail;
  209. memcpy(tail, element->string.pointer,
  210. element->string.length);
  211. head += sizeof(char *);
  212. tail += element->string.length * sizeof(char);
  213. /* NULL terminate string */
  214. *tail = (char)0;
  215. tail += sizeof(char);
  216. break;
  217. case 'B':
  218. pointer = (u8 **) head;
  219. *pointer = tail;
  220. memcpy(tail, element->buffer.pointer,
  221. element->buffer.length);
  222. head += sizeof(u8 *);
  223. tail += element->buffer.length * sizeof(u8);
  224. break;
  225. default:
  226. /* Should never get here */
  227. break;
  228. }
  229. break;
  230. case ACPI_TYPE_PACKAGE:
  231. /* TBD: handle nested packages... */
  232. default:
  233. /* Should never get here */
  234. break;
  235. }
  236. }
  237. return AE_OK;
  238. }
  239. EXPORT_SYMBOL(acpi_extract_package);
  240. acpi_status
  241. acpi_evaluate_integer(acpi_handle handle,
  242. acpi_string pathname,
  243. struct acpi_object_list *arguments, unsigned long long *data)
  244. {
  245. acpi_status status = AE_OK;
  246. union acpi_object element;
  247. struct acpi_buffer buffer = { 0, NULL };
  248. if (!data)
  249. return AE_BAD_PARAMETER;
  250. buffer.length = sizeof(union acpi_object);
  251. buffer.pointer = &element;
  252. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  253. if (ACPI_FAILURE(status)) {
  254. acpi_util_eval_error(handle, pathname, status);
  255. return status;
  256. }
  257. if (element.type != ACPI_TYPE_INTEGER) {
  258. acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
  259. return AE_BAD_DATA;
  260. }
  261. *data = element.integer.value;
  262. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
  263. return AE_OK;
  264. }
  265. EXPORT_SYMBOL(acpi_evaluate_integer);
  266. acpi_status
  267. acpi_evaluate_reference(acpi_handle handle,
  268. acpi_string pathname,
  269. struct acpi_object_list *arguments,
  270. struct acpi_handle_list *list)
  271. {
  272. acpi_status status = AE_OK;
  273. union acpi_object *package = NULL;
  274. union acpi_object *element = NULL;
  275. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  276. u32 i = 0;
  277. if (!list) {
  278. return AE_BAD_PARAMETER;
  279. }
  280. /* Evaluate object. */
  281. status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
  282. if (ACPI_FAILURE(status))
  283. goto end;
  284. package = buffer.pointer;
  285. if ((buffer.length == 0) || !package) {
  286. printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
  287. (unsigned)buffer.length, package);
  288. status = AE_BAD_DATA;
  289. acpi_util_eval_error(handle, pathname, status);
  290. goto end;
  291. }
  292. if (package->type != ACPI_TYPE_PACKAGE) {
  293. printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
  294. package->type);
  295. status = AE_BAD_DATA;
  296. acpi_util_eval_error(handle, pathname, status);
  297. goto end;
  298. }
  299. if (!package->package.count) {
  300. printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
  301. package);
  302. status = AE_BAD_DATA;
  303. acpi_util_eval_error(handle, pathname, status);
  304. goto end;
  305. }
  306. if (package->package.count > ACPI_MAX_HANDLES) {
  307. return AE_NO_MEMORY;
  308. }
  309. list->count = package->package.count;
  310. /* Extract package data. */
  311. for (i = 0; i < list->count; i++) {
  312. element = &(package->package.elements[i]);
  313. if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
  314. status = AE_BAD_DATA;
  315. printk(KERN_ERR PREFIX
  316. "Expecting a [Reference] package element, found type %X\n",
  317. element->type);
  318. acpi_util_eval_error(handle, pathname, status);
  319. break;
  320. }
  321. if (!element->reference.handle) {
  322. printk(KERN_WARNING PREFIX "Invalid reference in"
  323. " package %s\n", pathname);
  324. status = AE_NULL_ENTRY;
  325. break;
  326. }
  327. /* Get the acpi_handle. */
  328. list->handles[i] = element->reference.handle;
  329. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
  330. list->handles[i]));
  331. }
  332. end:
  333. if (ACPI_FAILURE(status)) {
  334. list->count = 0;
  335. //kfree(list->handles);
  336. }
  337. kfree(buffer.pointer);
  338. return status;
  339. }
  340. EXPORT_SYMBOL(acpi_evaluate_reference);
  341. acpi_status
  342. acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
  343. {
  344. acpi_status status;
  345. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  346. union acpi_object *output;
  347. status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
  348. if (ACPI_FAILURE(status))
  349. return status;
  350. output = buffer.pointer;
  351. if (!output || output->type != ACPI_TYPE_PACKAGE
  352. || !output->package.count
  353. || output->package.elements[0].type != ACPI_TYPE_BUFFER
  354. || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
  355. status = AE_TYPE;
  356. goto out;
  357. }
  358. status = acpi_decode_pld_buffer(
  359. output->package.elements[0].buffer.pointer,
  360. output->package.elements[0].buffer.length,
  361. pld);
  362. out:
  363. kfree(buffer.pointer);
  364. return status;
  365. }
  366. EXPORT_SYMBOL(acpi_get_physical_device_location);
  367. /**
  368. * acpi_evaluate_hotplug_ost: Evaluate _OST for hotplug operations
  369. * @handle: ACPI device handle
  370. * @source_event: source event code
  371. * @status_code: status code
  372. * @status_buf: optional detailed information (NULL if none)
  373. *
  374. * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
  375. * must call this function when evaluating _OST for hotplug operations.
  376. * When the platform does not support _OST, this function has no effect.
  377. */
  378. acpi_status
  379. acpi_evaluate_hotplug_ost(acpi_handle handle, u32 source_event,
  380. u32 status_code, struct acpi_buffer *status_buf)
  381. {
  382. #ifdef ACPI_HOTPLUG_OST
  383. union acpi_object params[3] = {
  384. {.type = ACPI_TYPE_INTEGER,},
  385. {.type = ACPI_TYPE_INTEGER,},
  386. {.type = ACPI_TYPE_BUFFER,}
  387. };
  388. struct acpi_object_list arg_list = {3, params};
  389. acpi_status status;
  390. params[0].integer.value = source_event;
  391. params[1].integer.value = status_code;
  392. if (status_buf != NULL) {
  393. params[2].buffer.pointer = status_buf->pointer;
  394. params[2].buffer.length = status_buf->length;
  395. } else {
  396. params[2].buffer.pointer = NULL;
  397. params[2].buffer.length = 0;
  398. }
  399. status = acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
  400. return status;
  401. #else
  402. return AE_OK;
  403. #endif
  404. }
  405. EXPORT_SYMBOL(acpi_evaluate_hotplug_ost);
  406. /**
  407. * acpi_handle_printk: Print message with ACPI prefix and object path
  408. *
  409. * This function is called through acpi_handle_<level> macros and prints
  410. * a message with ACPI prefix and object path. This function acquires
  411. * the global namespace mutex to obtain an object path. In interrupt
  412. * context, it shows the object path as <n/a>.
  413. */
  414. void
  415. acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
  416. {
  417. struct va_format vaf;
  418. va_list args;
  419. struct acpi_buffer buffer = {
  420. .length = ACPI_ALLOCATE_BUFFER,
  421. .pointer = NULL
  422. };
  423. const char *path;
  424. va_start(args, fmt);
  425. vaf.fmt = fmt;
  426. vaf.va = &args;
  427. if (in_interrupt() ||
  428. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
  429. path = "<n/a>";
  430. else
  431. path = buffer.pointer;
  432. printk("%sACPI: %s: %pV", level, path, &vaf);
  433. va_end(args);
  434. kfree(buffer.pointer);
  435. }
  436. EXPORT_SYMBOL(acpi_handle_printk);
  437. /**
  438. * acpi_has_method: Check whether @handle has a method named @name
  439. * @handle: ACPI device handle
  440. * @name: name of object or method
  441. *
  442. * Check whether @handle has a method named @name.
  443. */
  444. bool acpi_has_method(acpi_handle handle, char *name)
  445. {
  446. acpi_handle tmp;
  447. return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
  448. }
  449. EXPORT_SYMBOL(acpi_has_method);
  450. acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
  451. u64 arg)
  452. {
  453. union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
  454. struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
  455. obj.integer.value = arg;
  456. return acpi_evaluate_object(handle, method, &arg_list, NULL);
  457. }
  458. EXPORT_SYMBOL(acpi_execute_simple_method);
  459. /**
  460. * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
  461. * @handle: ACPI device handle
  462. *
  463. * Evaluate device's _EJ0 method for hotplug operations.
  464. */
  465. acpi_status acpi_evaluate_ej0(acpi_handle handle)
  466. {
  467. acpi_status status;
  468. status = acpi_execute_simple_method(handle, "_EJ0", 1);
  469. if (status == AE_NOT_FOUND)
  470. acpi_handle_warn(handle, "No _EJ0 support for device\n");
  471. else if (ACPI_FAILURE(status))
  472. acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
  473. return status;
  474. }
  475. /**
  476. * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
  477. * @handle: ACPI device handle
  478. * @lock: lock device if non-zero, otherwise unlock device
  479. *
  480. * Evaluate device's _LCK method if present to lock/unlock device
  481. */
  482. acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
  483. {
  484. acpi_status status;
  485. status = acpi_execute_simple_method(handle, "_LCK", !!lock);
  486. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  487. if (lock)
  488. acpi_handle_warn(handle,
  489. "Locking device failed (0x%x)\n", status);
  490. else
  491. acpi_handle_warn(handle,
  492. "Unlocking device failed (0x%x)\n", status);
  493. }
  494. return status;
  495. }