Subversion Repositories freemyipod

Rev

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