spi: sirf: Add missing spi_master_{resume,suspend} calls to PM callbacks
[cascardo/linux.git] / drivers / acpi / utils.c
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>
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
34 #include "internal.h"
35
36 #define _COMPONENT              ACPI_BUS_COMPONENT
37 ACPI_MODULE_NAME("utils");
38
39 /* --------------------------------------------------------------------------
40                             Object Evaluation Helpers
41    -------------------------------------------------------------------------- */
42 static void
43 acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
44 {
45 #ifdef ACPI_DEBUG_OUTPUT
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)));
51 #else
52         return;
53 #endif
54 }
55
56 acpi_status
57 acpi_extract_package(union acpi_object *package,
58                      struct acpi_buffer *format, 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
69         if (!package || (package->type != ACPI_TYPE_PACKAGE)
70             || (package->package.count < 1)) {
71                 printk(KERN_WARNING PREFIX "Invalid package argument\n");
72                 return AE_BAD_PARAMETER;
73         }
74
75         if (!format || !format->pointer || (format->length < 1)) {
76                 printk(KERN_WARNING PREFIX "Invalid format argument\n");
77                 return AE_BAD_PARAMETER;
78         }
79
80         if (!buffer) {
81                 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
82                 return AE_BAD_PARAMETER;
83         }
84
85         format_count = (format->length / sizeof(char)) - 1;
86         if (format_count > package->package.count) {
87                 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
88                               " than exist in package [%d].\n",
89                               format_count, package->package.count);
90                 return AE_BAD_DATA;
91         }
92
93         format_string = format->pointer;
94
95         /*
96          * Calculate size_required.
97          */
98         for (i = 0; i < format_count; i++) {
99
100                 union acpi_object *element = &(package->package.elements[i]);
101
102                 if (!element) {
103                         return AE_BAD_DATA;
104                 }
105
106                 switch (element->type) {
107
108                 case ACPI_TYPE_INTEGER:
109                         switch (format_string[i]) {
110                         case 'N':
111                                 size_required += sizeof(u64);
112                                 tail_offset += sizeof(u64);
113                                 break;
114                         case 'S':
115                                 size_required +=
116                                     sizeof(char *) + sizeof(u64) +
117                                     sizeof(char);
118                                 tail_offset += sizeof(char *);
119                                 break;
120                         default:
121                                 printk(KERN_WARNING PREFIX "Invalid package element"
122                                               " [%d]: got number, expecting"
123                                               " [%c]\n",
124                                               i, format_string[i]);
125                                 return AE_BAD_DATA;
126                                 break;
127                         }
128                         break;
129
130                 case ACPI_TYPE_STRING:
131                 case ACPI_TYPE_BUFFER:
132                         switch (format_string[i]) {
133                         case 'S':
134                                 size_required +=
135                                     sizeof(char *) +
136                                     (element->string.length * sizeof(char)) +
137                                     sizeof(char);
138                                 tail_offset += sizeof(char *);
139                                 break;
140                         case 'B':
141                                 size_required +=
142                                     sizeof(u8 *) +
143                                     (element->buffer.length * sizeof(u8));
144                                 tail_offset += sizeof(u8 *);
145                                 break;
146                         default:
147                                 printk(KERN_WARNING PREFIX "Invalid package element"
148                                               " [%d] got string/buffer,"
149                                               " expecting [%c]\n",
150                                               i, format_string[i]);
151                                 return AE_BAD_DATA;
152                                 break;
153                         }
154                         break;
155
156                 case ACPI_TYPE_PACKAGE:
157                 default:
158                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
159                                           "Found unsupported element at index=%d\n",
160                                           i));
161                         /* TBD: handle nested packages... */
162                         return AE_SUPPORT;
163                         break;
164                 }
165         }
166
167         /*
168          * Validate output buffer.
169          */
170         if (buffer->length == ACPI_ALLOCATE_BUFFER) {
171                 buffer->pointer = ACPI_ALLOCATE(size_required);
172                 if (!buffer->pointer)
173                         return AE_NO_MEMORY;
174                 buffer->length = size_required;
175                 memset(buffer->pointer, 0, size_required);
176         } else {
177                 if (buffer->length < size_required) {
178                         buffer->length = size_required;
179                         return AE_BUFFER_OVERFLOW;
180                 } else if (buffer->length != size_required ||
181                            !buffer->pointer) {
182                         return AE_BAD_PARAMETER;
183                 }
184         }
185
186         head = buffer->pointer;
187         tail = buffer->pointer + tail_offset;
188
189         /*
190          * Extract package data.
191          */
192         for (i = 0; i < format_count; i++) {
193
194                 u8 **pointer = NULL;
195                 union acpi_object *element = &(package->package.elements[i]);
196
197                 if (!element) {
198                         return AE_BAD_DATA;
199                 }
200
201                 switch (element->type) {
202
203                 case ACPI_TYPE_INTEGER:
204                         switch (format_string[i]) {
205                         case 'N':
206                                 *((u64 *) head) =
207                                     element->integer.value;
208                                 head += sizeof(u64);
209                                 break;
210                         case 'S':
211                                 pointer = (u8 **) head;
212                                 *pointer = tail;
213                                 *((u64 *) tail) =
214                                     element->integer.value;
215                                 head += sizeof(u64 *);
216                                 tail += sizeof(u64);
217                                 /* NULL terminate string */
218                                 *tail = (char)0;
219                                 tail += sizeof(char);
220                                 break;
221                         default:
222                                 /* Should never get here */
223                                 break;
224                         }
225                         break;
226
227                 case ACPI_TYPE_STRING:
228                 case ACPI_TYPE_BUFFER:
229                         switch (format_string[i]) {
230                         case 'S':
231                                 pointer = (u8 **) head;
232                                 *pointer = tail;
233                                 memcpy(tail, element->string.pointer,
234                                        element->string.length);
235                                 head += sizeof(char *);
236                                 tail += element->string.length * sizeof(char);
237                                 /* NULL terminate string */
238                                 *tail = (char)0;
239                                 tail += sizeof(char);
240                                 break;
241                         case 'B':
242                                 pointer = (u8 **) head;
243                                 *pointer = tail;
244                                 memcpy(tail, element->buffer.pointer,
245                                        element->buffer.length);
246                                 head += sizeof(u8 *);
247                                 tail += element->buffer.length * sizeof(u8);
248                                 break;
249                         default:
250                                 /* Should never get here */
251                                 break;
252                         }
253                         break;
254
255                 case ACPI_TYPE_PACKAGE:
256                         /* TBD: handle nested packages... */
257                 default:
258                         /* Should never get here */
259                         break;
260                 }
261         }
262
263         return AE_OK;
264 }
265
266 EXPORT_SYMBOL(acpi_extract_package);
267
268 acpi_status
269 acpi_evaluate_integer(acpi_handle handle,
270                       acpi_string pathname,
271                       struct acpi_object_list *arguments, unsigned long long *data)
272 {
273         acpi_status status = AE_OK;
274         union acpi_object element;
275         struct acpi_buffer buffer = { 0, NULL };
276
277         if (!data)
278                 return AE_BAD_PARAMETER;
279
280         buffer.length = sizeof(union acpi_object);
281         buffer.pointer = &element;
282         status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
283         if (ACPI_FAILURE(status)) {
284                 acpi_util_eval_error(handle, pathname, status);
285                 return status;
286         }
287
288         if (element.type != ACPI_TYPE_INTEGER) {
289                 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
290                 return AE_BAD_DATA;
291         }
292
293         *data = element.integer.value;
294
295         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
296
297         return AE_OK;
298 }
299
300 EXPORT_SYMBOL(acpi_evaluate_integer);
301
302 acpi_status
303 acpi_evaluate_reference(acpi_handle handle,
304                         acpi_string pathname,
305                         struct acpi_object_list *arguments,
306                         struct acpi_handle_list *list)
307 {
308         acpi_status status = AE_OK;
309         union acpi_object *package = NULL;
310         union acpi_object *element = NULL;
311         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
312         u32 i = 0;
313
314
315         if (!list) {
316                 return AE_BAD_PARAMETER;
317         }
318
319         /* Evaluate object. */
320
321         status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
322         if (ACPI_FAILURE(status))
323                 goto end;
324
325         package = buffer.pointer;
326
327         if ((buffer.length == 0) || !package) {
328                 printk(KERN_ERR PREFIX "No return object (len %X ptr %p)\n",
329                             (unsigned)buffer.length, package);
330                 status = AE_BAD_DATA;
331                 acpi_util_eval_error(handle, pathname, status);
332                 goto end;
333         }
334         if (package->type != ACPI_TYPE_PACKAGE) {
335                 printk(KERN_ERR PREFIX "Expecting a [Package], found type %X\n",
336                             package->type);
337                 status = AE_BAD_DATA;
338                 acpi_util_eval_error(handle, pathname, status);
339                 goto end;
340         }
341         if (!package->package.count) {
342                 printk(KERN_ERR PREFIX "[Package] has zero elements (%p)\n",
343                             package);
344                 status = AE_BAD_DATA;
345                 acpi_util_eval_error(handle, pathname, status);
346                 goto end;
347         }
348
349         if (package->package.count > ACPI_MAX_HANDLES) {
350                 return AE_NO_MEMORY;
351         }
352         list->count = package->package.count;
353
354         /* Extract package data. */
355
356         for (i = 0; i < list->count; i++) {
357
358                 element = &(package->package.elements[i]);
359
360                 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
361                         status = AE_BAD_DATA;
362                         printk(KERN_ERR PREFIX
363                                     "Expecting a [Reference] package element, found type %X\n",
364                                     element->type);
365                         acpi_util_eval_error(handle, pathname, status);
366                         break;
367                 }
368
369                 if (!element->reference.handle) {
370                         printk(KERN_WARNING PREFIX "Invalid reference in"
371                                " package %s\n", pathname);
372                         status = AE_NULL_ENTRY;
373                         break;
374                 }
375                 /* Get the  acpi_handle. */
376
377                 list->handles[i] = element->reference.handle;
378                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
379                                   list->handles[i]));
380         }
381
382       end:
383         if (ACPI_FAILURE(status)) {
384                 list->count = 0;
385                 //kfree(list->handles);
386         }
387
388         kfree(buffer.pointer);
389
390         return status;
391 }
392
393 EXPORT_SYMBOL(acpi_evaluate_reference);
394
395 acpi_status
396 acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
397 {
398         acpi_status status;
399         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
400         union acpi_object *output;
401
402         status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
403
404         if (ACPI_FAILURE(status))
405                 return status;
406
407         output = buffer.pointer;
408
409         if (!output || output->type != ACPI_TYPE_PACKAGE
410             || !output->package.count
411             || output->package.elements[0].type != ACPI_TYPE_BUFFER
412             || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
413                 status = AE_TYPE;
414                 goto out;
415         }
416
417         status = acpi_decode_pld_buffer(
418                         output->package.elements[0].buffer.pointer,
419                         output->package.elements[0].buffer.length,
420                         pld);
421
422 out:
423         kfree(buffer.pointer);
424         return status;
425 }
426 EXPORT_SYMBOL(acpi_get_physical_device_location);
427
428 /**
429  * acpi_evaluate_hotplug_ost: Evaluate _OST for hotplug operations
430  * @handle: ACPI device handle
431  * @source_event: source event code
432  * @status_code: status code
433  * @status_buf: optional detailed information (NULL if none)
434  *
435  * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
436  * must call this function when evaluating _OST for hotplug operations.
437  * When the platform does not support _OST, this function has no effect.
438  */
439 acpi_status
440 acpi_evaluate_hotplug_ost(acpi_handle handle, u32 source_event,
441                 u32 status_code, struct acpi_buffer *status_buf)
442 {
443 #ifdef ACPI_HOTPLUG_OST
444         union acpi_object params[3] = {
445                 {.type = ACPI_TYPE_INTEGER,},
446                 {.type = ACPI_TYPE_INTEGER,},
447                 {.type = ACPI_TYPE_BUFFER,}
448         };
449         struct acpi_object_list arg_list = {3, params};
450         acpi_status status;
451
452         params[0].integer.value = source_event;
453         params[1].integer.value = status_code;
454         if (status_buf != NULL) {
455                 params[2].buffer.pointer = status_buf->pointer;
456                 params[2].buffer.length = status_buf->length;
457         } else {
458                 params[2].buffer.pointer = NULL;
459                 params[2].buffer.length = 0;
460         }
461
462         status = acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
463         return status;
464 #else
465         return AE_OK;
466 #endif
467 }
468 EXPORT_SYMBOL(acpi_evaluate_hotplug_ost);
469
470 /**
471  * acpi_handle_printk: Print message with ACPI prefix and object path
472  *
473  * This function is called through acpi_handle_<level> macros and prints
474  * a message with ACPI prefix and object path.  This function acquires
475  * the global namespace mutex to obtain an object path.  In interrupt
476  * context, it shows the object path as <n/a>.
477  */
478 void
479 acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
480 {
481         struct va_format vaf;
482         va_list args;
483         struct acpi_buffer buffer = {
484                 .length = ACPI_ALLOCATE_BUFFER,
485                 .pointer = NULL
486         };
487         const char *path;
488
489         va_start(args, fmt);
490         vaf.fmt = fmt;
491         vaf.va = &args;
492
493         if (in_interrupt() ||
494             acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
495                 path = "<n/a>";
496         else
497                 path = buffer.pointer;
498
499         printk("%sACPI: %s: %pV", level, path, &vaf);
500
501         va_end(args);
502         kfree(buffer.pointer);
503 }
504 EXPORT_SYMBOL(acpi_handle_printk);
505
506 /**
507  * acpi_has_method: Check whether @handle has a method named @name
508  * @handle: ACPI device handle
509  * @name: name of object or method
510  *
511  * Check whether @handle has a method named @name.
512  */
513 bool acpi_has_method(acpi_handle handle, char *name)
514 {
515         acpi_handle tmp;
516
517         return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
518 }
519 EXPORT_SYMBOL(acpi_has_method);
520
521 acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
522                                        u64 arg)
523 {
524         union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
525         struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
526
527         obj.integer.value = arg;
528
529         return acpi_evaluate_object(handle, method, &arg_list, NULL);
530 }
531 EXPORT_SYMBOL(acpi_execute_simple_method);
532
533 /**
534  * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
535  * @handle: ACPI device handle
536  *
537  * Evaluate device's _EJ0 method for hotplug operations.
538  */
539 acpi_status acpi_evaluate_ej0(acpi_handle handle)
540 {
541         acpi_status status;
542
543         status = acpi_execute_simple_method(handle, "_EJ0", 1);
544         if (status == AE_NOT_FOUND)
545                 acpi_handle_warn(handle, "No _EJ0 support for device\n");
546         else if (ACPI_FAILURE(status))
547                 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
548
549         return status;
550 }
551
552 /**
553  * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
554  * @handle: ACPI device handle
555  * @lock: lock device if non-zero, otherwise unlock device
556  *
557  * Evaluate device's _LCK method if present to lock/unlock device
558  */
559 acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
560 {
561         acpi_status status;
562
563         status = acpi_execute_simple_method(handle, "_LCK", !!lock);
564         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
565                 if (lock)
566                         acpi_handle_warn(handle,
567                                 "Locking device failed (0x%x)\n", status);
568                 else
569                         acpi_handle_warn(handle,
570                                 "Unlocking device failed (0x%x)\n", status);
571         }
572
573         return status;
574 }
575
576 /**
577  * acpi_evaluate_dsm - evaluate device's _DSM method
578  * @handle: ACPI device handle
579  * @uuid: UUID of requested functions, should be 16 bytes
580  * @rev: revision number of requested function
581  * @func: requested function number
582  * @argv4: the function specific parameter
583  *
584  * Evaluate device's _DSM method with specified UUID, revision id and
585  * function number. Caller needs to free the returned object.
586  *
587  * Though ACPI defines the fourth parameter for _DSM should be a package,
588  * some old BIOSes do expect a buffer or an integer etc.
589  */
590 union acpi_object *
591 acpi_evaluate_dsm(acpi_handle handle, const u8 *uuid, int rev, int func,
592                   union acpi_object *argv4)
593 {
594         acpi_status ret;
595         struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
596         union acpi_object params[4];
597         struct acpi_object_list input = {
598                 .count = 4,
599                 .pointer = params,
600         };
601
602         params[0].type = ACPI_TYPE_BUFFER;
603         params[0].buffer.length = 16;
604         params[0].buffer.pointer = (char *)uuid;
605         params[1].type = ACPI_TYPE_INTEGER;
606         params[1].integer.value = rev;
607         params[2].type = ACPI_TYPE_INTEGER;
608         params[2].integer.value = func;
609         if (argv4) {
610                 params[3] = *argv4;
611         } else {
612                 params[3].type = ACPI_TYPE_PACKAGE;
613                 params[3].package.count = 0;
614                 params[3].package.elements = NULL;
615         }
616
617         ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
618         if (ACPI_SUCCESS(ret))
619                 return (union acpi_object *)buf.pointer;
620
621         if (ret != AE_NOT_FOUND)
622                 acpi_handle_warn(handle,
623                                 "failed to evaluate _DSM (0x%x)\n", ret);
624
625         return NULL;
626 }
627 EXPORT_SYMBOL(acpi_evaluate_dsm);
628
629 /**
630  * acpi_check_dsm - check if _DSM method supports requested functions.
631  * @handle: ACPI device handle
632  * @uuid: UUID of requested functions, should be 16 bytes at least
633  * @rev: revision number of requested functions
634  * @funcs: bitmap of requested functions
635  * @exclude: excluding special value, used to support i915 and nouveau
636  *
637  * Evaluate device's _DSM method to check whether it supports requested
638  * functions. Currently only support 64 functions at maximum, should be
639  * enough for now.
640  */
641 bool acpi_check_dsm(acpi_handle handle, const u8 *uuid, int rev, u64 funcs)
642 {
643         int i;
644         u64 mask = 0;
645         union acpi_object *obj;
646
647         if (funcs == 0)
648                 return false;
649
650         obj = acpi_evaluate_dsm(handle, uuid, rev, 0, NULL);
651         if (!obj)
652                 return false;
653
654         /* For compatibility, old BIOSes may return an integer */
655         if (obj->type == ACPI_TYPE_INTEGER)
656                 mask = obj->integer.value;
657         else if (obj->type == ACPI_TYPE_BUFFER)
658                 for (i = 0; i < obj->buffer.length && i < 8; i++)
659                         mask |= (((u8)obj->buffer.pointer[i]) << (i * 8));
660         ACPI_FREE(obj);
661
662         /*
663          * Bit 0 indicates whether there's support for any functions other than
664          * function 0 for the specified UUID and revision.
665          */
666         if ((mask & 0x1) && (mask & funcs) == funcs)
667                 return true;
668
669         return false;
670 }
671 EXPORT_SYMBOL(acpi_check_dsm);