1 /* $OpenBSD: ihidev.h,v 1.11 2025/01/07 19:26:14 mglocker Exp $ */ 2 /* 3 * HID-over-i2c driver 4 * 5 * Copyright (c) 2015, 2016 joshua stein <jcs@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/timeout.h> 21 22 /* from usbdi.h: Match codes. */ 23 /* First five codes is for a whole device. */ 24 #define IMATCH_VENDOR_PRODUCT_REV 14 25 #define IMATCH_VENDOR_PRODUCT 13 26 #define IMATCH_VENDOR_DEVCLASS_DEVPROTO 12 27 #define IMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO 11 28 #define IMATCH_DEVCLASS_DEVSUBCLASS 10 29 /* Next six codes are for interfaces. */ 30 #define IMATCH_VENDOR_PRODUCT_REV_CONF_IFACE 9 31 #define IMATCH_VENDOR_PRODUCT_CONF_IFACE 8 32 #define IMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO 7 33 #define IMATCH_VENDOR_IFACESUBCLASS 6 34 #define IMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO 5 35 #define IMATCH_IFACECLASS_IFACESUBCLASS 4 36 #define IMATCH_IFACECLASS 3 37 #define IMATCH_IFACECLASS_GENERIC 2 38 /* Generic driver */ 39 #define IMATCH_GENERIC 1 40 /* No match */ 41 #define IMATCH_NONE 0 42 43 #define IHIDBUSCF_REPORTID 0 44 #define IHIDBUSCF_REPORTID_DEFAULT -1 45 46 #define ihidevcf_reportid cf_loc[IHIDBUSCF_REPORTID] 47 #define IHIDEV_UNK_REPORTID IHIDBUSCF_REPORTID_DEFAULT 48 49 /* 5.1.1 - HID Descriptor Format */ 50 struct i2c_hid_desc { 51 uint16_t wHIDDescLength; 52 uint16_t bcdVersion; 53 uint16_t wReportDescLength; 54 uint16_t wReportDescRegister; 55 uint16_t wInputRegister; 56 uint16_t wMaxInputLength; 57 uint16_t wOutputRegister; 58 uint16_t wMaxOutputLength; 59 uint16_t wCommandRegister; 60 uint16_t wDataRegister; 61 uint16_t wVendorID; 62 uint16_t wProductID; 63 uint16_t wVersionID; 64 uint32_t reserved; 65 } __packed; 66 67 struct ihidev_softc { 68 struct device sc_dev; 69 i2c_tag_t sc_tag; 70 71 i2c_addr_t sc_addr; 72 void *sc_ih; 73 74 u_int sc_hid_desc_addr; 75 union { 76 uint8_t hid_desc_buf[sizeof(struct i2c_hid_desc)]; 77 struct i2c_hid_desc hid_desc; 78 }; 79 80 uint8_t *sc_report; 81 int sc_reportlen; 82 83 int sc_nrepid; 84 struct ihidev **sc_subdevs; 85 86 u_int sc_isize; 87 u_char *sc_ibuf; 88 89 int sc_refcnt; 90 91 int sc_poll; 92 int sc_frompoll; 93 int sc_fastpoll; 94 struct timeout sc_timer; 95 int sc_dying; 96 97 int sc_quirks; 98 }; 99 100 struct ihidev { 101 struct device sc_idev; 102 struct ihidev_softc *sc_parent; 103 uint8_t sc_report_id; 104 uint8_t sc_state; 105 #define IHIDEV_OPEN 0x01 /* device is open */ 106 void (*sc_intr)(struct ihidev *, void *, u_int); 107 108 int sc_isize; 109 int sc_osize; 110 int sc_fsize; 111 }; 112 113 struct ihidev_attach_arg { 114 struct i2c_attach_args *iaa; 115 struct ihidev_softc *parent; 116 uint8_t reportid; 117 uint8_t claims[16]; 118 uint8_t nclaims; 119 #define IHIDEV_CLAIM_MULTIPLEID 255 120 }; 121 122 struct i2c_hid_report_request { 123 u_int id; 124 u_int type; 125 #define I2C_HID_REPORT_TYPE_INPUT 0x1 126 #define I2C_HID_REPORT_TYPE_OUTPUT 0x2 127 #define I2C_HID_REPORT_TYPE_FEATURE 0x3 128 void *data; 129 u_int len; 130 }; 131 132 void ihidev_get_report_desc(struct ihidev_softc *, void **, int *); 133 int ihidev_open(struct ihidev *); 134 void ihidev_close(struct ihidev *); 135 int ihidev_ioctl(struct ihidev *, u_long, caddr_t, int, struct proc *); 136 137 int ihidev_report_type_conv(int); 138 int ihidev_set_report(struct device *, int, int, void *, int); 139 int ihidev_get_report(struct device *, int, int, void *, int); 140 int ihidev_send_report(struct device *, int, void *, int); 141 142 void ihidev_poll(void *); 143