1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
7 */
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/hardirq.h>
15 #include <linux/acpi.h>
16 #include <linux/dynamic_debug.h>
17
18 #include "internal.h"
19 #include "sleep.h"
20
21 #define _COMPONENT ACPI_BUS_COMPONENT
22 ACPI_MODULE_NAME("utils");
23
24 /* --------------------------------------------------------------------------
25 Object Evaluation Helpers
26 -------------------------------------------------------------------------- */
27 static void
acpi_util_eval_error(acpi_handle h,acpi_string p,acpi_status s)28 acpi_util_eval_error(acpi_handle h, acpi_string p, acpi_status s)
29 {
30 #ifdef ACPI_DEBUG_OUTPUT
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)));
36 #else
37 return;
38 #endif
39 }
40
41 acpi_status
acpi_extract_package(union acpi_object * package,struct acpi_buffer * format,struct acpi_buffer * buffer)42 acpi_extract_package(union acpi_object *package,
43 struct acpi_buffer *format, struct acpi_buffer *buffer)
44 {
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;
52
53
54 if (!package || (package->type != ACPI_TYPE_PACKAGE)
55 || (package->package.count < 1)) {
56 printk(KERN_WARNING PREFIX "Invalid package argument\n");
57 return AE_BAD_PARAMETER;
58 }
59
60 if (!format || !format->pointer || (format->length < 1)) {
61 printk(KERN_WARNING PREFIX "Invalid format argument\n");
62 return AE_BAD_PARAMETER;
63 }
64
65 if (!buffer) {
66 printk(KERN_WARNING PREFIX "Invalid buffer argument\n");
67 return AE_BAD_PARAMETER;
68 }
69
70 format_count = (format->length / sizeof(char)) - 1;
71 if (format_count > package->package.count) {
72 printk(KERN_WARNING PREFIX "Format specifies more objects [%d]"
73 " than exist in package [%d].\n",
74 format_count, package->package.count);
75 return AE_BAD_DATA;
76 }
77
78 format_string = format->pointer;
79
80 /*
81 * Calculate size_required.
82 */
83 for (i = 0; i < format_count; i++) {
84
85 union acpi_object *element = &(package->package.elements[i]);
86
87 switch (element->type) {
88
89 case ACPI_TYPE_INTEGER:
90 switch (format_string[i]) {
91 case 'N':
92 size_required += sizeof(u64);
93 tail_offset += sizeof(u64);
94 break;
95 case 'S':
96 size_required +=
97 sizeof(char *) + sizeof(u64) +
98 sizeof(char);
99 tail_offset += sizeof(char *);
100 break;
101 default:
102 printk(KERN_WARNING PREFIX "Invalid package element"
103 " [%d]: got number, expecting"
104 " [%c]\n",
105 i, format_string[i]);
106 return AE_BAD_DATA;
107 }
108 break;
109
110 case ACPI_TYPE_STRING:
111 case ACPI_TYPE_BUFFER:
112 switch (format_string[i]) {
113 case 'S':
114 size_required +=
115 sizeof(char *) +
116 (element->string.length * sizeof(char)) +
117 sizeof(char);
118 tail_offset += sizeof(char *);
119 break;
120 case 'B':
121 size_required +=
122 sizeof(u8 *) + element->buffer.length;
123 tail_offset += sizeof(u8 *);
124 break;
125 default:
126 printk(KERN_WARNING PREFIX "Invalid package element"
127 " [%d] got string/buffer,"
128 " expecting [%c]\n",
129 i, format_string[i]);
130 return AE_BAD_DATA;
131 }
132 break;
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;
145 }
146 break;
147
148 case ACPI_TYPE_PACKAGE:
149 default:
150 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
151 "Found unsupported element at index=%d\n",
152 i));
153 /* TBD: handle nested packages... */
154 return AE_SUPPORT;
155 }
156 }
157
158 /*
159 * Validate output buffer.
160 */
161 if (buffer->length == ACPI_ALLOCATE_BUFFER) {
162 buffer->pointer = ACPI_ALLOCATE_ZEROED(size_required);
163 if (!buffer->pointer)
164 return AE_NO_MEMORY;
165 buffer->length = size_required;
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 }
174 }
175
176 head = buffer->pointer;
177 tail = buffer->pointer + tail_offset;
178
179 /*
180 * Extract package data.
181 */
182 for (i = 0; i < format_count; i++) {
183
184 u8 **pointer = NULL;
185 union acpi_object *element = &(package->package.elements[i]);
186
187 switch (element->type) {
188
189 case ACPI_TYPE_INTEGER:
190 switch (format_string[i]) {
191 case 'N':
192 *((u64 *) head) =
193 element->integer.value;
194 head += sizeof(u64);
195 break;
196 case 'S':
197 pointer = (u8 **) head;
198 *pointer = tail;
199 *((u64 *) tail) =
200 element->integer.value;
201 head += sizeof(u64 *);
202 tail += sizeof(u64);
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':
217 pointer = (u8 **) head;
218 *pointer = tail;
219 memcpy(tail, element->string.pointer,
220 element->string.length);
221 head += sizeof(char *);
222 tail += element->string.length * sizeof(char);
223 /* NULL terminate string */
224 *tail = (char)0;
225 tail += sizeof(char);
226 break;
227 case 'B':
228 pointer = (u8 **) head;
229 *pointer = tail;
230 memcpy(tail, element->buffer.pointer,
231 element->buffer.length);
232 head += sizeof(u8 *);
233 tail += element->buffer.length;
234 break;
235 default:
236 /* Should never get here */
237 break;
238 }
239 break;
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;
252 case ACPI_TYPE_PACKAGE:
253 /* TBD: handle nested packages... */
254 default:
255 /* Should never get here */
256 break;
257 }
258 }
259
260 return AE_OK;
261 }
262
263 EXPORT_SYMBOL(acpi_extract_package);
264
265 acpi_status
acpi_evaluate_integer(acpi_handle handle,acpi_string pathname,struct acpi_object_list * arguments,unsigned long long * data)266 acpi_evaluate_integer(acpi_handle handle,
267 acpi_string pathname,
268 struct acpi_object_list *arguments, unsigned long long *data)
269 {
270 acpi_status status = AE_OK;
271 union acpi_object element;
272 struct acpi_buffer buffer = { 0, NULL };
273
274 if (!data)
275 return AE_BAD_PARAMETER;
276
277 buffer.length = sizeof(union acpi_object);
278 buffer.pointer = &element;
279 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
280 if (ACPI_FAILURE(status)) {
281 acpi_util_eval_error(handle, pathname, status);
282 return status;
283 }
284
285 if (element.type != ACPI_TYPE_INTEGER) {
286 acpi_util_eval_error(handle, pathname, AE_BAD_DATA);
287 return AE_BAD_DATA;
288 }
289
290 *data = element.integer.value;
291
292 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Return value [%llu]\n", *data));
293
294 return AE_OK;
295 }
296
297 EXPORT_SYMBOL(acpi_evaluate_integer);
298
299 acpi_status
acpi_evaluate_reference(acpi_handle handle,acpi_string pathname,struct acpi_object_list * arguments,struct acpi_handle_list * list)300 acpi_evaluate_reference(acpi_handle handle,
301 acpi_string pathname,
302 struct acpi_object_list *arguments,
303 struct acpi_handle_list *list)
304 {
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;
310
311
312 if (!list) {
313 return AE_BAD_PARAMETER;
314 }
315
316 /* Evaluate object. */
317
318 status = acpi_evaluate_object(handle, pathname, arguments, &buffer);
319 if (ACPI_FAILURE(status))
320 goto end;
321
322 package = buffer.pointer;
323
324 if ((buffer.length == 0) || !package) {
325 status = AE_BAD_DATA;
326 acpi_util_eval_error(handle, pathname, status);
327 goto end;
328 }
329 if (package->type != ACPI_TYPE_PACKAGE) {
330 status = AE_BAD_DATA;
331 acpi_util_eval_error(handle, pathname, status);
332 goto end;
333 }
334 if (!package->package.count) {
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) {
341 kfree(package);
342 return AE_NO_MEMORY;
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
352 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
353 status = AE_BAD_DATA;
354 acpi_util_eval_error(handle, pathname, status);
355 break;
356 }
357
358 if (!element->reference.handle) {
359 status = AE_NULL_ENTRY;
360 acpi_util_eval_error(handle, pathname, status);
361 break;
362 }
363 /* Get the acpi_handle. */
364
365 list->handles[i] = element->reference.handle;
366 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found reference [%p]\n",
367 list->handles[i]));
368 }
369
370 end:
371 if (ACPI_FAILURE(status)) {
372 list->count = 0;
373 //kfree(list->handles);
374 }
375
376 kfree(buffer.pointer);
377
378 return status;
379 }
380
381 EXPORT_SYMBOL(acpi_evaluate_reference);
382
383 acpi_status
acpi_get_physical_device_location(acpi_handle handle,struct acpi_pld_info ** pld)384 acpi_get_physical_device_location(acpi_handle handle, struct acpi_pld_info **pld)
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
400 || output->package.elements[0].buffer.length < ACPI_PLD_REV1_BUFFER_SIZE) {
401 status = AE_TYPE;
402 goto out;
403 }
404
405 status = acpi_decode_pld_buffer(
406 output->package.elements[0].buffer.pointer,
407 output->package.elements[0].buffer.length,
408 pld);
409
410 out:
411 kfree(buffer.pointer);
412 return status;
413 }
414 EXPORT_SYMBOL(acpi_get_physical_device_location);
415
416 /**
417 * acpi_evaluate_ost: Evaluate _OST for hotplug operations
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 */
427 acpi_status
acpi_evaluate_ost(acpi_handle handle,u32 source_event,u32 status_code,struct acpi_buffer * status_buf)428 acpi_evaluate_ost(acpi_handle handle, u32 source_event, u32 status_code,
429 struct acpi_buffer *status_buf)
430 {
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};
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
448 return acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
449 }
450 EXPORT_SYMBOL(acpi_evaluate_ost);
451
452 /**
453 * acpi_handle_path: Return the object path of handle
454 * @handle: ACPI device handle
455 *
456 * Caller must free the returned buffer
457 */
acpi_handle_path(acpi_handle handle)458 static 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
471 /**
472 * acpi_handle_printk: Print message with ACPI prefix and object path
473 * @level: log level
474 * @handle: ACPI device handle
475 * @fmt: format string
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 */
482 void
acpi_handle_printk(const char * level,acpi_handle handle,const char * fmt,...)483 acpi_handle_printk(const char *level, acpi_handle handle, const char *fmt, ...)
484 {
485 struct va_format vaf;
486 va_list args;
487 const char *path;
488
489 va_start(args, fmt);
490 vaf.fmt = fmt;
491 vaf.va = &args;
492
493 path = acpi_handle_path(handle);
494 printk("%sACPI: %s: %pV", level, path ? path : "<n/a>" , &vaf);
495
496 va_end(args);
497 kfree(path);
498 }
499 EXPORT_SYMBOL(acpi_handle_printk);
500
501 #if defined(CONFIG_DYNAMIC_DEBUG)
502 /**
503 * __acpi_handle_debug: pr_debug with ACPI prefix and object path
504 * @descriptor: Dynamic Debug descriptor
505 * @handle: ACPI device handle
506 * @fmt: format string
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 */
513 void
__acpi_handle_debug(struct _ddebug * descriptor,acpi_handle handle,const char * fmt,...)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 }
531 EXPORT_SYMBOL(__acpi_handle_debug);
532 #endif
533
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 */
acpi_has_method(acpi_handle handle,char * name)541 bool 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 }
547 EXPORT_SYMBOL(acpi_has_method);
548
acpi_execute_simple_method(acpi_handle handle,char * method,u64 arg)549 acpi_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 }
559 EXPORT_SYMBOL(acpi_execute_simple_method);
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 */
acpi_evaluate_ej0(acpi_handle handle)567 acpi_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 */
acpi_evaluate_lck(acpi_handle handle,int lock)587 acpi_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 }
603
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 */
acpi_evaluate_reg(acpi_handle handle,u8 space_id,u32 function)613 acpi_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 }
627 EXPORT_SYMBOL(acpi_evaluate_reg);
628
629 /**
630 * acpi_evaluate_dsm - evaluate device's _DSM method
631 * @handle: ACPI device handle
632 * @guid: GUID of requested functions, should be 16 bytes
633 * @rev: revision number of requested function
634 * @func: requested function number
635 * @argv4: the function specific parameter
636 *
637 * Evaluate device's _DSM method with specified GUID, revision id and
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 */
643 union acpi_object *
acpi_evaluate_dsm(acpi_handle handle,const guid_t * guid,u64 rev,u64 func,union acpi_object * argv4)644 acpi_evaluate_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 func,
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;
657 params[0].buffer.pointer = (u8 *)guid;
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 }
680 EXPORT_SYMBOL(acpi_evaluate_dsm);
681
682 /**
683 * acpi_check_dsm - check if _DSM method supports requested functions.
684 * @handle: ACPI device handle
685 * @guid: GUID of requested functions, should be 16 bytes at least
686 * @rev: revision number of requested functions
687 * @funcs: bitmap of requested functions
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 */
acpi_check_dsm(acpi_handle handle,const guid_t * guid,u64 rev,u64 funcs)693 bool acpi_check_dsm(acpi_handle handle, const guid_t *guid, u64 rev, u64 funcs)
694 {
695 int i;
696 u64 mask = 0;
697 union acpi_object *obj;
698
699 if (funcs == 0)
700 return false;
701
702 obj = acpi_evaluate_dsm(handle, guid, rev, 0, NULL);
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++)
711 mask |= (((u64)obj->buffer.pointer[i]) << (i * 8));
712 ACPI_FREE(obj);
713
714 /*
715 * Bit 0 indicates whether there's support for any functions other than
716 * function 0 for the specified GUID and revision.
717 */
718 if ((mask & 0x1) && (mask & funcs) == funcs)
719 return true;
720
721 return false;
722 }
723 EXPORT_SYMBOL(acpi_check_dsm);
724
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 */
acpi_dev_hid_uid_match(struct acpi_device * adev,const char * hid2,const char * uid2)734 bool 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 }
748 EXPORT_SYMBOL(acpi_dev_hid_uid_match);
749
750 /**
751 * acpi_dev_found - Detect presence of a given ACPI device in the namespace.
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 */
acpi_dev_found(const char * hid)763 bool acpi_dev_found(const char *hid)
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 }
778 EXPORT_SYMBOL(acpi_dev_found);
779
780 struct acpi_dev_match_info {
781 struct acpi_device_id hid[2];
782 const char *uid;
783 s64 hrv;
784 };
785
acpi_dev_match_cb(struct device * dev,const void * data)786 static int acpi_dev_match_cb(struct device *dev, const void *data)
787 {
788 struct acpi_device *adev = to_acpi_device(dev);
789 const struct acpi_dev_match_info *match = data;
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 */
acpi_dev_present(const char * hid,const char * uid,s64 hrv)830 bool acpi_dev_present(const char *hid, const char *uid, s64 hrv)
831 {
832 struct acpi_dev_match_info match = {};
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
839 dev = bus_find_device(&acpi_bus_type, NULL, &match, acpi_dev_match_cb);
840 put_device(dev);
841 return !!dev;
842 }
843 EXPORT_SYMBOL(acpi_dev_present);
844
845 /**
846 * acpi_dev_get_first_match_dev - Return the first match of ACPI device
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 *
851 * Return the first match of ACPI device if a matching device was present
852 * at the moment of invocation, or NULL otherwise.
853 *
854 * The caller is responsible to call put_device() on the returned device.
855 *
856 * See additional information in acpi_dev_present() as well.
857 */
858 struct acpi_device *
acpi_dev_get_first_match_dev(const char * hid,const char * uid,s64 hrv)859 acpi_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);
869 return dev ? to_acpi_device(dev) : NULL;
870 }
871 EXPORT_SYMBOL(acpi_dev_get_first_match_dev);
872
873 /*
874 * acpi_backlight= handling, this is done here rather then in video_detect.c
875 * because __setup cannot be used in modules.
876 */
877 char acpi_video_backlight_string[16];
878 EXPORT_SYMBOL(acpi_video_backlight_string);
879
acpi_backlight(char * str)880 static 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);
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 */
acpi_match_platform_list(const struct acpi_platform_list * plat)895 int 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 }
922 EXPORT_SYMBOL(acpi_match_platform_list);
923