2 * Prototypes, structure definitions and macros.
4 * Copyright (c) 2000-2003 Johannes Erdfelt <johannes@erdfelt.com>
6 * This library is covered by the LGPL, read LICENSE for details.
8 * This file (and only this file) may alternatively be licensed under the
9 * BSD license as well, read LICENSE for details.
18 #include <sys/param.h>
22 * USB spec information
24 * This is all stuff grabbed from various USB specs and is pretty much
25 * not subject to change
29 * Device and/or Interface Class codes
31 #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
32 #define USB_CLASS_AUDIO 1
33 #define USB_CLASS_COMM 2
34 #define USB_CLASS_HID 3
35 #define USB_CLASS_PRINTER 7
36 #define USB_CLASS_PTP 6
37 #define USB_CLASS_MASS_STORAGE 8
38 #define USB_CLASS_HUB 9
39 #define USB_CLASS_DATA 10
40 #define USB_CLASS_VENDOR_SPEC 0xff
45 #define USB_DT_DEVICE 0x01
46 #define USB_DT_CONFIG 0x02
47 #define USB_DT_STRING 0x03
48 #define USB_DT_INTERFACE 0x04
49 #define USB_DT_ENDPOINT 0x05
51 #define USB_DT_HID 0x21
52 #define USB_DT_REPORT 0x22
53 #define USB_DT_PHYSICAL 0x23
54 #define USB_DT_HUB 0x29
57 * Descriptor sizes per descriptor type
59 #define USB_DT_DEVICE_SIZE 18
60 #define USB_DT_CONFIG_SIZE 9
61 #define USB_DT_INTERFACE_SIZE 9
62 #define USB_DT_ENDPOINT_SIZE 7
63 #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
64 #define USB_DT_HUB_NONVAR_SIZE 7
66 /* All standard descriptors have these 2 fields in common */
67 struct usb_descriptor_header
{
69 uint8_t bDescriptorType
;
70 } __attribute__ ((packed
));
72 /* String descriptor */
73 struct usb_string_descriptor
{
75 uint8_t bDescriptorType
;
77 } __attribute__ ((packed
));
80 struct usb_hid_descriptor
{
82 uint8_t bDescriptorType
;
85 uint8_t bNumDescriptors
;
86 /* uint8_t bReportDescriptorType; */
87 /* uint16_t wDescriptorLength; */
89 } __attribute__ ((packed
));
91 /* Endpoint descriptor */
92 #define USB_MAXENDPOINTS 32
93 struct usb_endpoint_descriptor
{
95 uint8_t bDescriptorType
;
96 uint8_t bEndpointAddress
;
98 uint16_t wMaxPacketSize
;
101 uint8_t bSynchAddress
;
103 unsigned char *extra
; /* Extra descriptors */
107 #define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
108 #define USB_ENDPOINT_DIR_MASK 0x80
110 #define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */
111 #define USB_ENDPOINT_TYPE_CONTROL 0
112 #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1
113 #define USB_ENDPOINT_TYPE_BULK 2
114 #define USB_ENDPOINT_TYPE_INTERRUPT 3
116 /* Interface descriptor */
117 #define USB_MAXINTERFACES 32
118 struct usb_interface_descriptor
{
120 uint8_t bDescriptorType
;
121 uint8_t bInterfaceNumber
;
122 uint8_t bAlternateSetting
;
123 uint8_t bNumEndpoints
;
124 uint8_t bInterfaceClass
;
125 uint8_t bInterfaceSubClass
;
126 uint8_t bInterfaceProtocol
;
129 struct usb_endpoint_descriptor
*endpoint
;
131 unsigned char *extra
; /* Extra descriptors */
135 #define USB_MAXALTSETTING 128 /* Hard limit */
136 struct usb_interface
{
137 struct usb_interface_descriptor
*altsetting
;
142 /* Configuration descriptor information.. */
143 #define USB_MAXCONFIG 8
144 struct usb_config_descriptor
{
146 uint8_t bDescriptorType
;
147 uint16_t wTotalLength
;
148 uint8_t bNumInterfaces
;
149 uint8_t bConfigurationValue
;
150 uint8_t iConfiguration
;
151 uint8_t bmAttributes
;
154 struct usb_interface
*interface
;
156 unsigned char *extra
; /* Extra descriptors */
160 /* Device descriptor */
161 struct usb_device_descriptor
{
163 uint8_t bDescriptorType
;
165 uint8_t bDeviceClass
;
166 uint8_t bDeviceSubClass
;
167 uint8_t bDeviceProtocol
;
168 uint8_t bMaxPacketSize0
;
172 uint8_t iManufacturer
;
174 uint8_t iSerialNumber
;
175 uint8_t bNumConfigurations
;
176 } __attribute__ ((packed
));
178 struct usb_ctrl_setup
{
179 uint8_t bRequestType
;
184 } __attribute__ ((packed
));
189 #define USB_REQ_GET_STATUS 0x00
190 #define USB_REQ_CLEAR_FEATURE 0x01
191 /* 0x02 is reserved */
192 #define USB_REQ_SET_FEATURE 0x03
193 /* 0x04 is reserved */
194 #define USB_REQ_SET_ADDRESS 0x05
195 #define USB_REQ_GET_DESCRIPTOR 0x06
196 #define USB_REQ_SET_DESCRIPTOR 0x07
197 #define USB_REQ_GET_CONFIGURATION 0x08
198 #define USB_REQ_SET_CONFIGURATION 0x09
199 #define USB_REQ_GET_INTERFACE 0x0A
200 #define USB_REQ_SET_INTERFACE 0x0B
201 #define USB_REQ_SYNCH_FRAME 0x0C
203 #define USB_TYPE_STANDARD (0x00 << 5)
204 #define USB_TYPE_CLASS (0x01 << 5)
205 #define USB_TYPE_VENDOR (0x02 << 5)
206 #define USB_TYPE_RESERVED (0x03 << 5)
208 #define USB_RECIP_DEVICE 0x00
209 #define USB_RECIP_INTERFACE 0x01
210 #define USB_RECIP_ENDPOINT 0x02
211 #define USB_RECIP_OTHER 0x03
214 * Various libusb API related stuff
217 #define USB_ENDPOINT_IN 0x80
218 #define USB_ENDPOINT_OUT 0x00
221 #define USB_ERROR_BEGIN 500000
224 * This is supposed to look weird. This file is generated from autoconf
225 * and I didn't want to make this too complicated.
228 #define USB_LE16_TO_CPU(x) do { x = ((x & 0xff) << 8) | ((x & 0xff00) >> 8); } while(0)
230 #define USB_LE16_TO_CPU(x)
238 * To maintain compatibility with applications already built with libusb,
239 * we must only add entries to the end of this structure. NEVER delete or
240 * move members and only change types if you really know what you're doing.
243 struct usb_device
*next
, *prev
;
245 char filename
[PATH_MAX
+ 1];
249 struct usb_device_descriptor descriptor
;
250 struct usb_config_descriptor
*config
;
252 void *dev
; /* Darwin support */
256 unsigned char num_children
;
257 struct usb_device
**children
;
261 struct usb_bus
*next
, *prev
;
263 char dirname
[PATH_MAX
+ 1];
265 struct usb_device
*devices
;
268 struct usb_device
*root_dev
;
271 struct usb_dev_handle
;
272 typedef struct usb_dev_handle usb_dev_handle
;
275 extern struct usb_bus
*usb_busses
;
281 /* Function prototypes */
284 usb_dev_handle
*usb_open(struct usb_device
*dev
);
285 int usb_close(usb_dev_handle
*dev
);
286 int usb_get_string(usb_dev_handle
*dev
, int index
, int langid
, char *buf
,
288 int usb_get_string_simple(usb_dev_handle
*dev
, int index
, char *buf
,
292 int usb_get_descriptor_by_endpoint(usb_dev_handle
*udev
, int ep
,
293 unsigned char type
, unsigned char index
, void *buf
, int size
);
294 int usb_get_descriptor(usb_dev_handle
*udev
, unsigned char type
,
295 unsigned char index
, void *buf
, int size
);
298 int usb_bulk_write(usb_dev_handle
*dev
, int ep
, char *bytes
, int size
,
300 int usb_bulk_read(usb_dev_handle
*dev
, int ep
, char *bytes
, int size
,
302 int usb_interrupt_write(usb_dev_handle
*dev
, int ep
, char *bytes
, int size
,
304 int usb_interrupt_read(usb_dev_handle
*dev
, int ep
, char *bytes
, int size
,
306 int usb_control_msg(usb_dev_handle
*dev
, int requesttype
, int request
,
307 int value
, int index
, char *bytes
, int size
, int timeout
);
308 int usb_set_configuration(usb_dev_handle
*dev
, int configuration
);
309 int usb_claim_interface(usb_dev_handle
*dev
, int interface
);
310 int usb_release_interface(usb_dev_handle
*dev
, int interface
);
311 int usb_set_altinterface(usb_dev_handle
*dev
, int alternate
);
312 int usb_resetep(usb_dev_handle
*dev
, unsigned int ep
);
313 int usb_clear_halt(usb_dev_handle
*dev
, unsigned int ep
);
314 int usb_reset(usb_dev_handle
*dev
);
317 #define LIBUSB_HAS_GET_DRIVER_NP 1
318 int usb_get_driver_np(usb_dev_handle
*dev
, int interface
, char *name
,
319 unsigned int namelen
);
320 #define LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP 1
321 int usb_detach_kernel_driver_np(usb_dev_handle
*dev
, int interface
);
323 int usb_bulk_write_low_latency(usb_dev_handle
*dev
, int ep
, char *bytes
, int size
,
325 int usb_interrupt_write_low_latency(usb_dev_handle
*dev
, int ep
, char *bytes
, int size
,
329 int usb_bulk_setup_async(usb_dev_handle
*dev
, void **context
,
331 int usb_interrupt_setup_async(usb_dev_handle
*dev
, void **context
,
334 int usb_submit_async(void *context
, char *bytes
, int size
);
335 int usb_reap_async(void *context
, int timeout
);
336 int usb_reap_async_nocancel(void *context
, int timeout
);
337 int usb_async_is_done(void *context
);
338 int usb_cancel_async(void *context
);
339 int usb_free_async(void *context
);
340 int usb_async_get_fd(void *context
);
341 int usb_max_read_write();
345 char *usb_strerror(void);
348 void usb_set_debug(int level
);
349 int usb_find_busses(void);
350 int usb_find_devices(void);
351 struct usb_device
*usb_device(usb_dev_handle
*dev
);
352 struct usb_bus
*usb_get_busses(void);
358 #endif /* __USB_H__ */