ihidev.c revision 1.21
1/* $NetBSD: ihidev.c,v 1.21 2022/01/14 22:25:49 riastradh 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@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.21 2022/01/14 22:25:49 riastradh Exp $"); 58 59#include <sys/param.h> 60#include <sys/systm.h> 61#include <sys/device.h> 62#include <sys/kmem.h> 63#include <sys/workqueue.h> 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 */ 89enum { 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 104static int I2C_HID_POWER_ON = 0x0; 105static int I2C_HID_POWER_OFF = 0x1; 106 107static int ihidev_match(device_t, cfdata_t, void *); 108static void ihidev_attach(device_t, device_t, void *); 109static int ihidev_detach(device_t, int); 110CFATTACH_DECL_NEW(ihidev, sizeof(struct ihidev_softc), 111 ihidev_match, ihidev_attach, ihidev_detach, NULL); 112 113static bool ihidev_intr_init(struct ihidev_softc *); 114static void ihidev_intr_fini(struct ihidev_softc *); 115 116static bool ihidev_suspend(device_t, const pmf_qual_t *); 117static bool ihidev_resume(device_t, const pmf_qual_t *); 118static int ihidev_hid_command(struct ihidev_softc *, int, void *, bool); 119static int ihidev_intr(void *); 120static void ihidev_work(struct work *, void *); 121static int ihidev_reset(struct ihidev_softc *, bool); 122static int ihidev_hid_desc_parse(struct ihidev_softc *); 123 124static int ihidev_maxrepid(void *, int); 125static int ihidev_print(void *, const char *); 126static int ihidev_submatch(device_t, cfdata_t, const int *, void *); 127 128static bool ihidev_acpi_get_info(struct ihidev_softc *); 129 130static const struct device_compatible_entry compat_data[] = { 131 { .compat = "PNP0C50" }, 132 { .compat = "ACPI0C50" }, 133 { .compat = "hid-over-i2c" }, 134 DEVICE_COMPAT_EOL 135}; 136 137static int 138ihidev_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 149static void 150ihidev_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_lock, MUTEX_DEFAULT, IPL_NONE); 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 (!ihidev_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 CFARGS(.submatch = ihidev_submatch, 223 .locators = locs)); 224 if (dev != NULL) { 225 for (repid = 0; repid < sc->sc_nrepid; repid++) 226 sc->sc_subdevs[repid] = device_private(dev); 227 return; 228 } 229 230 for (repid = 0; repid < sc->sc_nrepid; repid++) { 231 if (hid_report_size(sc->sc_report, sc->sc_reportlen, hid_input, 232 repid) == 0 && 233 hid_report_size(sc->sc_report, sc->sc_reportlen, 234 hid_output, repid) == 0 && 235 hid_report_size(sc->sc_report, sc->sc_reportlen, 236 hid_feature, repid) == 0) 237 continue; 238 239 iha.reportid = repid; 240 locs[IHIDBUSCF_REPORTID] = repid; 241 dev = config_found(self, &iha, ihidev_print, 242 CFARGS(.submatch = ihidev_submatch, 243 .locators = locs)); 244 sc->sc_subdevs[repid] = device_private(dev); 245 } 246 247 /* power down until we're opened */ 248 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, &I2C_HID_POWER_OFF, 249 false)) { 250 aprint_error_dev(sc->sc_dev, "failed to power down\n"); 251 return; 252 } 253 if (!pmf_device_register(self, ihidev_suspend, ihidev_resume)) 254 aprint_error_dev(self, "couldn't establish power handler\n"); 255} 256 257static int 258ihidev_detach(device_t self, int flags) 259{ 260 struct ihidev_softc *sc = device_private(self); 261 int error; 262 263 error = config_detach_children(self, flags); 264 if (error) 265 return error; 266 267 pmf_device_deregister(self); 268 ihidev_intr_fini(sc); 269 270 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, &I2C_HID_POWER_OFF, 271 true)) 272 aprint_error_dev(sc->sc_dev, "failed to power down\n"); 273 274 if (sc->sc_ibuf != NULL) { 275 kmem_free(sc->sc_ibuf, sc->sc_isize); 276 sc->sc_ibuf = NULL; 277 } 278 279 if (sc->sc_report != NULL) 280 kmem_free(sc->sc_report, sc->sc_reportlen); 281 282 mutex_destroy(&sc->sc_lock); 283 284 return 0; 285} 286 287static bool 288ihidev_suspend(device_t self, const pmf_qual_t *q) 289{ 290 struct ihidev_softc *sc = device_private(self); 291 292 mutex_enter(&sc->sc_lock); 293 if (sc->sc_refcnt > 0) { 294 printf("ihidev power off\n"); 295 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, 296 &I2C_HID_POWER_OFF, true)) 297 aprint_error_dev(sc->sc_dev, "failed to power down\n"); 298 } 299 mutex_exit(&sc->sc_lock); 300 return true; 301} 302 303static bool 304ihidev_resume(device_t self, const pmf_qual_t *q) 305{ 306 struct ihidev_softc *sc = device_private(self); 307 308 mutex_enter(&sc->sc_lock); 309 if (sc->sc_refcnt > 0) { 310 printf("ihidev power reset\n"); 311 ihidev_reset(sc, true); 312 } 313 mutex_exit(&sc->sc_lock); 314 return true; 315} 316 317static int 318ihidev_hid_command(struct ihidev_softc *sc, int hidcmd, void *arg, bool poll) 319{ 320 int i, res = 1; 321 int flags = poll ? I2C_F_POLL : 0; 322 323 iic_acquire_bus(sc->sc_tag, flags); 324 325 switch (hidcmd) { 326 case I2C_HID_CMD_DESCR: { 327 /* 328 * 5.2.2 - HID Descriptor Retrieval 329 * register is passed from the controller 330 */ 331 uint8_t cmd[] = { 332 htole16(sc->sc_hid_desc_addr) & 0xff, 333 htole16(sc->sc_hid_desc_addr) >> 8, 334 }; 335 336 DPRINTF(("%s: HID command I2C_HID_CMD_DESCR at 0x%x\n", 337 sc->sc_dev.dv_xname, htole16(sc->sc_hid_desc_addr))); 338 339 /* 20 00 */ 340 res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 341 &cmd, sizeof(cmd), &sc->hid_desc_buf, 342 sizeof(struct i2c_hid_desc), flags); 343 344 DPRINTF(("%s: HID descriptor:", sc->sc_dev.dv_xname)); 345 for (i = 0; i < sizeof(struct i2c_hid_desc); i++) 346 DPRINTF((" %.2x", sc->hid_desc_buf[i])); 347 DPRINTF(("\n")); 348 349 break; 350 } 351 case I2C_HID_CMD_RESET: { 352 uint8_t cmd[] = { 353 htole16(sc->hid_desc.wCommandRegister) & 0xff, 354 htole16(sc->hid_desc.wCommandRegister) >> 8, 355 0, 356 I2C_HID_CMD_RESET, 357 }; 358 359 DPRINTF(("%s: HID command I2C_HID_CMD_RESET\n", 360 sc->sc_dev.dv_xname)); 361 362 /* 22 00 00 01 */ 363 res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 364 &cmd, sizeof(cmd), NULL, 0, flags); 365 366 break; 367 } 368 case I2C_HID_CMD_GET_REPORT: { 369 struct i2c_hid_report_request *rreq = 370 (struct i2c_hid_report_request *)arg; 371 372 uint8_t cmd[] = { 373 htole16(sc->hid_desc.wCommandRegister) & 0xff, 374 htole16(sc->hid_desc.wCommandRegister) >> 8, 375 0, 376 I2C_HID_CMD_GET_REPORT, 377 0, 0, 0, 378 }; 379 int cmdlen = 7; 380 int dataoff = 4; 381 int report_id = rreq->id; 382 int report_id_len = 1; 383 int report_len = rreq->len + 2; 384 int d; 385 uint8_t *tmprep; 386 387 DPRINTF(("%s: HID command I2C_HID_CMD_GET_REPORT %d " 388 "(type %d, len %d)\n", sc->sc_dev.dv_xname, report_id, 389 rreq->type, rreq->len)); 390 391 /* 392 * 7.2.2.4 - "The protocol is optimized for Report < 15. If a 393 * report ID >= 15 is necessary, then the Report ID in the Low 394 * Byte must be set to 1111 and a Third Byte is appended to the 395 * protocol. This Third Byte contains the entire/actual report 396 * ID." 397 */ 398 if (report_id >= 15) { 399 cmd[dataoff++] = report_id; 400 report_id = 15; 401 report_id_len = 2; 402 } else 403 cmdlen--; 404 405 cmd[2] = report_id | rreq->type << 4; 406 407 cmd[dataoff++] = sc->hid_desc.wDataRegister & 0xff; 408 cmd[dataoff] = sc->hid_desc.wDataRegister >> 8; 409 410 /* 411 * 7.2.2.2 - Response will be a 2-byte length value, the report 412 * id with length determined above, and then the report. 413 * Allocate rreq->len + 2 + 2 bytes, read into that temporary 414 * buffer, and then copy only the report back out to 415 * rreq->data. 416 */ 417 report_len += report_id_len; 418 tmprep = kmem_zalloc(report_len, KM_NOSLEEP); 419 420 /* type 3 id 8: 22 00 38 02 23 00 */ 421 res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 422 &cmd, cmdlen, tmprep, report_len, flags); 423 424 d = tmprep[0] | tmprep[1] << 8; 425 if (d != report_len) { 426 DPRINTF(("%s: response size %d != expected length %d\n", 427 sc->sc_dev.dv_xname, d, report_len)); 428 } 429 430 if (report_id_len == 2) 431 d = tmprep[2] | tmprep[3] << 8; 432 else 433 d = tmprep[2]; 434 435 if (d != rreq->id) { 436 DPRINTF(("%s: response report id %d != %d\n", 437 sc->sc_dev.dv_xname, d, rreq->id)); 438 iic_release_bus(sc->sc_tag, 0); 439 kmem_free(tmprep, report_len); 440 return (1); 441 } 442 443 DPRINTF(("%s: response:", sc->sc_dev.dv_xname)); 444 for (i = 0; i < report_len; i++) 445 DPRINTF((" %.2x", tmprep[i])); 446 DPRINTF(("\n")); 447 448 memcpy(rreq->data, tmprep + 2 + report_id_len, rreq->len); 449 kmem_free(tmprep, report_len); 450 451 break; 452 } 453 case I2C_HID_CMD_SET_REPORT: { 454 struct i2c_hid_report_request *rreq = 455 (struct i2c_hid_report_request *)arg; 456 457 uint8_t cmd[] = { 458 htole16(sc->hid_desc.wCommandRegister) & 0xff, 459 htole16(sc->hid_desc.wCommandRegister) >> 8, 460 0, 461 I2C_HID_CMD_SET_REPORT, 462 0, 0, 0, 0, 0, 0, 463 }; 464 int cmdlen = 10; 465 int report_id = rreq->id; 466 int report_len = 2 + (report_id ? 1 : 0) + rreq->len; 467 int dataoff; 468 uint8_t *finalcmd; 469 470 DPRINTF(("%s: HID command I2C_HID_CMD_SET_REPORT %d " 471 "(type %d, len %d):", sc->sc_dev.dv_xname, report_id, 472 rreq->type, rreq->len)); 473 for (i = 0; i < rreq->len; i++) 474 DPRINTF((" %.2x", ((uint8_t *)rreq->data)[i])); 475 DPRINTF(("\n")); 476 477 /* 478 * 7.2.2.4 - "The protocol is optimized for Report < 15. If a 479 * report ID >= 15 is necessary, then the Report ID in the Low 480 * Byte must be set to 1111 and a Third Byte is appended to the 481 * protocol. This Third Byte contains the entire/actual report 482 * ID." 483 */ 484 dataoff = 4; 485 if (report_id >= 15) { 486 cmd[dataoff++] = report_id; 487 report_id = 15; 488 } else 489 cmdlen--; 490 491 cmd[2] = report_id | rreq->type << 4; 492 493 if (rreq->type == I2C_HID_REPORT_TYPE_FEATURE) { 494 cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister) 495 & 0xff; 496 cmd[dataoff++] = htole16(sc->hid_desc.wDataRegister) 497 >> 8; 498 } else { 499 cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister) 500 & 0xff; 501 cmd[dataoff++] = htole16(sc->hid_desc.wOutputRegister) 502 >> 8; 503 } 504 505 cmd[dataoff++] = report_len & 0xff; 506 cmd[dataoff++] = report_len >> 8; 507 cmd[dataoff] = rreq->id; 508 509 finalcmd = kmem_zalloc(cmdlen + rreq->len, KM_NOSLEEP); 510 511 memcpy(finalcmd, cmd, cmdlen); 512 memcpy(finalcmd + cmdlen, rreq->data, rreq->len); 513 514 /* type 3 id 4: 22 00 34 03 23 00 04 00 04 03 */ 515 res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 516 finalcmd, cmdlen + rreq->len, NULL, 0, flags); 517 kmem_free(finalcmd, cmdlen + rreq->len); 518 519 break; 520 } 521 522 case I2C_HID_CMD_SET_POWER: { 523 int power = *(int *)arg; 524 uint8_t cmd[] = { 525 htole16(sc->hid_desc.wCommandRegister) & 0xff, 526 htole16(sc->hid_desc.wCommandRegister) >> 8, 527 power, 528 I2C_HID_CMD_SET_POWER, 529 }; 530 531 DPRINTF(("%s: HID command I2C_HID_CMD_SET_POWER(%d)\n", 532 sc->sc_dev.dv_xname, power)); 533 534 /* 22 00 00 08 */ 535 res = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 536 &cmd, sizeof(cmd), NULL, 0, flags); 537 538 break; 539 } 540 case I2C_HID_REPORT_DESCR: { 541 uint8_t cmd[] = { 542 htole16(sc->hid_desc.wReportDescRegister) & 0xff, 543 htole16(sc->hid_desc.wReportDescRegister) >> 8, 544 }; 545 546 DPRINTF(("%s: HID command I2C_HID_REPORT_DESCR at 0x%x with " 547 "size %d\n", sc->sc_dev.dv_xname, cmd[0], 548 sc->sc_reportlen)); 549 550 /* 20 00 */ 551 res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, 552 &cmd, sizeof(cmd), sc->sc_report, sc->sc_reportlen, flags); 553 554 DPRINTF(("%s: HID report descriptor:", sc->sc_dev.dv_xname)); 555 for (i = 0; i < sc->sc_reportlen; i++) 556 DPRINTF((" %.2x", sc->sc_report[i])); 557 DPRINTF(("\n")); 558 559 break; 560 } 561 default: 562 aprint_error_dev(sc->sc_dev, "unknown command %d\n", 563 hidcmd); 564 } 565 566 iic_release_bus(sc->sc_tag, flags); 567 568 return (res); 569} 570 571static int 572ihidev_reset(struct ihidev_softc *sc, bool poll) 573{ 574 DPRINTF(("%s: resetting\n", sc->sc_dev.dv_xname)); 575 576 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, 577 &I2C_HID_POWER_ON, poll)) { 578 aprint_error_dev(sc->sc_dev, "failed to power on\n"); 579 return (1); 580 } 581 582 DELAY(1000); 583 584 if (ihidev_hid_command(sc, I2C_HID_CMD_RESET, 0, poll)) { 585 aprint_error_dev(sc->sc_dev, "failed to reset hardware\n"); 586 587 ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, 588 &I2C_HID_POWER_OFF, poll); 589 590 return (1); 591 } 592 593 DELAY(1000); 594 595 return (0); 596} 597 598/* 599 * 5.2.2 - HID Descriptor Retrieval 600 * 601 * parse HID Descriptor that has already been read into hid_desc with 602 * I2C_HID_CMD_DESCR 603 */ 604static int 605ihidev_hid_desc_parse(struct ihidev_softc *sc) 606{ 607 int retries = 3; 608 609 /* must be v01.00 */ 610 if (le16toh(sc->hid_desc.bcdVersion) != 0x0100) { 611 aprint_error_dev(sc->sc_dev, 612 "bad HID descriptor bcdVersion (0x%x)\n", 613 le16toh(sc->hid_desc.bcdVersion)); 614 return (1); 615 } 616 617 /* must be 30 bytes for v1.00 */ 618 if (le16toh(sc->hid_desc.wHIDDescLength != 619 sizeof(struct i2c_hid_desc))) { 620 aprint_error_dev(sc->sc_dev, 621 "bad HID descriptor size (%d != %zu)\n", 622 le16toh(sc->hid_desc.wHIDDescLength), 623 sizeof(struct i2c_hid_desc)); 624 return (1); 625 } 626 627 if (le16toh(sc->hid_desc.wReportDescLength) <= 0) { 628 aprint_error_dev(sc->sc_dev, 629 "bad HID report descriptor size (%d)\n", 630 le16toh(sc->hid_desc.wReportDescLength)); 631 return (1); 632 } 633 634 while (retries-- > 0) { 635 if (ihidev_reset(sc, false)) { 636 if (retries == 0) 637 return(1); 638 639 DELAY(1000); 640 } 641 else 642 break; 643 } 644 645 sc->sc_reportlen = le16toh(sc->hid_desc.wReportDescLength); 646 sc->sc_report = kmem_zalloc(sc->sc_reportlen, KM_NOSLEEP); 647 648 if (ihidev_hid_command(sc, I2C_HID_REPORT_DESCR, 0, false)) { 649 aprint_error_dev(sc->sc_dev, "failed fetching HID report\n"); 650 return (1); 651 } 652 653 return (0); 654} 655 656static bool 657ihidev_intr_init(struct ihidev_softc *sc) 658{ 659#if NACPICA > 0 660 ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle; 661 struct acpi_resources res; 662 ACPI_STATUS rv; 663 char buf[100]; 664 665 rv = acpi_resource_parse(sc->sc_dev, hdl, "_CRS", &res, 666 &acpi_resource_parse_ops_quiet); 667 if (ACPI_FAILURE(rv)) { 668 aprint_error_dev(sc->sc_dev, "can't parse '_CRS'\n"); 669 return false; 670 } 671 672 const struct acpi_irq * const irq = acpi_res_irq(&res, 0); 673 if (irq == NULL) { 674 aprint_error_dev(sc->sc_dev, "no IRQ resource\n"); 675 acpi_resource_cleanup(&res); 676 return false; 677 } 678 679 sc->sc_intr_type = 680 irq->ar_type == ACPI_EDGE_SENSITIVE ? IST_EDGE : IST_LEVEL; 681 682 acpi_resource_cleanup(&res); 683 684 sc->sc_ih = acpi_intr_establish(sc->sc_dev, sc->sc_phandle, IPL_TTY, 685 false, ihidev_intr, sc, device_xname(sc->sc_dev)); 686 if (sc->sc_ih == NULL) { 687 aprint_error_dev(sc->sc_dev, "can't establish interrupt\n"); 688 return false; 689 } 690 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", 691 acpi_intr_string(sc->sc_ih, buf, sizeof(buf))); 692 693 if (workqueue_create(&sc->sc_wq, device_xname(sc->sc_dev), ihidev_work, 694 sc, PRI_NONE, IPL_TTY, WQ_MPSAFE)) { 695 aprint_error_dev(sc->sc_dev, 696 "can't establish workqueue\n"); 697 return false; 698 } 699 sc->sc_work_pending = 0; 700 701 return true; 702#else 703 aprint_error_dev(sc->sc_dev, "can't establish interrupt\n"); 704 return false; 705#endif 706} 707 708static void 709ihidev_intr_fini(struct ihidev_softc *sc) 710{ 711#if NACPICA > 0 712 if (sc->sc_ih != NULL) { 713 acpi_intr_disestablish(sc->sc_ih); 714 } 715 if (sc->sc_wq != NULL) { 716 workqueue_destroy(sc->sc_wq); 717 } 718#endif 719} 720 721static void 722ihidev_intr_mask(struct ihidev_softc * const sc) 723{ 724 725 if (sc->sc_intr_type == IST_LEVEL) { 726#if NACPICA > 0 727 acpi_intr_mask(sc->sc_ih); 728#endif 729 } 730} 731 732static void 733ihidev_intr_unmask(struct ihidev_softc * const sc) 734{ 735 736 if (sc->sc_intr_type == IST_LEVEL) { 737#if NACPICA > 0 738 acpi_intr_unmask(sc->sc_ih); 739#endif 740 } 741} 742 743static int 744ihidev_intr(void *arg) 745{ 746 struct ihidev_softc * const sc = arg; 747 748 /* 749 * Schedule our work. If we're using a level-triggered 750 * interrupt, we have to mask it off while we wait for service. 751 */ 752 if (atomic_swap_uint(&sc->sc_work_pending, 1) == 0) 753 workqueue_enqueue(sc->sc_wq, &sc->sc_work, NULL); 754 ihidev_intr_mask(sc); 755 756 return 1; 757} 758 759static void 760ihidev_work(struct work *wk, void *arg) 761{ 762 struct ihidev_softc * const sc = arg; 763 struct ihidev *scd; 764 u_int psize; 765 int res, i; 766 u_char *p; 767 u_int rep = 0; 768 769 atomic_store_relaxed(&sc->sc_work_pending, 0); 770 771 mutex_enter(&sc->sc_lock); 772 773 iic_acquire_bus(sc->sc_tag, 0); 774 res = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr, NULL, 0, 775 sc->sc_ibuf, sc->sc_isize, 0); 776 iic_release_bus(sc->sc_tag, 0); 777 if (res != 0) 778 goto out; 779 780 /* 781 * 6.1.1 - First two bytes are the packet length, which must be less 782 * than or equal to wMaxInputLength 783 */ 784 psize = sc->sc_ibuf[0] | sc->sc_ibuf[1] << 8; 785 if (!psize || psize > sc->sc_isize) { 786 DPRINTF(("%s: %s: invalid packet size (%d vs. %d)\n", 787 sc->sc_dev.dv_xname, __func__, psize, sc->sc_isize)); 788 goto out; 789 } 790 791 /* 3rd byte is the report id */ 792 p = sc->sc_ibuf + 2; 793 psize -= 2; 794 if (sc->sc_nrepid != 1) 795 rep = *p++, psize--; 796 797 if (rep >= sc->sc_nrepid) { 798 aprint_error_dev(sc->sc_dev, "%s: bad report id %d\n", 799 __func__, rep); 800 goto out; 801 } 802 803 DPRINTF(("%s: %s: hid input (rep %d):", sc->sc_dev.dv_xname, 804 __func__, rep)); 805 for (i = 0; i < sc->sc_isize; i++) 806 DPRINTF((" %.2x", sc->sc_ibuf[i])); 807 DPRINTF(("\n")); 808 809 scd = sc->sc_subdevs[rep]; 810 if (scd == NULL || !(scd->sc_state & IHIDEV_OPEN)) 811 goto out; 812 813 scd->sc_intr(scd, p, psize); 814 815 out: 816 mutex_exit(&sc->sc_lock); 817 818 /* 819 * If our interrupt is level-triggered, re-enable it now. 820 */ 821 ihidev_intr_unmask(sc); 822} 823 824static int 825ihidev_maxrepid(void *buf, int len) 826{ 827 struct hid_data *d; 828 struct hid_item h; 829 int maxid; 830 831 maxid = -1; 832 h.report_ID = 0; 833 for (d = hid_start_parse(buf, len, hid_none); hid_get_item(d, &h); ) 834 if ((int)h.report_ID > maxid) 835 maxid = h.report_ID; 836 hid_end_parse(d); 837 838 return (maxid); 839} 840 841static int 842ihidev_print(void *aux, const char *pnp) 843{ 844 struct ihidev_attach_arg *iha = aux; 845 846 if (iha->reportid == IHIDEV_CLAIM_ALLREPORTID) 847 return (QUIET); 848 849 if (pnp) 850 aprint_normal("hid at %s", pnp); 851 852 if (iha->reportid != 0) 853 aprint_normal(" reportid %d", iha->reportid); 854 855 return (UNCONF); 856} 857 858static int 859ihidev_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux) 860{ 861 struct ihidev_attach_arg *iha = aux; 862 863 if (cf->ihidevcf_reportid != IHIDEV_UNK_REPORTID && 864 cf->ihidevcf_reportid != iha->reportid) 865 return (0); 866 867 return config_match(parent, cf, aux); 868} 869 870int 871ihidev_open(struct ihidev *scd) 872{ 873 struct ihidev_softc *sc = scd->sc_parent; 874 875 DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname, 876 __func__, scd->sc_state, sc->sc_refcnt)); 877 878 if (scd->sc_state & IHIDEV_OPEN) 879 return (EBUSY); 880 881 scd->sc_state |= IHIDEV_OPEN; 882 883 if (sc->sc_refcnt++ || sc->sc_isize == 0) 884 return (0); 885 886 /* power on */ 887 ihidev_reset(sc, false); 888 889 return (0); 890} 891 892void 893ihidev_close(struct ihidev *scd) 894{ 895 struct ihidev_softc *sc = scd->sc_parent; 896 897 DPRINTF(("%s: %s: state=%d refcnt=%d\n", sc->sc_dev.dv_xname, 898 __func__, scd->sc_state, sc->sc_refcnt)); 899 900 if (!(scd->sc_state & IHIDEV_OPEN)) 901 return; 902 903 scd->sc_state &= ~IHIDEV_OPEN; 904 905 if (--sc->sc_refcnt) 906 return; 907 908 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_POWER, 909 &I2C_HID_POWER_OFF, false)) 910 aprint_error_dev(sc->sc_dev, "failed to power down\n"); 911} 912 913void 914ihidev_get_report_desc(struct ihidev_softc *sc, void **desc, int *size) 915{ 916 *desc = sc->sc_report; 917 *size = sc->sc_reportlen; 918} 919 920/* convert hid_* constants used throughout HID code to i2c HID equivalents */ 921int 922ihidev_report_type_conv(int hid_type_id) 923{ 924 switch (hid_type_id) { 925 case hid_input: 926 return I2C_HID_REPORT_TYPE_INPUT; 927 case hid_output: 928 return I2C_HID_REPORT_TYPE_OUTPUT; 929 case hid_feature: 930 return I2C_HID_REPORT_TYPE_FEATURE; 931 default: 932 return -1; 933 } 934} 935 936int 937ihidev_get_report(struct device *dev, int type, int id, void *data, int len) 938{ 939 struct ihidev_softc *sc = (struct ihidev_softc *)dev; 940 struct i2c_hid_report_request rreq; 941 int ctype; 942 943 if ((ctype = ihidev_report_type_conv(type)) < 0) 944 return (1); 945 946 rreq.type = ctype; 947 rreq.id = id; 948 rreq.data = data; 949 rreq.len = len; 950 951 if (ihidev_hid_command(sc, I2C_HID_CMD_GET_REPORT, &rreq, false)) { 952 aprint_error_dev(sc->sc_dev, "failed fetching report\n"); 953 return (1); 954 } 955 956 return 0; 957} 958 959int 960ihidev_set_report(struct device *dev, int type, int id, void *data, 961 int len) 962{ 963 struct ihidev_softc *sc = (struct ihidev_softc *)dev; 964 struct i2c_hid_report_request rreq; 965 int ctype; 966 967 if ((ctype = ihidev_report_type_conv(type)) < 0) 968 return (1); 969 970 rreq.type = ctype; 971 rreq.id = id; 972 rreq.data = data; 973 rreq.len = len; 974 975 if (ihidev_hid_command(sc, I2C_HID_CMD_SET_REPORT, &rreq, false)) { 976 aprint_error_dev(sc->sc_dev, "failed setting report\n"); 977 return (1); 978 } 979 980 return 0; 981} 982 983static bool 984ihidev_acpi_get_info(struct ihidev_softc *sc) 985{ 986 ACPI_HANDLE hdl = (void *)(uintptr_t)sc->sc_phandle; 987 ACPI_STATUS status; 988 ACPI_INTEGER val; 989 990 /* 3cdff6f7-4267-4555-ad05-b30a3d8938de */ 991 uint8_t i2c_hid_guid[] = { 992 0xF7, 0xF6, 0xDF, 0x3C, 993 0x67, 0x42, 994 0x55, 0x45, 995 0xAD, 0x05, 996 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE, 997 }; 998 999 status = acpi_dsm_integer(hdl, i2c_hid_guid, 1, 1, NULL, &val); 1000 if (ACPI_FAILURE(status)) { 1001 aprint_error_dev(sc->sc_dev, 1002 "failed to get HidDescriptorAddress: %s\n", 1003 AcpiFormatException(status)); 1004 return false; 1005 } 1006 1007 sc->sc_hid_desc_addr = (u_int)val; 1008 1009 return true; 1010} 1011