linux-old/drivers/acpi/utils.c
<<
>>
Prefs
   1/*
   2 *  acpi_utils.c - ACPI Utility Functions ($Revision: 8 $)
   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>
  28#include <linux/init.h>
  29#include <linux/types.h>
  30#include <acpi/acpi_bus.h>
  31#include <acpi/acpi_drivers.h>
  32
  33
  34#define _COMPONENT              ACPI_BUS_COMPONENT
  35ACPI_MODULE_NAME                ("acpi_utils")
  36
  37
  38/* --------------------------------------------------------------------------
  39                            Object Evaluation Helpers
  40   -------------------------------------------------------------------------- */
  41
  42#ifdef ACPI_DEBUG_OUTPUT
  43#define acpi_util_eval_error(h,p,s) {\
  44        char prefix[80] = {'\0'};\
  45        struct acpi_buffer buffer = {sizeof(prefix), prefix};\
  46        acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);\
  47        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",\
  48                (char *) prefix, p, acpi_format_exception(s))); }
  49#else
  50#define acpi_util_eval_error(h,p,s)
  51#endif
  52
  53
  54acpi_status
  55acpi_extract_package (
  56        union acpi_object       *package,
  57        struct acpi_buffer      *format,
  58        struct acpi_buffer      *buffer)
  59{
  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;
  67
  68        ACPI_FUNCTION_TRACE("acpi_extract_package");
  69
  70        if (!package || (package->type != ACPI_TYPE_PACKAGE) || (package->package.count < 1)) {
  71                ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid 'package' argument\n"));
  72                return_ACPI_STATUS(AE_BAD_PARAMETER);
  73        }
  74
  75        if (!format || !format->pointer || (format->length < 1)) {
  76                ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid 'format' argument\n"));
  77                return_ACPI_STATUS(AE_BAD_PARAMETER);
  78        }
  79
  80        if (!buffer) {
  81                ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid 'buffer' argument\n"));
  82                return_ACPI_STATUS(AE_BAD_PARAMETER);
  83        }
  84
  85        format_count = (format->length/sizeof(char)) - 1;
  86        if (format_count > package->package.count) {
  87                ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Format specifies more objects [%d] than exist in package [%d].", format_count, package->package.count));
  88                return_ACPI_STATUS(AE_BAD_DATA);
  89        }
  90
  91        format_string = (char*)format->pointer;
  92
  93        /*
  94         * Calculate size_required.
  95         */
  96        for (i=0; i<format_count; i++) {
  97
  98                union acpi_object *element = &(package->package.elements[i]);
  99
 100                if (!element) {
 101                        return_ACPI_STATUS(AE_BAD_DATA);
 102                }
 103
 104                switch (element->type) {
 105
 106                case ACPI_TYPE_INTEGER:
 107                        switch (format_string[i]) {
 108                        case 'N':
 109                                size_required += sizeof(acpi_integer);
 110                                tail_offset += sizeof(acpi_integer);
 111                                break;
 112                        case 'S':
 113                                size_required += sizeof(char*) + sizeof(acpi_integer) + sizeof(char);
 114                                tail_offset += sizeof(char*);
 115                                break;
 116                        default:
 117                                ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid package element [%d]: got number, expecing [%c].\n", i, format_string[i]));
 118                                return_ACPI_STATUS(AE_BAD_DATA);
 119                                break;
 120                        }
 121                        break;
 122
 123                case ACPI_TYPE_STRING:
 124                case ACPI_TYPE_BUFFER:
 125                        switch (format_string[i]) {
 126                        case 'S':
 127                                size_required += sizeof(char*) + (element->string.length * sizeof(char)) + sizeof(char);
 128                                tail_offset += sizeof(char*);
 129                                break;
 130                        case 'B':
 131                                size_required += sizeof(u8*) + (element->buffer.length * sizeof(u8));
 132                                tail_offset += sizeof(u8*);
 133                                break;
 134                        default:
 135                                ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid package element [%d] got string/buffer, expecing [%c].\n", i, format_string[i]));
 136                                return_ACPI_STATUS(AE_BAD_DATA);
 137                                break;
 138                        }
 139                        break;
 140
 141                case ACPI_TYPE_PACKAGE:
 142                default:
 143                        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unsupported element at index=%d\n", i));
 144                        /* TBD: handle nested packages... */
 145                        return_ACPI_STATUS(AE_SUPPORT);
 146                        break;
 147                }
 148        }
 149
 150        /*
 151         * Validate output buffer.
 152         */
 153        if (buffer->length < size_required) {
 154                buffer->length = size_required;
 155                return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
 156        }
 157        else if (buffer->length != size_required || !buffer->pointer) {
 158                return_ACPI_STATUS(AE_BAD_PARAMETER);
 159        }
 160
 161        head = buffer->pointer;
 162        tail = buffer->pointer + tail_offset;
 163
 164        /*
 165         * Extract package data.
 166         */
 167        for (i=0; i<format_count; i++) {
 168
 169                u8 **pointer = NULL;
 170                union acpi_object *element = &(package->package.elements[i]);
 171
 172                if (!element) {
 173                        return_ACPI_STATUS(AE_BAD_DATA);
 174                }
 175
 176                switch (element->type) {
 177
 178                case ACPI_TYPE_INTEGER:
 179                        switch (format_string[i]) {
 180                        case 'N':
 181                                *((acpi_integer*)head) = element->integer.value;
 182                                head += sizeof(acpi_integer);
 183                                break;
 184                        case 'S':
 185                                pointer = (u8**)head;
 186                                *pointer = tail;
 187                                *((acpi_integer*)tail) = element->integer.value;
 188                                head += sizeof(acpi_integer*);
 189                                tail += sizeof(acpi_integer);
 190                                /* NULL terminate string */
 191                                *tail = (char)0;
 192                                tail += sizeof(char);
 193                                break;
 194                        default:
 195                                /* Should never get here */
 196                                break;
 197                        }
 198                        break;
 199
 200                case ACPI_TYPE_STRING:
 201                case ACPI_TYPE_BUFFER:
 202                        switch (format_string[i]) {
 203                        case 'S':
 204                                pointer = (u8**)head;
 205                                *pointer = tail;
 206                                memcpy(tail, element->string.pointer, element->string.length);
 207                                head += sizeof(char*);
 208                                tail += element->string.length * sizeof(char);
 209                                /* NULL terminate string */
 210                                *tail = (char)0;
 211                                tail += sizeof(char);
 212                                break;
 213                        case 'B':
 214                                pointer = (u8**)head;
 215                                *pointer = tail;
 216                                memcpy(tail, element->buffer.pointer, element->buffer.length);
 217                                head += sizeof(u8*);
 218                                tail += element->buffer.length * sizeof(u8);
 219                                break;
 220                        default:
 221                                /* Should never get here */
 222                                break;
 223                        }
 224                        break;
 225
 226                case ACPI_TYPE_PACKAGE:
 227                        /* TBD: handle nested packages... */
 228                default:
 229                        /* Should never get here */
 230                        break;
 231                }
 232        }
 233
 234        return_ACPI_STATUS(AE_OK);
 235}
 236
 237
 238acpi_status
 239acpi_evaluate_integer (
 240        acpi_handle             handle,
 241        acpi_string             pathname,
 242        struct acpi_object_list *arguments,
 243        unsigned long           *data)
 244{
 245        acpi_status             status = AE_OK;
 246        union acpi_object       element;
 247        struct acpi_buffer      buffer = {sizeof(union acpi_object), &element};
 248
 249        ACPI_FUNCTION_TRACE("acpi_evaluate_integer");
 250
 251        if (!data)
 252                return_ACPI_STATUS(AE_BAD_PARAMETER);
 253
 254        status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
 255        if (ACPI_FAILURE(status)) {
 256                acpi_util_eval_error(handle, pathname, status);
 257                return_ACPI_STATUS(status);
 258        }
 259
 260        if (element.type != ACPI_TYPE_INTEGER) {
 261                acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
 262                return_ACPI_STATUS(AE_BAD_DATA);
 263        }
 264
 265        *data = element.integer.value;
 266
 267        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%lu]\n", *data));
 268
 269        return_ACPI_STATUS(AE_OK);
 270}
 271
 272
 273#if 0
 274acpi_status
 275acpi_evaluate_string (
 276        acpi_handle             handle,
 277        acpi_string             pathname,
 278        acpi_object_list        *arguments,
 279        acpi_string             *data)
 280{
 281        acpi_status             status = AE_OK;
 282        acpi_object             *element = NULL;
 283        acpi_buffer             buffer = {ACPI_ALLOCATE_BUFFER, NULL};
 284
 285        ACPI_FUNCTION_TRACE("acpi_evaluate_string");
 286
 287        if (!data)
 288                return_ACPI_STATUS(AE_BAD_PARAMETER);
 289
 290        status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
 291        if (ACPI_FAILURE(status)) {
 292                acpi_util_eval_error(handle, pathname, status);
 293                return_ACPI_STATUS(status);
 294        }
 295
 296        element = (acpi_object *) buffer.pointer;
 297
 298        if ((element->type != ACPI_TYPE_STRING) 
 299                || (element->type != ACPI_TYPE_BUFFER)
 300                || !element->string.length) {
 301                acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
 302                return_ACPI_STATUS(AE_BAD_DATA);
 303        }
 304
 305        *data = kmalloc(element->string.length + 1, GFP_KERNEL);
 306        if (!data) {
 307                ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
 308                return_VALUE(-ENOMEM);
 309        }
 310        memset(*data, 0, element->string.length + 1);
 311
 312        memcpy(*data, element->string.pointer, element->string.length);
 313
 314        ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%s]\n", *data));
 315
 316        acpi_os_free(buffer.pointer);
 317
 318        return_ACPI_STATUS(AE_OK);
 319}
 320#endif
 321
 322
 323acpi_status
 324acpi_evaluate_reference (
 325        acpi_handle             handle,
 326        acpi_string             pathname,
 327        struct acpi_object_list *arguments,
 328        struct acpi_handle_list *list)
 329{
 330        acpi_status             status = AE_OK;
 331        union acpi_object       *package = NULL;
 332        union acpi_object       *element = NULL;
 333        struct acpi_buffer      buffer = {ACPI_ALLOCATE_BUFFER, NULL};
 334        u32                     i = 0;
 335
 336        ACPI_FUNCTION_TRACE("acpi_evaluate_reference");
 337
 338        if (!list) {
 339                return_ACPI_STATUS(AE_BAD_PARAMETER);
 340        }
 341
 342        /* Evaluate object. */
 343
 344        status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
 345        if (ACPI_FAILURE(status))
 346                goto end;
 347
 348        package = (union acpi_object *) buffer.pointer;
 349
 350        if ((buffer.length == 0) || !package) {
 351                ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
 352                        "No return object (len %X ptr %p)\n", 
 353                        buffer.length, package));
 354                status = AE_BAD_DATA;
 355                acpi_util_eval_error(handle, pathname, status);
 356                goto end;
 357        }
 358        if (package->type != ACPI_TYPE_PACKAGE) {
 359                ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
 360                        "Expecting a [Package], found type %X\n", 
 361                        package->type));
 362                status = AE_BAD_DATA;
 363                acpi_util_eval_error(handle, pathname, status);
 364                goto end;
 365        }
 366        if (!package->package.count) {
 367                ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
 368                        "[Package] has zero elements (%p)\n", 
 369                        package));
 370                status = AE_BAD_DATA;
 371                acpi_util_eval_error(handle, pathname, status);
 372                goto end;
 373        }
 374
 375        if (package->package.count > ACPI_MAX_HANDLES) {
 376                return AE_NO_MEMORY;
 377        }
 378        list->count = package->package.count;
 379
 380        /* Extract package data. */
 381
 382        for (i = 0; i < list->count; i++) {
 383
 384                element = &(package->package.elements[i]);
 385
 386                if (element->type != ACPI_TYPE_ANY) {
 387                        status = AE_BAD_DATA;
 388                        ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
 389                                "Expecting a [Reference] package element, found type %X\n",
 390                                element->type));
 391                        acpi_util_eval_error(handle, pathname, status);
 392                        break;
 393                }
 394
 395                /* Get the  acpi_handle. */
 396
 397                list->handles[i] = element->reference.handle;
 398                ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
 399                        list->handles[i]));
 400        }
 401
 402end:
 403        if (ACPI_FAILURE(status)) {
 404                list->count = 0;
 405                //kfree(list->handles);
 406        }
 407
 408        acpi_os_free(buffer.pointer);
 409
 410        return_ACPI_STATUS(status);
 411}
 412
 413
 414
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.