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 * acknowledge 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 if (intr == NULL) 786 return; 787 788 sc = device_get_softc(dev); 789 /* 790 * Do not rely on wMaxInputLength, as some devices may set it to 791 * a wrong length. Find the longest input report in report descriptor. 792 */ 793 rdesc->rdsize = rdesc->isize; 794 /* Write and get/set_report sizes are limited by I2C-HID protocol. */ 795 rdesc->grsize = rdesc->srsize = IICHID_SIZE_MAX; 796 rdesc->wrsize = IICHID_SIZE_MAX; 797 798 sc->intr_handler = intr; 799 sc->intr_ctx = context; 800 sc->intr_buf = malloc(rdesc->rdsize, M_DEVBUF, M_WAITOK | M_ZERO); 801 sc->intr_bufsize = rdesc->rdsize; 802 #ifdef IICHID_SAMPLING 803 taskqueue_start_threads(&sc->taskqueue, 1, PI_TTY, 804 "%s taskq", device_get_nameunit(sc->dev)); 805 #endif 806 } 807 808 static void 809 iichid_intr_unsetup(device_t dev) 810 { 811 struct iichid_softc *sc; 812 813 sc = device_get_softc(dev); 814 #ifdef IICHID_SAMPLING 815 taskqueue_drain_all(sc->taskqueue); 816 #endif 817 free(sc->intr_buf, M_DEVBUF); 818 } 819 820 static int 821 iichid_intr_start(device_t dev) 822 { 823 struct iichid_softc *sc; 824 825 sc = device_get_softc(dev); 826 DPRINTF(sc, "iichid device open\n"); 827 iichid_set_power_state(sc, IICHID_PS_ON, IICHID_PS_NULL); 828 829 return (0); 830 } 831 832 static int 833 iichid_intr_stop(device_t dev) 834 { 835 struct iichid_softc *sc; 836 837 sc = device_get_softc(dev); 838 DPRINTF(sc, "iichid device close\n"); 839 /* 840 * 8.2 - The HOST determines that there are no active applications 841 * that are currently using the specific HID DEVICE. The HOST 842 * is recommended to issue a HIPO command to the DEVICE to force 843 * the DEVICE in to a lower power state. 844 */ 845 iichid_set_power_state(sc, IICHID_PS_OFF, IICHID_PS_NULL); 846 847 return (0); 848 } 849 850 static void 851 iichid_intr_poll(device_t dev) 852 { 853 struct iichid_softc *sc; 854 iichid_size_t actual; 855 int error; 856 857 sc = device_get_softc(dev); 858 error = iichid_cmd_read(sc, sc->intr_buf, sc->intr_bufsize, &actual); 859 if (error == 0 && actual != 0) 860 sc->intr_handler(sc->intr_ctx, sc->intr_buf, actual); 861 } 862 863 /* 864 * HID interface 865 */ 866 static int 867 iichid_get_rdesc(device_t dev, void *buf, hid_size_t len) 868 { 869 struct iichid_softc *sc; 870 int error; 871 872 sc = device_get_softc(dev); 873 error = iichid_cmd_get_report_desc(sc, buf, len); 874 if (error) 875 DPRINTF(sc, "failed to fetch report descriptor: %d\n", error); 876 877 return (iic2errno(error)); 878 } 879 880 static int 881 iichid_read(device_t dev, void *buf, hid_size_t maxlen, hid_size_t *actlen) 882 { 883 struct iichid_softc *sc; 884 device_t parent; 885 int error; 886 887 if (maxlen > IICHID_SIZE_MAX) 888 return (EMSGSIZE); 889 sc = device_get_softc(dev); 890 parent = device_get_parent(sc->dev); 891 error = iicbus_request_bus(parent, sc->dev, IIC_WAIT); 892 if (error == 0) { 893 error = iichid_cmd_read(sc, buf, maxlen, actlen); 894 iicbus_release_bus(parent, sc->dev); 895 } 896 return (iic2errno(error)); 897 } 898 899 static int 900 iichid_write(device_t dev, const void *buf, hid_size_t len) 901 { 902 struct iichid_softc *sc; 903 904 if (len > IICHID_SIZE_MAX) 905 return (EMSGSIZE); 906 sc = device_get_softc(dev); 907 return (iic2errno(iichid_cmd_write(sc, buf, len))); 908 } 909 910 static int 911 iichid_get_report(device_t dev, void *buf, hid_size_t maxlen, 912 hid_size_t *actlen, uint8_t type, uint8_t id) 913 { 914 struct iichid_softc *sc; 915 916 if (maxlen > IICHID_SIZE_MAX) 917 return (EMSGSIZE); 918 sc = device_get_softc(dev); 919 return (iic2errno( 920 iichid_cmd_get_report(sc, buf, maxlen, actlen, type, id))); 921 } 922 923 static int 924 iichid_set_report(device_t dev, const void *buf, hid_size_t len, uint8_t type, 925 uint8_t id) 926 { 927 struct iichid_softc *sc; 928 929 if (len > IICHID_SIZE_MAX) 930 return (EMSGSIZE); 931 sc = device_get_softc(dev); 932 return (iic2errno(iichid_cmd_set_report(sc, buf, len, type, id))); 933 } 934 935 static int 936 iichid_set_idle(device_t dev, uint16_t duration, uint8_t id) 937 { 938 return (ENOTSUP); 939 } 940 941 static int 942 iichid_set_protocol(device_t dev, uint16_t protocol) 943 { 944 return (ENOTSUP); 945 } 946 947 static int 948 iichid_fill_device_info(struct i2c_hid_desc *desc, ACPI_HANDLE handle, 949 struct hid_device_info *hw) 950 { 951 ACPI_DEVICE_INFO *device_info; 952 953 hw->idBus = BUS_I2C; 954 hw->idVendor = le16toh(desc->wVendorID); 955 hw->idProduct = le16toh(desc->wProductID); 956 hw->idVersion = le16toh(desc->wVersionID); 957 958 /* get ACPI HID. It is a base part of the device name. */ 959 if (ACPI_FAILURE(AcpiGetObjectInfo(handle, &device_info))) 960 return (ENXIO); 961 962 if (device_info->Valid & ACPI_VALID_HID) 963 strlcpy(hw->idPnP, device_info->HardwareId.String, 964 HID_PNP_ID_SIZE); 965 snprintf(hw->name, sizeof(hw->name), "%s:%02lX %04X:%04X", 966 (device_info->Valid & ACPI_VALID_HID) ? 967 device_info->HardwareId.String : "Unknown", 968 (device_info->Valid & ACPI_VALID_UID) ? 969 strtoul(device_info->UniqueId.String, NULL, 10) : 0UL, 970 le16toh(desc->wVendorID), le16toh(desc->wProductID)); 971 972 AcpiOsFree(device_info); 973 974 strlcpy(hw->serial, "", sizeof(hw->serial)); 975 hw->rdescsize = le16toh(desc->wReportDescLength); 976 if (desc->wOutputRegister == 0 || desc->wMaxOutputLength == 0) 977 hid_add_dynamic_quirk(hw, HQ_NOWRITE); 978 979 return (0); 980 } 981 982 static int 983 iichid_probe(device_t dev) 984 { 985 struct iichid_softc *sc; 986 ACPI_HANDLE handle; 987 char buf[80]; 988 uint16_t config_reg; 989 int error; 990 991 sc = device_get_softc(dev); 992 sc->dev = dev; 993 if (sc->probe_done) 994 goto done; 995 996 sc->probe_done = true; 997 sc->probe_result = ENXIO; 998 999 if (acpi_disabled("iichid")) 1000 return (ENXIO); 1001 1002 sc->addr = iicbus_get_addr(dev) << 1; 1003 if (sc->addr == 0) 1004 return (ENXIO); 1005 1006 handle = acpi_get_handle(dev); 1007 if (handle == NULL) 1008 return (ENXIO); 1009 1010 if (!acpi_is_iichid(handle)) 1011 return (ENXIO); 1012 1013 if (ACPI_FAILURE(iichid_get_config_reg(handle, &config_reg))) 1014 return (ENXIO); 1015 1016 DPRINTF(sc, " IICbus addr : 0x%02X\n", sc->addr >> 1); 1017 DPRINTF(sc, " HID descriptor reg: 0x%02X\n", config_reg); 1018 1019 error = iichid_cmd_get_hid_desc(sc, config_reg, &sc->desc); 1020 if (error) { 1021 DPRINTF(sc, "could not retrieve HID descriptor from the " 1022 "device: %d\n", error); 1023 return (ENXIO); 1024 } 1025 1026 if (le16toh(sc->desc.wHIDDescLength) != 30 || 1027 le16toh(sc->desc.bcdVersion) != 0x100) { 1028 DPRINTF(sc, "HID descriptor is broken\n"); 1029 return (ENXIO); 1030 } 1031 1032 /* Setup hid_device_info so we can figure out quirks for the device. */ 1033 if (iichid_fill_device_info(&sc->desc, handle, &sc->hw) != 0) { 1034 DPRINTF(sc, "error evaluating AcpiGetObjectInfo\n"); 1035 return (ENXIO); 1036 } 1037 1038 if (hid_test_quirk(&sc->hw, HQ_HID_IGNORE)) 1039 return (ENXIO); 1040 1041 sc->probe_result = BUS_PROBE_DEFAULT; 1042 done: 1043 if (sc->probe_result <= BUS_PROBE_SPECIFIC) { 1044 snprintf(buf, sizeof(buf), "%s I2C HID device", sc->hw.name); 1045 device_set_desc_copy(dev, buf); 1046 } 1047 return (sc->probe_result); 1048 } 1049 1050 static int 1051 iichid_attach(device_t dev) 1052 { 1053 struct iichid_softc *sc; 1054 device_t child; 1055 int error; 1056 1057 sc = device_get_softc(dev); 1058 error = iichid_set_power(sc, I2C_HID_POWER_ON); 1059 if (error) { 1060 device_printf(dev, "failed to power on: %d\n", error); 1061 return (ENXIO); 1062 } 1063 /* 1064 * Windows driver sleeps for 1ms between the SET_POWER and RESET 1065 * commands. So we too as some devices may depend on this. 1066 */ 1067 pause("iichid", (hz + 999) / 1000); 1068 1069 error = iichid_reset(sc); 1070 if (error) { 1071 device_printf(dev, "failed to reset hardware: %d\n", error); 1072 error = ENXIO; 1073 goto done; 1074 } 1075 1076 sc->power_on = true; 1077 1078 #ifdef IICHID_SAMPLING 1079 TASK_INIT(&sc->event_task, 0, iichid_event_task, sc); 1080 /* taskqueue_create can't fail with M_WAITOK mflag passed. */ 1081 sc->taskqueue = taskqueue_create("iichid_tq", M_WAITOK | M_ZERO, 1082 taskqueue_thread_enqueue, &sc->taskqueue); 1083 TIMEOUT_TASK_INIT(sc->taskqueue, &sc->periodic_task, 0, 1084 iichid_event_task, sc); 1085 1086 sc->sampling_rate_slow = -1; 1087 sc->sampling_rate_fast = IICHID_SAMPLING_RATE_FAST; 1088 sc->sampling_hysteresis = IICHID_SAMPLING_HYSTERESIS; 1089 #endif 1090 1091 sc->irq_rid = 0; 1092 sc->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, 1093 &sc->irq_rid, RF_ACTIVE); 1094 1095 if (sc->irq_res != NULL) { 1096 DPRINTF(sc, "allocated irq at %p and rid %d\n", 1097 sc->irq_res, sc->irq_rid); 1098 error = iichid_setup_interrupt(sc); 1099 } 1100 1101 if (sc->irq_res == NULL || error != 0) { 1102 #ifdef IICHID_SAMPLING 1103 device_printf(sc->dev, 1104 "Interrupt setup failed. Fallback to sampling\n"); 1105 sc->sampling_rate_slow = IICHID_SAMPLING_RATE_SLOW; 1106 #else 1107 device_printf(sc->dev, "Interrupt setup failed\n"); 1108 if (sc->irq_res != NULL) 1109 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, 1110 sc->irq_res); 1111 error = ENXIO; 1112 goto done; 1113 #endif 1114 } 1115 1116 #ifdef IICHID_SAMPLING 1117 SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev), 1118 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), 1119 OID_AUTO, "sampling_rate_slow", CTLTYPE_INT | CTLFLAG_RWTUN, 1120 sc, 0, iichid_sysctl_sampling_rate_handler, "I", 1121 "idle 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_rate_fast", CTLFLAG_RWTUN, 1125 &sc->sampling_rate_fast, 0, 1126 "active sampling rate in num/second"); 1127 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev), 1128 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), 1129 OID_AUTO, "sampling_hysteresis", CTLFLAG_RWTUN, 1130 &sc->sampling_hysteresis, 0, 1131 "number of missing samples before enabling of slow mode"); 1132 hid_add_dynamic_quirk(&sc->hw, HQ_IICHID_SAMPLING); 1133 #endif /* IICHID_SAMPLING */ 1134 1135 child = device_add_child(dev, "hidbus", -1); 1136 if (child == NULL) { 1137 device_printf(sc->dev, "Could not add I2C device\n"); 1138 iichid_detach(dev); 1139 error = ENOMEM; 1140 goto done; 1141 } 1142 1143 device_set_ivars(child, &sc->hw); 1144 error = bus_generic_attach(dev); 1145 if (error) { 1146 device_printf(dev, "failed to attach child: error %d\n", error); 1147 iichid_detach(dev); 1148 } 1149 1150 done: 1151 (void)iichid_set_power(sc, I2C_HID_POWER_OFF); 1152 sc->power_on = false; 1153 return (error); 1154 } 1155 1156 static int 1157 iichid_detach(device_t dev) 1158 { 1159 struct iichid_softc *sc; 1160 int error; 1161 1162 sc = device_get_softc(dev); 1163 error = device_delete_children(dev); 1164 if (error) 1165 return (error); 1166 iichid_teardown_interrupt(sc); 1167 if (sc->irq_res != NULL) 1168 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, 1169 sc->irq_res); 1170 #ifdef IICHID_SAMPLING 1171 if (sc->taskqueue != NULL) 1172 taskqueue_free(sc->taskqueue); 1173 sc->taskqueue = NULL; 1174 #endif 1175 return (0); 1176 } 1177 1178 static int 1179 iichid_suspend(device_t dev) 1180 { 1181 struct iichid_softc *sc; 1182 int error; 1183 1184 sc = device_get_softc(dev); 1185 (void)bus_generic_suspend(dev); 1186 #ifdef IICHID_SAMPLING 1187 if (sc->sampling_rate_slow < 0) 1188 #endif 1189 (void)bus_generic_suspend_intr(device_get_parent(dev), dev, 1190 sc->irq_res); 1191 1192 /* 1193 * 8.2 - The HOST is going into a deep power optimized state and wishes 1194 * to put all the devices into a low power state also. The HOST 1195 * is recommended to issue a HIPO command to the DEVICE to force 1196 * the DEVICE in to a lower power state. 1197 */ 1198 DPRINTF(sc, "Suspend called, setting device to power_state 1\n"); 1199 error = iichid_set_power_state(sc, IICHID_PS_NULL, IICHID_PS_OFF); 1200 if (error != 0) 1201 DPRINTF(sc, "Could not set power_state, error: %d\n", error); 1202 else 1203 DPRINTF(sc, "Successfully set power_state\n"); 1204 1205 return (0); 1206 } 1207 1208 static int 1209 iichid_resume(device_t dev) 1210 { 1211 struct iichid_softc *sc; 1212 int error; 1213 1214 sc = device_get_softc(dev); 1215 DPRINTF(sc, "Resume called, setting device to power_state 0\n"); 1216 error = iichid_set_power_state(sc, IICHID_PS_NULL, IICHID_PS_ON); 1217 if (error != 0) 1218 DPRINTF(sc, "Could not set power_state, error: %d\n", error); 1219 else 1220 DPRINTF(sc, "Successfully set power_state\n"); 1221 (void)bus_generic_resume(dev); 1222 #ifdef IICHID_SAMPLING 1223 if (sc->sampling_rate_slow < 0) 1224 #endif 1225 (void)bus_generic_resume_intr(device_get_parent(dev), dev, 1226 sc->irq_res); 1227 1228 return (0); 1229 } 1230 1231 static devclass_t iichid_devclass; 1232 1233 static device_method_t iichid_methods[] = { 1234 DEVMETHOD(device_probe, iichid_probe), 1235 DEVMETHOD(device_attach, iichid_attach), 1236 DEVMETHOD(device_detach, iichid_detach), 1237 DEVMETHOD(device_suspend, iichid_suspend), 1238 DEVMETHOD(device_resume, iichid_resume), 1239 1240 DEVMETHOD(hid_intr_setup, iichid_intr_setup), 1241 DEVMETHOD(hid_intr_unsetup, iichid_intr_unsetup), 1242 DEVMETHOD(hid_intr_start, iichid_intr_start), 1243 DEVMETHOD(hid_intr_stop, iichid_intr_stop), 1244 DEVMETHOD(hid_intr_poll, iichid_intr_poll), 1245 1246 /* HID interface */ 1247 DEVMETHOD(hid_get_rdesc, iichid_get_rdesc), 1248 DEVMETHOD(hid_read, iichid_read), 1249 DEVMETHOD(hid_write, iichid_write), 1250 DEVMETHOD(hid_get_report, iichid_get_report), 1251 DEVMETHOD(hid_set_report, iichid_set_report), 1252 DEVMETHOD(hid_set_idle, iichid_set_idle), 1253 DEVMETHOD(hid_set_protocol, iichid_set_protocol), 1254 1255 DEVMETHOD_END 1256 }; 1257 1258 static driver_t iichid_driver = { 1259 .name = "iichid", 1260 .methods = iichid_methods, 1261 .size = sizeof(struct iichid_softc), 1262 }; 1263 1264 DRIVER_MODULE(iichid, iicbus, iichid_driver, iichid_devclass, NULL, 0); 1265 MODULE_DEPEND(iichid, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 1266 MODULE_DEPEND(iichid, acpi, 1, 1, 1); 1267 MODULE_DEPEND(iichid, hid, 1, 1, 1); 1268 MODULE_DEPEND(iichid, hidbus, 1, 1, 1); 1269 MODULE_VERSION(iichid, 1); 1270 IICBUS_ACPI_PNP_INFO(iichid_ids); 1271