Home | History | Annotate | Line # | Download | only in i2c
ihidev.c revision 1.20.2.1
      1 /* $NetBSD: ihidev.c,v 1.20.2.1 2021/08/09 00:30:09 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.20.2.1 2021/08/09 00:30:09 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 bool	ihidev_acpi_get_info(struct ihidev_softc *);
    129 
    130 static const struct device_compatible_entry compat_data[] = {
    131 	{ .compat = "PNP0C50" },
    132 	{ .compat = "ACPI0C50" },
    133 	{ .compat = "hid-over-i2c" },
    134 	DEVICE_COMPAT_EOL
    135 };
    136 
    137 static int
    138 ihidev_match(device_t parent, cfdata_t match, void *aux)
    139 {
    140 	struct i2c_attach_args * const ia = aux;
    141 	int match_result;
    142 
    143 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    144 		return I2C_MATCH_DIRECT_COMPATIBLE;
    145 
    146 	return 0;
    147 }
    148 
    149 static void
    150 ihidev_attach(device_t parent, device_t self, void *aux)
    151 {
    152 	struct ihidev_softc *sc = device_private(self);
    153 	struct i2c_attach_args *ia = aux;
    154 	struct ihidev_attach_arg iha;
    155 	device_t dev;
    156 	int repid, repsz;
    157 	int isize;
    158 	int locs[IHIDBUSCF_NLOCS];
    159 
    160 	sc->sc_dev = self;
    161 	sc->sc_tag = ia->ia_tag;
    162 	sc->sc_addr = ia->ia_addr;
    163 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
    164 
    165 	if (devhandle_type(device_handle(self)) != DEVHANDLE_TYPE_ACPI) {
    166 		aprint_error(": unsupported device tree type\n");
    167 		return;
    168 	}
    169 	if (! ihidev_acpi_get_info(sc)) {
    170 		return;
    171 	}
    172 
    173 	if (ihidev_hid_command(sc, I2C_HID_CMD_DESCR, NULL, false) ||
    174 	    ihidev_hid_desc_parse(sc)) {
    175 		aprint_error(": failed fetching initial HID descriptor\n");
    176 		return;
    177 	}
    178 
    179 	aprint_naive("\n");
    180 	aprint_normal(": vendor 0x%x product 0x%x, %s\n",
    181 	    le16toh(sc->hid_desc.wVendorID), le16toh(sc->hid_desc.wProductID),
    182 	    ia->ia_name);
    183 
    184 	sc->sc_nrepid = ihidev_maxrepid(sc->sc_report, sc->sc_reportlen);
    185 	if (sc->sc_nrepid < 0)
    186 		return;
    187 
    188 	aprint_normal_dev(self, "%d report id%s\n", sc->sc_nrepid,
    189 	    sc->sc_nrepid > 1 ? "s" : "");
    190 
    191 	sc->sc_nrepid++;
    192 	sc->sc_subdevs = kmem_zalloc(sc->sc_nrepid * sizeof(struct ihidev *),
    193 	    KM_SLEEP);
    194 
    195 	/* find largest report size and allocate memory for input buffer */
    196 	sc->sc_isize = le16toh(sc->hid_desc.wMaxInputLength);
    197 	for (repid = 0; repid < sc->sc_nrepid; repid++) {
    198 		repsz = hid_report_size(sc->sc_report, sc->sc_reportlen,
    199 		    hid_input, repid);
    200 
    201 		isize = repsz + 2; /* two bytes for the length */
    202 		isize += (sc->sc_nrepid != 1); /* one byte for the report ID */
    203 		if (isize > sc->sc_isize)
    204 			sc->sc_isize = isize;
    205 
    206 		DPRINTF(("%s: repid %d size %d\n", sc->sc_dev.dv_xname, repid,
    207 		    repsz));
    208 	}
    209 	sc->sc_ibuf = kmem_zalloc(sc->sc_isize, KM_SLEEP);
    210 	if (! ihiddev_intr_init(sc)) {
    211 		return;
    212 	}
    213 
    214 	iha.iaa = ia;
    215 	iha.parent = sc;
    216 
    217 	/* Look for a driver claiming all report IDs first. */
    218 	iha.reportid = IHIDEV_CLAIM_ALLREPORTID;
    219 	locs[IHIDBUSCF_REPORTID] = IHIDEV_CLAIM_ALLREPORTID;
    220 	dev = config_found(self, &iha, ihidev_print,
    221 	    CFARGS(.submatch = ihidev_submatch,
    222 		   .locators = locs));
    223 	if (dev != NULL) {
    224 		for (repid = 0; repid < sc->sc_nrepid; repid++)
    225 			sc->sc_subdevs[repid] = device_private(dev);
    226 		return;
    227 	}
    228 
    229 	for (repid = 0; repid < sc->sc_nrepid; repid++) {
    230 		if (hid_report_size(sc->sc_report, sc->sc_reportlen, hid_input,
    231 		    repid) == 0 &&
    232 		    hid_report_size(sc->sc_report, sc->sc_reportlen,
    233 		    hid_output, repid) == 0 &&
    234 		    hid_report_size(sc->sc_report, sc->sc_reportlen,
    235 		    hid_feature, repid) == 0)
    236 			continue;
    237 
    238 		iha.reportid = repid;
    239 		locs[IHIDBUSCF_REPORTID] = repid;
    240 		dev = config_found(self, &iha, ihidev_print,
    241 		    CFARGS(.submatch = ihidev_submatch,
    242 			   .locators = locs));
    243 		sc->sc_subdevs[repid] = device_private(dev);
    244 	}
    245 
    246 	/* power down until we're opened */
    247 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, &I2C_HID_POWER_OFF, false)) {
    248 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
    249 		return;
    250 	}
    251 	if (!pmf_device_register(self, ihidev_suspend, ihidev_resume))
    252 		aprint_error_dev(self, "couldn't establish power handler\n");
    253 }
    254 
    255 static int
    256 ihidev_detach(device_t self, int flags)
    257 {
    258 	struct ihidev_softc *sc = device_private(self);
    259 
    260 	mutex_enter(&sc->sc_intr_lock);
    261 	ihiddev_intr_fini(sc);
    262 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    263 	    &I2C_HID_POWER_OFF, true))
    264 	aprint_error_dev(sc->sc_dev, "failed to power down\n");
    265 	mutex_exit(&sc->sc_intr_lock);
    266 	if (sc->sc_ibuf != NULL) {
    267 		kmem_free(sc->sc_ibuf, sc->sc_isize);
    268 		sc->sc_ibuf = NULL;
    269 	}
    270 
    271 	if (sc->sc_report != NULL)
    272 		kmem_free(sc->sc_report, sc->sc_reportlen);
    273 
    274 	pmf_device_deregister(self);
    275 	return (0);
    276 }
    277 
    278 static bool
    279 ihidev_suspend(device_t self, const pmf_qual_t *q)
    280 {
    281 	struct ihidev_softc *sc = device_private(self);
    282 
    283 	mutex_enter(&sc->sc_intr_lock);
    284 	if (sc->sc_refcnt > 0) {
    285 		printf("ihidev power off\n");
    286 		if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    287 		    &I2C_HID_POWER_OFF, true))
    288 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
    289 	}
    290 	mutex_exit(&sc->sc_intr_lock);
    291 	return true;
    292 }
    293 
    294 static bool
    295 ihidev_resume(device_t self, const pmf_qual_t *q)
    296 {
    297 	struct ihidev_softc *sc = device_private(self);
    298 
    299 	mutex_enter(&sc->sc_intr_lock);
    300 	if (sc->sc_refcnt > 0) {
    301 		printf("ihidev power reset\n");
    302 		ihidev_reset(sc, true);
    303 	}
    304 	mutex_exit(&sc->sc_intr_lock);
    305 	return true;
    306 }
    307 
    308 static int
    309 ihidev_hid_command(struct ihidev_softc *sc, int hidcmd, void *arg, bool poll)
    310 {
    311 	int i, res = 1;
    312 	int flags = poll ? I2C_F_POLL : 0;
    313 
    314 	iic_acquire_bus(sc->sc_tag, flags);
    315 
    316 	switch (hidcmd) {
    317 	case I2C_HID_CMD_DESCR: {
    318 		/*
    319 		 * 5.2.2 - HID Descriptor Retrieval
    320 		 * register is passed from the controller
    321 		 */
    322 		uint8_t cmd[] = {
    323 			htole16(sc->sc_hid_desc_addr) & 0xff,
    324 			htole16(sc->sc_hid_desc_addr) >> 8,
    325 		};
    326 
    327 		DPRINTF(("%s: HID command I2C_HID_CMD_DESCR at 0x%x\n",
    328 		    sc->sc_dev.dv_xname, htole16(sc->sc_hid_desc_addr)));
    329 
    330 		/* 20 00 */
    331 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    332 		    &cmd, sizeof(cmd), &sc->hid_desc_buf,
    333 		    sizeof(struct i2c_hid_desc), flags);
    334 
    335 		DPRINTF(("%s: HID descriptor:", sc->sc_dev.dv_xname));
    336 		for (i = 0; i < sizeof(struct i2c_hid_desc); i++)
    337 			DPRINTF((" %.2x", sc->hid_desc_buf[i]));
    338 		DPRINTF(("\n"));
    339 
    340 		break;
    341 	}
    342 	case I2C_HID_CMD_RESET: {
    343 		uint8_t cmd[] = {
    344 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    345 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    346 			0,
    347 			I2C_HID_CMD_RESET,
    348 		};
    349 
    350 		DPRINTF(("%s: HID command I2C_HID_CMD_RESET\n",
    351 		    sc->sc_dev.dv_xname));
    352 
    353 		/* 22 00 00 01 */
    354 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    355 		    &cmd, sizeof(cmd), NULL, 0, flags);
    356 
    357 		break;
    358 	}
    359 	case I2C_HID_CMD_GET_REPORT: {
    360 		struct i2c_hid_report_request *rreq =
    361 		    (struct i2c_hid_report_request *)arg;
    362 
    363 		uint8_t cmd[] = {
    364 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    365 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    366 			0,
    367 			I2C_HID_CMD_GET_REPORT,
    368 			0, 0, 0,
    369 		};
    370 		int cmdlen = 7;
    371 		int dataoff = 4;
    372 		int report_id = rreq->id;
    373 		int report_id_len = 1;
    374 		int report_len = rreq->len + 2;
    375 		int d;
    376 		uint8_t *tmprep;
    377 
    378 		DPRINTF(("%s: HID command I2C_HID_CMD_GET_REPORT %d "
    379 		    "(type %d, len %d)\n", sc->sc_dev.dv_xname, report_id,
    380 		    rreq->type, rreq->len));
    381 
    382 		/*
    383 		 * 7.2.2.4 - "The protocol is optimized for Report < 15.  If a
    384 		 * report ID >= 15 is necessary, then the Report ID in the Low
    385 		 * Byte must be set to 1111 and a Third Byte is appended to the
    386 		 * protocol.  This Third Byte contains the entire/actual report
    387 		 * ID."
    388 		 */
    389 		if (report_id >= 15) {
    390 			cmd[dataoff++] = report_id;
    391 			report_id = 15;
    392 			report_id_len = 2;
    393 		} else
    394 			cmdlen--;
    395 
    396 		cmd[2] = report_id | rreq->type << 4;
    397 
    398 		cmd[dataoff++] = sc->hid_desc.wDataRegister & 0xff;
    399 		cmd[dataoff] = sc->hid_desc.wDataRegister >> 8;
    400 
    401 		/*
    402 		 * 7.2.2.2 - Response will be a 2-byte length value, the report
    403 		 * id with length determined above, and then the report.
    404 		 * Allocate rreq->len + 2 + 2 bytes, read into that temporary
    405 		 * buffer, and then copy only the report back out to
    406 		 * rreq->data.
    407 		 */
    408 		report_len += report_id_len;
    409 		tmprep = kmem_zalloc(report_len, KM_NOSLEEP);
    410 
    411 		/* type 3 id 8: 22 00 38 02 23 00 */
    412 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    413 		    &cmd, cmdlen, tmprep, report_len, flags);
    414 
    415 		d = tmprep[0] | tmprep[1] << 8;
    416 		if (d != report_len) {
    417 			DPRINTF(("%s: response size %d != expected length %d\n",
    418 			    sc->sc_dev.dv_xname, d, report_len));
    419 		}
    420 
    421 		if (report_id_len == 2)
    422 			d = tmprep[2] | tmprep[3] << 8;
    423 		else
    424 			d = tmprep[2];
    425 
    426 		if (d != rreq->id) {
    427 			DPRINTF(("%s: response report id %d != %d\n",
    428 			    sc->sc_dev.dv_xname, d, rreq->id));
    429 			iic_release_bus(sc->sc_tag, 0);
    430 			kmem_free(tmprep, report_len);
    431 			return (1);
    432 		}
    433 
    434 		DPRINTF(("%s: response:", sc->sc_dev.dv_xname));
    435 		for (i = 0; i < report_len; i++)
    436 			DPRINTF((" %.2x", tmprep[i]));
    437 		DPRINTF(("\n"));
    438 
    439 		memcpy(rreq->data, tmprep + 2 + report_id_len, rreq->len);
    440 		kmem_free(tmprep, report_len);
    441 
    442 		break;
    443 	}
    444 	case I2C_HID_CMD_SET_REPORT: {
    445 		struct i2c_hid_report_request *rreq =
    446 		    (struct i2c_hid_report_request *)arg;
    447 
    448 		uint8_t cmd[] = {
    449 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    450 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    451 			0,
    452 			I2C_HID_CMD_SET_REPORT,
    453 			0, 0, 0, 0, 0, 0,
    454 		};
    455 		int cmdlen = 10;
    456 		int report_id = rreq->id;
    457 		int report_len = 2 + (report_id ? 1 : 0) + rreq->len;
    458 		int dataoff;
    459 		uint8_t *finalcmd;
    460 
    461 		DPRINTF(("%s: HID command I2C_HID_CMD_SET_REPORT %d "
    462 		    "(type %d, len %d):", sc->sc_dev.dv_xname, report_id,
    463 		    rreq->type, rreq->len));
    464 		for (i = 0; i < rreq->len; i++)
    465 			DPRINTF((" %.2x", ((uint8_t *)rreq->data)[i]));
    466 		DPRINTF(("\n"));
    467 
    468 		/*
    469 		 * 7.2.2.4 - "The protocol is optimized for Report < 15.  If a
    470 		 * report ID >= 15 is necessary, then the Report ID in the Low
    471 		 * Byte must be set to 1111 and a Third Byte is appended to the
    472 		 * protocol.  This Third Byte contains the entire/actual report
    473 		 * ID."
    474 		 */
    475 		dataoff = 4;
    476 		if (report_id >= 15) {
    477 			cmd[dataoff++] = report_id;
    478 			report_id = 15;
    479 		} else
    480 			cmdlen--;
    481 
    482 		cmd[2] = report_id | rreq->type << 4;
    483 
    484 		if (rreq->type == I2C_HID_REPORT_TYPE_FEATURE) {
    485 			cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister)
    486 			    & 0xff;
    487 			cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister)
    488 			    >> 8;
    489 		} else {
    490 			cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister)
    491 			    & 0xff;
    492 			cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister)
    493 			    >> 8;
    494 		}
    495 
    496 		cmd[dataoff++] = report_len & 0xff;
    497 		cmd[dataoff++] = report_len >> 8;
    498 		cmd[dataoff] = rreq->id;
    499 
    500 		finalcmd = kmem_zalloc(cmdlen + rreq->len, KM_NOSLEEP);
    501 
    502 		memcpy(finalcmd, cmd, cmdlen);
    503 		memcpy(finalcmd + cmdlen, rreq->data, rreq->len);
    504 
    505 		/* type 3 id 4: 22 00 34 03 23 00 04 00 04 03 */
    506 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    507 		    finalcmd, cmdlen + rreq->len, NULL, 0, flags);
    508 		kmem_free(finalcmd, cmdlen + rreq->len);
    509 
    510  		break;
    511  	}
    512 
    513 	case I2C_HID_CMD_SET_POWER: {
    514 		int power = *(int *)arg;
    515 		uint8_t cmd[] = {
    516 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
    517 			htole16(sc->hid_desc.wCommandRegister) >> 8,
    518 			power,
    519 			I2C_HID_CMD_SET_POWER,
    520 		};
    521 
    522 		DPRINTF(("%s: HID command I2C_HID_CMD_SET_POWER(%d)\n",
    523 		    sc->sc_dev.dv_xname, power));
    524 
    525 		/* 22 00 00 08 */
    526 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
    527 		    &cmd, sizeof(cmd), NULL, 0, flags);
    528 
    529 		break;
    530 	}
    531 	case I2C_HID_REPORT_DESCR: {
    532 		uint8_t cmd[] = {
    533 			htole16(sc->hid_desc.wReportDescRegister) & 0xff,
    534 			htole16(sc->hid_desc.wReportDescRegister) >> 8,
    535 		};
    536 
    537 		DPRINTF(("%s: HID command I2C_HID_REPORT_DESCR at 0x%x with "
    538 		    "size %d\n", sc->sc_dev.dv_xname, cmd[0],
    539 		    sc->sc_reportlen));
    540 
    541 		/* 20 00 */
    542 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
    543 		    &cmd, sizeof(cmd), sc->sc_report, sc->sc_reportlen, flags);
    544 
    545 		DPRINTF(("%s: HID report descriptor:", sc->sc_dev.dv_xname));
    546 		for (i = 0; i < sc->sc_reportlen; i++)
    547 			DPRINTF((" %.2x", sc->sc_report[i]));
    548 		DPRINTF(("\n"));
    549 
    550 		break;
    551 	}
    552 	default:
    553 		aprint_error_dev(sc->sc_dev, "unknown command %d\n",
    554 		    hidcmd);
    555 	}
    556 
    557 	iic_release_bus(sc->sc_tag, flags);
    558 
    559 	return (res);
    560 }
    561 
    562 static int
    563 ihidev_reset(struct ihidev_softc *sc, bool poll)
    564 {
    565 	DPRINTF(("%s: resetting\n", sc->sc_dev.dv_xname));
    566 
    567 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    568 	    &I2C_HID_POWER_ON, poll)) {
    569 		aprint_error_dev(sc->sc_dev, "failed to power on\n");
    570 		return (1);
    571 	}
    572 
    573 	DELAY(1000);
    574 
    575 	if (ihidev_hid_command(sc, I2C_HID_CMD_RESET, 0, poll)) {
    576 		aprint_error_dev(sc->sc_dev, "failed to reset hardware\n");
    577 
    578 		ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    579 		    &I2C_HID_POWER_OFF, poll);
    580 
    581 		return (1);
    582 	}
    583 
    584 	DELAY(1000);
    585 
    586 	return (0);
    587 }
    588 
    589 /*
    590  * 5.2.2 - HID Descriptor Retrieval
    591  *
    592  * parse HID Descriptor that has already been read into hid_desc with
    593  * I2C_HID_CMD_DESCR
    594  */
    595 static int
    596 ihidev_hid_desc_parse(struct ihidev_softc *sc)
    597 {
    598 	int retries = 3;
    599 
    600 	/* must be v01.00 */
    601 	if (le16toh(sc->hid_desc.bcdVersion) != 0x0100) {
    602 		aprint_error_dev(sc->sc_dev,
    603 		    "bad HID descriptor bcdVersion (0x%x)\n",
    604 		    le16toh(sc->hid_desc.bcdVersion));
    605 		return (1);
    606 	}
    607 
    608 	/* must be 30 bytes for v1.00 */
    609 	if (le16toh(sc->hid_desc.wHIDDescLength !=
    610 	    sizeof(struct i2c_hid_desc))) {
    611 		aprint_error_dev(sc->sc_dev,
    612 		    "bad HID descriptor size (%d != %zu)\n",
    613 		    le16toh(sc->hid_desc.wHIDDescLength),
    614 		    sizeof(struct i2c_hid_desc));
    615 		return (1);
    616 	}
    617 
    618 	if (le16toh(sc->hid_desc.wReportDescLength) <= 0) {
    619 		aprint_error_dev(sc->sc_dev,
    620 		    "bad HID report descriptor size (%d)\n",
    621 		    le16toh(sc->hid_desc.wReportDescLength));
    622 		return (1);
    623 	}
    624 
    625 	while (retries-- > 0) {
    626 		if (ihidev_reset(sc, false)) {
    627 			if (retries == 0)
    628 				return(1);
    629 
    630 			DELAY(1000);
    631 		}
    632 		else
    633 			break;
    634 	}
    635 
    636 	sc->sc_reportlen = le16toh(sc->hid_desc.wReportDescLength);
    637 	sc->sc_report = kmem_zalloc(sc->sc_reportlen, KM_NOSLEEP);
    638 
    639 	if (ihidev_hid_command(sc, I2C_HID_REPORT_DESCR, 0, false)) {
    640 		aprint_error_dev(sc->sc_dev, "failed fetching HID report\n");
    641 		return (1);
    642 	}
    643 
    644 	return (0);
    645 }
    646 
    647 static bool
    648 ihiddev_intr_init(struct ihidev_softc *sc)
    649 {
    650 #if NACPICA > 0
    651 	ACPI_HANDLE hdl = devhandle_to_acpi(device_handle(sc->sc_dev));
    652 	struct acpi_resources res;
    653 	ACPI_STATUS rv;
    654 	char buf[100];
    655 
    656 	rv = acpi_resource_parse(sc->sc_dev, hdl, "_CRS", &res,
    657 	    &acpi_resource_parse_ops_quiet);
    658 	if (ACPI_FAILURE(rv)) {
    659 		aprint_error_dev(sc->sc_dev, "can't parse '_CRS'\n");
    660 		return false;
    661 	}
    662 
    663 	const struct acpi_irq * const irq = acpi_res_irq(&res, 0);
    664 	if (irq == NULL) {
    665 		aprint_error_dev(sc->sc_dev, "no IRQ resource\n");
    666 		acpi_resource_cleanup(&res);
    667 		return false;
    668 	}
    669 
    670 	sc->sc_intr_type =
    671 	    irq->ar_type == ACPI_EDGE_SENSITIVE ? IST_EDGE : IST_LEVEL;
    672 
    673 	acpi_resource_cleanup(&res);
    674 
    675 	sc->sc_ih = acpi_intr_establish(sc->sc_dev, (uint64_t)hdl, IPL_TTY,
    676 	    false, ihidev_intr, sc, device_xname(sc->sc_dev));
    677 	if (sc->sc_ih == NULL) {
    678 		aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
    679 		return false;
    680 	}
    681 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n",
    682 	    acpi_intr_string(sc->sc_ih, buf, sizeof(buf)));
    683 
    684 	sc->sc_sih = softint_establish(SOFTINT_SERIAL, ihidev_softintr, sc);
    685 	if (sc->sc_sih == NULL) {
    686 		aprint_error_dev(sc->sc_dev,
    687 		    "can't establish soft interrupt\n");
    688 		return false;
    689 	}
    690 
    691 	return true;
    692 #else
    693 	aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
    694 	return false;
    695 #endif
    696 }
    697 
    698 static void
    699 ihiddev_intr_fini(struct ihidev_softc *sc)
    700 {
    701 #if NACPICA > 0
    702 	if (sc->sc_ih != NULL) {
    703 		acpi_intr_disestablish(sc->sc_ih);
    704 	}
    705 	if (sc->sc_sih != NULL) {
    706 		softint_disestablish(sc->sc_sih);
    707 	}
    708 #endif
    709 }
    710 
    711 static void
    712 ihidev_intr_mask(struct ihidev_softc * const sc)
    713 {
    714 
    715 	if (sc->sc_intr_type == IST_LEVEL) {
    716 #if NACPICA > 0
    717 		acpi_intr_mask(sc->sc_ih);
    718 #endif
    719 	}
    720 }
    721 
    722 static void
    723 ihidev_intr_unmask(struct ihidev_softc * const sc)
    724 {
    725 
    726 	if (sc->sc_intr_type == IST_LEVEL) {
    727 #if NACPICA > 0
    728 		acpi_intr_unmask(sc->sc_ih);
    729 #endif
    730 	}
    731 }
    732 
    733 static int
    734 ihidev_intr(void *arg)
    735 {
    736 	struct ihidev_softc * const sc = arg;
    737 
    738 	mutex_enter(&sc->sc_intr_lock);
    739 
    740 	/*
    741 	 * Schedule our soft interrupt handler.  If we're using a level-
    742 	 * triggered interrupt, we have to mask it off while we wait
    743 	 * for service.
    744 	 */
    745 	softint_schedule(sc->sc_sih);
    746 	ihidev_intr_mask(sc);
    747 
    748 	mutex_exit(&sc->sc_intr_lock);
    749 
    750 	return 1;
    751 }
    752 
    753 static void
    754 ihidev_softintr(void *arg)
    755 {
    756 	struct ihidev_softc * const sc = arg;
    757 	struct ihidev *scd;
    758 	u_int psize;
    759 	int res, i;
    760 	u_char *p;
    761 	u_int rep = 0;
    762 
    763 	mutex_enter(&sc->sc_intr_lock);
    764 	iic_acquire_bus(sc->sc_tag, 0);
    765 	res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, NULL, 0,
    766 	    sc->sc_ibuf, sc->sc_isize, 0);
    767 	iic_release_bus(sc->sc_tag, 0);
    768 	mutex_exit(&sc->sc_intr_lock);
    769 
    770 	if (res != 0)
    771 		goto out;
    772 
    773 	/*
    774 	 * 6.1.1 - First two bytes are the packet length, which must be less
    775 	 * than or equal to wMaxInputLength
    776 	 */
    777 	psize = sc->sc_ibuf[0] | sc->sc_ibuf[1] << 8;
    778 	if (!psize || psize > sc->sc_isize) {
    779 		DPRINTF(("%s: %s: invalid packet size (%d vs. %d)\n",
    780 		    sc->sc_dev.dv_xname, __func__, psize, sc->sc_isize));
    781 		goto out;
    782 	}
    783 
    784 	/* 3rd byte is the report id */
    785 	p = sc->sc_ibuf + 2;
    786 	psize -= 2;
    787 	if (sc->sc_nrepid != 1)
    788 		rep = *p++, psize--;
    789 
    790 	if (rep >= sc->sc_nrepid) {
    791 		aprint_error_dev(sc->sc_dev, "%s: bad report id %d\n",
    792 		    __func__, rep);
    793 		goto out;
    794 	}
    795 
    796 	DPRINTF(("%s: %s: hid input (rep %d):", sc->sc_dev.dv_xname,
    797 	    __func__, rep));
    798 	for (i = 0; i < sc->sc_isize; i++)
    799 		DPRINTF((" %.2x", sc->sc_ibuf[i]));
    800 	DPRINTF(("\n"));
    801 
    802 	scd = sc->sc_subdevs[rep];
    803 	if (scd == NULL || !(scd->sc_state & IHIDEV_OPEN))
    804 		goto out;
    805 
    806 	scd->sc_intr(scd, p, psize);
    807 
    808  out:
    809 	/*
    810 	 * If our interrupt is level-triggered, re-enable it now.
    811 	 */
    812 	ihidev_intr_unmask(sc);
    813 }
    814 
    815 static int
    816 ihidev_maxrepid(void *buf, int len)
    817 {
    818 	struct hid_data *d;
    819 	struct hid_item h;
    820 	int maxid;
    821 
    822 	maxid = -1;
    823 	h.report_ID = 0;
    824 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
    825 		if ((int)h.report_ID > maxid)
    826 			maxid = h.report_ID;
    827 	hid_end_parse(d);
    828 
    829 	return (maxid);
    830 }
    831 
    832 static int
    833 ihidev_print(void *aux, const char *pnp)
    834 {
    835 	struct ihidev_attach_arg *iha = aux;
    836 
    837 	if (iha->reportid == IHIDEV_CLAIM_ALLREPORTID)
    838 		return (QUIET);
    839 
    840 	if (pnp)
    841 		aprint_normal("hid at %s", pnp);
    842 
    843 	if (iha->reportid != 0)
    844 		aprint_normal(" reportid %d", iha->reportid);
    845 
    846 	return (UNCONF);
    847 }
    848 
    849 static int
    850 ihidev_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    851 {
    852 	struct ihidev_attach_arg *iha = aux;
    853 
    854 	if (cf->ihidevcf_reportid != IHIDEV_UNK_REPORTID &&
    855 	    cf->ihidevcf_reportid != iha->reportid)
    856 		return (0);
    857 
    858 	return config_match(parent, cf, aux);
    859 }
    860 
    861 int
    862 ihidev_open(struct ihidev *scd)
    863 {
    864 	struct ihidev_softc *sc = scd->sc_parent;
    865 
    866 	DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname,
    867 	    __func__, scd->sc_state, sc->sc_refcnt));
    868 
    869 	if (scd->sc_state & IHIDEV_OPEN)
    870 		return (EBUSY);
    871 
    872 	scd->sc_state |= IHIDEV_OPEN;
    873 
    874 	if (sc->sc_refcnt++ || sc->sc_isize == 0)
    875 		return (0);
    876 
    877 	/* power on */
    878 	ihidev_reset(sc, false);
    879 
    880 	return (0);
    881 }
    882 
    883 void
    884 ihidev_close(struct ihidev *scd)
    885 {
    886 	struct ihidev_softc *sc = scd->sc_parent;
    887 
    888 	DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname,
    889 	    __func__, scd->sc_state, sc->sc_refcnt));
    890 
    891 	if (!(scd->sc_state & IHIDEV_OPEN))
    892 		return;
    893 
    894 	scd->sc_state &= ~IHIDEV_OPEN;
    895 
    896 	if (--sc->sc_refcnt)
    897 		return;
    898 
    899 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
    900 	    &I2C_HID_POWER_OFF, false))
    901 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
    902 }
    903 
    904 void
    905 ihidev_get_report_desc(struct ihidev_softc *sc, void **desc, int *size)
    906 {
    907 	*desc = sc->sc_report;
    908 	*size = sc->sc_reportlen;
    909 }
    910 
    911 /* convert hid_* constants used throughout HID code to i2c HID equivalents */
    912 int
    913 ihidev_report_type_conv(int hid_type_id)
    914 {
    915 	switch (hid_type_id) {
    916 	case hid_input:
    917 		return I2C_HID_REPORT_TYPE_INPUT;
    918 	case hid_output:
    919 		return I2C_HID_REPORT_TYPE_OUTPUT;
    920 	case hid_feature:
    921 		return I2C_HID_REPORT_TYPE_FEATURE;
    922 	default:
    923 		return -1;
    924 	}
    925 }
    926 
    927 int
    928 ihidev_get_report(struct device *dev, int type, int id, void *data, int len)
    929 {
    930 	struct ihidev_softc *sc = (struct ihidev_softc *)dev;
    931 	struct i2c_hid_report_request rreq;
    932 	int ctype;
    933 
    934 	if ((ctype = ihidev_report_type_conv(type)) < 0)
    935 		return (1);
    936 
    937 	rreq.type = ctype;
    938 	rreq.id = id;
    939 	rreq.data = data;
    940 	rreq.len = len;
    941 
    942 	if (ihidev_hid_command(sc, I2C_HID_CMD_GET_REPORT, &rreq, false)) {
    943 		aprint_error_dev(sc->sc_dev, "failed fetching report\n");
    944 		return (1);
    945 	}
    946 
    947 	return 0;
    948 }
    949 
    950 int
    951 ihidev_set_report(struct device *dev, int type, int id, void *data,
    952     int len)
    953 {
    954 	struct ihidev_softc *sc = (struct ihidev_softc *)dev;
    955 	struct i2c_hid_report_request rreq;
    956 	int ctype;
    957 
    958 	if ((ctype = ihidev_report_type_conv(type)) < 0)
    959 		return (1);
    960 
    961 	rreq.type = ctype;
    962 	rreq.id = id;
    963 	rreq.data = data;
    964 	rreq.len = len;
    965 
    966 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_REPORT, &rreq, false)) {
    967 		aprint_error_dev(sc->sc_dev, "failed setting report\n");
    968 		return (1);
    969 	}
    970 
    971 	return 0;
    972 }
    973 
    974 static bool
    975 ihidev_acpi_get_info(struct ihidev_softc *sc)
    976 {
    977 	ACPI_HANDLE hdl = devhandle_to_acpi(device_handle(sc->sc_dev));
    978 	ACPI_STATUS status;
    979 	ACPI_INTEGER val;
    980 
    981 	/* 3cdff6f7-4267-4555-ad05-b30a3d8938de */
    982 	uint8_t i2c_hid_guid[] = {
    983 		0xF7, 0xF6, 0xDF, 0x3C,
    984 		0x67, 0x42,
    985 		0x55, 0x45,
    986 		0xAD, 0x05,
    987 		0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
    988 	};
    989 
    990 	status = acpi_dsm_integer(hdl, i2c_hid_guid, 1, 1, NULL, &val);
    991 	if (ACPI_FAILURE(status)) {
    992 		aprint_error_dev(sc->sc_dev,
    993 		    "failed to get HidDescriptorAddress: %s\n",
    994 		    AcpiFormatException(status));
    995 		return false;
    996 	}
    997 
    998 	sc->sc_hid_desc_addr = (u_int)val;
    999 
   1000 	return true;
   1001 }
   1002