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