1 /* $NetBSD: ihidev.h,v 1.7 2024/12/08 20:49:56 jmcneill 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 #ifndef   _DEV_I2C_IHIDEV_H_
34 #define   _DEV_I2C_IHIDEV_H_
35 
36 /* ihidevreg.h */
37 
38 /*
39  * HID-over-i2c driver
40  *
41  * Copyright (c) 2015, 2016 joshua stein <jcs@openbsd.org>
42  *
43  * Permission to use, copy, modify, and distribute this software for any
44  * purpose with or without fee is hereby granted, provided that the above
45  * copyright notice and this permission notice appear in all copies.
46  *
47  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
48  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
49  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
50  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
51  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
52  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
53  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
54  */
55 
56 #include <sys/types.h>
57 
58 /* from usbdi.h: Match codes. */
59 /* First five codes is for a whole device. */
60 #define IMATCH_VENDOR_PRODUCT_REV                           14
61 #define IMATCH_VENDOR_PRODUCT                               13
62 #define IMATCH_VENDOR_DEVCLASS_DEVPROTO                     12
63 #define IMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO                11
64 #define IMATCH_DEVCLASS_DEVSUBCLASS                         10
65 /* Next six codes are for interfaces. */
66 #define IMATCH_VENDOR_PRODUCT_REV_CONF_IFACE                 9
67 #define IMATCH_VENDOR_PRODUCT_CONF_IFACE                     8
68 #define IMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO               7
69 #define IMATCH_VENDOR_IFACESUBCLASS                          6
70 #define IMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO           5
71 #define IMATCH_IFACECLASS_IFACESUBCLASS                      4
72 #define IMATCH_IFACECLASS                                    3
73 #define IMATCH_IFACECLASS_GENERIC                            2
74 /* Generic driver */
75 #define IMATCH_GENERIC                                                 1
76 /* No match */
77 #define IMATCH_NONE                                          0
78 
79 #define IHIDBUSCF_REPORTID                                  0
80 #define IHIDBUSCF_REPORTID_DEFAULT                          -1
81 
82 #define ihidevcf_reportid cf_loc[IHIDBUSCF_REPORTID]
83 #define IHIDEV_UNK_REPORTID IHIDBUSCF_REPORTID_DEFAULT
84 
85 /* 5.1.1 - HID Descriptor Format */
86 struct i2c_hid_desc {
87           uint16_t wHIDDescLength;
88           uint16_t bcdVersion;
89           uint16_t wReportDescLength;
90           uint16_t wReportDescRegister;
91           uint16_t wInputRegister;
92           uint16_t wMaxInputLength;
93           uint16_t wOutputRegister;
94           uint16_t wMaxOutputLength;
95           uint16_t wCommandRegister;
96           uint16_t wDataRegister;
97           uint16_t wVendorID;
98           uint16_t wProductID;
99           uint16_t wVersionID;
100           uint32_t reserved;
101 } __packed;
102 
103 /* ihidevvar.h */
104 
105 #include <sys/types.h>
106 
107 #include <sys/device.h>
108 #include <sys/mutex.h>
109 #include <sys/workqueue.h>
110 #include <sys/gpio.h>
111 
112 #include <dev/i2c/i2cvar.h>
113 #include <dev/gpio/gpiovar.h>
114 
115 struct ihidev_softc {
116           device_t  sc_dev;
117           i2c_tag_t sc_tag;
118           i2c_addr_t          sc_addr;
119           uint64_t  sc_phandle;
120           kmutex_t  sc_lock;
121 
122           void *              sc_ih;
123           void *              sc_ih_gpio;
124           struct gpio_pinmap sc_ih_gpiomap;
125           int                 sc_ih_gpiopins[1];
126           struct workqueue *sc_wq;
127           struct work         sc_work;
128           volatile unsigned sc_work_pending;
129           int                 sc_intr_type;
130 
131           u_int               sc_hid_desc_addr;
132           union {
133                     uint8_t   hid_desc_buf[sizeof(struct i2c_hid_desc)];
134                     struct i2c_hid_desc hid_desc;
135           };
136 
137           uint8_t             *sc_report;
138           int                 sc_reportlen;
139 
140           int                 sc_nrepid;
141           struct              ihidev **sc_subdevs;
142 
143           u_int               sc_isize;
144           u_char              *sc_ibuf;
145 
146           int                 sc_refcnt;
147 };
148 
149 struct ihidev {
150           device_t  sc_idev;
151           struct ihidev_softc *sc_parent;
152           uint8_t             sc_report_id;
153           uint8_t             sc_state;
154 #define   IHIDEV_OPEN         0x01      /* device is open */
155           void                (*sc_intr)(struct ihidev *, void *, u_int);
156 
157           int                 sc_isize;
158           int                 sc_osize;
159           int                 sc_fsize;
160 };
161 
162 struct ihidev_attach_arg {
163           struct i2c_attach_args        *iaa;
164           struct ihidev_softc *parent;
165           uint8_t                        reportid;
166 #define   IHIDEV_CLAIM_ALLREPORTID      255
167 };
168 
169 struct i2c_hid_report_request {
170           u_int id;
171           u_int type;
172 #define I2C_HID_REPORT_TYPE_INPUT       0x1
173 #define I2C_HID_REPORT_TYPE_OUTPUT      0x2
174 #define I2C_HID_REPORT_TYPE_FEATURE     0x3
175           void *data;
176           u_int len;
177 };
178 
179 void ihidev_get_report_desc(struct ihidev_softc *, void **, int *);
180 int ihidev_open(struct ihidev *);
181 void ihidev_close(struct ihidev *);
182 
183 int ihidev_set_report(device_t, int, int, void *, int);
184 int ihidev_get_report(device_t, int, int, void *, int);
185 int ihidev_report_type_conv(int);
186 
187 #endif    /* _DEV_I2C_IHIDEV_H_ */
188