Lines Matching +full:report +full:- +full:rate +full:- +full:hz

1 /*-
2 * Copyright (c) 2018-2019 Marc Priggemeyer <marc.priggemeyer@gmail.com>
3 * Copyright (c) 2019-2020 Vladimir Kondratyev <wulf@FreeBSD.org>
74 device_printf((sc)->dev, __VA_ARGS__); \
83 #define IICHID_SIZE_MAX (UINT16_MAX - 2)
108 * callout and, thereby, disable further report requests. Do not set the
116 /* 5.1.1 - HID Descriptor Format */
134 #define IICHID_REG_NONE -1
220 for (ids = iichid_ids; ids->id != NULL; ids++) { in acpi_is_iichid()
221 if (acpi_MatchHid(handle, ids->id)) { in acpi_is_iichid()
222 reg = ids->reg; in acpi_is_iichid()
226 if (ids->id == NULL) in acpi_is_iichid()
251 /* 3cdff6f7-4267-4555-ad05-b30a3d8938de */ in iichid_get_config_reg()
264 *config_reg = result->Integer.Value & 0xFFFF; in iichid_get_config_reg()
275 * 6.1.3 - Retrieval of Input Reports
276 * DEVICE returns the length (2 Bytes) and the entire Input Report.
279 /* Read actual input report length. */
281 { sc->addr, IIC_M_RD | IIC_M_NOSTOP, sizeof(actbuf), actbuf },
286 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
292 if ((actlen == 0 && sc->sampling_rate_slow < 0) ||
293 (maxlen == 0 && sc->sampling_rate_slow >= 0)) {
299 { sc->addr, IIC_M_RD | IIC_M_NOSTART,
300 le16toh(sc->desc.wMaxInputLength) - 2, sc->intr_buf };
302 if (!sc->reset_acked) {
303 mtx_lock(&sc->mtx);
304 sc->reset_acked = true;
305 wakeup(&sc->reset_acked);
306 mtx_unlock(&sc->mtx);
310 sc->sampling_rate_slow >= 0) {
313 { sc->addr, IIC_M_RD | IIC_M_NOSTART, 1, actbuf };
317 actlen -= 2;
319 DPRINTF(sc, "input report too big. requested=%d "
323 /* Read input report itself. */
325 { sc->addr, IIC_M_RD | IIC_M_NOSTART, actlen, buf };
328 error = iicbus_transfer(sc->dev, msgs, 1);
333 "%*D - %*D\n", 2, actbuf, " ", msgs[0].len, msgs[0].buf, " ");
341 /* 6.2.3 - Sending Output Reports. */
342 uint8_t *cmdreg = (uint8_t *)&sc->desc.wOutputRegister;
346 {sc->addr, IIC_M_WR | IIC_M_NOSTOP, sizeof(cmd), cmd},
347 {sc->addr, IIC_M_WR | IIC_M_NOSTART, len, __DECONST(void *, buf)},
350 if (le16toh(sc->desc.wMaxOutputLength) == 0)
358 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
366 * 5.2.2 - HID Descriptor Retrieval
371 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, 2, (uint8_t *)&cmd },
372 { sc->addr, IIC_M_RD, sizeof(*hid_desc), (uint8_t *)hid_desc },
378 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
391 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
394 { sc->addr, IIC_M_WR, sizeof(cmd), cmd },
399 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
405 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
408 { sc->addr, IIC_M_WR, sizeof(cmd), cmd },
413 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
420 uint16_t cmd = sc->desc.wReportDescRegister;
422 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, 2, (uint8_t *)&cmd },
423 { sc->addr, IIC_M_RD, len, buf },
430 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
434 DPRINTF(sc, "HID report descriptor: %*D\n", len, buf, " ");
444 * 7.2.2.4 - "The protocol is optimized for Report < 15. If a
445 * report ID >= 15 is necessary, then the Report ID in the Low Byte
447 * This Third Byte contains the entire/actual report ID."
449 uint8_t *dtareg = (uint8_t *)&sc->desc.wDataRegister;
450 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
465 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, cmdlen, cmd },
466 { sc->addr, IIC_M_RD | IIC_M_NOSTOP, 2, actbuf },
467 { sc->addr, IIC_M_RD | IIC_M_NOSTART, maxlen, buf },
477 * 7.2.2.2 - Response will be a 2-byte length value, the report
478 * id (1 byte, if defined in Report Descriptor), and then the report.
480 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
494 DPRINTF(sc, "response report id %d != %d\n", d, id);
498 actlen -= 2;
514 * 7.2.2.4 - "The protocol is optimized for Report < 15. If a
515 * report ID >= 15 is necessary, then the Report ID in the Low Byte
517 * This Third Byte contains the entire/actual report ID."
519 uint8_t *dtareg = (uint8_t *)&sc->desc.wDataRegister;
520 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
535 {sc->addr, IIC_M_WR | IIC_M_NOSTOP, cmdlen, cmd},
536 {sc->addr, IIC_M_WR | IIC_M_NOSTART, len, __DECONST(void *, buf)},
542 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
553 int error, rate; local
556 parent = device_get_parent(sc->dev);
559 if (iicbus_request_bus(parent, sc->dev, IIC_WAIT) != 0)
563 if (!sc->power_on)
566 error = iichid_cmd_read(sc, sc->intr_buf, sc->intr_bufsize, &actual);
569 sc->intr_handler(sc->intr_ctx, sc->intr_buf, actual);
570 sc->missing_samples = 0;
571 if (sc->dup_size != actual ||
572 memcmp(sc->dup_buf, sc->intr_buf, actual) != 0) {
573 sc->dup_size = actual;
574 memcpy(sc->dup_buf, sc->intr_buf, actual);
575 sc->dup_samples = 0;
577 ++sc->dup_samples;
579 if (++sc->missing_samples == 1)
580 sc->intr_handler(sc->intr_ctx, sc->intr_buf, 0);
581 sc->dup_samples = 0;
587 if (sc->callout_setup && sc->sampling_rate_slow > 0) {
588 if (sc->missing_samples >= sc->sampling_hysteresis ||
589 sc->dup_samples >= sc->sampling_hysteresis)
590 rate = sc->sampling_rate_slow;
592 rate = sc->sampling_rate_fast;
593 taskqueue_enqueue_timeout_sbt(sc->taskqueue, &sc->sampling_task,
594 SBT_1S / MAX(rate, 1), 0, C_PREL(2));
598 iicbus_release_bus(parent, sc->dev);
611 parent = device_get_parent(sc->dev);
614 * Designware(IG4) driver-specific hack.
619 if (iicbus_request_bus(parent, sc->dev, IIC_DONTWAIT) != 0)
630 error = iichid_cmd_read(sc, sc->intr_buf, sc->intr_bufsize, &actual);
633 if (sc->power_on) {
635 sc->intr_handler(sc->intr_ctx, sc->intr_buf,
643 iicbus_release_bus(parent, sc->dev);
657 * Request iicbus early as sc->suspend and sc->power_on
660 parent = device_get_parent(sc->dev);
663 error = iicbus_request_bus(parent, sc->dev, how_request);
669 sc->open = true;
672 sc->open = false;
681 sc->suspend = false;
684 sc->suspend = true;
691 power_on = sc->open & !sc->suspend;
693 if (power_on != sc->power_on) {
697 sc->power_on = power_on;
699 if (sc->sampling_rate_slow >= 0 && sc->intr_handler != NULL) {
709 iicbus_release_bus(parent, sc->dev);
717 sc->irq_cookie = 0;
719 int error = bus_setup_intr(sc->dev, sc->irq_res,
720 INTR_TYPE_TTY|INTR_MPSAFE, NULL, iichid_intr, sc, &sc->irq_cookie);
732 if (sc->irq_cookie)
733 bus_teardown_intr(sc->dev, sc->irq_res, sc->irq_cookie);
735 sc->irq_cookie = 0;
743 if (sc->sampling_rate_slow < 0) {
748 sc->callout_setup = true;
757 if (sc->sampling_rate_slow <= 0) {
763 if (!sc->callout_setup)
767 sc->missing_samples = sc->sampling_hysteresis;
768 sc->dup_samples = 0;
769 sc->dup_size = 0;
770 taskqueue_enqueue_timeout(sc->taskqueue, &sc->sampling_task, 0);
779 sc->callout_setup = false;
780 taskqueue_cancel_timeout(sc->taskqueue, &sc->sampling_task, NULL);
793 value = sc->sampling_rate_slow;
796 if (error != 0 || req->newptr == NULL ||
797 value == sc->sampling_rate_slow)
801 if (sc->irq_res == NULL && value < 0)
804 parent = device_get_parent(sc->dev);
805 error = iicbus_request_bus(parent, sc->dev, IIC_WAIT);
809 oldval = sc->sampling_rate_slow;
810 sc->sampling_rate_slow = value;
814 if (sc->power_on)
817 if (sc->power_on)
822 if (sc->power_on && value > 0)
825 iicbus_release_bus(parent, sc->dev);
846 * a wrong length. Find the longest input report in report descriptor.
848 rdesc->rdsize =
849 MAX(rdesc->isize, le16toh(sc->desc.wMaxInputLength) - 2);
850 /* Write and get/set_report sizes are limited by I2C-HID protocol. */
851 rdesc->grsize = rdesc->srsize = IICHID_SIZE_MAX;
852 rdesc->wrsize = IICHID_SIZE_MAX;
854 parent = device_get_parent(sc->dev);
855 iicbus_request_bus(parent, sc->dev, IIC_WAIT);
857 sc->intr_handler = intr;
858 sc->intr_ctx = context;
859 sc->intr_bufsize = rdesc->rdsize;
860 sc->intr_buf = realloc(sc->intr_buf, sc->intr_bufsize,
863 sc->dup_buf = realloc(sc->dup_buf, sc->intr_bufsize,
865 taskqueue_start_threads(&sc->taskqueue, 1, PI_TTY,
866 "%s taskq", device_get_nameunit(sc->dev));
868 iicbus_release_bus(parent, sc->dev);
878 taskqueue_drain_all(sc->taskqueue);
902 * 8.2 - The HOST determines that there are no active applications
920 error = iichid_cmd_read(sc, sc->intr_buf, sc->intr_bufsize, &actual);
922 sc->intr_handler(sc->intr_ctx, sc->intr_buf, actual);
938 DPRINTF(sc, "failed to fetch report descriptor: %d\n", error);
954 parent = device_get_parent(sc->dev);
955 error = iicbus_request_bus(parent, sc->dev, IIC_WAIT);
958 iicbus_release_bus(parent, sc->dev);
1022 ((struct iic_rdwr_data *)data)->msgs,
1023 ((struct iic_rdwr_data *)data)->nmsgs));
1038 hw->idBus = BUS_I2C;
1039 hw->idVendor = le16toh(desc->wVendorID);
1040 hw->idProduct = le16toh(desc->wProductID);
1041 hw->idVersion = le16toh(desc->wVersionID);
1047 if (device_info->Valid & ACPI_VALID_HID)
1048 strlcpy(hw->idPnP, device_info->HardwareId.String,
1050 snprintf(hw->name, sizeof(hw->name), "%s:%02lX %04X:%04X",
1051 (device_info->Valid & ACPI_VALID_HID) ?
1052 device_info->HardwareId.String : "Unknown",
1053 (device_info->Valid & ACPI_VALID_UID) ?
1054 strtoul(device_info->UniqueId.String, NULL, 10) : 0UL,
1055 le16toh(desc->wVendorID), le16toh(desc->wProductID));
1059 strlcpy(hw->serial, "", sizeof(hw->serial));
1060 hw->rdescsize = le16toh(desc->wReportDescLength);
1061 if (desc->wOutputRegister == 0 || desc->wMaxOutputLength == 0)
1076 sc->dev = dev;
1077 if (sc->probe_done)
1080 sc->probe_done = true;
1081 sc->probe_result = ENXIO;
1086 sc->addr = iicbus_get_addr(dev) << 1;
1087 if (sc->addr == 0)
1104 DPRINTF(sc, " IICbus addr : 0x%02X\n", sc->addr >> 1);
1107 error = iichid_cmd_get_hid_desc(sc, config_reg, &sc->desc);
1114 if (le16toh(sc->desc.wHIDDescLength) != 30 ||
1115 le16toh(sc->desc.bcdVersion) != 0x100) {
1121 if (iichid_fill_device_info(&sc->desc, handle, &sc->hw) != 0) {
1126 if (hid_test_quirk(&sc->hw, HQ_HID_IGNORE))
1129 sc->probe_result = BUS_PROBE_DEFAULT;
1131 if (sc->probe_result <= BUS_PROBE_SPECIFIC)
1132 device_set_descf(dev, "%s I2C HID device", sc->hw.name);
1133 return (sc->probe_result);
1149 sc->power_on = true;
1151 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
1152 sc->intr_bufsize = le16toh(sc->desc.wMaxInputLength) - 2;
1153 sc->intr_buf = malloc(sc->intr_bufsize, M_DEVBUF, M_WAITOK | M_ZERO);
1154 TASK_INIT(&sc->suspend_task, 0, iichid_suspend_task, sc);
1156 sc->taskqueue = taskqueue_create_fast("iichid_tq", M_WAITOK | M_ZERO,
1157 taskqueue_thread_enqueue, &sc->taskqueue);
1158 TIMEOUT_TASK_INIT(sc->taskqueue, &sc->sampling_task, 0,
1161 sc->sampling_rate_slow = -1;
1162 sc->sampling_rate_fast = IICHID_SAMPLING_RATE_FAST;
1163 sc->sampling_hysteresis = IICHID_SAMPLING_HYSTERESIS;
1164 sc->dup_buf = malloc(sc->intr_bufsize, M_DEVBUF, M_WAITOK | M_ZERO);
1167 sc->irq_rid = 0;
1168 sc->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
1169 &sc->irq_rid, RF_ACTIVE);
1171 if (sc->irq_res != NULL) {
1173 sc->irq_res, sc->irq_rid);
1177 if (sc->irq_res == NULL || error != 0) {
1179 device_printf(sc->dev,
1181 sc->sampling_rate_slow = IICHID_SAMPLING_RATE_SLOW;
1183 device_printf(sc->dev, "Interrupt setup failed\n");
1184 if (sc->irq_res != NULL)
1185 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
1186 sc->irq_res);
1194 SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
1195 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1198 "idle sampling rate in num/second");
1199 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev),
1200 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1202 &sc->sampling_rate_fast, 0,
1203 "active sampling rate in num/second");
1204 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev),
1205 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1207 &sc->sampling_hysteresis, 0,
1209 hid_add_dynamic_quirk(&sc->hw, HQ_IICHID_SAMPLING);
1216 pause("iichid", (hz + 999) / 1000);
1228 if (sc->sampling_rate_slow >= 0) {
1229 pause("iichid", (hz + 999) / 1000);
1230 (void)iichid_cmd_read(sc, sc->intr_buf, 0, NULL);
1234 mtx_lock(&sc->mtx);
1235 if (!sc->reset_acked && !cold) {
1236 error = mtx_sleep(&sc->reset_acked, &sc->mtx, 0,
1237 "iichid_reset", hz * IICHID_RESET_TIMEOUT);
1239 device_printf(sc->dev,
1242 mtx_unlock(&sc->mtx);
1247 device_printf(sc->dev, "Could not add I2C device\n");
1253 device_set_ivars(child, &sc->hw);
1258 if (!sc->open) {
1260 sc->power_on = false;
1277 if (sc->irq_res != NULL)
1278 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
1279 sc->irq_res);
1281 if (sc->taskqueue != NULL)
1282 taskqueue_free(sc->taskqueue);
1283 sc->taskqueue = NULL;
1284 free(sc->dup_buf, M_DEVBUF);
1286 free(sc->intr_buf, M_DEVBUF);
1287 mtx_destroy(&sc->mtx);
1308 * 8.2 - The HOST is going into a deep power optimized state and wishes
1321 if (sc->sampling_rate_slow < 0)
1330 #define suspend_thread sc->taskqueue
1334 taskqueue_enqueue(suspend_thread, &sc->suspend_task);
1335 taskqueue_drain(suspend_thread, &sc->suspend_task);
1349 if (sc->sampling_rate_slow < 0)