xref: /netbsd-src/sys/dev/i2c/ihidev.c (revision 87d689fb734c654d2486f87f7be32f1b53ecdbec)
1 /* $NetBSD: ihidev.c,v 1.1 2017/12/10 17:05:54 bouyer 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@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.1 2017/12/10 17:05:54 bouyer 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/acpi_intr.h>
75 #endif
76 
77 #include "locators.h"
78 
79 /* #define IHIDEV_DEBUG */
80 
81 #ifdef IHIDEV_DEBUG
82 #define DPRINTF(x) printf x
83 #else
84 #define DPRINTF(x)
85 #endif
86 
87 /* 7.2 */
88 enum {
89 	I2C_HID_CMD_DESCR	= 0x0,
90 	I2C_HID_CMD_RESET	= 0x1,
91 	I2C_HID_CMD_GET_REPORT	= 0x2,
92 	I2C_HID_CMD_SET_REPORT	= 0x3,
93 	I2C_HID_CMD_GET_IDLE	= 0x4,
94 	I2C_HID_CMD_SET_IDLE	= 0x5,
95 	I2C_HID_CMD_GET_PROTO	= 0x6,
96 	I2C_HID_CMD_SET_PROTO	= 0x7,
97 	I2C_HID_CMD_SET_POWER	= 0x8,
98 
99 	/* pseudo commands */
100 	I2C_HID_REPORT_DESCR	= 0x100,
101 };
102 
103 static int I2C_HID_POWER_ON	= 0x0;
104 static int I2C_HID_POWER_OFF	= 0x1;
105 
106 static int	ihidev_match(device_t, cfdata_t, void *);
107 static void	ihidev_attach(device_t, device_t, void *);
108 static int	ihidev_detach(device_t, int);
109 CFATTACH_DECL_NEW(ihidev, sizeof(struct ihidev_softc),
110     ihidev_match, ihidev_attach, ihidev_detach, NULL);
111 
112 static bool	ihidev_suspend(device_t, const pmf_qual_t *);
113 static bool	ihidev_resume(device_t, const pmf_qual_t *);
114 static int	ihidev_hid_command(struct ihidev_softc *, int, void *, bool);
115 static unsigned int ihidev_intr(void *);
116 static int	ihidev_reset(struct ihidev_softc *, bool);
117 static int	ihidev_hid_desc_parse(struct ihidev_softc *);
118 
119 static int	ihidev_maxrepid(void *, int);
120 static int	ihidev_print(void *, const char *);
121 static int	ihidev_submatch(device_t, cfdata_t, const int *, void *);
122 
123 static const char *ihidev_compats[] = {
124 	"hid-over-i2c",
125 	NULL
126 };
127 
128 static int
129 ihidev_match(device_t parent, cfdata_t match, void *aux)
130 {
131 	struct i2c_attach_args * const ia = aux;
132 
133 	if (ia->ia_ncompat > 0) {
134 		if (iic_compat_match(ia, ihidev_compats))
135 			return 1;
136 	}
137 	return 0;
138 }
139 
140 static void
141 ihidev_attach(device_t parent, device_t self, void *aux)
142 {
143 	struct ihidev_softc *sc = device_private(self);
144 	struct i2c_attach_args *ia = aux;
145 	struct ihidev_attach_arg iha;
146 	device_t dev;
147 	int repid, repsz;
148 	int isize;
149 	uint32_t v;
150 	int locs[IHIDBUSCF_NLOCS];
151 
152 
153 	sc->sc_dev = self;
154 	sc->sc_tag = ia->ia_tag;
155 	sc->sc_addr = ia->ia_addr;
156 	sc->sc_phandle = ia->ia_cookie;
157 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
158 
159 	if (!prop_dictionary_get_uint32(ia->ia_prop, "hid-descr-addr", &v)) {
160 		aprint_error(": no hid-descr-addr value\n");
161 		return;
162 	}
163 
164 	sc->sc_hid_desc_addr = v;
165 
166 	if (ihidev_hid_command(sc, I2C_HID_CMD_DESCR, NULL, false) ||
167 	    ihidev_hid_desc_parse(sc)) {
168 		aprint_error(": failed fetching initial HID descriptor\n");
169 		return;
170 	}
171 
172 	aprint_naive("\n");
173 	aprint_normal(": vendor 0x%x product 0x%x, %s\n",
174 	    le16toh(sc->hid_desc.wVendorID), le16toh(sc->hid_desc.wProductID),
175 	    ia->ia_name);
176 
177 	sc->sc_nrepid = ihidev_maxrepid(sc->sc_report, sc->sc_reportlen);
178 	if (sc->sc_nrepid < 0)
179 		return;
180 
181 	aprint_normal_dev(self, "%d report id%s\n", sc->sc_nrepid,
182 	    sc->sc_nrepid > 1 ? "s" : "");
183 
184 	sc->sc_nrepid++;
185 	sc->sc_subdevs = kmem_zalloc(sc->sc_nrepid * sizeof(struct ihidev *),
186 	    KM_NOSLEEP);
187 	if (sc->sc_subdevs == NULL) {
188 		aprint_error_dev(self, "failed allocating memory\n");
189 		return;
190 	}
191 
192 	/* find largest report size and allocate memory for input buffer */
193 	sc->sc_isize = le16toh(sc->hid_desc.wMaxInputLength);
194 	for (repid = 0; repid < sc->sc_nrepid; repid++) {
195 		repsz = hid_report_size(sc->sc_report, sc->sc_reportlen,
196 		    hid_input, repid);
197 
198 		isize = repsz + 2; /* two bytes for the length */
199 		isize += (sc->sc_nrepid != 1); /* one byte for the report ID */
200 		if (isize > sc->sc_isize)
201 			sc->sc_isize = isize;
202 
203 		DPRINTF(("%s: repid %d size %d\n", sc->sc_dev.dv_xname, repid,
204 		    repsz));
205 	}
206 	sc->sc_ibuf = kmem_zalloc(sc->sc_isize, KM_NOSLEEP);
207 #if NACPICA > 0
208 	{
209 		char buf[100];
210 
211 		sc->sc_ih = acpi_intr_establish(self, sc->sc_phandle, ihidev_intr, sc);
212 		if (sc->sc_ih == NULL)
213 			aprint_error_dev(self, "can't establish interrupt\n");
214 		aprint_normal_dev(self, "interrupting at %s\n",
215 		    acpi_intr_string(sc->sc_ih, buf, sizeof(buf)));
216 	}
217 #endif
218 
219 	iha.iaa = ia;
220 	iha.parent = sc;
221 
222 	/* Look for a driver claiming all report IDs first. */
223 	iha.reportid = IHIDEV_CLAIM_ALLREPORTID;
224 	locs[IHIDBUSCF_REPORTID] = IHIDEV_CLAIM_ALLREPORTID;
225 	dev = config_found_sm_loc(self, "ihidbus", locs, &iha,
226 	    ihidev_print, ihidev_submatch);
227 	if (dev != NULL) {
228 		for (repid = 0; repid < sc->sc_nrepid; repid++)
229 			sc->sc_subdevs[repid] = device_private(dev);
230 		return;
231 	}
232 
233 	for (repid = 0; repid < sc->sc_nrepid; repid++) {
234 		if (hid_report_size(sc->sc_report, sc->sc_reportlen, hid_input,
235 		    repid) == 0 &&
236 		    hid_report_size(sc->sc_report, sc->sc_reportlen,
237 		    hid_output, repid) == 0 &&
238 		    hid_report_size(sc->sc_report, sc->sc_reportlen,
239 		    hid_feature, repid) == 0)
240 			continue;
241 
242 		iha.reportid = repid;
243 		locs[IHIDBUSCF_REPORTID] = repid;
244 		dev = config_found_sm_loc(self, "ihidbus", locs,
245 		    &iha, ihidev_print, ihidev_submatch);
246 		sc->sc_subdevs[repid] = device_private(dev);
247 	}
248 
249 	/* power down until we're opened */
250 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, &I2C_HID_POWER_OFF, false)) {
251 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
252 		return;
253 	}
254 	if (!pmf_device_register(self, ihidev_suspend, ihidev_resume))
255 		aprint_error_dev(self, "couldn't establish power handler\n");
256 }
257 
258 static int
259 ihidev_detach(device_t self, int flags)
260 {
261 	struct ihidev_softc *sc = device_private(self);
262 
263 	mutex_enter(&sc->sc_intr_lock);
264 #if NACPICA > 0
265 	if (sc->sc_ih != NULL)
266 		acpi_intr_disestablish(sc->sc_ih, ihidev_intr);
267 #endif
268 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
269 	    &I2C_HID_POWER_OFF, true))
270 	aprint_error_dev(sc->sc_dev, "failed to power down\n");
271 	mutex_exit(&sc->sc_intr_lock);
272 	if (sc->sc_ibuf != NULL) {
273 		kmem_free(sc->sc_ibuf, sc->sc_isize);
274 		sc->sc_ibuf = NULL;
275 	}
276 
277 	if (sc->sc_report != NULL)
278 		kmem_free(sc->sc_report, sc->sc_reportlen);
279 
280 	pmf_device_deregister(self);
281 	return (0);
282 }
283 
284 static bool
285 ihidev_suspend(device_t self, const pmf_qual_t *q)
286 {
287 	struct ihidev_softc *sc = device_private(self);
288 
289 	mutex_enter(&sc->sc_intr_lock);
290 	if (sc->sc_refcnt > 0) {
291 		printf("ihidev power off\n");
292 		if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
293 		    &I2C_HID_POWER_OFF, true))
294 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
295 	}
296 	mutex_exit(&sc->sc_intr_lock);
297 	return true;
298 }
299 
300 static bool
301 ihidev_resume(device_t self, const pmf_qual_t *q)
302 {
303 	struct ihidev_softc *sc = device_private(self);
304 
305 	mutex_enter(&sc->sc_intr_lock);
306 	if (sc->sc_refcnt > 0) {
307 		printf("ihidev power reset\n");
308 		ihidev_reset(sc, true);
309 	}
310 	mutex_exit(&sc->sc_intr_lock);
311 	return true;
312 }
313 
314 static int
315 ihidev_hid_command(struct ihidev_softc *sc, int hidcmd, void *arg, bool poll)
316 {
317 	int i, res = 1;
318 	int flags = poll ? I2C_F_POLL : 0;
319 
320 	iic_acquire_bus(sc->sc_tag, flags);
321 
322 	switch (hidcmd) {
323 	case I2C_HID_CMD_DESCR: {
324 		/*
325 		 * 5.2.2 - HID Descriptor Retrieval
326 		 * register is passed from the controller
327 		 */
328 		uint8_t cmd[] = {
329 			htole16(sc->sc_hid_desc_addr) & 0xff,
330 			htole16(sc->sc_hid_desc_addr) >> 8,
331 		};
332 
333 		DPRINTF(("%s: HID command I2C_HID_CMD_DESCR at 0x%x\n",
334 		    sc->sc_dev.dv_xname, htole16(sc->sc_hid_desc_addr)));
335 
336 		/* 20 00 */
337 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
338 		    &cmd, sizeof(cmd), &sc->hid_desc_buf,
339 		    sizeof(struct i2c_hid_desc), flags);
340 
341 		DPRINTF(("%s: HID descriptor:", sc->sc_dev.dv_xname));
342 		for (i = 0; i < sizeof(struct i2c_hid_desc); i++)
343 			DPRINTF((" %.2x", sc->hid_desc_buf[i]));
344 		DPRINTF(("\n"));
345 
346 		break;
347 	}
348 	case I2C_HID_CMD_RESET: {
349 		uint8_t cmd[] = {
350 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
351 			htole16(sc->hid_desc.wCommandRegister) >> 8,
352 			0,
353 			I2C_HID_CMD_RESET,
354 		};
355 
356 		DPRINTF(("%s: HID command I2C_HID_CMD_RESET\n",
357 		    sc->sc_dev.dv_xname));
358 
359 		/* 22 00 00 01 */
360 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
361 		    &cmd, sizeof(cmd), NULL, 0, flags);
362 
363 		break;
364 	}
365 	case I2C_HID_CMD_GET_REPORT: {
366 		struct i2c_hid_report_request *rreq =
367 		    (struct i2c_hid_report_request *)arg;
368 
369 		uint8_t cmd[] = {
370 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
371 			htole16(sc->hid_desc.wCommandRegister) >> 8,
372 			0,
373 			I2C_HID_CMD_GET_REPORT,
374 			0, 0, 0,
375 		};
376 		int cmdlen = 7;
377 		int dataoff = 4;
378 		int report_id = rreq->id;
379 		int report_id_len = 1;
380 		int report_len = rreq->len + 2;
381 		int d;
382 		uint8_t *tmprep;
383 
384 		DPRINTF(("%s: HID command I2C_HID_CMD_GET_REPORT %d "
385 		    "(type %d, len %d)\n", sc->sc_dev.dv_xname, report_id,
386 		    rreq->type, rreq->len));
387 
388 		/*
389 		 * 7.2.2.4 - "The protocol is optimized for Report < 15.  If a
390 		 * report ID >= 15 is necessary, then the Report ID in the Low
391 		 * Byte must be set to 1111 and a Third Byte is appended to the
392 		 * protocol.  This Third Byte contains the entire/actual report
393 		 * ID."
394 		 */
395 		if (report_id >= 15) {
396 			cmd[dataoff++] = report_id;
397 			report_id = 15;
398 			report_id_len = 2;
399 		} else
400 			cmdlen--;
401 
402 		cmd[2] = report_id | rreq->type << 4;
403 
404 		cmd[dataoff++] = sc->hid_desc.wDataRegister & 0xff;
405 		cmd[dataoff] = sc->hid_desc.wDataRegister >> 8;
406 
407 		/*
408 		 * 7.2.2.2 - Response will be a 2-byte length value, the report
409 		 * id with length determined above, and then the report.
410 		 * Allocate rreq->len + 2 + 2 bytes, read into that temporary
411 		 * buffer, and then copy only the report back out to
412 		 * rreq->data.
413 		 */
414 		report_len += report_id_len;
415 		tmprep = kmem_zalloc(report_len, KM_NOSLEEP);
416 
417 		/* type 3 id 8: 22 00 38 02 23 00 */
418 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
419 		    &cmd, cmdlen, tmprep, report_len, flags);
420 
421 		d = tmprep[0] | tmprep[1] << 8;
422 		if (d != report_len) {
423 			DPRINTF(("%s: response size %d != expected length %d\n",
424 			    sc->sc_dev.dv_xname, d, report_len));
425 		}
426 
427 		if (report_id_len == 2)
428 			d = tmprep[2] | tmprep[3] << 8;
429 		else
430 			d = tmprep[2];
431 
432 		if (d != rreq->id) {
433 			DPRINTF(("%s: response report id %d != %d\n",
434 			    sc->sc_dev.dv_xname, d, rreq->id));
435 			iic_release_bus(sc->sc_tag, 0);
436 			kmem_free(tmprep, report_len);
437 			return (1);
438 		}
439 
440 		DPRINTF(("%s: response:", sc->sc_dev.dv_xname));
441 		for (i = 0; i < report_len; i++)
442 			DPRINTF((" %.2x", tmprep[i]));
443 		DPRINTF(("\n"));
444 
445 		memcpy(rreq->data, tmprep + 2 + report_id_len, rreq->len);
446 		kmem_free(tmprep, report_len);
447 
448 		break;
449 	}
450 	case I2C_HID_CMD_SET_REPORT: {
451 		struct i2c_hid_report_request *rreq =
452 		    (struct i2c_hid_report_request *)arg;
453 
454 		uint8_t cmd[] = {
455 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
456 			htole16(sc->hid_desc.wCommandRegister) >> 8,
457 			0,
458 			I2C_HID_CMD_SET_REPORT,
459 			0, 0, 0, 0, 0, 0,
460 		};
461 		int cmdlen = 10;
462 		int report_id = rreq->id;
463 		int report_len = 2 + (report_id ? 1 : 0) + rreq->len;
464 		int dataoff;
465 		uint8_t *finalcmd;
466 
467 		DPRINTF(("%s: HID command I2C_HID_CMD_SET_REPORT %d "
468 		    "(type %d, len %d):", sc->sc_dev.dv_xname, report_id,
469 		    rreq->type, rreq->len));
470 		for (i = 0; i < rreq->len; i++)
471 			DPRINTF((" %.2x", ((uint8_t *)rreq->data)[i]));
472 		DPRINTF(("\n"));
473 
474 		/*
475 		 * 7.2.2.4 - "The protocol is optimized for Report < 15.  If a
476 		 * report ID >= 15 is necessary, then the Report ID in the Low
477 		 * Byte must be set to 1111 and a Third Byte is appended to the
478 		 * protocol.  This Third Byte contains the entire/actual report
479 		 * ID."
480 		 */
481 		dataoff = 4;
482 		if (report_id >= 15) {
483 			cmd[dataoff++] = report_id;
484 			report_id = 15;
485 		} else
486 			cmdlen--;
487 
488 		cmd[2] = report_id | rreq->type << 4;
489 
490 		if (rreq->type == I2C_HID_REPORT_TYPE_FEATURE) {
491 			cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister)
492 			    & 0xff;
493 			cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister)
494 			    >> 8;
495 		} else {
496 			cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister)
497 			    & 0xff;
498 			cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister)
499 			    >> 8;
500 		}
501 
502 		cmd[dataoff++] = report_len & 0xff;
503 		cmd[dataoff++] = report_len >> 8;
504 		cmd[dataoff] = rreq->id;
505 
506 		finalcmd = kmem_zalloc(cmdlen + rreq->len, KM_NOSLEEP);
507 
508 		memcpy(finalcmd, cmd, cmdlen);
509 		memcpy(finalcmd + cmdlen, rreq->data, rreq->len);
510 
511 		/* type 3 id 4: 22 00 34 03 23 00 04 00 04 03 */
512 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
513 		    finalcmd, cmdlen + rreq->len, NULL, 0, flags);
514 		kmem_free(finalcmd, cmdlen + rreq->len);
515 
516  		break;
517  	}
518 
519 	case I2C_HID_CMD_SET_POWER: {
520 		int power = *(int *)arg;
521 		uint8_t cmd[] = {
522 			htole16(sc->hid_desc.wCommandRegister) & 0xff,
523 			htole16(sc->hid_desc.wCommandRegister) >> 8,
524 			power,
525 			I2C_HID_CMD_SET_POWER,
526 		};
527 
528 		DPRINTF(("%s: HID command I2C_HID_CMD_SET_POWER(%d)\n",
529 		    sc->sc_dev.dv_xname, power));
530 
531 		/* 22 00 00 08 */
532 		res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
533 		    &cmd, sizeof(cmd), NULL, 0, flags);
534 
535 		break;
536 	}
537 	case I2C_HID_REPORT_DESCR: {
538 		uint8_t cmd[] = {
539 			htole16(sc->hid_desc.wReportDescRegister) & 0xff,
540 			htole16(sc->hid_desc.wReportDescRegister) >> 8,
541 		};
542 
543 		DPRINTF(("%s: HID command I2C_HID_REPORT_DESCR at 0x%x with "
544 		    "size %d\n", sc->sc_dev.dv_xname, cmd[0],
545 		    sc->sc_reportlen));
546 
547 		/* 20 00 */
548 		res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
549 		    &cmd, sizeof(cmd), sc->sc_report, sc->sc_reportlen, flags);
550 
551 		DPRINTF(("%s: HID report descriptor:", sc->sc_dev.dv_xname));
552 		for (i = 0; i < sc->sc_reportlen; i++)
553 			DPRINTF((" %.2x", sc->sc_report[i]));
554 		DPRINTF(("\n"));
555 
556 		break;
557 	}
558 	default:
559 		aprint_error_dev(sc->sc_dev, "unknown command %d\n",
560 		    hidcmd);
561 	}
562 
563 	iic_release_bus(sc->sc_tag, flags);
564 
565 	return (res);
566 }
567 
568 static int
569 ihidev_reset(struct ihidev_softc *sc, bool poll)
570 {
571 	DPRINTF(("%s: resetting\n", sc->sc_dev.dv_xname));
572 
573 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
574 	    &I2C_HID_POWER_ON, poll)) {
575 		aprint_error_dev(sc->sc_dev, "failed to power on\n");
576 		return (1);
577 	}
578 
579 	DELAY(1000);
580 
581 	if (ihidev_hid_command(sc, I2C_HID_CMD_RESET, 0, poll)) {
582 		aprint_error_dev(sc->sc_dev, "failed to reset hardware\n");
583 
584 		ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
585 		    &I2C_HID_POWER_OFF, poll);
586 
587 		return (1);
588 	}
589 
590 	DELAY(1000);
591 
592 	return (0);
593 }
594 
595 /*
596  * 5.2.2 - HID Descriptor Retrieval
597  *
598  * parse HID Descriptor that has already been read into hid_desc with
599  * I2C_HID_CMD_DESCR
600  */
601 static int
602 ihidev_hid_desc_parse(struct ihidev_softc *sc)
603 {
604 	int retries = 3;
605 
606 	/* must be v01.00 */
607 	if (le16toh(sc->hid_desc.bcdVersion) != 0x0100) {
608 		aprint_error_dev(sc->sc_dev,
609 		    "bad HID descriptor bcdVersion (0x%x)\n",
610 		    le16toh(sc->hid_desc.bcdVersion));
611 		return (1);
612 	}
613 
614 	/* must be 30 bytes for v1.00 */
615 	if (le16toh(sc->hid_desc.wHIDDescLength !=
616 	    sizeof(struct i2c_hid_desc))) {
617 		aprint_error_dev(sc->sc_dev,
618 		    "bad HID descriptor size (%d != %zu)\n",
619 		    le16toh(sc->hid_desc.wHIDDescLength),
620 		    sizeof(struct i2c_hid_desc));
621 		return (1);
622 	}
623 
624 	if (le16toh(sc->hid_desc.wReportDescLength) <= 0) {
625 		aprint_error_dev(sc->sc_dev,
626 		    "bad HID report descriptor size (%d)\n",
627 		    le16toh(sc->hid_desc.wReportDescLength));
628 		return (1);
629 	}
630 
631 	while (retries-- > 0) {
632 		if (ihidev_reset(sc, false)) {
633 			if (retries == 0)
634 				return(1);
635 
636 			DELAY(1000);
637 		}
638 		else
639 			break;
640 	}
641 
642 	sc->sc_reportlen = le16toh(sc->hid_desc.wReportDescLength);
643 	sc->sc_report = kmem_zalloc(sc->sc_reportlen, KM_NOSLEEP);
644 
645 	if (ihidev_hid_command(sc, I2C_HID_REPORT_DESCR, 0, false)) {
646 		aprint_error_dev(sc->sc_dev, "failed fetching HID report\n");
647 		return (1);
648 	}
649 
650 	return (0);
651 }
652 
653 static unsigned int
654 ihidev_intr(void *arg)
655 {
656 	struct ihidev_softc *sc = arg;
657 	struct ihidev *scd;
658 	u_int psize;
659 	int res, i;
660 	u_char *p;
661 	u_int rep = 0;
662 
663 	/*
664 	 * XXX: force I2C_F_POLL for now to avoid dwiic interrupting
665 	 * while we are interrupting
666 	 */
667 
668 	mutex_enter(&sc->sc_intr_lock);
669 	iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
670 
671 	res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, NULL, 0,
672 	    sc->sc_ibuf, sc->sc_isize, I2C_F_POLL);
673 
674 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
675 	mutex_exit(&sc->sc_intr_lock);
676 	if (res != 0)
677 		return 1;
678 
679 	/*
680 	 * 6.1.1 - First two bytes are the packet length, which must be less
681 	 * than or equal to wMaxInputLength
682 	 */
683 	psize = sc->sc_ibuf[0] | sc->sc_ibuf[1] << 8;
684 	if (!psize || psize > sc->sc_isize) {
685 		DPRINTF(("%s: %s: invalid packet size (%d vs. %d)\n",
686 		    sc->sc_dev.dv_xname, __func__, psize, sc->sc_isize));
687 		return (1);
688 	}
689 
690 	/* 3rd byte is the report id */
691 	p = sc->sc_ibuf + 2;
692 	psize -= 2;
693 	if (sc->sc_nrepid != 1)
694 		rep = *p++, psize--;
695 
696 	if (rep >= sc->sc_nrepid) {
697 		aprint_error_dev(sc->sc_dev, "%s: bad report id %d\n",
698 		    __func__, rep);
699 		return (1);
700 	}
701 
702 	DPRINTF(("%s: ihidev_intr: hid input (rep %d):", sc->sc_dev.dv_xname,
703 	    rep));
704 	for (i = 0; i < sc->sc_isize; i++)
705 		DPRINTF((" %.2x", sc->sc_ibuf[i]));
706 	DPRINTF(("\n"));
707 
708 	scd = sc->sc_subdevs[rep];
709 	if (scd == NULL || !(scd->sc_state & IHIDEV_OPEN))
710 		return (1);
711 
712 	scd->sc_intr(scd, p, psize);
713 
714 	return 1;
715 }
716 
717 static int
718 ihidev_maxrepid(void *buf, int len)
719 {
720 	struct hid_data *d;
721 	struct hid_item h;
722 	int maxid;
723 
724 	maxid = -1;
725 	h.report_ID = 0;
726 	for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
727 		if (h.report_ID > maxid)
728 			maxid = h.report_ID;
729 	hid_end_parse(d);
730 
731 	return (maxid);
732 }
733 
734 static int
735 ihidev_print(void *aux, const char *pnp)
736 {
737 	struct ihidev_attach_arg *iha = aux;
738 
739 	if (iha->reportid == IHIDEV_CLAIM_ALLREPORTID)
740 		return (QUIET);
741 
742 	if (pnp)
743 		aprint_normal("hid at %s", pnp);
744 
745 	if (iha->reportid != 0)
746 		aprint_normal(" reportid %d", iha->reportid);
747 
748 	return (UNCONF);
749 }
750 
751 static int
752 ihidev_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
753 {
754 	struct ihidev_attach_arg *iha = aux;
755 
756 	if (cf->ihidevcf_reportid != IHIDEV_UNK_REPORTID &&
757 	    cf->ihidevcf_reportid != iha->reportid)
758 		return (0);
759 
760 	return config_match(parent, cf, aux);
761 }
762 
763 int
764 ihidev_open(struct ihidev *scd)
765 {
766 	struct ihidev_softc *sc = scd->sc_parent;
767 
768 	DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname,
769 	    __func__, scd->sc_state, sc->sc_refcnt));
770 
771 	if (scd->sc_state & IHIDEV_OPEN)
772 		return (EBUSY);
773 
774 	scd->sc_state |= IHIDEV_OPEN;
775 
776 	if (sc->sc_refcnt++ || sc->sc_isize == 0)
777 		return (0);
778 
779 	/* power on */
780 	ihidev_reset(sc, false);
781 
782 	return (0);
783 }
784 
785 void
786 ihidev_close(struct ihidev *scd)
787 {
788 	struct ihidev_softc *sc = scd->sc_parent;
789 
790 	DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname,
791 	    __func__, scd->sc_state, sc->sc_refcnt));
792 
793 	if (!(scd->sc_state & IHIDEV_OPEN))
794 		return;
795 
796 	scd->sc_state &= ~IHIDEV_OPEN;
797 
798 	if (--sc->sc_refcnt)
799 		return;
800 
801 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
802 	    &I2C_HID_POWER_OFF, false))
803 		aprint_error_dev(sc->sc_dev, "failed to power down\n");
804 }
805 
806 void
807 ihidev_get_report_desc(struct ihidev_softc *sc, void **desc, int *size)
808 {
809 	*desc = sc->sc_report;
810 	*size = sc->sc_reportlen;
811 }
812 
813 /* convert hid_* constants used throughout HID code to i2c HID equivalents */
814 int
815 ihidev_report_type_conv(int hid_type_id)
816 {
817 	switch (hid_type_id) {
818 	case hid_input:
819 		return I2C_HID_REPORT_TYPE_INPUT;
820 	case hid_output:
821 		return I2C_HID_REPORT_TYPE_OUTPUT;
822 	case hid_feature:
823 		return I2C_HID_REPORT_TYPE_FEATURE;
824 	default:
825 		return -1;
826 	}
827 }
828 
829 int
830 ihidev_get_report(struct device *dev, int type, int id, void *data, int len)
831 {
832 	struct ihidev_softc *sc = (struct ihidev_softc *)dev;
833 	struct i2c_hid_report_request rreq;
834 	int ctype;
835 
836 	if ((ctype = ihidev_report_type_conv(type)) < 0)
837 		return (1);
838 
839 	rreq.type = ctype;
840 	rreq.id = id;
841 	rreq.data = data;
842 	rreq.len = len;
843 
844 	if (ihidev_hid_command(sc, I2C_HID_CMD_GET_REPORT, &rreq, false)) {
845 		aprint_error_dev(sc->sc_dev, "failed fetching report\n");
846 		return (1);
847 	}
848 
849 	return 0;
850 }
851 
852 int
853 ihidev_set_report(struct device *dev, int type, int id, void *data,
854     int len)
855 {
856 	struct ihidev_softc *sc = (struct ihidev_softc *)dev;
857 	struct i2c_hid_report_request rreq;
858 	int ctype;
859 
860 	if ((ctype = ihidev_report_type_conv(type)) < 0)
861 		return (1);
862 
863 	rreq.type = ctype;
864 	rreq.id = id;
865 	rreq.data = data;
866 	rreq.len = len;
867 
868 	if (ihidev_hid_command(sc, I2C_HID_CMD_SET_REPORT, &rreq, false)) {
869 		aprint_error_dev(sc->sc_dev, "failed setting report\n");
870 		return (1);
871 	}
872 
873 	return 0;
874 }
875