Subversion Repositories freemyipod

Rev

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