1 /* $NetBSD: ihidev.h,v 1.1 2017/12/10 17:05:54 bouyer Exp $ */ 2 /* $OpenBSD ihidev.h,v 1.4 2016/01/31 18:24:35 jcs Exp $ */ 3 4 /*- 5 * Copyright (c) 2017 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Manuel Bouyer. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * HID-over-i2c driver 35 * 36 * Copyright (c) 2015, 2016 joshua stein <jcs@openbsd.org> 37 * 38 * Permission to use, copy, modify, and distribute this software for any 39 * purpose with or without fee is hereby granted, provided that the above 40 * copyright notice and this permission notice appear in all copies. 41 * 42 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 43 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 44 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 45 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 46 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 47 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 48 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 49 */ 50 51 /* from usbdi.h: Match codes. */ 52 /* First five codes is for a whole device. */ 53 #define IMATCH_VENDOR_PRODUCT_REV 14 54 #define IMATCH_VENDOR_PRODUCT 13 55 #define IMATCH_VENDOR_DEVCLASS_DEVPROTO 12 56 #define IMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO 11 57 #define IMATCH_DEVCLASS_DEVSUBCLASS 10 58 /* Next six codes are for interfaces. */ 59 #define IMATCH_VENDOR_PRODUCT_REV_CONF_IFACE 9 60 #define IMATCH_VENDOR_PRODUCT_CONF_IFACE 8 61 #define IMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO 7 62 #define IMATCH_VENDOR_IFACESUBCLASS 6 63 #define IMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO 5 64 #define IMATCH_IFACECLASS_IFACESUBCLASS 4 65 #define IMATCH_IFACECLASS 3 66 #define IMATCH_IFACECLASS_GENERIC 2 67 /* Generic driver */ 68 #define IMATCH_GENERIC 1 69 /* No match */ 70 #define IMATCH_NONE 0 71 72 #define IHIDBUSCF_REPORTID 0 73 #define IHIDBUSCF_REPORTID_DEFAULT -1 74 75 #define ihidevcf_reportid cf_loc[IHIDBUSCF_REPORTID] 76 #define IHIDEV_UNK_REPORTID IHIDBUSCF_REPORTID_DEFAULT 77 78 /* 5.1.1 - HID Descriptor Format */ 79 struct i2c_hid_desc { 80 uint16_t wHIDDescLength; 81 uint16_t bcdVersion; 82 uint16_t wReportDescLength; 83 uint16_t wReportDescRegister; 84 uint16_t wInputRegister; 85 uint16_t wMaxInputLength; 86 uint16_t wOutputRegister; 87 uint16_t wMaxOutputLength; 88 uint16_t wCommandRegister; 89 uint16_t wDataRegister; 90 uint16_t wVendorID; 91 uint16_t wProductID; 92 uint16_t wVersionID; 93 uint32_t reserved; 94 } __packed; 95 96 struct ihidev_softc { 97 device_t sc_dev; 98 i2c_tag_t sc_tag; 99 i2c_addr_t sc_addr; 100 uint64_t sc_phandle; 101 102 void * sc_ih; 103 kmutex_t sc_intr_lock; 104 105 u_int sc_hid_desc_addr; 106 union { 107 uint8_t hid_desc_buf[sizeof(struct i2c_hid_desc)]; 108 struct i2c_hid_desc hid_desc; 109 }; 110 111 uint8_t *sc_report; 112 int sc_reportlen; 113 114 int sc_nrepid; 115 struct ihidev **sc_subdevs; 116 117 u_int sc_isize; 118 u_char *sc_ibuf; 119 120 int sc_refcnt; 121 }; 122 123 struct ihidev { 124 device_t sc_idev; 125 struct ihidev_softc *sc_parent; 126 uint8_t sc_report_id; 127 uint8_t sc_state; 128 #define IHIDEV_OPEN 0x01 /* device is open */ 129 void (*sc_intr)(struct ihidev *, void *, u_int); 130 131 int sc_isize; 132 int sc_osize; 133 int sc_fsize; 134 }; 135 136 struct ihidev_attach_arg { 137 struct i2c_attach_args *iaa; 138 struct ihidev_softc *parent; 139 uint8_t reportid; 140 #define IHIDEV_CLAIM_ALLREPORTID 255 141 }; 142 143 struct i2c_hid_report_request { 144 u_int id; 145 u_int type; 146 #define I2C_HID_REPORT_TYPE_INPUT 0x1 147 #define I2C_HID_REPORT_TYPE_OUTPUT 0x2 148 #define I2C_HID_REPORT_TYPE_FEATURE 0x3 149 void *data; 150 u_int len; 151 }; 152 153 void ihidev_get_report_desc(struct ihidev_softc *, void **, int *); 154 int ihidev_open(struct ihidev *); 155 void ihidev_close(struct ihidev *); 156 157 int ihidev_set_report(device_t, int, int, void *, int); 158 int ihidev_get_report(device_t, int, int, void *, int); 159 int ihidev_report_type_conv(int); 160 161