Subversion Repositories freemyipod

Rev

Rev 427 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 427 Rev 891
Line 1... Line 1...
1
//
1
#ifndef __PROTOCOL_USB_USB_H__
2
//
2
#define __PROTOCOL_USB_USB_H__
3
//    Copyright 2010 TheSeven
3
 
4
//
4
#include "global.h"
5
//
5
 
6
//    This file is part of emCORE.
6
struct usb_instance;
7
//
7
 
8
//    emCORE is free software: you can redistribute it and/or
8
union __attribute__((packed,aligned(4))) usb_ep0_buffer
9
//    modify it under the terms of the GNU General Public License as
9
{
10
//    published by the Free Software Foundation, either version 2 of the
10
    struct __attribute__((packed,aligned(4)))
11
//    License, or (at your option) any later version.
11
    {
12
//
12
        struct __attribute__((packed))
13
//    emCORE is distributed in the hope that it will be useful,
13
        {
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
            enum
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
            {
16
//    See the GNU General Public License for more details.
16
                USB_SETUP_BMREQUESTTYPE_RECIPIENT_DEVICE = 0,
17
//
17
                USB_SETUP_BMREQUESTTYPE_RECIPIENT_INTERFACE = 1,
18
//    You should have received a copy of the GNU General Public License along
18
                USB_SETUP_BMREQUESTTYPE_RECIPIENT_ENDPOINT = 2,
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
19
                USB_SETUP_BMREQUESTTYPE_RECIPIENT_OTHER = 3,
20
//
20
            } recipient : 5;
21
//
21
            enum
22
 
22
            {
23
 
23
                USB_SETUP_BMREQUESTTYPE_TYPE_STANDARD = 0,
24
#ifndef __USB_H__
24
                USB_SETUP_BMREQUESTTYPE_TYPE_CLASS = 1,
25
#define __USB_H__
25
                USB_SETUP_BMREQUESTTYPE_TYPE_VENDOR = 2,
26
 
26
            } type : 2;
27
 
27
            enum
28
#include "global.h"
28
            {
29
#include "usb_ch9.h"
29
                USB_SETUP_BMREQUESTTYPE_DIRECTION_OUT = 0,
30
 
30
                USB_SETUP_BMREQUESTTYPE_DIRECTION_IN = 1,
31
 
31
            } direction : 1;
32
void usb_handle_control_request(struct usb_ctrlrequest* req);
32
        } bmRequestType;
33
void usb_handle_transfer_complete(int endpoint, int dir, int status, int length);
33
        union
34
void usb_handle_bus_reset(void);
34
        {
35
void usb_init(void) INITCODE_ATTR;
35
            enum __attribute__((packed))
36
void usb_exit(void);
36
            {
37
 
37
                USB_SETUP_BREQUEST_GET_STATUS = 0,
38
 
38
                USB_SETUP_BREQUEST_CLEAR_FEATURE = 1,
39
#endif
39
                USB_SETUP_BREQUEST_SET_FEATURE = 3,
-
 
40
                USB_SETUP_BREQUEST_SET_ADDRESS = 5,
-
 
41
                USB_SETUP_BREQUEST_GET_DESCRIPTOR = 6,
-
 
42
                USB_SETUP_BREQUEST_SET_DESCRIPTOR = 7,
-
 
43
                USB_SETUP_BREQUEST_GET_CONFIGURATION = 8,
-
 
44
                USB_SETUP_BREQUEST_SET_CONFIGURATION = 9,
-
 
45
                USB_SETUP_BREQUEST_GET_INTERFACE = 10,
-
 
46
                USB_SETUP_BREQUEST_SET_INTERFACE = 11,
-
 
47
                USB_SETUP_BREQUEST_SYNCH_FRAME = 12,
-
 
48
            } req;
-
 
49
            uint8_t raw;
-
 
50
        } bRequest;
-
 
51
        uint16_t wValue;
-
 
52
        uint16_t wIndex;
-
 
53
        uint16_t wLength;
-
 
54
    } setup;
-
 
55
    uint8_t raw[64];
-
 
56
};
-
 
57
 
-
 
58
enum __attribute__((packed)) usb_descriptor_type
-
 
59
{
-
 
60
    USB_DESCRIPTOR_TYPE_DEVICE = 1,
-
 
61
    USB_DESCRIPTOR_TYPE_CONFIGURATION = 2,
-
 
62
    USB_DESCRIPTOR_TYPE_STRING = 3,
-
 
63
    USB_DESCRIPTOR_TYPE_INTERFACE = 4,
-
 
64
    USB_DESCRIPTOR_TYPE_ENDPOINT = 5,
-
 
65
    USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER = 6,
-
 
66
    USB_DESCRIPTOR_TYPE_OTHER_SPEED_CONFIG = 7,
-
 
67
    USB_DESCRIPTOR_TYPE_INTERFACE_POWER = 8,
-
 
68
    USB_DESCRIPTOR_TYPE_OTG = 9,
-
 
69
    USB_DESCRIPTOR_TYPE_DEBUG = 10,
-
 
70
    USB_DESCRIPTOR_TYPE_INTERFACE_ASSOCIATION = 11,
-
 
71
};
-
 
72
 
-
 
73
enum usb_endpoint_direction
-
 
74
{
-
 
75
    USB_ENDPOINT_DIRECTION_OUT = 0,
-
 
76
    USB_ENDPOINT_DIRECTION_IN = 1,
-
 
77
};
-
 
78
 
-
 
79
union __attribute__((packed)) usb_endpoint_number
-
 
80
{
-
 
81
    struct __attribute__((packed))
-
 
82
    {
-
 
83
        int number : 4;
-
 
84
        int reserved: 3;
-
 
85
        enum usb_endpoint_direction direction : 1;
-
 
86
    };
-
 
87
    uint8_t byte;
-
 
88
};
-
 
89
 
-
 
90
enum usb_endpoint_type
-
 
91
{
-
 
92
    USB_ENDPOINT_TYPE_CONTROL = 0,
-
 
93
    USB_ENDPOINT_TYPE_ISOCHRONOUS = 1,
-
 
94
    USB_ENDPOINT_TYPE_BULK = 2,
-
 
95
    USB_ENDPOINT_TYPE_INTERRUPT = 3,
-
 
96
};
-
 
97
 
-
 
98
struct __attribute__((packed)) usb_devicedescriptor
-
 
99
{
-
 
100
    uint8_t bLength;
-
 
101
    enum usb_descriptor_type bDescriptorType;
-
 
102
    uint16_t bcdUSB;
-
 
103
    uint8_t bDeviceClass;
-
 
104
    uint8_t bDeviceSubClass;
-
 
105
    uint8_t bDeviceProtocol;
-
 
106
    uint8_t bMaxPacketSize0;
-
 
107
    uint16_t idVendor;
-
 
108
    uint16_t idProduct;
-
 
109
    uint16_t bcdDevice;
-
 
110
    uint8_t iManufacturer;
-
 
111
    uint8_t iProduct;
-
 
112
    uint8_t iSerialNumber;
-
 
113
    uint8_t bNumConfigurations;
-
 
114
};
-
 
115
 
-
 
116
struct __attribute__((packed)) usb_configurationdescriptor
-
 
117
{
-
 
118
    uint8_t bLength;
-
 
119
    enum usb_descriptor_type bDescriptorType;
-
 
120
    uint16_t wTotalLength;
-
 
121
    uint8_t bNumInterfaces;
-
 
122
    uint8_t bConfigurationValue;
-
 
123
    uint8_t iConfiguration;
-
 
124
    struct __attribute__((packed))
-
 
125
    {
-
 
126
        unsigned int reserved : 5;
-
 
127
        unsigned int remotewakeup : 1;
-
 
128
        unsigned int selfpowered : 1;
-
 
129
        unsigned int buspowered : 1;
-
 
130
    } bmAttributes;
-
 
131
    uint8_t bMaxPower;
-
 
132
};
-
 
133
 
-
 
134
struct __attribute__((packed)) usb_interfacedescriptor
-
 
135
{
-
 
136
    uint8_t bLength;
-
 
137
    enum usb_descriptor_type bDescriptorType;
-
 
138
    uint8_t bInterfaceNumber;
-
 
139
    uint8_t bAlternateSetting;
-
 
140
    uint8_t bNumEndpoints;
-
 
141
    uint8_t bInterfaceClass;
-
 
142
    uint8_t bInterfaceSubClass;
-
 
143
    uint8_t bInterfaceProtocol;
-
 
144
    uint8_t iInterface;
-
 
145
};
-
 
146
 
-
 
147
struct __attribute__((packed)) usb_endpointdescriptor
-
 
148
{
-
 
149
    uint8_t bLength;
-
 
150
    enum usb_descriptor_type bDescriptorType;
-
 
151
    union usb_endpoint_number bEndpointAddress;
-
 
152
    struct __attribute__((packed))
-
 
153
    {
-
 
154
        enum
-
 
155
        {
-
 
156
            USB_ENDPOINT_ATTRIBUTE_TYPE_CONTROL = 0,
-
 
157
            USB_ENDPOINT_ATTRIBUTE_TYPE_ISOCHRONOUS = 1,
-
 
158
            USB_ENDPOINT_ATTRIBUTE_TYPE_BULK = 2,
-
 
159
            USB_ENDPOINT_ATTRIBUTE_TYPE_INTERRUPT = 3,
-
 
160
        } type : 2;
-
 
161
        enum
-
 
162
        {
-
 
163
            USB_ENDPOINT_ATTRIBUTE_SYNCTYPE_NOSYNC = 0,
-
 
164
            USB_ENDPOINT_ATTRIBUTE_SYNCTYPE_ASYNC = 1,
-
 
165
            USB_ENDPOINT_ATTRIBUTE_SYNCTYPE_ADAPTIVE = 2,
-
 
166
            USB_ENDPOINT_ATTRIBUTE_SYNCTYPE_SYNC = 3,
-
 
167
        } synctype : 2;
-
 
168
        enum
-
 
169
        {
-
 
170
            USB_ENDPOINT_ATTRIBUTE_USAGE_DATA = 0,
-
 
171
            USB_ENDPOINT_ATTRIBUTE_USAGE_FEEDBACK = 1,
-
 
172
            USB_ENDPOINT_ATTRIBUTE_USAGE_EXPLICIT = 2,
-
 
173
        } usage : 2;
-
 
174
        unsigned int reserved : 2;
-
 
175
    } bmAttributes;
-
 
176
    uint16_t wMaxPacketSize;
-
 
177
    uint8_t bInterval;
-
 
178
};
-
 
179
 
-
 
180
struct __attribute__((packed)) usb_stringdescriptor
-
 
181
{
-
 
182
    uint8_t bLength;
-
 
183
    enum usb_descriptor_type bDescriptorType;
-
 
184
    uint16_t wString[];
-
 
185
};
-
 
186
 
-
 
187
struct __attribute__((packed,aligned(4))) usb_endpoint
-
 
188
{
-
 
189
    union usb_endpoint_number number;
-
 
190
    uint8_t reserved1;
-
 
191
    uint8_t reserved2;
-
 
192
    uint8_t reserved3;
-
 
193
    int (*ctrl_request)(const struct usb_instance* data, int interface, int endpoint, union usb_ep0_buffer* request, const void** response);
-
 
194
    void (*xfer_complete)(const struct usb_instance* data, int interface, int endpoint, int bytesleft);
-
 
195
    union __attribute__((packed))
-
 
196
    {
-
 
197
        void (*setup_received)(const struct usb_instance* data, int interface, int endpoint);
-
 
198
        void (*timeout)(const struct usb_instance* data, int interface, int endpoint, int bytesleft);
-
 
199
    };
-
 
200
};
-
 
201
 
-
 
202
struct __attribute__((packed,aligned(4))) usb_altsetting
-
 
203
{
-
 
204
    void (*set_altsetting)(const struct usb_instance* data, int interface, int altsetting);
-
 
205
    void (*unset_altsetting)(const struct usb_instance* data, int interface, int altsetting);
-
 
206
    uint8_t reserved1;
-
 
207
    uint8_t reserved2;
-
 
208
    uint8_t reserved3;
-
 
209
    uint8_t endpoint_count;
-
 
210
    const struct usb_endpoint* endpoints[];
-
 
211
};
-
 
212
 
-
 
213
struct __attribute__((packed,aligned(4))) usb_interface
-
 
214
{
-
 
215
    void (*bus_reset)(const struct usb_instance* data, int configuration, int interface, int highspeed);
-
 
216
    int (*ctrl_request)(const struct usb_instance* data, int interface, union usb_ep0_buffer* request, const void** response);
-
 
217
    uint8_t reserved1;
-
 
218
    uint8_t reserved2;
-
 
219
    uint8_t reserved3;
-
 
220
    uint8_t altsetting_count;
-
 
221
    const struct usb_altsetting* altsettings[];
-
 
222
};
-
 
223
 
-
 
224
struct __attribute__((packed,aligned(4))) usb_configuration
-
 
225
{
-
 
226
    const struct usb_configurationdescriptor* descriptor;
-
 
227
    void (*set_configuration)(const struct usb_instance* data, int configuration);
-
 
228
    void (*unset_configuration)(const struct usb_instance* data, int configuration);
-
 
229
    uint8_t reserved1;
-
 
230
    uint8_t reserved2;
-
 
231
    uint8_t reserved3;
-
 
232
    uint8_t interface_count;
-
 
233
    const struct usb_interface* interfaces[];
-
 
234
};
-
 
235
 
-
 
236
struct __attribute__((packed,aligned(4))) usb_state
-
 
237
{
-
 
238
    bool (*ep0_rx_callback)(const struct usb_instance* data, int bytesleft);
-
 
239
    bool (*ep0_tx_callback)(const struct usb_instance* data, int bytesleft);
-
 
240
    const void* ep0_tx_ptr;
-
 
241
    uint16_t ep0_tx_len;
-
 
242
    uint8_t current_address;
-
 
243
    uint8_t current_configuration;
-
 
244
    uint8_t interface_altsetting[];
-
 
245
};
-
 
246
 
-
 
247
struct __attribute__((packed,aligned(4))) usb_driver
-
 
248
{
-
 
249
    void (*init)(const struct usb_instance* data);
-
 
250
    void (*exit)(const struct usb_instance* data);
-
 
251
    void (*ep0_start_rx)(const struct usb_instance* data, int non_setup);
-
 
252
    void (*ep0_start_tx)(const struct usb_instance* data, const void* buf, int len);
-
 
253
    void (*start_rx)(const struct usb_instance* data, union usb_endpoint_number ep, void* buf, int size);
-
 
254
    void (*start_tx)(const struct usb_instance* data, union usb_endpoint_number ep, const void* buf, int size);
-
 
255
    int (*get_stall)(const struct usb_instance* data, union usb_endpoint_number ep);
-
 
256
    void (*set_stall)(const struct usb_instance* data, union usb_endpoint_number ep, int stall);
-
 
257
    void (*set_address)(const struct usb_instance* data, uint8_t address);
-
 
258
    void (*configure_ep)(const struct usb_instance* data, union usb_endpoint_number ep, enum usb_endpoint_type type, int maxpacket);
-
 
259
    void (*unconfigure_ep)(const struct usb_instance* data, union usb_endpoint_number ep);
-
 
260
    int (*get_max_transfer_size)(const struct usb_instance* data, union usb_endpoint_number ep);
-
 
261
};
-
 
262
 
-
 
263
struct __attribute__((packed,aligned(4))) usb_instance
-
 
264
{
-
 
265
    const struct usb_driver* driver;
-
 
266
    const void* driver_config;
-
 
267
    void* driver_state;
-
 
268
    struct usb_state* state;
-
 
269
    union usb_ep0_buffer* buffer;
-
 
270
    void (*bus_reset)(const struct usb_instance* data, int highspeed);
-
 
271
    int (*ctrl_request)(const struct usb_instance* data, union usb_ep0_buffer* request, const void** response);
-
 
272
    int (*ep0_setup_hook)(const struct usb_instance* data, union usb_ep0_buffer* buf);
-
 
273
    uint8_t configuration_count;
-
 
274
    uint8_t stringdescriptor_count;
-
 
275
    uint8_t reserved1;
-
 
276
    uint8_t reserved2;
-
 
277
    const struct usb_devicedescriptor* devicedescriptor;
-
 
278
    const struct usb_stringdescriptor** stringdescriptors;
-
 
279
    const struct usb_configuration* configurations[];
-
 
280
};
-
 
281
 
-
 
282
extern void usb_init(const struct usb_instance* data);
-
 
283
extern void usb_exit(const struct usb_instance* data);
-
 
284
extern void usb_handle_bus_reset(const struct usb_instance* data, int highspeed);
-
 
285
extern void usb_handle_timeout(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft);
-
 
286
extern void usb_handle_xfer_complete(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft);
-
 
287
extern void usb_handle_setup_received(const struct usb_instance* data, union usb_endpoint_number epnum);
-
 
288
extern void usb_ep0_start_rx(const struct usb_instance* data, int non_setup, bool (*callback)(const struct usb_instance* data, int bytesleft));
-
 
289
extern void usb_ep0_start_tx(const struct usb_instance* data, const void* buf, int len, bool last, bool (*callback)(const struct usb_instance* data, int bytesleft));
-
 
290
extern void usb_ep0_expect_setup(const struct usb_instance* data);
-
 
291
extern void usb_start_rx(const struct usb_instance* data, union usb_endpoint_number ep, void* buf, int size);
-
 
292
extern void usb_start_tx(const struct usb_instance* data, union usb_endpoint_number ep, const void* buf, int size);
-
 
293
extern void usb_set_stall(const struct usb_instance* data, union usb_endpoint_number ep, int stall);
-
 
294
extern void usb_configure_ep(const struct usb_instance* data, union usb_endpoint_number ep, enum usb_endpoint_type type, int maxpacket);
-
 
295
extern void usb_unconfigure_ep(const struct usb_instance* data, union usb_endpoint_number ep);
-
 
296
extern int usb_get_max_transfer_size(const struct usb_instance* data, union usb_endpoint_number ep);
-
 
297
extern bool usb_ep0_tx_callback(const struct usb_instance* data, int bytesleft);
-
 
298
 
-
 
299
 
-
 
300
#endif