ihidev.c revision 1.32 1 /* $NetBSD: ihidev.c,v 1.32 2024/12/09 01:17:30 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.32 2024/12/09 01:17:30 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
729 sc->sc_ih_gpiomap.pm_map = sc->sc_ih_gpiopins;
730 error = gpio_pin_map(sc->sc_ih_gpio, pin, 1,
731 &sc->sc_ih_gpiomap);
732 if (error) {
733 aprint_error_dev(sc->sc_dev, "can't map pin %d\n", pin);
734 return false;
735 }
736
737 sc->sc_ih = gpio_intr_establish(sc->sc_ih_gpio,
738 &sc->sc_ih_gpiomap, 0, IPL_VM, irqmode, ihidev_intr, sc);
739 if (sc->sc_ih == NULL) {
740 aprint_error_dev(sc->sc_dev,
741 "can't establish gpio interrupt\n");
742 return false;
743 }
744
745 sc->sc_intr_type = (irqmode & GPIO_INTR_LEVEL_MASK) ?
746 IST_LEVEL : IST_EDGE;
747
748 gpio_intr_str(sc->sc_ih_gpio, &sc->sc_ih_gpiomap, 0,
749 irqmode, buf, sizeof(buf));
750 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", buf);
751 }
752 #endif
753
754 if (workqueue_create(&sc->sc_wq, device_xname(sc->sc_dev), ihidev_work,
755 sc, PRI_NONE, IPL_TTY, WQ_MPSAFE)) {
756 aprint_error_dev(sc->sc_dev,
757 "can't establish workqueue\n");
758 return false;
759 }
760 sc->sc_work_pending = 0;
761
762 return true;
763 #else
764 aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
765 return false;
766 #endif
767 }
768
769 static void
770 ihidev_intr_fini(struct ihidev_softc *sc)
771 {
772 #if NACPICA > 0
773 if (sc->sc_ih != NULL) {
774 if (sc->sc_ih_gpio != NULL) {
775 #if NGPIO > 0
776 gpio_intr_disestablish(sc->sc_ih_gpio, sc->sc_ih);
777 #endif
778 } else {
779 acpi_intr_disestablish(sc->sc_ih);
780 }
781 }
782 if (sc->sc_wq != NULL) {
783 workqueue_destroy(sc->sc_wq);
784 }
785 #endif
786 }
787
788 #if NACPICA > 0
789 static void
790 ihidev_intr_mask(struct ihidev_softc * const sc)
791 {
792
793 if (sc->sc_intr_type == IST_LEVEL) {
794 if (sc->sc_ih_gpio != NULL) {
795 #if NGPIO > 0
796 gpio_intr_mask(sc->sc_ih_gpio, sc->sc_ih);
797 #endif
798 } else {
799 acpi_intr_mask(sc->sc_ih);
800 }
801 }
802 }
803
804 static void
805 ihidev_intr_unmask(struct ihidev_softc * const sc)
806 {
807
808 if (sc->sc_intr_type == IST_LEVEL) {
809 if (sc->sc_ih_gpio != NULL) {
810 #if NGPIO > 0
811 gpio_intr_unmask(sc->sc_ih_gpio, sc->sc_ih);
812 #endif
813 } else {
814 acpi_intr_unmask(sc->sc_ih);
815 }
816 }
817 }
818
819 static int
820 ihidev_intr(void *arg)
821 {
822 struct ihidev_softc * const sc = arg;
823
824 /*
825 * Schedule our work. If we're using a level-triggered
826 * interrupt, we have to mask it off while we wait for service.
827 */
828 if (atomic_swap_uint(&sc->sc_work_pending, 1) == 0)
829 workqueue_enqueue(sc->sc_wq, &sc->sc_work, NULL);
830 ihidev_intr_mask(sc);
831
832 return 1;
833 }
834
835 static void
836 ihidev_work(struct work *wk, void *arg)
837 {
838 struct ihidev_softc * const sc = arg;
839 struct ihidev *scd;
840 u_int psize;
841 int res, i;
842 u_char *p;
843 u_int rep = 0;
844
845 atomic_store_relaxed(&sc->sc_work_pending, 0);
846
847 mutex_enter(&sc->sc_lock);
848
849 iic_acquire_bus(sc->sc_tag, 0);
850 res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, NULL, 0,
851 sc->sc_ibuf, sc->sc_isize, 0);
852 iic_release_bus(sc->sc_tag, 0);
853 if (res != 0)
854 goto out;
855
856 /*
857 * 6.1.1 - First two bytes are the packet length, which must be less
858 * than or equal to wMaxInputLength
859 */
860 psize = sc->sc_ibuf[0] | sc->sc_ibuf[1] << 8;
861 if (!psize || psize > sc->sc_isize) {
862 DPRINTF(("%s: %s: invalid packet size (%d vs. %d)\n",
863 device_xname(sc->sc_dev), __func__, psize, sc->sc_isize));
864 goto out;
865 }
866
867 /* 3rd byte is the report id */
868 p = sc->sc_ibuf + 2;
869 psize -= 2;
870 if (sc->sc_nrepid != 1)
871 rep = *p++, psize--;
872
873 if (rep >= sc->sc_nrepid) {
874 aprint_error_dev(sc->sc_dev, "%s: bad report id %d\n",
875 __func__, rep);
876 goto out;
877 }
878
879 DPRINTF(("%s: %s: hid input (rep %d):", device_xname(sc->sc_dev),
880 __func__, rep));
881 for (i = 0; i < sc->sc_isize; i++)
882 DPRINTF((" %.2x", sc->sc_ibuf[i]));
883 DPRINTF(("\n"));
884
885 scd = sc->sc_subdevs[rep];
886 if (scd == NULL || !(scd->sc_state & IHIDEV_OPEN))
887 goto out;
888
889 scd->sc_intr(scd, p, psize);
890
891 out:
892 mutex_exit(&sc->sc_lock);
893
894 /*
895 * If our interrupt is level-triggered, re-enable it now.
896 */
897 ihidev_intr_unmask(sc);
898 }
899 #endif
900
901 static int
902 ihidev_maxrepid(void *buf, int len)
903 {
904 struct hid_data *d;
905 struct hid_item h;
906 int maxid;
907
908 maxid = -1;
909 h.report_ID = 0;
910 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
911 if ((int)h.report_ID > maxid)
912 maxid = h.report_ID;
913 hid_end_parse(d);
914
915 return (maxid);
916 }
917
918 static int
919 ihidev_print(void *aux, const char *pnp)
920 {
921 struct ihidev_attach_arg *iha = aux;
922
923 if (iha->reportid == IHIDEV_CLAIM_ALLREPORTID)
924 return (QUIET);
925
926 if (pnp)
927 aprint_normal("hid at %s", pnp);
928
929 if (iha->reportid != 0)
930 aprint_normal(" reportid %d", iha->reportid);
931
932 return (UNCONF);
933 }
934
935 static int
936 ihidev_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
937 {
938 struct ihidev_attach_arg *iha = aux;
939
940 if (cf->ihidevcf_reportid != IHIDEV_UNK_REPORTID &&
941 cf->ihidevcf_reportid != iha->reportid)
942 return (0);
943
944 return config_match(parent, cf, aux);
945 }
946
947 int
948 ihidev_open(struct ihidev *scd)
949 {
950 struct ihidev_softc *sc = scd->sc_parent;
951 int error;
952
953 DPRINTF(("%s: %s: state=%d refcnt=%d\n", device_xname(sc->sc_dev),
954 __func__, scd->sc_state, sc->sc_refcnt));
955
956 mutex_enter(&sc->sc_lock);
957
958 if (scd->sc_state & IHIDEV_OPEN || sc->sc_refcnt == INT_MAX) {
959 error = EBUSY;
960 goto out;
961 }
962
963 scd->sc_state |= IHIDEV_OPEN;
964
965 if (sc->sc_refcnt++ || sc->sc_isize == 0) {
966 error = 0;
967 goto out;
968 }
969
970 /* power on */
971 ihidev_reset(sc, false);
972 error = 0;
973
974 out: mutex_exit(&sc->sc_lock);
975 return error;
976 }
977
978 void
979 ihidev_close(struct ihidev *scd)
980 {
981 struct ihidev_softc *sc = scd->sc_parent;
982
983 DPRINTF(("%s: %s: state=%d refcnt=%d\n", device_xname(sc->sc_dev),
984 __func__, scd->sc_state, sc->sc_refcnt));
985
986 mutex_enter(&sc->sc_lock);
987
988 KASSERTMSG(scd->sc_state & IHIDEV_OPEN,
989 "%s: closing %s when not open",
990 device_xname(scd->sc_idev),
991 device_xname(sc->sc_dev));
992 scd->sc_state &= ~IHIDEV_OPEN;
993
994 if (--sc->sc_refcnt)
995 goto out;
996
997 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
998 &I2C_HID_POWER_OFF, false))
999 aprint_error_dev(sc->sc_dev, "failed to power down\n");
1000
1001 out: mutex_exit(&sc->sc_lock);
1002 }
1003
1004 void
1005 ihidev_get_report_desc(struct ihidev_softc *sc, void **desc, int *size)
1006 {
1007 *desc = sc->sc_report;
1008 *size = sc->sc_reportlen;
1009 }
1010
1011 /* convert hid_* constants used throughout HID code to i2c HID equivalents */
1012 int
1013 ihidev_report_type_conv(int hid_type_id)
1014 {
1015 switch (hid_type_id) {
1016 case hid_input:
1017 return I2C_HID_REPORT_TYPE_INPUT;
1018 case hid_output:
1019 return I2C_HID_REPORT_TYPE_OUTPUT;
1020 case hid_feature:
1021 return I2C_HID_REPORT_TYPE_FEATURE;
1022 default:
1023 return -1;
1024 }
1025 }
1026
1027 int
1028 ihidev_get_report(struct device *dev, int type, int id, void *data, int len)
1029 {
1030 struct ihidev_softc *sc = (struct ihidev_softc *)dev;
1031 struct i2c_hid_report_request rreq;
1032 int ctype;
1033
1034 if ((ctype = ihidev_report_type_conv(type)) < 0)
1035 return (1);
1036
1037 rreq.type = ctype;
1038 rreq.id = id;
1039 rreq.data = data;
1040 rreq.len = len;
1041
1042 if (ihidev_hid_command(sc, I2C_HID_CMD_GET_REPORT, &rreq, false)) {
1043 aprint_error_dev(sc->sc_dev, "failed fetching report\n");
1044 return (1);
1045 }
1046
1047 return 0;
1048 }
1049
1050 int
1051 ihidev_set_report(struct device *dev, int type, int id, void *data,
1052 int len)
1053 {
1054 struct ihidev_softc *sc = (struct ihidev_softc *)dev;
1055 struct i2c_hid_report_request rreq;
1056 int ctype;
1057
1058 if ((ctype = ihidev_report_type_conv(type)) < 0)
1059 return (1);
1060
1061 rreq.type = ctype;
1062 rreq.id = id;
1063 rreq.data = data;
1064 rreq.len = len;
1065
1066 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_REPORT, &rreq, false)) {
1067 aprint_error_dev(sc->sc_dev, "failed setting report\n");
1068 return (1);
1069 }
1070
1071 return 0;
1072 }
1073
1074 static bool
1075 ihidev_acpi_get_info(struct ihidev_softc *sc)
1076 {
1077 #if NACPICA > 0
1078 ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle;
1079 ACPI_STATUS status;
1080 ACPI_INTEGER val;
1081
1082 /* 3cdff6f7-4267-4555-ad05-b30a3d8938de */
1083 uint8_t i2c_hid_guid[] = {
1084 0xF7, 0xF6, 0xDF, 0x3C,
1085 0x67, 0x42,
1086 0x55, 0x45,
1087 0xAD, 0x05,
1088 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
1089 };
1090
1091 status = acpi_dsm_integer(hdl, i2c_hid_guid, 1, 1, NULL, &val);
1092 if (ACPI_FAILURE(status)) {
1093 aprint_error_dev(sc->sc_dev,
1094 "failed to get HidDescriptorAddress: %s\n",
1095 AcpiFormatException(status));
1096 return false;
1097 }
1098
1099 sc->sc_hid_desc_addr = (u_int)val;
1100
1101 return true;
1102 #else
1103 return false;
1104 #endif
1105 }
1106