Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/init.h> |
| 30 | #include <linux/types.h> |
Toshi Kani | fbfddae | 2012-11-21 01:36:28 +0000 | [diff] [blame] | 31 | #include <linux/hardirq.h> |
| 32 | #include <linux/acpi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <acpi/acpi_bus.h> |
| 34 | #include <acpi/acpi_drivers.h> |
| 35 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 36 | #include "internal.h" |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #define _COMPONENT ACPI_BUS_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 39 | ACPI_MODULE_NAME("utils"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | /* -------------------------------------------------------------------------- |
| 42 | Object Evaluation Helpers |
| 43 | -------------------------------------------------------------------------- */ |
Harvey Harrison | 4fd7f51 | 2008-02-15 17:07:19 -0800 | [diff] [blame] | 44 | static void |
| 45 | acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s) |
| 46 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #ifdef ACPI_DEBUG_OUTPUT |
Harvey Harrison | 4fd7f51 | 2008-02-15 17:07:19 -0800 | [diff] [blame] | 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))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #else |
Harvey Harrison | 4fd7f51 | 2008-02-15 17:07:19 -0800 | [diff] [blame] | 54 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #endif |
Harvey Harrison | 4fd7f51 | 2008-02-15 17:07:19 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 59 | acpi_extract_package(union acpi_object *package, |
| 60 | struct acpi_buffer *format, struct acpi_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 62 | u32 size_required = 0; |
| 63 | u32 tail_offset = 0; |
| 64 | char *format_string = NULL; |
| 65 | u32 format_count = 0; |
| 66 | u32 i = 0; |
| 67 | u8 *head = NULL; |
| 68 | u8 *tail = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 71 | if (!package || (package->type != ACPI_TYPE_PACKAGE) |
| 72 | || (package->package.count < 1)) { |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 73 | printk(KERN_WARNING PREFIX "Invalid package argument\n"); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 74 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (!format || !format->pointer || (format->length < 1)) { |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 78 | printk(KERN_WARNING PREFIX "Invalid format argument\n"); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 79 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (!buffer) { |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 83 | printk(KERN_WARNING PREFIX "Invalid buffer argument\n"); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 84 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 87 | format_count = (format->length / sizeof(char)) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (format_count > package->package.count) { |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 89 | printk(KERN_WARNING PREFIX "Format specifies more objects [%d]" |
| 90 | " than exist in package [%d].\n", |
| 91 | format_count, package->package.count); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 92 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 95 | format_string = format->pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * Calculate size_required. |
| 99 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 100 | for (i = 0; i < format_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | union acpi_object *element = &(package->package.elements[i]); |
| 103 | |
| 104 | if (!element) { |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 105 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | switch (element->type) { |
| 109 | |
| 110 | case ACPI_TYPE_INTEGER: |
| 111 | switch (format_string[i]) { |
| 112 | case 'N': |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 113 | size_required += sizeof(u64); |
| 114 | tail_offset += sizeof(u64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | break; |
| 116 | case 'S': |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 117 | size_required += |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 118 | sizeof(char *) + sizeof(u64) + |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 119 | sizeof(char); |
| 120 | tail_offset += sizeof(char *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | break; |
| 122 | default: |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 123 | printk(KERN_WARNING PREFIX "Invalid package element" |
Colin Ian King | e7e92ec9 | 2013-10-29 10:34:19 +0000 | [diff] [blame] | 124 | " [%d]: got number, expecting" |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 125 | " [%c]\n", |
| 126 | i, format_string[i]); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 127 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | break; |
| 129 | } |
| 130 | break; |
| 131 | |
| 132 | case ACPI_TYPE_STRING: |
| 133 | case ACPI_TYPE_BUFFER: |
| 134 | switch (format_string[i]) { |
| 135 | case 'S': |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 136 | size_required += |
| 137 | sizeof(char *) + |
| 138 | (element->string.length * sizeof(char)) + |
| 139 | sizeof(char); |
| 140 | tail_offset += sizeof(char *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | break; |
| 142 | case 'B': |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 143 | size_required += |
| 144 | sizeof(u8 *) + |
| 145 | (element->buffer.length * sizeof(u8)); |
| 146 | tail_offset += sizeof(u8 *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | break; |
| 148 | default: |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 149 | printk(KERN_WARNING PREFIX "Invalid package element" |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 150 | " [%d] got string/buffer," |
Colin Ian King | e7e92ec9 | 2013-10-29 10:34:19 +0000 | [diff] [blame] | 151 | " expecting [%c]\n", |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 152 | i, format_string[i]); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 153 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | break; |
| 155 | } |
| 156 | break; |
| 157 | |
| 158 | case ACPI_TYPE_PACKAGE: |
| 159 | default: |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 160 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 161 | "Found unsupported element at index=%d\n", |
| 162 | i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* TBD: handle nested packages... */ |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 164 | return AE_SUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Validate output buffer. |
| 171 | */ |
Al Stone | e83dda0 | 2013-10-09 14:21:10 -0600 | [diff] [blame] | 172 | if (buffer->length == ACPI_ALLOCATE_BUFFER) { |
| 173 | buffer->pointer = ACPI_ALLOCATE(size_required); |
| 174 | if (!buffer->pointer) |
| 175 | return AE_NO_MEMORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | buffer->length = size_required; |
Al Stone | e83dda0 | 2013-10-09 14:21:10 -0600 | [diff] [blame] | 177 | memset(buffer->pointer, 0, size_required); |
| 178 | } else { |
| 179 | if (buffer->length < size_required) { |
| 180 | buffer->length = size_required; |
| 181 | return AE_BUFFER_OVERFLOW; |
| 182 | } else if (buffer->length != size_required || |
| 183 | !buffer->pointer) { |
| 184 | return AE_BAD_PARAMETER; |
| 185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | head = buffer->pointer; |
| 189 | tail = buffer->pointer + tail_offset; |
| 190 | |
| 191 | /* |
| 192 | * Extract package data. |
| 193 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 194 | for (i = 0; i < format_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
| 196 | u8 **pointer = NULL; |
| 197 | union acpi_object *element = &(package->package.elements[i]); |
| 198 | |
| 199 | if (!element) { |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 200 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | switch (element->type) { |
| 204 | |
| 205 | case ACPI_TYPE_INTEGER: |
| 206 | switch (format_string[i]) { |
| 207 | case 'N': |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 208 | *((u64 *) head) = |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 209 | element->integer.value; |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 210 | head += sizeof(u64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | break; |
| 212 | case 'S': |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 213 | pointer = (u8 **) head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | *pointer = tail; |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 215 | *((u64 *) tail) = |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 216 | element->integer.value; |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 217 | head += sizeof(u64 *); |
| 218 | tail += sizeof(u64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | /* NULL terminate string */ |
| 220 | *tail = (char)0; |
| 221 | tail += sizeof(char); |
| 222 | break; |
| 223 | default: |
| 224 | /* Should never get here */ |
| 225 | break; |
| 226 | } |
| 227 | break; |
| 228 | |
| 229 | case ACPI_TYPE_STRING: |
| 230 | case ACPI_TYPE_BUFFER: |
| 231 | switch (format_string[i]) { |
| 232 | case 'S': |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 233 | pointer = (u8 **) head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | *pointer = tail; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 235 | memcpy(tail, element->string.pointer, |
| 236 | element->string.length); |
| 237 | head += sizeof(char *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | tail += element->string.length * sizeof(char); |
| 239 | /* NULL terminate string */ |
| 240 | *tail = (char)0; |
| 241 | tail += sizeof(char); |
| 242 | break; |
| 243 | case 'B': |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 244 | pointer = (u8 **) head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | *pointer = tail; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 246 | memcpy(tail, element->buffer.pointer, |
| 247 | element->buffer.length); |
| 248 | head += sizeof(u8 *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | tail += element->buffer.length * sizeof(u8); |
| 250 | break; |
| 251 | default: |
| 252 | /* Should never get here */ |
| 253 | break; |
| 254 | } |
| 255 | break; |
| 256 | |
| 257 | case ACPI_TYPE_PACKAGE: |
| 258 | /* TBD: handle nested packages... */ |
| 259 | default: |
| 260 | /* Should never get here */ |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 265 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | EXPORT_SYMBOL(acpi_extract_package); |
| 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 271 | acpi_evaluate_integer(acpi_handle handle, |
| 272 | acpi_string pathname, |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 273 | struct acpi_object_list *arguments, unsigned long long *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 275 | acpi_status status = AE_OK; |
Pavel Machek | 4059907 | 2008-11-25 12:05:08 +0100 | [diff] [blame] | 276 | union acpi_object element; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 277 | struct acpi_buffer buffer = { 0, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | if (!data) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 280 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | buffer.length = sizeof(union acpi_object); |
Pavel Machek | 4059907 | 2008-11-25 12:05:08 +0100 | [diff] [blame] | 283 | buffer.pointer = &element; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | status = acpi_evaluate_object(handle, pathname, arguments, &buffer); |
| 285 | if (ACPI_FAILURE(status)) { |
| 286 | acpi_util_eval_error(handle, pathname, status); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 287 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Pavel Machek | 4059907 | 2008-11-25 12:05:08 +0100 | [diff] [blame] | 290 | if (element.type != ACPI_TYPE_INTEGER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | acpi_util_eval_error(handle, pathname, AE_BAD_DATA); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 292 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Pavel Machek | 4059907 | 2008-11-25 12:05:08 +0100 | [diff] [blame] | 295 | *data = element.integer.value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 297 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 299 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 302 | EXPORT_SYMBOL(acpi_evaluate_integer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 305 | acpi_evaluate_reference(acpi_handle handle, |
| 306 | acpi_string pathname, |
| 307 | struct acpi_object_list *arguments, |
| 308 | struct acpi_handle_list *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 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 }; |
| 314 | u32 i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | if (!list) { |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 318 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* Evaluate object. */ |
| 322 | |
| 323 | status = acpi_evaluate_object(handle, pathname, arguments, &buffer); |
| 324 | if (ACPI_FAILURE(status)) |
| 325 | goto end; |
| 326 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 327 | package = buffer.pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
| 329 | if ((buffer.length == 0) || !package) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 330 | printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n", |
| 331 | (unsigned)buffer.length, package); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | status = AE_BAD_DATA; |
| 333 | acpi_util_eval_error(handle, pathname, status); |
| 334 | goto end; |
| 335 | } |
| 336 | if (package->type != ACPI_TYPE_PACKAGE) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 337 | printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n", |
| 338 | package->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | status = AE_BAD_DATA; |
| 340 | acpi_util_eval_error(handle, pathname, status); |
| 341 | goto end; |
| 342 | } |
| 343 | if (!package->package.count) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 344 | printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n", |
| 345 | package); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | status = AE_BAD_DATA; |
| 347 | acpi_util_eval_error(handle, pathname, status); |
| 348 | goto end; |
| 349 | } |
| 350 | |
| 351 | if (package->package.count > ACPI_MAX_HANDLES) { |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 352 | return AE_NO_MEMORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | list->count = package->package.count; |
| 355 | |
| 356 | /* Extract package data. */ |
| 357 | |
| 358 | for (i = 0; i < list->count; i++) { |
| 359 | |
| 360 | element = &(package->package.elements[i]); |
| 361 | |
Bob Moore | cd0b224 | 2008-04-10 19:06:43 +0400 | [diff] [blame] | 362 | if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | status = AE_BAD_DATA; |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 364 | printk(KERN_ERR PREFIX |
| 365 | "Expecting a [Reference] package element, found type %X\n", |
| 366 | element->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | acpi_util_eval_error(handle, pathname, status); |
| 368 | break; |
| 369 | } |
| 370 | |
Thomas Renninger | b6a1638 | 2008-03-12 01:06:24 +0100 | [diff] [blame] | 371 | if (!element->reference.handle) { |
| 372 | printk(KERN_WARNING PREFIX "Invalid reference in" |
| 373 | " package %s\n", pathname); |
| 374 | status = AE_NULL_ENTRY; |
| 375 | break; |
| 376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | /* Get the acpi_handle. */ |
| 378 | |
| 379 | list->handles[i] = element->reference.handle; |
| 380 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 381 | list->handles[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 384 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | if (ACPI_FAILURE(status)) { |
| 386 | list->count = 0; |
| 387 | //kfree(list->handles); |
| 388 | } |
| 389 | |
Len Brown | 02438d8 | 2006-06-30 03:19:10 -0400 | [diff] [blame] | 390 | kfree(buffer.pointer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 392 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 395 | EXPORT_SYMBOL(acpi_evaluate_reference); |
Matthew Garrett | 38ac0f1 | 2012-05-11 16:08:26 +0800 | [diff] [blame] | 396 | |
| 397 | acpi_status |
Feng Tang | 8ede06a | 2012-08-21 09:56:58 +0800 | [diff] [blame] | 398 | acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld) |
Matthew Garrett | 38ac0f1 | 2012-05-11 16:08:26 +0800 | [diff] [blame] | 399 | { |
| 400 | acpi_status status; |
| 401 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 402 | union acpi_object *output; |
| 403 | |
| 404 | status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer); |
| 405 | |
| 406 | if (ACPI_FAILURE(status)) |
| 407 | return status; |
| 408 | |
| 409 | output = buffer.pointer; |
| 410 | |
| 411 | if (!output || output->type != ACPI_TYPE_PACKAGE |
| 412 | || !output->package.count |
| 413 | || output->package.elements[0].type != ACPI_TYPE_BUFFER |
Feng Tang | 8ede06a | 2012-08-21 09:56:58 +0800 | [diff] [blame] | 414 | || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) { |
Matthew Garrett | 38ac0f1 | 2012-05-11 16:08:26 +0800 | [diff] [blame] | 415 | status = AE_TYPE; |
| 416 | goto out; |
| 417 | } |
| 418 | |
Feng Tang | 8ede06a | 2012-08-21 09:56:58 +0800 | [diff] [blame] | 419 | status = acpi_decode_pld_buffer( |
| 420 | output->package.elements[0].buffer.pointer, |
| 421 | output->package.elements[0].buffer.length, |
| 422 | pld); |
| 423 | |
Matthew Garrett | 38ac0f1 | 2012-05-11 16:08:26 +0800 | [diff] [blame] | 424 | out: |
| 425 | kfree(buffer.pointer); |
| 426 | return status; |
| 427 | } |
| 428 | EXPORT_SYMBOL(acpi_get_physical_device_location); |
Toshi Kani | 275c58d | 2012-05-23 20:25:19 -0600 | [diff] [blame] | 429 | |
| 430 | /** |
| 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) |
| 436 | * |
| 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. |
| 440 | */ |
| 441 | acpi_status |
| 442 | acpi_evaluate_hotplug_ost(acpi_handle handle, u32 source_event, |
| 443 | u32 status_code, struct acpi_buffer *status_buf) |
| 444 | { |
| 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,} |
| 450 | }; |
| 451 | struct acpi_object_list arg_list = {3, params}; |
| 452 | acpi_status status; |
| 453 | |
| 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; |
| 459 | } else { |
| 460 | params[2].buffer.pointer = NULL; |
| 461 | params[2].buffer.length = 0; |
| 462 | } |
| 463 | |
| 464 | status = acpi_evaluate_object(handle, "_OST", &arg_list, NULL); |
| 465 | return status; |
| 466 | #else |
| 467 | return AE_OK; |
| 468 | #endif |
| 469 | } |
| 470 | EXPORT_SYMBOL(acpi_evaluate_hotplug_ost); |
Toshi Kani | fbfddae | 2012-11-21 01:36:28 +0000 | [diff] [blame] | 471 | |
| 472 | /** |
| 473 | * acpi_handle_printk: Print message with ACPI prefix and object path |
| 474 | * |
| 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>. |
| 479 | */ |
| 480 | void |
| 481 | acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...) |
| 482 | { |
| 483 | struct va_format vaf; |
| 484 | va_list args; |
| 485 | struct acpi_buffer buffer = { |
| 486 | .length = ACPI_ALLOCATE_BUFFER, |
| 487 | .pointer = NULL |
| 488 | }; |
| 489 | const char *path; |
| 490 | |
| 491 | va_start(args, fmt); |
| 492 | vaf.fmt = fmt; |
| 493 | vaf.va = &args; |
| 494 | |
| 495 | if (in_interrupt() || |
| 496 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK) |
| 497 | path = "<n/a>"; |
| 498 | else |
| 499 | path = buffer.pointer; |
| 500 | |
| 501 | printk("%sACPI: %s: %pV", level, path, &vaf); |
| 502 | |
| 503 | va_end(args); |
| 504 | kfree(buffer.pointer); |
| 505 | } |
| 506 | EXPORT_SYMBOL(acpi_handle_printk); |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 507 | |
| 508 | /** |
| 509 | * acpi_has_method: Check whether @handle has a method named @name |
| 510 | * @handle: ACPI device handle |
| 511 | * @name: name of object or method |
| 512 | * |
| 513 | * Check whether @handle has a method named @name. |
| 514 | */ |
| 515 | bool acpi_has_method(acpi_handle handle, char *name) |
| 516 | { |
| 517 | acpi_handle tmp; |
| 518 | |
| 519 | return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp)); |
| 520 | } |
| 521 | EXPORT_SYMBOL(acpi_has_method); |
Jiang Liu | 0db9820 | 2013-06-29 00:24:39 +0800 | [diff] [blame] | 522 | |
| 523 | acpi_status acpi_execute_simple_method(acpi_handle handle, char *method, |
| 524 | u64 arg) |
| 525 | { |
| 526 | union acpi_object obj = { .type = ACPI_TYPE_INTEGER }; |
| 527 | struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, }; |
| 528 | |
| 529 | obj.integer.value = arg; |
| 530 | |
| 531 | return acpi_evaluate_object(handle, method, &arg_list, NULL); |
| 532 | } |
| 533 | EXPORT_SYMBOL(acpi_execute_simple_method); |
Jiang Liu | 7d2421f | 2013-06-29 00:24:40 +0800 | [diff] [blame] | 534 | |
| 535 | /** |
| 536 | * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations |
| 537 | * @handle: ACPI device handle |
| 538 | * |
| 539 | * Evaluate device's _EJ0 method for hotplug operations. |
| 540 | */ |
| 541 | acpi_status acpi_evaluate_ej0(acpi_handle handle) |
| 542 | { |
| 543 | acpi_status status; |
| 544 | |
| 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); |
| 550 | |
| 551 | return status; |
| 552 | } |
| 553 | |
| 554 | /** |
| 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 |
| 558 | * |
| 559 | * Evaluate device's _LCK method if present to lock/unlock device |
| 560 | */ |
| 561 | acpi_status acpi_evaluate_lck(acpi_handle handle, int lock) |
| 562 | { |
| 563 | acpi_status status; |
| 564 | |
| 565 | status = acpi_execute_simple_method(handle, "_LCK", !!lock); |
| 566 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { |
| 567 | if (lock) |
| 568 | acpi_handle_warn(handle, |
| 569 | "Locking device failed (0x%x)\n", status); |
| 570 | else |
| 571 | acpi_handle_warn(handle, |
| 572 | "Unlocking device failed (0x%x)\n", status); |
| 573 | } |
| 574 | |
| 575 | return status; |
| 576 | } |