1 /*- 2 * Copyright (c) 2018-2019 Marc Priggemeyer <marc.priggemeyer@gmail.com> 3 * Copyright (c) 2019-2020 Vladimir Kondratyev <wulf@FreeBSD.org> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * I2C HID transport backend. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include "opt_hid.h" 35 36 #include <sys/param.h> 37 #include <sys/bus.h> 38 #include <sys/callout.h> 39 #include <sys/endian.h> 40 #include <sys/kernel.h> 41 #include <sys/lock.h> 42 #include <sys/malloc.h> 43 #include <sys/module.h> 44 #include <sys/rman.h> 45 #include <sys/sysctl.h> 46 #include <sys/systm.h> 47 #include <sys/taskqueue.h> 48 49 #include <machine/resource.h> 50 51 #include <contrib/dev/acpica/include/acpi.h> 52 #include <contrib/dev/acpica/include/accommon.h> 53 #include <dev/acpica/acpivar.h> 54 55 #include <dev/evdev/input.h> 56 57 #include <dev/hid/hid.h> 58 #include <dev/hid/hidquirk.h> 59 60 #include <dev/iicbus/iic.h> 61 #include <dev/iicbus/iicbus.h> 62 #include <dev/iicbus/iiconf.h> 63 64 #include "hid_if.h" 65 66 #ifdef IICHID_DEBUG 67 static int iichid_debug = 0; 68 69 static SYSCTL_NODE(_hw, OID_AUTO, iichid, CTLFLAG_RW, 0, "I2C HID"); 70 SYSCTL_INT(_hw_iichid, OID_AUTO, debug, CTLFLAG_RWTUN, 71 &iichid_debug, 1, "Debug level"); 72 73 #define DPRINTFN(sc, n, ...) do { \ 74 if (iichid_debug >= (n)) \ 75 device_printf((sc)->dev, __VA_ARGS__); \ 76 } while (0) 77 #define DPRINTF(sc, ...) DPRINTFN(sc, 1, __VA_ARGS__) 78 #else 79 #define DPRINTFN(...) do {} while (0) 80 #define DPRINTF(...) do {} while (0) 81 #endif 82 83 typedef hid_size_t iichid_size_t; 84 #define IICHID_SIZE_MAX (UINT16_MAX - 2) 85 86 /* 7.2 */ 87 enum { 88 I2C_HID_CMD_DESCR = 0x0, 89 I2C_HID_CMD_RESET = 0x1, 90 I2C_HID_CMD_GET_REPORT = 0x2, 91 I2C_HID_CMD_SET_REPORT = 0x3, 92 I2C_HID_CMD_GET_IDLE = 0x4, 93 I2C_HID_CMD_SET_IDLE = 0x5, 94 I2C_HID_CMD_GET_PROTO = 0x6, 95 I2C_HID_CMD_SET_PROTO = 0x7, 96 I2C_HID_CMD_SET_POWER = 0x8, 97 }; 98 99 #define I2C_HID_POWER_ON 0x0 100 #define I2C_HID_POWER_OFF 0x1 101 102 /* 103 * Since interrupt resource acquisition is not always possible (in case of GPIO 104 * interrupts) iichid now supports a sampling_mode. 105 * Set dev.iichid.<unit>.sampling_rate_slow to a value greater then 0 106 * to activate sampling. A value of 0 is possible but will not reset the 107 * callout and, thereby, disable further report requests. Do not set the 108 * sampling_rate_fast value too high as it may result in periodical lags of 109 * cursor motion. 110 */ 111 #define IICHID_SAMPLING_RATE_FAST 60 112 #define IICHID_SAMPLING_RATE_SLOW 10 113 #define IICHID_SAMPLING_HYSTERESIS 1 114 115 /* 5.1.1 - HID Descriptor Format */ 116 struct i2c_hid_desc { 117 uint16_t wHIDDescLength; 118 uint16_t bcdVersion; 119 uint16_t wReportDescLength; 120 uint16_t wReportDescRegister; 121 uint16_t wInputRegister; 122 uint16_t wMaxInputLength; 123 uint16_t wOutputRegister; 124 uint16_t wMaxOutputLength; 125 uint16_t wCommandRegister; 126 uint16_t wDataRegister; 127 uint16_t wVendorID; 128 uint16_t wProductID; 129 uint16_t wVersionID; 130 uint32_t reserved; 131 } __packed; 132 133 static char *iichid_ids[] = { 134 "PNP0C50", 135 "ACPI0C50", 136 NULL 137 }; 138 139 enum iichid_powerstate_how { 140 IICHID_PS_NULL, 141 IICHID_PS_ON, 142 IICHID_PS_OFF, 143 }; 144 145 /* 146 * Locking: no internal locks are used. To serialize access to shared members, 147 * external iicbus lock should be taken. That allows to make locking greatly 148 * simple at the cost of running front interrupt handlers with locked bus. 149 */ 150 struct iichid_softc { 151 device_t dev; 152 153 bool probe_done; 154 int probe_result; 155 156 struct hid_device_info hw; 157 uint16_t addr; /* Shifted left by 1 */ 158 struct i2c_hid_desc desc; 159 160 hid_intr_t *intr_handler; 161 void *intr_ctx; 162 uint8_t *intr_buf; 163 iichid_size_t intr_bufsize; 164 165 int irq_rid; 166 struct resource *irq_res; 167 void *irq_cookie; 168 169 #ifdef IICHID_SAMPLING 170 int sampling_rate_slow; /* iicbus lock */ 171 int sampling_rate_fast; 172 int sampling_hysteresis; 173 int missing_samples; /* iicbus lock */ 174 struct timeout_task periodic_task; /* iicbus lock */ 175 bool callout_setup; /* iicbus lock */ 176 struct taskqueue *taskqueue; 177 struct task event_task; 178 #endif 179 180 bool open; /* iicbus lock */ 181 bool suspend; /* iicbus lock */ 182 bool power_on; /* iicbus lock */ 183 }; 184 185 static device_probe_t iichid_probe; 186 static device_attach_t iichid_attach; 187 static device_detach_t iichid_detach; 188 static device_resume_t iichid_resume; 189 static device_suspend_t iichid_suspend; 190 191 #ifdef IICHID_SAMPLING 192 static int iichid_setup_callout(struct iichid_softc *); 193 static int iichid_reset_callout(struct iichid_softc *); 194 static void iichid_teardown_callout(struct iichid_softc *); 195 #endif 196 197 static __inline bool 198 acpi_is_iichid(ACPI_HANDLE handle) 199 { 200 char **ids; 201 UINT32 sta; 202 203 for (ids = iichid_ids; *ids != NULL; ids++) { 204 if (acpi_MatchHid(handle, *ids)) 205 break; 206 } 207 if (*ids == NULL) 208 return (false); 209 210 /* 211 * If no _STA method or if it failed, then assume that 212 * the device is present. 213 */ 214 if (ACPI_FAILURE(acpi_GetInteger(handle, "_STA", &sta)) || 215 ACPI_DEVICE_PRESENT(sta)) 216 return (true); 217 218 return (false); 219 } 220 221 static ACPI_STATUS 222 iichid_get_config_reg(ACPI_HANDLE handle, uint16_t *config_reg) 223 { 224 ACPI_OBJECT *result; 225 ACPI_BUFFER acpi_buf; 226 ACPI_STATUS status; 227 228 /* 229 * function (_DSM) to be evaluated to retrieve the address of 230 * the configuration register of the HID device. 231 */ 232 /* 3cdff6f7-4267-4555-ad05-b30a3d8938de */ 233 static uint8_t dsm_guid[ACPI_UUID_LENGTH] = { 234 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45, 235 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE, 236 }; 237 238 status = acpi_EvaluateDSMTyped(handle, dsm_guid, 1, 1, NULL, &acpi_buf, 239 ACPI_TYPE_INTEGER); 240 if (ACPI_FAILURE(status)) { 241 printf("%s: error evaluating _DSM\n", __func__); 242 return (status); 243 } 244 result = (ACPI_OBJECT *) acpi_buf.Pointer; 245 *config_reg = result->Integer.Value & 0xFFFF; 246 247 AcpiOsFree(result); 248 return (status); 249 } 250 251 static int 252 iichid_cmd_read(struct iichid_softc* sc, void *buf, iichid_size_t maxlen, 253 iichid_size_t *actual_len) 254 { 255 /* 256 * 6.1.3 - Retrieval of Input Reports 257 * DEVICE returns the length (2 Bytes) and the entire Input Report. 258 */ 259 uint8_t actbuf[2] = { 0, 0 }; 260 /* Read actual input report length. */ 261 struct iic_msg msgs[] = { 262 { sc->addr, IIC_M_RD | IIC_M_NOSTOP, sizeof(actbuf), actbuf }, 263 }; 264 uint16_t actlen; 265 int error; 266 267 error = iicbus_transfer(sc->dev, msgs, nitems(msgs)); 268 if (error != 0) 269 return (error); 270 271 actlen = actbuf[0] | actbuf[1] << 8; 272 if (actlen <= 2 || actlen == 0xFFFF || maxlen == 0) { 273 /* Read and discard 1 byte to send I2C STOP condition. */ 274 msgs[0] = (struct iic_msg) 275 { sc->addr, IIC_M_RD | IIC_M_NOSTART, 1, actbuf }; 276 actlen = 0; 277 } else { 278 actlen -= 2; 279 if (actlen > maxlen) { 280 DPRINTF(sc, "input report too big. requested=%d " 281 "received=%d\n", maxlen, actlen); 282 actlen = maxlen; 283 } 284 /* Read input report itself. */ 285 msgs[0] = (struct iic_msg) 286 { sc->addr, IIC_M_RD | IIC_M_NOSTART, actlen, buf }; 287 } 288 289 error = iicbus_transfer(sc->dev, msgs, 1); 290 if (error == 0 && actual_len != NULL) 291 *actual_len = actlen; 292 293 DPRINTFN(sc, 5, 294 "%*D - %*D\n", 2, actbuf, " ", msgs[0].len, msgs[0].buf, " "); 295 296 return (error); 297 } 298 299 static int 300 iichid_cmd_write(struct iichid_softc *sc, const void *buf, iichid_size_t len) 301 { 302 /* 6.2.3 - Sending Output Reports. */ 303 uint8_t *cmdreg = (uint8_t *)&sc->desc.wOutputRegister; 304 uint16_t replen = 2 + len; 305 uint8_t cmd[4] = { cmdreg[0], cmdreg[1], replen & 0xFF, replen >> 8 }; 306 struct iic_msg msgs[] = { 307 {sc->addr, IIC_M_WR | IIC_M_NOSTOP, sizeof(cmd), cmd}, 308 {sc->addr, IIC_M_WR | IIC_M_NOSTART, len, __DECONST(void *, buf)}, 309 }; 310 311 if (le16toh(sc->desc.wMaxOutputLength) == 0) 312 return (IIC_ENOTSUPP); 313 if (len < 2) 314 return (IIC_ENOTSUPP); 315 316 DPRINTF(sc, "HID command I2C_HID_CMD_WRITE (len %d): " 317 "%*D\n", len, len, buf, " "); 318 319 return (iicbus_transfer(sc->dev, msgs, nitems(msgs))); 320 } 321 322 static int 323 iichid_cmd_get_hid_desc(struct iichid_softc *sc, uint16_t config_reg, 324 struct i2c_hid_desc *hid_desc) 325 { 326 /* 327 * 5.2.2 - HID Descriptor Retrieval 328 * register is passed from the controller. 329 */ 330 uint16_t cmd = htole16(config_reg); 331 struct iic_msg msgs[] = { 332 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, 2, (uint8_t *)&cmd }, 333 { sc->addr, IIC_M_RD, sizeof(*hid_desc), (uint8_t *)hid_desc }, 334 }; 335 int error; 336 337 DPRINTF(sc, "HID command I2C_HID_CMD_DESCR at 0x%x\n", config_reg); 338 339 error = iicbus_transfer(sc->dev, msgs, nitems(msgs)); 340 if (error != 0) 341 return (error); 342 343 DPRINTF(sc, "HID descriptor: %*D\n", 344 (int)sizeof(struct i2c_hid_desc), hid_desc, " "); 345 346 return (0); 347 } 348 349 static int 350 iichid_set_power(struct iichid_softc *sc, uint8_t param) 351 { 352 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister; 353 uint8_t cmd[] = { cmdreg[0], cmdreg[1], param, I2C_HID_CMD_SET_POWER }; 354 struct iic_msg msgs[] = { 355 { sc->addr, IIC_M_WR, sizeof(cmd), cmd }, 356 }; 357 358 DPRINTF(sc, "HID command I2C_HID_CMD_SET_POWER(%d)\n", param); 359 360 return (iicbus_transfer(sc->dev, msgs, nitems(msgs))); 361 } 362 363 static int 364 iichid_reset(struct iichid_softc *sc) 365 { 366 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister; 367 uint8_t cmd[] = { cmdreg[0], cmdreg[1], 0, I2C_HID_CMD_RESET }; 368 struct iic_msg msgs[] = { 369 { sc->addr, IIC_M_WR, sizeof(cmd), cmd }, 370 }; 371 372 DPRINTF(sc, "HID command I2C_HID_CMD_RESET\n"); 373 374 return (iicbus_transfer(sc->dev, msgs, nitems(msgs))); 375 } 376 377 static int 378 iichid_cmd_get_report_desc(struct iichid_softc* sc, void *buf, 379 iichid_size_t len) 380 { 381 uint16_t cmd = sc->desc.wReportDescRegister; 382 struct iic_msg msgs[] = { 383 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, 2, (uint8_t *)&cmd }, 384 { sc->addr, IIC_M_RD, len, buf }, 385 }; 386 int error; 387 388 DPRINTF(sc, "HID command I2C_HID_REPORT_DESCR at 0x%x with size %d\n", 389 le16toh(cmd), len); 390 391 error = iicbus_transfer(sc->dev, msgs, nitems(msgs)); 392 if (error != 0) 393 return (error); 394 395 DPRINTF(sc, "HID report descriptor: %*D\n", len, buf, " "); 396 397 return (0); 398 } 399 400 static int 401 iichid_cmd_get_report(struct iichid_softc* sc, void *buf, iichid_size_t maxlen, 402 iichid_size_t *actual_len, uint8_t type, uint8_t id) 403 { 404 /* 405 * 7.2.2.4 - "The protocol is optimized for Report < 15. If a 406 * report ID >= 15 is necessary, then the Report ID in the Low Byte 407 * must be set to 1111 and a Third Byte is appended to the protocol. 408 * This Third Byte contains the entire/actual report ID." 409 */ 410 uint8_t *dtareg = (uint8_t *)&sc->desc.wDataRegister; 411 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister; 412 uint8_t cmd[] = { /*________|______id>=15_____|______id<15______*/ 413 cmdreg[0] , 414 cmdreg[1] , 415 (id >= 15 ? 15 | (type << 4): id | (type << 4)), 416 I2C_HID_CMD_GET_REPORT , 417 (id >= 15 ? id : dtareg[0] ), 418 (id >= 15 ? dtareg[0] : dtareg[1] ), 419 (id >= 15 ? dtareg[1] : 0 ), 420 }; 421 int cmdlen = (id >= 15 ? 7 : 6 ); 422 uint8_t actbuf[2] = { 0, 0 }; 423 uint16_t actlen; 424 int d, error; 425 struct iic_msg msgs[] = { 426 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, cmdlen, cmd }, 427 { sc->addr, IIC_M_RD | IIC_M_NOSTOP, 2, actbuf }, 428 { sc->addr, IIC_M_RD | IIC_M_NOSTART, maxlen, buf }, 429 }; 430 431 if (maxlen == 0) 432 return (EINVAL); 433 434 DPRINTF(sc, "HID command I2C_HID_CMD_GET_REPORT %d " 435 "(type %d, len %d)\n", id, type, maxlen); 436 437 /* 438 * 7.2.2.2 - Response will be a 2-byte length value, the report 439 * id (1 byte, if defined in Report Descriptor), and then the report. 440 */ 441 error = iicbus_transfer(sc->dev, msgs, nitems(msgs)); 442 if (error != 0) 443 return (error); 444 445 actlen = actbuf[0] | actbuf[1] << 8; 446 if (actlen != maxlen + 2) 447 DPRINTF(sc, "response size %d != expected length %d\n", 448 actlen, maxlen + 2); 449 450 if (actlen <= 2 || actlen == 0xFFFF) 451 return (ENOMSG); 452 453 d = id != 0 ? *(uint8_t *)buf : 0; 454 if (d != id) { 455 DPRINTF(sc, "response report id %d != %d\n", d, id); 456 return (EBADMSG); 457 } 458 459 actlen -= 2; 460 if (actlen > maxlen) 461 actlen = maxlen; 462 if (actual_len != NULL) 463 *actual_len = actlen; 464 465 DPRINTF(sc, "response: %*D %*D\n", 2, actbuf, " ", actlen, buf, " "); 466 467 return (0); 468 } 469 470 static int 471 iichid_cmd_set_report(struct iichid_softc* sc, const void *buf, 472 iichid_size_t len, uint8_t type, uint8_t id) 473 { 474 /* 475 * 7.2.2.4 - "The protocol is optimized for Report < 15. If a 476 * report ID >= 15 is necessary, then the Report ID in the Low Byte 477 * must be set to 1111 and a Third Byte is appended to the protocol. 478 * This Third Byte contains the entire/actual report ID." 479 */ 480 uint8_t *dtareg = (uint8_t *)&sc->desc.wDataRegister; 481 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister; 482 uint16_t replen = 2 + len; 483 uint8_t cmd[] = { /*________|______id>=15_____|______id<15______*/ 484 cmdreg[0] , 485 cmdreg[1] , 486 (id >= 15 ? 15 | (type << 4): id | (type << 4)), 487 I2C_HID_CMD_SET_REPORT , 488 (id >= 15 ? id : dtareg[0] ), 489 (id >= 15 ? dtareg[0] : dtareg[1] ), 490 (id >= 15 ? dtareg[1] : replen & 0xff ), 491 (id >= 15 ? replen & 0xff : replen >> 8 ), 492 (id >= 15 ? replen >> 8 : 0 ), 493 }; 494 int cmdlen = (id >= 15 ? 9 : 8 ); 495 struct iic_msg msgs[] = { 496 {sc->addr, IIC_M_WR | IIC_M_NOSTOP, cmdlen, cmd}, 497 {sc->addr, IIC_M_WR | IIC_M_NOSTART, len, __DECONST(void *, buf)}, 498 }; 499 500 DPRINTF(sc, "HID command I2C_HID_CMD_SET_REPORT %d (type %d, len %d): " 501 "%*D\n", id, type, len, len, buf, " "); 502 503 return (iicbus_transfer(sc->dev, msgs, nitems(msgs))); 504 } 505 506 #ifdef IICHID_SAMPLING 507 static void 508 iichid_event_task(void *context, int pending) 509 { 510 struct iichid_softc *sc; 511 device_t parent; 512 iichid_size_t actual; 513 bool bus_requested; 514 int error; 515 516 sc = context; 517 parent = device_get_parent(sc->dev); 518 519 bus_requested = false; 520 if (iicbus_request_bus(parent, sc->dev, IIC_WAIT) != 0) 521 goto rearm; 522 bus_requested = true; 523 524 if (!sc->power_on) 525 goto out; 526 527 error = iichid_cmd_read(sc, sc->intr_buf, sc->intr_bufsize, &actual); 528 if (error == 0) { 529 if (actual > 0) { 530 sc->intr_handler(sc->intr_ctx, sc->intr_buf, actual); 531 sc->missing_samples = 0; 532 } else 533 ++sc->missing_samples; 534 } else 535 DPRINTF(sc, "read error occured: %d\n", error); 536 537 rearm: 538 if (sc->callout_setup && sc->sampling_rate_slow > 0) { 539 if (sc->missing_samples == sc->sampling_hysteresis) 540 sc->intr_handler(sc->intr_ctx, sc->intr_buf, 0); 541 taskqueue_enqueue_timeout(sc->taskqueue, &sc->periodic_task, 542 hz / MAX(sc->missing_samples >= sc->sampling_hysteresis ? 543 sc->sampling_rate_slow : sc->sampling_rate_fast, 1)); 544 } 545 out: 546 if (bus_requested) 547 iicbus_release_bus(parent, sc->dev); 548 } 549 #endif /* IICHID_SAMPLING */ 550 551 static void 552 iichid_intr(void *context) 553 { 554 struct iichid_softc *sc; 555 device_t parent; 556 iichid_size_t maxlen, actual; 557 int error; 558 559 sc = context; 560 parent = device_get_parent(sc->dev); 561 562 /* 563 * Designware(IG4) driver-specific hack. 564 * Requesting of an I2C bus with IIC_DONTWAIT parameter enables polled 565 * mode in the driver, making possible iicbus_transfer execution from 566 * interrupt handlers and callouts. 567 */ 568 if (iicbus_request_bus(parent, sc->dev, IIC_DONTWAIT) != 0) 569 return; 570 571 /* 572 * Reading of input reports of I2C devices residing in SLEEP state is 573 * not allowed and often returns a garbage. If a HOST needs to 574 * communicate with the DEVICE it MUST issue a SET POWER command 575 * (to ON) before any other command. As some hardware requires reads to 576 * acknoledge interrupts we fetch only length header and discard it. 577 */ 578 maxlen = sc->power_on ? sc->intr_bufsize : 0; 579 error = iichid_cmd_read(sc, sc->intr_buf, maxlen, &actual); 580 if (error == 0) { 581 if (sc->power_on) { 582 if (actual != 0) 583 sc->intr_handler(sc->intr_ctx, sc->intr_buf, 584 actual); 585 else 586 DPRINTF(sc, "no data received\n"); 587 } 588 } else 589 DPRINTF(sc, "read error occured: %d\n", error); 590 591 iicbus_release_bus(parent, sc->dev); 592 } 593 594 static int 595 iichid_set_power_state(struct iichid_softc *sc, 596 enum iichid_powerstate_how how_open, 597 enum iichid_powerstate_how how_suspend) 598 { 599 device_t parent; 600 int error; 601 int how_request; 602 bool power_on; 603 604 /* 605 * Request iicbus early as sc->suspend and sc->power_on 606 * are protected by iicbus internal lock. 607 */ 608 parent = device_get_parent(sc->dev); 609 /* Allow to interrupt open()/close() handlers by SIGINT */ 610 how_request = how_open == IICHID_PS_NULL ? IIC_WAIT : IIC_INTRWAIT; 611 error = iicbus_request_bus(parent, sc->dev, how_request); 612 if (error != 0) 613 return (error); 614 615 switch (how_open) { 616 case IICHID_PS_ON: 617 sc->open = true; 618 break; 619 case IICHID_PS_OFF: 620 sc->open = false; 621 break; 622 case IICHID_PS_NULL: 623 default: 624 break; 625 } 626 627 switch (how_suspend) { 628 case IICHID_PS_ON: 629 sc->suspend = false; 630 break; 631 case IICHID_PS_OFF: 632 sc->suspend = true; 633 break; 634 case IICHID_PS_NULL: 635 default: 636 break; 637 } 638 639 power_on = sc->open & !sc->suspend; 640 641 if (power_on != sc->power_on) { 642 error = iichid_set_power(sc, 643 power_on ? I2C_HID_POWER_ON : I2C_HID_POWER_OFF); 644 645 sc->power_on = power_on; 646 #ifdef IICHID_SAMPLING 647 if (sc->sampling_rate_slow >= 0 && sc->intr_handler != NULL) { 648 if (power_on) { 649 iichid_setup_callout(sc); 650 iichid_reset_callout(sc); 651 } else 652 iichid_teardown_callout(sc); 653 } 654 #endif 655 } 656 657 iicbus_release_bus(parent, sc->dev); 658 659 return (error); 660 } 661 662 static int 663 iichid_setup_interrupt(struct iichid_softc *sc) 664 { 665 sc->irq_cookie = 0; 666 667 int error = bus_setup_intr(sc->dev, sc->irq_res, 668 INTR_TYPE_TTY|INTR_MPSAFE, NULL, iichid_intr, sc, &sc->irq_cookie); 669 if (error != 0) 670 DPRINTF(sc, "Could not setup interrupt handler\n"); 671 else 672 DPRINTF(sc, "successfully setup interrupt\n"); 673 674 return (error); 675 } 676 677 static void 678 iichid_teardown_interrupt(struct iichid_softc *sc) 679 { 680 if (sc->irq_cookie) 681 bus_teardown_intr(sc->dev, sc->irq_res, sc->irq_cookie); 682 683 sc->irq_cookie = 0; 684 } 685 686 #ifdef IICHID_SAMPLING 687 static int 688 iichid_setup_callout(struct iichid_softc *sc) 689 { 690 691 if (sc->sampling_rate_slow < 0) { 692 DPRINTF(sc, "sampling_rate is below 0, can't setup callout\n"); 693 return (EINVAL); 694 } 695 696 sc->callout_setup = true; 697 DPRINTF(sc, "successfully setup callout\n"); 698 return (0); 699 } 700 701 static int 702 iichid_reset_callout(struct iichid_softc *sc) 703 { 704 705 if (sc->sampling_rate_slow <= 0) { 706 DPRINTF(sc, "sampling_rate is below or equal to 0, " 707 "can't reset callout\n"); 708 return (EINVAL); 709 } 710 711 if (!sc->callout_setup) 712 return (EINVAL); 713 714 /* Start with slow sampling. */ 715 sc->missing_samples = sc->sampling_hysteresis; 716 taskqueue_enqueue(sc->taskqueue, &sc->event_task); 717 718 return (0); 719 } 720 721 static void 722 iichid_teardown_callout(struct iichid_softc *sc) 723 { 724 725 sc->callout_setup = false; 726 taskqueue_cancel_timeout(sc->taskqueue, &sc->periodic_task, NULL); 727 DPRINTF(sc, "tore callout down\n"); 728 } 729 730 static int 731 iichid_sysctl_sampling_rate_handler(SYSCTL_HANDLER_ARGS) 732 { 733 struct iichid_softc *sc; 734 device_t parent; 735 int error, oldval, value; 736 737 sc = arg1; 738 739 value = sc->sampling_rate_slow; 740 error = sysctl_handle_int(oidp, &value, 0, req); 741 742 if (error != 0 || req->newptr == NULL || 743 value == sc->sampling_rate_slow) 744 return (error); 745 746 /* Can't switch to interrupt mode if it is not supported. */ 747 if (sc->irq_res == NULL && value < 0) 748 return (EINVAL); 749 750 parent = device_get_parent(sc->dev); 751 error = iicbus_request_bus(parent, sc->dev, IIC_WAIT); 752 if (error != 0) 753 return (iic2errno(error)); 754 755 oldval = sc->sampling_rate_slow; 756 sc->sampling_rate_slow = value; 757 758 if (oldval < 0 && value >= 0) { 759 iichid_teardown_interrupt(sc); 760 if (sc->power_on) 761 iichid_setup_callout(sc); 762 } else if (oldval >= 0 && value < 0) { 763 if (sc->power_on) 764 iichid_teardown_callout(sc); 765 iichid_setup_interrupt(sc); 766 } 767 768 if (sc->power_on && value > 0) 769 iichid_reset_callout(sc); 770 771 iicbus_release_bus(parent, sc->dev); 772 773 DPRINTF(sc, "new sampling_rate value: %d\n", value); 774 775 return (0); 776 } 777 #endif /* IICHID_SAMPLING */ 778 779 static void 780 iichid_intr_setup(device_t dev, hid_intr_t intr, void *context, 781 struct hid_rdesc_info *rdesc) 782 { 783 struct iichid_softc *sc; 784 785 sc = device_get_softc(dev); 786 /* 787 * Do not rely on wMaxInputLength, as some devices may set it to 788 * a wrong length. Find the longest input report in report descriptor. 789 */ 790 rdesc->rdsize = rdesc->isize; 791 /* Write and get/set_report sizes are limited by I2C-HID protocol. */ 792 rdesc->grsize = rdesc->srsize = IICHID_SIZE_MAX; 793 rdesc->wrsize = IICHID_SIZE_MAX; 794 795 sc->intr_handler = intr; 796 sc->intr_ctx = context; 797 sc->intr_buf = malloc(rdesc->rdsize, M_DEVBUF, M_WAITOK | M_ZERO); 798 sc->intr_bufsize = rdesc->rdsize; 799 #ifdef IICHID_SAMPLING 800 taskqueue_start_threads(&sc->taskqueue, 1, PI_TTY, 801 "%s taskq", device_get_nameunit(sc->dev)); 802 #endif 803 } 804 805 static void 806 iichid_intr_unsetup(device_t dev) 807 { 808 struct iichid_softc *sc; 809 810 sc = device_get_softc(dev); 811 #ifdef IICHID_SAMPLING 812 taskqueue_drain_all(sc->taskqueue); 813 #endif 814 free(sc->intr_buf, M_DEVBUF); 815 } 816 817 static int 818 iichid_intr_start(device_t dev) 819 { 820 struct iichid_softc *sc; 821 822 sc = device_get_softc(dev); 823 DPRINTF(sc, "iichid device open\n"); 824 iichid_set_power_state(sc, IICHID_PS_ON, IICHID_PS_NULL); 825 826 return (0); 827 } 828 829 static int 830 iichid_intr_stop(device_t dev) 831 { 832 struct iichid_softc *sc; 833 834 sc = device_get_softc(dev); 835 DPRINTF(sc, "iichid device close\n"); 836 /* 837 * 8.2 - The HOST determines that there are no active applications 838 * that are currently using the specific HID DEVICE. The HOST 839 * is recommended to issue a HIPO command to the DEVICE to force 840 * the DEVICE in to a lower power state. 841 */ 842 iichid_set_power_state(sc, IICHID_PS_OFF, IICHID_PS_NULL); 843 844 return (0); 845 } 846 847 static void 848 iichid_intr_poll(device_t dev) 849 { 850 struct iichid_softc *sc; 851 iichid_size_t actual; 852 int error; 853 854 sc = device_get_softc(dev); 855 error = iichid_cmd_read(sc, sc->intr_buf, sc->intr_bufsize, &actual); 856 if (error == 0 && actual != 0) 857 sc->intr_handler(sc->intr_ctx, sc->intr_buf, actual); 858 } 859 860 /* 861 * HID interface 862 */ 863 static int 864 iichid_get_rdesc(device_t dev, void *buf, hid_size_t len) 865 { 866 struct iichid_softc *sc; 867 int error; 868 869 sc = device_get_softc(dev); 870 error = iichid_cmd_get_report_desc(sc, buf, len); 871 if (error) 872 DPRINTF(sc, "failed to fetch report descriptor: %d\n", error); 873 874 return (iic2errno(error)); 875 } 876 877 static int 878 iichid_read(device_t dev, void *buf, hid_size_t maxlen, hid_size_t *actlen) 879 { 880 struct iichid_softc *sc; 881 device_t parent; 882 int error; 883 884 if (maxlen > IICHID_SIZE_MAX) 885 return (EMSGSIZE); 886 sc = device_get_softc(dev); 887 parent = device_get_parent(sc->dev); 888 error = iicbus_request_bus(parent, sc->dev, IIC_WAIT); 889 if (error == 0) { 890 error = iichid_cmd_read(sc, buf, maxlen, actlen); 891 iicbus_release_bus(parent, sc->dev); 892 } 893 return (iic2errno(error)); 894 } 895 896 static int 897 iichid_write(device_t dev, const void *buf, hid_size_t len) 898 { 899 struct iichid_softc *sc; 900 901 if (len > IICHID_SIZE_MAX) 902 return (EMSGSIZE); 903 sc = device_get_softc(dev); 904 return (iic2errno(iichid_cmd_write(sc, buf, len))); 905 } 906 907 static int 908 iichid_get_report(device_t dev, void *buf, hid_size_t maxlen, 909 hid_size_t *actlen, uint8_t type, uint8_t id) 910 { 911 struct iichid_softc *sc; 912 913 if (maxlen > IICHID_SIZE_MAX) 914 return (EMSGSIZE); 915 sc = device_get_softc(dev); 916 return (iic2errno( 917 iichid_cmd_get_report(sc, buf, maxlen, actlen, type, id))); 918 } 919 920 static int 921 iichid_set_report(device_t dev, const void *buf, hid_size_t len, uint8_t type, 922 uint8_t id) 923 { 924 struct iichid_softc *sc; 925 926 if (len > IICHID_SIZE_MAX) 927 return (EMSGSIZE); 928 sc = device_get_softc(dev); 929 return (iic2errno(iichid_cmd_set_report(sc, buf, len, type, id))); 930 } 931 932 static int 933 iichid_set_idle(device_t dev, uint16_t duration, uint8_t id) 934 { 935 return (ENOTSUP); 936 } 937 938 static int 939 iichid_set_protocol(device_t dev, uint16_t protocol) 940 { 941 return (ENOTSUP); 942 } 943 944 static int 945 iichid_fill_device_info(struct i2c_hid_desc *desc, ACPI_HANDLE handle, 946 struct hid_device_info *hw) 947 { 948 ACPI_DEVICE_INFO *device_info; 949 950 hw->idBus = BUS_I2C; 951 hw->idVendor = le16toh(desc->wVendorID); 952 hw->idProduct = le16toh(desc->wProductID); 953 hw->idVersion = le16toh(desc->wVersionID); 954 955 /* get ACPI HID. It is a base part of the device name. */ 956 if (ACPI_FAILURE(AcpiGetObjectInfo(handle, &device_info))) 957 return (ENXIO); 958 959 if (device_info->Valid & ACPI_VALID_HID) 960 strlcpy(hw->idPnP, device_info->HardwareId.String, 961 HID_PNP_ID_SIZE); 962 snprintf(hw->name, sizeof(hw->name), "%s:%02lX %04X:%04X", 963 (device_info->Valid & ACPI_VALID_HID) ? 964 device_info->HardwareId.String : "Unknown", 965 (device_info->Valid & ACPI_VALID_UID) ? 966 strtoul(device_info->UniqueId.String, NULL, 10) : 0UL, 967 le16toh(desc->wVendorID), le16toh(desc->wProductID)); 968 969 AcpiOsFree(device_info); 970 971 strlcpy(hw->serial, "", sizeof(hw->serial)); 972 hw->rdescsize = le16toh(desc->wReportDescLength); 973 if (desc->wOutputRegister == 0 || desc->wMaxOutputLength == 0) 974 hid_add_dynamic_quirk(hw, HQ_NOWRITE); 975 976 return (0); 977 } 978 979 static int 980 iichid_probe(device_t dev) 981 { 982 struct iichid_softc *sc; 983 ACPI_HANDLE handle; 984 char buf[80]; 985 uint16_t config_reg; 986 int error; 987 988 sc = device_get_softc(dev); 989 sc->dev = dev; 990 if (sc->probe_done) 991 goto done; 992 993 sc->probe_done = true; 994 sc->probe_result = ENXIO; 995 996 if (acpi_disabled("iichid")) 997 return (ENXIO); 998 999 sc->addr = iicbus_get_addr(dev) << 1; 1000 if (sc->addr == 0) 1001 return (ENXIO); 1002 1003 handle = acpi_get_handle(dev); 1004 if (handle == NULL) 1005 return (ENXIO); 1006 1007 if (!acpi_is_iichid(handle)) 1008 return (ENXIO); 1009 1010 if (ACPI_FAILURE(iichid_get_config_reg(handle, &config_reg))) 1011 return (ENXIO); 1012 1013 DPRINTF(sc, " IICbus addr : 0x%02X\n", sc->addr >> 1); 1014 DPRINTF(sc, " HID descriptor reg: 0x%02X\n", config_reg); 1015 1016 error = iichid_cmd_get_hid_desc(sc, config_reg, &sc->desc); 1017 if (error) { 1018 DPRINTF(sc, "could not retrieve HID descriptor from the " 1019 "device: %d\n", error); 1020 return (ENXIO); 1021 } 1022 1023 if (le16toh(sc->desc.wHIDDescLength) != 30 || 1024 le16toh(sc->desc.bcdVersion) != 0x100) { 1025 DPRINTF(sc, "HID descriptor is broken\n"); 1026 return (ENXIO); 1027 } 1028 1029 /* Setup hid_device_info so we can figure out quirks for the device. */ 1030 if (iichid_fill_device_info(&sc->desc, handle, &sc->hw) != 0) { 1031 DPRINTF(sc, "error evaluating AcpiGetObjectInfo\n"); 1032 return (ENXIO); 1033 } 1034 1035 if (hid_test_quirk(&sc->hw, HQ_HID_IGNORE)) 1036 return (ENXIO); 1037 1038 sc->probe_result = BUS_PROBE_DEFAULT; 1039 done: 1040 if (sc->probe_result <= BUS_PROBE_SPECIFIC) { 1041 snprintf(buf, sizeof(buf), "%s I2C HID device", sc->hw.name); 1042 device_set_desc_copy(dev, buf); 1043 } 1044 return (sc->probe_result); 1045 } 1046 1047 static int 1048 iichid_attach(device_t dev) 1049 { 1050 struct iichid_softc *sc; 1051 device_t child; 1052 int error; 1053 1054 sc = device_get_softc(dev); 1055 error = iichid_set_power(sc, I2C_HID_POWER_ON); 1056 if (error) { 1057 device_printf(dev, "failed to power on: %d\n", error); 1058 return (ENXIO); 1059 } 1060 /* 1061 * Windows driver sleeps for 1ms between the SET_POWER and RESET 1062 * commands. So we too as some devices may depend on this. 1063 */ 1064 pause("iichid", (hz + 999) / 1000); 1065 1066 error = iichid_reset(sc); 1067 if (error) { 1068 device_printf(dev, "failed to reset hardware: %d\n", error); 1069 return (ENXIO); 1070 } 1071 1072 sc->power_on = false; 1073 #ifdef IICHID_SAMPLING 1074 TASK_INIT(&sc->event_task, 0, iichid_event_task, sc); 1075 /* taskqueue_create can't fail with M_WAITOK mflag passed. */ 1076 sc->taskqueue = taskqueue_create("hmt_tq", M_WAITOK | M_ZERO, 1077 taskqueue_thread_enqueue, &sc->taskqueue); 1078 TIMEOUT_TASK_INIT(sc->taskqueue, &sc->periodic_task, 0, 1079 iichid_event_task, sc); 1080 1081 sc->sampling_rate_slow = -1; 1082 sc->sampling_rate_fast = IICHID_SAMPLING_RATE_FAST; 1083 sc->sampling_hysteresis = IICHID_SAMPLING_HYSTERESIS; 1084 #endif 1085 1086 sc->irq_rid = 0; 1087 sc->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, 1088 &sc->irq_rid, RF_ACTIVE); 1089 1090 if (sc->irq_res != NULL) { 1091 DPRINTF(sc, "allocated irq at %p and rid %d\n", 1092 sc->irq_res, sc->irq_rid); 1093 error = iichid_setup_interrupt(sc); 1094 } 1095 1096 if (sc->irq_res == NULL || error != 0) { 1097 #ifdef IICHID_SAMPLING 1098 device_printf(sc->dev, 1099 "Interrupt setup failed. Fallback to sampling\n"); 1100 sc->sampling_rate_slow = IICHID_SAMPLING_RATE_SLOW; 1101 #else 1102 device_printf(sc->dev, "Interrupt setup failed\n"); 1103 if (sc->irq_res != NULL) 1104 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, 1105 sc->irq_res); 1106 error = ENXIO; 1107 goto done; 1108 #endif 1109 } 1110 1111 #ifdef IICHID_SAMPLING 1112 SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev), 1113 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), 1114 OID_AUTO, "sampling_rate_slow", CTLTYPE_INT | CTLFLAG_RWTUN, 1115 sc, 0, iichid_sysctl_sampling_rate_handler, "I", 1116 "idle sampling rate in num/second"); 1117 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev), 1118 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), 1119 OID_AUTO, "sampling_rate_fast", CTLFLAG_RWTUN, 1120 &sc->sampling_rate_fast, 0, 1121 "active sampling rate in num/second"); 1122 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev), 1123 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), 1124 OID_AUTO, "sampling_hysteresis", CTLFLAG_RWTUN, 1125 &sc->sampling_hysteresis, 0, 1126 "number of missing samples before enabling of slow mode"); 1127 hid_add_dynamic_quirk(&sc->hw, HQ_IICHID_SAMPLING); 1128 #endif /* IICHID_SAMPLING */ 1129 1130 child = device_add_child(dev, "hidbus", -1); 1131 if (child == NULL) { 1132 device_printf(sc->dev, "Could not add I2C device\n"); 1133 iichid_detach(dev); 1134 error = ENOMEM; 1135 goto done; 1136 } 1137 1138 device_set_ivars(child, &sc->hw); 1139 error = bus_generic_attach(dev); 1140 if (error) { 1141 device_printf(dev, "failed to attach child: error %d\n", error); 1142 iichid_detach(dev); 1143 } 1144 done: 1145 (void)iichid_set_power(sc, I2C_HID_POWER_OFF); 1146 return (error); 1147 } 1148 1149 static int 1150 iichid_detach(device_t dev) 1151 { 1152 struct iichid_softc *sc; 1153 int error; 1154 1155 sc = device_get_softc(dev); 1156 error = device_delete_children(dev); 1157 if (error) 1158 return (error); 1159 iichid_teardown_interrupt(sc); 1160 if (sc->irq_res != NULL) 1161 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, 1162 sc->irq_res); 1163 #ifdef IICHID_SAMPLING 1164 if (sc->taskqueue != NULL) 1165 taskqueue_free(sc->taskqueue); 1166 sc->taskqueue = NULL; 1167 #endif 1168 return (0); 1169 } 1170 1171 static int 1172 iichid_suspend(device_t dev) 1173 { 1174 struct iichid_softc *sc; 1175 int error; 1176 1177 sc = device_get_softc(dev); 1178 DPRINTF(sc, "Suspend called, setting device to power_state 1\n"); 1179 (void)bus_generic_suspend(dev); 1180 /* 1181 * 8.2 - The HOST is going into a deep power optimized state and wishes 1182 * to put all the devices into a low power state also. The HOST 1183 * is recommended to issue a HIPO command to the DEVICE to force 1184 * the DEVICE in to a lower power state. 1185 */ 1186 error = iichid_set_power_state(sc, IICHID_PS_NULL, IICHID_PS_OFF); 1187 if (error != 0) 1188 DPRINTF(sc, "Could not set power_state, error: %d\n", error); 1189 else 1190 DPRINTF(sc, "Successfully set power_state\n"); 1191 1192 return (0); 1193 } 1194 1195 static int 1196 iichid_resume(device_t dev) 1197 { 1198 struct iichid_softc *sc; 1199 int error; 1200 1201 sc = device_get_softc(dev); 1202 DPRINTF(sc, "Resume called, setting device to power_state 0\n"); 1203 error = iichid_set_power_state(sc, IICHID_PS_NULL, IICHID_PS_ON); 1204 if (error != 0) 1205 DPRINTF(sc, "Could not set power_state, error: %d\n", error); 1206 else 1207 DPRINTF(sc, "Successfully set power_state\n"); 1208 (void)bus_generic_resume(dev); 1209 1210 return (0); 1211 } 1212 1213 static devclass_t iichid_devclass; 1214 1215 static device_method_t iichid_methods[] = { 1216 DEVMETHOD(device_probe, iichid_probe), 1217 DEVMETHOD(device_attach, iichid_attach), 1218 DEVMETHOD(device_detach, iichid_detach), 1219 DEVMETHOD(device_suspend, iichid_suspend), 1220 DEVMETHOD(device_resume, iichid_resume), 1221 1222 DEVMETHOD(hid_intr_setup, iichid_intr_setup), 1223 DEVMETHOD(hid_intr_unsetup, iichid_intr_unsetup), 1224 DEVMETHOD(hid_intr_start, iichid_intr_start), 1225 DEVMETHOD(hid_intr_stop, iichid_intr_stop), 1226 DEVMETHOD(hid_intr_poll, iichid_intr_poll), 1227 1228 /* HID interface */ 1229 DEVMETHOD(hid_get_rdesc, iichid_get_rdesc), 1230 DEVMETHOD(hid_read, iichid_read), 1231 DEVMETHOD(hid_write, iichid_write), 1232 DEVMETHOD(hid_get_report, iichid_get_report), 1233 DEVMETHOD(hid_set_report, iichid_set_report), 1234 DEVMETHOD(hid_set_idle, iichid_set_idle), 1235 DEVMETHOD(hid_set_protocol, iichid_set_protocol), 1236 1237 DEVMETHOD_END 1238 }; 1239 1240 static driver_t iichid_driver = { 1241 .name = "iichid", 1242 .methods = iichid_methods, 1243 .size = sizeof(struct iichid_softc), 1244 }; 1245 1246 DRIVER_MODULE(iichid, iicbus, iichid_driver, iichid_devclass, NULL, 0); 1247 MODULE_DEPEND(iichid, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 1248 MODULE_DEPEND(iichid, acpi, 1, 1, 1); 1249 MODULE_DEPEND(iichid, hid, 1, 1, 1); 1250 MODULE_DEPEND(iichid, hidbus, 1, 1, 1); 1251 MODULE_VERSION(iichid, 1); 1252 IICBUS_ACPI_PNP_INFO(iichid_ids); 1253