Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
264 masterof 1
#ifndef __USB_H__
2
#define __USB_H__
3
 
4
#include <stdlib.h>
5
 
6
/*
7
 * 'interface' is defined somewhere in the Windows header files. This macro
8
 * is deleted here to avoid conflicts and compile errors.
9
 */
10
 
11
#ifdef interface
12
#undef interface
13
#endif
14
 
15
/*
16
 * PATH_MAX from limits.h can't be used on Windows if the dll and
17
 * import libraries are build/used by different compilers
18
 */
19
 
20
#define LIBUSB_PATH_MAX 512
21
 
22
 
23
/*
24
 * USB spec information
25
 *
26
 * This is all stuff grabbed from various USB specs and is pretty much
27
 * not subject to change
28
 */
29
 
30
/*
31
 * Device and/or Interface Class codes
32
 */
33
#define USB_CLASS_PER_INTERFACE         0       /* for DeviceClass */
34
#define USB_CLASS_AUDIO                 1
35
#define USB_CLASS_COMM                  2
36
#define USB_CLASS_HID                   3
37
#define USB_CLASS_PRINTER               7
38
#define USB_CLASS_MASS_STORAGE          8
39
#define USB_CLASS_HUB                   9
40
#define USB_CLASS_DATA                  10
41
#define USB_CLASS_VENDOR_SPEC           0xff
42
 
43
/*
44
 * Descriptor types
45
 */
46
#define USB_DT_DEVICE                   0x01
47
#define USB_DT_CONFIG                   0x02
48
#define USB_DT_STRING                   0x03
49
#define USB_DT_INTERFACE                0x04
50
#define USB_DT_ENDPOINT                 0x05
51
 
52
#define USB_DT_HID                      0x21
53
#define USB_DT_REPORT                   0x22
54
#define USB_DT_PHYSICAL                 0x23
55
#define USB_DT_HUB                      0x29
56
 
57
/*
58
 * Descriptor sizes per descriptor type
59
 */
60
#define USB_DT_DEVICE_SIZE              18
61
#define USB_DT_CONFIG_SIZE              9
62
#define USB_DT_INTERFACE_SIZE           9
63
#define USB_DT_ENDPOINT_SIZE            7
64
#define USB_DT_ENDPOINT_AUDIO_SIZE      9       /* Audio extension */
65
#define USB_DT_HUB_NONVAR_SIZE          7
66
 
67
 
68
/* ensure byte-packed structures */
69
 
70
#include <pshpack1.h> 
71
 
72
 
73
/* All standard descriptors have these 2 fields in common */
74
struct usb_descriptor_header {
75
  unsigned char  bLength;
76
  unsigned char  bDescriptorType;
77
};
78
 
79
/* String descriptor */
80
struct usb_string_descriptor {
81
  unsigned char  bLength;
82
  unsigned char  bDescriptorType;
83
  unsigned short wData[1];
84
};
85
 
86
/* HID descriptor */
87
struct usb_hid_descriptor {
88
  unsigned char  bLength;
89
  unsigned char  bDescriptorType;
90
  unsigned short bcdHID;
91
  unsigned char  bCountryCode;
92
  unsigned char  bNumDescriptors;
93
};
94
 
95
/* Endpoint descriptor */
96
#define USB_MAXENDPOINTS        32
97
struct usb_endpoint_descriptor {
98
  unsigned char  bLength;
99
  unsigned char  bDescriptorType;
100
  unsigned char  bEndpointAddress;
101
  unsigned char  bmAttributes;
102
  unsigned short wMaxPacketSize;
103
  unsigned char  bInterval;
104
  unsigned char  bRefresh;
105
  unsigned char  bSynchAddress;
106
 
107
  unsigned char *extra; /* Extra descriptors */
108
  int extralen;
109
};
110
 
111
#define USB_ENDPOINT_ADDRESS_MASK       0x0f    /* in bEndpointAddress */
112
#define USB_ENDPOINT_DIR_MASK           0x80
113
 
114
#define USB_ENDPOINT_TYPE_MASK          0x03    /* in bmAttributes */
115
#define USB_ENDPOINT_TYPE_CONTROL       0
116
#define USB_ENDPOINT_TYPE_ISOCHRONOUS   1
117
#define USB_ENDPOINT_TYPE_BULK          2
118
#define USB_ENDPOINT_TYPE_INTERRUPT     3
119
 
120
/* Interface descriptor */
121
#define USB_MAXINTERFACES       32
122
struct usb_interface_descriptor {
123
  unsigned char  bLength;
124
  unsigned char  bDescriptorType;
125
  unsigned char  bInterfaceNumber;
126
  unsigned char  bAlternateSetting;
127
  unsigned char  bNumEndpoints;
128
  unsigned char  bInterfaceClass;
129
  unsigned char  bInterfaceSubClass;
130
  unsigned char  bInterfaceProtocol;
131
  unsigned char  iInterface;
132
 
133
  struct usb_endpoint_descriptor *endpoint;
134
 
135
  unsigned char *extra; /* Extra descriptors */
136
  int extralen;
137
};
138
 
139
#define USB_MAXALTSETTING       128     /* Hard limit */
140
 
141
struct usb_interface {
142
  struct usb_interface_descriptor *altsetting;
143
 
144
  int num_altsetting;
145
};
146
 
147
/* Configuration descriptor information.. */
148
#define USB_MAXCONFIG           8
149
struct usb_config_descriptor {
150
  unsigned char  bLength;
151
  unsigned char  bDescriptorType;
152
  unsigned short wTotalLength;
153
  unsigned char  bNumInterfaces;
154
  unsigned char  bConfigurationValue;
155
  unsigned char  iConfiguration;
156
  unsigned char  bmAttributes;
157
  unsigned char  MaxPower;
158
 
159
  struct usb_interface *interface;
160
 
161
  unsigned char *extra; /* Extra descriptors */
162
  int extralen;
163
};
164
 
165
/* Device descriptor */
166
struct usb_device_descriptor {
167
  unsigned char  bLength;
168
  unsigned char  bDescriptorType;
169
  unsigned short bcdUSB;
170
  unsigned char  bDeviceClass;
171
  unsigned char  bDeviceSubClass;
172
  unsigned char  bDeviceProtocol;
173
  unsigned char  bMaxPacketSize0;
174
  unsigned short idVendor;
175
  unsigned short idProduct;
176
  unsigned short bcdDevice;
177
  unsigned char  iManufacturer;
178
  unsigned char  iProduct;
179
  unsigned char  iSerialNumber;
180
  unsigned char  bNumConfigurations;
181
};
182
 
183
struct usb_ctrl_setup {
184
  unsigned char  bRequestType;
185
  unsigned char  bRequest;
186
  unsigned short wValue;
187
  unsigned short wIndex;
188
  unsigned short wLength;
189
};
190
 
191
/*
192
 * Standard requests
193
 */
194
#define USB_REQ_GET_STATUS              0x00
195
#define USB_REQ_CLEAR_FEATURE           0x01
196
/* 0x02 is reserved */
197
#define USB_REQ_SET_FEATURE             0x03
198
/* 0x04 is reserved */
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
 
208
#define USB_TYPE_STANDARD               (0x00 << 5)
209
#define USB_TYPE_CLASS                  (0x01 << 5)
210
#define USB_TYPE_VENDOR                 (0x02 << 5)
211
#define USB_TYPE_RESERVED               (0x03 << 5)
212
 
213
#define USB_RECIP_DEVICE                0x00
214
#define USB_RECIP_INTERFACE             0x01
215
#define USB_RECIP_ENDPOINT              0x02
216
#define USB_RECIP_OTHER                 0x03
217
 
218
/*
219
 * Various libusb API related stuff
220
 */
221
 
222
#define USB_ENDPOINT_IN                 0x80
223
#define USB_ENDPOINT_OUT                0x00
224
 
225
/* Error codes */
226
#define USB_ERROR_BEGIN                 500000
227
 
228
/*
229
 * This is supposed to look weird. This file is generated from autoconf
230
 * and I didn't want to make this too complicated.
231
 */
232
#define USB_LE16_TO_CPU(x)
233
 
234
/* Data types */
235
/* struct usb_device; */
236
/* struct usb_bus; */
237
 
238
struct usb_device {
239
  struct usb_device *next, *prev;
240
 
241
  char filename[LIBUSB_PATH_MAX];
242
 
243
  struct usb_bus *bus;
244
 
245
  struct usb_device_descriptor descriptor;
246
  struct usb_config_descriptor *config;
247
 
248
  void *dev;            /* Darwin support */
249
};
250
 
251
struct usb_bus {
252
  struct usb_bus *next, *prev;
253
 
254
  char dirname[LIBUSB_PATH_MAX];
255
 
256
  struct usb_device *devices;
257
  unsigned long location;
258
};
259
 
260
/* Version information, Windows specific */
261
struct usb_version {
262
  struct {
263
    int major;
264
    int minor;
265
    int micro;
266
    int nano;
267
  } dll;
268
  struct {
269
    int major;
270
    int minor;
271
    int micro;
272
    int nano;
273
  } driver;
274
};
275
 
276
 
277
struct usb_dev_handle;
278
typedef struct usb_dev_handle usb_dev_handle;
279
 
280
/* Variables */
281
extern struct usb_bus *usb_busses;
282
 
283
 
284
#include <poppack.h>
285
 
286
 
287
#ifdef __cplusplus
288
extern "C" {
289
#endif
290
 
291
  /* Function prototypes */
292
 
293
  /* usb.c */
294
  usb_dev_handle *usb_open(struct usb_device *dev);
295
  int usb_close(usb_dev_handle *dev);
296
  int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf,
297
                     size_t buflen);
298
  int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf,
299
                            size_t buflen);
300
 
301
  /* descriptors.c */
302
  int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep,
303
                                     unsigned char type, unsigned char index,
304
                                     void *buf, int size);
305
  int usb_get_descriptor(usb_dev_handle *udev, unsigned char type,
306
                         unsigned char index, void *buf, int size);
307
 
308
  /* <arch>.c */
309
  int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size,
310
                     int timeout);
311
  int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,
312
                    int timeout);
313
  int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size,
314
                          int timeout);
315
  int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size,
316
                         int timeout);
317
  int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
318
                      int value, int index, char *bytes, int size,
319
                      int timeout);
320
  int usb_set_configuration(usb_dev_handle *dev, int configuration);
321
  int usb_claim_interface(usb_dev_handle *dev, int interface);
322
  int usb_release_interface(usb_dev_handle *dev, int interface);
323
  int usb_set_altinterface(usb_dev_handle *dev, int alternate);
324
  int usb_resetep(usb_dev_handle *dev, unsigned int ep);
325
  int usb_clear_halt(usb_dev_handle *dev, unsigned int ep);
326
  int usb_reset(usb_dev_handle *dev);
327
 
328
  char *usb_strerror(void);
329
 
330
  void usb_init(void);
331
  void usb_set_debug(int level);
332
  int usb_find_busses(void);
333
  int usb_find_devices(void);
334
  struct usb_device *usb_device(usb_dev_handle *dev);
335
  struct usb_bus *usb_get_busses(void);
336
 
337
 
338
  /* Windows specific functions */
339
 
340
  const struct usb_version *usb_get_version(void);
341
 
342
  int usb_isochronous_setup_async(usb_dev_handle *dev, void **context,
343
                                  unsigned char ep, int pktsize);
344
  int usb_bulk_setup_async(usb_dev_handle *dev, void **context,
345
                           unsigned char ep);
346
  int usb_interrupt_setup_async(usb_dev_handle *dev, void **context,
347
                                unsigned char ep);
348
 
349
  int usb_submit_async(void *context, char *bytes, int size);
350
  int usb_reap_async(void *context, int timeout);
351
  int usb_free_async(void **context);
352
 
353
 
354
#ifdef __cplusplus
355
}
356
#endif
357
 
358
#endif /* __USB_H__ */
359