xref: /openbsd-src/sys/dev/i2c/ihidev.h (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /* $OpenBSD: ihidev.h,v 1.4 2016/01/31 18:24:35 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 /* from usbdi.h: Match codes. */
21 /* First five codes is for a whole device. */
22 #define IMATCH_VENDOR_PRODUCT_REV			14
23 #define IMATCH_VENDOR_PRODUCT				13
24 #define IMATCH_VENDOR_DEVCLASS_DEVPROTO			12
25 #define IMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO		11
26 #define IMATCH_DEVCLASS_DEVSUBCLASS			10
27 /* Next six codes are for interfaces. */
28 #define IMATCH_VENDOR_PRODUCT_REV_CONF_IFACE		 9
29 #define IMATCH_VENDOR_PRODUCT_CONF_IFACE		 8
30 #define IMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO		 7
31 #define IMATCH_VENDOR_IFACESUBCLASS			 6
32 #define IMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO	 5
33 #define IMATCH_IFACECLASS_IFACESUBCLASS			 4
34 #define IMATCH_IFACECLASS				 3
35 #define IMATCH_IFACECLASS_GENERIC			 2
36 /* Generic driver */
37 #define IMATCH_GENERIC					 1
38 /* No match */
39 #define IMATCH_NONE					 0
40 
41 #define IHIDBUSCF_REPORTID				0
42 #define IHIDBUSCF_REPORTID_DEFAULT			-1
43 
44 #define ihidevcf_reportid cf_loc[IHIDBUSCF_REPORTID]
45 #define IHIDEV_UNK_REPORTID IHIDBUSCF_REPORTID_DEFAULT
46 
47 /* 5.1.1 - HID Descriptor Format */
48 struct i2c_hid_desc {
49 	uint16_t wHIDDescLength;
50 	uint16_t bcdVersion;
51 	uint16_t wReportDescLength;
52 	uint16_t wReportDescRegister;
53 	uint16_t wInputRegister;
54 	uint16_t wMaxInputLength;
55 	uint16_t wOutputRegister;
56 	uint16_t wMaxOutputLength;
57 	uint16_t wCommandRegister;
58 	uint16_t wDataRegister;
59 	uint16_t wVendorID;
60 	uint16_t wProductID;
61 	uint16_t wVersionID;
62 	uint32_t reserved;
63 } __packed;
64 
65 struct ihidev_softc {
66 	struct device	sc_dev;
67 	i2c_tag_t	sc_tag;
68 
69 	i2c_addr_t	sc_addr;
70 	void		*sc_ih;
71 
72 	u_int		sc_hid_desc_addr;
73 	union {
74 		uint8_t	hid_desc_buf[sizeof(struct i2c_hid_desc)];
75 		struct i2c_hid_desc hid_desc;
76 	};
77 
78 	uint8_t		*sc_report;
79 	int		sc_reportlen;
80 
81 	int		sc_nrepid;
82 	struct		ihidev **sc_subdevs;
83 
84 	u_int		sc_isize;
85 	u_char		*sc_ibuf;
86 
87 	int		sc_refcnt;
88 };
89 
90 struct ihidev {
91 	struct device	sc_idev;
92 	struct ihidev_softc *sc_parent;
93 	uint8_t		sc_report_id;
94 	uint8_t		sc_state;
95 #define	IHIDEV_OPEN	0x01	/* device is open */
96 	void		(*sc_intr)(struct ihidev *, void *, u_int);
97 
98 	int		sc_isize;
99 	int		sc_osize;
100 	int		sc_fsize;
101 };
102 
103 struct ihidev_attach_arg {
104 	struct i2c_attach_args	*iaa;
105 	struct ihidev_softc	*parent;
106 	uint8_t			 reportid;
107 #define	IHIDEV_CLAIM_ALLREPORTID	255
108 };
109 
110 struct i2c_hid_report_request {
111 	u_int id;
112 	u_int type;
113 #define I2C_HID_REPORT_TYPE_INPUT	0x1
114 #define I2C_HID_REPORT_TYPE_OUTPUT	0x2
115 #define I2C_HID_REPORT_TYPE_FEATURE	0x3
116 	void *data;
117 	u_int len;
118 };
119 
120 void ihidev_get_report_desc(struct ihidev_softc *, void **, int *);
121 int ihidev_open(struct ihidev *);
122 void ihidev_close(struct ihidev *);
123 int ihidev_ioctl(struct ihidev *, u_long, caddr_t, int, struct proc *);
124 
125 int ihidev_set_report(struct device *, int, int, void *, int);
126 int ihidev_get_report(struct device *, int, int, void *, int);
127