blob: 0347a37eb4389dcbd5c497ca6833e867618acbf5 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Len Browna192a952009-07-28 16:45:54 -040034#include "internal.h"
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050037ACPI_MODULE_NAME("utils");
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/* --------------------------------------------------------------------------
40 Object Evaluation Helpers
41 -------------------------------------------------------------------------- */
Harvey Harrison4fd7f512008-02-15 17:07:19 -080042static void
43acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
44{
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#ifdef ACPI_DEBUG_OUTPUT
Harvey Harrison4fd7f512008-02-15 17:07:19 -080046 char prefix[80] = {'\0'};
47 struct acpi_buffer buffer = {sizeof(prefix), prefix};
48 acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);
49 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",
50 (char *) prefix, p, acpi_format_exception(s)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#else
Harvey Harrison4fd7f512008-02-15 17:07:19 -080052 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#endif
Harvey Harrison4fd7f512008-02-15 17:07:19 -080054}
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040057acpi_extract_package(union acpi_object *package,
58 struct acpi_buffer *format, struct acpi_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Len Brown4be44fc2005-08-05 00:44:28 -040060 u32 size_required = 0;
61 u32 tail_offset = 0;
62 char *format_string = NULL;
63 u32 format_count = 0;
64 u32 i = 0;
65 u8 *head = NULL;
66 u8 *tail = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Len Brown4be44fc2005-08-05 00:44:28 -040069 if (!package || (package->type != ACPI_TYPE_PACKAGE)
70 || (package->package.count < 1)) {
Len Browncece9292006-06-26 23:04:31 -040071 printk(KERN_WARNING PREFIX "Invalid package argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040072 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74
75 if (!format || !format->pointer || (format->length < 1)) {
Len Browncece9292006-06-26 23:04:31 -040076 printk(KERN_WARNING PREFIX "Invalid format argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040077 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
80 if (!buffer) {
Len Browncece9292006-06-26 23:04:31 -040081 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
Patrick Mocheld550d982006-06-27 00:41:40 -040082 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84
Len Brown4be44fc2005-08-05 00:44:28 -040085 format_count = (format->length / sizeof(char)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (format_count > package->package.count) {
Len Browncece9292006-06-26 23:04:31 -040087 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
88 " than exist in package [%d].\n",
89 format_count, package->package.count);
Patrick Mocheld550d982006-06-27 00:41:40 -040090 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
Jan Engelhardt50dd0962006-10-01 00:28:50 +020093 format_string = format->pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 /*
96 * Calculate size_required.
97 */
Len Brown4be44fc2005-08-05 00:44:28 -040098 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 union acpi_object *element = &(package->package.elements[i]);
101
102 if (!element) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400103 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
106 switch (element->type) {
107
108 case ACPI_TYPE_INTEGER:
109 switch (format_string[i]) {
110 case 'N':
Lin Ming439913f2010-01-28 10:53:19 +0800111 size_required += sizeof(u64);
112 tail_offset += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 break;
114 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400115 size_required +=
Lin Ming439913f2010-01-28 10:53:19 +0800116 sizeof(char *) + sizeof(u64) +
Len Brown4be44fc2005-08-05 00:44:28 -0400117 sizeof(char);
118 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 break;
120 default:
Len Browncece9292006-06-26 23:04:31 -0400121 printk(KERN_WARNING PREFIX "Invalid package element"
Colin Ian Kinge7e92ec92013-10-29 10:34:19 +0000122 " [%d]: got number, expecting"
Len Browncece9292006-06-26 23:04:31 -0400123 " [%c]\n",
124 i, format_string[i]);
Patrick Mocheld550d982006-06-27 00:41:40 -0400125 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 break;
127 }
128 break;
129
130 case ACPI_TYPE_STRING:
131 case ACPI_TYPE_BUFFER:
132 switch (format_string[i]) {
133 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400134 size_required +=
135 sizeof(char *) +
136 (element->string.length * sizeof(char)) +
137 sizeof(char);
138 tail_offset += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 break;
140 case 'B':
Len Brown4be44fc2005-08-05 00:44:28 -0400141 size_required +=
142 sizeof(u8 *) +
143 (element->buffer.length * sizeof(u8));
144 tail_offset += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 break;
146 default:
Len Browncece9292006-06-26 23:04:31 -0400147 printk(KERN_WARNING PREFIX "Invalid package element"
Thomas Renningera6fc6722006-06-26 23:58:43 -0400148 " [%d] got string/buffer,"
Colin Ian Kinge7e92ec92013-10-29 10:34:19 +0000149 " expecting [%c]\n",
Len Browncece9292006-06-26 23:04:31 -0400150 i, format_string[i]);
Patrick Mocheld550d982006-06-27 00:41:40 -0400151 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 break;
153 }
154 break;
155
156 case ACPI_TYPE_PACKAGE:
157 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400158 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
159 "Found unsupported element at index=%d\n",
160 i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 /* TBD: handle nested packages... */
Patrick Mocheld550d982006-06-27 00:41:40 -0400162 return AE_SUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 break;
164 }
165 }
166
167 /*
168 * Validate output buffer.
169 */
Al Stonee83dda02013-10-09 14:21:10 -0600170 if (buffer->length == ACPI_ALLOCATE_BUFFER) {
171 buffer->pointer = ACPI_ALLOCATE(size_required);
172 if (!buffer->pointer)
173 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 buffer->length = size_required;
Al Stonee83dda02013-10-09 14:21:10 -0600175 memset(buffer->pointer, 0, size_required);
176 } else {
177 if (buffer->length < size_required) {
178 buffer->length = size_required;
179 return AE_BUFFER_OVERFLOW;
180 } else if (buffer->length != size_required ||
181 !buffer->pointer) {
182 return AE_BAD_PARAMETER;
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185
186 head = buffer->pointer;
187 tail = buffer->pointer + tail_offset;
188
189 /*
190 * Extract package data.
191 */
Len Brown4be44fc2005-08-05 00:44:28 -0400192 for (i = 0; i < format_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 u8 **pointer = NULL;
195 union acpi_object *element = &(package->package.elements[i]);
196
197 if (!element) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400198 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200
201 switch (element->type) {
202
203 case ACPI_TYPE_INTEGER:
204 switch (format_string[i]) {
205 case 'N':
Lin Ming439913f2010-01-28 10:53:19 +0800206 *((u64 *) head) =
Len Brown4be44fc2005-08-05 00:44:28 -0400207 element->integer.value;
Lin Ming439913f2010-01-28 10:53:19 +0800208 head += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 break;
210 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400211 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *pointer = tail;
Lin Ming439913f2010-01-28 10:53:19 +0800213 *((u64 *) tail) =
Len Brown4be44fc2005-08-05 00:44:28 -0400214 element->integer.value;
Lin Ming439913f2010-01-28 10:53:19 +0800215 head += sizeof(u64 *);
216 tail += sizeof(u64);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* NULL terminate string */
218 *tail = (char)0;
219 tail += sizeof(char);
220 break;
221 default:
222 /* Should never get here */
223 break;
224 }
225 break;
226
227 case ACPI_TYPE_STRING:
228 case ACPI_TYPE_BUFFER:
229 switch (format_string[i]) {
230 case 'S':
Len Brown4be44fc2005-08-05 00:44:28 -0400231 pointer = (u8 **) head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 *pointer = tail;
Len Brown4be44fc2005-08-05 00:44:28 -0400233 memcpy(tail, element->string.pointer,
234 element->string.length);
235 head += sizeof(char *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 tail += element->string.length * sizeof(char);
237 /* NULL terminate string */
238 *tail = (char)0;
239 tail += sizeof(char);
240 break;
241 case 'B':
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->buffer.pointer,
245 element->buffer.length);
246 head += sizeof(u8 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 tail += element->buffer.length * sizeof(u8);
248 break;
249 default:
250 /* Should never get here */
251 break;
252 }
253 break;
254
255 case ACPI_TYPE_PACKAGE:
256 /* TBD: handle nested packages... */
257 default:
258 /* Should never get here */
259 break;
260 }
261 }
262
Patrick Mocheld550d982006-06-27 00:41:40 -0400263 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
Len Brown4be44fc2005-08-05 00:44:28 -0400265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266EXPORT_SYMBOL(acpi_extract_package);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400269acpi_evaluate_integer(acpi_handle handle,
270 acpi_string pathname,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400271 struct acpi_object_list *arguments, unsigned long long *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Len Brown4be44fc2005-08-05 00:44:28 -0400273 acpi_status status = AE_OK;
Pavel Machek40599072008-11-25 12:05:08 +0100274 union acpi_object element;
Len Brown4be44fc2005-08-05 00:44:28 -0400275 struct acpi_buffer buffer = { 0, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -0400278 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 buffer.length = sizeof(union acpi_object);
Pavel Machek40599072008-11-25 12:05:08 +0100281 buffer.pointer = &element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
283 if (ACPI_FAILURE(status)) {
284 acpi_util_eval_error(handle, pathname, status);
Patrick Mocheld550d982006-06-27 00:41:40 -0400285 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287
Pavel Machek40599072008-11-25 12:05:08 +0100288 if (element.type != ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
Patrick Mocheld550d982006-06-27 00:41:40 -0400290 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
Pavel Machek40599072008-11-25 12:05:08 +0100293 *data = element.integer.value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Matthew Wilcox27663c52008-10-10 02:22:59 -0400295 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Patrick Mocheld550d982006-06-27 00:41:40 -0400297 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Len Brown4be44fc2005-08-05 00:44:28 -0400300EXPORT_SYMBOL(acpi_evaluate_integer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400303acpi_evaluate_reference(acpi_handle handle,
304 acpi_string pathname,
305 struct acpi_object_list *arguments,
306 struct acpi_handle_list *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Len Brown4be44fc2005-08-05 00:44:28 -0400308 acpi_status status = AE_OK;
309 union acpi_object *package = NULL;
310 union acpi_object *element = NULL;
311 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
312 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (!list) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400316 return AE_BAD_PARAMETER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
319 /* Evaluate object. */
320
321 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
322 if (ACPI_FAILURE(status))
323 goto end;
324
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200325 package = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 if ((buffer.length == 0) || !package) {
Len Brown64684632006-06-26 23:41:38 -0400328 printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
329 (unsigned)buffer.length, package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 status = AE_BAD_DATA;
331 acpi_util_eval_error(handle, pathname, status);
332 goto end;
333 }
334 if (package->type != ACPI_TYPE_PACKAGE) {
Len Brown64684632006-06-26 23:41:38 -0400335 printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
336 package->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 status = AE_BAD_DATA;
338 acpi_util_eval_error(handle, pathname, status);
339 goto end;
340 }
341 if (!package->package.count) {
Len Brown64684632006-06-26 23:41:38 -0400342 printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
343 package);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 status = AE_BAD_DATA;
345 acpi_util_eval_error(handle, pathname, status);
346 goto end;
347 }
348
349 if (package->package.count > ACPI_MAX_HANDLES) {
Patrick Mocheld550d982006-06-27 00:41:40 -0400350 return AE_NO_MEMORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352 list->count = package->package.count;
353
354 /* Extract package data. */
355
356 for (i = 0; i < list->count; i++) {
357
358 element = &(package->package.elements[i]);
359
Bob Moorecd0b2242008-04-10 19:06:43 +0400360 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 status = AE_BAD_DATA;
Len Brown64684632006-06-26 23:41:38 -0400362 printk(KERN_ERR PREFIX
363 "Expecting a [Reference] package element, found type %X\n",
364 element->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 acpi_util_eval_error(handle, pathname, status);
366 break;
367 }
368
Thomas Renningerb6a16382008-03-12 01:06:24 +0100369 if (!element->reference.handle) {
370 printk(KERN_WARNING PREFIX "Invalid reference in"
371 " package %s\n", pathname);
372 status = AE_NULL_ENTRY;
373 break;
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* Get the acpi_handle. */
376
377 list->handles[i] = element->reference.handle;
378 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400379 list->handles[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
Len Brown4be44fc2005-08-05 00:44:28 -0400382 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (ACPI_FAILURE(status)) {
384 list->count = 0;
385 //kfree(list->handles);
386 }
387
Len Brown02438d82006-06-30 03:19:10 -0400388 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Patrick Mocheld550d982006-06-27 00:41:40 -0400390 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Len Brown4be44fc2005-08-05 00:44:28 -0400393EXPORT_SYMBOL(acpi_evaluate_reference);
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800394
395acpi_status
Feng Tang8ede06a2012-08-21 09:56:58 +0800396acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800397{
398 acpi_status status;
399 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
400 union acpi_object *output;
401
402 status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
403
404 if (ACPI_FAILURE(status))
405 return status;
406
407 output = buffer.pointer;
408
409 if (!output || output->type != ACPI_TYPE_PACKAGE
410 || !output->package.count
411 || output->package.elements[0].type != ACPI_TYPE_BUFFER
Feng Tang8ede06a2012-08-21 09:56:58 +0800412 || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800413 status = AE_TYPE;
414 goto out;
415 }
416
Feng Tang8ede06a2012-08-21 09:56:58 +0800417 status = acpi_decode_pld_buffer(
418 output->package.elements[0].buffer.pointer,
419 output->package.elements[0].buffer.length,
420 pld);
421
Matthew Garrett38ac0f12012-05-11 16:08:26 +0800422out:
423 kfree(buffer.pointer);
424 return status;
425}
426EXPORT_SYMBOL(acpi_get_physical_device_location);
Toshi Kani275c58d2012-05-23 20:25:19 -0600427
428/**
429 * acpi_evaluate_hotplug_ost: Evaluate _OST for hotplug operations
430 * @handle: ACPI device handle
431 * @source_event: source event code
432 * @status_code: status code
433 * @status_buf: optional detailed information (NULL if none)
434 *
435 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
436 * must call this function when evaluating _OST for hotplug operations.
437 * When the platform does not support _OST, this function has no effect.
438 */
439acpi_status
440acpi_evaluate_hotplug_ost(acpi_handle handle, u32 source_event,
441 u32 status_code, struct acpi_buffer *status_buf)
442{
443#ifdef ACPI_HOTPLUG_OST
444 union acpi_object params[3] = {
445 {.type = ACPI_TYPE_INTEGER,},
446 {.type = ACPI_TYPE_INTEGER,},
447 {.type = ACPI_TYPE_BUFFER,}
448 };
449 struct acpi_object_list arg_list = {3, params};
450 acpi_status status;
451
452 params[0].integer.value = source_event;
453 params[1].integer.value = status_code;
454 if (status_buf != NULL) {
455 params[2].buffer.pointer = status_buf->pointer;
456 params[2].buffer.length = status_buf->length;
457 } else {
458 params[2].buffer.pointer = NULL;
459 params[2].buffer.length = 0;
460 }
461
462 status = acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
463 return status;
464#else
465 return AE_OK;
466#endif
467}
468EXPORT_SYMBOL(acpi_evaluate_hotplug_ost);
Toshi Kanifbfddae2012-11-21 01:36:28 +0000469
470/**
471 * acpi_handle_printk: Print message with ACPI prefix and object path
472 *
473 * This function is called through acpi_handle_<level> macros and prints
474 * a message with ACPI prefix and object path. This function acquires
475 * the global namespace mutex to obtain an object path. In interrupt
476 * context, it shows the object path as <n/a>.
477 */
478void
479acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
480{
481 struct va_format vaf;
482 va_list args;
483 struct acpi_buffer buffer = {
484 .length = ACPI_ALLOCATE_BUFFER,
485 .pointer = NULL
486 };
487 const char *path;
488
489 va_start(args, fmt);
490 vaf.fmt = fmt;
491 vaf.va = &args;
492
493 if (in_interrupt() ||
494 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
495 path = "<n/a>";
496 else
497 path = buffer.pointer;
498
499 printk("%sACPI: %s: %pV", level, path, &vaf);
500
501 va_end(args);
502 kfree(buffer.pointer);
503}
504EXPORT_SYMBOL(acpi_handle_printk);
Jiang Liu952c63e2013-06-29 00:24:38 +0800505
506/**
507 * acpi_has_method: Check whether @handle has a method named @name
508 * @handle: ACPI device handle
509 * @name: name of object or method
510 *
511 * Check whether @handle has a method named @name.
512 */
513bool acpi_has_method(acpi_handle handle, char *name)
514{
515 acpi_handle tmp;
516
517 return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
518}
519EXPORT_SYMBOL(acpi_has_method);
Jiang Liu0db98202013-06-29 00:24:39 +0800520
521acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
522 u64 arg)
523{
524 union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
525 struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
526
527 obj.integer.value = arg;
528
529 return acpi_evaluate_object(handle, method, &arg_list, NULL);
530}
531EXPORT_SYMBOL(acpi_execute_simple_method);
Jiang Liu7d2421f2013-06-29 00:24:40 +0800532
533/**
534 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
535 * @handle: ACPI device handle
536 *
537 * Evaluate device's _EJ0 method for hotplug operations.
538 */
539acpi_status acpi_evaluate_ej0(acpi_handle handle)
540{
541 acpi_status status;
542
543 status = acpi_execute_simple_method(handle, "_EJ0", 1);
544 if (status == AE_NOT_FOUND)
545 acpi_handle_warn(handle, "No _EJ0 support for device\n");
546 else if (ACPI_FAILURE(status))
547 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
548
549 return status;
550}
551
552/**
553 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
554 * @handle: ACPI device handle
555 * @lock: lock device if non-zero, otherwise unlock device
556 *
557 * Evaluate device's _LCK method if present to lock/unlock device
558 */
559acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
560{
561 acpi_status status;
562
563 status = acpi_execute_simple_method(handle, "_LCK", !!lock);
564 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
565 if (lock)
566 acpi_handle_warn(handle,
567 "Locking device failed (0x%x)\n", status);
568 else
569 acpi_handle_warn(handle,
570 "Unlocking device failed (0x%x)\n", status);
571 }
572
573 return status;
574}
Jiang Liua65ac522013-12-19 20:38:10 +0800575
576/**
577 * acpi_evaluate_dsm - evaluate device's _DSM method
578 * @handle: ACPI device handle
579 * @uuid: UUID of requested functions, should be 16 bytes
580 * @rev: revision number of requested function
581 * @func: requested function number
582 * @argv4: the function specific parameter
583 *
584 * Evaluate device's _DSM method with specified UUID, revision id and
585 * function number. Caller needs to free the returned object.
586 *
587 * Though ACPI defines the fourth parameter for _DSM should be a package,
588 * some old BIOSes do expect a buffer or an integer etc.
589 */
590union acpi_object *
591acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
592 union acpi_object *argv4)
593{
594 acpi_status ret;
595 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
596 union acpi_object params[4];
597 struct acpi_object_list input = {
598 .count = 4,
599 .pointer = params,
600 };
601
602 params[0].type = ACPI_TYPE_BUFFER;
603 params[0].buffer.length = 16;
604 params[0].buffer.pointer = (char *)uuid;
605 params[1].type = ACPI_TYPE_INTEGER;
606 params[1].integer.value = rev;
607 params[2].type = ACPI_TYPE_INTEGER;
608 params[2].integer.value = func;
609 if (argv4) {
610 params[3] = *argv4;
611 } else {
612 params[3].type = ACPI_TYPE_PACKAGE;
613 params[3].package.count = 0;
614 params[3].package.elements = NULL;
615 }
616
617 ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
618 if (ACPI_SUCCESS(ret))
619 return (union acpi_object *)buf.pointer;
620
621 if (ret != AE_NOT_FOUND)
622 acpi_handle_warn(handle,
623 "failed to evaluate _DSM (0x%x)\n", ret);
624
625 return NULL;
626}
627EXPORT_SYMBOL(acpi_evaluate_dsm);
628
629/**
630 * acpi_check_dsm - check if _DSM method supports requested functions.
631 * @handle: ACPI device handle
632 * @uuid: UUID of requested functions, should be 16 bytes at least
633 * @rev: revision number of requested functions
634 * @funcs: bitmap of requested functions
635 * @exclude: excluding special value, used to support i915 and nouveau
636 *
637 * Evaluate device's _DSM method to check whether it supports requested
638 * functions. Currently only support 64 functions at maximum, should be
639 * enough for now.
640 */
641bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
642{
643 int i;
644 u64 mask = 0;
645 union acpi_object *obj;
646
647 if (funcs == 0)
648 return false;
649
650 obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
651 if (!obj)
652 return false;
653
654 /* For compatibility, old BIOSes may return an integer */
655 if (obj->type == ACPI_TYPE_INTEGER)
656 mask = obj->integer.value;
657 else if (obj->type == ACPI_TYPE_BUFFER)
658 for (i = 0; i < obj->buffer.length && i < 8; i++)
659 mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
660 ACPI_FREE(obj);
661
662 /*
663 * Bit 0 indicates whether there's support for any functions other than
664 * function 0 for the specified UUID and revision.
665 */
666 if ((mask & 0x1) && (mask & funcs) == funcs)
667 return true;
668
669 return false;
670}
671EXPORT_SYMBOL(acpi_check_dsm);