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