Home | History | Annotate | Line # | Download | only in acpi
acpi_i2c.c revision 1.21
      1  1.21   thorpej /* $NetBSD: acpi_i2c.c,v 1.21 2025/09/15 15:28:48 thorpej Exp $ */
      2   1.1    bouyer 
      3   1.1    bouyer /*-
      4  1.11   thorpej  * Copyright (c) 2017, 2021 The NetBSD Foundation, Inc.
      5   1.1    bouyer  * All rights reserved.
      6   1.1    bouyer  *
      7   1.1    bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1    bouyer  * by Manuel Bouyer.
      9   1.1    bouyer  *
     10   1.1    bouyer  * Redistribution and use in source and binary forms, with or without
     11   1.1    bouyer  * modification, are permitted provided that the following conditions
     12   1.1    bouyer  * are met:
     13   1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     14   1.1    bouyer  *    notice, this list of conditions and the following disclaimer.
     15   1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     18   1.1    bouyer  *
     19   1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1    bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1    bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1    bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1    bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1    bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1    bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1    bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1    bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1    bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1    bouyer  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1    bouyer  */
     31   1.1    bouyer 
     32  1.16  jmcneill #include "iic.h"
     33  1.16  jmcneill 
     34   1.1    bouyer #include <sys/cdefs.h>
     35  1.21   thorpej __KERNEL_RCSID(0, "$NetBSD: acpi_i2c.c,v 1.21 2025/09/15 15:28:48 thorpej Exp $");
     36  1.13  jmcneill 
     37  1.13  jmcneill #include <sys/device.h>
     38   1.1    bouyer 
     39   1.1    bouyer #include <dev/acpi/acpireg.h>
     40   1.1    bouyer #include <dev/acpi/acpivar.h>
     41   1.1    bouyer #include <dev/acpi/acpi_i2c.h>
     42  1.14  jmcneill #include <external/bsd/acpica/dist/include/acinterp.h>
     43  1.17  jmcneill #include <external/bsd/acpica/dist/include/amlcode.h>
     44   1.9  jmcneill #include <dev/i2c/i2cvar.h>
     45   1.1    bouyer 
     46  1.11   thorpej #include <sys/kmem.h>
     47  1.11   thorpej 
     48   1.2    bouyer #define _COMPONENT	ACPI_BUS_COMPONENT
     49   1.2    bouyer ACPI_MODULE_NAME	("acpi_i2c")
     50   1.2    bouyer 
     51  1.14  jmcneill struct acpi_i2c_address_space_context {
     52  1.14  jmcneill 	ACPI_CONNECTION_INFO conn_info;	/* must be first */
     53  1.14  jmcneill 	i2c_tag_t tag;
     54  1.14  jmcneill };
     55  1.14  jmcneill 
     56  1.13  jmcneill static const struct device_compatible_entry hid_compat_data[] = {
     57  1.13  jmcneill 	{ .compat = "PNP0C50" },
     58  1.13  jmcneill 	DEVICE_COMPAT_EOL
     59  1.13  jmcneill };
     60  1.13  jmcneill 
     61  1.16  jmcneill #if NIIC > 0
     62   1.1    bouyer struct acpi_i2c_context {
     63   1.1    bouyer 	uint16_t i2c_addr;
     64  1.13  jmcneill 	struct acpi_devnode *res_src;
     65   1.1    bouyer };
     66  1.16  jmcneill #endif
     67   1.1    bouyer 
     68  1.13  jmcneill static struct acpi_devnode *
     69  1.13  jmcneill acpi_i2c_resource_find_source(ACPI_RESOURCE_SOURCE *rs)
     70  1.13  jmcneill {
     71  1.13  jmcneill 	ACPI_STATUS rv;
     72  1.13  jmcneill 	ACPI_HANDLE hdl;
     73  1.13  jmcneill 	struct acpi_devnode *ad;
     74  1.13  jmcneill 
     75  1.13  jmcneill 	if (rs->StringPtr == NULL) {
     76  1.13  jmcneill 		return NULL;
     77  1.13  jmcneill 	}
     78  1.13  jmcneill 
     79  1.13  jmcneill 	rv = AcpiGetHandle(NULL, rs->StringPtr, &hdl);
     80  1.13  jmcneill 	if (ACPI_FAILURE(rv)) {
     81  1.13  jmcneill 		printf("%s: couldn't lookup '%s': %s\n", __func__,
     82  1.13  jmcneill 		    rs->StringPtr, AcpiFormatException(rv));
     83  1.13  jmcneill 		return NULL;
     84  1.13  jmcneill 	}
     85  1.13  jmcneill 
     86  1.13  jmcneill 	SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
     87  1.13  jmcneill 		if (ad->ad_handle == hdl) {
     88  1.13  jmcneill 			return ad;
     89  1.13  jmcneill 		}
     90  1.13  jmcneill 	}
     91  1.13  jmcneill 
     92  1.13  jmcneill 	printf("%s: no acpi devnode matching resource source '%s'\n",
     93  1.13  jmcneill 	    __func__, rs->StringPtr);
     94  1.13  jmcneill 	return NULL;
     95  1.13  jmcneill }
     96  1.13  jmcneill 
     97   1.1    bouyer static ACPI_STATUS
     98   1.1    bouyer acpi_i2c_resource_parse_callback(ACPI_RESOURCE *res, void *context)
     99   1.1    bouyer {
    100   1.1    bouyer 	struct acpi_i2c_context *i2cc = context;
    101   1.1    bouyer 
    102   1.1    bouyer 	switch (res->Type) {
    103   1.1    bouyer 	case ACPI_RESOURCE_TYPE_END_TAG:
    104   1.1    bouyer 		break;
    105   1.1    bouyer 	case ACPI_RESOURCE_TYPE_SERIAL_BUS:
    106   1.1    bouyer 		switch (res->Data.I2cSerialBus.Type) {
    107   1.1    bouyer 		case ACPI_RESOURCE_SERIAL_TYPE_I2C:
    108   1.1    bouyer 			i2cc->i2c_addr = res->Data.I2cSerialBus.SlaveAddress;
    109  1.13  jmcneill 			i2cc->res_src = acpi_i2c_resource_find_source(
    110  1.13  jmcneill 			    &res->Data.I2cSerialBus.ResourceSource);
    111   1.1    bouyer 			break;
    112   1.1    bouyer 		}
    113   1.1    bouyer 		break;
    114   1.1    bouyer 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
    115   1.1    bouyer 		break;
    116   1.1    bouyer 	default:
    117  1.15  jmcneill 		break;
    118   1.1    bouyer 	}
    119   1.1    bouyer 	return_ACPI_STATUS(AE_OK);
    120   1.1    bouyer }
    121   1.1    bouyer 
    122   1.1    bouyer static void
    123   1.1    bouyer acpi_enter_i2c_device(struct acpi_devnode *ad, prop_array_t array)
    124   1.1    bouyer {
    125   1.1    bouyer 	prop_dictionary_t dev;
    126   1.1    bouyer 	struct acpi_i2c_context i2cc;
    127   1.1    bouyer 	ACPI_STATUS rv;
    128  1.11   thorpej 	char *clist;
    129  1.11   thorpej 	size_t clist_size;
    130   1.1    bouyer 
    131   1.1    bouyer 	memset(&i2cc, 0, sizeof(i2cc));
    132   1.1    bouyer 	rv = AcpiWalkResources(ad->ad_handle, "_CRS",
    133   1.1    bouyer 	     acpi_i2c_resource_parse_callback, &i2cc);
    134   1.1    bouyer 	if (ACPI_FAILURE(rv)) {
    135   1.1    bouyer 		return;
    136   1.1    bouyer 	}
    137   1.1    bouyer 	if (i2cc.i2c_addr == 0)
    138   1.1    bouyer 		return;
    139   1.1    bouyer 	dev = prop_dictionary_create();
    140   1.1    bouyer 	if (dev == NULL) {
    141   1.1    bouyer 		aprint_error("ignoring device %s (no memory)\n",
    142   1.1    bouyer 		    ad->ad_name);
    143   1.1    bouyer 		return;
    144   1.1    bouyer 	}
    145  1.12   thorpej 	clist = acpi_pack_compat_list(ad, &clist_size);
    146  1.11   thorpej 	if (clist == NULL) {
    147  1.11   thorpej 		prop_object_release(dev);
    148  1.11   thorpej 		aprint_error("ignoring device %s (no _HID or _CID)\n",
    149  1.11   thorpej 		    ad->ad_name);
    150  1.11   thorpej 		return;
    151  1.11   thorpej 	}
    152  1.12   thorpej 	prop_dictionary_set_string(dev, "name", ad->ad_name);
    153   1.1    bouyer 	prop_dictionary_set_uint32(dev, "addr", i2cc.i2c_addr);
    154   1.1    bouyer 	prop_dictionary_set_uint64(dev, "cookie", (uintptr_t)ad->ad_handle);
    155   1.9  jmcneill 	prop_dictionary_set_uint32(dev, "cookietype", I2C_COOKIE_ACPI);
    156  1.11   thorpej 	prop_dictionary_set_data(dev, "compatible", clist, clist_size);
    157  1.11   thorpej 	kmem_free(clist, clist_size);
    158  1.11   thorpej 
    159   1.1    bouyer 	prop_array_add(array, dev);
    160   1.1    bouyer 	prop_object_release(dev);
    161   1.1    bouyer }
    162   1.1    bouyer 
    163  1.13  jmcneill static void
    164  1.13  jmcneill acpi_enter_i2chid_devs(device_t dev, struct acpi_devnode *devnode,
    165  1.13  jmcneill     prop_array_t array)
    166  1.13  jmcneill {
    167  1.13  jmcneill 	struct acpi_devnode *ad;
    168  1.13  jmcneill 
    169  1.13  jmcneill 	KASSERT(dev != NULL);
    170  1.13  jmcneill 
    171  1.13  jmcneill 	SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
    172  1.13  jmcneill 		struct acpi_attach_args aa = {
    173  1.13  jmcneill 			.aa_node = ad
    174  1.13  jmcneill 		};
    175  1.13  jmcneill 		struct acpi_i2c_context i2cc;
    176  1.13  jmcneill 		ACPI_STATUS rv;
    177  1.13  jmcneill 
    178  1.13  jmcneill 		if (!acpi_device_present(ad->ad_handle))
    179  1.13  jmcneill 			continue;
    180  1.13  jmcneill 		if (ad->ad_device != NULL)
    181  1.13  jmcneill 			continue;
    182  1.13  jmcneill 		if (acpi_compatible_match(&aa, hid_compat_data) == 0)
    183  1.13  jmcneill 			continue;
    184  1.13  jmcneill 
    185  1.13  jmcneill 		memset(&i2cc, 0, sizeof(i2cc));
    186  1.13  jmcneill 		rv = AcpiWalkResources(ad->ad_handle, "_CRS",
    187  1.13  jmcneill 		    acpi_i2c_resource_parse_callback, &i2cc);
    188  1.13  jmcneill 		if (ACPI_SUCCESS(rv) &&
    189  1.13  jmcneill 		    i2cc.i2c_addr != 0 &&
    190  1.13  jmcneill 		    i2cc.res_src == devnode) {
    191  1.13  jmcneill 			aprint_debug_dev(dev, "claiming %s\n", ad->ad_name);
    192  1.18  jmcneill 			ad->ad_device = dev;
    193  1.19  jmcneill 			acpi_claim_childdevs(dev, ad, NULL);
    194  1.13  jmcneill 			acpi_enter_i2c_device(ad, array);
    195  1.13  jmcneill 		}
    196  1.13  jmcneill 	}
    197  1.13  jmcneill }
    198  1.13  jmcneill 
    199   1.1    bouyer prop_array_t
    200  1.20   thorpej acpi_copy_i2c_devs(device_t dev)
    201   1.1    bouyer {
    202   1.1    bouyer 	struct acpi_devnode *ad;
    203  1.20   thorpej 	ACPI_HANDLE *hdl = devhandle_to_acpi(device_handle(dev));
    204  1.20   thorpej 	struct acpi_devnode *devnode = acpi_match_node(hdl);
    205  1.20   thorpej 
    206  1.20   thorpej 	if (devnode == NULL) {
    207  1.20   thorpej 		aprint_error_dev(dev, "%s: no devnode matching handle\n",
    208  1.20   thorpej 		    __func__);
    209  1.20   thorpej 		return NULL;
    210  1.20   thorpej 	}
    211  1.20   thorpej 
    212   1.1    bouyer 	prop_array_t array = prop_array_create();
    213   1.1    bouyer 	if (array == NULL)
    214   1.1    bouyer 		return NULL;
    215   1.1    bouyer 
    216   1.1    bouyer 	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
    217   1.1    bouyer 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
    218   1.1    bouyer 			continue;
    219   1.4  christos 		if (!acpi_device_present(ad->ad_handle))
    220   1.4  christos 			continue;
    221   1.1    bouyer 		acpi_enter_i2c_device(ad, array);
    222   1.1    bouyer 	}
    223  1.10  jmcneill 
    224  1.10  jmcneill 	if (dev != NULL) {
    225  1.19  jmcneill 		acpi_claim_childdevs(dev, devnode, "_CRS");
    226  1.19  jmcneill 		acpi_claim_childdevs(dev, devnode, "_ADR");
    227  1.13  jmcneill 		acpi_enter_i2chid_devs(dev, devnode, array);
    228  1.10  jmcneill 	}
    229  1.10  jmcneill 
    230   1.1    bouyer 	return array;
    231   1.1    bouyer }
    232  1.14  jmcneill 
    233  1.16  jmcneill #if NIIC > 0
    234  1.14  jmcneill static ACPI_STATUS
    235  1.14  jmcneill acpi_i2c_gsb_init(ACPI_HANDLE region_hdl, UINT32 function,
    236  1.14  jmcneill     void *handler_ctx, void **region_ctx)
    237  1.14  jmcneill {
    238  1.14  jmcneill 	if (function == ACPI_REGION_DEACTIVATE) {
    239  1.14  jmcneill 		*region_ctx = NULL;
    240  1.14  jmcneill 	} else {
    241  1.14  jmcneill 		*region_ctx = region_hdl;
    242  1.14  jmcneill 	}
    243  1.14  jmcneill 	return AE_OK;
    244  1.14  jmcneill }
    245  1.14  jmcneill 
    246  1.14  jmcneill static ACPI_STATUS
    247  1.14  jmcneill acpi_i2c_gsb_handler(UINT32 function, ACPI_PHYSICAL_ADDRESS address,
    248  1.14  jmcneill     UINT32 bit_width, UINT64 *value, void *handler_ctx,
    249  1.14  jmcneill     void *region_ctx)
    250  1.14  jmcneill {
    251  1.14  jmcneill 	ACPI_OPERAND_OBJECT *region_obj = region_ctx;
    252  1.14  jmcneill 	struct acpi_i2c_address_space_context *context = handler_ctx;
    253  1.17  jmcneill 	UINT8 *buf = ACPI_CAST_PTR(uint8_t, value);
    254  1.14  jmcneill 	ACPI_PHYSICAL_ADDRESS base_address;
    255  1.14  jmcneill 	ACPI_RESOURCE *res;
    256  1.14  jmcneill 	ACPI_STATUS rv;
    257  1.17  jmcneill 	ACPI_CONNECTION_INFO *conn_info = &context->conn_info;
    258  1.14  jmcneill 	i2c_tag_t tag = context->tag;
    259  1.17  jmcneill 	i2c_addr_t i2c_addr;
    260  1.14  jmcneill 	i2c_op_t op;
    261  1.14  jmcneill 	union {
    262  1.14  jmcneill 		uint8_t cmd8;
    263  1.14  jmcneill 		uint16_t cmd16;
    264  1.17  jmcneill 		uint32_t cmd32;
    265  1.14  jmcneill 	} cmd;
    266  1.17  jmcneill 	size_t buflen;
    267  1.14  jmcneill 	size_t cmdlen;
    268  1.17  jmcneill 	bool do_xfer = true;
    269  1.14  jmcneill 
    270  1.14  jmcneill 	if (region_obj->Region.Type != ACPI_TYPE_REGION) {
    271  1.14  jmcneill 		return AE_OK;
    272  1.14  jmcneill 	}
    273  1.14  jmcneill 
    274  1.14  jmcneill 	base_address = region_obj->Region.Address;
    275  1.17  jmcneill 	KASSERT(region_obj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS);
    276  1.14  jmcneill 
    277  1.14  jmcneill 	rv = AcpiBufferToResource(conn_info->Connection, conn_info->Length,
    278  1.14  jmcneill 	    &res);
    279  1.14  jmcneill 	if (ACPI_FAILURE(rv)) {
    280  1.14  jmcneill 		return rv;
    281  1.14  jmcneill 	}
    282  1.14  jmcneill 	if (res->Type != ACPI_RESOURCE_TYPE_SERIAL_BUS ||
    283  1.14  jmcneill 	    res->Data.CommonSerialBus.Type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
    284  1.14  jmcneill 		return AE_TYPE;
    285  1.14  jmcneill 	}
    286  1.14  jmcneill 
    287  1.17  jmcneill 	i2c_addr = res->Data.I2cSerialBus.SlaveAddress;
    288  1.14  jmcneill 	if ((function & ACPI_IO_MASK) != 0) {
    289  1.14  jmcneill 		op = I2C_OP_WRITE_WITH_STOP;
    290  1.14  jmcneill 	} else {
    291  1.14  jmcneill 		op = I2C_OP_READ_WITH_STOP;
    292  1.14  jmcneill 	}
    293  1.14  jmcneill 
    294  1.14  jmcneill #ifdef ACPI_I2C_DEBUG
    295  1.17  jmcneill 	UINT32 length;
    296  1.17  jmcneill 	rv = AcpiExGetProtocolBufferLength(function >> 16, &length);
    297  1.17  jmcneill 	if (ACPI_FAILURE(rv)) {
    298  1.17  jmcneill 		printf("%s AcpiExGetProtocolBufferLength failed: %s\n",
    299  1.17  jmcneill 		    __func__, AcpiFormatException(rv));
    300  1.17  jmcneill 		length = UINT32_MAX;
    301  1.17  jmcneill 	}
    302  1.14  jmcneill 	printf("%s %s: %s Attr %X Addr %.4X BaseAddr %.4X Length %.2X BitWidth %X BufLen %X",
    303  1.17  jmcneill 	       __func__, AcpiUtGetRegionName(region_obj->Region.SpaceId),
    304  1.14  jmcneill 	       (function & ACPI_IO_MASK) ? "Write" : "Read ",
    305  1.14  jmcneill 	       (UINT32) (function >> 16),
    306  1.14  jmcneill 	       (UINT32) address, (UINT32) base_address,
    307  1.17  jmcneill 	       length, bit_width, buf[1]);
    308  1.14  jmcneill 	printf(" [AccessLength %.2X Connection %p]\n",
    309  1.14  jmcneill 	       conn_info->AccessLength, conn_info->Connection);
    310  1.14  jmcneill #endif
    311  1.14  jmcneill 
    312  1.17  jmcneill 	switch ((UINT32)(function >> 16)) {
    313  1.17  jmcneill 	case AML_FIELD_ATTRIB_QUICK:
    314  1.17  jmcneill 		cmdlen = 0;
    315  1.17  jmcneill 		buflen = 0;
    316  1.17  jmcneill 		break;
    317  1.17  jmcneill 	case AML_FIELD_ATTRIB_SEND_RECEIVE:
    318  1.17  jmcneill 		cmdlen = 0;
    319  1.17  jmcneill 		buflen = 1;
    320  1.17  jmcneill 		break;
    321  1.17  jmcneill 	case AML_FIELD_ATTRIB_BYTE:
    322  1.17  jmcneill 		cmdlen = bit_width / NBBY;
    323  1.17  jmcneill 		buflen = 1;
    324  1.17  jmcneill 		break;
    325  1.17  jmcneill 	case AML_FIELD_ATTRIB_WORD:
    326  1.17  jmcneill 		cmdlen = bit_width / NBBY;
    327  1.17  jmcneill 		buflen = 2;
    328  1.17  jmcneill 		break;
    329  1.17  jmcneill 	case AML_FIELD_ATTRIB_BYTES:
    330  1.17  jmcneill 		cmdlen = bit_width / NBBY;
    331  1.17  jmcneill 		buflen = buf[1];
    332  1.17  jmcneill 		break;
    333  1.17  jmcneill 	case AML_FIELD_ATTRIB_BLOCK:
    334  1.17  jmcneill 		cmdlen = bit_width / NBBY;
    335  1.17  jmcneill 		buflen = buf[1];
    336  1.17  jmcneill 		op |= I2C_OPMASK_BLKMODE;
    337  1.17  jmcneill 		break;
    338  1.17  jmcneill 	case AML_FIELD_ATTRIB_RAW_BYTES:
    339  1.17  jmcneill 	case AML_FIELD_ATTRIB_RAW_PROCESS_BYTES:
    340  1.17  jmcneill 	case AML_FIELD_ATTRIB_PROCESS_CALL:
    341  1.17  jmcneill 	default:
    342  1.17  jmcneill 		cmdlen = 0;
    343  1.17  jmcneill 		do_xfer = false;
    344  1.17  jmcneill #ifdef ACPI_I2C_DEBUG
    345  1.17  jmcneill 		printf("field attrib 0x%x not supported\n",
    346  1.17  jmcneill 		    (UINT32)(function >> 16));
    347  1.17  jmcneill #endif
    348  1.17  jmcneill 		break;
    349  1.17  jmcneill 	}
    350  1.17  jmcneill 
    351  1.17  jmcneill 	switch (cmdlen) {
    352  1.17  jmcneill 	case 0:
    353  1.17  jmcneill 	case 1:
    354  1.14  jmcneill 		cmd.cmd8 = (uint8_t)(base_address + address);
    355  1.17  jmcneill 		break;
    356  1.17  jmcneill 	case 2:
    357  1.14  jmcneill 		cmd.cmd16 = (uint16_t)(base_address + address);
    358  1.17  jmcneill 		break;
    359  1.17  jmcneill 	case 4:
    360  1.17  jmcneill 		cmd.cmd32 = (uint32_t)(base_address + address);
    361  1.17  jmcneill 		break;
    362  1.17  jmcneill 	default:
    363  1.17  jmcneill 		do_xfer = false;
    364  1.17  jmcneill #ifdef ACPI_I2C_DEBUG
    365  1.17  jmcneill 		printf("cmdlen %zu not supported\n", cmdlen);
    366  1.17  jmcneill #endif
    367  1.17  jmcneill 		break;
    368  1.14  jmcneill 	}
    369  1.17  jmcneill 
    370  1.17  jmcneill 	if (!do_xfer) {
    371  1.17  jmcneill 		buf[0] = EINVAL;
    372  1.14  jmcneill 	} else {
    373  1.14  jmcneill 		const int flags = I2C_F_POLL;
    374  1.14  jmcneill 		iic_acquire_bus(tag, flags);
    375  1.17  jmcneill 		buf[0] = iic_exec(tag, op, i2c_addr,
    376  1.17  jmcneill 				  &cmd, cmdlen, &buf[2], buflen, flags);
    377  1.14  jmcneill 		iic_release_bus(tag, flags);
    378  1.17  jmcneill 		if (buf[0] == 0) {
    379  1.17  jmcneill 			buf[1] = buflen;
    380  1.14  jmcneill 		}
    381  1.14  jmcneill #ifdef ACPI_I2C_DEBUG
    382  1.17  jmcneill 		printf("%s iic_exec op %u addr 0x%x len %zu/%zu returned %d\n",
    383  1.17  jmcneill 		    __func__, op, res->Data.I2cSerialBus.SlaveAddress, cmdlen,
    384  1.17  jmcneill 		    buflen, buf[0]);
    385  1.14  jmcneill #endif
    386  1.14  jmcneill 	}
    387  1.14  jmcneill 
    388  1.14  jmcneill 	ACPI_FREE(res);
    389  1.14  jmcneill 
    390  1.14  jmcneill 	return AE_OK;
    391  1.14  jmcneill }
    392  1.16  jmcneill #endif
    393  1.14  jmcneill 
    394  1.21   thorpej void
    395  1.21   thorpej acpi_i2c_register(device_t dev, i2c_tag_t tag)
    396  1.14  jmcneill {
    397  1.16  jmcneill #if NIIC > 0
    398  1.21   thorpej 	ACPI_HANDLE hdl = devhandle_to_acpi(device_handle(dev));
    399  1.14  jmcneill 	struct acpi_i2c_address_space_context *context;
    400  1.14  jmcneill 	ACPI_STATUS rv;
    401  1.14  jmcneill 
    402  1.14  jmcneill 	context = kmem_zalloc(sizeof(*context), KM_SLEEP);
    403  1.14  jmcneill 	context->tag = tag;
    404  1.14  jmcneill 
    405  1.21   thorpej 	rv = AcpiInstallAddressSpaceHandler(hdl,
    406  1.14  jmcneill 	    ACPI_ADR_SPACE_GSBUS, acpi_i2c_gsb_handler, acpi_i2c_gsb_init,
    407  1.14  jmcneill 	    context);
    408  1.14  jmcneill 	if (ACPI_FAILURE(rv)) {
    409  1.14  jmcneill 		aprint_error_dev(dev,
    410  1.14  jmcneill 		    "couldn't install address space handler: %s",
    411  1.14  jmcneill 		    AcpiFormatException(rv));
    412  1.14  jmcneill 	}
    413  1.16  jmcneill #endif
    414  1.14  jmcneill }
    415