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 | |
Len Brown | a192a95 | 2009-07-28 16:45:54 -0400 | [diff] [blame] | 34 | #include "internal.h" |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #define _COMPONENT ACPI_BUS_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 37 | ACPI_MODULE_NAME("utils"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | /* -------------------------------------------------------------------------- |
| 40 | Object Evaluation Helpers |
| 41 | -------------------------------------------------------------------------- */ |
Harvey Harrison | 4fd7f51 | 2008-02-15 17:07:19 -0800 | [diff] [blame] | 42 | static void |
| 43 | acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s) |
| 44 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #ifdef ACPI_DEBUG_OUTPUT |
Harvey Harrison | 4fd7f51 | 2008-02-15 17:07:19 -0800 | [diff] [blame] | 46 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #else |
Harvey Harrison | 4fd7f51 | 2008-02-15 17:07:19 -0800 | [diff] [blame] | 52 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #endif |
Harvey Harrison | 4fd7f51 | 2008-02-15 17:07:19 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 57 | acpi_extract_package(union acpi_object *package, |
| 58 | struct acpi_buffer *format, struct acpi_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 60 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 69 | if (!package || (package->type != ACPI_TYPE_PACKAGE) |
| 70 | || (package->package.count < 1)) { |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 71 | printk(KERN_WARNING PREFIX "Invalid package argument\n"); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 72 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | if (!format || !format->pointer || (format->length < 1)) { |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 76 | printk(KERN_WARNING PREFIX "Invalid format argument\n"); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 77 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if (!buffer) { |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 81 | printk(KERN_WARNING PREFIX "Invalid buffer argument\n"); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 82 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 85 | format_count = (format->length / sizeof(char)) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | if (format_count > package->package.count) { |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 87 | printk(KERN_WARNING PREFIX "Format specifies more objects [%d]" |
| 88 | " than exist in package [%d].\n", |
| 89 | format_count, package->package.count); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 90 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 93 | format_string = format->pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * Calculate size_required. |
| 97 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 98 | for (i = 0; i < format_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | union acpi_object *element = &(package->package.elements[i]); |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | switch (element->type) { |
| 103 | |
| 104 | case ACPI_TYPE_INTEGER: |
| 105 | switch (format_string[i]) { |
| 106 | case 'N': |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 107 | size_required += sizeof(u64); |
| 108 | tail_offset += sizeof(u64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | break; |
| 110 | case 'S': |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 111 | size_required += |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 112 | sizeof(char *) + sizeof(u64) + |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 113 | sizeof(char); |
| 114 | tail_offset += sizeof(char *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | break; |
| 116 | default: |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 117 | printk(KERN_WARNING PREFIX "Invalid package element" |
Colin Ian King | e7e92ec9 | 2013-10-29 10:34:19 +0000 | [diff] [blame] | 118 | " [%d]: got number, expecting" |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 119 | " [%c]\n", |
| 120 | i, format_string[i]); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 121 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | break; |
| 125 | |
| 126 | case ACPI_TYPE_STRING: |
| 127 | case ACPI_TYPE_BUFFER: |
| 128 | switch (format_string[i]) { |
| 129 | case 'S': |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 130 | size_required += |
| 131 | sizeof(char *) + |
| 132 | (element->string.length * sizeof(char)) + |
| 133 | sizeof(char); |
| 134 | tail_offset += sizeof(char *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | break; |
| 136 | case 'B': |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 137 | size_required += |
| 138 | sizeof(u8 *) + |
| 139 | (element->buffer.length * sizeof(u8)); |
| 140 | tail_offset += sizeof(u8 *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | break; |
| 142 | default: |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 143 | printk(KERN_WARNING PREFIX "Invalid package element" |
Thomas Renninger | a6fc672 | 2006-06-26 23:58:43 -0400 | [diff] [blame] | 144 | " [%d] got string/buffer," |
Colin Ian King | e7e92ec9 | 2013-10-29 10:34:19 +0000 | [diff] [blame] | 145 | " expecting [%c]\n", |
Len Brown | cece929 | 2006-06-26 23:04:31 -0400 | [diff] [blame] | 146 | i, format_string[i]); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 147 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | break; |
| 151 | |
| 152 | case ACPI_TYPE_PACKAGE: |
| 153 | default: |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 154 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 155 | "Found unsupported element at index=%d\n", |
| 156 | i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* TBD: handle nested packages... */ |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 158 | return AE_SUPPORT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | break; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Validate output buffer. |
| 165 | */ |
Al Stone | e83dda0 | 2013-10-09 14:21:10 -0600 | [diff] [blame] | 166 | if (buffer->length == ACPI_ALLOCATE_BUFFER) { |
| 167 | buffer->pointer = ACPI_ALLOCATE(size_required); |
| 168 | if (!buffer->pointer) |
| 169 | return AE_NO_MEMORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | buffer->length = size_required; |
Al Stone | e83dda0 | 2013-10-09 14:21:10 -0600 | [diff] [blame] | 171 | memset(buffer->pointer, 0, size_required); |
| 172 | } 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | head = buffer->pointer; |
| 183 | tail = buffer->pointer + tail_offset; |
| 184 | |
| 185 | /* |
| 186 | * Extract package data. |
| 187 | */ |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 188 | for (i = 0; i < format_count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | u8 **pointer = NULL; |
| 191 | union acpi_object *element = &(package->package.elements[i]); |
| 192 | |
| 193 | if (!element) { |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 194 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | switch (element->type) { |
| 198 | |
| 199 | case ACPI_TYPE_INTEGER: |
| 200 | switch (format_string[i]) { |
| 201 | case 'N': |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 202 | *((u64 *) head) = |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 203 | element->integer.value; |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 204 | head += sizeof(u64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | break; |
| 206 | case 'S': |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 207 | pointer = (u8 **) head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | *pointer = tail; |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 209 | *((u64 *) tail) = |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 210 | element->integer.value; |
Lin Ming | 439913f | 2010-01-28 10:53:19 +0800 | [diff] [blame] | 211 | head += sizeof(u64 *); |
| 212 | tail += sizeof(u64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | /* 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 Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 227 | pointer = (u8 **) head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | *pointer = tail; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 229 | memcpy(tail, element->string.pointer, |
| 230 | element->string.length); |
| 231 | head += sizeof(char *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | tail += element->string.length * sizeof(char); |
| 233 | /* NULL terminate string */ |
| 234 | *tail = (char)0; |
| 235 | tail += sizeof(char); |
| 236 | break; |
| 237 | case 'B': |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 238 | pointer = (u8 **) head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | *pointer = tail; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 240 | memcpy(tail, element->buffer.pointer, |
| 241 | element->buffer.length); |
| 242 | head += sizeof(u8 *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | 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 Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 259 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | EXPORT_SYMBOL(acpi_extract_package); |
| 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 265 | acpi_evaluate_integer(acpi_handle handle, |
| 266 | acpi_string pathname, |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 267 | struct acpi_object_list *arguments, unsigned long long *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 269 | acpi_status status = AE_OK; |
Pavel Machek | 4059907 | 2008-11-25 12:05:08 +0100 | [diff] [blame] | 270 | union acpi_object element; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 271 | struct acpi_buffer buffer = { 0, NULL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | if (!data) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 274 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | buffer.length = sizeof(union acpi_object); |
Pavel Machek | 4059907 | 2008-11-25 12:05:08 +0100 | [diff] [blame] | 277 | buffer.pointer = &element; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | status = acpi_evaluate_object(handle, pathname, arguments, &buffer); |
| 279 | if (ACPI_FAILURE(status)) { |
| 280 | acpi_util_eval_error(handle, pathname, status); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 281 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Pavel Machek | 4059907 | 2008-11-25 12:05:08 +0100 | [diff] [blame] | 284 | if (element.type != ACPI_TYPE_INTEGER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | acpi_util_eval_error(handle, pathname, AE_BAD_DATA); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 286 | return AE_BAD_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Pavel Machek | 4059907 | 2008-11-25 12:05:08 +0100 | [diff] [blame] | 289 | *data = element.integer.value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Matthew Wilcox | 27663c5 | 2008-10-10 02:22:59 -0400 | [diff] [blame] | 291 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 293 | return AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 296 | EXPORT_SYMBOL(acpi_evaluate_integer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 299 | acpi_evaluate_reference(acpi_handle handle, |
| 300 | acpi_string pathname, |
| 301 | struct acpi_object_list *arguments, |
| 302 | struct acpi_handle_list *list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 304 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | if (!list) { |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 312 | return AE_BAD_PARAMETER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* Evaluate object. */ |
| 316 | |
| 317 | status = acpi_evaluate_object(handle, pathname, arguments, &buffer); |
| 318 | if (ACPI_FAILURE(status)) |
| 319 | goto end; |
| 320 | |
Jan Engelhardt | 50dd096 | 2006-10-01 00:28:50 +0200 | [diff] [blame] | 321 | package = buffer.pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
| 323 | if ((buffer.length == 0) || !package) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 324 | printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n", |
| 325 | (unsigned)buffer.length, package); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | status = AE_BAD_DATA; |
| 327 | acpi_util_eval_error(handle, pathname, status); |
| 328 | goto end; |
| 329 | } |
| 330 | if (package->type != ACPI_TYPE_PACKAGE) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 331 | printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n", |
| 332 | package->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | status = AE_BAD_DATA; |
| 334 | acpi_util_eval_error(handle, pathname, status); |
| 335 | goto end; |
| 336 | } |
| 337 | if (!package->package.count) { |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 338 | printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n", |
| 339 | package); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | 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 Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 346 | return AE_NO_MEMORY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 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 Moore | cd0b224 | 2008-04-10 19:06:43 +0400 | [diff] [blame] | 356 | if (element->type != ACPI_TYPE_LOCAL_REFERENCE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | status = AE_BAD_DATA; |
Len Brown | 6468463 | 2006-06-26 23:41:38 -0400 | [diff] [blame] | 358 | printk(KERN_ERR PREFIX |
| 359 | "Expecting a [Reference] package element, found type %X\n", |
| 360 | element->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | acpi_util_eval_error(handle, pathname, status); |
| 362 | break; |
| 363 | } |
| 364 | |
Thomas Renninger | b6a1638 | 2008-03-12 01:06:24 +0100 | [diff] [blame] | 365 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | /* Get the acpi_handle. */ |
| 372 | |
| 373 | list->handles[i] = element->reference.handle; |
| 374 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n", |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 375 | list->handles[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 378 | end: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | if (ACPI_FAILURE(status)) { |
| 380 | list->count = 0; |
| 381 | //kfree(list->handles); |
| 382 | } |
| 383 | |
Len Brown | 02438d8 | 2006-06-30 03:19:10 -0400 | [diff] [blame] | 384 | kfree(buffer.pointer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 386 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 389 | EXPORT_SYMBOL(acpi_evaluate_reference); |
Matthew Garrett | 38ac0f1 | 2012-05-11 16:08:26 +0800 | [diff] [blame] | 390 | |
| 391 | acpi_status |
Feng Tang | 8ede06a | 2012-08-21 09:56:58 +0800 | [diff] [blame] | 392 | 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] | 393 | { |
| 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 Tang | 8ede06a | 2012-08-21 09:56:58 +0800 | [diff] [blame] | 408 | || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) { |
Matthew Garrett | 38ac0f1 | 2012-05-11 16:08:26 +0800 | [diff] [blame] | 409 | status = AE_TYPE; |
| 410 | goto out; |
| 411 | } |
| 412 | |
Feng Tang | 8ede06a | 2012-08-21 09:56:58 +0800 | [diff] [blame] | 413 | status = acpi_decode_pld_buffer( |
| 414 | output->package.elements[0].buffer.pointer, |
| 415 | output->package.elements[0].buffer.length, |
| 416 | pld); |
| 417 | |
Matthew Garrett | 38ac0f1 | 2012-05-11 16:08:26 +0800 | [diff] [blame] | 418 | out: |
| 419 | kfree(buffer.pointer); |
| 420 | return status; |
| 421 | } |
| 422 | EXPORT_SYMBOL(acpi_get_physical_device_location); |
Toshi Kani | 275c58d | 2012-05-23 20:25:19 -0600 | [diff] [blame] | 423 | |
| 424 | /** |
| 425 | * acpi_evaluate_hotplug_ost: Evaluate _OST for hotplug operations |
| 426 | * @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 | */ |
| 435 | acpi_status |
| 436 | acpi_evaluate_hotplug_ost(acpi_handle handle, u32 source_event, |
| 437 | u32 status_code, struct acpi_buffer *status_buf) |
| 438 | { |
| 439 | #ifdef ACPI_HOTPLUG_OST |
| 440 | union acpi_object params[3] = { |
| 441 | {.type = ACPI_TYPE_INTEGER,}, |
| 442 | {.type = ACPI_TYPE_INTEGER,}, |
| 443 | {.type = ACPI_TYPE_BUFFER,} |
| 444 | }; |
| 445 | struct acpi_object_list arg_list = {3, params}; |
| 446 | acpi_status status; |
| 447 | |
| 448 | params[0].integer.value = source_event; |
| 449 | params[1].integer.value = status_code; |
| 450 | if (status_buf != NULL) { |
| 451 | params[2].buffer.pointer = status_buf->pointer; |
| 452 | params[2].buffer.length = status_buf->length; |
| 453 | } else { |
| 454 | params[2].buffer.pointer = NULL; |
| 455 | params[2].buffer.length = 0; |
| 456 | } |
| 457 | |
| 458 | status = acpi_evaluate_object(handle, "_OST", &arg_list, NULL); |
| 459 | return status; |
| 460 | #else |
| 461 | return AE_OK; |
| 462 | #endif |
| 463 | } |
| 464 | EXPORT_SYMBOL(acpi_evaluate_hotplug_ost); |
Toshi Kani | fbfddae | 2012-11-21 01:36:28 +0000 | [diff] [blame] | 465 | |
| 466 | /** |
| 467 | * acpi_handle_printk: Print message with ACPI prefix and object path |
| 468 | * |
| 469 | * This function is called through acpi_handle_<level> macros and prints |
| 470 | * a message with ACPI prefix and object path. This function acquires |
| 471 | * the global namespace mutex to obtain an object path. In interrupt |
| 472 | * context, it shows the object path as <n/a>. |
| 473 | */ |
| 474 | void |
| 475 | acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...) |
| 476 | { |
| 477 | struct va_format vaf; |
| 478 | va_list args; |
| 479 | struct acpi_buffer buffer = { |
| 480 | .length = ACPI_ALLOCATE_BUFFER, |
| 481 | .pointer = NULL |
| 482 | }; |
| 483 | const char *path; |
| 484 | |
| 485 | va_start(args, fmt); |
| 486 | vaf.fmt = fmt; |
| 487 | vaf.va = &args; |
| 488 | |
| 489 | if (in_interrupt() || |
| 490 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK) |
| 491 | path = "<n/a>"; |
| 492 | else |
| 493 | path = buffer.pointer; |
| 494 | |
| 495 | printk("%sACPI: %s: %pV", level, path, &vaf); |
| 496 | |
| 497 | va_end(args); |
| 498 | kfree(buffer.pointer); |
| 499 | } |
| 500 | EXPORT_SYMBOL(acpi_handle_printk); |
Jiang Liu | 952c63e | 2013-06-29 00:24:38 +0800 | [diff] [blame] | 501 | |
| 502 | /** |
| 503 | * acpi_has_method: Check whether @handle has a method named @name |
| 504 | * @handle: ACPI device handle |
| 505 | * @name: name of object or method |
| 506 | * |
| 507 | * Check whether @handle has a method named @name. |
| 508 | */ |
| 509 | bool acpi_has_method(acpi_handle handle, char *name) |
| 510 | { |
| 511 | acpi_handle tmp; |
| 512 | |
| 513 | return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp)); |
| 514 | } |
| 515 | EXPORT_SYMBOL(acpi_has_method); |
Jiang Liu | 0db9820 | 2013-06-29 00:24:39 +0800 | [diff] [blame] | 516 | |
| 517 | acpi_status acpi_execute_simple_method(acpi_handle handle, char *method, |
| 518 | u64 arg) |
| 519 | { |
| 520 | union acpi_object obj = { .type = ACPI_TYPE_INTEGER }; |
| 521 | struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, }; |
| 522 | |
| 523 | obj.integer.value = arg; |
| 524 | |
| 525 | return acpi_evaluate_object(handle, method, &arg_list, NULL); |
| 526 | } |
| 527 | EXPORT_SYMBOL(acpi_execute_simple_method); |
Jiang Liu | 7d2421f | 2013-06-29 00:24:40 +0800 | [diff] [blame] | 528 | |
| 529 | /** |
| 530 | * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations |
| 531 | * @handle: ACPI device handle |
| 532 | * |
| 533 | * Evaluate device's _EJ0 method for hotplug operations. |
| 534 | */ |
| 535 | acpi_status acpi_evaluate_ej0(acpi_handle handle) |
| 536 | { |
| 537 | acpi_status status; |
| 538 | |
| 539 | status = acpi_execute_simple_method(handle, "_EJ0", 1); |
| 540 | if (status == AE_NOT_FOUND) |
| 541 | acpi_handle_warn(handle, "No _EJ0 support for device\n"); |
| 542 | else if (ACPI_FAILURE(status)) |
| 543 | acpi_handle_warn(handle, "Eject failed (0x%x)\n", status); |
| 544 | |
| 545 | return status; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device |
| 550 | * @handle: ACPI device handle |
| 551 | * @lock: lock device if non-zero, otherwise unlock device |
| 552 | * |
| 553 | * Evaluate device's _LCK method if present to lock/unlock device |
| 554 | */ |
| 555 | acpi_status acpi_evaluate_lck(acpi_handle handle, int lock) |
| 556 | { |
| 557 | acpi_status status; |
| 558 | |
| 559 | status = acpi_execute_simple_method(handle, "_LCK", !!lock); |
| 560 | if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) { |
| 561 | if (lock) |
| 562 | acpi_handle_warn(handle, |
| 563 | "Locking device failed (0x%x)\n", status); |
| 564 | else |
| 565 | acpi_handle_warn(handle, |
| 566 | "Unlocking device failed (0x%x)\n", status); |
| 567 | } |
| 568 | |
| 569 | return status; |
| 570 | } |
Jiang Liu | a65ac52 | 2013-12-19 20:38:10 +0800 | [diff] [blame] | 571 | |
| 572 | /** |
| 573 | * acpi_evaluate_dsm - evaluate device's _DSM method |
| 574 | * @handle: ACPI device handle |
| 575 | * @uuid: UUID of requested functions, should be 16 bytes |
| 576 | * @rev: revision number of requested function |
| 577 | * @func: requested function number |
| 578 | * @argv4: the function specific parameter |
| 579 | * |
| 580 | * Evaluate device's _DSM method with specified UUID, revision id and |
| 581 | * function number. Caller needs to free the returned object. |
| 582 | * |
| 583 | * Though ACPI defines the fourth parameter for _DSM should be a package, |
| 584 | * some old BIOSes do expect a buffer or an integer etc. |
| 585 | */ |
| 586 | union acpi_object * |
| 587 | acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func, |
| 588 | union acpi_object *argv4) |
| 589 | { |
| 590 | acpi_status ret; |
| 591 | struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL}; |
| 592 | union acpi_object params[4]; |
| 593 | struct acpi_object_list input = { |
| 594 | .count = 4, |
| 595 | .pointer = params, |
| 596 | }; |
| 597 | |
| 598 | params[0].type = ACPI_TYPE_BUFFER; |
| 599 | params[0].buffer.length = 16; |
| 600 | params[0].buffer.pointer = (char *)uuid; |
| 601 | params[1].type = ACPI_TYPE_INTEGER; |
| 602 | params[1].integer.value = rev; |
| 603 | params[2].type = ACPI_TYPE_INTEGER; |
| 604 | params[2].integer.value = func; |
| 605 | if (argv4) { |
| 606 | params[3] = *argv4; |
| 607 | } else { |
| 608 | params[3].type = ACPI_TYPE_PACKAGE; |
| 609 | params[3].package.count = 0; |
| 610 | params[3].package.elements = NULL; |
| 611 | } |
| 612 | |
| 613 | ret = acpi_evaluate_object(handle, "_DSM", &input, &buf); |
| 614 | if (ACPI_SUCCESS(ret)) |
| 615 | return (union acpi_object *)buf.pointer; |
| 616 | |
| 617 | if (ret != AE_NOT_FOUND) |
| 618 | acpi_handle_warn(handle, |
| 619 | "failed to evaluate _DSM (0x%x)\n", ret); |
| 620 | |
| 621 | return NULL; |
| 622 | } |
| 623 | EXPORT_SYMBOL(acpi_evaluate_dsm); |
| 624 | |
| 625 | /** |
| 626 | * acpi_check_dsm - check if _DSM method supports requested functions. |
| 627 | * @handle: ACPI device handle |
| 628 | * @uuid: UUID of requested functions, should be 16 bytes at least |
| 629 | * @rev: revision number of requested functions |
| 630 | * @funcs: bitmap of requested functions |
| 631 | * @exclude: excluding special value, used to support i915 and nouveau |
| 632 | * |
| 633 | * Evaluate device's _DSM method to check whether it supports requested |
| 634 | * functions. Currently only support 64 functions at maximum, should be |
| 635 | * enough for now. |
| 636 | */ |
| 637 | bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs) |
| 638 | { |
| 639 | int i; |
| 640 | u64 mask = 0; |
| 641 | union acpi_object *obj; |
| 642 | |
| 643 | if (funcs == 0) |
| 644 | return false; |
| 645 | |
| 646 | obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL); |
| 647 | if (!obj) |
| 648 | return false; |
| 649 | |
| 650 | /* For compatibility, old BIOSes may return an integer */ |
| 651 | if (obj->type == ACPI_TYPE_INTEGER) |
| 652 | mask = obj->integer.value; |
| 653 | else if (obj->type == ACPI_TYPE_BUFFER) |
| 654 | for (i = 0; i < obj->buffer.length && i < 8; i++) |
| 655 | mask |= (((u8)obj->buffer.pointer[i]) << (i * 8)); |
| 656 | ACPI_FREE(obj); |
| 657 | |
| 658 | /* |
| 659 | * Bit 0 indicates whether there's support for any functions other than |
| 660 | * function 0 for the specified UUID and revision. |
| 661 | */ |
| 662 | if ((mask & 0x1) && (mask & funcs) == funcs) |
| 663 | return true; |
| 664 | |
| 665 | return false; |
| 666 | } |
| 667 | EXPORT_SYMBOL(acpi_check_dsm); |