ihidev.c revision 1.18.2.1 1 /* $NetBSD: ihidev.c,v 1.18.2.1 2021/03/22 02:01:00 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 <sys/cdefs.h>
57 __KERNEL_RCSID(0, "$NetBSD: ihidev.c,v 1.18.2.1 2021/03/22 02:01:00 thorpej 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/acpivar.h>
75 #include <dev/acpi/acpi_intr.h>
76 #endif
77
78 #include "locators.h"
79
80 /* #define IHIDEV_DEBUG */
81
82 #ifdef IHIDEV_DEBUG
83 #define DPRINTF(x) printf x
84 #else
85 #define DPRINTF(x)
86 #endif
87
88 /* 7.2 */
89 enum {
90 I2C_HID_CMD_DESCR = 0x0,
91 I2C_HID_CMD_RESET = 0x1,
92 I2C_HID_CMD_GET_REPORT = 0x2,
93 I2C_HID_CMD_SET_REPORT = 0x3,
94 I2C_HID_CMD_GET_IDLE = 0x4,
95 I2C_HID_CMD_SET_IDLE = 0x5,
96 I2C_HID_CMD_GET_PROTO = 0x6,
97 I2C_HID_CMD_SET_PROTO = 0x7,
98 I2C_HID_CMD_SET_POWER = 0x8,
99
100 /* pseudo commands */
101 I2C_HID_REPORT_DESCR = 0x100,
102 };
103
104 static int I2C_HID_POWER_ON = 0x0;
105 static int I2C_HID_POWER_OFF = 0x1;
106
107 static int ihidev_match(device_t, cfdata_t, void *);
108 static void ihidev_attach(device_t, device_t, void *);
109 static int ihidev_detach(device_t, int);
110 CFATTACH_DECL_NEW(ihidev, sizeof(struct ihidev_softc),
111 ihidev_match, ihidev_attach, ihidev_detach, NULL);
112
113 static bool ihiddev_intr_init(struct ihidev_softc *);
114 static void ihiddev_intr_fini(struct ihidev_softc *);
115
116 static bool ihidev_suspend(device_t, const pmf_qual_t *);
117 static bool ihidev_resume(device_t, const pmf_qual_t *);
118 static int ihidev_hid_command(struct ihidev_softc *, int, void *, bool);
119 static int ihidev_intr(void *);
120 static void ihidev_softintr(void *);
121 static int ihidev_reset(struct ihidev_softc *, bool);
122 static int ihidev_hid_desc_parse(struct ihidev_softc *);
123
124 static int ihidev_maxrepid(void *, int);
125 static int ihidev_print(void *, const char *);
126 static int ihidev_submatch(device_t, cfdata_t, const int *, void *);
127
128 static bool ihidev_acpi_get_info(struct ihidev_softc *);
129
130 static const struct device_compatible_entry compat_data[] = {
131 { .compat = "PNP0C50" },
132 { .compat = "ACPI0C50" },
133 { .compat = "hid-over-i2c" },
134 DEVICE_COMPAT_EOL
135 };
136
137 static int
138 ihidev_match(device_t parent, cfdata_t match, void *aux)
139 {
140 struct i2c_attach_args * const ia = aux;
141 int match_result;
142
143 if (iic_use_direct_match(ia, match, compat_data, &match_result))
144 return I2C_MATCH_DIRECT_COMPATIBLE;
145
146 return 0;
147 }
148
149 static void
150 ihidev_attach(device_t parent, device_t self, void *aux)
151 {
152 struct ihidev_softc *sc = device_private(self);
153 struct i2c_attach_args *ia = aux;
154 struct ihidev_attach_arg iha;
155 device_t dev;
156 int repid, repsz;
157 int isize;
158 int locs[IHIDBUSCF_NLOCS];
159
160 sc->sc_dev = self;
161 sc->sc_tag = ia->ia_tag;
162 sc->sc_addr = ia->ia_addr;
163 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
164
165 sc->sc_phandle = ia->ia_cookie;
166 if (ia->ia_cookietype != I2C_COOKIE_ACPI) {
167 aprint_error(": unsupported device tree type\n");
168 return;
169 }
170 if (! ihidev_acpi_get_info(sc)) {
171 return;
172 }
173
174 if (ihidev_hid_command(sc, I2C_HID_CMD_DESCR, NULL, false) ||
175 ihidev_hid_desc_parse(sc)) {
176 aprint_error(": failed fetching initial HID descriptor\n");
177 return;
178 }
179
180 aprint_naive("\n");
181 aprint_normal(": vendor 0x%x product 0x%x, %s\n",
182 le16toh(sc->hid_desc.wVendorID), le16toh(sc->hid_desc.wProductID),
183 ia->ia_name);
184
185 sc->sc_nrepid = ihidev_maxrepid(sc->sc_report, sc->sc_reportlen);
186 if (sc->sc_nrepid < 0)
187 return;
188
189 aprint_normal_dev(self, "%d report id%s\n", sc->sc_nrepid,
190 sc->sc_nrepid > 1 ? "s" : "");
191
192 sc->sc_nrepid++;
193 sc->sc_subdevs = kmem_zalloc(sc->sc_nrepid * sizeof(struct ihidev *),
194 KM_SLEEP);
195
196 /* find largest report size and allocate memory for input buffer */
197 sc->sc_isize = le16toh(sc->hid_desc.wMaxInputLength);
198 for (repid = 0; repid < sc->sc_nrepid; repid++) {
199 repsz = hid_report_size(sc->sc_report, sc->sc_reportlen,
200 hid_input, repid);
201
202 isize = repsz + 2; /* two bytes for the length */
203 isize += (sc->sc_nrepid != 1); /* one byte for the report ID */
204 if (isize > sc->sc_isize)
205 sc->sc_isize = isize;
206
207 DPRINTF(("%s: repid %d size %d\n", sc->sc_dev.dv_xname, repid,
208 repsz));
209 }
210 sc->sc_ibuf = kmem_zalloc(sc->sc_isize, KM_SLEEP);
211 if (! ihiddev_intr_init(sc)) {
212 return;
213 }
214
215 iha.iaa = ia;
216 iha.parent = sc;
217
218 /* Look for a driver claiming all report IDs first. */
219 iha.reportid = IHIDEV_CLAIM_ALLREPORTID;
220 locs[IHIDBUSCF_REPORTID] = IHIDEV_CLAIM_ALLREPORTID;
221 dev = config_found(self, &iha, ihidev_print,
222 CFARG_SUBMATCH, ihidev_submatch,
223 CFARG_IATTR, "ihidbus",
224 CFARG_LOCATORS, locs,
225 CFARG_EOL);
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(self, &iha, ihidev_print,
244 CFARG_SUBMATCH, ihidev_submatch,
245 CFARG_IATTR, "ihidbus",
246 CFARG_LOCATORS, locs,
247 CFARG_EOL);
248 sc->sc_subdevs[repid] = device_private(dev);
249 }
250
251 /* power down until we're opened */
252 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, &I2C_HID_POWER_OFF, false)) {
253 aprint_error_dev(sc->sc_dev, "failed to power down\n");
254 return;
255 }
256 if (!pmf_device_register(self, ihidev_suspend, ihidev_resume))
257 aprint_error_dev(self, "couldn't establish power handler\n");
258 }
259
260 static int
261 ihidev_detach(device_t self, int flags)
262 {
263 struct ihidev_softc *sc = device_private(self);
264
265 mutex_enter(&sc->sc_intr_lock);
266 ihiddev_intr_fini(sc);
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 bool
653 ihiddev_intr_init(struct ihidev_softc *sc)
654 {
655 #if NACPICA > 0
656 ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle;
657 struct acpi_resources res;
658 ACPI_STATUS rv;
659 char buf[100];
660
661 rv = acpi_resource_parse(sc->sc_dev, hdl, "_CRS", &res,
662 &acpi_resource_parse_ops_quiet);
663 if (ACPI_FAILURE(rv)) {
664 aprint_error_dev(sc->sc_dev, "can't parse '_CRS'\n");
665 return false;
666 }
667
668 const struct acpi_irq * const irq = acpi_res_irq(&res, 0);
669 if (irq == NULL) {
670 aprint_error_dev(sc->sc_dev, "no IRQ resource\n");
671 acpi_resource_cleanup(&res);
672 return false;
673 }
674
675 sc->sc_intr_type =
676 irq->ar_type == ACPI_EDGE_SENSITIVE ? IST_EDGE : IST_LEVEL;
677
678 acpi_resource_cleanup(&res);
679
680 sc->sc_ih = acpi_intr_establish(sc->sc_dev, sc->sc_phandle, IPL_TTY,
681 false, ihidev_intr, sc, device_xname(sc->sc_dev));
682 if (sc->sc_ih == NULL) {
683 aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
684 return false;
685 }
686 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n",
687 acpi_intr_string(sc->sc_ih, buf, sizeof(buf)));
688
689 sc->sc_sih = softint_establish(SOFTINT_SERIAL, ihidev_softintr, sc);
690 if (sc->sc_sih == NULL) {
691 aprint_error_dev(sc->sc_dev,
692 "can't establish soft interrupt\n");
693 return false;
694 }
695
696 return true;
697 #else
698 aprint_error_dev(sc->sc_dev, "can't establish interrupt\n");
699 return false;
700 #endif
701 }
702
703 static void
704 ihiddev_intr_fini(struct ihidev_softc *sc)
705 {
706 #if NACPICA > 0
707 if (sc->sc_ih != NULL) {
708 acpi_intr_disestablish(sc->sc_ih);
709 }
710 if (sc->sc_sih != NULL) {
711 softint_disestablish(sc->sc_sih);
712 }
713 #endif
714 }
715
716 static void
717 ihidev_intr_mask(struct ihidev_softc * const sc)
718 {
719
720 if (sc->sc_intr_type == IST_LEVEL) {
721 #if NACPICA > 0
722 acpi_intr_mask(sc->sc_ih);
723 #endif
724 }
725 }
726
727 static void
728 ihidev_intr_unmask(struct ihidev_softc * const sc)
729 {
730
731 if (sc->sc_intr_type == IST_LEVEL) {
732 #if NACPICA > 0
733 acpi_intr_unmask(sc->sc_ih);
734 #endif
735 }
736 }
737
738 static int
739 ihidev_intr(void *arg)
740 {
741 struct ihidev_softc * const sc = arg;
742
743 mutex_enter(&sc->sc_intr_lock);
744
745 /*
746 * Schedule our soft interrupt handler. If we're using a level-
747 * triggered interrupt, we have to mask it off while we wait
748 * for service.
749 */
750 softint_schedule(sc->sc_sih);
751 ihidev_intr_mask(sc);
752
753 mutex_exit(&sc->sc_intr_lock);
754
755 return 1;
756 }
757
758 static void
759 ihidev_softintr(void *arg)
760 {
761 struct ihidev_softc * const sc = arg;
762 struct ihidev *scd;
763 u_int psize;
764 int res, i;
765 u_char *p;
766 u_int rep = 0;
767
768 mutex_enter(&sc->sc_intr_lock);
769 iic_acquire_bus(sc->sc_tag, 0);
770 res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, NULL, 0,
771 sc->sc_ibuf, sc->sc_isize, 0);
772 iic_release_bus(sc->sc_tag, 0);
773 mutex_exit(&sc->sc_intr_lock);
774
775 if (res != 0)
776 goto out;
777
778 /*
779 * 6.1.1 - First two bytes are the packet length, which must be less
780 * than or equal to wMaxInputLength
781 */
782 psize = sc->sc_ibuf[0] | sc->sc_ibuf[1] << 8;
783 if (!psize || psize > sc->sc_isize) {
784 DPRINTF(("%s: %s: invalid packet size (%d vs. %d)\n",
785 sc->sc_dev.dv_xname, __func__, psize, sc->sc_isize));
786 goto out;
787 }
788
789 /* 3rd byte is the report id */
790 p = sc->sc_ibuf + 2;
791 psize -= 2;
792 if (sc->sc_nrepid != 1)
793 rep = *p++, psize--;
794
795 if (rep >= sc->sc_nrepid) {
796 aprint_error_dev(sc->sc_dev, "%s: bad report id %d\n",
797 __func__, rep);
798 goto out;
799 }
800
801 DPRINTF(("%s: %s: hid input (rep %d):", sc->sc_dev.dv_xname,
802 __func__, rep));
803 for (i = 0; i < sc->sc_isize; i++)
804 DPRINTF((" %.2x", sc->sc_ibuf[i]));
805 DPRINTF(("\n"));
806
807 scd = sc->sc_subdevs[rep];
808 if (scd == NULL || !(scd->sc_state & IHIDEV_OPEN))
809 goto out;
810
811 scd->sc_intr(scd, p, psize);
812
813 out:
814 /*
815 * If our interrupt is level-triggered, re-enable it now.
816 */
817 ihidev_intr_unmask(sc);
818 }
819
820 static int
821 ihidev_maxrepid(void *buf, int len)
822 {
823 struct hid_data *d;
824 struct hid_item h;
825 int maxid;
826
827 maxid = -1;
828 h.report_ID = 0;
829 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); )
830 if ((int)h.report_ID > maxid)
831 maxid = h.report_ID;
832 hid_end_parse(d);
833
834 return (maxid);
835 }
836
837 static int
838 ihidev_print(void *aux, const char *pnp)
839 {
840 struct ihidev_attach_arg *iha = aux;
841
842 if (iha->reportid == IHIDEV_CLAIM_ALLREPORTID)
843 return (QUIET);
844
845 if (pnp)
846 aprint_normal("hid at %s", pnp);
847
848 if (iha->reportid != 0)
849 aprint_normal(" reportid %d", iha->reportid);
850
851 return (UNCONF);
852 }
853
854 static int
855 ihidev_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
856 {
857 struct ihidev_attach_arg *iha = aux;
858
859 if (cf->ihidevcf_reportid != IHIDEV_UNK_REPORTID &&
860 cf->ihidevcf_reportid != iha->reportid)
861 return (0);
862
863 return config_match(parent, cf, aux);
864 }
865
866 int
867 ihidev_open(struct ihidev *scd)
868 {
869 struct ihidev_softc *sc = scd->sc_parent;
870
871 DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname,
872 __func__, scd->sc_state, sc->sc_refcnt));
873
874 if (scd->sc_state & IHIDEV_OPEN)
875 return (EBUSY);
876
877 scd->sc_state |= IHIDEV_OPEN;
878
879 if (sc->sc_refcnt++ || sc->sc_isize == 0)
880 return (0);
881
882 /* power on */
883 ihidev_reset(sc, false);
884
885 return (0);
886 }
887
888 void
889 ihidev_close(struct ihidev *scd)
890 {
891 struct ihidev_softc *sc = scd->sc_parent;
892
893 DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname,
894 __func__, scd->sc_state, sc->sc_refcnt));
895
896 if (!(scd->sc_state & IHIDEV_OPEN))
897 return;
898
899 scd->sc_state &= ~IHIDEV_OPEN;
900
901 if (--sc->sc_refcnt)
902 return;
903
904 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER,
905 &I2C_HID_POWER_OFF, false))
906 aprint_error_dev(sc->sc_dev, "failed to power down\n");
907 }
908
909 void
910 ihidev_get_report_desc(struct ihidev_softc *sc, void **desc, int *size)
911 {
912 *desc = sc->sc_report;
913 *size = sc->sc_reportlen;
914 }
915
916 /* convert hid_* constants used throughout HID code to i2c HID equivalents */
917 int
918 ihidev_report_type_conv(int hid_type_id)
919 {
920 switch (hid_type_id) {
921 case hid_input:
922 return I2C_HID_REPORT_TYPE_INPUT;
923 case hid_output:
924 return I2C_HID_REPORT_TYPE_OUTPUT;
925 case hid_feature:
926 return I2C_HID_REPORT_TYPE_FEATURE;
927 default:
928 return -1;
929 }
930 }
931
932 int
933 ihidev_get_report(struct device *dev, int type, int id, void *data, int len)
934 {
935 struct ihidev_softc *sc = (struct ihidev_softc *)dev;
936 struct i2c_hid_report_request rreq;
937 int ctype;
938
939 if ((ctype = ihidev_report_type_conv(type)) < 0)
940 return (1);
941
942 rreq.type = ctype;
943 rreq.id = id;
944 rreq.data = data;
945 rreq.len = len;
946
947 if (ihidev_hid_command(sc, I2C_HID_CMD_GET_REPORT, &rreq, false)) {
948 aprint_error_dev(sc->sc_dev, "failed fetching report\n");
949 return (1);
950 }
951
952 return 0;
953 }
954
955 int
956 ihidev_set_report(struct device *dev, int type, int id, void *data,
957 int len)
958 {
959 struct ihidev_softc *sc = (struct ihidev_softc *)dev;
960 struct i2c_hid_report_request rreq;
961 int ctype;
962
963 if ((ctype = ihidev_report_type_conv(type)) < 0)
964 return (1);
965
966 rreq.type = ctype;
967 rreq.id = id;
968 rreq.data = data;
969 rreq.len = len;
970
971 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_REPORT, &rreq, false)) {
972 aprint_error_dev(sc->sc_dev, "failed setting report\n");
973 return (1);
974 }
975
976 return 0;
977 }
978
979 static bool
980 ihidev_acpi_get_info(struct ihidev_softc *sc)
981 {
982 ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle;
983 ACPI_STATUS status;
984 ACPI_INTEGER val;
985
986 /* 3cdff6f7-4267-4555-ad05-b30a3d8938de */
987 uint8_t i2c_hid_guid[] = {
988 0xF7, 0xF6, 0xDF, 0x3C,
989 0x67, 0x42,
990 0x55, 0x45,
991 0xAD, 0x05,
992 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
993 };
994
995 status = acpi_dsm_integer(hdl, i2c_hid_guid, 1, 1, NULL, &val);
996 if (ACPI_FAILURE(status)) {
997 aprint_error_dev(sc->sc_dev,
998 "failed to get HidDescriptorAddress: %s\n",
999 AcpiFormatException(status));
1000 return false;
1001 }
1002
1003 sc->sc_hid_desc_addr = (u_int)val;
1004
1005 return true;
1006 }
1007