2 * acpi_utils.c - ACPI Utility Functions ($Revision: 10 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/hardirq.h>
32 #include <linux/acpi.h>
33 #include <acpi/acpi_bus.h>
34 #include <acpi/acpi_drivers.h>
38 #define _COMPONENT ACPI_BUS_COMPONENT
39 ACPI_MODULE_NAME("utils");
41 /* --------------------------------------------------------------------------
42 Object Evaluation Helpers
43 -------------------------------------------------------------------------- */
45 acpi_util_eval_error(acpi_handle h
, acpi_string p
, acpi_status s
)
47 #ifdef ACPI_DEBUG_OUTPUT
48 char prefix
[80] = {'\0'};
49 struct acpi_buffer buffer
= {sizeof(prefix
), prefix
};
50 acpi_get_name(h
, ACPI_FULL_PATHNAME
, &buffer
);
51 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Evaluate [%s.%s]: %s\n",
52 (char *) prefix
, p
, acpi_format_exception(s
)));
59 acpi_extract_package(union acpi_object
*package
,
60 struct acpi_buffer
*format
, struct acpi_buffer
*buffer
)
62 u32 size_required
= 0;
64 char *format_string
= NULL
;
71 if (!package
|| (package
->type
!= ACPI_TYPE_PACKAGE
)
72 || (package
->package
.count
< 1)) {
73 printk(KERN_WARNING PREFIX
"Invalid package argument\n");
74 return AE_BAD_PARAMETER
;
77 if (!format
|| !format
->pointer
|| (format
->length
< 1)) {
78 printk(KERN_WARNING PREFIX
"Invalid format argument\n");
79 return AE_BAD_PARAMETER
;
83 printk(KERN_WARNING PREFIX
"Invalid buffer argument\n");
84 return AE_BAD_PARAMETER
;
87 format_count
= (format
->length
/ sizeof(char)) - 1;
88 if (format_count
> package
->package
.count
) {
89 printk(KERN_WARNING PREFIX
"Format specifies more objects [%d]"
90 " than exist in package [%d].\n",
91 format_count
, package
->package
.count
);
95 format_string
= format
->pointer
;
98 * Calculate size_required.
100 for (i
= 0; i
< format_count
; i
++) {
102 union acpi_object
*element
= &(package
->package
.elements
[i
]);
108 switch (element
->type
) {
110 case ACPI_TYPE_INTEGER
:
111 switch (format_string
[i
]) {
113 size_required
+= sizeof(u64
);
114 tail_offset
+= sizeof(u64
);
118 sizeof(char *) + sizeof(u64
) +
120 tail_offset
+= sizeof(char *);
123 printk(KERN_WARNING PREFIX
"Invalid package element"
124 " [%d]: got number, expecting"
126 i
, format_string
[i
]);
132 case ACPI_TYPE_STRING
:
133 case ACPI_TYPE_BUFFER
:
134 switch (format_string
[i
]) {
138 (element
->string
.length
* sizeof(char)) +
140 tail_offset
+= sizeof(char *);
145 (element
->buffer
.length
* sizeof(u8
));
146 tail_offset
+= sizeof(u8
*);
149 printk(KERN_WARNING PREFIX
"Invalid package element"
150 " [%d] got string/buffer,"
152 i
, format_string
[i
]);
158 case ACPI_TYPE_PACKAGE
:
160 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
161 "Found unsupported element at index=%d\n",
163 /* TBD: handle nested packages... */
170 * Validate output buffer.
172 if (buffer
->length
== ACPI_ALLOCATE_BUFFER
) {
173 buffer
->pointer
= ACPI_ALLOCATE(size_required
);
174 if (!buffer
->pointer
)
176 buffer
->length
= size_required
;
177 memset(buffer
->pointer
, 0, size_required
);
179 if (buffer
->length
< size_required
) {
180 buffer
->length
= size_required
;
181 return AE_BUFFER_OVERFLOW
;
182 } else if (buffer
->length
!= size_required
||
184 return AE_BAD_PARAMETER
;
188 head
= buffer
->pointer
;
189 tail
= buffer
->pointer
+ tail_offset
;
192 * Extract package data.
194 for (i
= 0; i
< format_count
; i
++) {
197 union acpi_object
*element
= &(package
->package
.elements
[i
]);
203 switch (element
->type
) {
205 case ACPI_TYPE_INTEGER
:
206 switch (format_string
[i
]) {
209 element
->integer
.value
;
213 pointer
= (u8
**) head
;
216 element
->integer
.value
;
217 head
+= sizeof(u64
*);
219 /* NULL terminate string */
221 tail
+= sizeof(char);
224 /* Should never get here */
229 case ACPI_TYPE_STRING
:
230 case ACPI_TYPE_BUFFER
:
231 switch (format_string
[i
]) {
233 pointer
= (u8
**) head
;
235 memcpy(tail
, element
->string
.pointer
,
236 element
->string
.length
);
237 head
+= sizeof(char *);
238 tail
+= element
->string
.length
* sizeof(char);
239 /* NULL terminate string */
241 tail
+= sizeof(char);
244 pointer
= (u8
**) head
;
246 memcpy(tail
, element
->buffer
.pointer
,
247 element
->buffer
.length
);
248 head
+= sizeof(u8
*);
249 tail
+= element
->buffer
.length
* sizeof(u8
);
252 /* Should never get here */
257 case ACPI_TYPE_PACKAGE
:
258 /* TBD: handle nested packages... */
260 /* Should never get here */
268 EXPORT_SYMBOL(acpi_extract_package
);
271 acpi_evaluate_integer(acpi_handle handle
,
272 acpi_string pathname
,
273 struct acpi_object_list
*arguments
, unsigned long long *data
)
275 acpi_status status
= AE_OK
;
276 union acpi_object element
;
277 struct acpi_buffer buffer
= { 0, NULL
};
280 return AE_BAD_PARAMETER
;
282 buffer
.length
= sizeof(union acpi_object
);
283 buffer
.pointer
= &element
;
284 status
= acpi_evaluate_object(handle
, pathname
, arguments
, &buffer
);
285 if (ACPI_FAILURE(status
)) {
286 acpi_util_eval_error(handle
, pathname
, status
);
290 if (element
.type
!= ACPI_TYPE_INTEGER
) {
291 acpi_util_eval_error(handle
, pathname
, AE_BAD_DATA
);
295 *data
= element
.integer
.value
;
297 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Return value [%llu]\n", *data
));
302 EXPORT_SYMBOL(acpi_evaluate_integer
);
305 acpi_evaluate_reference(acpi_handle handle
,
306 acpi_string pathname
,
307 struct acpi_object_list
*arguments
,
308 struct acpi_handle_list
*list
)
310 acpi_status status
= AE_OK
;
311 union acpi_object
*package
= NULL
;
312 union acpi_object
*element
= NULL
;
313 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
318 return AE_BAD_PARAMETER
;
321 /* Evaluate object. */
323 status
= acpi_evaluate_object(handle
, pathname
, arguments
, &buffer
);
324 if (ACPI_FAILURE(status
))
327 package
= buffer
.pointer
;
329 if ((buffer
.length
== 0) || !package
) {
330 printk(KERN_ERR PREFIX
"No return object (len %X ptr %p)\n",
331 (unsigned)buffer
.length
, package
);
332 status
= AE_BAD_DATA
;
333 acpi_util_eval_error(handle
, pathname
, status
);
336 if (package
->type
!= ACPI_TYPE_PACKAGE
) {
337 printk(KERN_ERR PREFIX
"Expecting a [Package], found type %X\n",
339 status
= AE_BAD_DATA
;
340 acpi_util_eval_error(handle
, pathname
, status
);
343 if (!package
->package
.count
) {
344 printk(KERN_ERR PREFIX
"[Package] has zero elements (%p)\n",
346 status
= AE_BAD_DATA
;
347 acpi_util_eval_error(handle
, pathname
, status
);
351 if (package
->package
.count
> ACPI_MAX_HANDLES
) {
354 list
->count
= package
->package
.count
;
356 /* Extract package data. */
358 for (i
= 0; i
< list
->count
; i
++) {
360 element
= &(package
->package
.elements
[i
]);
362 if (element
->type
!= ACPI_TYPE_LOCAL_REFERENCE
) {
363 status
= AE_BAD_DATA
;
364 printk(KERN_ERR PREFIX
365 "Expecting a [Reference] package element, found type %X\n",
367 acpi_util_eval_error(handle
, pathname
, status
);
371 if (!element
->reference
.handle
) {
372 printk(KERN_WARNING PREFIX
"Invalid reference in"
373 " package %s\n", pathname
);
374 status
= AE_NULL_ENTRY
;
377 /* Get the acpi_handle. */
379 list
->handles
[i
] = element
->reference
.handle
;
380 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found reference [%p]\n",
385 if (ACPI_FAILURE(status
)) {
387 //kfree(list->handles);
390 kfree(buffer
.pointer
);
395 EXPORT_SYMBOL(acpi_evaluate_reference
);
398 acpi_get_physical_device_location(acpi_handle handle
, struct acpi_pld_info
**pld
)
401 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
402 union acpi_object
*output
;
404 status
= acpi_evaluate_object(handle
, "_PLD", NULL
, &buffer
);
406 if (ACPI_FAILURE(status
))
409 output
= buffer
.pointer
;
411 if (!output
|| output
->type
!= ACPI_TYPE_PACKAGE
412 || !output
->package
.count
413 || output
->package
.elements
[0].type
!= ACPI_TYPE_BUFFER
414 || output
->package
.elements
[0].buffer
.length
< ACPI_PLD_REV1_BUFFER_SIZE
) {
419 status
= acpi_decode_pld_buffer(
420 output
->package
.elements
[0].buffer
.pointer
,
421 output
->package
.elements
[0].buffer
.length
,
425 kfree(buffer
.pointer
);
428 EXPORT_SYMBOL(acpi_get_physical_device_location
);
431 * acpi_evaluate_hotplug_ost: Evaluate _OST for hotplug operations
432 * @handle: ACPI device handle
433 * @source_event: source event code
434 * @status_code: status code
435 * @status_buf: optional detailed information (NULL if none)
437 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
438 * must call this function when evaluating _OST for hotplug operations.
439 * When the platform does not support _OST, this function has no effect.
442 acpi_evaluate_hotplug_ost(acpi_handle handle
, u32 source_event
,
443 u32 status_code
, struct acpi_buffer
*status_buf
)
445 #ifdef ACPI_HOTPLUG_OST
446 union acpi_object params
[3] = {
447 {.type
= ACPI_TYPE_INTEGER
,},
448 {.type
= ACPI_TYPE_INTEGER
,},
449 {.type
= ACPI_TYPE_BUFFER
,}
451 struct acpi_object_list arg_list
= {3, params
};
454 params
[0].integer
.value
= source_event
;
455 params
[1].integer
.value
= status_code
;
456 if (status_buf
!= NULL
) {
457 params
[2].buffer
.pointer
= status_buf
->pointer
;
458 params
[2].buffer
.length
= status_buf
->length
;
460 params
[2].buffer
.pointer
= NULL
;
461 params
[2].buffer
.length
= 0;
464 status
= acpi_evaluate_object(handle
, "_OST", &arg_list
, NULL
);
470 EXPORT_SYMBOL(acpi_evaluate_hotplug_ost
);
473 * acpi_handle_printk: Print message with ACPI prefix and object path
475 * This function is called through acpi_handle_<level> macros and prints
476 * a message with ACPI prefix and object path. This function acquires
477 * the global namespace mutex to obtain an object path. In interrupt
478 * context, it shows the object path as <n/a>.
481 acpi_handle_printk(const char *level
, acpi_handle handle
, const char *fmt
, ...)
483 struct va_format vaf
;
485 struct acpi_buffer buffer
= {
486 .length
= ACPI_ALLOCATE_BUFFER
,
495 if (in_interrupt() ||
496 acpi_get_name(handle
, ACPI_FULL_PATHNAME
, &buffer
) != AE_OK
)
499 path
= buffer
.pointer
;
501 printk("%sACPI: %s: %pV", level
, path
, &vaf
);
504 kfree(buffer
.pointer
);
506 EXPORT_SYMBOL(acpi_handle_printk
);
509 * acpi_has_method: Check whether @handle has a method named @name
510 * @handle: ACPI device handle
511 * @name: name of object or method
513 * Check whether @handle has a method named @name.
515 bool acpi_has_method(acpi_handle handle
, char *name
)
519 return ACPI_SUCCESS(acpi_get_handle(handle
, name
, &tmp
));
521 EXPORT_SYMBOL(acpi_has_method
);
523 acpi_status
acpi_execute_simple_method(acpi_handle handle
, char *method
,
526 union acpi_object obj
= { .type
= ACPI_TYPE_INTEGER
};
527 struct acpi_object_list arg_list
= { .count
= 1, .pointer
= &obj
, };
529 obj
.integer
.value
= arg
;
531 return acpi_evaluate_object(handle
, method
, &arg_list
, NULL
);
533 EXPORT_SYMBOL(acpi_execute_simple_method
);
536 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
537 * @handle: ACPI device handle
539 * Evaluate device's _EJ0 method for hotplug operations.
541 acpi_status
acpi_evaluate_ej0(acpi_handle handle
)
545 status
= acpi_execute_simple_method(handle
, "_EJ0", 1);
546 if (status
== AE_NOT_FOUND
)
547 acpi_handle_warn(handle
, "No _EJ0 support for device\n");
548 else if (ACPI_FAILURE(status
))
549 acpi_handle_warn(handle
, "Eject failed (0x%x)\n", status
);
555 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
556 * @handle: ACPI device handle
557 * @lock: lock device if non-zero, otherwise unlock device
559 * Evaluate device's _LCK method if present to lock/unlock device
561 acpi_status
acpi_evaluate_lck(acpi_handle handle
, int lock
)
565 status
= acpi_execute_simple_method(handle
, "_LCK", !!lock
);
566 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
568 acpi_handle_warn(handle
,
569 "Locking device failed (0x%x)\n", status
);
571 acpi_handle_warn(handle
,
572 "Unlocking device failed (0x%x)\n", status
);