1 /* $NetBSD: acpi_i2c.c,v 1.8 2020/08/24 05:37:41 msaitoh Exp $ */ 2 3 /*- 4 * Copyright (c) 2017 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Manuel Bouyer. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: acpi_i2c.c,v 1.8 2020/08/24 05:37:41 msaitoh Exp $"); 34 35 #include <dev/acpi/acpireg.h> 36 #include <dev/acpi/acpivar.h> 37 #include <dev/acpi/acpi_i2c.h> 38 39 #define _COMPONENT ACPI_BUS_COMPONENT 40 ACPI_MODULE_NAME ("acpi_i2c") 41 42 static void 43 acpi_enter_i2c_hid(struct acpi_devnode *devnode, prop_dictionary_t dev) 44 { 45 ACPI_OBJECT_LIST arg; 46 ACPI_OBJECT obj[4]; 47 ACPI_OBJECT *osc; 48 ACPI_BUFFER buf; 49 ACPI_STATUS rv; 50 /* 3cdff6f7-4267-4555-ad05-b30a3d8938de */ 51 static uint8_t i2c_hid_guid[] = { 52 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45, 53 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE, 54 }; 55 56 arg.Count = 4; 57 arg.Pointer = obj; 58 59 obj[0].Type = ACPI_TYPE_BUFFER; 60 obj[0].Buffer.Length = sizeof(i2c_hid_guid); 61 obj[0].Buffer.Pointer = i2c_hid_guid; 62 63 /* rev */ 64 obj[1].Type = ACPI_TYPE_INTEGER; 65 obj[1].Integer.Value = 1; 66 67 /* func */ 68 obj[2].Type = ACPI_TYPE_INTEGER; 69 obj[2].Integer.Value = 1; 70 71 obj[3].Type = ACPI_TYPE_PACKAGE; 72 obj[3].Buffer.Length = 0; 73 74 buf.Pointer = NULL; 75 buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER; 76 77 rv = AcpiEvaluateObject(devnode->ad_handle, "_DSM", &arg, &buf); 78 79 if (ACPI_FAILURE(rv)) { 80 aprint_error("failed to evaluate _DSM for %s: %s\n", 81 devnode->ad_name, AcpiFormatException(rv)); 82 return; 83 } 84 85 osc = buf.Pointer; 86 if (osc->Type != ACPI_TYPE_INTEGER) { 87 aprint_error("bad _DSM return type %d for %s\n", 88 osc->Type, devnode->ad_name); 89 return; 90 } 91 prop_dictionary_set_uint32(dev, "hid-descr-addr", osc->Integer.Value); 92 } 93 94 struct acpi_i2c_id { 95 const char *id; 96 const char *compat; 97 const int compatlen; 98 void (*parse)(struct acpi_devnode *, prop_dictionary_t); 99 }; 100 101 static const struct acpi_i2c_id acpi_i2c_ids[] = { 102 { 103 .id = "PNP0C50", 104 .compat = "hid-over-i2c", 105 .compatlen = 13, 106 .parse = acpi_enter_i2c_hid 107 }, 108 { 109 .id = "ACPI0C50", 110 .compat = "hid-over-i2c", 111 .compatlen = 13, 112 .parse = acpi_enter_i2c_hid 113 }, 114 { 115 .id = NULL, 116 .compat = NULL, 117 .compatlen = 0, 118 .parse = NULL 119 } 120 }; 121 122 static const struct acpi_i2c_id * 123 acpi_i2c_search(const char *name) 124 { 125 int i; 126 for (i = 0; acpi_i2c_ids[i].id != NULL; i++) { 127 if (strcmp(name, acpi_i2c_ids[i].id) == 0) 128 return &acpi_i2c_ids[i]; 129 } 130 return NULL; 131 } 132 133 struct acpi_i2c_context { 134 uint16_t i2c_addr; 135 }; 136 137 static ACPI_STATUS 138 acpi_i2c_resource_parse_callback(ACPI_RESOURCE *res, void *context) 139 { 140 struct acpi_i2c_context *i2cc = context; 141 142 switch (res->Type) { 143 case ACPI_RESOURCE_TYPE_END_TAG: 144 break; 145 case ACPI_RESOURCE_TYPE_SERIAL_BUS: 146 switch (res->Data.I2cSerialBus.Type) { 147 case ACPI_RESOURCE_SERIAL_TYPE_I2C: 148 i2cc->i2c_addr = res->Data.I2cSerialBus.SlaveAddress; 149 break; 150 } 151 break; 152 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 153 break; 154 default: 155 printf("resource type 0x%x ignored\n", res->Type); 156 } 157 return_ACPI_STATUS(AE_OK); 158 } 159 160 static void 161 acpi_enter_i2c_device(struct acpi_devnode *ad, prop_array_t array) 162 { 163 prop_dictionary_t dev; 164 struct acpi_i2c_context i2cc; 165 ACPI_STATUS rv; 166 int cidi; 167 ACPI_PNP_DEVICE_ID_LIST *idlist; 168 const char *name; 169 static const struct acpi_i2c_id *i2c_id; 170 171 memset(&i2cc, 0, sizeof(i2cc)); 172 rv = AcpiWalkResources(ad->ad_handle, "_CRS", 173 acpi_i2c_resource_parse_callback, &i2cc); 174 if (ACPI_FAILURE(rv)) { 175 aprint_error("ACPI: unable to get resources " 176 "for %s: %s\n", ad->ad_name, 177 AcpiFormatException(rv)); 178 return; 179 } 180 if (i2cc.i2c_addr == 0) 181 return; 182 dev = prop_dictionary_create(); 183 if (dev == NULL) { 184 aprint_error("ignoring device %s (no memory)\n", 185 ad->ad_name); 186 return; 187 } 188 if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0) 189 name = ad->ad_name; 190 else 191 name = ad->ad_devinfo->HardwareId.String; 192 prop_dictionary_set_string(dev, "name", name); 193 prop_dictionary_set_uint32(dev, "addr", i2cc.i2c_addr); 194 prop_dictionary_set_uint64(dev, "cookie", (uintptr_t)ad->ad_handle); 195 /* first search by name, then by CID */ 196 i2c_id = acpi_i2c_search(name); 197 idlist = &ad->ad_devinfo->CompatibleIdList; 198 for (cidi = 0; 199 cidi < idlist->Count && i2c_id == NULL; 200 cidi++) { 201 i2c_id = acpi_i2c_search(idlist->Ids[cidi].String); 202 } 203 if (i2c_id != NULL) { 204 if (i2c_id->compat != NULL) { 205 prop_data_t data; 206 data = prop_data_create_copy(i2c_id->compat, 207 i2c_id->compatlen); 208 prop_dictionary_set(dev, "compatible", data); 209 prop_object_release(data); 210 } 211 if (i2c_id->parse != NULL) 212 i2c_id->parse(ad, dev); 213 } 214 prop_array_add(array, dev); 215 prop_object_release(dev); 216 } 217 218 219 prop_array_t 220 acpi_enter_i2c_devs(struct acpi_devnode *devnode) 221 { 222 struct acpi_devnode *ad; 223 prop_array_t array = prop_array_create(); 224 225 if (array == NULL) 226 return NULL; 227 228 SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) { 229 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE) 230 continue; 231 if (!acpi_device_present(ad->ad_handle)) 232 continue; 233 acpi_enter_i2c_device(ad, array); 234 } 235 return array; 236 } 237