2 * Copyright (c) 2018-2019 Marc Priggemeyer <marc.priggemeyer@gmail.com>
3 * Copyright (c) 2019-2020 Vladimir Kondratyev <wulf@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * I2C HID transport backend.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
38 #include <sys/callout.h>
39 #include <sys/endian.h>
40 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
47 #include <sys/taskqueue.h>
49 #include <machine/resource.h>
51 #include <contrib/dev/acpica/include/acpi.h>
52 #include <contrib/dev/acpica/include/accommon.h>
53 #include <dev/acpica/acpivar.h>
55 #include <dev/evdev/input.h>
57 #include <dev/hid/hid.h>
58 #include <dev/hid/hidquirk.h>
60 #include <dev/iicbus/iic.h>
61 #include <dev/iicbus/iicbus.h>
62 #include <dev/iicbus/iiconf.h>
67 static int iichid_debug = 0;
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");
73 #define DPRINTFN(sc, n, ...) do { \
74 if (iichid_debug >= (n)) \
75 device_printf((sc)->dev, __VA_ARGS__); \
77 #define DPRINTF(sc, ...) DPRINTFN(sc, 1, __VA_ARGS__)
79 #define DPRINTFN(...) do {} while (0)
80 #define DPRINTF(...) do {} while (0)
83 typedef hid_size_t iichid_size_t;
84 #define IICHID_SIZE_MAX (UINT16_MAX - 2)
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,
99 #define I2C_HID_POWER_ON 0x0
100 #define I2C_HID_POWER_OFF 0x1
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
111 #define IICHID_SAMPLING_RATE_FAST 60
112 #define IICHID_SAMPLING_RATE_SLOW 10
113 #define IICHID_SAMPLING_HYSTERESIS 1
115 /* 5.1.1 - HID Descriptor Format */
116 struct i2c_hid_desc {
117 uint16_t wHIDDescLength;
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;
133 static char *iichid_ids[] = {
139 enum iichid_powerstate_how {
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.
150 struct iichid_softc {
156 struct hid_device_info hw;
157 uint16_t addr; /* Shifted left by 1 */
158 struct i2c_hid_desc desc;
160 hid_intr_t *intr_handler;
163 iichid_size_t intr_bufsize;
166 struct resource *irq_res;
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;
180 struct task suspend_task;
181 bool open; /* iicbus lock */
182 bool suspend; /* iicbus lock */
183 bool power_on; /* iicbus lock */
186 static device_probe_t iichid_probe;
187 static device_attach_t iichid_attach;
188 static device_detach_t iichid_detach;
189 static device_resume_t iichid_resume;
190 static device_suspend_t iichid_suspend;
192 static void iichid_suspend_task(void *, int);
194 #ifdef IICHID_SAMPLING
195 static int iichid_setup_callout(struct iichid_softc *);
196 static int iichid_reset_callout(struct iichid_softc *);
197 static void iichid_teardown_callout(struct iichid_softc *);
201 acpi_is_iichid(ACPI_HANDLE handle)
206 for (ids = iichid_ids; *ids != NULL; ids++) {
207 if (acpi_MatchHid(handle, *ids))
214 * If no _STA method or if it failed, then assume that
215 * the device is present.
217 if (ACPI_FAILURE(acpi_GetInteger(handle, "_STA", &sta)) ||
218 ACPI_DEVICE_PRESENT(sta))
225 iichid_get_config_reg(ACPI_HANDLE handle, uint16_t *config_reg)
228 ACPI_BUFFER acpi_buf;
232 * function (_DSM) to be evaluated to retrieve the address of
233 * the configuration register of the HID device.
235 /* 3cdff6f7-4267-4555-ad05-b30a3d8938de */
236 static uint8_t dsm_guid[ACPI_UUID_LENGTH] = {
237 0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
238 0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
241 status = acpi_EvaluateDSMTyped(handle, dsm_guid, 1, 1, NULL, &acpi_buf,
243 if (ACPI_FAILURE(status)) {
244 printf("%s: error evaluating _DSM\n", __func__);
247 result = (ACPI_OBJECT *) acpi_buf.Pointer;
248 *config_reg = result->Integer.Value & 0xFFFF;
255 iichid_cmd_read(struct iichid_softc* sc, void *buf, iichid_size_t maxlen,
256 iichid_size_t *actual_len)
259 * 6.1.3 - Retrieval of Input Reports
260 * DEVICE returns the length (2 Bytes) and the entire Input Report.
262 uint8_t actbuf[2] = { 0, 0 };
263 /* Read actual input report length. */
264 struct iic_msg msgs[] = {
265 { sc->addr, IIC_M_RD | IIC_M_NOSTOP, sizeof(actbuf), actbuf },
270 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
274 actlen = actbuf[0] | actbuf[1] << 8;
275 if (actlen <= 2 || actlen == 0xFFFF || maxlen == 0) {
276 /* Read and discard 1 byte to send I2C STOP condition. */
277 msgs[0] = (struct iic_msg)
278 { sc->addr, IIC_M_RD | IIC_M_NOSTART, 1, actbuf };
282 if (actlen > maxlen) {
283 DPRINTF(sc, "input report too big. requested=%d "
284 "received=%d\n", maxlen, actlen);
287 /* Read input report itself. */
288 msgs[0] = (struct iic_msg)
289 { sc->addr, IIC_M_RD | IIC_M_NOSTART, actlen, buf };
292 error = iicbus_transfer(sc->dev, msgs, 1);
293 if (error == 0 && actual_len != NULL)
294 *actual_len = actlen;
297 "%*D - %*D\n", 2, actbuf, " ", msgs[0].len, msgs[0].buf, " ");
303 iichid_cmd_write(struct iichid_softc *sc, const void *buf, iichid_size_t len)
305 /* 6.2.3 - Sending Output Reports. */
306 uint8_t *cmdreg = (uint8_t *)&sc->desc.wOutputRegister;
307 uint16_t replen = 2 + len;
308 uint8_t cmd[4] = { cmdreg[0], cmdreg[1], replen & 0xFF, replen >> 8 };
309 struct iic_msg msgs[] = {
310 {sc->addr, IIC_M_WR | IIC_M_NOSTOP, sizeof(cmd), cmd},
311 {sc->addr, IIC_M_WR | IIC_M_NOSTART, len, __DECONST(void *, buf)},
314 if (le16toh(sc->desc.wMaxOutputLength) == 0)
315 return (IIC_ENOTSUPP);
317 return (IIC_ENOTSUPP);
319 DPRINTF(sc, "HID command I2C_HID_CMD_WRITE (len %d): "
320 "%*D\n", len, len, buf, " ");
322 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
326 iichid_cmd_get_hid_desc(struct iichid_softc *sc, uint16_t config_reg,
327 struct i2c_hid_desc *hid_desc)
330 * 5.2.2 - HID Descriptor Retrieval
331 * register is passed from the controller.
333 uint16_t cmd = htole16(config_reg);
334 struct iic_msg msgs[] = {
335 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, 2, (uint8_t *)&cmd },
336 { sc->addr, IIC_M_RD, sizeof(*hid_desc), (uint8_t *)hid_desc },
340 DPRINTF(sc, "HID command I2C_HID_CMD_DESCR at 0x%x\n", config_reg);
342 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
346 DPRINTF(sc, "HID descriptor: %*D\n",
347 (int)sizeof(struct i2c_hid_desc), hid_desc, " ");
353 iichid_set_power(struct iichid_softc *sc, uint8_t param)
355 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
356 uint8_t cmd[] = { cmdreg[0], cmdreg[1], param, I2C_HID_CMD_SET_POWER };
357 struct iic_msg msgs[] = {
358 { sc->addr, IIC_M_WR, sizeof(cmd), cmd },
361 DPRINTF(sc, "HID command I2C_HID_CMD_SET_POWER(%d)\n", param);
363 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
367 iichid_reset(struct iichid_softc *sc)
369 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
370 uint8_t cmd[] = { cmdreg[0], cmdreg[1], 0, I2C_HID_CMD_RESET };
371 struct iic_msg msgs[] = {
372 { sc->addr, IIC_M_WR, sizeof(cmd), cmd },
375 DPRINTF(sc, "HID command I2C_HID_CMD_RESET\n");
377 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
381 iichid_cmd_get_report_desc(struct iichid_softc* sc, void *buf,
384 uint16_t cmd = sc->desc.wReportDescRegister;
385 struct iic_msg msgs[] = {
386 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, 2, (uint8_t *)&cmd },
387 { sc->addr, IIC_M_RD, len, buf },
391 DPRINTF(sc, "HID command I2C_HID_REPORT_DESCR at 0x%x with size %d\n",
394 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
398 DPRINTF(sc, "HID report descriptor: %*D\n", len, buf, " ");
404 iichid_cmd_get_report(struct iichid_softc* sc, void *buf, iichid_size_t maxlen,
405 iichid_size_t *actual_len, uint8_t type, uint8_t id)
408 * 7.2.2.4 - "The protocol is optimized for Report < 15. If a
409 * report ID >= 15 is necessary, then the Report ID in the Low Byte
410 * must be set to 1111 and a Third Byte is appended to the protocol.
411 * This Third Byte contains the entire/actual report ID."
413 uint8_t *dtareg = (uint8_t *)&sc->desc.wDataRegister;
414 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
415 uint8_t cmd[] = { /*________|______id>=15_____|______id<15______*/
418 (id >= 15 ? 15 | (type << 4): id | (type << 4)),
419 I2C_HID_CMD_GET_REPORT ,
420 (id >= 15 ? id : dtareg[0] ),
421 (id >= 15 ? dtareg[0] : dtareg[1] ),
422 (id >= 15 ? dtareg[1] : 0 ),
424 int cmdlen = (id >= 15 ? 7 : 6 );
425 uint8_t actbuf[2] = { 0, 0 };
428 struct iic_msg msgs[] = {
429 { sc->addr, IIC_M_WR | IIC_M_NOSTOP, cmdlen, cmd },
430 { sc->addr, IIC_M_RD | IIC_M_NOSTOP, 2, actbuf },
431 { sc->addr, IIC_M_RD | IIC_M_NOSTART, maxlen, buf },
437 DPRINTF(sc, "HID command I2C_HID_CMD_GET_REPORT %d "
438 "(type %d, len %d)\n", id, type, maxlen);
441 * 7.2.2.2 - Response will be a 2-byte length value, the report
442 * id (1 byte, if defined in Report Descriptor), and then the report.
444 error = iicbus_transfer(sc->dev, msgs, nitems(msgs));
448 actlen = actbuf[0] | actbuf[1] << 8;
449 if (actlen != maxlen + 2)
450 DPRINTF(sc, "response size %d != expected length %d\n",
453 if (actlen <= 2 || actlen == 0xFFFF)
456 d = id != 0 ? *(uint8_t *)buf : 0;
458 DPRINTF(sc, "response report id %d != %d\n", d, id);
465 if (actual_len != NULL)
466 *actual_len = actlen;
468 DPRINTF(sc, "response: %*D %*D\n", 2, actbuf, " ", actlen, buf, " ");
474 iichid_cmd_set_report(struct iichid_softc* sc, const void *buf,
475 iichid_size_t len, uint8_t type, uint8_t id)
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 Byte
480 * must be set to 1111 and a Third Byte is appended to the protocol.
481 * This Third Byte contains the entire/actual report ID."
483 uint8_t *dtareg = (uint8_t *)&sc->desc.wDataRegister;
484 uint8_t *cmdreg = (uint8_t *)&sc->desc.wCommandRegister;
485 uint16_t replen = 2 + len;
486 uint8_t cmd[] = { /*________|______id>=15_____|______id<15______*/
489 (id >= 15 ? 15 | (type << 4): id | (type << 4)),
490 I2C_HID_CMD_SET_REPORT ,
491 (id >= 15 ? id : dtareg[0] ),
492 (id >= 15 ? dtareg[0] : dtareg[1] ),
493 (id >= 15 ? dtareg[1] : replen & 0xff ),
494 (id >= 15 ? replen & 0xff : replen >> 8 ),
495 (id >= 15 ? replen >> 8 : 0 ),
497 int cmdlen = (id >= 15 ? 9 : 8 );
498 struct iic_msg msgs[] = {
499 {sc->addr, IIC_M_WR | IIC_M_NOSTOP, cmdlen, cmd},
500 {sc->addr, IIC_M_WR | IIC_M_NOSTART, len, __DECONST(void *, buf)},
503 DPRINTF(sc, "HID command I2C_HID_CMD_SET_REPORT %d (type %d, len %d): "
504 "%*D\n", id, type, len, len, buf, " ");
506 return (iicbus_transfer(sc->dev, msgs, nitems(msgs)));
509 #ifdef IICHID_SAMPLING
511 iichid_event_task(void *context, int pending)
513 struct iichid_softc *sc;
515 iichid_size_t actual;
520 parent = device_get_parent(sc->dev);
522 bus_requested = false;
523 if (iicbus_request_bus(parent, sc->dev, IIC_WAIT) != 0)
525 bus_requested = true;
530 error = iichid_cmd_read(sc, sc->intr_buf, sc->intr_bufsize, &actual);
533 sc->intr_handler(sc->intr_ctx, sc->intr_buf, actual);
534 sc->missing_samples = 0;
536 ++sc->missing_samples;
538 DPRINTF(sc, "read error occured: %d\n", error);
541 if (sc->callout_setup && sc->sampling_rate_slow > 0) {
542 if (sc->missing_samples == sc->sampling_hysteresis)
543 sc->intr_handler(sc->intr_ctx, sc->intr_buf, 0);
544 taskqueue_enqueue_timeout(sc->taskqueue, &sc->periodic_task,
545 hz / MAX(sc->missing_samples >= sc->sampling_hysteresis ?
546 sc->sampling_rate_slow : sc->sampling_rate_fast, 1));
550 iicbus_release_bus(parent, sc->dev);
552 #endif /* IICHID_SAMPLING */
555 iichid_intr(void *context)
557 struct iichid_softc *sc;
559 iichid_size_t maxlen, actual;
563 parent = device_get_parent(sc->dev);
566 * Designware(IG4) driver-specific hack.
567 * Requesting of an I2C bus with IIC_DONTWAIT parameter enables polled
568 * mode in the driver, making possible iicbus_transfer execution from
569 * interrupt handlers and callouts.
571 if (iicbus_request_bus(parent, sc->dev, IIC_DONTWAIT) != 0)
575 * Reading of input reports of I2C devices residing in SLEEP state is
576 * not allowed and often returns a garbage. If a HOST needs to
577 * communicate with the DEVICE it MUST issue a SET POWER command
578 * (to ON) before any other command. As some hardware requires reads to
579 * acknowledge interrupts we fetch only length header and discard it.
581 maxlen = sc->power_on ? sc->intr_bufsize : 0;
582 error = iichid_cmd_read(sc, sc->intr_buf, maxlen, &actual);
586 sc->intr_handler(sc->intr_ctx, sc->intr_buf,
589 DPRINTF(sc, "no data received\n");
592 DPRINTF(sc, "read error occured: %d\n", error);
594 iicbus_release_bus(parent, sc->dev);
598 iichid_set_power_state(struct iichid_softc *sc,
599 enum iichid_powerstate_how how_open,
600 enum iichid_powerstate_how how_suspend)
608 * Request iicbus early as sc->suspend and sc->power_on
609 * are protected by iicbus internal lock.
611 parent = device_get_parent(sc->dev);
612 /* Allow to interrupt open()/close() handlers by SIGINT */
613 how_request = how_open == IICHID_PS_NULL ? IIC_WAIT : IIC_INTRWAIT;
614 error = iicbus_request_bus(parent, sc->dev, how_request);
630 switch (how_suspend) {
642 power_on = sc->open & !sc->suspend;
644 if (power_on != sc->power_on) {
645 error = iichid_set_power(sc,
646 power_on ? I2C_HID_POWER_ON : I2C_HID_POWER_OFF);
648 sc->power_on = power_on;
649 #ifdef IICHID_SAMPLING
650 if (sc->sampling_rate_slow >= 0 && sc->intr_handler != NULL) {
652 iichid_setup_callout(sc);
653 iichid_reset_callout(sc);
655 iichid_teardown_callout(sc);
660 iicbus_release_bus(parent, sc->dev);
666 iichid_setup_interrupt(struct iichid_softc *sc)
670 int error = bus_setup_intr(sc->dev, sc->irq_res,
671 INTR_TYPE_TTY|INTR_MPSAFE, NULL, iichid_intr, sc, &sc->irq_cookie);
673 DPRINTF(sc, "Could not setup interrupt handler\n");
675 DPRINTF(sc, "successfully setup interrupt\n");
681 iichid_teardown_interrupt(struct iichid_softc *sc)
684 bus_teardown_intr(sc->dev, sc->irq_res, sc->irq_cookie);
689 #ifdef IICHID_SAMPLING
691 iichid_setup_callout(struct iichid_softc *sc)
694 if (sc->sampling_rate_slow < 0) {
695 DPRINTF(sc, "sampling_rate is below 0, can't setup callout\n");
699 sc->callout_setup = true;
700 DPRINTF(sc, "successfully setup callout\n");
705 iichid_reset_callout(struct iichid_softc *sc)
708 if (sc->sampling_rate_slow <= 0) {
709 DPRINTF(sc, "sampling_rate is below or equal to 0, "
710 "can't reset callout\n");
714 if (!sc->callout_setup)
717 /* Start with slow sampling. */
718 sc->missing_samples = sc->sampling_hysteresis;
719 taskqueue_enqueue(sc->taskqueue, &sc->event_task);
725 iichid_teardown_callout(struct iichid_softc *sc)
728 sc->callout_setup = false;
729 taskqueue_cancel_timeout(sc->taskqueue, &sc->periodic_task, NULL);
730 DPRINTF(sc, "tore callout down\n");
734 iichid_sysctl_sampling_rate_handler(SYSCTL_HANDLER_ARGS)
736 struct iichid_softc *sc;
738 int error, oldval, value;
742 value = sc->sampling_rate_slow;
743 error = sysctl_handle_int(oidp, &value, 0, req);
745 if (error != 0 || req->newptr == NULL ||
746 value == sc->sampling_rate_slow)
749 /* Can't switch to interrupt mode if it is not supported. */
750 if (sc->irq_res == NULL && value < 0)
753 parent = device_get_parent(sc->dev);
754 error = iicbus_request_bus(parent, sc->dev, IIC_WAIT);
756 return (iic2errno(error));
758 oldval = sc->sampling_rate_slow;
759 sc->sampling_rate_slow = value;
761 if (oldval < 0 && value >= 0) {
762 iichid_teardown_interrupt(sc);
764 iichid_setup_callout(sc);
765 } else if (oldval >= 0 && value < 0) {
767 iichid_teardown_callout(sc);
768 iichid_setup_interrupt(sc);
771 if (sc->power_on && value > 0)
772 iichid_reset_callout(sc);
774 iicbus_release_bus(parent, sc->dev);
776 DPRINTF(sc, "new sampling_rate value: %d\n", value);
780 #endif /* IICHID_SAMPLING */
783 iichid_intr_setup(device_t dev, hid_intr_t intr, void *context,
784 struct hid_rdesc_info *rdesc)
786 struct iichid_softc *sc;
791 sc = device_get_softc(dev);
793 * Do not rely on wMaxInputLength, as some devices may set it to
794 * a wrong length. Find the longest input report in report descriptor.
796 rdesc->rdsize = rdesc->isize;
797 /* Write and get/set_report sizes are limited by I2C-HID protocol. */
798 rdesc->grsize = rdesc->srsize = IICHID_SIZE_MAX;
799 rdesc->wrsize = IICHID_SIZE_MAX;
801 sc->intr_handler = intr;
802 sc->intr_ctx = context;
803 sc->intr_buf = malloc(rdesc->rdsize, M_DEVBUF, M_WAITOK | M_ZERO);
804 sc->intr_bufsize = rdesc->rdsize;
805 #ifdef IICHID_SAMPLING
806 taskqueue_start_threads(&sc->taskqueue, 1, PI_TTY,
807 "%s taskq", device_get_nameunit(sc->dev));
812 iichid_intr_unsetup(device_t dev)
814 struct iichid_softc *sc;
816 sc = device_get_softc(dev);
817 #ifdef IICHID_SAMPLING
818 taskqueue_drain_all(sc->taskqueue);
820 free(sc->intr_buf, M_DEVBUF);
824 iichid_intr_start(device_t dev)
826 struct iichid_softc *sc;
828 sc = device_get_softc(dev);
829 DPRINTF(sc, "iichid device open\n");
830 iichid_set_power_state(sc, IICHID_PS_ON, IICHID_PS_NULL);
836 iichid_intr_stop(device_t dev)
838 struct iichid_softc *sc;
840 sc = device_get_softc(dev);
841 DPRINTF(sc, "iichid device close\n");
843 * 8.2 - The HOST determines that there are no active applications
844 * that are currently using the specific HID DEVICE. The HOST
845 * is recommended to issue a HIPO command to the DEVICE to force
846 * the DEVICE in to a lower power state.
848 iichid_set_power_state(sc, IICHID_PS_OFF, IICHID_PS_NULL);
854 iichid_intr_poll(device_t dev)
856 struct iichid_softc *sc;
857 iichid_size_t actual;
860 sc = device_get_softc(dev);
861 error = iichid_cmd_read(sc, sc->intr_buf, sc->intr_bufsize, &actual);
862 if (error == 0 && actual != 0)
863 sc->intr_handler(sc->intr_ctx, sc->intr_buf, actual);
870 iichid_get_rdesc(device_t dev, void *buf, hid_size_t len)
872 struct iichid_softc *sc;
875 sc = device_get_softc(dev);
876 error = iichid_cmd_get_report_desc(sc, buf, len);
878 DPRINTF(sc, "failed to fetch report descriptor: %d\n", error);
880 return (iic2errno(error));
884 iichid_read(device_t dev, void *buf, hid_size_t maxlen, hid_size_t *actlen)
886 struct iichid_softc *sc;
890 if (maxlen > IICHID_SIZE_MAX)
892 sc = device_get_softc(dev);
893 parent = device_get_parent(sc->dev);
894 error = iicbus_request_bus(parent, sc->dev, IIC_WAIT);
896 error = iichid_cmd_read(sc, buf, maxlen, actlen);
897 iicbus_release_bus(parent, sc->dev);
899 return (iic2errno(error));
903 iichid_write(device_t dev, const void *buf, hid_size_t len)
905 struct iichid_softc *sc;
907 if (len > IICHID_SIZE_MAX)
909 sc = device_get_softc(dev);
910 return (iic2errno(iichid_cmd_write(sc, buf, len)));
914 iichid_get_report(device_t dev, void *buf, hid_size_t maxlen,
915 hid_size_t *actlen, uint8_t type, uint8_t id)
917 struct iichid_softc *sc;
919 if (maxlen > IICHID_SIZE_MAX)
921 sc = device_get_softc(dev);
923 iichid_cmd_get_report(sc, buf, maxlen, actlen, type, id)));
927 iichid_set_report(device_t dev, const void *buf, hid_size_t len, uint8_t type,
930 struct iichid_softc *sc;
932 if (len > IICHID_SIZE_MAX)
934 sc = device_get_softc(dev);
935 return (iic2errno(iichid_cmd_set_report(sc, buf, len, type, id)));
939 iichid_set_idle(device_t dev, uint16_t duration, uint8_t id)
945 iichid_set_protocol(device_t dev, uint16_t protocol)
951 iichid_fill_device_info(struct i2c_hid_desc *desc, ACPI_HANDLE handle,
952 struct hid_device_info *hw)
954 ACPI_DEVICE_INFO *device_info;
957 hw->idVendor = le16toh(desc->wVendorID);
958 hw->idProduct = le16toh(desc->wProductID);
959 hw->idVersion = le16toh(desc->wVersionID);
961 /* get ACPI HID. It is a base part of the device name. */
962 if (ACPI_FAILURE(AcpiGetObjectInfo(handle, &device_info)))
965 if (device_info->Valid & ACPI_VALID_HID)
966 strlcpy(hw->idPnP, device_info->HardwareId.String,
968 snprintf(hw->name, sizeof(hw->name), "%s:%02lX %04X:%04X",
969 (device_info->Valid & ACPI_VALID_HID) ?
970 device_info->HardwareId.String : "Unknown",
971 (device_info->Valid & ACPI_VALID_UID) ?
972 strtoul(device_info->UniqueId.String, NULL, 10) : 0UL,
973 le16toh(desc->wVendorID), le16toh(desc->wProductID));
975 AcpiOsFree(device_info);
977 strlcpy(hw->serial, "", sizeof(hw->serial));
978 hw->rdescsize = le16toh(desc->wReportDescLength);
979 if (desc->wOutputRegister == 0 || desc->wMaxOutputLength == 0)
980 hid_add_dynamic_quirk(hw, HQ_NOWRITE);
986 iichid_probe(device_t dev)
988 struct iichid_softc *sc;
994 sc = device_get_softc(dev);
999 sc->probe_done = true;
1000 sc->probe_result = ENXIO;
1002 if (acpi_disabled("iichid"))
1005 sc->addr = iicbus_get_addr(dev) << 1;
1009 handle = acpi_get_handle(dev);
1013 if (!acpi_is_iichid(handle))
1016 if (ACPI_FAILURE(iichid_get_config_reg(handle, &config_reg)))
1019 DPRINTF(sc, " IICbus addr : 0x%02X\n", sc->addr >> 1);
1020 DPRINTF(sc, " HID descriptor reg: 0x%02X\n", config_reg);
1022 error = iichid_cmd_get_hid_desc(sc, config_reg, &sc->desc);
1024 DPRINTF(sc, "could not retrieve HID descriptor from the "
1025 "device: %d\n", error);
1029 if (le16toh(sc->desc.wHIDDescLength) != 30 ||
1030 le16toh(sc->desc.bcdVersion) != 0x100) {
1031 DPRINTF(sc, "HID descriptor is broken\n");
1035 /* Setup hid_device_info so we can figure out quirks for the device. */
1036 if (iichid_fill_device_info(&sc->desc, handle, &sc->hw) != 0) {
1037 DPRINTF(sc, "error evaluating AcpiGetObjectInfo\n");
1041 if (hid_test_quirk(&sc->hw, HQ_HID_IGNORE))
1044 sc->probe_result = BUS_PROBE_DEFAULT;
1046 if (sc->probe_result <= BUS_PROBE_SPECIFIC) {
1047 snprintf(buf, sizeof(buf), "%s I2C HID device", sc->hw.name);
1048 device_set_desc_copy(dev, buf);
1050 return (sc->probe_result);
1054 iichid_attach(device_t dev)
1056 struct iichid_softc *sc;
1060 sc = device_get_softc(dev);
1061 error = iichid_set_power(sc, I2C_HID_POWER_ON);
1063 device_printf(dev, "failed to power on: %d\n", error);
1067 * Windows driver sleeps for 1ms between the SET_POWER and RESET
1068 * commands. So we too as some devices may depend on this.
1070 pause("iichid", (hz + 999) / 1000);
1072 error = iichid_reset(sc);
1074 device_printf(dev, "failed to reset hardware: %d\n", error);
1079 sc->power_on = true;
1081 TASK_INIT(&sc->suspend_task, 0, iichid_suspend_task, sc);
1082 #ifdef IICHID_SAMPLING
1083 TASK_INIT(&sc->event_task, 0, iichid_event_task, sc);
1084 /* taskqueue_create can't fail with M_WAITOK mflag passed. */
1085 sc->taskqueue = taskqueue_create("iichid_tq", M_WAITOK | M_ZERO,
1086 taskqueue_thread_enqueue, &sc->taskqueue);
1087 TIMEOUT_TASK_INIT(sc->taskqueue, &sc->periodic_task, 0,
1088 iichid_event_task, sc);
1090 sc->sampling_rate_slow = -1;
1091 sc->sampling_rate_fast = IICHID_SAMPLING_RATE_FAST;
1092 sc->sampling_hysteresis = IICHID_SAMPLING_HYSTERESIS;
1096 sc->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
1097 &sc->irq_rid, RF_ACTIVE);
1099 if (sc->irq_res != NULL) {
1100 DPRINTF(sc, "allocated irq at %p and rid %d\n",
1101 sc->irq_res, sc->irq_rid);
1102 error = iichid_setup_interrupt(sc);
1105 if (sc->irq_res == NULL || error != 0) {
1106 #ifdef IICHID_SAMPLING
1107 device_printf(sc->dev,
1108 "Interrupt setup failed. Fallback to sampling\n");
1109 sc->sampling_rate_slow = IICHID_SAMPLING_RATE_SLOW;
1111 device_printf(sc->dev, "Interrupt setup failed\n");
1112 if (sc->irq_res != NULL)
1113 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
1120 #ifdef IICHID_SAMPLING
1121 SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
1122 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1123 OID_AUTO, "sampling_rate_slow", CTLTYPE_INT | CTLFLAG_RWTUN,
1124 sc, 0, iichid_sysctl_sampling_rate_handler, "I",
1125 "idle sampling rate in num/second");
1126 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev),
1127 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1128 OID_AUTO, "sampling_rate_fast", CTLFLAG_RWTUN,
1129 &sc->sampling_rate_fast, 0,
1130 "active sampling rate in num/second");
1131 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev),
1132 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1133 OID_AUTO, "sampling_hysteresis", CTLFLAG_RWTUN,
1134 &sc->sampling_hysteresis, 0,
1135 "number of missing samples before enabling of slow mode");
1136 hid_add_dynamic_quirk(&sc->hw, HQ_IICHID_SAMPLING);
1137 #endif /* IICHID_SAMPLING */
1139 child = device_add_child(dev, "hidbus", -1);
1140 if (child == NULL) {
1141 device_printf(sc->dev, "Could not add I2C device\n");
1147 device_set_ivars(child, &sc->hw);
1148 error = bus_generic_attach(dev);
1150 device_printf(dev, "failed to attach child: error %d\n", error);
1154 (void)iichid_set_power(sc, I2C_HID_POWER_OFF);
1155 sc->power_on = false;
1160 iichid_detach(device_t dev)
1162 struct iichid_softc *sc;
1165 sc = device_get_softc(dev);
1166 error = device_delete_children(dev);
1169 iichid_teardown_interrupt(sc);
1170 if (sc->irq_res != NULL)
1171 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid,
1173 #ifdef IICHID_SAMPLING
1174 if (sc->taskqueue != NULL)
1175 taskqueue_free(sc->taskqueue);
1176 sc->taskqueue = NULL;
1182 iichid_suspend_task(void *context, int pending)
1184 struct iichid_softc *sc = context;
1186 iichid_teardown_interrupt(sc);
1190 iichid_suspend(device_t dev)
1192 struct iichid_softc *sc;
1195 sc = device_get_softc(dev);
1196 (void)bus_generic_suspend(dev);
1198 * 8.2 - The HOST is going into a deep power optimized state and wishes
1199 * to put all the devices into a low power state also. The HOST
1200 * is recommended to issue a HIPO command to the DEVICE to force
1201 * the DEVICE in to a lower power state.
1203 DPRINTF(sc, "Suspend called, setting device to power_state 1\n");
1204 error = iichid_set_power_state(sc, IICHID_PS_NULL, IICHID_PS_OFF);
1206 DPRINTF(sc, "Could not set power_state, error: %d\n", error);
1208 DPRINTF(sc, "Successfully set power_state\n");
1210 #ifdef IICHID_SAMPLING
1211 if (sc->sampling_rate_slow < 0)
1215 * bus_teardown_intr can not be executed right here as it wants
1216 * to run on certain CPU to interacts with LAPIC while suspend
1217 * thread is bound to CPU0. So run it from taskqueue context.
1219 #ifdef IICHID_SAMPLING
1220 #define suspend_thread sc->taskqueue
1222 #define suspend_thread taskqueue_thread
1224 taskqueue_enqueue(suspend_thread, &sc->suspend_task);
1225 taskqueue_drain(suspend_thread, &sc->suspend_task);
1232 iichid_resume(device_t dev)
1234 struct iichid_softc *sc;
1237 sc = device_get_softc(dev);
1238 #ifdef IICHID_SAMPLING
1239 if (sc->sampling_rate_slow < 0)
1241 iichid_setup_interrupt(sc);
1243 DPRINTF(sc, "Resume called, setting device to power_state 0\n");
1244 error = iichid_set_power_state(sc, IICHID_PS_NULL, IICHID_PS_ON);
1246 DPRINTF(sc, "Could not set power_state, error: %d\n", error);
1248 DPRINTF(sc, "Successfully set power_state\n");
1249 (void)bus_generic_resume(dev);
1254 static devclass_t iichid_devclass;
1256 static device_method_t iichid_methods[] = {
1257 DEVMETHOD(device_probe, iichid_probe),
1258 DEVMETHOD(device_attach, iichid_attach),
1259 DEVMETHOD(device_detach, iichid_detach),
1260 DEVMETHOD(device_suspend, iichid_suspend),
1261 DEVMETHOD(device_resume, iichid_resume),
1263 DEVMETHOD(hid_intr_setup, iichid_intr_setup),
1264 DEVMETHOD(hid_intr_unsetup, iichid_intr_unsetup),
1265 DEVMETHOD(hid_intr_start, iichid_intr_start),
1266 DEVMETHOD(hid_intr_stop, iichid_intr_stop),
1267 DEVMETHOD(hid_intr_poll, iichid_intr_poll),
1270 DEVMETHOD(hid_get_rdesc, iichid_get_rdesc),
1271 DEVMETHOD(hid_read, iichid_read),
1272 DEVMETHOD(hid_write, iichid_write),
1273 DEVMETHOD(hid_get_report, iichid_get_report),
1274 DEVMETHOD(hid_set_report, iichid_set_report),
1275 DEVMETHOD(hid_set_idle, iichid_set_idle),
1276 DEVMETHOD(hid_set_protocol, iichid_set_protocol),
1281 static driver_t iichid_driver = {
1283 .methods = iichid_methods,
1284 .size = sizeof(struct iichid_softc),
1287 DRIVER_MODULE(iichid, iicbus, iichid_driver, iichid_devclass, NULL, 0);
1288 MODULE_DEPEND(iichid, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
1289 MODULE_DEPEND(iichid, acpi, 1, 1, 1);
1290 MODULE_DEPEND(iichid, hid, 1, 1, 1);
1291 MODULE_DEPEND(iichid, hidbus, 1, 1, 1);
1292 MODULE_VERSION(iichid, 1);
1293 IICBUS_ACPI_PNP_INFO(iichid_ids);