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