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