Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
891 theseven 1
#ifndef __PROTOCOL_USB_USB_H__
2
#define __PROTOCOL_USB_USB_H__
3
 
892 theseven 4
#include "../global.h"
891 theseven 5
 
949 theseven 6
#ifndef IN_APPLICATION_CODE
7
#if USB_ENDPOINTS & 0b00000000000000000000000000000010
8
#define USBDEBUG_ENDPOINT_OUT 1
9
#elif USB_ENDPOINTS & 0b00000000000000000000000000000010
10
#define USBDEBUG_ENDPOINT_OUT 2
11
#elif USB_ENDPOINTS & 0b00000000000000000000000000000100
12
#define USBDEBUG_ENDPOINT_OUT 3
13
#elif USB_ENDPOINTS & 0b00000000000000000000000000001000
14
#define USBDEBUG_ENDPOINT_OUT 4
15
#elif USB_ENDPOINTS & 0b00000000000000000000000000010000
16
#define USBDEBUG_ENDPOINT_OUT 5
17
#elif USB_ENDPOINTS & 0b00000000000000000000000000100000
18
#define USBDEBUG_ENDPOINT_OUT 6
19
#elif USB_ENDPOINTS & 0b00000000000000000000000001000000
20
#define USBDEBUG_ENDPOINT_OUT 7
21
#elif USB_ENDPOINTS & 0b00000000000000000000000010000000
22
#define USBDEBUG_ENDPOINT_OUT 8
23
#elif USB_ENDPOINTS & 0b00000000000000000000001000000000
24
#define USBDEBUG_ENDPOINT_OUT 9
25
#elif USB_ENDPOINTS & 0b00000000000000000000010000000000
26
#define USBDEBUG_ENDPOINT_OUT 10
27
#elif USB_ENDPOINTS & 0b00000000000000000000100000000000
28
#define USBDEBUG_ENDPOINT_OUT 11
29
#elif USB_ENDPOINTS & 0b00000000000000000001000000000000
30
#define USBDEBUG_ENDPOINT_OUT 12
31
#elif USB_ENDPOINTS & 0b00000000000000000010000000000000
32
#define USBDEBUG_ENDPOINT_OUT 13
33
#elif USB_ENDPOINTS & 0b00000000000000000100000000000000
34
#define USBDEBUG_ENDPOINT_OUT 14
35
#elif USB_ENDPOINTS & 0b00000000000000001000000000000000
36
#define USBDEBUG_ENDPOINT_OUT 15
37
#else
38
#error USBDEBUG: No OUT endpoints available!
39
#endif
40
#if USB_ENDPOINTS & 0b00000000000000100000000000000000
41
#define USBDEBUG_ENDPOINT_IN 1
42
#elif USB_ENDPOINTS & 0b00000000000000100000000000000000
43
#define USBDEBUG_ENDPOINT_IN 2
44
#elif USB_ENDPOINTS & 0b00000000000001000000000000000000
45
#define USBDEBUG_ENDPOINT_IN 3
46
#elif USB_ENDPOINTS & 0b00000000000010000000000000000000
47
#define USBDEBUG_ENDPOINT_IN 4
48
#elif USB_ENDPOINTS & 0b00000000000100000000000000000000
49
#define USBDEBUG_ENDPOINT_IN 5
50
#elif USB_ENDPOINTS & 0b00000000001000000000000000000000
51
#define USBDEBUG_ENDPOINT_IN 6
52
#elif USB_ENDPOINTS & 0b00000000010000000000000000000000
53
#define USBDEBUG_ENDPOINT_IN 7
54
#elif USB_ENDPOINTS & 0b00000000100000000000000000000000
55
#define USBDEBUG_ENDPOINT_IN 8
56
#elif USB_ENDPOINTS & 0b00000010000000000000000000000000
57
#define USBDEBUG_ENDPOINT_IN 9
58
#elif USB_ENDPOINTS & 0b00000100000000000000000000000000
59
#define USBDEBUG_ENDPOINT_IN 10
60
#elif USB_ENDPOINTS & 0b00001000000000000000000000000000
61
#define USBDEBUG_ENDPOINT_IN 11
62
#elif USB_ENDPOINTS & 0b00010000000000000000000000000000
63
#define USBDEBUG_ENDPOINT_IN 12
64
#elif USB_ENDPOINTS & 0b00100000000000000000000000000000
65
#define USBDEBUG_ENDPOINT_IN 13
66
#elif USB_ENDPOINTS & 0b01000000000000000000000000000000
67
#define USBDEBUG_ENDPOINT_IN 14
68
#elif USB_ENDPOINTS & 0b10000000000000000000000000000000
69
#define USBDEBUG_ENDPOINT_IN 15
70
#else
71
#error USBDEBUG: No IN endpoints available!
72
#endif
73
#endif
74
 
891 theseven 75
struct usb_instance;
76
 
77
union __attribute__((packed,aligned(4))) usb_ep0_buffer
78
{
79
    struct __attribute__((packed,aligned(4)))
80
    {
81
        struct __attribute__((packed))
82
        {
83
            enum
84
            {
85
                USB_SETUP_BMREQUESTTYPE_RECIPIENT_DEVICE = 0,
86
                USB_SETUP_BMREQUESTTYPE_RECIPIENT_INTERFACE = 1,
87
                USB_SETUP_BMREQUESTTYPE_RECIPIENT_ENDPOINT = 2,
88
                USB_SETUP_BMREQUESTTYPE_RECIPIENT_OTHER = 3,
89
            } recipient : 5;
90
            enum
91
            {
92
                USB_SETUP_BMREQUESTTYPE_TYPE_STANDARD = 0,
93
                USB_SETUP_BMREQUESTTYPE_TYPE_CLASS = 1,
94
                USB_SETUP_BMREQUESTTYPE_TYPE_VENDOR = 2,
95
            } type : 2;
96
            enum
97
            {
98
                USB_SETUP_BMREQUESTTYPE_DIRECTION_OUT = 0,
99
                USB_SETUP_BMREQUESTTYPE_DIRECTION_IN = 1,
100
            } direction : 1;
101
        } bmRequestType;
102
        union
103
        {
104
            enum __attribute__((packed))
105
            {
106
                USB_SETUP_BREQUEST_GET_STATUS = 0,
107
                USB_SETUP_BREQUEST_CLEAR_FEATURE = 1,
108
                USB_SETUP_BREQUEST_SET_FEATURE = 3,
109
                USB_SETUP_BREQUEST_SET_ADDRESS = 5,
110
                USB_SETUP_BREQUEST_GET_DESCRIPTOR = 6,
111
                USB_SETUP_BREQUEST_SET_DESCRIPTOR = 7,
112
                USB_SETUP_BREQUEST_GET_CONFIGURATION = 8,
113
                USB_SETUP_BREQUEST_SET_CONFIGURATION = 9,
114
                USB_SETUP_BREQUEST_GET_INTERFACE = 10,
115
                USB_SETUP_BREQUEST_SET_INTERFACE = 11,
116
                USB_SETUP_BREQUEST_SYNCH_FRAME = 12,
117
            } req;
118
            uint8_t raw;
119
        } bRequest;
120
        uint16_t wValue;
121
        uint16_t wIndex;
122
        uint16_t wLength;
123
    } setup;
124
    uint8_t raw[64];
125
};
126
 
127
enum __attribute__((packed)) usb_descriptor_type
128
{
129
    USB_DESCRIPTOR_TYPE_DEVICE = 1,
130
    USB_DESCRIPTOR_TYPE_CONFIGURATION = 2,
131
    USB_DESCRIPTOR_TYPE_STRING = 3,
132
    USB_DESCRIPTOR_TYPE_INTERFACE = 4,
133
    USB_DESCRIPTOR_TYPE_ENDPOINT = 5,
134
    USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER = 6,
135
    USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIG = 7,
136
    USB_DESCRIPTOR_TYPE_INTERFACE_POWER = 8,
137
    USB_DESCRIPTOR_TYPE_OTG = 9,
138
    USB_DESCRIPTOR_TYPE_DEBUG = 10,
139
    USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION = 11,
140
};
141
 
142
enum usb_endpoint_direction
143
{
144
    USB_ENDPOINT_DIRECTION_OUT = 0,
145
    USB_ENDPOINT_DIRECTION_IN = 1,
146
};
147
 
148
union __attribute__((packed)) usb_endpoint_number
149
{
150
    struct __attribute__((packed))
151
    {
152
        int number : 4;
153
        int reserved: 3;
154
        enum usb_endpoint_direction direction : 1;
155
    };
156
    uint8_t byte;
157
};
158
 
159
enum usb_endpoint_type
160
{
161
    USB_ENDPOINT_TYPE_CONTROL = 0,
162
    USB_ENDPOINT_TYPE_ISOCHRONOUS = 1,
163
    USB_ENDPOINT_TYPE_BULK = 2,
164
    USB_ENDPOINT_TYPE_INTERRUPT = 3,
165
};
166
 
167
struct __attribute__((packed)) usb_devicedescriptor
168
{
169
    uint8_t bLength;
170
    enum usb_descriptor_type bDescriptorType;
171
    uint16_t bcdUSB;
172
    uint8_t bDeviceClass;
173
    uint8_t bDeviceSubClass;
174
    uint8_t bDeviceProtocol;
175
    uint8_t bMaxPacketSize0;
176
    uint16_t idVendor;
177
    uint16_t idProduct;
178
    uint16_t bcdDevice;
179
    uint8_t iManufacturer;
180
    uint8_t iProduct;
181
    uint8_t iSerialNumber;
182
    uint8_t bNumConfigurations;
183
};
184
 
185
struct __attribute__((packed)) usb_configurationdescriptor
186
{
187
    uint8_t bLength;
188
    enum usb_descriptor_type bDescriptorType;
189
    uint16_t wTotalLength;
190
    uint8_t bNumInterfaces;
191
    uint8_t bConfigurationValue;
192
    uint8_t iConfiguration;
193
    struct __attribute__((packed))
194
    {
195
        unsigned int reserved : 5;
196
        unsigned int remotewakeup : 1;
197
        unsigned int selfpowered : 1;
198
        unsigned int buspowered : 1;
199
    } bmAttributes;
200
    uint8_t bMaxPower;
201
};
202
 
203
struct __attribute__((packed)) usb_interfacedescriptor
204
{
205
    uint8_t bLength;
206
    enum usb_descriptor_type bDescriptorType;
207
    uint8_t bInterfaceNumber;
208
    uint8_t bAlternateSetting;
209
    uint8_t bNumEndpoints;
210
    uint8_t bInterfaceClass;
211
    uint8_t bInterfaceSubClass;
212
    uint8_t bInterfaceProtocol;
213
    uint8_t iInterface;
214
};
215
 
216
struct __attribute__((packed)) usb_endpointdescriptor
217
{
218
    uint8_t bLength;
219
    enum usb_descriptor_type bDescriptorType;
220
    union usb_endpoint_number bEndpointAddress;
221
    struct __attribute__((packed))
222
    {
223
        enum
224
        {
225
            USB_ENDPOINT_ATTRIBUTE_TYPE_CONTROL = 0,
226
            USB_ENDPOINT_ATTRIBUTE_TYPE_ISOCHRONOUS = 1,
227
            USB_ENDPOINT_ATTRIBUTE_TYPE_BULK = 2,
228
            USB_ENDPOINT_ATTRIBUTE_TYPE_INTERRUPT = 3,
229
        } type : 2;
230
        enum
231
        {
232
            USB_ENDPOINT_ATTRIBUTE_SYNCTYPE_NOSYNC = 0,
233
            USB_ENDPOINT_ATTRIBUTE_SYNCTYPE_ASYNC = 1,
234
            USB_ENDPOINT_ATTRIBUTE_SYNCTYPE_ADAPTIVE = 2,
235
            USB_ENDPOINT_ATTRIBUTE_SYNCTYPE_SYNC = 3,
236
        } synctype : 2;
237
        enum
238
        {
239
            USB_ENDPOINT_ATTRIBUTE_USAGE_DATA = 0,
240
            USB_ENDPOINT_ATTRIBUTE_USAGE_FEEDBACK = 1,
241
            USB_ENDPOINT_ATTRIBUTE_USAGE_EXPLICIT = 2,
242
        } usage : 2;
243
        unsigned int reserved : 2;
244
    } bmAttributes;
245
    uint16_t wMaxPacketSize;
246
    uint8_t bInterval;
247
};
248
 
249
struct __attribute__((packed)) usb_stringdescriptor
250
{
251
    uint8_t bLength;
252
    enum usb_descriptor_type bDescriptorType;
253
    uint16_t wString[];
254
};
255
 
256
struct __attribute__((packed,aligned(4))) usb_endpoint
257
{
258
    union usb_endpoint_number number;
259
    uint8_t reserved1;
260
    uint8_t reserved2;
261
    uint8_t reserved3;
262
    int (*ctrl_request)(const struct usb_instance* data, int interface, int endpoint, union usb_ep0_buffer* request, const void** response);
263
    void (*xfer_complete)(const struct usb_instance* data, int interface, int endpoint, int bytesleft);
264
    union __attribute__((packed))
265
    {
266
        void (*setup_received)(const struct usb_instance* data, int interface, int endpoint);
267
        void (*timeout)(const struct usb_instance* data, int interface, int endpoint, int bytesleft);
268
    };
269
};
270
 
271
struct __attribute__((packed,aligned(4))) usb_altsetting
272
{
273
    void (*set_altsetting)(const struct usb_instance* data, int interface, int altsetting);
274
    void (*unset_altsetting)(const struct usb_instance* data, int interface, int altsetting);
275
    uint8_t reserved1;
276
    uint8_t reserved2;
277
    uint8_t reserved3;
278
    uint8_t endpoint_count;
279
    const struct usb_endpoint* endpoints[];
280
};
281
 
282
struct __attribute__((packed,aligned(4))) usb_interface
283
{
284
    void (*bus_reset)(const struct usb_instance* data, int configuration, int interface, int highspeed);
285
    int (*ctrl_request)(const struct usb_instance* data, int interface, union usb_ep0_buffer* request, const void** response);
286
    uint8_t reserved1;
287
    uint8_t reserved2;
288
    uint8_t reserved3;
289
    uint8_t altsetting_count;
290
    const struct usb_altsetting* altsettings[];
291
};
292
 
293
struct __attribute__((packed,aligned(4))) usb_configuration
294
{
295
    const struct usb_configurationdescriptor* descriptor;
296
    void (*set_configuration)(const struct usb_instance* data, int configuration);
297
    void (*unset_configuration)(const struct usb_instance* data, int configuration);
298
    uint8_t reserved1;
299
    uint8_t reserved2;
300
    uint8_t reserved3;
301
    uint8_t interface_count;
302
    const struct usb_interface* interfaces[];
303
};
304
 
305
struct __attribute__((packed,aligned(4))) usb_state
306
{
944 theseven 307
    bool (*ep0_rx_callback)(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft);
308
    bool (*ep0_tx_callback)(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft);
891 theseven 309
    const void* ep0_tx_ptr;
310
    uint16_t ep0_tx_len;
311
    uint8_t current_address;
312
    uint8_t current_configuration;
313
    uint8_t interface_altsetting[];
314
};
315
 
316
struct __attribute__((packed,aligned(4))) usb_driver
317
{
318
    void (*init)(const struct usb_instance* data);
319
    void (*exit)(const struct usb_instance* data);
944 theseven 320
    void (*ep0_start_rx)(const struct usb_instance* data, bool non_setup, int len);
891 theseven 321
    void (*ep0_start_tx)(const struct usb_instance* data, const void* buf, int len);
322
    void (*start_rx)(const struct usb_instance* data, union usb_endpoint_number ep, void* buf, int size);
323
    void (*start_tx)(const struct usb_instance* data, union usb_endpoint_number ep, const void* buf, int size);
324
    int (*get_stall)(const struct usb_instance* data, union usb_endpoint_number ep);
944 theseven 325
    void (*set_stall)(const struct usb_instance* data, union usb_endpoint_number ep, bool stall);
891 theseven 326
    void (*set_address)(const struct usb_instance* data, uint8_t address);
327
    void (*configure_ep)(const struct usb_instance* data, union usb_endpoint_number ep, enum usb_endpoint_type type, int maxpacket);
328
    void (*unconfigure_ep)(const struct usb_instance* data, union usb_endpoint_number ep);
329
    int (*get_max_transfer_size)(const struct usb_instance* data, union usb_endpoint_number ep);
330
};
331
 
332
struct __attribute__((packed,aligned(4))) usb_instance
333
{
334
    const struct usb_driver* driver;
335
    const void* driver_config;
336
    void* driver_state;
337
    struct usb_state* state;
338
    union usb_ep0_buffer* buffer;
339
    void (*bus_reset)(const struct usb_instance* data, int highspeed);
340
    int (*ctrl_request)(const struct usb_instance* data, union usb_ep0_buffer* request, const void** response);
341
    int (*ep0_setup_hook)(const struct usb_instance* data, union usb_ep0_buffer* buf);
342
    uint8_t configuration_count;
343
    uint8_t stringdescriptor_count;
344
    uint8_t reserved1;
345
    uint8_t reserved2;
346
    const struct usb_devicedescriptor* devicedescriptor;
347
    const struct usb_stringdescriptor** stringdescriptors;
348
    const struct usb_configuration* configurations[];
349
};
350
 
351
extern void usb_init(const struct usb_instance* data);
352
extern void usb_exit(const struct usb_instance* data);
353
extern void usb_handle_bus_reset(const struct usb_instance* data, int highspeed);
354
extern void usb_handle_timeout(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft);
355
extern void usb_handle_xfer_complete(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft);
356
extern void usb_handle_setup_received(const struct usb_instance* data, union usb_endpoint_number epnum);
944 theseven 357
extern void usb_ep0_start_rx(const struct usb_instance* data, bool non_setup, int len, bool (*callback)(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft));
358
extern void usb_ep0_start_tx(const struct usb_instance* data, const void* buf, int len, bool (*callback)(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft));
891 theseven 359
extern void usb_start_rx(const struct usb_instance* data, union usb_endpoint_number ep, void* buf, int size);
360
extern void usb_start_tx(const struct usb_instance* data, union usb_endpoint_number ep, const void* buf, int size);
944 theseven 361
extern void usb_set_stall(const struct usb_instance* data, union usb_endpoint_number ep, bool stall);
891 theseven 362
extern void usb_configure_ep(const struct usb_instance* data, union usb_endpoint_number ep, enum usb_endpoint_type type, int maxpacket);
363
extern void usb_unconfigure_ep(const struct usb_instance* data, union usb_endpoint_number ep);
364
extern int usb_get_max_transfer_size(const struct usb_instance* data, union usb_endpoint_number ep);
944 theseven 365
extern bool usb_ep0_tx_callback(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft);
366
extern bool usb_ep0_short_tx_callback(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft);
367
extern bool usb_ep0_ack_callback(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft);
891 theseven 368
 
369
 
370
#endif