usb.h revision 203775
1/* $FreeBSD: head/lib/libusb/usb.h 203775 2010-02-11 08:34:41Z wkoszek $ */
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#include <stdint.h>
31#include <sys/types.h>
32#include <sys/param.h>
33
34/* USB interface class codes */
35
36#define	USB_CLASS_PER_INTERFACE         0
37#define	USB_CLASS_AUDIO                 1
38#define	USB_CLASS_COMM                  2
39#define	USB_CLASS_HID                   3
40#define	USB_CLASS_PRINTER               7
41#define	USB_CLASS_PTP                   6
42#define	USB_CLASS_MASS_STORAGE          8
43#define	USB_CLASS_HUB                   9
44#define	USB_CLASS_DATA                  10
45#define	USB_CLASS_VENDOR_SPEC           0xff
46
47/* USB descriptor types */
48
49#define	USB_DT_DEVICE                   0x01
50#define	USB_DT_CONFIG                   0x02
51#define	USB_DT_STRING                   0x03
52#define	USB_DT_INTERFACE                0x04
53#define	USB_DT_ENDPOINT                 0x05
54
55#define	USB_DT_HID                      0x21
56#define	USB_DT_REPORT                   0x22
57#define	USB_DT_PHYSICAL                 0x23
58#define	USB_DT_HUB                      0x29
59
60/* USB descriptor type sizes */
61
62#define	USB_DT_DEVICE_SIZE              18
63#define	USB_DT_CONFIG_SIZE              9
64#define	USB_DT_INTERFACE_SIZE           9
65#define	USB_DT_ENDPOINT_SIZE            7
66#define	USB_DT_ENDPOINT_AUDIO_SIZE      9
67#define	USB_DT_HUB_NONVAR_SIZE          7
68
69/* USB descriptor header */
70struct usb_descriptor_header {
71	uint8_t	bLength;
72	uint8_t	bDescriptorType;
73};
74
75/* USB string descriptor */
76struct usb_string_descriptor {
77	uint8_t	bLength;
78	uint8_t	bDescriptorType;
79	uint16_t wData[1];
80};
81
82/* USB HID descriptor */
83struct usb_hid_descriptor {
84	uint8_t	bLength;
85	uint8_t	bDescriptorType;
86	uint16_t bcdHID;
87	uint8_t	bCountryCode;
88	uint8_t	bNumDescriptors;
89	/* uint8_t  bReportDescriptorType; */
90	/* uint16_t wDescriptorLength; */
91	/* ... */
92};
93
94/* USB endpoint descriptor */
95#define	USB_MAXENDPOINTS        32
96struct usb_endpoint_descriptor {
97	uint8_t	bLength;
98	uint8_t	bDescriptorType;
99	uint8_t	bEndpointAddress;
100#define	USB_ENDPOINT_ADDRESS_MASK       0x0f
101#define	USB_ENDPOINT_DIR_MASK           0x80
102	uint8_t	bmAttributes;
103#define	USB_ENDPOINT_TYPE_MASK          0x03
104#define	USB_ENDPOINT_TYPE_CONTROL       0
105#define	USB_ENDPOINT_TYPE_ISOCHRONOUS   1
106#define	USB_ENDPOINT_TYPE_BULK          2
107#define	USB_ENDPOINT_TYPE_INTERRUPT     3
108	uint16_t wMaxPacketSize;
109	uint8_t	bInterval;
110	uint8_t	bRefresh;
111	uint8_t	bSynchAddress;
112
113	uint8_t *extra;			/* Extra descriptors */
114	int	extralen;
115};
116
117/* USB interface descriptor */
118#define	USB_MAXINTERFACES       32
119struct usb_interface_descriptor {
120	uint8_t	bLength;
121	uint8_t	bDescriptorType;
122	uint8_t	bInterfaceNumber;
123	uint8_t	bAlternateSetting;
124	uint8_t	bNumEndpoints;
125	uint8_t	bInterfaceClass;
126	uint8_t	bInterfaceSubClass;
127	uint8_t	bInterfaceProtocol;
128	uint8_t	iInterface;
129
130	struct usb_endpoint_descriptor *endpoint;
131
132	uint8_t *extra;			/* Extra descriptors */
133	int	extralen;
134};
135
136#define	USB_MAXALTSETTING       128	/* Hard limit */
137struct usb_interface {
138	struct usb_interface_descriptor *altsetting;
139
140	int	num_altsetting;
141};
142
143/* USB configuration descriptor */
144#define	USB_MAXCONFIG           8
145struct usb_config_descriptor {
146	uint8_t	bLength;
147	uint8_t	bDescriptorType;
148	uint16_t wTotalLength;
149	uint8_t	bNumInterfaces;
150	uint8_t	bConfigurationValue;
151	uint8_t	iConfiguration;
152	uint8_t	bmAttributes;
153	uint8_t	MaxPower;
154
155	struct usb_interface *interface;
156
157	uint8_t *extra;			/* Extra descriptors */
158	int	extralen;
159};
160
161/* USB device descriptor */
162struct usb_device_descriptor {
163	uint8_t	bLength;
164	uint8_t	bDescriptorType;
165	uint16_t bcdUSB;
166	uint8_t	bDeviceClass;
167	uint8_t	bDeviceSubClass;
168	uint8_t	bDeviceProtocol;
169	uint8_t	bMaxPacketSize0;
170	uint16_t idVendor;
171	uint16_t idProduct;
172	uint16_t bcdDevice;
173	uint8_t	iManufacturer;
174	uint8_t	iProduct;
175	uint8_t	iSerialNumber;
176	uint8_t	bNumConfigurations;
177};
178
179/* USB setup packet */
180struct usb_ctrl_setup {
181	uint8_t	bRequestType;
182#define	USB_RECIP_DEVICE                0x00
183#define	USB_RECIP_INTERFACE             0x01
184#define	USB_RECIP_ENDPOINT              0x02
185#define	USB_RECIP_OTHER                 0x03
186#define	USB_TYPE_STANDARD               (0x00 << 5)
187#define	USB_TYPE_CLASS                  (0x01 << 5)
188#define	USB_TYPE_VENDOR                 (0x02 << 5)
189#define	USB_TYPE_RESERVED               (0x03 << 5)
190#define	USB_ENDPOINT_IN                 0x80
191#define	USB_ENDPOINT_OUT                0x00
192	uint8_t	bRequest;
193#define	USB_REQ_GET_STATUS              0x00
194#define	USB_REQ_CLEAR_FEATURE           0x01
195#define	USB_REQ_SET_FEATURE             0x03
196#define	USB_REQ_SET_ADDRESS             0x05
197#define	USB_REQ_GET_DESCRIPTOR          0x06
198#define	USB_REQ_SET_DESCRIPTOR          0x07
199#define	USB_REQ_GET_CONFIGURATION       0x08
200#define	USB_REQ_SET_CONFIGURATION       0x09
201#define	USB_REQ_GET_INTERFACE           0x0A
202#define	USB_REQ_SET_INTERFACE           0x0B
203#define	USB_REQ_SYNCH_FRAME             0x0C
204	uint16_t wValue;
205	uint16_t wIndex;
206	uint16_t wLength;
207};
208
209/* Error codes */
210#define	USB_ERROR_BEGIN                 500000
211
212/* Byte swapping */
213#define	USB_LE16_TO_CPU(x) le16toh(x)
214
215/* Data types */
216struct usb_device;
217struct usb_bus;
218
219/*
220 * To maintain compatibility with applications already built with libusb,
221 * we must only add entries to the end of this structure. NEVER delete or
222 * move members and only change types if you really know what you're doing.
223 */
224struct usb_device {
225	struct usb_device *next;
226	struct usb_device *prev;
227
228	char	filename[PATH_MAX + 1];
229
230	struct usb_bus *bus;
231
232	struct usb_device_descriptor descriptor;
233	struct usb_config_descriptor *config;
234
235	void   *dev;
236
237	uint8_t	devnum;
238
239	uint8_t	num_children;
240	struct usb_device **children;
241};
242
243struct usb_bus {
244	struct usb_bus *next;
245	struct usb_bus *prev;
246
247	char	dirname[PATH_MAX + 1];
248
249	struct usb_device *devices;
250	uint32_t location;
251
252	struct usb_device *root_dev;
253};
254
255struct usb_dev_handle;
256typedef struct usb_dev_handle usb_dev_handle;
257
258/* Variables */
259extern struct usb_bus *usb_busses;
260
261#ifdef __cplusplus
262extern	"C" {
263#endif
264#if 0
265}					/* style */
266
267#endif
268
269/* Function prototypes from "libusb20_compat01.c" */
270
271usb_dev_handle *usb_open(struct usb_device *dev);
272int	usb_close(usb_dev_handle * dev);
273int	usb_get_string(usb_dev_handle * dev, int index, int langid, char *buf, size_t buflen);
274int	usb_get_string_simple(usb_dev_handle * dev, int index, char *buf, size_t buflen);
275int	usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type, uint8_t index, void *buf, int size);
276int	usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t index, void *buf, int size);
277int	usb_parse_descriptor(uint8_t *source, char *description, void *dest);
278int	usb_parse_configuration(struct usb_config_descriptor *config, uint8_t *buffer);
279void	usb_destroy_configuration(struct usb_device *dev);
280void	usb_fetch_and_parse_descriptors(usb_dev_handle * udev);
281int	usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
282int	usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
283int	usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
284int	usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
285int	usb_control_msg(usb_dev_handle * dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
286int	usb_set_configuration(usb_dev_handle * dev, int configuration);
287int	usb_claim_interface(usb_dev_handle * dev, int interface);
288int	usb_release_interface(usb_dev_handle * dev, int interface);
289int	usb_set_altinterface(usb_dev_handle * dev, int alternate);
290int	usb_resetep(usb_dev_handle * dev, unsigned int ep);
291int	usb_clear_halt(usb_dev_handle * dev, unsigned int ep);
292int	usb_reset(usb_dev_handle * dev);
293const char *usb_strerror(void);
294void	usb_init(void);
295void	usb_set_debug(int level);
296int	usb_find_busses(void);
297int	usb_find_devices(void);
298struct usb_device *usb_device(usb_dev_handle * dev);
299struct usb_bus *usb_get_busses(void);
300
301#if 0
302{					/* style */
303#endif
304#ifdef __cplusplus
305}
306
307#endif
308
309#endif					/* _LIBUSB20_COMPAT01_H_ */
310