blob: dd8ff63ee2b43e255313f973a668e33e5a630ac3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
26#include <linux/kernel.h>
27#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/init.h>
30#include <linux/types.h>
Toshi Kanifbfddae2012-11-21 01:36:28 +000031#include <linux/hardirq.h>
32#include <linux/acpi.h>
Bjørn Mork45fef5b2014-05-22 12:47:47 +020033#include <linux/dynamic_debug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Len Browna192a952009-07-28 16:45:54 -040035#include "internal.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050038ACPI_MODULE_NAME("utils");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* --------------------------------------------------------------------------
41 Object Evaluation Helpers
42 -------------------------------------------------------------------------- */
Harvey Harrison4fd7f512008-02-15 17:07:19 -080043static void
44acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
45{
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#ifdef ACPI_DEBUG_OUTPUT
Harvey Harrison4fd7f512008-02-15 17:07:19 -080047 char prefix[80] = {'\0'};
48 struct acpi_buffer buffer = {sizeof(prefix), prefix};
49 acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);
50 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",
51 (char *) prefix, p, acpi_format_exception(s)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#else
Harvey Harrison4fd7f512008-02-15 17:07:19 -080053 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#endif
Harvey Harrison4fd7f512008-02-15 17:07:19 -080055}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040058acpi_extract_package(union acpi_object *package,
59 struct acpi_buffer *format, struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Len Brown4be44fc2005-08-05 00:44:28 -040061 u32 size_required = 0;
62 u32 tail_offset = 0;
63 char *format_string = NULL;
64 u32 format_count = 0;
65 u32 i = 0;
66 u8 *head = NULL;
67 u8 *tail = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Len Brown4be44fc2005-08-05 00:44:28 -040070 if (!package || (package->type != ACPI_TYPE_PACKAGE)
71 || (package->package.count < 1)) {
Len Browncece9292006-06-26 23:04:31 -040072 printk(KERN_WARNING PREFIX "Invalid package argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040073 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
75
76 if (!format || !format->pointer || (format->length < 1)) {
Len Browncece9292006-06-26 23:04:31 -040077 printk(KERN_WARNING PREFIX "Invalid format argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040078 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 }
80
81 if (!buffer) {
Len Browncece9292006-06-26 23:04:31 -040082 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040083 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85
Len Brown4be44fc2005-08-05 00:44:28 -040086 format_count = (format->length / sizeof(char)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (format_count > package->package.count) {
Len Browncece9292006-06-26 23:04:31 -040088 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
89 " than exist in package [%d].\n",
90 format_count, package->package.count);
Patrick Mocheld550d982006-06-27 00:41:40 -040091 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
93
Jan Engelhardt50dd0962006-10-01 00:28:50 +020094 format_string = format->pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 /*
97 * Calculate size_required.
98 */
Len Brown4be44fc2005-08-05 00:44:28 -040099 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 union acpi_object *element = &(package->package.elements[i]);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 switch (element->type) {
104
105 case ACPI_TYPE_INTEGER:
106 switch (format_string[i]) {
107 case 'N':
Lin Ming439913f2010-01-28 10:53:19 +0800108 size_required += sizeof(u64);
109 tail_offset += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 break;
111 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400112 size_required +=
Lin Ming439913f2010-01-28 10:53:19 +0800113 sizeof(char *) + sizeof(u64) +
Len Brown4be44fc2005-08-05 00:44:28 -0400114 sizeof(char);
115 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 break;
117 default:
Len Browncece9292006-06-26 23:04:31 -0400118 printk(KERN_WARNING PREFIX "Invalid package element"
Colin Ian Kinge7e92ec92013-10-29 10:34:19 +0000119 " [%d]: got number, expecting"
Len Browncece9292006-06-26 23:04:31 -0400120 " [%c]\n",
121 i, format_string[i]);
Patrick Mocheld550d982006-06-27 00:41:40 -0400122 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 break;
124 }
125 break;
126
127 case ACPI_TYPE_STRING:
128 case ACPI_TYPE_BUFFER:
129 switch (format_string[i]) {
130 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400131 size_required +=
132 sizeof(char *) +
133 (element->string.length * sizeof(char)) +
134 sizeof(char);
135 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 break;
137 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400138 size_required +=
Fabian Frederickd93de342014-11-16 10:57:00 +0100139 sizeof(u8 *) + element->buffer.length;
Len Brown4be44fc2005-08-05 00:44:28 -0400140 tail_offset += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142 default:
Len Browncece9292006-06-26 23:04:31 -0400143 printk(KERN_WARNING PREFIX "Invalid package element"
Thomas Renningera6fc6722006-06-26 23:58:43 -0400144 " [%d] got string/buffer,"
Colin Ian Kinge7e92ec92013-10-29 10:34:19 +0000145 " expecting [%c]\n",
Len Browncece9292006-06-26 23:04:31 -0400146 i, format_string[i]);
Patrick Mocheld550d982006-06-27 00:41:40 -0400147 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 break;
149 }
150 break;
Zhang Ruie3ec4832014-03-16 21:34:16 +0800151 case ACPI_TYPE_LOCAL_REFERENCE:
152 switch (format_string[i]) {
153 case 'R':
154 size_required += sizeof(void *);
155 tail_offset += sizeof(void *);
156 break;
157 default:
158 printk(KERN_WARNING PREFIX "Invalid package element"
159 " [%d] got reference,"
160 " expecting [%c]\n",
161 i, format_string[i]);
162 return AE_BAD_DATA;
163 break;
164 }
165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 case ACPI_TYPE_PACKAGE:
168 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400169 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
170 "Found unsupported element at index=%d\n",
171 i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* TBD: handle nested packages... */
Patrick Mocheld550d982006-06-27 00:41:40 -0400173 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 break;
175 }
176 }
177
178 /*
179 * Validate output buffer.
180 */
Al Stonee83dda02013-10-09 14:21:10 -0600181 if (buffer->length == ACPI_ALLOCATE_BUFFER) {
jhbird.choi@samsung.com2d0acb42014-03-20 16:35:56 +0900182 buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required);
Al Stonee83dda02013-10-09 14:21:10 -0600183 if (!buffer->pointer)
184 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 buffer->length = size_required;
Al Stonee83dda02013-10-09 14:21:10 -0600186 } else {
187 if (buffer->length < size_required) {
188 buffer->length = size_required;
189 return AE_BUFFER_OVERFLOW;
190 } else if (buffer->length != size_required ||
191 !buffer->pointer) {
192 return AE_BAD_PARAMETER;
193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195
196 head = buffer->pointer;
197 tail = buffer->pointer + tail_offset;
198
199 /*
200 * Extract package data.
201 */
Len Brown4be44fc2005-08-05 00:44:28 -0400202 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 u8 **pointer = NULL;
205 union acpi_object *element = &(package->package.elements[i]);
206
207 if (!element) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400208 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
211 switch (element->type) {
212
213 case ACPI_TYPE_INTEGER:
214 switch (format_string[i]) {
215 case 'N':
Lin Ming439913f2010-01-28 10:53:19 +0800216 *((u64 *) head) =
Len Brown4be44fc2005-08-05 00:44:28 -0400217 element->integer.value;
Lin Ming439913f2010-01-28 10:53:19 +0800218 head += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 break;
220 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400221 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *pointer = tail;
Lin Ming439913f2010-01-28 10:53:19 +0800223 *((u64 *) tail) =
Len Brown4be44fc2005-08-05 00:44:28 -0400224 element->integer.value;
Lin Ming439913f2010-01-28 10:53:19 +0800225 head += sizeof(u64 *);
226 tail += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* NULL terminate string */
228 *tail = (char)0;
229 tail += sizeof(char);
230 break;
231 default:
232 /* Should never get here */
233 break;
234 }
235 break;
236
237 case ACPI_TYPE_STRING:
238 case ACPI_TYPE_BUFFER:
239 switch (format_string[i]) {
240 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400241 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400243 memcpy(tail, element->string.pointer,
244 element->string.length);
245 head += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 tail += element->string.length * sizeof(char);
247 /* NULL terminate string */
248 *tail = (char)0;
249 tail += sizeof(char);
250 break;
251 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400252 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400254 memcpy(tail, element->buffer.pointer,
255 element->buffer.length);
256 head += sizeof(u8 *);
Fabian Frederickd93de342014-11-16 10:57:00 +0100257 tail += element->buffer.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 break;
259 default:
260 /* Should never get here */
261 break;
262 }
263 break;
Zhang Ruie3ec4832014-03-16 21:34:16 +0800264 case ACPI_TYPE_LOCAL_REFERENCE:
265 switch (format_string[i]) {
266 case 'R':
267 *(void **)head =
268 (void *)element->reference.handle;
269 head += sizeof(void *);
270 break;
271 default:
272 /* Should never get here */
273 break;
274 }
275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 case ACPI_TYPE_PACKAGE:
277 /* TBD: handle nested packages... */
278 default:
279 /* Should never get here */
280 break;
281 }
282 }
283
Patrick Mocheld550d982006-06-27 00:41:40 -0400284 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
Len Brown4be44fc2005-08-05 00:44:28 -0400286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287EXPORT_SYMBOL(acpi_extract_package);
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400290acpi_evaluate_integer(acpi_handle handle,
291 acpi_string pathname,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400292 struct acpi_object_list *arguments, unsigned long long *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Len Brown4be44fc2005-08-05 00:44:28 -0400294 acpi_status status = AE_OK;
Pavel Machek40599072008-11-25 12:05:08 +0100295 union acpi_object element;
Len Brown4be44fc2005-08-05 00:44:28 -0400296 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400299 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 buffer.length = sizeof(union acpi_object);
Pavel Machek40599072008-11-25 12:05:08 +0100302 buffer.pointer = &element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
304 if (ACPI_FAILURE(status)) {
305 acpi_util_eval_error(handle, pathname, status);
Patrick Mocheld550d982006-06-27 00:41:40 -0400306 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
Pavel Machek40599072008-11-25 12:05:08 +0100309 if (element.type != ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
Patrick Mocheld550d982006-06-27 00:41:40 -0400311 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
Pavel Machek40599072008-11-25 12:05:08 +0100314 *data = element.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Matthew Wilcox27663c52008-10-10 02:22:59 -0400316 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Patrick Mocheld550d982006-06-27 00:41:40 -0400318 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Len Brown4be44fc2005-08-05 00:44:28 -0400321EXPORT_SYMBOL(acpi_evaluate_integer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400324acpi_evaluate_reference(acpi_handle handle,
325 acpi_string pathname,
326 struct acpi_object_list *arguments,
327 struct acpi_handle_list *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Len Brown4be44fc2005-08-05 00:44:28 -0400329 acpi_status status = AE_OK;
330 union acpi_object *package = NULL;
331 union acpi_object *element = NULL;
332 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
333 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 if (!list) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400337 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
340 /* Evaluate object. */
341
342 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
343 if (ACPI_FAILURE(status))
344 goto end;
345
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200346 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if ((buffer.length == 0) || !package) {
Len Brown64684632006-06-26 23:41:38 -0400349 printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
350 (unsigned)buffer.length, package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 status = AE_BAD_DATA;
352 acpi_util_eval_error(handle, pathname, status);
353 goto end;
354 }
355 if (package->type != ACPI_TYPE_PACKAGE) {
Len Brown64684632006-06-26 23:41:38 -0400356 printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
357 package->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 status = AE_BAD_DATA;
359 acpi_util_eval_error(handle, pathname, status);
360 goto end;
361 }
362 if (!package->package.count) {
Len Brown64684632006-06-26 23:41:38 -0400363 printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
364 package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 status = AE_BAD_DATA;
366 acpi_util_eval_error(handle, pathname, status);
367 goto end;
368 }
369
370 if (package->package.count > ACPI_MAX_HANDLES) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400371 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 list->count = package->package.count;
374
375 /* Extract package data. */
376
377 for (i = 0; i < list->count; i++) {
378
379 element = &(package->package.elements[i]);
380
Bob Moorecd0b2242008-04-10 19:06:43 +0400381 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 status = AE_BAD_DATA;
Len Brown64684632006-06-26 23:41:38 -0400383 printk(KERN_ERR PREFIX
384 "Expecting a [Reference] package element, found type %X\n",
385 element->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 acpi_util_eval_error(handle, pathname, status);
387 break;
388 }
389
Thomas Renningerb6a16382008-03-12 01:06:24 +0100390 if (!element->reference.handle) {
391 printk(KERN_WARNING PREFIX "Invalid reference in"
392 " package %s\n", pathname);
393 status = AE_NULL_ENTRY;
394 break;
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* Get the acpi_handle. */
397
398 list->handles[i] = element->reference.handle;
399 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400400 list->handles[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
Len Brown4be44fc2005-08-05 00:44:28 -0400403 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (ACPI_FAILURE(status)) {
405 list->count = 0;
406 //kfree(list->handles);
407 }
408
Len Brown02438d82006-06-30 03:19:10 -0400409 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Patrick Mocheld550d982006-06-27 00:41:40 -0400411 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Len Brown4be44fc2005-08-05 00:44:28 -0400414EXPORT_SYMBOL(acpi_evaluate_reference);
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800415
416acpi_status
Feng Tang8ede06a2012-08-21 09:56:58 +0800417acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800418{
419 acpi_status status;
420 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
421 union acpi_object *output;
422
423 status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
424
425 if (ACPI_FAILURE(status))
426 return status;
427
428 output = buffer.pointer;
429
430 if (!output || output->type != ACPI_TYPE_PACKAGE
431 || !output->package.count
432 || output->package.elements[0].type != ACPI_TYPE_BUFFER
Feng Tang8ede06a2012-08-21 09:56:58 +0800433 || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800434 status = AE_TYPE;
435 goto out;
436 }
437
Feng Tang8ede06a2012-08-21 09:56:58 +0800438 status = acpi_decode_pld_buffer(
439 output->package.elements[0].buffer.pointer,
440 output->package.elements[0].buffer.length,
441 pld);
442
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800443out:
444 kfree(buffer.pointer);
445 return status;
446}
447EXPORT_SYMBOL(acpi_get_physical_device_location);
Toshi Kani275c58d2012-05-23 20:25:19 -0600448
449/**
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100450 * acpi_evaluate_ost: Evaluate _OST for hotplug operations
Toshi Kani275c58d2012-05-23 20:25:19 -0600451 * @handle: ACPI device handle
452 * @source_event: source event code
453 * @status_code: status code
454 * @status_buf: optional detailed information (NULL if none)
455 *
456 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
457 * must call this function when evaluating _OST for hotplug operations.
458 * When the platform does not support _OST, this function has no effect.
459 */
460acpi_status
Jiang Liu05730c12014-02-19 14:02:15 +0800461acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
462 struct acpi_buffer *status_buf)
Toshi Kani275c58d2012-05-23 20:25:19 -0600463{
Toshi Kani275c58d2012-05-23 20:25:19 -0600464 union acpi_object params[3] = {
465 {.type = ACPI_TYPE_INTEGER,},
466 {.type = ACPI_TYPE_INTEGER,},
467 {.type = ACPI_TYPE_BUFFER,}
468 };
469 struct acpi_object_list arg_list = {3, params};
Toshi Kani275c58d2012-05-23 20:25:19 -0600470
471 params[0].integer.value = source_event;
472 params[1].integer.value = status_code;
473 if (status_buf != NULL) {
474 params[2].buffer.pointer = status_buf->pointer;
475 params[2].buffer.length = status_buf->length;
476 } else {
477 params[2].buffer.pointer = NULL;
478 params[2].buffer.length = 0;
479 }
480
Jiang Liu05730c12014-02-19 14:02:15 +0800481 return acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
Toshi Kani275c58d2012-05-23 20:25:19 -0600482}
Jiang Liu05730c12014-02-19 14:02:15 +0800483EXPORT_SYMBOL(acpi_evaluate_ost);
Toshi Kanifbfddae2012-11-21 01:36:28 +0000484
485/**
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200486 * acpi_handle_path: Return the object path of handle
487 *
488 * Caller must free the returned buffer
489 */
490static char *acpi_handle_path(acpi_handle handle)
491{
492 struct acpi_buffer buffer = {
493 .length = ACPI_ALLOCATE_BUFFER,
494 .pointer = NULL
495 };
496
497 if (in_interrupt() ||
498 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
499 return NULL;
500 return buffer.pointer;
501}
502
503/**
Toshi Kanifbfddae2012-11-21 01:36:28 +0000504 * acpi_handle_printk: Print message with ACPI prefix and object path
505 *
506 * This function is called through acpi_handle_<level> macros and prints
507 * a message with ACPI prefix and object path. This function acquires
508 * the global namespace mutex to obtain an object path. In interrupt
509 * context, it shows the object path as <n/a>.
510 */
511void
512acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
513{
514 struct va_format vaf;
515 va_list args;
Toshi Kanifbfddae2012-11-21 01:36:28 +0000516 const char *path;
517
518 va_start(args, fmt);
519 vaf.fmt = fmt;
520 vaf.va = &args;
521
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200522 path = acpi_handle_path(handle);
523 printk("%sACPI: %s: %pV", level, path ? path : "<n/a>" , &vaf);
Toshi Kanifbfddae2012-11-21 01:36:28 +0000524
525 va_end(args);
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200526 kfree(path);
Toshi Kanifbfddae2012-11-21 01:36:28 +0000527}
528EXPORT_SYMBOL(acpi_handle_printk);
Jiang Liu952c63e2013-06-29 00:24:38 +0800529
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200530#if defined(CONFIG_DYNAMIC_DEBUG)
531/**
532 * __acpi_handle_debug: pr_debug with ACPI prefix and object path
533 *
534 * This function is called through acpi_handle_debug macro and debug
535 * prints a message with ACPI prefix and object path. This function
536 * acquires the global namespace mutex to obtain an object path. In
537 * interrupt context, it shows the object path as <n/a>.
538 */
539void
540__acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle,
541 const char *fmt, ...)
542{
543 struct va_format vaf;
544 va_list args;
545 const char *path;
546
547 va_start(args, fmt);
548 vaf.fmt = fmt;
549 vaf.va = &args;
550
551 path = acpi_handle_path(handle);
552 __dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf);
553
554 va_end(args);
555 kfree(path);
556}
557EXPORT_SYMBOL(__acpi_handle_debug);
558#endif
559
Jiang Liu952c63e2013-06-29 00:24:38 +0800560/**
561 * acpi_has_method: Check whether @handle has a method named @name
562 * @handle: ACPI device handle
563 * @name: name of object or method
564 *
565 * Check whether @handle has a method named @name.
566 */
567bool acpi_has_method(acpi_handle handle, char *name)
568{
569 acpi_handle tmp;
570
571 return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
572}
573EXPORT_SYMBOL(acpi_has_method);
Jiang Liu0db98202013-06-29 00:24:39 +0800574
575acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
576 u64 arg)
577{
578 union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
579 struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
580
581 obj.integer.value = arg;
582
583 return acpi_evaluate_object(handle, method, &arg_list, NULL);
584}
585EXPORT_SYMBOL(acpi_execute_simple_method);
Jiang Liu7d2421f2013-06-29 00:24:40 +0800586
587/**
588 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
589 * @handle: ACPI device handle
590 *
591 * Evaluate device's _EJ0 method for hotplug operations.
592 */
593acpi_status acpi_evaluate_ej0(acpi_handle handle)
594{
595 acpi_status status;
596
597 status = acpi_execute_simple_method(handle, "_EJ0", 1);
598 if (status == AE_NOT_FOUND)
599 acpi_handle_warn(handle, "No _EJ0 support for device\n");
600 else if (ACPI_FAILURE(status))
601 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
602
603 return status;
604}
605
606/**
607 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
608 * @handle: ACPI device handle
609 * @lock: lock device if non-zero, otherwise unlock device
610 *
611 * Evaluate device's _LCK method if present to lock/unlock device
612 */
613acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
614{
615 acpi_status status;
616
617 status = acpi_execute_simple_method(handle, "_LCK", !!lock);
618 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
619 if (lock)
620 acpi_handle_warn(handle,
621 "Locking device failed (0x%x)\n", status);
622 else
623 acpi_handle_warn(handle,
624 "Unlocking device failed (0x%x)\n", status);
625 }
626
627 return status;
628}
Jiang Liua65ac522013-12-19 20:38:10 +0800629
630/**
631 * acpi_evaluate_dsm - evaluate device's _DSM method
632 * @handle: ACPI device handle
633 * @uuid: UUID of requested functions, should be 16 bytes
634 * @rev: revision number of requested function
635 * @func: requested function number
636 * @argv4: the function specific parameter
637 *
638 * Evaluate device's _DSM method with specified UUID, revision id and
639 * function number. Caller needs to free the returned object.
640 *
641 * Though ACPI defines the fourth parameter for _DSM should be a package,
642 * some old BIOSes do expect a buffer or an integer etc.
643 */
644union acpi_object *
645acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
646 union acpi_object *argv4)
647{
648 acpi_status ret;
649 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
650 union acpi_object params[4];
651 struct acpi_object_list input = {
652 .count = 4,
653 .pointer = params,
654 };
655
656 params[0].type = ACPI_TYPE_BUFFER;
657 params[0].buffer.length = 16;
658 params[0].buffer.pointer = (char *)uuid;
659 params[1].type = ACPI_TYPE_INTEGER;
660 params[1].integer.value = rev;
661 params[2].type = ACPI_TYPE_INTEGER;
662 params[2].integer.value = func;
663 if (argv4) {
664 params[3] = *argv4;
665 } else {
666 params[3].type = ACPI_TYPE_PACKAGE;
667 params[3].package.count = 0;
668 params[3].package.elements = NULL;
669 }
670
671 ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
672 if (ACPI_SUCCESS(ret))
673 return (union acpi_object *)buf.pointer;
674
675 if (ret != AE_NOT_FOUND)
676 acpi_handle_warn(handle,
677 "failed to evaluate _DSM (0x%x)\n", ret);
678
679 return NULL;
680}
681EXPORT_SYMBOL(acpi_evaluate_dsm);
682
683/**
684 * acpi_check_dsm - check if _DSM method supports requested functions.
685 * @handle: ACPI device handle
686 * @uuid: UUID of requested functions, should be 16 bytes at least
687 * @rev: revision number of requested functions
688 * @funcs: bitmap of requested functions
Jiang Liua65ac522013-12-19 20:38:10 +0800689 *
690 * Evaluate device's _DSM method to check whether it supports requested
691 * functions. Currently only support 64 functions at maximum, should be
692 * enough for now.
693 */
694bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
695{
696 int i;
697 u64 mask = 0;
698 union acpi_object *obj;
699
700 if (funcs == 0)
701 return false;
702
703 obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
704 if (!obj)
705 return false;
706
707 /* For compatibility, old BIOSes may return an integer */
708 if (obj->type == ACPI_TYPE_INTEGER)
709 mask = obj->integer.value;
710 else if (obj->type == ACPI_TYPE_BUFFER)
711 for (i = 0; i < obj->buffer.length && i < 8; i++)
712 mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
713 ACPI_FREE(obj);
714
715 /*
716 * Bit 0 indicates whether there's support for any functions other than
717 * function 0 for the specified UUID and revision.
718 */
719 if ((mask & 0x1) && (mask & funcs) == funcs)
720 return true;
721
722 return false;
723}
724EXPORT_SYMBOL(acpi_check_dsm);