blob: 5512309d167bddfa930ea384aca6aa7661ce96d7 [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 +=
139 sizeof(u8 *) +
140 (element->buffer.length * sizeof(u8));
141 tail_offset += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 break;
143 default:
Len Browncece9292006-06-26 23:04:31 -0400144 printk(KERN_WARNING PREFIX "Invalid package element"
Thomas Renningera6fc6722006-06-26 23:58:43 -0400145 " [%d] got string/buffer,"
Colin Ian Kinge7e92ec92013-10-29 10:34:19 +0000146 " expecting [%c]\n",
Len Browncece9292006-06-26 23:04:31 -0400147 i, format_string[i]);
Patrick Mocheld550d982006-06-27 00:41:40 -0400148 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 break;
150 }
151 break;
Zhang Ruie3ec4832014-03-16 21:34:16 +0800152 case ACPI_TYPE_LOCAL_REFERENCE:
153 switch (format_string[i]) {
154 case 'R':
155 size_required += sizeof(void *);
156 tail_offset += sizeof(void *);
157 break;
158 default:
159 printk(KERN_WARNING PREFIX "Invalid package element"
160 " [%d] got reference,"
161 " expecting [%c]\n",
162 i, format_string[i]);
163 return AE_BAD_DATA;
164 break;
165 }
166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 case ACPI_TYPE_PACKAGE:
169 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400170 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
171 "Found unsupported element at index=%d\n",
172 i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /* TBD: handle nested packages... */
Patrick Mocheld550d982006-06-27 00:41:40 -0400174 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 break;
176 }
177 }
178
179 /*
180 * Validate output buffer.
181 */
Al Stonee83dda02013-10-09 14:21:10 -0600182 if (buffer->length == ACPI_ALLOCATE_BUFFER) {
jhbird.choi@samsung.com2d0acb42014-03-20 16:35:56 +0900183 buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required);
Al Stonee83dda02013-10-09 14:21:10 -0600184 if (!buffer->pointer)
185 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 buffer->length = size_required;
Al Stonee83dda02013-10-09 14:21:10 -0600187 } else {
188 if (buffer->length < size_required) {
189 buffer->length = size_required;
190 return AE_BUFFER_OVERFLOW;
191 } else if (buffer->length != size_required ||
192 !buffer->pointer) {
193 return AE_BAD_PARAMETER;
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196
197 head = buffer->pointer;
198 tail = buffer->pointer + tail_offset;
199
200 /*
201 * Extract package data.
202 */
Len Brown4be44fc2005-08-05 00:44:28 -0400203 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 u8 **pointer = NULL;
206 union acpi_object *element = &(package->package.elements[i]);
207
208 if (!element) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400209 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212 switch (element->type) {
213
214 case ACPI_TYPE_INTEGER:
215 switch (format_string[i]) {
216 case 'N':
Lin Ming439913f2010-01-28 10:53:19 +0800217 *((u64 *) head) =
Len Brown4be44fc2005-08-05 00:44:28 -0400218 element->integer.value;
Lin Ming439913f2010-01-28 10:53:19 +0800219 head += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 break;
221 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400222 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 *pointer = tail;
Lin Ming439913f2010-01-28 10:53:19 +0800224 *((u64 *) tail) =
Len Brown4be44fc2005-08-05 00:44:28 -0400225 element->integer.value;
Lin Ming439913f2010-01-28 10:53:19 +0800226 head += sizeof(u64 *);
227 tail += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* NULL terminate string */
229 *tail = (char)0;
230 tail += sizeof(char);
231 break;
232 default:
233 /* Should never get here */
234 break;
235 }
236 break;
237
238 case ACPI_TYPE_STRING:
239 case ACPI_TYPE_BUFFER:
240 switch (format_string[i]) {
241 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400242 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400244 memcpy(tail, element->string.pointer,
245 element->string.length);
246 head += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 tail += element->string.length * sizeof(char);
248 /* NULL terminate string */
249 *tail = (char)0;
250 tail += sizeof(char);
251 break;
252 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400253 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400255 memcpy(tail, element->buffer.pointer,
256 element->buffer.length);
257 head += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 tail += element->buffer.length * sizeof(u8);
259 break;
260 default:
261 /* Should never get here */
262 break;
263 }
264 break;
Zhang Ruie3ec4832014-03-16 21:34:16 +0800265 case ACPI_TYPE_LOCAL_REFERENCE:
266 switch (format_string[i]) {
267 case 'R':
268 *(void **)head =
269 (void *)element->reference.handle;
270 head += sizeof(void *);
271 break;
272 default:
273 /* Should never get here */
274 break;
275 }
276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 case ACPI_TYPE_PACKAGE:
278 /* TBD: handle nested packages... */
279 default:
280 /* Should never get here */
281 break;
282 }
283 }
284
Patrick Mocheld550d982006-06-27 00:41:40 -0400285 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
Len Brown4be44fc2005-08-05 00:44:28 -0400287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288EXPORT_SYMBOL(acpi_extract_package);
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400291acpi_evaluate_integer(acpi_handle handle,
292 acpi_string pathname,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400293 struct acpi_object_list *arguments, unsigned long long *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Len Brown4be44fc2005-08-05 00:44:28 -0400295 acpi_status status = AE_OK;
Pavel Machek40599072008-11-25 12:05:08 +0100296 union acpi_object element;
Len Brown4be44fc2005-08-05 00:44:28 -0400297 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400300 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 buffer.length = sizeof(union acpi_object);
Pavel Machek40599072008-11-25 12:05:08 +0100303 buffer.pointer = &element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
305 if (ACPI_FAILURE(status)) {
306 acpi_util_eval_error(handle, pathname, status);
Patrick Mocheld550d982006-06-27 00:41:40 -0400307 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
Pavel Machek40599072008-11-25 12:05:08 +0100310 if (element.type != ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
Patrick Mocheld550d982006-06-27 00:41:40 -0400312 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
Pavel Machek40599072008-11-25 12:05:08 +0100315 *data = element.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Matthew Wilcox27663c52008-10-10 02:22:59 -0400317 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Patrick Mocheld550d982006-06-27 00:41:40 -0400319 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Len Brown4be44fc2005-08-05 00:44:28 -0400322EXPORT_SYMBOL(acpi_evaluate_integer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400325acpi_evaluate_reference(acpi_handle handle,
326 acpi_string pathname,
327 struct acpi_object_list *arguments,
328 struct acpi_handle_list *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Len Brown4be44fc2005-08-05 00:44:28 -0400330 acpi_status status = AE_OK;
331 union acpi_object *package = NULL;
332 union acpi_object *element = NULL;
333 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
334 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 if (!list) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400338 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
341 /* Evaluate object. */
342
343 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
344 if (ACPI_FAILURE(status))
345 goto end;
346
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200347 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 if ((buffer.length == 0) || !package) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 status = AE_BAD_DATA;
351 acpi_util_eval_error(handle, pathname, status);
352 goto end;
353 }
354 if (package->type != ACPI_TYPE_PACKAGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 status = AE_BAD_DATA;
356 acpi_util_eval_error(handle, pathname, status);
357 goto end;
358 }
359 if (!package->package.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 status = AE_BAD_DATA;
361 acpi_util_eval_error(handle, pathname, status);
362 goto end;
363 }
364
365 if (package->package.count > ACPI_MAX_HANDLES) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400366 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 list->count = package->package.count;
369
370 /* Extract package data. */
371
372 for (i = 0; i < list->count; i++) {
373
374 element = &(package->package.elements[i]);
375
Bob Moorecd0b2242008-04-10 19:06:43 +0400376 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 status = AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 acpi_util_eval_error(handle, pathname, status);
379 break;
380 }
381
Thomas Renningerb6a16382008-03-12 01:06:24 +0100382 if (!element->reference.handle) {
Thomas Renningerb6a16382008-03-12 01:06:24 +0100383 status = AE_NULL_ENTRY;
Rafael J. Wysockic48cf1b2014-12-12 22:51:01 +0100384 acpi_util_eval_error(handle, pathname, status);
Thomas Renningerb6a16382008-03-12 01:06:24 +0100385 break;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 /* Get the acpi_handle. */
388
389 list->handles[i] = element->reference.handle;
390 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400391 list->handles[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Len Brown4be44fc2005-08-05 00:44:28 -0400394 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (ACPI_FAILURE(status)) {
396 list->count = 0;
397 //kfree(list->handles);
398 }
399
Len Brown02438d82006-06-30 03:19:10 -0400400 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Patrick Mocheld550d982006-06-27 00:41:40 -0400402 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Len Brown4be44fc2005-08-05 00:44:28 -0400405EXPORT_SYMBOL(acpi_evaluate_reference);
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800406
407acpi_status
Feng Tang8ede06a2012-08-21 09:56:58 +0800408acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800409{
410 acpi_status status;
411 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
412 union acpi_object *output;
413
414 status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
415
416 if (ACPI_FAILURE(status))
417 return status;
418
419 output = buffer.pointer;
420
421 if (!output || output->type != ACPI_TYPE_PACKAGE
422 || !output->package.count
423 || output->package.elements[0].type != ACPI_TYPE_BUFFER
Feng Tang8ede06a2012-08-21 09:56:58 +0800424 || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800425 status = AE_TYPE;
426 goto out;
427 }
428
Feng Tang8ede06a2012-08-21 09:56:58 +0800429 status = acpi_decode_pld_buffer(
430 output->package.elements[0].buffer.pointer,
431 output->package.elements[0].buffer.length,
432 pld);
433
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800434out:
435 kfree(buffer.pointer);
436 return status;
437}
438EXPORT_SYMBOL(acpi_get_physical_device_location);
Toshi Kani275c58d2012-05-23 20:25:19 -0600439
440/**
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100441 * acpi_evaluate_ost: Evaluate _OST for hotplug operations
Toshi Kani275c58d2012-05-23 20:25:19 -0600442 * @handle: ACPI device handle
443 * @source_event: source event code
444 * @status_code: status code
445 * @status_buf: optional detailed information (NULL if none)
446 *
447 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
448 * must call this function when evaluating _OST for hotplug operations.
449 * When the platform does not support _OST, this function has no effect.
450 */
451acpi_status
Jiang Liu05730c12014-02-19 14:02:15 +0800452acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
453 struct acpi_buffer *status_buf)
Toshi Kani275c58d2012-05-23 20:25:19 -0600454{
Toshi Kani275c58d2012-05-23 20:25:19 -0600455 union acpi_object params[3] = {
456 {.type = ACPI_TYPE_INTEGER,},
457 {.type = ACPI_TYPE_INTEGER,},
458 {.type = ACPI_TYPE_BUFFER,}
459 };
460 struct acpi_object_list arg_list = {3, params};
Toshi Kani275c58d2012-05-23 20:25:19 -0600461
462 params[0].integer.value = source_event;
463 params[1].integer.value = status_code;
464 if (status_buf != NULL) {
465 params[2].buffer.pointer = status_buf->pointer;
466 params[2].buffer.length = status_buf->length;
467 } else {
468 params[2].buffer.pointer = NULL;
469 params[2].buffer.length = 0;
470 }
471
Jiang Liu05730c12014-02-19 14:02:15 +0800472 return acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
Toshi Kani275c58d2012-05-23 20:25:19 -0600473}
Jiang Liu05730c12014-02-19 14:02:15 +0800474EXPORT_SYMBOL(acpi_evaluate_ost);
Toshi Kanifbfddae2012-11-21 01:36:28 +0000475
476/**
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200477 * acpi_handle_path: Return the object path of handle
478 *
479 * Caller must free the returned buffer
480 */
481static char *acpi_handle_path(acpi_handle handle)
482{
483 struct acpi_buffer buffer = {
484 .length = ACPI_ALLOCATE_BUFFER,
485 .pointer = NULL
486 };
487
488 if (in_interrupt() ||
489 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
490 return NULL;
491 return buffer.pointer;
492}
493
494/**
Toshi Kanifbfddae2012-11-21 01:36:28 +0000495 * acpi_handle_printk: Print message with ACPI prefix and object path
496 *
497 * This function is called through acpi_handle_<level> macros and prints
498 * a message with ACPI prefix and object path. This function acquires
499 * the global namespace mutex to obtain an object path. In interrupt
500 * context, it shows the object path as <n/a>.
501 */
502void
503acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
504{
505 struct va_format vaf;
506 va_list args;
Toshi Kanifbfddae2012-11-21 01:36:28 +0000507 const char *path;
508
509 va_start(args, fmt);
510 vaf.fmt = fmt;
511 vaf.va = &args;
512
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200513 path = acpi_handle_path(handle);
514 printk("%sACPI: %s: %pV", level, path ? path : "<n/a>" , &vaf);
Toshi Kanifbfddae2012-11-21 01:36:28 +0000515
516 va_end(args);
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200517 kfree(path);
Toshi Kanifbfddae2012-11-21 01:36:28 +0000518}
519EXPORT_SYMBOL(acpi_handle_printk);
Jiang Liu952c63e2013-06-29 00:24:38 +0800520
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200521#if defined(CONFIG_DYNAMIC_DEBUG)
522/**
523 * __acpi_handle_debug: pr_debug with ACPI prefix and object path
524 *
525 * This function is called through acpi_handle_debug macro and debug
526 * prints a message with ACPI prefix and object path. This function
527 * acquires the global namespace mutex to obtain an object path. In
528 * interrupt context, it shows the object path as <n/a>.
529 */
530void
531__acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle,
532 const char *fmt, ...)
533{
534 struct va_format vaf;
535 va_list args;
536 const char *path;
537
538 va_start(args, fmt);
539 vaf.fmt = fmt;
540 vaf.va = &args;
541
542 path = acpi_handle_path(handle);
543 __dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf);
544
545 va_end(args);
546 kfree(path);
547}
548EXPORT_SYMBOL(__acpi_handle_debug);
549#endif
550
Jiang Liu952c63e2013-06-29 00:24:38 +0800551/**
552 * acpi_has_method: Check whether @handle has a method named @name
553 * @handle: ACPI device handle
554 * @name: name of object or method
555 *
556 * Check whether @handle has a method named @name.
557 */
558bool acpi_has_method(acpi_handle handle, char *name)
559{
560 acpi_handle tmp;
561
562 return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
563}
564EXPORT_SYMBOL(acpi_has_method);
Jiang Liu0db98202013-06-29 00:24:39 +0800565
566acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
567 u64 arg)
568{
569 union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
570 struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
571
572 obj.integer.value = arg;
573
574 return acpi_evaluate_object(handle, method, &arg_list, NULL);
575}
576EXPORT_SYMBOL(acpi_execute_simple_method);
Jiang Liu7d2421f2013-06-29 00:24:40 +0800577
578/**
579 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
580 * @handle: ACPI device handle
581 *
582 * Evaluate device's _EJ0 method for hotplug operations.
583 */
584acpi_status acpi_evaluate_ej0(acpi_handle handle)
585{
586 acpi_status status;
587
588 status = acpi_execute_simple_method(handle, "_EJ0", 1);
589 if (status == AE_NOT_FOUND)
590 acpi_handle_warn(handle, "No _EJ0 support for device\n");
591 else if (ACPI_FAILURE(status))
592 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
593
594 return status;
595}
596
597/**
598 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
599 * @handle: ACPI device handle
600 * @lock: lock device if non-zero, otherwise unlock device
601 *
602 * Evaluate device's _LCK method if present to lock/unlock device
603 */
604acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
605{
606 acpi_status status;
607
608 status = acpi_execute_simple_method(handle, "_LCK", !!lock);
609 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
610 if (lock)
611 acpi_handle_warn(handle,
612 "Locking device failed (0x%x)\n", status);
613 else
614 acpi_handle_warn(handle,
615 "Unlocking device failed (0x%x)\n", status);
616 }
617
618 return status;
619}
Jiang Liua65ac522013-12-19 20:38:10 +0800620
621/**
622 * acpi_evaluate_dsm - evaluate device's _DSM method
623 * @handle: ACPI device handle
624 * @uuid: UUID of requested functions, should be 16 bytes
625 * @rev: revision number of requested function
626 * @func: requested function number
627 * @argv4: the function specific parameter
628 *
629 * Evaluate device's _DSM method with specified UUID, revision id and
630 * function number. Caller needs to free the returned object.
631 *
632 * Though ACPI defines the fourth parameter for _DSM should be a package,
633 * some old BIOSes do expect a buffer or an integer etc.
634 */
635union acpi_object *
636acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
637 union acpi_object *argv4)
638{
639 acpi_status ret;
640 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
641 union acpi_object params[4];
642 struct acpi_object_list input = {
643 .count = 4,
644 .pointer = params,
645 };
646
647 params[0].type = ACPI_TYPE_BUFFER;
648 params[0].buffer.length = 16;
649 params[0].buffer.pointer = (char *)uuid;
650 params[1].type = ACPI_TYPE_INTEGER;
651 params[1].integer.value = rev;
652 params[2].type = ACPI_TYPE_INTEGER;
653 params[2].integer.value = func;
654 if (argv4) {
655 params[3] = *argv4;
656 } else {
657 params[3].type = ACPI_TYPE_PACKAGE;
658 params[3].package.count = 0;
659 params[3].package.elements = NULL;
660 }
661
662 ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
663 if (ACPI_SUCCESS(ret))
664 return (union acpi_object *)buf.pointer;
665
666 if (ret != AE_NOT_FOUND)
667 acpi_handle_warn(handle,
668 "failed to evaluate _DSM (0x%x)\n", ret);
669
670 return NULL;
671}
672EXPORT_SYMBOL(acpi_evaluate_dsm);
673
674/**
675 * acpi_check_dsm - check if _DSM method supports requested functions.
676 * @handle: ACPI device handle
677 * @uuid: UUID of requested functions, should be 16 bytes at least
678 * @rev: revision number of requested functions
679 * @funcs: bitmap of requested functions
Jiang Liua65ac522013-12-19 20:38:10 +0800680 *
681 * Evaluate device's _DSM method to check whether it supports requested
682 * functions. Currently only support 64 functions at maximum, should be
683 * enough for now.
684 */
685bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
686{
687 int i;
688 u64 mask = 0;
689 union acpi_object *obj;
690
691 if (funcs == 0)
692 return false;
693
694 obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
695 if (!obj)
696 return false;
697
698 /* For compatibility, old BIOSes may return an integer */
699 if (obj->type == ACPI_TYPE_INTEGER)
700 mask = obj->integer.value;
701 else if (obj->type == ACPI_TYPE_BUFFER)
702 for (i = 0; i < obj->buffer.length && i < 8; i++)
703 mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
704 ACPI_FREE(obj);
705
706 /*
707 * Bit 0 indicates whether there's support for any functions other than
708 * function 0 for the specified UUID and revision.
709 */
710 if ((mask & 0x1) && (mask & funcs) == funcs)
711 return true;
712
713 return false;
714}
715EXPORT_SYMBOL(acpi_check_dsm);