Subversion Repositories freemyipod

Rev

Rev 949 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
892 theseven 1
#include "global.h"
2
#include "usbglue.h"
3
#include "usb.h"
4
#include "usbdebug.h"
5
#include "util.h"
6
#include "thread.h"
7
#include "malloc.h"
8
#include USB_DRIVER_HEADER
9
 
10
static const struct usb_devicedescriptor usb_devicedescriptor =
11
{
12
    .bLength = sizeof(struct usb_devicedescriptor),
13
    .bDescriptorType = USB_DESCRIPTOR_TYPE_DEVICE,
14
    .bcdUSB = 0x0200,
15
    .bDeviceClass = 0,
16
    .bDeviceSubClass = 0,
17
    .bDeviceProtocol = 0,
18
    .bMaxPacketSize0 = 64,
19
    .idVendor = 0xffff,
20
    .idProduct = 0xe000,
21
    .bcdDevice = 2,
22
    .iManufacturer = 1,
23
    .iProduct = 2,
24
    .iSerialNumber = 0,
25
    .bNumConfigurations = 1,
26
};
27
 
949 theseven 28
static struct __attribute__((packed)) _usb_config1_descriptors
892 theseven 29
{
30
    struct usb_configurationdescriptor c1;
31
    struct usb_interfacedescriptor c1_i0_a0;
949 theseven 32
    struct usb_endpointdescriptor c1_i0_a0_e1out;
33
    struct usb_endpointdescriptor c1_i0_a0_e1in;
892 theseven 34
} usb_config1_descriptors =
35
{
36
    .c1 =
37
    {
38
        .bLength = sizeof(struct usb_configurationdescriptor),
39
        .bDescriptorType = USB_DESCRIPTOR_TYPE_CONFIGURATION,
40
        .wTotalLength = sizeof(struct _usb_config1_descriptors),
41
        .bNumInterfaces = 1,
42
        .bConfigurationValue = 1,
43
        .iConfiguration = 0,
44
        .bmAttributes = { .buspowered = 1, .selfpowered = 1 },
45
        .bMaxPower = USB_MAXCURRENT / 2,
46
    },
47
    .c1_i0_a0 =
48
    {
49
        .bLength = sizeof(struct usb_interfacedescriptor),
50
        .bDescriptorType = USB_DESCRIPTOR_TYPE_INTERFACE,
51
        .bInterfaceNumber = 0,
52
        .bAlternateSetting = 0,
949 theseven 53
        .bNumEndpoints = 2,
892 theseven 54
        .bInterfaceClass = 0xff,
55
        .bInterfaceSubClass = 0x00,
56
        .bInterfaceProtocol = 0x00,
57
        .iInterface = 0,
58
    },
949 theseven 59
    .c1_i0_a0_e1out =
60
    {
61
        .bLength = sizeof(struct usb_endpointdescriptor),
62
        .bDescriptorType = USB_DESCRIPTOR_TYPE_ENDPOINT,
63
        .bEndpointAddress = { .number = USBDEBUG_ENDPOINT_OUT, .direction = USB_ENDPOINT_DIRECTION_OUT },
64
        .bmAttributes = { .type = USB_ENDPOINT_ATTRIBUTE_TYPE_BULK },
65
        .wMaxPacketSize = 512,
66
        .bInterval = 1,
67
    },
68
    .c1_i0_a0_e1in =
69
    {
70
        .bLength = sizeof(struct usb_endpointdescriptor),
71
        .bDescriptorType = USB_DESCRIPTOR_TYPE_ENDPOINT,
72
        .bEndpointAddress = { .number = USBDEBUG_ENDPOINT_IN, .direction = USB_ENDPOINT_DIRECTION_IN },
73
        .bmAttributes = { .type = USB_ENDPOINT_ATTRIBUTE_TYPE_BULK },
74
        .wMaxPacketSize = 512,
75
        .bInterval = 1,
76
    },
892 theseven 77
};
78
 
949 theseven 79
static const struct usb_interfacedescriptor usb_simpledebug_intf_desc =
80
{
81
    .bLength = sizeof(struct usb_interfacedescriptor),
82
    .bDescriptorType = USB_DESCRIPTOR_TYPE_INTERFACE,
83
    .bInterfaceNumber = 0,
84
    .bAlternateSetting = 0,
85
    .bNumEndpoints = 0,
86
    .bInterfaceClass = 0xff,
87
    .bInterfaceSubClass = 0x00,
88
    .bInterfaceProtocol = 0x00,
89
    .iInterface = 0,
90
};
91
 
892 theseven 92
static const struct usb_stringdescriptor usb_string_language =
93
{
94
    .bLength = sizeof(usb_string_language) + sizeof(*usb_string_language.wString),
95
    .bDescriptorType = USB_DESCRIPTOR_TYPE_STRING,
96
    .wString = { 0x0409 },
97
};
98
 
99
static const struct usb_stringdescriptor usb_string_vendor =
100
{
101
    .bLength = sizeof(usb_string_vendor) + sizeof(*usb_string_vendor.wString) * 14,
102
    .bDescriptorType = USB_DESCRIPTOR_TYPE_STRING,
103
    .wString = { 'f', 'r', 'e', 'e', 'm', 'y', 'i', 'p', 'o', 'd', '.', 'o', 'r', 'g' },
104
};
105
 
106
static const struct usb_stringdescriptor usb_string_product =
107
{
108
    .bLength = sizeof(usb_string_product) + sizeof(*usb_string_product.wString) * 15,
109
    .bDescriptorType = USB_DESCRIPTOR_TYPE_STRING,
110
    .wString = { 'e', 'm', 'C', 'O', 'R', 'E', ' ', 'd', 'e', 'b', 'u', 'g', 'g', 'e', 'r' },
111
};
112
 
113
static const struct usb_stringdescriptor* usb_stringdescriptors[] =
114
{
115
    &usb_string_language,
116
    &usb_string_vendor,
117
    &usb_string_product,
118
};
119
 
949 theseven 120
static const struct usb_endpoint usb_c1_i0_a0_ep1out =
121
{
122
    .number = { .number = USBDEBUG_ENDPOINT_OUT, .direction = USB_ENDPOINT_DIRECTION_OUT },
123
    .ctrl_request = usbdebug_bulk_ctrl_request,
124
    .xfer_complete = usbdebug_bulk_xfer_complete,
125
    .setup_received = NULL,
126
};
127
 
128
static const struct usb_endpoint usb_c1_i0_a0_ep1in =
129
{
130
    .number = { .number = USBDEBUG_ENDPOINT_IN, .direction = USB_ENDPOINT_DIRECTION_IN },
131
    .ctrl_request = usbdebug_bulk_ctrl_request,
132
    .xfer_complete = usbdebug_bulk_xfer_complete,
133
    .timeout = NULL,
134
};
135
 
892 theseven 136
static const struct usb_altsetting usb_c1_i0_a0 =
137
{
949 theseven 138
    .set_altsetting = usbdebug_bulk_enable,
139
    .unset_altsetting = usbdebug_bulk_disable,
140
    .endpoint_count = 2,
141
    .endpoints =
142
    {
143
        &usb_c1_i0_a0_ep1out,
144
        &usb_c1_i0_a0_ep1in,
145
    },
146
};
147
 
148
static void usbglue_bus_reset(const struct usb_instance* data, int highspeed)
149
{
150
    usb_config1_descriptors.c1_i0_a0_e1out.wMaxPacketSize = highspeed ? 512 : 64;
151
    usb_config1_descriptors.c1_i0_a0_e1in.wMaxPacketSize = highspeed ? 512 : 64;
152
}
153
 
154
static struct usb_interface usb_c1_i0 =
155
{
156
    .bus_reset = usbdebug_bus_reset,
157
    .ctrl_request = usbdebug_handle_setup,
158
    .altsetting_count = 1,
159
    .altsettings =
160
    {
161
        &usb_c1_i0_a0,
162
    },
163
};
164
 
165
static const struct usb_altsetting usb_simpledebug_intf_a0 =
166
{
892 theseven 167
    .set_altsetting = usbdebug_enable,
168
    .unset_altsetting = usbdebug_disable,
169
    .endpoint_count = 0,
170
    .endpoints =
171
    {
172
    },
173
};
174
 
949 theseven 175
static struct usb_interface usb_simpledebug_intf =
892 theseven 176
{
177
    .bus_reset = NULL,
178
    .ctrl_request = usbdebug_handle_setup,
179
    .altsetting_count = 1,
180
    .altsettings =
181
    {
949 theseven 182
        &usb_simpledebug_intf_a0,
892 theseven 183
    },
184
};
185
 
186
static const struct usb_configuration usb_c1 =
187
{
188
    .descriptor = &usb_config1_descriptors.c1,
189
    .set_configuration = NULL,
190
    .unset_configuration = NULL,
191
    .interface_count = 1,
192
    .interfaces =
193
    {
194
        &usb_c1_i0,
195
    },
196
};
197
 
198
 
199
static USB_DRIVER_CONFIG_TYPE usb_driver_config = USB_DRIVER_CONFIG;
200
 
201
static USB_DRIVER_STATE_TYPE usb_driver_state = USB_DRIVER_STATE;
202
 
203
static struct usb_state usb_state =
204
{
205
    .interface_altsetting = { 0 },
206
};
207
 
208
static union usb_ep0_buffer usb_buffer CACHEALIGN_ATTR;
209
 
210
struct usb_instance usb_default_data =
211
{
212
    .driver = &USB_DRIVER,
213
    .driver_config = &usb_driver_config,
214
    .driver_state = &usb_driver_state,
215
    .state = &usb_state,
216
    .buffer = &usb_buffer,
949 theseven 217
    .bus_reset = usbglue_bus_reset,
892 theseven 218
    .ep0_setup_hook = NULL,
219
    .configuration_count = 1,
220
    .stringdescriptor_count = 3,
221
    .devicedescriptor = &usb_devicedescriptor,
222
    .stringdescriptors = usb_stringdescriptors,
223
    .configurations =
224
    {
225
        &usb_c1,
226
    },
227
};
228
 
229
struct usb_instance* usb_data = &usb_default_data;
230
 
231
static struct scheduler_thread usbmanager_thread_handle;
232
static uint32_t usbmanager_stack[0x40] STACK_ATTR;
233
static bool usb_connected;
234
static struct mutex usbmanager_mutex;
235
 
236
void usbmanager_thread(void* arg0, void* arg1, void* arg2, void* arg3)
237
{
238
    while (true)
239
    {
240
        sleep(200000);
241
        mutex_lock(&usbmanager_mutex, TIMEOUT_BLOCK);
242
        bool newstate = vbus_state();
243
        if (usb_connected != newstate)
244
        {
245
            if (newstate) usb_init(usb_data);
246
            else usb_exit(usb_data);
247
            usb_connected = newstate;
248
        }
249
        mutex_unlock(&usbmanager_mutex);
250
    }
251
}
252
 
253
void usbmanager_init()
254
{
255
    mutex_init(&usbmanager_mutex);
256
    usb_connected = false;
257
    usbdebug_init();
258
 
259
    thread_create(&usbmanager_thread_handle, "synopsysotg", usbmanager_thread,
260
                  usbmanager_stack, sizeof(usbmanager_stack), OS_THREAD, 63, true,
261
                  NULL, NULL, NULL, NULL);
262
}
263
 
264
void usbmanager_exit()
265
{
266
    if (usb_connected) usb_exit(usb_data);
267
}
268
 
269
int usbmanager_install_custom(const struct usb_devicedescriptor* devicedescriptor,
270
                              uint8_t config_count, const struct usb_configuration** configurations,
271
                              uint8_t string_count, const struct usb_stringdescriptor** stringdescriptors,
272
                              bool enable_debug)
273
{
274
    int i, j, k, l;
275
    int size = sizeof(struct usb_instance);
276
    int descsize = sizeof(struct usb_devicedescriptor);
277
    int max_interfaces = 0;
278
    for (i = 0; i < config_count; i++)
279
    {
280
        const struct usb_configuration* configuration = configurations[i];
281
        size += 4 + sizeof(struct usb_configuration);
282
        descsize += configuration->descriptor->wTotalLength;
283
        if (configuration->interface_count > max_interfaces)
284
            max_interfaces = configuration->interface_count;
285
        for (j = 0; j < configuration->interface_count; j++)
286
        {
287
            const struct usb_interface* interface = configuration->interfaces[j];
288
            size += 4 + sizeof(struct usb_interface);
289
            for (k = 0; k < interface->altsetting_count; k++)
290
            {
291
                const struct usb_altsetting* altsetting = interface->altsettings[k];
292
                size += 4 + sizeof(struct usb_altsetting)
293
                      + (4 + sizeof(struct usb_endpoint)) * altsetting->endpoint_count;
294
            }
295
        }
296
    }
297
    for (i = 0; i < string_count; i++) descsize += stringdescriptors[i]->bLength;
298
    if (enable_debug)
299
    {
300
        size += 4;
301
        descsize += sizeof(struct usb_interfacedescriptor);
302
        if (configurations[0]->interface_count == max_interfaces) max_interfaces++;
303
    }
304
    size += 4 * string_count + sizeof(struct usb_state) + max_interfaces;
305
    void* buf = malloc(size + descsize);
306
    if (!buf) RET_ERR(1);
307
    reownalloc(buf, KERNEL_OWNER(KERNEL_OWNER_CUSTOM_USB));
308
    void* descbuf = buf + size;
309
    struct usb_instance* instance = (struct usb_instance*)buf;
310
    buf += sizeof(struct usb_instance) + 4 * config_count;
311
    memcpy(instance, &usb_default_data, sizeof(struct usb_instance));
312
    struct usb_devicedescriptor* devdescriptor = (struct usb_devicedescriptor*)descbuf;
313
    descbuf += sizeof(struct usb_devicedescriptor);
314
    memcpy(devdescriptor, devicedescriptor, sizeof(struct usb_devicedescriptor));
315
    instance->devicedescriptor = devdescriptor;
316
    for (i = 0; i < config_count; i++)
317
    {
318
        const struct usb_configuration* configuration = configurations[i];
319
        struct usb_configuration* config = (struct usb_configuration*)buf;
320
        buf += sizeof(struct usb_configuration) + 4 * configuration->interface_count;
321
        memcpy(config, configuration, sizeof(struct usb_configuration));
322
        instance->configurations[i] = config;
323
        struct usb_configurationdescriptor* cfgdescriptor = (struct usb_configurationdescriptor*)descbuf;
324
        descbuf += configuration->descriptor->wTotalLength;
325
        memcpy(cfgdescriptor, configuration->descriptor, configuration->descriptor->wTotalLength);
326
        config->descriptor = cfgdescriptor;
327
        if (!i && enable_debug) buf += 4;
328
        for (j = 0; j < configuration->interface_count; j++)
329
        {
330
            const struct usb_interface* interface = configuration->interfaces[j];
331
            struct usb_interface* intf = (struct usb_interface*)buf;
332
            buf += sizeof(struct usb_interface) + 4 * interface->altsetting_count;
333
            memcpy(intf, interface, sizeof(struct usb_interface));
334
            config->interfaces[j] = intf;
335
            for (k = 0; k < interface->altsetting_count; k++)
336
            {
337
                const struct usb_altsetting* altsetting = interface->altsettings[k];
338
                struct usb_altsetting* as = (struct usb_altsetting*)buf;
339
                buf += sizeof(struct usb_altsetting) + 4 * altsetting->endpoint_count;
340
                memcpy(as, altsetting, sizeof(struct usb_altsetting));
341
                intf->altsettings[k] = as;
342
                for (l = 0; l < altsetting->endpoint_count; l++)
343
                {
344
                    const struct usb_endpoint* endpoint = altsetting->endpoints[l];
345
                    struct usb_endpoint* ep = (struct usb_endpoint*)buf;
346
                    buf += sizeof(struct usb_endpoint);
347
                    memcpy(ep, endpoint, sizeof(struct usb_endpoint));
348
                    as->endpoints[l] = ep;
349
                }
350
            }
351
        }
352
        if (!i && enable_debug)
353
        {
354
            struct usb_interfacedescriptor* intfdescriptor = (struct usb_interfacedescriptor*)descbuf;
355
            descbuf += sizeof(struct usb_interfacedescriptor);
949 theseven 356
            memcpy(intfdescriptor, &usb_simpledebug_intf_desc, sizeof(struct usb_interfacedescriptor));
892 theseven 357
            intfdescriptor->bInterfaceNumber = j;
949 theseven 358
            config->interfaces[j] = &usb_simpledebug_intf;
892 theseven 359
            config->interface_count++;
360
            cfgdescriptor->wTotalLength += sizeof(struct usb_interfacedescriptor);
361
            cfgdescriptor->bNumInterfaces++;
362
        }
363
    }
364
    const struct usb_stringdescriptor** stringdescs = (const struct usb_stringdescriptor**)buf;
365
    buf += 4 * string_count;
366
    instance->stringdescriptors = stringdescs;
367
    for (i = 0; i < string_count; i++)
368
    {
369
        const struct usb_stringdescriptor* stringdescriptor = stringdescriptors[i];
370
        struct usb_stringdescriptor* stringdesc = (struct usb_stringdescriptor*)descbuf;
371
        descbuf += stringdescriptor->bLength;
372
        memcpy(stringdesc, stringdescriptor, stringdescriptor->bLength);
373
        stringdescs[i] = stringdesc;
374
    }
375
    struct usb_state* state = (struct usb_state*)buf;
376
    buf += sizeof(struct usb_state) + max_interfaces;
377
    instance->state = state;
378
    mutex_lock(&usbmanager_mutex, TIMEOUT_BLOCK);
379
    if (usb_connected) usb_exit(usb_data);
380
    if (usb_data != &usb_default_data) free(usb_data);
381
    usb_data = instance;
382
    if (usb_connected) usb_init(usb_data);
383
    mutex_unlock(&usbmanager_mutex);
384
    return 0;
385
}
386
 
387
void usbmanager_uninstall_custom()
388
{
389
    mutex_lock(&usbmanager_mutex, TIMEOUT_BLOCK);
390
    if (usb_data != &usb_default_data)
391
    {
392
        if (usb_connected) usb_exit(usb_data);
393
        free(usb_data);
394
        usb_data = &usb_default_data;
395
        if (usb_connected) usb_init(usb_data);
396
    }
397
    mutex_unlock(&usbmanager_mutex);
398
}
399
 
400
uint32_t usbmanager_get_available_endpoints()
401
{
402
    return USB_ENDPOINTS;
403
}
404
 
405
bool usbmanager_get_connected()
406
{
407
    return usb_connected;
408
}
409