xref: /freebsd/lib/libusb/usb.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef _LIBUSB20_COMPAT_01_H_
29 #define	_LIBUSB20_COMPAT_01_H_
30 
31 #ifndef LIBUSB_GLOBAL_INCLUDE_FILE
32 #include <stdint.h>
33 #include <sys/param.h>
34 #include <sys/endian.h>
35 #endif
36 
37 /* USB interface class codes */
38 
39 #define	USB_CLASS_PER_INTERFACE         0
40 #define	USB_CLASS_AUDIO                 1
41 #define	USB_CLASS_COMM                  2
42 #define	USB_CLASS_HID                   3
43 #define	USB_CLASS_PRINTER               7
44 #define	USB_CLASS_PTP                   6
45 #define	USB_CLASS_MASS_STORAGE          8
46 #define	USB_CLASS_HUB                   9
47 #define	USB_CLASS_DATA                  10
48 #define	USB_CLASS_VENDOR_SPEC           0xff
49 
50 /* USB descriptor types */
51 
52 #define	USB_DT_DEVICE                   0x01
53 #define	USB_DT_CONFIG                   0x02
54 #define	USB_DT_STRING                   0x03
55 #define	USB_DT_INTERFACE                0x04
56 #define	USB_DT_ENDPOINT                 0x05
57 
58 #define	USB_DT_HID                      0x21
59 #define	USB_DT_REPORT                   0x22
60 #define	USB_DT_PHYSICAL                 0x23
61 #define	USB_DT_HUB                      0x29
62 
63 /* USB descriptor type sizes */
64 
65 #define	USB_DT_DEVICE_SIZE              18
66 #define	USB_DT_CONFIG_SIZE              9
67 #define	USB_DT_INTERFACE_SIZE           9
68 #define	USB_DT_ENDPOINT_SIZE            7
69 #define	USB_DT_ENDPOINT_AUDIO_SIZE      9
70 #define	USB_DT_HUB_NONVAR_SIZE          7
71 
72 /* USB descriptor header */
73 struct usb_descriptor_header {
74 	uint8_t	bLength;
75 	uint8_t	bDescriptorType;
76 };
77 
78 /* USB string descriptor */
79 struct usb_string_descriptor {
80 	uint8_t	bLength;
81 	uint8_t	bDescriptorType;
82 	uint16_t wData[1];
83 };
84 
85 /* USB HID descriptor */
86 struct usb_hid_descriptor {
87 	uint8_t	bLength;
88 	uint8_t	bDescriptorType;
89 	uint16_t bcdHID;
90 	uint8_t	bCountryCode;
91 	uint8_t	bNumDescriptors;
92 	/* uint8_t  bReportDescriptorType; */
93 	/* uint16_t wDescriptorLength; */
94 	/* ... */
95 };
96 
97 /* USB endpoint descriptor */
98 #define	USB_MAXENDPOINTS        32
99 struct usb_endpoint_descriptor {
100 	uint8_t	bLength;
101 	uint8_t	bDescriptorType;
102 	uint8_t	bEndpointAddress;
103 #define	USB_ENDPOINT_ADDRESS_MASK       0x0f
104 #define	USB_ENDPOINT_DIR_MASK           0x80
105 	uint8_t	bmAttributes;
106 #define	USB_ENDPOINT_TYPE_MASK          0x03
107 #define	USB_ENDPOINT_TYPE_CONTROL       0
108 #define	USB_ENDPOINT_TYPE_ISOCHRONOUS   1
109 #define	USB_ENDPOINT_TYPE_BULK          2
110 #define	USB_ENDPOINT_TYPE_INTERRUPT     3
111 	uint16_t wMaxPacketSize;
112 	uint8_t	bInterval;
113 	uint8_t	bRefresh;
114 	uint8_t	bSynchAddress;
115 
116 	uint8_t *extra;			/* Extra descriptors */
117 	int	extralen;
118 };
119 
120 /* USB interface descriptor */
121 #define	USB_MAXINTERFACES       32
122 struct usb_interface_descriptor {
123 	uint8_t	bLength;
124 	uint8_t	bDescriptorType;
125 	uint8_t	bInterfaceNumber;
126 	uint8_t	bAlternateSetting;
127 	uint8_t	bNumEndpoints;
128 	uint8_t	bInterfaceClass;
129 	uint8_t	bInterfaceSubClass;
130 	uint8_t	bInterfaceProtocol;
131 	uint8_t	iInterface;
132 
133 	struct usb_endpoint_descriptor *endpoint;
134 
135 	uint8_t *extra;			/* Extra descriptors */
136 	int	extralen;
137 };
138 
139 #define	USB_MAXALTSETTING       128	/* Hard limit */
140 struct usb_interface {
141 	struct usb_interface_descriptor *altsetting;
142 
143 	int	num_altsetting;
144 };
145 
146 /* USB configuration descriptor */
147 #define	USB_MAXCONFIG           8
148 struct usb_config_descriptor {
149 	uint8_t	bLength;
150 	uint8_t	bDescriptorType;
151 	uint16_t wTotalLength;
152 	uint8_t	bNumInterfaces;
153 	uint8_t	bConfigurationValue;
154 	uint8_t	iConfiguration;
155 	uint8_t	bmAttributes;
156 	uint8_t	MaxPower;
157 
158 	struct usb_interface *interface;
159 
160 	uint8_t *extra;			/* Extra descriptors */
161 	int	extralen;
162 };
163 
164 /* USB device descriptor */
165 struct usb_device_descriptor {
166 	uint8_t	bLength;
167 	uint8_t	bDescriptorType;
168 	uint16_t bcdUSB;
169 	uint8_t	bDeviceClass;
170 	uint8_t	bDeviceSubClass;
171 	uint8_t	bDeviceProtocol;
172 	uint8_t	bMaxPacketSize0;
173 	uint16_t idVendor;
174 	uint16_t idProduct;
175 	uint16_t bcdDevice;
176 	uint8_t	iManufacturer;
177 	uint8_t	iProduct;
178 	uint8_t	iSerialNumber;
179 	uint8_t	bNumConfigurations;
180 };
181 
182 /* USB setup packet */
183 struct usb_ctrl_setup {
184 	uint8_t	bRequestType;
185 #define	USB_RECIP_DEVICE                0x00
186 #define	USB_RECIP_INTERFACE             0x01
187 #define	USB_RECIP_ENDPOINT              0x02
188 #define	USB_RECIP_OTHER                 0x03
189 #define	USB_TYPE_STANDARD               (0x00 << 5)
190 #define	USB_TYPE_CLASS                  (0x01 << 5)
191 #define	USB_TYPE_VENDOR                 (0x02 << 5)
192 #define	USB_TYPE_RESERVED               (0x03 << 5)
193 #define	USB_ENDPOINT_IN                 0x80
194 #define	USB_ENDPOINT_OUT                0x00
195 	uint8_t	bRequest;
196 #define	USB_REQ_GET_STATUS              0x00
197 #define	USB_REQ_CLEAR_FEATURE           0x01
198 #define	USB_REQ_SET_FEATURE             0x03
199 #define	USB_REQ_SET_ADDRESS             0x05
200 #define	USB_REQ_GET_DESCRIPTOR          0x06
201 #define	USB_REQ_SET_DESCRIPTOR          0x07
202 #define	USB_REQ_GET_CONFIGURATION       0x08
203 #define	USB_REQ_SET_CONFIGURATION       0x09
204 #define	USB_REQ_GET_INTERFACE           0x0A
205 #define	USB_REQ_SET_INTERFACE           0x0B
206 #define	USB_REQ_SYNCH_FRAME             0x0C
207 	uint16_t wValue;
208 	uint16_t wIndex;
209 	uint16_t wLength;
210 };
211 
212 /* Error codes */
213 #define	USB_ERROR_BEGIN                 500000
214 
215 /* Byte swapping */
216 #define	USB_LE16_TO_CPU(x) le16toh(x)
217 
218 /* Data types */
219 struct usb_device;
220 struct usb_bus;
221 
222 /*
223  * To maintain compatibility with applications already built with libusb,
224  * we must only add entries to the end of this structure. NEVER delete or
225  * move members and only change types if you really know what you're doing.
226  */
227 struct usb_device {
228 	struct usb_device *next;
229 	struct usb_device *prev;
230 
231 	char	filename[PATH_MAX + 1];
232 
233 	struct usb_bus *bus;
234 
235 	struct usb_device_descriptor descriptor;
236 	struct usb_config_descriptor *config;
237 
238 	void   *dev;
239 
240 	uint8_t	devnum;
241 
242 	uint8_t	num_children;
243 	struct usb_device **children;
244 };
245 
246 struct usb_bus {
247 	struct usb_bus *next;
248 	struct usb_bus *prev;
249 
250 	char	dirname[PATH_MAX + 1];
251 
252 	struct usb_device *devices;
253 	uint32_t location;
254 
255 	struct usb_device *root_dev;
256 };
257 
258 struct usb_dev_handle;
259 typedef struct usb_dev_handle usb_dev_handle;
260 
261 /* Variables */
262 extern struct usb_bus *usb_busses;
263 
264 #ifdef __cplusplus
265 extern	"C" {
266 #endif
267 #if 0
268 }					/* style */
269 
270 #endif
271 
272 /* Function prototypes from "libusb20_compat01.c" */
273 
274 usb_dev_handle *usb_open(struct usb_device *dev);
275 int	usb_close(usb_dev_handle * dev);
276 int	usb_get_string(usb_dev_handle * dev, int index, int langid, char *buf, size_t buflen);
277 int	usb_get_string_simple(usb_dev_handle * dev, int index, char *buf, size_t buflen);
278 int	usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type, uint8_t index, void *buf, int size);
279 int	usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t index, void *buf, int size);
280 int	usb_parse_descriptor(uint8_t *source, char *description, void *dest);
281 int	usb_parse_configuration(struct usb_config_descriptor *config, uint8_t *buffer);
282 void	usb_destroy_configuration(struct usb_device *dev);
283 void	usb_fetch_and_parse_descriptors(usb_dev_handle * udev);
284 int	usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
285 int	usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
286 int	usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
287 int	usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
288 int	usb_control_msg(usb_dev_handle * dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
289 int	usb_set_configuration(usb_dev_handle * dev, int configuration);
290 int	usb_claim_interface(usb_dev_handle * dev, int interface);
291 int	usb_release_interface(usb_dev_handle * dev, int interface);
292 int	usb_set_altinterface(usb_dev_handle * dev, int alternate);
293 int	usb_resetep(usb_dev_handle * dev, unsigned int ep);
294 int	usb_clear_halt(usb_dev_handle * dev, unsigned int ep);
295 int	usb_reset(usb_dev_handle * dev);
296 int	usb_check_connected(usb_dev_handle * dev);
297 const char *usb_strerror(void);
298 void	usb_init(void);
299 void	usb_set_debug(int level);
300 int	usb_find_busses(void);
301 int	usb_find_devices(void);
302 struct usb_device *usb_device(usb_dev_handle * dev);
303 struct usb_bus *usb_get_busses(void);
304 int	usb_get_driver_np(usb_dev_handle * dev, int interface, char *name, int namelen);
305 int	usb_detach_kernel_driver_np(usb_dev_handle * dev, int interface);
306 
307 #if 0
308 {					/* style */
309 #endif
310 #ifdef __cplusplus
311 }
312 
313 #endif
314 
315 #endif					/* _LIBUSB20_COMPAT01_H_ */
316