Subversion Repositories freemyipod

Rev

Rev 889 | Rev 945 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
881 theseven 1
#include "global.h"
2
#include "protocol/usb/usb.h"
889 theseven 3
#include "sys/util.h"
881 theseven 4
 
5
void usb_start_rx(const struct usb_instance* data, union usb_endpoint_number ep, void* buf, int size)
6
{
7
    data->driver->start_rx(data, ep, buf, size);
8
}
9
 
10
void usb_start_tx(const struct usb_instance* data, union usb_endpoint_number ep, const void* buf, int size)
11
{
12
    data->driver->start_tx(data, ep, buf, size);
13
}
14
 
15
void usb_set_stall(const struct usb_instance* data, union usb_endpoint_number ep, int stall)
16
{
17
    data->driver->set_stall(data, ep, stall);
18
}
19
 
889 theseven 20
void usb_ep0_start_rx(const struct usb_instance* data, int non_setup, bool (*callback)(const struct usb_instance* data, int bytesleft))
881 theseven 21
{
889 theseven 22
    data->state->ep0_rx_callback = callback;
881 theseven 23
    data->driver->ep0_start_rx(data, non_setup);
24
}
25
 
889 theseven 26
bool usb_ep0_rx_callback(const struct usb_instance* data, int bytesleft)
881 theseven 27
{
889 theseven 28
    usb_ep0_start_rx(data, 0, NULL);
29
    return true;
30
}
31
 
32
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))
33
{
881 theseven 34
    // Expect zero-length ACK if we are about to actually send data, otherwise expect SETUP.
889 theseven 35
    if (last) usb_ep0_start_rx(data, !!len, usb_ep0_rx_callback);
881 theseven 36
 
890 theseven 37
    if (((uint32_t)buf) & (CACHEALIGN_SIZE - 1))
38
    {
39
        memcpy(data->buffer->raw, buf, len);
40
        buf = data->buffer->raw;
41
    }
42
 
889 theseven 43
    data->state->ep0_tx_callback = callback;
881 theseven 44
    data->driver->ep0_start_tx(data, buf, len);
45
}
46
 
889 theseven 47
bool usb_ep0_tx_callback(const struct usb_instance* data, int bytesleft)
881 theseven 48
{
889 theseven 49
    if (bytesleft || !data->state->ep0_tx_len) return false;
50
    int len = MIN(64, data->state->ep0_tx_len);
51
    data->state->ep0_tx_ptr += 64;
52
    data->state->ep0_tx_len -= len;
890 theseven 53
    usb_ep0_start_tx(data, data->state->ep0_tx_ptr, len, !data->state->ep0_tx_len, usb_ep0_tx_callback);
889 theseven 54
    return true;
881 theseven 55
}
56
 
57
void usb_unconfigure_ep(const struct usb_instance* data, union usb_endpoint_number ep)
58
{
59
    data->driver->unconfigure_ep(data, ep);
60
}
61
 
62
void usb_configure_ep(const struct usb_instance* data, union usb_endpoint_number ep, enum usb_endpoint_type type, int maxpacket)
63
{
64
    // Reset the endpoint, just in case someone left it in a dirty state.
65
    usb_unconfigure_ep(data, ep);
66
 
67
    // Write the new configuration for the endpoint.
68
    // This resets the data toggle to DATA0, as required by the USB specification.
69
    data->driver->configure_ep(data, ep, type, maxpacket);
70
}
71
 
72
int usb_get_max_transfer_size(const struct usb_instance* data, union usb_endpoint_number ep)
73
{
74
    return data->driver->get_max_transfer_size(data, ep);
75
}
76
 
77
static void usb_unconfigure(const struct usb_instance* data)
78
{
79
    // Notify a configuration and its active interface altsettings that it was just kicked out.
80
    int configid = data->state->current_configuration;
81
    if (!configid) return;
82
    const struct usb_configuration* configuration = data->configurations[configid - 1];
83
    int i;
84
    for (i = 0; i < configuration->interface_count; i++)
85
    {
889 theseven 86
        const struct usb_interface* interface = configuration->interfaces[i];
87
        const struct usb_altsetting* altsetting = interface->altsettings[data->state->interface_altsetting[i]];
881 theseven 88
        if (altsetting->unset_altsetting)
889 theseven 89
            altsetting->unset_altsetting(data, i, data->state->interface_altsetting[i]);
881 theseven 90
    }
91
    if (configuration->unset_configuration)
92
        configuration->unset_configuration(data, configid);
93
    data->state->current_configuration = 0;
94
}
95
 
96
static const struct usb_endpoint* usb_find_endpoint(const struct usb_instance* data,
97
                                                    union usb_endpoint_number ep, int* ifidx, int* epidx)
98
{
99
    // Figure out who's currently owning a (hardware) endpoint.
100
    // Returns a pointer to the usb_endpoint struct, and sets *epidx to its index within its altsetting.
101
    // *ifidx will be set to the interface number within the current configuration that the altsetting belongs to.
102
    // If nobody currently owns the specified endpoint, return NULL (*epidx and *ifidx will contain garbage).
103
    int configid = data->state->current_configuration;
104
    if (!configid) return NULL;
105
    const struct usb_configuration* configuration = data->configurations[configid - 1];
106
    for (*ifidx = 0; *ifidx < configuration->interface_count; (*ifidx)++)
107
    {
108
        const struct usb_interface* interface = configuration->interfaces[*ifidx];
889 theseven 109
        const struct usb_altsetting* altsetting = interface->altsettings[data->state->interface_altsetting[*ifidx]];
881 theseven 110
        for (*epidx = 0; *epidx < altsetting->endpoint_count; (*epidx)++)
111
        {
112
            const struct usb_endpoint* endpoint = altsetting->endpoints[*epidx];
113
            if (endpoint->number.byte == ep.byte) return endpoint;
114
        }
115
    }
116
    return NULL;
117
}
118
 
119
static void usb_handle_ep0_setup(const struct usb_instance* data, union usb_ep0_buffer* buffer)
120
{
121
    // size < -2: do nothing at all (dangerous, you need to take care of priming EP0 yourself!)
122
    // size == -2: send STALL
123
    // size == -1: try to run default handler, or send STALL if none exists
124
    // size == 0: send ACK
125
    // size > 0: send <size> bytes at <addr>, then expect ACK
889 theseven 126
    const void* addr = data->buffer;
881 theseven 127
    int size = -1;
128
    switch (buffer->setup.bmRequestType.recipient)
129
    {
130
    case USB_SETUP_BMREQUESTTYPE_RECIPIENT_DEVICE:
131
        if (data->ctrl_request) size = data->ctrl_request(data, buffer, &addr);
132
        if (size != -1) break;
133
        switch (buffer->setup.bmRequestType.type)
134
        {
135
        case USB_SETUP_BMREQUESTTYPE_TYPE_STANDARD:
136
            switch (buffer->setup.bRequest.req)
137
            {
138
            case USB_SETUP_BREQUEST_GET_STATUS:
139
                if (buffer->setup.wLength != 2 || buffer->setup.wIndex
140
                 || !data->state->current_address || buffer->setup.wValue) break;
141
                data->buffer->raw[0] = 0;
142
                data->buffer->raw[1] = 1;
143
                size = 2;
144
                break;
145
            case USB_SETUP_BREQUEST_SET_ADDRESS:
146
                if (buffer->setup.wLength || buffer->setup.wIndex || buffer->setup.wValue > 127) break;
147
                data->state->current_address = buffer->setup.wValue;
148
                // We can already set the address here, the driver has to take care to send the ACK with the old address.
149
                data->driver->set_address(data, data->state->current_address);
150
                size = 0;
151
                break;
152
            case USB_SETUP_BREQUEST_GET_DESCRIPTOR:
153
                if (!buffer->setup.wLength) break;
154
                switch (buffer->setup.wValue >> 8)
155
                {
156
                case USB_DESCRIPTOR_TYPE_DEVICE:
157
                    if ((buffer->setup.wValue & 0xff) || buffer->setup.wIndex) break;
158
                    addr = data->devicedescriptor;
159
                    size = data->devicedescriptor->bLength;
160
                    break;
161
                case USB_DESCRIPTOR_TYPE_CONFIGURATION:
162
                    if (buffer->setup.wIndex
163
                     || (buffer->setup.wValue & 0xff) >= data->configuration_count) break;
164
                    addr = data->configurations[buffer->setup.wValue & 0xff]->descriptor;
165
                    size = data->configurations[buffer->setup.wValue & 0xff]->descriptor->wTotalLength;
166
                    break;
167
                case USB_DESCRIPTOR_TYPE_STRING:
168
                    if ((buffer->setup.wValue & 0xff) > data->stringdescriptor_count) break;
169
                    addr = data->stringdescriptors[buffer->setup.wValue & 0xff];
170
                    size = data->stringdescriptors[buffer->setup.wValue & 0xff]->bLength;
171
                    break;
172
                }
173
                if (size > buffer->setup.wLength) size = buffer->setup.wLength;
174
                break;
175
            case USB_SETUP_BREQUEST_GET_CONFIGURATION:
176
                if (buffer->setup.wLength != 1 || buffer->setup.wIndex
177
                 || !data->state->current_address || buffer->setup.wValue) break;
178
                data->buffer->raw[0] = data->state->current_configuration;
179
                size = 1;
180
                break;
181
            case USB_SETUP_BREQUEST_SET_CONFIGURATION:
182
                if (buffer->setup.wLength || buffer->setup.wIndex || !data->state->current_address
183
                 || buffer->setup.wValue > data->configuration_count) break;
184
                usb_unconfigure(data);
185
                data->state->current_configuration = buffer->setup.wValue;
186
                if (data->state->current_configuration)
187
                {
188
                    // Notify the configuration and its interface default altsettings that it should set up stuff
189
                    int configid = data->state->current_configuration;
190
                    const struct usb_configuration* configuration = data->configurations[configid - 1];
191
                    if (configuration->set_configuration)
192
                        configuration->set_configuration(data, configid);
193
                    int i;
194
                    for (i = 0; i < configuration->interface_count; i++)
195
                    {
889 theseven 196
                        const struct usb_interface* interface = configuration->interfaces[i];
197
                        data->state->interface_altsetting[i] = 0;
881 theseven 198
                        const struct usb_altsetting* altsetting = interface->altsettings[0];
199
                        if (altsetting->set_altsetting) altsetting->set_altsetting(data, i, 0);
200
                    }
201
                }
202
                size = 0;
203
                break;
204
            default: break;
205
            }
206
            break;
207
        }
208
        break;
209
    case USB_SETUP_BMREQUESTTYPE_RECIPIENT_INTERFACE:
210
    {
211
        if (!data->state->current_configuration) break;
212
        int configid = data->state->current_configuration;
213
        const struct usb_configuration* configuration = data->configurations[configid - 1];
214
        int intfid = buffer->setup.wIndex;
889 theseven 215
        if (intfid >= configuration->interface_count) break;
216
        const struct usb_interface* interface = configuration->interfaces[intfid];
881 theseven 217
        if (interface->ctrl_request) size = interface->ctrl_request(data, intfid, buffer, &addr);
218
        if (size != -1) break;
219
        switch (buffer->setup.bmRequestType.type)
220
        {
221
        case USB_SETUP_BMREQUESTTYPE_TYPE_STANDARD:
222
            switch (buffer->setup.bRequest.req)
223
            {
224
            case USB_SETUP_BREQUEST_GET_STATUS:
225
                if (buffer->setup.wLength != 2 || buffer->setup.wValue) break;
226
                data->buffer->raw[0] = 0;
227
                data->buffer->raw[1] = 0;
228
                size = 2;
229
                break;
230
            case USB_SETUP_BREQUEST_GET_INTERFACE:
231
                if (buffer->setup.wLength != 1 || buffer->setup.wValue) break;
889 theseven 232
                data->buffer->raw[0] = data->state->interface_altsetting[intfid];
881 theseven 233
                size = 1;
234
                break;
235
            case USB_SETUP_BREQUEST_SET_INTERFACE:
236
            {
237
                if (buffer->setup.wLength
238
                 || buffer->setup.wValue > interface->altsetting_count) break;
889 theseven 239
                const struct usb_altsetting* altsetting = interface->altsettings[data->state->interface_altsetting[intfid]];
881 theseven 240
                if (altsetting->unset_altsetting)
889 theseven 241
                    altsetting->unset_altsetting(data, intfid, data->state->interface_altsetting[intfid]);
242
                data->state->interface_altsetting[intfid] = buffer->setup.wValue;
243
                altsetting = interface->altsettings[data->state->interface_altsetting[intfid]];
881 theseven 244
                if (altsetting->set_altsetting)
889 theseven 245
                    altsetting->set_altsetting(data, intfid, data->state->interface_altsetting[intfid]);
881 theseven 246
                break;
247
            }
248
            default: break;
249
            }
250
            break;
251
            default: break;
252
        }
253
        break;
254
        case USB_SETUP_BMREQUESTTYPE_RECIPIENT_ENDPOINT:
255
        {
256
            if (!data->state->current_configuration) break;
257
            union usb_endpoint_number ep = { .byte = buffer->setup.wIndex };
258
            int intfid;
259
            int epid;
260
            const struct usb_endpoint* endpoint = usb_find_endpoint(data, ep, &intfid, &epid);
261
            if (!endpoint) break;
262
            if (endpoint->ctrl_request) size = endpoint->ctrl_request(data, intfid, epid, buffer, &addr);
263
            if (size != -1) break;
264
            switch (buffer->setup.bmRequestType.type)
265
            {
266
            case USB_SETUP_BMREQUESTTYPE_TYPE_STANDARD:
267
                switch (buffer->setup.bRequest.req)
268
                {
269
                case USB_SETUP_BREQUEST_GET_STATUS:
270
                    if (buffer->setup.wLength != 2 || buffer->setup.wValue) break;
271
                    data->buffer->raw[0] = 0;
272
                    data->buffer->raw[1] = data->driver->get_stall(data, ep);
273
                    addr = data->buffer;
274
                    size = 2;
275
                    break;
276
                case USB_SETUP_BREQUEST_CLEAR_FEATURE:
277
                    if (buffer->setup.wLength || buffer->setup.wValue) break;
278
                    data->driver->set_stall(data, ep, false);
279
                    size = 0;
280
                    break;
281
                case USB_SETUP_BREQUEST_SET_FEATURE:
282
                    if (buffer->setup.wLength || buffer->setup.wValue) break;
283
                    data->driver->set_stall(data, ep, true);
284
                    size = 0;
285
                    break;
286
                default: break;
287
                }
288
                break;
289
                default: break;
290
            }
291
            break;
292
        }
293
        default: break;
294
    }
295
    }
296
    // See comment at the top of this function
889 theseven 297
    if (size == 0) usb_ep0_start_tx(data, NULL, 0, true, NULL);
298
    else if (size > 0)
299
    {
300
        usb_ep0_start_rx(data, 0, NULL);
301
        int len = MIN(64, size);
302
        data->state->ep0_tx_ptr = addr;
303
        data->state->ep0_tx_len = size - len;
304
        usb_ep0_start_tx(data, addr, len, !data->state->ep0_tx_len, usb_ep0_tx_callback);
305
    }
306
    else if (size >= -2)
307
    {
308
        union usb_endpoint_number ep = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_IN };
309
        usb_set_stall(data, ep, 1);
310
        ep.direction = USB_ENDPOINT_DIRECTION_OUT;
311
        usb_set_stall(data, ep, 1);
312
    }
881 theseven 313
}
314
 
315
void usb_handle_bus_reset(const struct usb_instance* data, int highspeed)
316
{
317
    data->state->current_address = 0;
318
    usb_unconfigure(data);
319
    if (data->bus_reset) data->bus_reset(data, highspeed);
320
    int c, i;
321
    for (c = 0; c < data->configuration_count; c++)
322
    {
323
        const struct usb_configuration* configuration = data->configurations[c];
324
        for (i = 0; i < configuration->interface_count; i++)
325
        {
889 theseven 326
            const struct usb_interface* interface = configuration->interfaces[i];
881 theseven 327
            if (interface->bus_reset) interface->bus_reset(data, c, i, highspeed);
328
        }
329
    }
889 theseven 330
 
331
    // Prime EP0 for the first setup packet.
332
    usb_ep0_start_rx(data, 0, NULL);
881 theseven 333
}
334
 
335
void usb_handle_timeout(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft)
336
{
889 theseven 337
    // If the host doesn't fetch an EP0 IN packet, we can't do much about it.
338
    // This will be recovered by the next SETUP packet.
339
    if (epnum.number)
881 theseven 340
    {
341
        int epidx;
342
        int ifidx;
343
        const struct usb_endpoint* endpoint = usb_find_endpoint(data, epnum, &epidx, &ifidx);
344
        if (!endpoint) data->driver->unconfigure_ep(data, epnum);
345
        else if (endpoint->timeout) endpoint->timeout(data, ifidx, epidx, bytesleft);
346
    }
347
}
348
 
349
void usb_handle_xfer_complete(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft)
350
{
351
    if (!epnum.number)
352
    {
889 theseven 353
        bool (*callback)(const struct usb_instance* data, int size);
354
        if (epnum.direction == USB_ENDPOINT_DIRECTION_OUT)
355
        {
356
            callback = data->state->ep0_rx_callback;
357
            data->state->ep0_rx_callback = NULL;
358
        }
359
        else
360
        {
361
            callback = data->state->ep0_tx_callback;
362
            data->state->ep0_tx_callback = NULL;
363
        }
364
        if (callback) callback(data, bytesleft);
881 theseven 365
    }
366
    else
367
    {
368
        int epidx;
369
        int ifidx;
370
        const struct usb_endpoint* endpoint = usb_find_endpoint(data, epnum, &epidx, &ifidx);
371
        if (!endpoint) usb_unconfigure_ep(data, epnum);
372
        else if (endpoint->xfer_complete) endpoint->xfer_complete(data, ifidx, epidx, bytesleft);
373
    }
374
}
375
 
889 theseven 376
void usb_handle_setup_received(const struct usb_instance* data, union usb_endpoint_number epnum)
881 theseven 377
{
378
    if (!epnum.number)
379
    {
889 theseven 380
        if (!data->ep0_setup_hook || !data->ep0_setup_hook(data, data->buffer)) usb_handle_ep0_setup(data, data->buffer);
881 theseven 381
    }
382
    else
383
    {
384
        int epidx;
385
        int ifidx;
386
        const struct usb_endpoint* endpoint = usb_find_endpoint(data, epnum, &epidx, &ifidx);
387
        if (!endpoint) usb_unconfigure_ep(data, epnum);
889 theseven 388
        else if (endpoint->setup_received) endpoint->setup_received(data, ifidx, epidx);
881 theseven 389
    }
390
}
391
 
392
void usb_init(const struct usb_instance* data)
393
{
394
    // Initialize data struct
395
    data->state->current_address = 0;
396
    data->state->current_configuration = 0;
397
 
398
    // Initialize driver
399
    data->driver->init(data);
400
}
401
 
402
void usb_exit(const struct usb_instance* data)
403
{
404
    // Shut down driver
405
    data->driver->exit(data);
406
 
407
    // Unconfigure everything
408
    usb_unconfigure(data);
409
}
410