ihidev.c revision 1.31 1 /* $NetBSD: ihidev.c,v 1.31 2024/12/08 20:49:56 jmcneill 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.31 2024/12/08 20:49:56 jmcneill 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_reset(struct ihidev_softc *, bool);
128 static int ihidev_hid_desc_parse(struct ihidev_softc *);
129
130 static int ihidev_maxrepid(void *, int);
131 static int ihidev_print(void *, const char *);
132 static int ihidev_submatch(device_t, cfdata_t, const int *, void *);
133
134 static bool ihidev_acpi_get_info(struct ihidev_softc *);
135
136 static const struct device_compatible_entry compat_data[] = {
137 { .compat = "PNP0C50" },
138 { .compat = "ACPI0C50" },
139 { .compat = "hid-over-i2c" },
140 DEVICE_COMPAT_EOL
141 };
142
143 static int
144 ihidev_match(device_t parent, cfdata_t match, void *aux)
145 {
146 struct i2c_attach_args * const ia = aux;
147 int match_result;
148
149 if (iic_use_direct_match(ia, match, compat_data, &match_result))
150 return match_result;
151
152 return 0;
153 }
154
155 static void
156 ihidev_attach(device_t parent, device_t self, void *aux)
157 {
158 struct ihidev_softc *sc = device_private(self);
159 struct i2c_attach_args *ia = aux;
160 struct ihidev_attach_arg iha;
161 device_t dev;
162 int repid, repsz;
163 int isize;
164 int locs[IHIDBUSCF_NLOCS];
165
166 sc->sc_dev = self;
167 sc->sc_tag = ia->ia_tag;
168 sc->sc_addr = ia->ia_addr;
169 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
170
171 sc->sc_phandle = ia->ia_cookie;
172 if (ia->ia_cookietype != I2C_COOKIE_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_reset(struct ihidev_softc *sc, bool poll)
593 {
594 DPRINTF(("%s: resetting\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 if (ihidev_hid_command(sc, I2C_HID_CMD_RESET, 0, poll)) {
605 aprint_error_dev(sc->sc_dev, "failed to reset hardware\n");
606
607 ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
608 &I2C_HID_POWER_OFF, poll);
609
610 return (1);
611 }
612
613 DELAY(1000);
614
615 return (0);
616 }
617
618 /*
619 * 5.2.2 - HID Descriptor Retrieval
620 *
621 * parse HID Descriptor that has already been read into hid_desc with
622 * I2C_HID_CMD_DESCR
623 */
624 static int
625 ihidev_hid_desc_parse(struct ihidev_softc *sc)
626 {
627 int retries = 3;
628
629 /* must be v01.00 */
630 if (le16toh(sc->hid_desc.bcdVersion) != 0x0100) {
631 aprint_error_dev(sc->sc_dev,
632 "bad HID descriptor bcdVersion (0x%x)\n",
633 le16toh(sc->hid_desc.bcdVersion));
634 return (1);
635 }
636
637 /* must be 30 bytes for v1.00 */
638 if (le16toh(sc->hid_desc.wHIDDescLength !=
639 sizeof(struct i2c_hid_desc))) {
640 aprint_error_dev(sc->sc_dev,
641 "bad HID descriptor size (%d != %zu)\n",
642 le16toh(sc->hid_desc.wHIDDescLength),
643 sizeof(struct i2c_hid_desc));
644 return (1);
645 }
646
647 if (le16toh(sc->hid_desc.wReportDescLength) <= 0) {
648 aprint_error_dev(sc->sc_dev,
649 "bad HID report descriptor size (%d)\n",
650 le16toh(sc->hid_desc.wReportDescLength));
651 return (1);
652 }
653
654 while (retries-- > 0) {
655 if (ihidev_reset(sc, false)) {
656 if (retries == 0)
657 return(1);
658
659 DELAY(1000);
660 }
661 else
662 break;
663 }
664
665 sc->sc_reportlen = le16toh(sc->hid_desc.wReportDescLength);
666 sc->sc_report = kmem_zalloc(sc->sc_reportlen, KM_SLEEP);
667
668 if (ihidev_hid_command(sc, I2C_HID_REPORT_DESCR, 0, false)) {
669 aprint_error_dev(sc->sc_dev, "failed fetching HID report\n");
670 return (1);
671 }
672
673 return (0);
674 }
675
676 static bool
677 ihidev_intr_init(struct ihidev_softc *sc)
678 {
679 #if NACPICA > 0
680 ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle;
681 struct acpi_resources res;
682 ACPI_STATUS rv;
683 char buf[100];
684
685 rv = acpi_resource_parse(sc->sc_dev, hdl, "_CRS", &res,
686 &acpi_resource_parse_ops_quiet);
687 if (ACPI_FAILURE(rv)) {
688 aprint_error_dev(sc->sc_dev, "can't parse '_CRS'\n");
689 return false;
690 }
691
692 const struct acpi_irq * const irq = acpi_res_irq(&res, 0);
693 if (irq == NULL) {
694 aprint_debug_dev(sc->sc_dev, "no IRQ resource\n");
695 acpi_resource_cleanup(&res);
696 #if NGPIO > 0
697 goto try_gpioint;
698 #else
699 return false;
700 #endif
701 }
702
703 sc->sc_intr_type =
704 irq->ar_type == ACPI_EDGE_SENSITIVE ? IST_EDGE : IST_LEVEL;
705
706 acpi_resource_cleanup(&res);
707
708 sc->sc_ih = acpi_intr_establish(sc->sc_dev, sc->sc_phandle, IPL_TTY,
709 false, ihidev_intr, sc, device_xname(sc->sc_dev));
710 if (sc->sc_ih == NULL) {
711 aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
712 return false;
713 }
714 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n",
715 acpi_intr_string(sc->sc_ih, buf, sizeof(buf)));
716
717 #if NGPIO > 0
718 try_gpioint:
719 if (sc->sc_ih == NULL) {
720 int pin, irqmode, error;
721
722 rv = acpi_gpio_get_int(hdl, 0, &sc->sc_ih_gpio, &pin, &irqmode);
723 if (ACPI_FAILURE(rv)) {
724 aprint_error_dev(sc->sc_dev,
725 "can't find gpioint resource\n");
726 return false;
727 }
728 device_printf(sc->sc_dev, "[GPIO] got controller %p\n", sc->sc_ih_gpio);
729
730 sc->sc_ih_gpiomap.pm_map = sc->sc_ih_gpiopins;
731 error = gpio_pin_map(sc->sc_ih_gpio, pin, 1,
732 &sc->sc_ih_gpiomap);
733 if (error) {
734 aprint_error_dev(sc->sc_dev, "can't map pin %d\n", pin);
735 return false;
736 }
737
738 sc->sc_ih = gpio_intr_establish(sc->sc_ih_gpio,
739 &sc->sc_ih_gpiomap, 0, IPL_VM, irqmode, ihidev_intr, sc);
740 if (sc->sc_ih == NULL) {
741 aprint_error_dev(sc->sc_dev,
742 "can't establish gpio interrupt\n");
743 return false;
744 }
745
746 sc->sc_intr_type = (irqmode & GPIO_INTR_LEVEL_MASK) ?
747 IST_LEVEL : IST_EDGE;
748
749 gpio_intr_str(sc->sc_ih_gpio, &sc->sc_ih_gpiomap, 0,
750 irqmode, buf, sizeof(buf));
751 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", buf);
752 }
753 #endif
754
755 if (workqueue_create(&sc->sc_wq, device_xname(sc->sc_dev), ihidev_work,
756 sc, PRI_NONE, IPL_TTY, WQ_MPSAFE)) {
757 aprint_error_dev(sc->sc_dev,
758 "can't establish workqueue\n");
759 return false;
760 }
761 sc->sc_work_pending = 0;
762
763 return true;
764 #else
765 aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
766 return false;
767 #endif
768 }
769
770 static void
771 ihidev_intr_fini(struct ihidev_softc *sc)
772 {
773 #if NACPICA > 0
774 if (sc->sc_ih != NULL) {
775 if (sc->sc_ih_gpio != NULL) {
776 #if NGPIO > 0
777 gpio_intr_disestablish(sc->sc_ih_gpio, sc->sc_ih);
778 #endif
779 } else {
780 acpi_intr_disestablish(sc->sc_ih);
781 }
782 }
783 if (sc->sc_wq != NULL) {
784 workqueue_destroy(sc->sc_wq);
785 }
786 #endif
787 }
788
789 #if NACPICA > 0
790 static void
791 ihidev_intr_mask(struct ihidev_softc * const sc)
792 {
793
794 if (sc->sc_intr_type == IST_LEVEL) {
795 if (sc->sc_ih_gpio != NULL) {
796 #if NGPIO > 0
797 gpio_intr_mask(sc->sc_ih_gpio, sc->sc_ih);
798 #endif
799 } else {
800 acpi_intr_mask(sc->sc_ih);
801 }
802 }
803 }
804
805 static void
806 ihidev_intr_unmask(struct ihidev_softc * const sc)
807 {
808
809 if (sc->sc_intr_type == IST_LEVEL) {
810 if (sc->sc_ih_gpio != NULL) {
811 #if NGPIO > 0
812 gpio_intr_unmask(sc->sc_ih_gpio, sc->sc_ih);
813 #endif
814 } else {
815 acpi_intr_unmask(sc->sc_ih);
816 }
817 }
818 }
819
820 static int
821 ihidev_intr(void *arg)
822 {
823 struct ihidev_softc * const sc = arg;
824
825 /*
826 * Schedule our work. If we're using a level-triggered
827 * interrupt, we have to mask it off while we wait for service.
828 */
829 if (atomic_swap_uint(&sc->sc_work_pending, 1) == 0)
830 workqueue_enqueue(sc->sc_wq, &sc->sc_work, NULL);
831 ihidev_intr_mask(sc);
832
833 return 1;
834 }
835
836 static void
837 ihidev_work(struct work *wk, void *arg)
838 {
839 struct ihidev_softc * const sc = arg;
840 struct ihidev *scd;
841 u_int psize;
842 int res, i;
843 u_char *p;
844 u_int rep = 0;
845
846 atomic_store_relaxed(&sc->sc_work_pending, 0);
847
848 mutex_enter(&sc->sc_lock);
849
850 iic_acquire_bus(sc->sc_tag, 0);
851 res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, NULL, 0,
852 sc->sc_ibuf, sc->sc_isize, 0);
853 iic_release_bus(sc->sc_tag, 0);
854 if (res != 0)
855 goto out;
856
857 /*
858 * 6.1.1 - First two bytes are the packet length, which must be less
859 * than or equal to wMaxInputLength
860 */
861 psize = sc->sc_ibuf[0] | sc->sc_ibuf[1] << 8;
862 if (!psize || psize > sc->sc_isize) {
863 DPRINTF(("%s: %s: invalid packet size (%d vs. %d)\n",
864 device_xname(sc->sc_dev), __func__, psize, sc->sc_isize));
865 goto out;
866 }
867
868 /* 3rd byte is the report id */
869 p = sc->sc_ibuf + 2;
870 psize -= 2;
871 if (sc->sc_nrepid != 1)
872 rep = *p++, psize--;
873
874 if (rep >= sc->sc_nrepid) {
875 aprint_error_dev(sc->sc_dev, "%s: bad report id %d\n",
876 __func__, rep);
877 goto out;
878 }
879
880 DPRINTF(("%s: %s: hid input (rep %d):", device_xname(sc->sc_dev),
881 __func__, rep));
882 for (i = 0; i < sc->sc_isize; i++)
883 DPRINTF((" %.2x", sc->sc_ibuf[i]));
884 DPRINTF(("\n"));
885
886 scd = sc->sc_subdevs[rep];
887 if (scd == NULL || !(scd->sc_state & IHIDEV_OPEN))
888 goto out;
889
890 scd->sc_intr(scd, p, psize);
891
892 out:
893 mutex_exit(&sc->sc_lock);
894
895 /*
896 * If our interrupt is level-triggered, re-enable it now.
897 */
898 ihidev_intr_unmask(sc);
899 }
900 #endif
901
902 static int
903 ihidev_maxrepid(void *buf, int len)
904 {
905 struct hid_data *d;
906 struct hid_item h;
907 int maxid;
908
909 maxid = -1;
910 h.report_ID = 0;
911 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
912 if ((int)h.report_ID > maxid)
913 maxid = h.report_ID;
914 hid_end_parse(d);
915
916 return (maxid);
917 }
918
919 static int
920 ihidev_print(void *aux, const char *pnp)
921 {
922 struct ihidev_attach_arg *iha = aux;
923
924 if (iha->reportid == IHIDEV_CLAIM_ALLREPORTID)
925 return (QUIET);
926
927 if (pnp)
928 aprint_normal("hid at %s", pnp);
929
930 if (iha->reportid != 0)
931 aprint_normal(" reportid %d", iha->reportid);
932
933 return (UNCONF);
934 }
935
936 static int
937 ihidev_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
938 {
939 struct ihidev_attach_arg *iha = aux;
940
941 if (cf->ihidevcf_reportid != IHIDEV_UNK_REPORTID &&
942 cf->ihidevcf_reportid != iha->reportid)
943 return (0);
944
945 return config_match(parent, cf, aux);
946 }
947
948 int
949 ihidev_open(struct ihidev *scd)
950 {
951 struct ihidev_softc *sc = scd->sc_parent;
952 int error;
953
954 DPRINTF(("%s: %s: state=%d refcnt=%d\n", device_xname(sc->sc_dev),
955 __func__, scd->sc_state, sc->sc_refcnt));
956
957 mutex_enter(&sc->sc_lock);
958
959 if (scd->sc_state & IHIDEV_OPEN || sc->sc_refcnt == INT_MAX) {
960 error = EBUSY;
961 goto out;
962 }
963
964 scd->sc_state |= IHIDEV_OPEN;
965
966 if (sc->sc_refcnt++ || sc->sc_isize == 0) {
967 error = 0;
968 goto out;
969 }
970
971 /* power on */
972 ihidev_reset(sc, false);
973 error = 0;
974
975 out: mutex_exit(&sc->sc_lock);
976 return error;
977 }
978
979 void
980 ihidev_close(struct ihidev *scd)
981 {
982 struct ihidev_softc *sc = scd->sc_parent;
983
984 DPRINTF(("%s: %s: state=%d refcnt=%d\n", device_xname(sc->sc_dev),
985 __func__, scd->sc_state, sc->sc_refcnt));
986
987 mutex_enter(&sc->sc_lock);
988
989 KASSERTMSG(scd->sc_state & IHIDEV_OPEN,
990 "%s: closing %s when not open",
991 device_xname(scd->sc_idev),
992 device_xname(sc->sc_dev));
993 scd->sc_state &= ~IHIDEV_OPEN;
994
995 if (--sc->sc_refcnt)
996 goto out;
997
998 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
999 &I2C_HID_POWER_OFF, false))
1000 aprint_error_dev(sc->sc_dev, "failed to power down\n");
1001
1002 out: mutex_exit(&sc->sc_lock);
1003 }
1004
1005 void
1006 ihidev_get_report_desc(struct ihidev_softc *sc, void **desc, int *size)
1007 {
1008 *desc = sc->sc_report;
1009 *size = sc->sc_reportlen;
1010 }
1011
1012 /* convert hid_* constants used throughout HID code to i2c HID equivalents */
1013 int
1014 ihidev_report_type_conv(int hid_type_id)
1015 {
1016 switch (hid_type_id) {
1017 case hid_input:
1018 return I2C_HID_REPORT_TYPE_INPUT;
1019 case hid_output:
1020 return I2C_HID_REPORT_TYPE_OUTPUT;
1021 case hid_feature:
1022 return I2C_HID_REPORT_TYPE_FEATURE;
1023 default:
1024 return -1;
1025 }
1026 }
1027
1028 int
1029 ihidev_get_report(struct device *dev, int type, int id, void *data, int len)
1030 {
1031 struct ihidev_softc *sc = (struct ihidev_softc *)dev;
1032 struct i2c_hid_report_request rreq;
1033 int ctype;
1034
1035 if ((ctype = ihidev_report_type_conv(type)) < 0)
1036 return (1);
1037
1038 rreq.type = ctype;
1039 rreq.id = id;
1040 rreq.data = data;
1041 rreq.len = len;
1042
1043 if (ihidev_hid_command(sc, I2C_HID_CMD_GET_REPORT, &rreq, false)) {
1044 aprint_error_dev(sc->sc_dev, "failed fetching report\n");
1045 return (1);
1046 }
1047
1048 return 0;
1049 }
1050
1051 int
1052 ihidev_set_report(struct device *dev, int type, int id, void *data,
1053 int len)
1054 {
1055 struct ihidev_softc *sc = (struct ihidev_softc *)dev;
1056 struct i2c_hid_report_request rreq;
1057 int ctype;
1058
1059 if ((ctype = ihidev_report_type_conv(type)) < 0)
1060 return (1);
1061
1062 rreq.type = ctype;
1063 rreq.id = id;
1064 rreq.data = data;
1065 rreq.len = len;
1066
1067 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_REPORT, &rreq, false)) {
1068 aprint_error_dev(sc->sc_dev, "failed setting report\n");
1069 return (1);
1070 }
1071
1072 return 0;
1073 }
1074
1075 static bool
1076 ihidev_acpi_get_info(struct ihidev_softc *sc)
1077 {
1078 #if NACPICA > 0
1079 ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle;
1080 ACPI_STATUS status;
1081 ACPI_INTEGER val;
1082
1083 /* 3cdff6f7-4267-4555-ad05-b30a3d8938de */
1084 uint8_t i2c_hid_guid[] = {
1085 0xF7, 0xF6, 0xDF, 0x3C,
1086 0x67, 0x42,
1087 0x55, 0x45,
1088 0xAD, 0x05,
1089 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
1090 };
1091
1092 status = acpi_dsm_integer(hdl, i2c_hid_guid, 1, 1, NULL, &val);
1093 if (ACPI_FAILURE(status)) {
1094 aprint_error_dev(sc->sc_dev,
1095 "failed to get HidDescriptorAddress: %s\n",
1096 AcpiFormatException(status));
1097 return false;
1098 }
1099
1100 sc->sc_hid_desc_addr = (u_int)val;
1101
1102 return true;
1103 #else
1104 return false;
1105 #endif
1106 }
1107