Home | History | Annotate | Line # | Download | only in i2c
ihidev.c revision 1.13
      1 /* $NetBSD: ihidev.c,v 1.13 2021/01/17 21:47:50 thorpej Exp $ */
      2 /* $OpenBSD ihidev.c,v 1.13 2017/04/08 02:57:23 deraadt Exp $ */
      3 
      4 /*-
      5  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Manuel Bouyer.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Copyright (c) 2015, 2016 joshua stein <jcs (at) openbsd.org>
     35  *
     36  * Permission to use, copy, modify, and distribute this software for any
     37  * purpose with or without fee is hereby granted, provided that the above
     38  * copyright notice and this permission notice appear in all copies.
     39  *
     40  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     41  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     42  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     43  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     44  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     45  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     46  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     47  */
     48 
     49 /*
     50  * HID-over-i2c driver
     51  *
     52  * https://msdn.microsoft.com/en-us/library/windows/hardware/dn642101%28v=vs.85%29.aspx
     53  *
     54  */
     55 
     56 #include <sys/cdefs.h>
     57 __KERNEL_RCSID(0, "$NetBSD: ihidev.c,v 1.13 2021/01/17 21:47:50 thorpej Exp $");
     58 
     59 #include <sys/param.h>
     60 #include <sys/systm.h>
     61 #include <sys/device.h>
     62 #include <sys/kmem.h>
     63 
     64 
     65 #include <dev/i2c/i2cvar.h>
     66 #include <dev/i2c/ihidev.h>
     67 
     68 #include <dev/hid/hid.h>
     69 
     70 #if defined(__i386__) || defined(__amd64__)
     71 #  include "acpica.h"
     72 #endif
     73 #if NACPICA > 0
     74 #include <dev/acpi/acpivar.h>
     75 #include <dev/acpi/acpi_intr.h>
     76 #endif
     77 
     78 #include "locators.h"
     79 
     80 /* #define IHIDEV_DEBUG */
     81 
     82 #ifdef IHIDEV_DEBUG
     83 #define DPRINTF(x) printf x
     84 #else
     85 #define DPRINTF(x)
     86 #endif
     87 
     88 /* 7.2 */
     89 enum {
     90 	I2C_HID_CMD_DESCR	= 0x0,
     91 	I2C_HID_CMD_RESET	= 0x1,
     92 	I2C_HID_CMD_GET_REPORT	= 0x2,
     93 	I2C_HID_CMD_SET_REPORT	= 0x3,
     94 	I2C_HID_CMD_GET_IDLE	= 0x4,
     95 	I2C_HID_CMD_SET_IDLE	= 0x5,
     96 	I2C_HID_CMD_GET_PROTO	= 0x6,
     97 	I2C_HID_CMD_SET_PROTO	= 0x7,
     98 	I2C_HID_CMD_SET_POWER	= 0x8,
     99 
    100 	/* pseudo commands */
    101 	I2C_HID_REPORT_DESCR	= 0x100,
    102 };
    103 
    104 static int I2C_HID_POWER_ON	= 0x0;
    105 static int I2C_HID_POWER_OFF	= 0x1;
    106 
    107 static int	ihidev_match(device_t, cfdata_t, void *);
    108 static void	ihidev_attach(device_t, device_t, void *);
    109 static int	ihidev_detach(device_t, int);
    110 CFATTACH_DECL_NEW(ihidev, sizeof(struct ihidev_softc),
    111     ihidev_match, ihidev_attach, ihidev_detach, NULL);
    112 
    113 static bool	ihiddev_intr_init(struct ihidev_softc *);
    114 static void	ihiddev_intr_fini(struct ihidev_softc *);
    115 
    116 static bool	ihidev_suspend(device_t, const pmf_qual_t *);
    117 static bool	ihidev_resume(device_t, const pmf_qual_t *);
    118 static int	ihidev_hid_command(struct ihidev_softc *, int, void *, bool);
    119 static int	ihidev_intr(void *);
    120 static void	ihidev_softintr(void *);
    121 static int	ihidev_reset(struct ihidev_softc *, bool);
    122 static int	ihidev_hid_desc_parse(struct ihidev_softc *);
    123 
    124 static int	ihidev_maxrepid(void *, int);
    125 static int	ihidev_print(void *, const char *);
    126 static int	ihidev_submatch(device_t, cfdata_t, const int *, void *);
    127 
    128 static const struct device_compatible_entry compat_data[] = {
    129 	{ .compat = "hid-over-i2c" },
    130 
    131 	{ 0 }
    132 };
    133 
    134 static int
    135 ihidev_match(device_t parent, cfdata_t match, void *aux)
    136 {
    137 	struct i2c_attach_args * const ia = aux;
    138 	int match_result;
    139 
    140 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    141 		return I2C_MATCH_DIRECT_COMPATIBLE;
    142 
    143 	return 0;
    144 }
    145 
    146 static void
    147 ihidev_attach(device_t parent, device_t self, void *aux)
    148 {
    149 	struct ihidev_softc *sc = device_private(self);
    150 	struct i2c_attach_args *ia = aux;
    151 	struct ihidev_attach_arg iha;
    152 	device_t dev;
    153 	int repid, repsz;
    154 	int isize;
    155 	uint32_t v;
    156 	int locs[IHIDBUSCF_NLOCS];
    157 
    158 
    159 	sc->sc_dev = self;
    160 	sc->sc_tag = ia->ia_tag;
    161 	sc->sc_addr = ia->ia_addr;
    162 	sc->sc_phandle = ia->ia_cookie;
    163 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
    164 
    165 	if (!prop_dictionary_get_uint32(ia->ia_prop, "hid-descr-addr", &v)) {
    166 		aprint_error(": no hid-descr-addr value\n");
    167 		return;
    168 	}
    169 
    170 	sc->sc_hid_desc_addr = v;
    171 
    172 	if (ihidev_hid_command(sc, I2C_HID_CMD_DESCR, NULL, false) ||
    173 	    ihidev_hid_desc_parse(sc)) {
    174 		aprint_error(": failed fetching initial HID descriptor\n");
    175 		return;
    176 	}
    177 
    178 	aprint_naive("\n");
    179 	aprint_normal(": vendor 0x%x product 0x%x, %s\n",
    180 	    le16toh(sc->hid_desc.wVendorID), le16toh(sc->hid_desc.wProductID),
    181 	    ia->ia_name);
    182 
    183 	sc->sc_nrepid = ihidev_maxrepid(sc->sc_report, sc->sc_reportlen);
    184 	if (sc->sc_nrepid < 0)
    185 		return;
    186 
    187 	aprint_normal_dev(self, "%d report id%s\n", sc->sc_nrepid,
    188 	    sc->sc_nrepid > 1 ? "s" : "");
    189 
    190 	sc->sc_nrepid++;
    191 	sc->sc_subdevs = kmem_zalloc(sc->sc_nrepid * sizeof(struct ihidev *),
    192 	    KM_SLEEP);
    193 
    194 	/* find largest report size and allocate memory for input buffer */
    195 	sc->sc_isize = le16toh(sc->hid_desc.wMaxInputLength);
    196 	for (repid = 0; repid < sc->sc_nrepid; repid++) {
    197 		repsz = hid_report_size(sc->sc_report, sc->sc_reportlen,
    198 		    hid_input, repid);
    199 
    200 		isize = repsz + 2; /* two bytes for the length */
    201 		isize += (sc->sc_nrepid != 1); /* one byte for the report ID */
    202 		if (isize > sc->sc_isize)
    203 			sc->sc_isize = isize;
    204 
    205 		DPRINTF(("%s: repid %d size %d\n", sc->sc_dev.dv_xname, repid,
    206 		    repsz));
    207 	}
    208 	sc->sc_ibuf = kmem_zalloc(sc->sc_isize, KM_SLEEP);
    209 	if (! ihiddev_intr_init(sc)) {
    210 		return;
    211 	}
    212 
    213 	iha.iaa = ia;
    214 	iha.parent = sc;
    215 
    216 	/* Look for a driver claiming all report IDs first. */
    217 	iha.reportid = IHIDEV_CLAIM_ALLREPORTID;
    218 	locs[IHIDBUSCF_REPORTID] = IHIDEV_CLAIM_ALLREPORTID;
    219 	dev = config_found_sm_loc(self, "ihidbus", locs, &iha,
    220 	    ihidev_print, ihidev_submatch);
    221 	if (dev != NULL) {
    222 		for (repid = 0; repid < sc->sc_nrepid; repid++)
    223 			sc->sc_subdevs[repid] = device_private(dev);
    224 		return;
    225 	}
    226 
    227 	for (repid = 0; repid < sc->sc_nrepid; repid++) {
    228 		if (hid_report_size(sc->sc_report, sc->sc_reportlen, hid_input,
    229 		    repid) == 0 &&
    230 		    hid_report_size(sc->sc_report, sc->sc_reportlen,
    231 		    hid_output, repid) == 0 &&
    232 		    hid_report_size(sc->sc_report, sc->sc_reportlen,
    233 		    hid_feature, repid) == 0)
    234 			continue;
    235 
    236 		iha.reportid = repid;
    237 		locs[IHIDBUSCF_REPORTID] = repid;
    238 		dev = config_found_sm_loc(self, "ihidbus", locs,
    239 		    &iha, ihidev_print, ihidev_submatch);
    240 		sc->sc_subdevs[repid] = device_private(dev);
    241 	}
    242 
    243 	/* power down until we're opened */
    244 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, &I2C_HID_POWER_OFF, false)) {
    245 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
    246 		return;
    247 	}
    248 	if (!pmf_device_register(self, ihidev_suspend, ihidev_resume))
    249 		aprint_error_dev(self, "couldn't establish power handler\n");
    250 }
    251 
    252 static int
    253 ihidev_detach(device_t self, int flags)
    254 {
    255 	struct ihidev_softc *sc = device_private(self);
    256 
    257 	mutex_enter(&sc->sc_intr_lock);
    258 	ihiddev_intr_fini(sc);
    259 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    260 	    &I2C_HID_POWER_OFF, true))
    261 	aprint_error_dev(sc->sc_dev, "failed to power down\n");
    262 	mutex_exit(&sc->sc_intr_lock);
    263 	if (sc->sc_ibuf != NULL) {
    264 		kmem_free(sc->sc_ibuf, sc->sc_isize);
    265 		sc->sc_ibuf = NULL;
    266 	}
    267 
    268 	if (sc->sc_report != NULL)
    269 		kmem_free(sc->sc_report, sc->sc_reportlen);
    270 
    271 	pmf_device_deregister(self);
    272 	return (0);
    273 }
    274 
    275 static bool
    276 ihidev_suspend(device_t self, const pmf_qual_t *q)
    277 {
    278 	struct ihidev_softc *sc = device_private(self);
    279 
    280 	mutex_enter(&sc->sc_intr_lock);
    281 	if (sc->sc_refcnt > 0) {
    282 		printf("ihidev power off\n");
    283 		if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    284 		    &I2C_HID_POWER_OFF, true))
    285 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
    286 	}
    287 	mutex_exit(&sc->sc_intr_lock);
    288 	return true;
    289 }
    290 
    291 static bool
    292 ihidev_resume(device_t self, const pmf_qual_t *q)
    293 {
    294 	struct ihidev_softc *sc = device_private(self);
    295 
    296 	mutex_enter(&sc->sc_intr_lock);
    297 	if (sc->sc_refcnt > 0) {
    298 		printf("ihidev power reset\n");
    299 		ihidev_reset(sc, true);
    300 	}
    301 	mutex_exit(&sc->sc_intr_lock);
    302 	return true;
    303 }
    304 
    305 static int
    306 ihidev_hid_command(struct ihidev_softc *sc, int hidcmd, void *arg, bool poll)
    307 {
    308 	int i, res = 1;
    309 	int flags = poll ? I2C_F_POLL : 0;
    310 
    311 	iic_acquire_bus(sc->sc_tag, flags);
    312 
    313 	switch (hidcmd) {
    314 	case I2C_HID_CMD_DESCR: {
    315 		/*
    316 		 * 5.2.2 - HID Descriptor Retrieval
    317 		 * register is passed from the controller
    318 		 */
    319 		uint8_t cmd[] = {
    320 			htole16(sc->sc_hid_desc_addr) & 0xff,
    321 			htole16(sc->sc_hid_desc_addr) >> 8,
    322 		};
    323 
    324 		DPRINTF(("%s: HID command I2C_HID_CMD_DESCR at 0x%x\n",
    325 		    sc->sc_dev.dv_xname, htole16(sc->sc_hid_desc_addr)));
    326 
    327 		/* 20 00 */
    328 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    329 		    &cmd, sizeof(cmd), &sc->hid_desc_buf,
    330 		    sizeof(struct i2c_hid_desc), flags);
    331 
    332 		DPRINTF(("%s: HID descriptor:", sc->sc_dev.dv_xname));
    333 		for (i = 0; i < sizeof(struct i2c_hid_desc); i++)
    334 			DPRINTF((" %.2x", sc->hid_desc_buf[i]));
    335 		DPRINTF(("\n"));
    336 
    337 		break;
    338 	}
    339 	case I2C_HID_CMD_RESET: {
    340 		uint8_t cmd[] = {
    341 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    342 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    343 			0,
    344 			I2C_HID_CMD_RESET,
    345 		};
    346 
    347 		DPRINTF(("%s: HID command I2C_HID_CMD_RESET\n",
    348 		    sc->sc_dev.dv_xname));
    349 
    350 		/* 22 00 00 01 */
    351 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    352 		    &cmd, sizeof(cmd), NULL, 0, flags);
    353 
    354 		break;
    355 	}
    356 	case I2C_HID_CMD_GET_REPORT: {
    357 		struct i2c_hid_report_request *rreq =
    358 		    (struct i2c_hid_report_request *)arg;
    359 
    360 		uint8_t cmd[] = {
    361 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    362 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    363 			0,
    364 			I2C_HID_CMD_GET_REPORT,
    365 			0, 0, 0,
    366 		};
    367 		int cmdlen = 7;
    368 		int dataoff = 4;
    369 		int report_id = rreq->id;
    370 		int report_id_len = 1;
    371 		int report_len = rreq->len + 2;
    372 		int d;
    373 		uint8_t *tmprep;
    374 
    375 		DPRINTF(("%s: HID command I2C_HID_CMD_GET_REPORT %d "
    376 		    "(type %d, len %d)\n", sc->sc_dev.dv_xname, report_id,
    377 		    rreq->type, rreq->len));
    378 
    379 		/*
    380 		 * 7.2.2.4 - "The protocol is optimized for Report < 15.  If a
    381 		 * report ID >= 15 is necessary, then the Report ID in the Low
    382 		 * Byte must be set to 1111 and a Third Byte is appended to the
    383 		 * protocol.  This Third Byte contains the entire/actual report
    384 		 * ID."
    385 		 */
    386 		if (report_id >= 15) {
    387 			cmd[dataoff++] = report_id;
    388 			report_id = 15;
    389 			report_id_len = 2;
    390 		} else
    391 			cmdlen--;
    392 
    393 		cmd[2] = report_id | rreq->type << 4;
    394 
    395 		cmd[dataoff++] = sc->hid_desc.wDataRegister & 0xff;
    396 		cmd[dataoff] = sc->hid_desc.wDataRegister >> 8;
    397 
    398 		/*
    399 		 * 7.2.2.2 - Response will be a 2-byte length value, the report
    400 		 * id with length determined above, and then the report.
    401 		 * Allocate rreq->len + 2 + 2 bytes, read into that temporary
    402 		 * buffer, and then copy only the report back out to
    403 		 * rreq->data.
    404 		 */
    405 		report_len += report_id_len;
    406 		tmprep = kmem_zalloc(report_len, KM_NOSLEEP);
    407 
    408 		/* type 3 id 8: 22 00 38 02 23 00 */
    409 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    410 		    &cmd, cmdlen, tmprep, report_len, flags);
    411 
    412 		d = tmprep[0] | tmprep[1] << 8;
    413 		if (d != report_len) {
    414 			DPRINTF(("%s: response size %d != expected length %d\n",
    415 			    sc->sc_dev.dv_xname, d, report_len));
    416 		}
    417 
    418 		if (report_id_len == 2)
    419 			d = tmprep[2] | tmprep[3] << 8;
    420 		else
    421 			d = tmprep[2];
    422 
    423 		if (d != rreq->id) {
    424 			DPRINTF(("%s: response report id %d != %d\n",
    425 			    sc->sc_dev.dv_xname, d, rreq->id));
    426 			iic_release_bus(sc->sc_tag, 0);
    427 			kmem_free(tmprep, report_len);
    428 			return (1);
    429 		}
    430 
    431 		DPRINTF(("%s: response:", sc->sc_dev.dv_xname));
    432 		for (i = 0; i < report_len; i++)
    433 			DPRINTF((" %.2x", tmprep[i]));
    434 		DPRINTF(("\n"));
    435 
    436 		memcpy(rreq->data, tmprep + 2 + report_id_len, rreq->len);
    437 		kmem_free(tmprep, report_len);
    438 
    439 		break;
    440 	}
    441 	case I2C_HID_CMD_SET_REPORT: {
    442 		struct i2c_hid_report_request *rreq =
    443 		    (struct i2c_hid_report_request *)arg;
    444 
    445 		uint8_t cmd[] = {
    446 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    447 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    448 			0,
    449 			I2C_HID_CMD_SET_REPORT,
    450 			0, 0, 0, 0, 0, 0,
    451 		};
    452 		int cmdlen = 10;
    453 		int report_id = rreq->id;
    454 		int report_len = 2 + (report_id ? 1 : 0) + rreq->len;
    455 		int dataoff;
    456 		uint8_t *finalcmd;
    457 
    458 		DPRINTF(("%s: HID command I2C_HID_CMD_SET_REPORT %d "
    459 		    "(type %d, len %d):", sc->sc_dev.dv_xname, report_id,
    460 		    rreq->type, rreq->len));
    461 		for (i = 0; i < rreq->len; i++)
    462 			DPRINTF((" %.2x", ((uint8_t *)rreq->data)[i]));
    463 		DPRINTF(("\n"));
    464 
    465 		/*
    466 		 * 7.2.2.4 - "The protocol is optimized for Report < 15.  If a
    467 		 * report ID >= 15 is necessary, then the Report ID in the Low
    468 		 * Byte must be set to 1111 and a Third Byte is appended to the
    469 		 * protocol.  This Third Byte contains the entire/actual report
    470 		 * ID."
    471 		 */
    472 		dataoff = 4;
    473 		if (report_id >= 15) {
    474 			cmd[dataoff++] = report_id;
    475 			report_id = 15;
    476 		} else
    477 			cmdlen--;
    478 
    479 		cmd[2] = report_id | rreq->type << 4;
    480 
    481 		if (rreq->type == I2C_HID_REPORT_TYPE_FEATURE) {
    482 			cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister)
    483 			    & 0xff;
    484 			cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister)
    485 			    >> 8;
    486 		} else {
    487 			cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister)
    488 			    & 0xff;
    489 			cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister)
    490 			    >> 8;
    491 		}
    492 
    493 		cmd[dataoff++] = report_len & 0xff;
    494 		cmd[dataoff++] = report_len >> 8;
    495 		cmd[dataoff] = rreq->id;
    496 
    497 		finalcmd = kmem_zalloc(cmdlen + rreq->len, KM_NOSLEEP);
    498 
    499 		memcpy(finalcmd, cmd, cmdlen);
    500 		memcpy(finalcmd + cmdlen, rreq->data, rreq->len);
    501 
    502 		/* type 3 id 4: 22 00 34 03 23 00 04 00 04 03 */
    503 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    504 		    finalcmd, cmdlen + rreq->len, NULL, 0, flags);
    505 		kmem_free(finalcmd, cmdlen + rreq->len);
    506 
    507  		break;
    508  	}
    509 
    510 	case I2C_HID_CMD_SET_POWER: {
    511 		int power = *(int *)arg;
    512 		uint8_t cmd[] = {
    513 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    514 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    515 			power,
    516 			I2C_HID_CMD_SET_POWER,
    517 		};
    518 
    519 		DPRINTF(("%s: HID command I2C_HID_CMD_SET_POWER(%d)\n",
    520 		    sc->sc_dev.dv_xname, power));
    521 
    522 		/* 22 00 00 08 */
    523 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    524 		    &cmd, sizeof(cmd), NULL, 0, flags);
    525 
    526 		break;
    527 	}
    528 	case I2C_HID_REPORT_DESCR: {
    529 		uint8_t cmd[] = {
    530 			htole16(sc->hid_desc.wReportDescRegister) & 0xff,
    531 			htole16(sc->hid_desc.wReportDescRegister) >> 8,
    532 		};
    533 
    534 		DPRINTF(("%s: HID command I2C_HID_REPORT_DESCR at 0x%x with "
    535 		    "size %d\n", sc->sc_dev.dv_xname, cmd[0],
    536 		    sc->sc_reportlen));
    537 
    538 		/* 20 00 */
    539 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    540 		    &cmd, sizeof(cmd), sc->sc_report, sc->sc_reportlen, flags);
    541 
    542 		DPRINTF(("%s: HID report descriptor:", sc->sc_dev.dv_xname));
    543 		for (i = 0; i < sc->sc_reportlen; i++)
    544 			DPRINTF((" %.2x", sc->sc_report[i]));
    545 		DPRINTF(("\n"));
    546 
    547 		break;
    548 	}
    549 	default:
    550 		aprint_error_dev(sc->sc_dev, "unknown command %d\n",
    551 		    hidcmd);
    552 	}
    553 
    554 	iic_release_bus(sc->sc_tag, flags);
    555 
    556 	return (res);
    557 }
    558 
    559 static int
    560 ihidev_reset(struct ihidev_softc *sc, bool poll)
    561 {
    562 	DPRINTF(("%s: resetting\n", sc->sc_dev.dv_xname));
    563 
    564 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    565 	    &I2C_HID_POWER_ON, poll)) {
    566 		aprint_error_dev(sc->sc_dev, "failed to power on\n");
    567 		return (1);
    568 	}
    569 
    570 	DELAY(1000);
    571 
    572 	if (ihidev_hid_command(sc, I2C_HID_CMD_RESET, 0, poll)) {
    573 		aprint_error_dev(sc->sc_dev, "failed to reset hardware\n");
    574 
    575 		ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    576 		    &I2C_HID_POWER_OFF, poll);
    577 
    578 		return (1);
    579 	}
    580 
    581 	DELAY(1000);
    582 
    583 	return (0);
    584 }
    585 
    586 /*
    587  * 5.2.2 - HID Descriptor Retrieval
    588  *
    589  * parse HID Descriptor that has already been read into hid_desc with
    590  * I2C_HID_CMD_DESCR
    591  */
    592 static int
    593 ihidev_hid_desc_parse(struct ihidev_softc *sc)
    594 {
    595 	int retries = 3;
    596 
    597 	/* must be v01.00 */
    598 	if (le16toh(sc->hid_desc.bcdVersion) != 0x0100) {
    599 		aprint_error_dev(sc->sc_dev,
    600 		    "bad HID descriptor bcdVersion (0x%x)\n",
    601 		    le16toh(sc->hid_desc.bcdVersion));
    602 		return (1);
    603 	}
    604 
    605 	/* must be 30 bytes for v1.00 */
    606 	if (le16toh(sc->hid_desc.wHIDDescLength !=
    607 	    sizeof(struct i2c_hid_desc))) {
    608 		aprint_error_dev(sc->sc_dev,
    609 		    "bad HID descriptor size (%d != %zu)\n",
    610 		    le16toh(sc->hid_desc.wHIDDescLength),
    611 		    sizeof(struct i2c_hid_desc));
    612 		return (1);
    613 	}
    614 
    615 	if (le16toh(sc->hid_desc.wReportDescLength) <= 0) {
    616 		aprint_error_dev(sc->sc_dev,
    617 		    "bad HID report descriptor size (%d)\n",
    618 		    le16toh(sc->hid_desc.wReportDescLength));
    619 		return (1);
    620 	}
    621 
    622 	while (retries-- > 0) {
    623 		if (ihidev_reset(sc, false)) {
    624 			if (retries == 0)
    625 				return(1);
    626 
    627 			DELAY(1000);
    628 		}
    629 		else
    630 			break;
    631 	}
    632 
    633 	sc->sc_reportlen = le16toh(sc->hid_desc.wReportDescLength);
    634 	sc->sc_report = kmem_zalloc(sc->sc_reportlen, KM_NOSLEEP);
    635 
    636 	if (ihidev_hid_command(sc, I2C_HID_REPORT_DESCR, 0, false)) {
    637 		aprint_error_dev(sc->sc_dev, "failed fetching HID report\n");
    638 		return (1);
    639 	}
    640 
    641 	return (0);
    642 }
    643 
    644 static bool
    645 ihiddev_intr_init(struct ihidev_softc *sc)
    646 {
    647 #if NACPICA > 0
    648 	ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle;
    649 	struct acpi_resources res;
    650 	ACPI_STATUS rv;
    651 	char buf[100];
    652 
    653 	rv = acpi_resource_parse(sc->sc_dev, hdl, "_CRS", &res,
    654 	    &acpi_resource_parse_ops_quiet);
    655 	if (ACPI_FAILURE(rv)) {
    656 		aprint_error_dev(sc->sc_dev, "can't parse '_CRS'\n");
    657 		return false;
    658 	}
    659 
    660 	const struct acpi_irq * const irq = acpi_res_irq(&res, 0);
    661 	if (irq == NULL) {
    662 		aprint_error_dev(sc->sc_dev, "no IRQ resource\n");
    663 		acpi_resource_cleanup(&res);
    664 		return false;
    665 	}
    666 
    667 	sc->sc_intr_type =
    668 	    irq->ar_type == ACPI_EDGE_SENSITIVE ? IST_EDGE : IST_LEVEL;
    669 
    670 	acpi_resource_cleanup(&res);
    671 
    672 	sc->sc_ih = acpi_intr_establish(sc->sc_dev, sc->sc_phandle, IPL_TTY,
    673 	    false, ihidev_intr, sc, device_xname(sc->sc_dev));
    674 	if (sc->sc_ih == NULL) {
    675 		aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
    676 		return false;
    677 	}
    678 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n",
    679 	    acpi_intr_string(sc->sc_ih, buf, sizeof(buf)));
    680 
    681 	sc->sc_sih = softint_establish(SOFTINT_SERIAL, ihidev_softintr, sc);
    682 	if (sc->sc_sih == NULL) {
    683 		aprint_error_dev(sc->sc_dev,
    684 		    "can't establish soft interrupt\n");
    685 		return false;
    686 	}
    687 
    688 	return true;
    689 #else
    690 	aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
    691 	return false;
    692 #endif
    693 }
    694 
    695 static void
    696 ihiddev_intr_fini(struct ihidev_softc *sc)
    697 {
    698 #if NACPICA > 0
    699 	if (sc->sc_ih != NULL) {
    700 		acpi_intr_disestablish(sc->sc_ih);
    701 	}
    702 	if (sc->sc_sih != NULL) {
    703 		softint_disestablish(sc->sc_sih);
    704 	}
    705 #endif
    706 }
    707 
    708 static void
    709 ihidev_intr_mask(struct ihidev_softc * const sc)
    710 {
    711 
    712 	if (sc->sc_intr_type == IST_LEVEL) {
    713 #if NACPICA > 0
    714 		acpi_intr_mask(sc->sc_ih);
    715 #endif
    716 	}
    717 }
    718 
    719 static void
    720 ihidev_intr_unmask(struct ihidev_softc * const sc)
    721 {
    722 
    723 	if (sc->sc_intr_type == IST_LEVEL) {
    724 #if NACPICA > 0
    725 		acpi_intr_unmask(sc->sc_ih);
    726 #endif
    727 	}
    728 }
    729 
    730 static int
    731 ihidev_intr(void *arg)
    732 {
    733 	struct ihidev_softc * const sc = arg;
    734 
    735 	mutex_enter(&sc->sc_intr_lock);
    736 
    737 	/*
    738 	 * Schedule our soft interrupt handler.  If we're using a level-
    739 	 * triggered interrupt, we have to mask it off while we wait
    740 	 * for service.
    741 	 */
    742 	softint_schedule(sc->sc_sih);
    743 	ihidev_intr_mask(sc);
    744 
    745 	mutex_exit(&sc->sc_intr_lock);
    746 
    747 	return 1;
    748 }
    749 
    750 static void
    751 ihidev_softintr(void *arg)
    752 {
    753 	struct ihidev_softc * const sc = arg;
    754 	struct ihidev *scd;
    755 	u_int psize;
    756 	int res, i;
    757 	u_char *p;
    758 	u_int rep = 0;
    759 
    760 	mutex_enter(&sc->sc_intr_lock);
    761 	iic_acquire_bus(sc->sc_tag, 0);
    762 	res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, NULL, 0,
    763 	    sc->sc_ibuf, sc->sc_isize, 0);
    764 	iic_release_bus(sc->sc_tag, 0);
    765 	mutex_exit(&sc->sc_intr_lock);
    766 
    767 	if (res != 0)
    768 		goto out;
    769 
    770 	/*
    771 	 * 6.1.1 - First two bytes are the packet length, which must be less
    772 	 * than or equal to wMaxInputLength
    773 	 */
    774 	psize = sc->sc_ibuf[0] | sc->sc_ibuf[1] << 8;
    775 	if (!psize || psize > sc->sc_isize) {
    776 		DPRINTF(("%s: %s: invalid packet size (%d vs. %d)\n",
    777 		    sc->sc_dev.dv_xname, __func__, psize, sc->sc_isize));
    778 		goto out;
    779 	}
    780 
    781 	/* 3rd byte is the report id */
    782 	p = sc->sc_ibuf + 2;
    783 	psize -= 2;
    784 	if (sc->sc_nrepid != 1)
    785 		rep = *p++, psize--;
    786 
    787 	if (rep >= sc->sc_nrepid) {
    788 		aprint_error_dev(sc->sc_dev, "%s: bad report id %d\n",
    789 		    __func__, rep);
    790 		goto out;
    791 	}
    792 
    793 	DPRINTF(("%s: %s: hid input (rep %d):", sc->sc_dev.dv_xname,
    794 	    __func__, rep));
    795 	for (i = 0; i < sc->sc_isize; i++)
    796 		DPRINTF((" %.2x", sc->sc_ibuf[i]));
    797 	DPRINTF(("\n"));
    798 
    799 	scd = sc->sc_subdevs[rep];
    800 	if (scd == NULL || !(scd->sc_state & IHIDEV_OPEN))
    801 		goto out;
    802 
    803 	scd->sc_intr(scd, p, psize);
    804 
    805  out:
    806 	/*
    807 	 * If our interrupt is level-triggered, re-enable it now.
    808 	 */
    809 	ihidev_intr_unmask(sc);
    810 }
    811 
    812 static int
    813 ihidev_maxrepid(void *buf, int len)
    814 {
    815 	struct hid_data *d;
    816 	struct hid_item h;
    817 	int maxid;
    818 
    819 	maxid = -1;
    820 	h.report_ID = 0;
    821 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    822 		if ((int)h.report_ID > maxid)
    823 			maxid = h.report_ID;
    824 	hid_end_parse(d);
    825 
    826 	return (maxid);
    827 }
    828 
    829 static int
    830 ihidev_print(void *aux, const char *pnp)
    831 {
    832 	struct ihidev_attach_arg *iha = aux;
    833 
    834 	if (iha->reportid == IHIDEV_CLAIM_ALLREPORTID)
    835 		return (QUIET);
    836 
    837 	if (pnp)
    838 		aprint_normal("hid at %s", pnp);
    839 
    840 	if (iha->reportid != 0)
    841 		aprint_normal(" reportid %d", iha->reportid);
    842 
    843 	return (UNCONF);
    844 }
    845 
    846 static int
    847 ihidev_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    848 {
    849 	struct ihidev_attach_arg *iha = aux;
    850 
    851 	if (cf->ihidevcf_reportid != IHIDEV_UNK_REPORTID &&
    852 	    cf->ihidevcf_reportid != iha->reportid)
    853 		return (0);
    854 
    855 	return config_match(parent, cf, aux);
    856 }
    857 
    858 int
    859 ihidev_open(struct ihidev *scd)
    860 {
    861 	struct ihidev_softc *sc = scd->sc_parent;
    862 
    863 	DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname,
    864 	    __func__, scd->sc_state, sc->sc_refcnt));
    865 
    866 	if (scd->sc_state & IHIDEV_OPEN)
    867 		return (EBUSY);
    868 
    869 	scd->sc_state |= IHIDEV_OPEN;
    870 
    871 	if (sc->sc_refcnt++ || sc->sc_isize == 0)
    872 		return (0);
    873 
    874 	/* power on */
    875 	ihidev_reset(sc, false);
    876 
    877 	return (0);
    878 }
    879 
    880 void
    881 ihidev_close(struct ihidev *scd)
    882 {
    883 	struct ihidev_softc *sc = scd->sc_parent;
    884 
    885 	DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname,
    886 	    __func__, scd->sc_state, sc->sc_refcnt));
    887 
    888 	if (!(scd->sc_state & IHIDEV_OPEN))
    889 		return;
    890 
    891 	scd->sc_state &= ~IHIDEV_OPEN;
    892 
    893 	if (--sc->sc_refcnt)
    894 		return;
    895 
    896 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    897 	    &I2C_HID_POWER_OFF, false))
    898 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
    899 }
    900 
    901 void
    902 ihidev_get_report_desc(struct ihidev_softc *sc, void **desc, int *size)
    903 {
    904 	*desc = sc->sc_report;
    905 	*size = sc->sc_reportlen;
    906 }
    907 
    908 /* convert hid_* constants used throughout HID code to i2c HID equivalents */
    909 int
    910 ihidev_report_type_conv(int hid_type_id)
    911 {
    912 	switch (hid_type_id) {
    913 	case hid_input:
    914 		return I2C_HID_REPORT_TYPE_INPUT;
    915 	case hid_output:
    916 		return I2C_HID_REPORT_TYPE_OUTPUT;
    917 	case hid_feature:
    918 		return I2C_HID_REPORT_TYPE_FEATURE;
    919 	default:
    920 		return -1;
    921 	}
    922 }
    923 
    924 int
    925 ihidev_get_report(struct device *dev, int type, int id, void *data, int len)
    926 {
    927 	struct ihidev_softc *sc = (struct ihidev_softc *)dev;
    928 	struct i2c_hid_report_request rreq;
    929 	int ctype;
    930 
    931 	if ((ctype = ihidev_report_type_conv(type)) < 0)
    932 		return (1);
    933 
    934 	rreq.type = ctype;
    935 	rreq.id = id;
    936 	rreq.data = data;
    937 	rreq.len = len;
    938 
    939 	if (ihidev_hid_command(sc, I2C_HID_CMD_GET_REPORT, &rreq, false)) {
    940 		aprint_error_dev(sc->sc_dev, "failed fetching report\n");
    941 		return (1);
    942 	}
    943 
    944 	return 0;
    945 }
    946 
    947 int
    948 ihidev_set_report(struct device *dev, int type, int id, void *data,
    949     int len)
    950 {
    951 	struct ihidev_softc *sc = (struct ihidev_softc *)dev;
    952 	struct i2c_hid_report_request rreq;
    953 	int ctype;
    954 
    955 	if ((ctype = ihidev_report_type_conv(type)) < 0)
    956 		return (1);
    957 
    958 	rreq.type = ctype;
    959 	rreq.id = id;
    960 	rreq.data = data;
    961 	rreq.len = len;
    962 
    963 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_REPORT, &rreq, false)) {
    964 		aprint_error_dev(sc->sc_dev, "failed setting report\n");
    965 		return (1);
    966 	}
    967 
    968 	return 0;
    969 }
    970