1 /*- 2 * Copyright (c) 2003 Nate Lawson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/bus.h> 33 #include <sys/sbuf.h> 34 35 #include <machine/bus.h> 36 #include <machine/resource.h> 37 #include <sys/rman.h> 38 39 #include <contrib/dev/acpica/acpi.h> 40 #include <dev/acpica/acpivar.h> 41 42 /* 43 * Package manipulation convenience functions 44 */ 45 46 int 47 acpi_PkgInt(ACPI_OBJECT *res, int idx, ACPI_INTEGER *dst) 48 { 49 ACPI_OBJECT *obj; 50 51 obj = &res->Package.Elements[idx]; 52 if (obj == NULL || obj->Type != ACPI_TYPE_INTEGER) 53 return (EINVAL); 54 *dst = obj->Integer.Value; 55 56 return (0); 57 } 58 59 int 60 acpi_PkgInt32(ACPI_OBJECT *res, int idx, uint32_t *dst) 61 { 62 ACPI_INTEGER tmp; 63 int error; 64 65 error = acpi_PkgInt(res, idx, &tmp); 66 if (error == 0) 67 *dst = (uint32_t)tmp; 68 69 return (error); 70 } 71 72 int 73 acpi_PkgStr(ACPI_OBJECT *res, int idx, void *dst, size_t size) 74 { 75 ACPI_OBJECT *obj; 76 void *ptr; 77 size_t length; 78 79 obj = &res->Package.Elements[idx]; 80 if (obj == NULL) 81 return (EINVAL); 82 bzero(dst, sizeof(dst)); 83 84 switch (obj->Type) { 85 case ACPI_TYPE_STRING: 86 ptr = obj->String.Pointer; 87 length = obj->String.Length; 88 break; 89 case ACPI_TYPE_BUFFER: 90 ptr = obj->Buffer.Pointer; 91 length = obj->Buffer.Length; 92 break; 93 default: 94 return (EINVAL); 95 } 96 97 /* Make sure string will fit, including terminating NUL */ 98 if (++length > size) 99 return (E2BIG); 100 101 strlcpy(dst, ptr, length); 102 return (0); 103 } 104 105 int 106 acpi_PkgGas(device_t dev, ACPI_OBJECT *res, int idx, int *type, int *rid, 107 struct resource **dst, u_int flags) 108 { 109 ACPI_GENERIC_ADDRESS gas; 110 ACPI_OBJECT *obj; 111 112 obj = &res->Package.Elements[idx]; 113 if (obj == NULL || obj->Type != ACPI_TYPE_BUFFER || 114 obj->Buffer.Length < sizeof(ACPI_GENERIC_ADDRESS) + 3) 115 return (EINVAL); 116 117 memcpy(&gas, obj->Buffer.Pointer + 3, sizeof(gas)); 118 119 return (acpi_bus_alloc_gas(dev, type, rid, &gas, dst, flags)); 120 } 121 122 ACPI_HANDLE 123 acpi_GetReference(ACPI_HANDLE scope, ACPI_OBJECT *obj) 124 { 125 ACPI_HANDLE h; 126 127 if (obj == NULL) 128 return (NULL); 129 130 switch (obj->Type) { 131 case ACPI_TYPE_LOCAL_REFERENCE: 132 case ACPI_TYPE_ANY: 133 h = obj->Reference.Handle; 134 break; 135 case ACPI_TYPE_STRING: 136 /* 137 * The String object usually contains a fully-qualified path, so 138 * scope can be NULL. 139 * 140 * XXX This may not always be the case. 141 */ 142 if (ACPI_FAILURE(AcpiGetHandle(scope, obj->String.Pointer, &h))) 143 h = NULL; 144 break; 145 default: 146 h = NULL; 147 break; 148 } 149 150 return (h); 151 } 152