blob: 07c8c5a5ee95cfec11ae94114a22398cd99273f6 [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;
152
153 case ACPI_TYPE_PACKAGE:
154 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400155 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
156 "Found unsupported element at index=%d\n",
157 i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* TBD: handle nested packages... */
Patrick Mocheld550d982006-06-27 00:41:40 -0400159 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 break;
161 }
162 }
163
164 /*
165 * Validate output buffer.
166 */
Al Stonee83dda02013-10-09 14:21:10 -0600167 if (buffer->length == ACPI_ALLOCATE_BUFFER) {
jhbird.choi@samsung.com2d0acb42014-03-20 16:35:56 +0900168 buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required);
Al Stonee83dda02013-10-09 14:21:10 -0600169 if (!buffer->pointer)
170 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 buffer->length = size_required;
Al Stonee83dda02013-10-09 14:21:10 -0600172 } else {
173 if (buffer->length < size_required) {
174 buffer->length = size_required;
175 return AE_BUFFER_OVERFLOW;
176 } else if (buffer->length != size_required ||
177 !buffer->pointer) {
178 return AE_BAD_PARAMETER;
179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
182 head = buffer->pointer;
183 tail = buffer->pointer + tail_offset;
184
185 /*
186 * Extract package data.
187 */
Len Brown4be44fc2005-08-05 00:44:28 -0400188 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 u8 **pointer = NULL;
191 union acpi_object *element = &(package->package.elements[i]);
192
193 if (!element) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400194 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196
197 switch (element->type) {
198
199 case ACPI_TYPE_INTEGER:
200 switch (format_string[i]) {
201 case 'N':
Lin Ming439913f2010-01-28 10:53:19 +0800202 *((u64 *) head) =
Len Brown4be44fc2005-08-05 00:44:28 -0400203 element->integer.value;
Lin Ming439913f2010-01-28 10:53:19 +0800204 head += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 break;
206 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400207 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *pointer = tail;
Lin Ming439913f2010-01-28 10:53:19 +0800209 *((u64 *) tail) =
Len Brown4be44fc2005-08-05 00:44:28 -0400210 element->integer.value;
Lin Ming439913f2010-01-28 10:53:19 +0800211 head += sizeof(u64 *);
212 tail += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* NULL terminate string */
214 *tail = (char)0;
215 tail += sizeof(char);
216 break;
217 default:
218 /* Should never get here */
219 break;
220 }
221 break;
222
223 case ACPI_TYPE_STRING:
224 case ACPI_TYPE_BUFFER:
225 switch (format_string[i]) {
226 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400227 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400229 memcpy(tail, element->string.pointer,
230 element->string.length);
231 head += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 tail += element->string.length * sizeof(char);
233 /* NULL terminate string */
234 *tail = (char)0;
235 tail += sizeof(char);
236 break;
237 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400238 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400240 memcpy(tail, element->buffer.pointer,
241 element->buffer.length);
242 head += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 tail += element->buffer.length * sizeof(u8);
244 break;
245 default:
246 /* Should never get here */
247 break;
248 }
249 break;
250
251 case ACPI_TYPE_PACKAGE:
252 /* TBD: handle nested packages... */
253 default:
254 /* Should never get here */
255 break;
256 }
257 }
258
Patrick Mocheld550d982006-06-27 00:41:40 -0400259 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
Len Brown4be44fc2005-08-05 00:44:28 -0400261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262EXPORT_SYMBOL(acpi_extract_package);
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400265acpi_evaluate_integer(acpi_handle handle,
266 acpi_string pathname,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400267 struct acpi_object_list *arguments, unsigned long long *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Len Brown4be44fc2005-08-05 00:44:28 -0400269 acpi_status status = AE_OK;
Pavel Machek40599072008-11-25 12:05:08 +0100270 union acpi_object element;
Len Brown4be44fc2005-08-05 00:44:28 -0400271 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400274 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 buffer.length = sizeof(union acpi_object);
Pavel Machek40599072008-11-25 12:05:08 +0100277 buffer.pointer = &element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
279 if (ACPI_FAILURE(status)) {
280 acpi_util_eval_error(handle, pathname, status);
Patrick Mocheld550d982006-06-27 00:41:40 -0400281 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283
Pavel Machek40599072008-11-25 12:05:08 +0100284 if (element.type != ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
Patrick Mocheld550d982006-06-27 00:41:40 -0400286 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
Pavel Machek40599072008-11-25 12:05:08 +0100289 *data = element.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Matthew Wilcox27663c52008-10-10 02:22:59 -0400291 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Patrick Mocheld550d982006-06-27 00:41:40 -0400293 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Len Brown4be44fc2005-08-05 00:44:28 -0400296EXPORT_SYMBOL(acpi_evaluate_integer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400299acpi_evaluate_reference(acpi_handle handle,
300 acpi_string pathname,
301 struct acpi_object_list *arguments,
302 struct acpi_handle_list *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Len Brown4be44fc2005-08-05 00:44:28 -0400304 acpi_status status = AE_OK;
305 union acpi_object *package = NULL;
306 union acpi_object *element = NULL;
307 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
308 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 if (!list) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400312 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 /* Evaluate object. */
316
317 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
318 if (ACPI_FAILURE(status))
319 goto end;
320
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200321 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if ((buffer.length == 0) || !package) {
Len Brown64684632006-06-26 23:41:38 -0400324 printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
325 (unsigned)buffer.length, package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 status = AE_BAD_DATA;
327 acpi_util_eval_error(handle, pathname, status);
328 goto end;
329 }
330 if (package->type != ACPI_TYPE_PACKAGE) {
Len Brown64684632006-06-26 23:41:38 -0400331 printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
332 package->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 status = AE_BAD_DATA;
334 acpi_util_eval_error(handle, pathname, status);
335 goto end;
336 }
337 if (!package->package.count) {
Len Brown64684632006-06-26 23:41:38 -0400338 printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
339 package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 status = AE_BAD_DATA;
341 acpi_util_eval_error(handle, pathname, status);
342 goto end;
343 }
344
345 if (package->package.count > ACPI_MAX_HANDLES) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400346 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 list->count = package->package.count;
349
350 /* Extract package data. */
351
352 for (i = 0; i < list->count; i++) {
353
354 element = &(package->package.elements[i]);
355
Bob Moorecd0b2242008-04-10 19:06:43 +0400356 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 status = AE_BAD_DATA;
Len Brown64684632006-06-26 23:41:38 -0400358 printk(KERN_ERR PREFIX
359 "Expecting a [Reference] package element, found type %X\n",
360 element->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 acpi_util_eval_error(handle, pathname, status);
362 break;
363 }
364
Thomas Renningerb6a16382008-03-12 01:06:24 +0100365 if (!element->reference.handle) {
366 printk(KERN_WARNING PREFIX "Invalid reference in"
367 " package %s\n", pathname);
368 status = AE_NULL_ENTRY;
369 break;
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* Get the acpi_handle. */
372
373 list->handles[i] = element->reference.handle;
374 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400375 list->handles[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
Len Brown4be44fc2005-08-05 00:44:28 -0400378 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (ACPI_FAILURE(status)) {
380 list->count = 0;
381 //kfree(list->handles);
382 }
383
Len Brown02438d82006-06-30 03:19:10 -0400384 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Patrick Mocheld550d982006-06-27 00:41:40 -0400386 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Len Brown4be44fc2005-08-05 00:44:28 -0400389EXPORT_SYMBOL(acpi_evaluate_reference);
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800390
391acpi_status
Feng Tang8ede06a2012-08-21 09:56:58 +0800392acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800393{
394 acpi_status status;
395 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
396 union acpi_object *output;
397
398 status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
399
400 if (ACPI_FAILURE(status))
401 return status;
402
403 output = buffer.pointer;
404
405 if (!output || output->type != ACPI_TYPE_PACKAGE
406 || !output->package.count
407 || output->package.elements[0].type != ACPI_TYPE_BUFFER
Feng Tang8ede06a2012-08-21 09:56:58 +0800408 || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800409 status = AE_TYPE;
410 goto out;
411 }
412
Feng Tang8ede06a2012-08-21 09:56:58 +0800413 status = acpi_decode_pld_buffer(
414 output->package.elements[0].buffer.pointer,
415 output->package.elements[0].buffer.length,
416 pld);
417
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800418out:
419 kfree(buffer.pointer);
420 return status;
421}
422EXPORT_SYMBOL(acpi_get_physical_device_location);
Toshi Kani275c58d2012-05-23 20:25:19 -0600423
424/**
Rafael J. Wysocki700b8422014-02-21 01:07:17 +0100425 * acpi_evaluate_ost: Evaluate _OST for hotplug operations
Toshi Kani275c58d2012-05-23 20:25:19 -0600426 * @handle: ACPI device handle
427 * @source_event: source event code
428 * @status_code: status code
429 * @status_buf: optional detailed information (NULL if none)
430 *
431 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
432 * must call this function when evaluating _OST for hotplug operations.
433 * When the platform does not support _OST, this function has no effect.
434 */
435acpi_status
Jiang Liu05730c12014-02-19 14:02:15 +0800436acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
437 struct acpi_buffer *status_buf)
Toshi Kani275c58d2012-05-23 20:25:19 -0600438{
Toshi Kani275c58d2012-05-23 20:25:19 -0600439 union acpi_object params[3] = {
440 {.type = ACPI_TYPE_INTEGER,},
441 {.type = ACPI_TYPE_INTEGER,},
442 {.type = ACPI_TYPE_BUFFER,}
443 };
444 struct acpi_object_list arg_list = {3, params};
Toshi Kani275c58d2012-05-23 20:25:19 -0600445
446 params[0].integer.value = source_event;
447 params[1].integer.value = status_code;
448 if (status_buf != NULL) {
449 params[2].buffer.pointer = status_buf->pointer;
450 params[2].buffer.length = status_buf->length;
451 } else {
452 params[2].buffer.pointer = NULL;
453 params[2].buffer.length = 0;
454 }
455
Jiang Liu05730c12014-02-19 14:02:15 +0800456 return acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
Toshi Kani275c58d2012-05-23 20:25:19 -0600457}
Jiang Liu05730c12014-02-19 14:02:15 +0800458EXPORT_SYMBOL(acpi_evaluate_ost);
Toshi Kanifbfddae2012-11-21 01:36:28 +0000459
460/**
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200461 * acpi_handle_path: Return the object path of handle
462 *
463 * Caller must free the returned buffer
464 */
465static char *acpi_handle_path(acpi_handle handle)
466{
467 struct acpi_buffer buffer = {
468 .length = ACPI_ALLOCATE_BUFFER,
469 .pointer = NULL
470 };
471
472 if (in_interrupt() ||
473 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
474 return NULL;
475 return buffer.pointer;
476}
477
478/**
Toshi Kanifbfddae2012-11-21 01:36:28 +0000479 * acpi_handle_printk: Print message with ACPI prefix and object path
480 *
481 * This function is called through acpi_handle_<level> macros and prints
482 * a message with ACPI prefix and object path. This function acquires
483 * the global namespace mutex to obtain an object path. In interrupt
484 * context, it shows the object path as <n/a>.
485 */
486void
487acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
488{
489 struct va_format vaf;
490 va_list args;
Toshi Kanifbfddae2012-11-21 01:36:28 +0000491 const char *path;
492
493 va_start(args, fmt);
494 vaf.fmt = fmt;
495 vaf.va = &args;
496
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200497 path = acpi_handle_path(handle);
498 printk("%sACPI: %s: %pV", level, path ? path : "<n/a>" , &vaf);
Toshi Kanifbfddae2012-11-21 01:36:28 +0000499
500 va_end(args);
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200501 kfree(path);
Toshi Kanifbfddae2012-11-21 01:36:28 +0000502}
503EXPORT_SYMBOL(acpi_handle_printk);
Jiang Liu952c63e2013-06-29 00:24:38 +0800504
Bjørn Mork45fef5b2014-05-22 12:47:47 +0200505#if defined(CONFIG_DYNAMIC_DEBUG)
506/**
507 * __acpi_handle_debug: pr_debug with ACPI prefix and object path
508 *
509 * This function is called through acpi_handle_debug macro and debug
510 * prints a message with ACPI prefix and object path. This function
511 * acquires the global namespace mutex to obtain an object path. In
512 * interrupt context, it shows the object path as <n/a>.
513 */
514void
515__acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle,
516 const char *fmt, ...)
517{
518 struct va_format vaf;
519 va_list args;
520 const char *path;
521
522 va_start(args, fmt);
523 vaf.fmt = fmt;
524 vaf.va = &args;
525
526 path = acpi_handle_path(handle);
527 __dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf);
528
529 va_end(args);
530 kfree(path);
531}
532EXPORT_SYMBOL(__acpi_handle_debug);
533#endif
534
Jiang Liu952c63e2013-06-29 00:24:38 +0800535/**
536 * acpi_has_method: Check whether @handle has a method named @name
537 * @handle: ACPI device handle
538 * @name: name of object or method
539 *
540 * Check whether @handle has a method named @name.
541 */
542bool acpi_has_method(acpi_handle handle, char *name)
543{
544 acpi_handle tmp;
545
546 return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
547}
548EXPORT_SYMBOL(acpi_has_method);
Jiang Liu0db98202013-06-29 00:24:39 +0800549
550acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
551 u64 arg)
552{
553 union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
554 struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
555
556 obj.integer.value = arg;
557
558 return acpi_evaluate_object(handle, method, &arg_list, NULL);
559}
560EXPORT_SYMBOL(acpi_execute_simple_method);
Jiang Liu7d2421f2013-06-29 00:24:40 +0800561
562/**
563 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
564 * @handle: ACPI device handle
565 *
566 * Evaluate device's _EJ0 method for hotplug operations.
567 */
568acpi_status acpi_evaluate_ej0(acpi_handle handle)
569{
570 acpi_status status;
571
572 status = acpi_execute_simple_method(handle, "_EJ0", 1);
573 if (status == AE_NOT_FOUND)
574 acpi_handle_warn(handle, "No _EJ0 support for device\n");
575 else if (ACPI_FAILURE(status))
576 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
577
578 return status;
579}
580
581/**
582 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
583 * @handle: ACPI device handle
584 * @lock: lock device if non-zero, otherwise unlock device
585 *
586 * Evaluate device's _LCK method if present to lock/unlock device
587 */
588acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
589{
590 acpi_status status;
591
592 status = acpi_execute_simple_method(handle, "_LCK", !!lock);
593 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
594 if (lock)
595 acpi_handle_warn(handle,
596 "Locking device failed (0x%x)\n", status);
597 else
598 acpi_handle_warn(handle,
599 "Unlocking device failed (0x%x)\n", status);
600 }
601
602 return status;
603}
Jiang Liua65ac522013-12-19 20:38:10 +0800604
605/**
606 * acpi_evaluate_dsm - evaluate device's _DSM method
607 * @handle: ACPI device handle
608 * @uuid: UUID of requested functions, should be 16 bytes
609 * @rev: revision number of requested function
610 * @func: requested function number
611 * @argv4: the function specific parameter
612 *
613 * Evaluate device's _DSM method with specified UUID, revision id and
614 * function number. Caller needs to free the returned object.
615 *
616 * Though ACPI defines the fourth parameter for _DSM should be a package,
617 * some old BIOSes do expect a buffer or an integer etc.
618 */
619union acpi_object *
620acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
621 union acpi_object *argv4)
622{
623 acpi_status ret;
624 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
625 union acpi_object params[4];
626 struct acpi_object_list input = {
627 .count = 4,
628 .pointer = params,
629 };
630
631 params[0].type = ACPI_TYPE_BUFFER;
632 params[0].buffer.length = 16;
633 params[0].buffer.pointer = (char *)uuid;
634 params[1].type = ACPI_TYPE_INTEGER;
635 params[1].integer.value = rev;
636 params[2].type = ACPI_TYPE_INTEGER;
637 params[2].integer.value = func;
638 if (argv4) {
639 params[3] = *argv4;
640 } else {
641 params[3].type = ACPI_TYPE_PACKAGE;
642 params[3].package.count = 0;
643 params[3].package.elements = NULL;
644 }
645
646 ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
647 if (ACPI_SUCCESS(ret))
648 return (union acpi_object *)buf.pointer;
649
650 if (ret != AE_NOT_FOUND)
651 acpi_handle_warn(handle,
652 "failed to evaluate _DSM (0x%x)\n", ret);
653
654 return NULL;
655}
656EXPORT_SYMBOL(acpi_evaluate_dsm);
657
658/**
659 * acpi_check_dsm - check if _DSM method supports requested functions.
660 * @handle: ACPI device handle
661 * @uuid: UUID of requested functions, should be 16 bytes at least
662 * @rev: revision number of requested functions
663 * @funcs: bitmap of requested functions
664 * @exclude: excluding special value, used to support i915 and nouveau
665 *
666 * Evaluate device's _DSM method to check whether it supports requested
667 * functions. Currently only support 64 functions at maximum, should be
668 * enough for now.
669 */
670bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
671{
672 int i;
673 u64 mask = 0;
674 union acpi_object *obj;
675
676 if (funcs == 0)
677 return false;
678
679 obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
680 if (!obj)
681 return false;
682
683 /* For compatibility, old BIOSes may return an integer */
684 if (obj->type == ACPI_TYPE_INTEGER)
685 mask = obj->integer.value;
686 else if (obj->type == ACPI_TYPE_BUFFER)
687 for (i = 0; i < obj->buffer.length && i < 8; i++)
688 mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
689 ACPI_FREE(obj);
690
691 /*
692 * Bit 0 indicates whether there's support for any functions other than
693 * function 0 for the specified UUID and revision.
694 */
695 if ((mask & 0x1) && (mask & funcs) == funcs)
696 return true;
697
698 return false;
699}
700EXPORT_SYMBOL(acpi_check_dsm);