3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #ifndef _LIBUSB20_COMPAT_01_H_
28 #define _LIBUSB20_COMPAT_01_H_
30 #include <sys/param.h>
31 #include <sys/endian.h>
35 /* USB interface class codes */
37 #define USB_CLASS_PER_INTERFACE 0
38 #define USB_CLASS_AUDIO 1
39 #define USB_CLASS_COMM 2
40 #define USB_CLASS_HID 3
41 #define USB_CLASS_PRINTER 7
42 #define USB_CLASS_PTP 6
43 #define USB_CLASS_MASS_STORAGE 8
44 #define USB_CLASS_HUB 9
45 #define USB_CLASS_DATA 10
46 #define USB_CLASS_VENDOR_SPEC 0xff
48 /* USB descriptor types */
50 #define USB_DT_DEVICE 0x01
51 #define USB_DT_CONFIG 0x02
52 #define USB_DT_STRING 0x03
53 #define USB_DT_INTERFACE 0x04
54 #define USB_DT_ENDPOINT 0x05
56 #define USB_DT_HID 0x21
57 #define USB_DT_REPORT 0x22
58 #define USB_DT_PHYSICAL 0x23
59 #define USB_DT_HUB 0x29
61 /* USB descriptor type sizes */
63 #define USB_DT_DEVICE_SIZE 18
64 #define USB_DT_CONFIG_SIZE 9
65 #define USB_DT_INTERFACE_SIZE 9
66 #define USB_DT_ENDPOINT_SIZE 7
67 #define USB_DT_ENDPOINT_AUDIO_SIZE 9
68 #define USB_DT_HUB_NONVAR_SIZE 7
70 /* USB descriptor header */
71 struct usb_descriptor_header {
73 uint8_t bDescriptorType;
76 /* USB string descriptor */
77 struct usb_string_descriptor {
79 uint8_t bDescriptorType;
83 /* USB HID descriptor */
84 struct usb_hid_descriptor {
86 uint8_t bDescriptorType;
89 uint8_t bNumDescriptors;
90 /* uint8_t bReportDescriptorType; */
91 /* uint16_t wDescriptorLength; */
95 /* USB endpoint descriptor */
96 #define USB_MAXENDPOINTS 32
97 struct usb_endpoint_descriptor {
99 uint8_t bDescriptorType;
100 uint8_t bEndpointAddress;
101 #define USB_ENDPOINT_ADDRESS_MASK 0x0f
102 #define USB_ENDPOINT_DIR_MASK 0x80
103 uint8_t bmAttributes;
104 #define USB_ENDPOINT_TYPE_MASK 0x03
105 #define USB_ENDPOINT_TYPE_CONTROL 0
106 #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1
107 #define USB_ENDPOINT_TYPE_BULK 2
108 #define USB_ENDPOINT_TYPE_INTERRUPT 3
109 uint16_t wMaxPacketSize;
112 uint8_t bSynchAddress;
114 uint8_t *extra; /* Extra descriptors */
118 /* USB interface descriptor */
119 #define USB_MAXINTERFACES 32
120 struct usb_interface_descriptor {
122 uint8_t bDescriptorType;
123 uint8_t bInterfaceNumber;
124 uint8_t bAlternateSetting;
125 uint8_t bNumEndpoints;
126 uint8_t bInterfaceClass;
127 uint8_t bInterfaceSubClass;
128 uint8_t bInterfaceProtocol;
131 struct usb_endpoint_descriptor *endpoint;
133 uint8_t *extra; /* Extra descriptors */
137 #define USB_MAXALTSETTING 128 /* Hard limit */
138 struct usb_interface {
139 struct usb_interface_descriptor *altsetting;
144 /* USB configuration descriptor */
145 #define USB_MAXCONFIG 8
146 struct usb_config_descriptor {
148 uint8_t bDescriptorType;
149 uint16_t wTotalLength;
150 uint8_t bNumInterfaces;
151 uint8_t bConfigurationValue;
152 uint8_t iConfiguration;
153 uint8_t bmAttributes;
156 struct usb_interface *interface;
158 uint8_t *extra; /* Extra descriptors */
162 /* USB device descriptor */
163 struct usb_device_descriptor {
165 uint8_t bDescriptorType;
167 uint8_t bDeviceClass;
168 uint8_t bDeviceSubClass;
169 uint8_t bDeviceProtocol;
170 uint8_t bMaxPacketSize0;
174 uint8_t iManufacturer;
176 uint8_t iSerialNumber;
177 uint8_t bNumConfigurations;
180 /* USB setup packet */
181 struct usb_ctrl_setup {
182 uint8_t bRequestType;
183 #define USB_RECIP_DEVICE 0x00
184 #define USB_RECIP_INTERFACE 0x01
185 #define USB_RECIP_ENDPOINT 0x02
186 #define USB_RECIP_OTHER 0x03
187 #define USB_TYPE_STANDARD (0x00 << 5)
188 #define USB_TYPE_CLASS (0x01 << 5)
189 #define USB_TYPE_VENDOR (0x02 << 5)
190 #define USB_TYPE_RESERVED (0x03 << 5)
191 #define USB_ENDPOINT_IN 0x80
192 #define USB_ENDPOINT_OUT 0x00
194 #define USB_REQ_GET_STATUS 0x00
195 #define USB_REQ_CLEAR_FEATURE 0x01
196 #define USB_REQ_SET_FEATURE 0x03
197 #define USB_REQ_SET_ADDRESS 0x05
198 #define USB_REQ_GET_DESCRIPTOR 0x06
199 #define USB_REQ_SET_DESCRIPTOR 0x07
200 #define USB_REQ_GET_CONFIGURATION 0x08
201 #define USB_REQ_SET_CONFIGURATION 0x09
202 #define USB_REQ_GET_INTERFACE 0x0A
203 #define USB_REQ_SET_INTERFACE 0x0B
204 #define USB_REQ_SYNCH_FRAME 0x0C
211 #define USB_ERROR_BEGIN 500000
214 #define USB_LE16_TO_CPU(x) le16toh(x)
221 * To maintain compatibility with applications already built with libusb,
222 * we must only add entries to the end of this structure. NEVER delete or
223 * move members and only change types if you really know what you're doing.
226 struct usb_device *next;
227 struct usb_device *prev;
229 char filename[PATH_MAX + 1];
233 struct usb_device_descriptor descriptor;
234 struct usb_config_descriptor *config;
240 uint8_t num_children;
241 struct usb_device **children;
245 struct usb_bus *next;
246 struct usb_bus *prev;
248 char dirname[PATH_MAX + 1];
250 struct usb_device *devices;
253 struct usb_device *root_dev;
256 struct usb_dev_handle;
257 typedef struct usb_dev_handle usb_dev_handle;
260 extern struct usb_bus *usb_busses;
270 /* Function prototypes from "libusb20_compat01.c" */
272 usb_dev_handle *usb_open(struct usb_device *dev);
273 int usb_close(usb_dev_handle * dev);
274 int usb_get_string(usb_dev_handle * dev, int index, int langid, char *buf, size_t buflen);
275 int usb_get_string_simple(usb_dev_handle * dev, int index, char *buf, size_t buflen);
276 int usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type, uint8_t index, void *buf, int size);
277 int usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t index, void *buf, int size);
278 int usb_parse_descriptor(uint8_t *source, char *description, void *dest);
279 int usb_parse_configuration(struct usb_config_descriptor *config, uint8_t *buffer);
280 void usb_destroy_configuration(struct usb_device *dev);
281 void usb_fetch_and_parse_descriptors(usb_dev_handle * udev);
282 int usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
283 int usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
284 int usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
285 int usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes, int size, int timeout);
286 int usb_control_msg(usb_dev_handle * dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
287 int usb_set_configuration(usb_dev_handle * dev, int configuration);
288 int usb_claim_interface(usb_dev_handle * dev, int interface);
289 int usb_release_interface(usb_dev_handle * dev, int interface);
290 int usb_set_altinterface(usb_dev_handle * dev, int alternate);
291 int usb_resetep(usb_dev_handle * dev, unsigned int ep);
292 int usb_clear_halt(usb_dev_handle * dev, unsigned int ep);
293 int usb_reset(usb_dev_handle * dev);
294 const char *usb_strerror(void);
296 void usb_set_debug(int level);
297 int usb_find_busses(void);
298 int usb_find_devices(void);
299 struct usb_device *usb_device(usb_dev_handle * dev);
300 struct usb_bus *usb_get_busses(void);
310 #endif /* _LIBUSB20_COMPAT01_H_ */