]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/acpi/utils.c
net: dsa: lan9303: fix broken backpressure in .port_fdb_dump
[mirror_ubuntu-hirsute-kernel.git] / drivers / acpi / utils.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * acpi_utils.c - ACPI Utility Functions ($Revision: 10 $)
4 *
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
1da177e4
LT
7 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
5a0e3ad6 11#include <linux/slab.h>
1da177e4
LT
12#include <linux/init.h>
13#include <linux/types.h>
fbfddae6
TK
14#include <linux/hardirq.h>
15#include <linux/acpi.h>
45fef5b8 16#include <linux/dynamic_debug.h>
1da177e4 17
a192a958 18#include "internal.h"
2d12b6b3 19#include "sleep.h"
a192a958 20
1da177e4 21#define _COMPONENT ACPI_BUS_COMPONENT
f52fd66d 22ACPI_MODULE_NAME("utils");
1da177e4
LT
23
24/* --------------------------------------------------------------------------
25 Object Evaluation Helpers
26 -------------------------------------------------------------------------- */
4fd7f518
HH
27static void
28acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
29{
1da177e4 30#ifdef ACPI_DEBUG_OUTPUT
4fd7f518
HH
31 char prefix[80] = {'\0'};
32 struct acpi_buffer buffer = {sizeof(prefix), prefix};
33 acpi_get_name(h, ACPI_FULL_PATHNAME, &buffer);
34 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Evaluate [%s.%s]: %s\n",
35 (char *) prefix, p, acpi_format_exception(s)));
1da177e4 36#else
4fd7f518 37 return;
1da177e4 38#endif
4fd7f518
HH
39}
40
1da177e4 41acpi_status
4be44fcd
LB
42acpi_extract_package(union acpi_object *package,
43 struct acpi_buffer *format, struct acpi_buffer *buffer)
1da177e4 44{
4be44fcd
LB
45 u32 size_required = 0;
46 u32 tail_offset = 0;
47 char *format_string = NULL;
48 u32 format_count = 0;
49 u32 i = 0;
50 u8 *head = NULL;
51 u8 *tail = NULL;
1da177e4 52
1da177e4 53
4be44fcd
LB
54 if (!package || (package->type != ACPI_TYPE_PACKAGE)
55 || (package->package.count < 1)) {
cece9296 56 printk(KERN_WARNING PREFIX "Invalid package argument\n");
d550d98d 57 return AE_BAD_PARAMETER;
1da177e4
LT
58 }
59
60 if (!format || !format->pointer || (format->length < 1)) {
cece9296 61 printk(KERN_WARNING PREFIX "Invalid format argument\n");
d550d98d 62 return AE_BAD_PARAMETER;
1da177e4
LT
63 }
64
65 if (!buffer) {
cece9296 66 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
d550d98d 67 return AE_BAD_PARAMETER;
1da177e4
LT
68 }
69
4be44fcd 70 format_count = (format->length / sizeof(char)) - 1;
1da177e4 71 if (format_count > package->package.count) {
cece9296
LB
72 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
73 " than exist in package [%d].\n",
74 format_count, package->package.count);
d550d98d 75 return AE_BAD_DATA;
1da177e4
LT
76 }
77
50dd0969 78 format_string = format->pointer;
1da177e4
LT
79
80 /*
81 * Calculate size_required.
82 */
4be44fcd 83 for (i = 0; i < format_count; i++) {
1da177e4
LT
84
85 union acpi_object *element = &(package->package.elements[i]);
86
1da177e4
LT
87 switch (element->type) {
88
89 case ACPI_TYPE_INTEGER:
90 switch (format_string[i]) {
91 case 'N':
439913ff
LM
92 size_required += sizeof(u64);
93 tail_offset += sizeof(u64);
1da177e4
LT
94 break;
95 case 'S':
4be44fcd 96 size_required +=
439913ff 97 sizeof(char *) + sizeof(u64) +
4be44fcd
LB
98 sizeof(char);
99 tail_offset += sizeof(char *);
1da177e4
LT
100 break;
101 default:
cece9296 102 printk(KERN_WARNING PREFIX "Invalid package element"
e7e92ec9 103 " [%d]: got number, expecting"
cece9296
LB
104 " [%c]\n",
105 i, format_string[i]);
d550d98d 106 return AE_BAD_DATA;
1da177e4
LT
107 }
108 break;
109
110 case ACPI_TYPE_STRING:
111 case ACPI_TYPE_BUFFER:
112 switch (format_string[i]) {
113 case 'S':
4be44fcd
LB
114 size_required +=
115 sizeof(char *) +
116 (element->string.length * sizeof(char)) +
117 sizeof(char);
118 tail_offset += sizeof(char *);
1da177e4
LT
119 break;
120 case 'B':
4be44fcd 121 size_required +=
d93de345 122 sizeof(u8 *) + element->buffer.length;
4be44fcd 123 tail_offset += sizeof(u8 *);
1da177e4
LT
124 break;
125 default:
cece9296 126 printk(KERN_WARNING PREFIX "Invalid package element"
a6fc6720 127 " [%d] got string/buffer,"
e7e92ec9 128 " expecting [%c]\n",
cece9296 129 i, format_string[i]);
d550d98d 130 return AE_BAD_DATA;
1da177e4
LT
131 }
132 break;
e3ec483a
ZR
133 case ACPI_TYPE_LOCAL_REFERENCE:
134 switch (format_string[i]) {
135 case 'R':
136 size_required += sizeof(void *);
137 tail_offset += sizeof(void *);
138 break;
139 default:
140 printk(KERN_WARNING PREFIX "Invalid package element"
141 " [%d] got reference,"
142 " expecting [%c]\n",
143 i, format_string[i]);
144 return AE_BAD_DATA;
e3ec483a
ZR
145 }
146 break;
1da177e4
LT
147
148 case ACPI_TYPE_PACKAGE:
149 default:
4be44fcd
LB
150 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
151 "Found unsupported element at index=%d\n",
152 i));
1da177e4 153 /* TBD: handle nested packages... */
d550d98d 154 return AE_SUPPORT;
1da177e4
LT
155 }
156 }
157
158 /*
159 * Validate output buffer.
160 */
e83dda06 161 if (buffer->length == ACPI_ALLOCATE_BUFFER) {
2d0acb4a 162 buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required);
e83dda06
AS
163 if (!buffer->pointer)
164 return AE_NO_MEMORY;
1da177e4 165 buffer->length = size_required;
e83dda06
AS
166 } else {
167 if (buffer->length < size_required) {
168 buffer->length = size_required;
169 return AE_BUFFER_OVERFLOW;
170 } else if (buffer->length != size_required ||
171 !buffer->pointer) {
172 return AE_BAD_PARAMETER;
173 }
1da177e4
LT
174 }
175
176 head = buffer->pointer;
177 tail = buffer->pointer + tail_offset;
178
179 /*
180 * Extract package data.
181 */
4be44fcd 182 for (i = 0; i < format_count; i++) {
1da177e4
LT
183
184 u8 **pointer = NULL;
185 union acpi_object *element = &(package->package.elements[i]);
186
1da177e4
LT
187 switch (element->type) {
188
189 case ACPI_TYPE_INTEGER:
190 switch (format_string[i]) {
191 case 'N':
439913ff 192 *((u64 *) head) =
4be44fcd 193 element->integer.value;
439913ff 194 head += sizeof(u64);
1da177e4
LT
195 break;
196 case 'S':
4be44fcd 197 pointer = (u8 **) head;
1da177e4 198 *pointer = tail;
439913ff 199 *((u64 *) tail) =
4be44fcd 200 element->integer.value;
439913ff
LM
201 head += sizeof(u64 *);
202 tail += sizeof(u64);
1da177e4
LT
203 /* NULL terminate string */
204 *tail = (char)0;
205 tail += sizeof(char);
206 break;
207 default:
208 /* Should never get here */
209 break;
210 }
211 break;
212
213 case ACPI_TYPE_STRING:
214 case ACPI_TYPE_BUFFER:
215 switch (format_string[i]) {
216 case 'S':
4be44fcd 217 pointer = (u8 **) head;
1da177e4 218 *pointer = tail;
4be44fcd
LB
219 memcpy(tail, element->string.pointer,
220 element->string.length);
221 head += sizeof(char *);
1da177e4
LT
222 tail += element->string.length * sizeof(char);
223 /* NULL terminate string */
224 *tail = (char)0;
225 tail += sizeof(char);
226 break;
227 case 'B':
4be44fcd 228 pointer = (u8 **) head;
1da177e4 229 *pointer = tail;
4be44fcd
LB
230 memcpy(tail, element->buffer.pointer,
231 element->buffer.length);
232 head += sizeof(u8 *);
d93de345 233 tail += element->buffer.length;
1da177e4
LT
234 break;
235 default:
236 /* Should never get here */
237 break;
238 }
239 break;
e3ec483a
ZR
240 case ACPI_TYPE_LOCAL_REFERENCE:
241 switch (format_string[i]) {
242 case 'R':
243 *(void **)head =
244 (void *)element->reference.handle;
245 head += sizeof(void *);
246 break;
247 default:
248 /* Should never get here */
249 break;
250 }
251 break;
1da177e4
LT
252 case ACPI_TYPE_PACKAGE:
253 /* TBD: handle nested packages... */
254 default:
255 /* Should never get here */
256 break;
257 }
258 }
259
d550d98d 260 return AE_OK;
1da177e4 261}
1da177e4 262
4be44fcd 263EXPORT_SYMBOL(acpi_extract_package);
1da177e4
LT
264
265acpi_status
4be44fcd
LB
266acpi_evaluate_integer(acpi_handle handle,
267 acpi_string pathname,
27663c58 268 struct acpi_object_list *arguments, unsigned long long *data)
1da177e4 269{
4be44fcd 270 acpi_status status = AE_OK;
40599072 271 union acpi_object element;
4be44fcd 272 struct acpi_buffer buffer = { 0, NULL };
1da177e4 273
1da177e4 274 if (!data)
d550d98d 275 return AE_BAD_PARAMETER;
1da177e4 276
1da177e4 277 buffer.length = sizeof(union acpi_object);
40599072 278 buffer.pointer = &element;
1da177e4
LT
279 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
280 if (ACPI_FAILURE(status)) {
281 acpi_util_eval_error(handle, pathname, status);
d550d98d 282 return status;
1da177e4
LT
283 }
284
40599072 285 if (element.type != ACPI_TYPE_INTEGER) {
1da177e4 286 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
d550d98d 287 return AE_BAD_DATA;
1da177e4
LT
288 }
289
40599072 290 *data = element.integer.value;
1da177e4 291
27663c58 292 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
1da177e4 293
d550d98d 294 return AE_OK;
1da177e4 295}
1da177e4 296
4be44fcd 297EXPORT_SYMBOL(acpi_evaluate_integer);
1da177e4 298
1da177e4 299acpi_status
4be44fcd
LB
300acpi_evaluate_reference(acpi_handle handle,
301 acpi_string pathname,
302 struct acpi_object_list *arguments,
303 struct acpi_handle_list *list)
1da177e4 304{
4be44fcd
LB
305 acpi_status status = AE_OK;
306 union acpi_object *package = NULL;
307 union acpi_object *element = NULL;
308 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
309 u32 i = 0;
1da177e4 310
1da177e4
LT
311
312 if (!list) {
d550d98d 313 return AE_BAD_PARAMETER;
1da177e4
LT
314 }
315
316 /* Evaluate object. */
317
318 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
319 if (ACPI_FAILURE(status))
320 goto end;
321
50dd0969 322 package = buffer.pointer;
1da177e4
LT
323
324 if ((buffer.length == 0) || !package) {
1da177e4
LT
325 status = AE_BAD_DATA;
326 acpi_util_eval_error(handle, pathname, status);
327 goto end;
328 }
329 if (package->type != ACPI_TYPE_PACKAGE) {
1da177e4
LT
330 status = AE_BAD_DATA;
331 acpi_util_eval_error(handle, pathname, status);
332 goto end;
333 }
334 if (!package->package.count) {
1da177e4
LT
335 status = AE_BAD_DATA;
336 acpi_util_eval_error(handle, pathname, status);
337 goto end;
338 }
339
340 if (package->package.count > ACPI_MAX_HANDLES) {
09e15086 341 kfree(package);
d550d98d 342 return AE_NO_MEMORY;
1da177e4
LT
343 }
344 list->count = package->package.count;
345
346 /* Extract package data. */
347
348 for (i = 0; i < list->count; i++) {
349
350 element = &(package->package.elements[i]);
351
cd0b2248 352 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
1da177e4 353 status = AE_BAD_DATA;
1da177e4
LT
354 acpi_util_eval_error(handle, pathname, status);
355 break;
356 }
357
b6a16387 358 if (!element->reference.handle) {
b6a16387 359 status = AE_NULL_ENTRY;
c48cf1b9 360 acpi_util_eval_error(handle, pathname, status);
b6a16387
TR
361 break;
362 }
1da177e4
LT
363 /* Get the acpi_handle. */
364
365 list->handles[i] = element->reference.handle;
366 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
4be44fcd 367 list->handles[i]));
1da177e4
LT
368 }
369
4be44fcd 370 end:
1da177e4
LT
371 if (ACPI_FAILURE(status)) {
372 list->count = 0;
373 //kfree(list->handles);
374 }
375
02438d87 376 kfree(buffer.pointer);
1da177e4 377
d550d98d 378 return status;
1da177e4 379}
1da177e4 380
4be44fcd 381EXPORT_SYMBOL(acpi_evaluate_reference);
38ac0f1b
MG
382
383acpi_status
8ede06ab 384acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
38ac0f1b
MG
385{
386 acpi_status status;
387 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
388 union acpi_object *output;
389
390 status = acpi_evaluate_object(handle, "_PLD", NULL, &buffer);
391
392 if (ACPI_FAILURE(status))
393 return status;
394
395 output = buffer.pointer;
396
397 if (!output || output->type != ACPI_TYPE_PACKAGE
398 || !output->package.count
399 || output->package.elements[0].type != ACPI_TYPE_BUFFER
8ede06ab 400 || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
38ac0f1b
MG
401 status = AE_TYPE;
402 goto out;
403 }
404
8ede06ab
FT
405 status = acpi_decode_pld_buffer(
406 output->package.elements[0].buffer.pointer,
407 output->package.elements[0].buffer.length,
408 pld);
409
38ac0f1b
MG
410out:
411 kfree(buffer.pointer);
412 return status;
413}
414EXPORT_SYMBOL(acpi_get_physical_device_location);
275c58d7
TK
415
416/**
700b8422 417 * acpi_evaluate_ost: Evaluate _OST for hotplug operations
275c58d7
TK
418 * @handle: ACPI device handle
419 * @source_event: source event code
420 * @status_code: status code
421 * @status_buf: optional detailed information (NULL if none)
422 *
423 * Evaluate _OST for hotplug operations. All ACPI hotplug handlers
424 * must call this function when evaluating _OST for hotplug operations.
425 * When the platform does not support _OST, this function has no effect.
426 */
427acpi_status
05730c19
JL
428acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
429 struct acpi_buffer *status_buf)
275c58d7 430{
275c58d7
TK
431 union acpi_object params[3] = {
432 {.type = ACPI_TYPE_INTEGER,},
433 {.type = ACPI_TYPE_INTEGER,},
434 {.type = ACPI_TYPE_BUFFER,}
435 };
436 struct acpi_object_list arg_list = {3, params};
275c58d7
TK
437
438 params[0].integer.value = source_event;
439 params[1].integer.value = status_code;
440 if (status_buf != NULL) {
441 params[2].buffer.pointer = status_buf->pointer;
442 params[2].buffer.length = status_buf->length;
443 } else {
444 params[2].buffer.pointer = NULL;
445 params[2].buffer.length = 0;
446 }
447
05730c19 448 return acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
275c58d7 449}
05730c19 450EXPORT_SYMBOL(acpi_evaluate_ost);
fbfddae6 451
45fef5b8
BM
452/**
453 * acpi_handle_path: Return the object path of handle
8373f8c6 454 * @handle: ACPI device handle
45fef5b8
BM
455 *
456 * Caller must free the returned buffer
457 */
458static char *acpi_handle_path(acpi_handle handle)
459{
460 struct acpi_buffer buffer = {
461 .length = ACPI_ALLOCATE_BUFFER,
462 .pointer = NULL
463 };
464
465 if (in_interrupt() ||
466 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer) != AE_OK)
467 return NULL;
468 return buffer.pointer;
469}
470
fbfddae6
TK
471/**
472 * acpi_handle_printk: Print message with ACPI prefix and object path
8373f8c6
AS
473 * @level: log level
474 * @handle: ACPI device handle
475 * @fmt: format string
fbfddae6
TK
476 *
477 * This function is called through acpi_handle_<level> macros and prints
478 * a message with ACPI prefix and object path. This function acquires
479 * the global namespace mutex to obtain an object path. In interrupt
480 * context, it shows the object path as <n/a>.
481 */
482void
483acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
484{
485 struct va_format vaf;
486 va_list args;
fbfddae6
TK
487 const char *path;
488
489 va_start(args, fmt);
490 vaf.fmt = fmt;
491 vaf.va = &args;
492
45fef5b8
BM
493 path = acpi_handle_path(handle);
494 printk("%sACPI: %s: %pV", level, path ? path : "<n/a>" , &vaf);
fbfddae6
TK
495
496 va_end(args);
45fef5b8 497 kfree(path);
fbfddae6
TK
498}
499EXPORT_SYMBOL(acpi_handle_printk);
952c63e9 500
45fef5b8
BM
501#if defined(CONFIG_DYNAMIC_DEBUG)
502/**
503 * __acpi_handle_debug: pr_debug with ACPI prefix and object path
8373f8c6
AS
504 * @descriptor: Dynamic Debug descriptor
505 * @handle: ACPI device handle
506 * @fmt: format string
45fef5b8
BM
507 *
508 * This function is called through acpi_handle_debug macro and debug
509 * prints a message with ACPI prefix and object path. This function
510 * acquires the global namespace mutex to obtain an object path. In
511 * interrupt context, it shows the object path as <n/a>.
512 */
513void
514__acpi_handle_debug(struct _ddebug *descriptor, acpi_handle handle,
515 const char *fmt, ...)
516{
517 struct va_format vaf;
518 va_list args;
519 const char *path;
520
521 va_start(args, fmt);
522 vaf.fmt = fmt;
523 vaf.va = &args;
524
525 path = acpi_handle_path(handle);
526 __dynamic_pr_debug(descriptor, "ACPI: %s: %pV", path ? path : "<n/a>", &vaf);
527
528 va_end(args);
529 kfree(path);
530}
531EXPORT_SYMBOL(__acpi_handle_debug);
532#endif
533
952c63e9
JL
534/**
535 * acpi_has_method: Check whether @handle has a method named @name
536 * @handle: ACPI device handle
537 * @name: name of object or method
538 *
539 * Check whether @handle has a method named @name.
540 */
541bool acpi_has_method(acpi_handle handle, char *name)
542{
543 acpi_handle tmp;
544
545 return ACPI_SUCCESS(acpi_get_handle(handle, name, &tmp));
546}
547EXPORT_SYMBOL(acpi_has_method);
0db98202
JL
548
549acpi_status acpi_execute_simple_method(acpi_handle handle, char *method,
550 u64 arg)
551{
552 union acpi_object obj = { .type = ACPI_TYPE_INTEGER };
553 struct acpi_object_list arg_list = { .count = 1, .pointer = &obj, };
554
555 obj.integer.value = arg;
556
557 return acpi_evaluate_object(handle, method, &arg_list, NULL);
558}
559EXPORT_SYMBOL(acpi_execute_simple_method);
7d2421f8
JL
560
561/**
562 * acpi_evaluate_ej0: Evaluate _EJ0 method for hotplug operations
563 * @handle: ACPI device handle
564 *
565 * Evaluate device's _EJ0 method for hotplug operations.
566 */
567acpi_status acpi_evaluate_ej0(acpi_handle handle)
568{
569 acpi_status status;
570
571 status = acpi_execute_simple_method(handle, "_EJ0", 1);
572 if (status == AE_NOT_FOUND)
573 acpi_handle_warn(handle, "No _EJ0 support for device\n");
574 else if (ACPI_FAILURE(status))
575 acpi_handle_warn(handle, "Eject failed (0x%x)\n", status);
576
577 return status;
578}
579
580/**
581 * acpi_evaluate_lck: Evaluate _LCK method to lock/unlock device
582 * @handle: ACPI device handle
583 * @lock: lock device if non-zero, otherwise unlock device
584 *
585 * Evaluate device's _LCK method if present to lock/unlock device
586 */
587acpi_status acpi_evaluate_lck(acpi_handle handle, int lock)
588{
589 acpi_status status;
590
591 status = acpi_execute_simple_method(handle, "_LCK", !!lock);
592 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
593 if (lock)
594 acpi_handle_warn(handle,
595 "Locking device failed (0x%x)\n", status);
596 else
597 acpi_handle_warn(handle,
598 "Unlocking device failed (0x%x)\n", status);
599 }
600
601 return status;
602}
a65ac520 603
132565d8
HG
604/**
605 * acpi_evaluate_reg: Evaluate _REG method to register OpRegion presence
606 * @handle: ACPI device handle
607 * @space_id: ACPI address space id to register OpRegion presence for
608 * @function: Parameter to pass to _REG one of ACPI_REG_CONNECT or
609 * ACPI_REG_DISCONNECT
610 *
611 * Evaluate device's _REG method to register OpRegion presence.
612 */
613acpi_status acpi_evaluate_reg(acpi_handle handle, u8 space_id, u32 function)
614{
615 struct acpi_object_list arg_list;
616 union acpi_object params[2];
617
618 params[0].type = ACPI_TYPE_INTEGER;
619 params[0].integer.value = space_id;
620 params[1].type = ACPI_TYPE_INTEGER;
621 params[1].integer.value = function;
622 arg_list.count = 2;
623 arg_list.pointer = params;
624
625 return acpi_evaluate_object(handle, "_REG", &arg_list, NULL);
626}
627EXPORT_SYMBOL(acpi_evaluate_reg);
628
a65ac520
JL
629/**
630 * acpi_evaluate_dsm - evaluate device's _DSM method
631 * @handle: ACPI device handle
94116f81 632 * @guid: GUID of requested functions, should be 16 bytes
a65ac520
JL
633 * @rev: revision number of requested function
634 * @func: requested function number
635 * @argv4: the function specific parameter
636 *
94116f81 637 * Evaluate device's _DSM method with specified GUID, revision id and
a65ac520
JL
638 * function number. Caller needs to free the returned object.
639 *
640 * Though ACPI defines the fourth parameter for _DSM should be a package,
641 * some old BIOSes do expect a buffer or an integer etc.
642 */
643union acpi_object *
94116f81 644acpi_evaluate_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 func,
a65ac520
JL
645 union acpi_object *argv4)
646{
647 acpi_status ret;
648 struct acpi_buffer buf = {ACPI_ALLOCATE_BUFFER, NULL};
649 union acpi_object params[4];
650 struct acpi_object_list input = {
651 .count = 4,
652 .pointer = params,
653 };
654
655 params[0].type = ACPI_TYPE_BUFFER;
656 params[0].buffer.length = 16;
94116f81 657 params[0].buffer.pointer = (u8 *)guid;
a65ac520
JL
658 params[1].type = ACPI_TYPE_INTEGER;
659 params[1].integer.value = rev;
660 params[2].type = ACPI_TYPE_INTEGER;
661 params[2].integer.value = func;
662 if (argv4) {
663 params[3] = *argv4;
664 } else {
665 params[3].type = ACPI_TYPE_PACKAGE;
666 params[3].package.count = 0;
667 params[3].package.elements = NULL;
668 }
669
670 ret = acpi_evaluate_object(handle, "_DSM", &input, &buf);
671 if (ACPI_SUCCESS(ret))
672 return (union acpi_object *)buf.pointer;
673
674 if (ret != AE_NOT_FOUND)
675 acpi_handle_warn(handle,
676 "failed to evaluate _DSM (0x%x)\n", ret);
677
678 return NULL;
679}
680EXPORT_SYMBOL(acpi_evaluate_dsm);
681
682/**
683 * acpi_check_dsm - check if _DSM method supports requested functions.
684 * @handle: ACPI device handle
94116f81 685 * @guid: GUID of requested functions, should be 16 bytes at least
a65ac520
JL
686 * @rev: revision number of requested functions
687 * @funcs: bitmap of requested functions
a65ac520
JL
688 *
689 * Evaluate device's _DSM method to check whether it supports requested
690 * functions. Currently only support 64 functions at maximum, should be
691 * enough for now.
692 */
94116f81 693bool acpi_check_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 funcs)
a65ac520
JL
694{
695 int i;
696 u64 mask = 0;
697 union acpi_object *obj;
698
a7225598
DW
699 if (funcs == 0)
700 return false;
701
94116f81 702 obj = acpi_evaluate_dsm(handle, guid, rev, 0, NULL);
a65ac520
JL
703 if (!obj)
704 return false;
705
706 /* For compatibility, old BIOSes may return an integer */
707 if (obj->type == ACPI_TYPE_INTEGER)
708 mask = obj->integer.value;
709 else if (obj->type == ACPI_TYPE_BUFFER)
710 for (i = 0; i < obj->buffer.length && i < 8; i++)
4a798f50 711 mask |= (((u64)obj->buffer.pointer[i]) << (i * 8));
a65ac520
JL
712 ACPI_FREE(obj);
713
714 /*
715 * Bit 0 indicates whether there's support for any functions other than
94116f81 716 * function 0 for the specified GUID and revision.
a65ac520
JL
717 */
718 if ((mask & 0x1) && (mask & funcs) == funcs)
719 return true;
720
721 return false;
722}
723EXPORT_SYMBOL(acpi_check_dsm);
14ca7a47 724
35009c80
AS
725/**
726 * acpi_dev_hid_uid_match - Match device by supplied HID and UID
727 * @adev: ACPI device to match.
728 * @hid2: Hardware ID of the device.
729 * @uid2: Unique ID of the device, pass NULL to not check _UID.
730 *
731 * Matches HID and UID in @adev with given @hid2 and @uid2.
732 * Returns true if matches.
733 */
734bool acpi_dev_hid_uid_match(struct acpi_device *adev,
735 const char *hid2, const char *uid2)
736{
737 const char *hid1 = acpi_device_hid(adev);
738 const char *uid1 = acpi_device_uid(adev);
739
740 if (strcmp(hid1, hid2))
741 return false;
742
743 if (!uid2)
744 return true;
745
746 return uid1 && !strcmp(uid1, uid2);
747}
748EXPORT_SYMBOL(acpi_dev_hid_uid_match);
749
2d12b6b3 750/**
c68ae33e 751 * acpi_dev_found - Detect presence of a given ACPI device in the namespace.
2d12b6b3
LW
752 * @hid: Hardware ID of the device.
753 *
754 * Return %true if the device was present at the moment of invocation.
755 * Note that if the device is pluggable, it may since have disappeared.
756 *
757 * For this function to work, acpi_bus_scan() must have been executed
758 * which happens in the subsys_initcall() subsection. Hence, do not
759 * call from a subsys_initcall() or earlier (use acpi_get_devices()
760 * instead). Calling from module_init() is fine (which is synonymous
761 * with device_initcall()).
762 */
c68ae33e 763bool acpi_dev_found(const char *hid)
2d12b6b3
LW
764{
765 struct acpi_device_bus_id *acpi_device_bus_id;
766 bool found = false;
767
768 mutex_lock(&acpi_device_lock);
769 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node)
770 if (!strcmp(acpi_device_bus_id->bus_id, hid)) {
771 found = true;
772 break;
773 }
774 mutex_unlock(&acpi_device_lock);
775
776 return found;
777}
c68ae33e 778EXPORT_SYMBOL(acpi_dev_found);
2d12b6b3 779
67dcf8a3 780struct acpi_dev_match_info {
8661423e
HG
781 struct acpi_device_id hid[2];
782 const char *uid;
783 s64 hrv;
784};
785
418e3ea1 786static int acpi_dev_match_cb(struct device *dev, const void *data)
8661423e
HG
787{
788 struct acpi_device *adev = to_acpi_device(dev);
418e3ea1 789 const struct acpi_dev_match_info *match = data;
8661423e
HG
790 unsigned long long hrv;
791 acpi_status status;
792
793 if (acpi_match_device_ids(adev, match->hid))
794 return 0;
795
796 if (match->uid && (!adev->pnp.unique_id ||
797 strcmp(adev->pnp.unique_id, match->uid)))
798 return 0;
799
800 if (match->hrv == -1)
801 return 1;
802
803 status = acpi_evaluate_integer(adev->handle, "_HRV", NULL, &hrv);
804 if (ACPI_FAILURE(status))
805 return 0;
806
807 return hrv == match->hrv;
808}
809
810/**
811 * acpi_dev_present - Detect that a given ACPI device is present
812 * @hid: Hardware ID of the device.
813 * @uid: Unique ID of the device, pass NULL to not check _UID
814 * @hrv: Hardware Revision of the device, pass -1 to not check _HRV
815 *
816 * Return %true if a matching device was present at the moment of invocation.
817 * Note that if the device is pluggable, it may since have disappeared.
818 *
819 * Note that unlike acpi_dev_found() this function checks the status
820 * of the device. So for devices which are present in the dsdt, but
821 * which are disabled (their _STA callback returns 0) this function
822 * will return false.
823 *
824 * For this function to work, acpi_bus_scan() must have been executed
825 * which happens in the subsys_initcall() subsection. Hence, do not
826 * call from a subsys_initcall() or earlier (use acpi_get_devices()
827 * instead). Calling from module_init() is fine (which is synonymous
828 * with device_initcall()).
829 */
830bool acpi_dev_present(const char *hid, const char *uid, s64 hrv)
831{
67dcf8a3 832 struct acpi_dev_match_info match = {};
8661423e
HG
833 struct device *dev;
834
835 strlcpy(match.hid[0].id, hid, sizeof(match.hid[0].id));
836 match.uid = uid;
837 match.hrv = hrv;
838
67dcf8a3 839 dev = bus_find_device(&acpi_bus_type, NULL, &match, acpi_dev_match_cb);
54e3aca8 840 put_device(dev);
8661423e
HG
841 return !!dev;
842}
843EXPORT_SYMBOL(acpi_dev_present);
844
67dcf8a3 845/**
817b4d64 846 * acpi_dev_get_first_match_dev - Return the first match of ACPI device
67dcf8a3
AS
847 * @hid: Hardware ID of the device.
848 * @uid: Unique ID of the device, pass NULL to not check _UID
849 * @hrv: Hardware Revision of the device, pass -1 to not check _HRV
850 *
817b4d64 851 * Return the first match of ACPI device if a matching device was present
67dcf8a3
AS
852 * at the moment of invocation, or NULL otherwise.
853 *
817b4d64
AS
854 * The caller is responsible to call put_device() on the returned device.
855 *
67dcf8a3
AS
856 * See additional information in acpi_dev_present() as well.
857 */
817b4d64
AS
858struct acpi_device *
859acpi_dev_get_first_match_dev(const char *hid, const char *uid, s64 hrv)
860{
861 struct acpi_dev_match_info match = {};
862 struct device *dev;
863
864 strlcpy(match.hid[0].id, hid, sizeof(match.hid[0].id));
865 match.uid = uid;
866 match.hrv = hrv;
867
868 dev = bus_find_device(&acpi_bus_type, NULL, &match, acpi_dev_match_cb);
e6374f6b 869 return dev ? to_acpi_device(dev) : NULL;
817b4d64
AS
870}
871EXPORT_SYMBOL(acpi_dev_get_first_match_dev);
872
14ca7a47
HG
873/*
874 * acpi_backlight= handling, this is done here rather then in video_detect.c
875 * because __setup cannot be used in modules.
876 */
877char acpi_video_backlight_string[16];
878EXPORT_SYMBOL(acpi_video_backlight_string);
879
880static int __init acpi_backlight(char *str)
881{
882 strlcpy(acpi_video_backlight_string, str,
883 sizeof(acpi_video_backlight_string));
884 return 1;
885}
886__setup("acpi_backlight=", acpi_backlight);
5aa5911a
TK
887
888/**
889 * acpi_match_platform_list - Check if the system matches with a given list
890 * @plat: pointer to acpi_platform_list table terminated by a NULL entry
891 *
892 * Return the matched index if the system is found in the platform list.
893 * Otherwise, return a negative error code.
894 */
895int acpi_match_platform_list(const struct acpi_platform_list *plat)
896{
897 struct acpi_table_header hdr;
898 int idx = 0;
899
900 if (acpi_disabled)
901 return -ENODEV;
902
903 for (; plat->oem_id[0]; plat++, idx++) {
904 if (ACPI_FAILURE(acpi_get_table_header(plat->table, 0, &hdr)))
905 continue;
906
907 if (strncmp(plat->oem_id, hdr.oem_id, ACPI_OEM_ID_SIZE))
908 continue;
909
910 if (strncmp(plat->oem_table_id, hdr.oem_table_id, ACPI_OEM_TABLE_ID_SIZE))
911 continue;
912
913 if ((plat->pred == all_versions) ||
914 (plat->pred == less_than_or_equal && hdr.oem_revision <= plat->oem_revision) ||
915 (plat->pred == greater_than_or_equal && hdr.oem_revision >= plat->oem_revision) ||
916 (plat->pred == equal && hdr.oem_revision == plat->oem_revision))
917 return idx;
918 }
919
920 return -ENODEV;
921}
922EXPORT_SYMBOL(acpi_match_platform_list);