xref: /openbsd-src/sys/dev/i2c/ihidev.h (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 /* $OpenBSD: ihidev.h,v 1.8 2020/07/09 21:01:56 jcs 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 
98 struct ihidev {
99 	struct device	sc_idev;
100 	struct ihidev_softc *sc_parent;
101 	uint8_t		sc_report_id;
102 	uint8_t		sc_state;
103 #define	IHIDEV_OPEN	0x01	/* device is open */
104 	void		(*sc_intr)(struct ihidev *, void *, u_int);
105 
106 	int		sc_isize;
107 	int		sc_osize;
108 	int		sc_fsize;
109 };
110 
111 struct ihidev_attach_arg {
112 	struct i2c_attach_args	*iaa;
113 	struct ihidev_softc	*parent;
114 	uint8_t			 reportid;
115 	uint8_t			 claims[16];
116 	uint8_t			 nclaims;
117 #define	IHIDEV_CLAIM_MULTIPLEID	255
118 };
119 
120 struct i2c_hid_report_request {
121 	u_int id;
122 	u_int type;
123 #define I2C_HID_REPORT_TYPE_INPUT	0x1
124 #define I2C_HID_REPORT_TYPE_OUTPUT	0x2
125 #define I2C_HID_REPORT_TYPE_FEATURE	0x3
126 	void *data;
127 	u_int len;
128 };
129 
130 void ihidev_get_report_desc(struct ihidev_softc *, void **, int *);
131 int ihidev_open(struct ihidev *);
132 void ihidev_close(struct ihidev *);
133 int ihidev_ioctl(struct ihidev *, u_long, caddr_t, int, struct proc *);
134 
135 int ihidev_report_type_conv(int);
136 int ihidev_set_report(struct device *, int, int, void *, int);
137 int ihidev_get_report(struct device *, int, int, void *, int);
138