Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emBIOS is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "panic.h"
26
#include "usb.h"
27
#include "usb_ch9.h"
28
#include "usbdrv.h"
29
#include "thread.h"
30
#include "console.h"
31
#include "util.h"
32
#include "i2c.h"
25 theseven 33
#include "strlen.h"
34
#include "contextswitch.h"
15 theseven 35
 
36
 
37
static uint8_t ctrlresp[2] CACHEALIGN_ATTR;
38
static uint32_t dbgrecvbuf[0x80] CACHEALIGN_ATTR;
39
static uint32_t dbgsendbuf[0x80] CACHEALIGN_ATTR;
40
static uint32_t dbgasyncsendbuf[0x80] CACHEALIGN_ATTR;
41
static char dbgendpoints[4] IBSS_ATTR;
42
 
43
enum dbgaction_t
44
{
45
    DBGACTION_IDLE = 0,
46
    DBGACTION_I2CSEND,
47
    DBGACTION_I2CRECV,
48
    DBGACTION_POWEROFF
49
};
50
 
51
static uint32_t dbgstack[0x100] STACK_ATTR;
52
struct wakeup dbgwakeup IBSS_ATTR;
53
extern struct scheduler_thread* scheduler_threads;
54
static enum dbgaction_t dbgaction IBSS_ATTR;
55
static int dbgi2cbus IBSS_ATTR;
56
static int dbgi2cslave IBSS_ATTR;
57
static int dbgi2caddr IBSS_ATTR;
58
static int dbgi2clen IBSS_ATTR;
25 theseven 59
static char dbgconsendbuf[4096];
60
static char dbgconrecvbuf[1024];
61
static int dbgconsendreadidx IBSS_ATTR;
62
static int dbgconsendwriteidx IBSS_ATTR;
63
static int dbgconrecvreadidx IBSS_ATTR;
64
static int dbgconrecvwriteidx IBSS_ATTR;
65
static struct wakeup dbgconsendwakeup IBSS_ATTR;
66
static struct wakeup dbgconrecvwakeup IBSS_ATTR;
67
static bool dbgconsoleattached IBSS_ATTR;
15 theseven 68
 
25 theseven 69
static const char dbgconoverflowstr[] = "\n\n[overflowed]\n\n";
15 theseven 70
 
25 theseven 71
 
15 theseven 72
static struct usb_device_descriptor CACHEALIGN_ATTR device_descriptor =
73
{
74
    .bLength            = sizeof(struct usb_device_descriptor),
75
    .bDescriptorType    = USB_DT_DEVICE,
76
    .bcdUSB             = 0x0200,
77
    .bDeviceClass       = USB_CLASS_VENDOR_SPEC,
78
    .bDeviceSubClass    = 0xff,
79
    .bDeviceProtocol    = 0xff,
80
    .bMaxPacketSize0    = 64,
81
    .idVendor           = 0xffff,
82
    .idProduct          = 0xe000,
83
    .bcdDevice          = 0x0001,
84
    .iManufacturer      = 1,
85
    .iProduct           = 2,
86
    .iSerialNumber      = 0,
87
    .bNumConfigurations = 1
88
};
89
 
90
static struct usb_config_bundle
91
{
92
    struct usb_config_descriptor config_descriptor;
93
    struct usb_interface_descriptor interface_descriptor;
94
    struct usb_endpoint_descriptor endpoint1_descriptor;
95
    struct usb_endpoint_descriptor endpoint2_descriptor;
96
    struct usb_endpoint_descriptor endpoint3_descriptor;
97
    struct usb_endpoint_descriptor endpoint4_descriptor;
98
} __attribute__((packed)) CACHEALIGN_ATTR config_bundle = 
99
{
100
    .config_descriptor =
101
    {
102
        .bLength             = sizeof(struct usb_config_descriptor),
103
        .bDescriptorType     = USB_DT_CONFIG,
104
        .wTotalLength        = sizeof(struct usb_config_descriptor)
105
                             + sizeof(struct usb_interface_descriptor)
106
                             + sizeof(struct usb_endpoint_descriptor) * 4,
107
        .bNumInterfaces      = 1,
108
        .bConfigurationValue = 1,
109
        .iConfiguration      = 0,
110
        .bmAttributes        = USB_CONFIG_ATT_ONE,
111
        .bMaxPower           = 250
112
    },
113
    .interface_descriptor =
114
    {
115
        .bLength             = sizeof(struct usb_interface_descriptor),
116
        .bDescriptorType     = USB_DT_INTERFACE,
117
        .bInterfaceNumber    = 0,
118
        .bAlternateSetting   = 0,
119
        .bNumEndpoints       = 4,
120
        .bInterfaceClass     = USB_CLASS_VENDOR_SPEC,
121
        .bInterfaceSubClass  = 0xff,
122
        .bInterfaceProtocol  = 0xff,
123
        .iInterface          = 0
124
    },
125
    .endpoint1_descriptor =
126
    {
127
        .bLength             = sizeof(struct usb_endpoint_descriptor),
128
        .bDescriptorType     = USB_DT_ENDPOINT,
129
        .bEndpointAddress    = 0,
130
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
131
        .wMaxPacketSize      = 0,
132
        .bInterval           = 1
133
    },
134
    .endpoint2_descriptor =
135
    {
136
        .bLength             = sizeof(struct usb_endpoint_descriptor),
137
        .bDescriptorType     = USB_DT_ENDPOINT,
138
        .bEndpointAddress    = 0,
139
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
140
        .wMaxPacketSize      = 0,
141
        .bInterval           = 1
142
    },
143
    .endpoint3_descriptor =
144
    {
145
        .bLength             = sizeof(struct usb_endpoint_descriptor),
146
        .bDescriptorType     = USB_DT_ENDPOINT,
147
        .bEndpointAddress    = 0,
148
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
149
        .wMaxPacketSize      = 0,
150
        .bInterval           = 1
151
    },
152
    .endpoint4_descriptor =
153
    {
154
        .bLength             = sizeof(struct usb_endpoint_descriptor),
155
        .bDescriptorType     = USB_DT_ENDPOINT,
156
        .bEndpointAddress    = 0,
157
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
158
        .wMaxPacketSize      = 0,
159
        .bInterval           = 1
160
    }
161
};
162
 
163
static struct usb_string_descriptor CACHEALIGN_ATTR string_devicename =
164
{
165
    32,
166
    USB_DT_STRING,
167
    {'e', 'm', 'B', 'I', 'O', 'S', ' ', 'D', 'e', 'b', 'u', 'g', 'g', 'e', 'r'}
168
};
169
 
170
static const struct usb_string_descriptor CACHEALIGN_ATTR lang_descriptor =
171
{
172
    4,
173
    USB_DT_STRING,
174
    {0x0409}
175
};
176
 
177
 
178
void usb_setup_dbg_listener()
179
{
180
    usb_drv_recv(dbgendpoints[0], dbgrecvbuf, usb_drv_port_speed() ? 512 : 64);
181
}
182
 
183
void usb_handle_control_request(struct usb_ctrlrequest* req)
184
{
185
    const void* addr;
186
    int size = -1;
187
    switch (req->bRequest)
188
    {
189
    case USB_REQ_GET_STATUS:
190
        if (req->bRequestType == USB_DIR_IN) ctrlresp[0] = 1;
191
        else ctrlresp[0] = 0;
192
        ctrlresp[1] = 0;
193
        addr = ctrlresp;
194
        size = 2;
195
        break;
196
    case USB_REQ_CLEAR_FEATURE:
197
        if (req->bRequestType == USB_RECIP_ENDPOINT && req->wValue == USB_ENDPOINT_HALT)
198
            usb_drv_stall(req->wIndex & 0xf, false, req->wIndex >> 7);
199
        size = 0;
200
        break;
201
    case USB_REQ_SET_FEATURE:
202
        size = 0;
203
        break;
204
    case USB_REQ_SET_ADDRESS:
205
        size = 0;
206
        usb_drv_cancel_all_transfers();
207
        usb_drv_set_address(req->wValue);
208
        usb_setup_dbg_listener();
209
        break;
210
    case USB_REQ_GET_DESCRIPTOR:
211
        switch (req->wValue >> 8)
212
        {
213
        case USB_DT_DEVICE:
214
            addr = &device_descriptor;
215
            size = sizeof(device_descriptor);
216
            break;
217
        case USB_DT_OTHER_SPEED_CONFIG:
218
        case USB_DT_CONFIG:
219
            if ((req->wValue >> 8) == USB_DT_CONFIG)
220
            {
221
                int maxpacket = usb_drv_port_speed() ? 512 : 64;
222
                config_bundle.endpoint1_descriptor.wMaxPacketSize = maxpacket;
223
                config_bundle.endpoint2_descriptor.wMaxPacketSize = maxpacket;
224
                config_bundle.endpoint3_descriptor.wMaxPacketSize = maxpacket;
225
                config_bundle.endpoint4_descriptor.wMaxPacketSize = maxpacket;
226
                config_bundle.config_descriptor.bDescriptorType = USB_DT_CONFIG;
227
            }
228
            else
229
            {
230
                int maxpacket = usb_drv_port_speed() ? 64 : 512;
231
                config_bundle.endpoint1_descriptor.wMaxPacketSize = maxpacket;
232
                config_bundle.endpoint2_descriptor.wMaxPacketSize = maxpacket;
233
                config_bundle.endpoint3_descriptor.wMaxPacketSize = maxpacket;
234
                config_bundle.endpoint4_descriptor.wMaxPacketSize = maxpacket;
235
                config_bundle.config_descriptor.bDescriptorType = USB_DT_OTHER_SPEED_CONFIG;
236
            }
237
            addr = &config_bundle;
238
            size = sizeof(config_bundle);
239
            break;
240
        case USB_DT_STRING:
241
            switch (req->wValue & 0xff)
242
            {
243
            case 0:
244
                addr = &lang_descriptor;
245
                size = sizeof(lang_descriptor);
246
                break;
247
            case 1:
248
                string_devicename.bLength = 14;
249
                addr = &string_devicename;
250
                size = 14;
251
            case 2:
252
                string_devicename.bLength = sizeof(string_devicename);
253
                addr = &string_devicename;
254
                size = sizeof(string_devicename);
255
                break;
256
            }
257
            break;
258
        }
259
        break;
260
    case USB_REQ_GET_CONFIGURATION:
261
        ctrlresp[0] = 1;
262
        addr = ctrlresp;
263
        size = 1;
264
        break;
265
    case USB_REQ_SET_CONFIGURATION:
266
        usb_drv_cancel_all_transfers();
267
        usb_setup_dbg_listener();
268
        size = 0;
269
        break;
270
    }
271
    if (!size) usb_drv_send_nonblocking(0, NULL, 0);
272
    else if (size == -1)
273
    {
274
        usb_drv_stall(0, true, true);
275
        usb_drv_stall(0, true, false);
276
    }
277
    else
278
    {
279
        usb_drv_recv(0, NULL, 0);
280
        usb_drv_send_nonblocking(0, addr, size > req->wLength ? req->wLength : size);
281
    }
282
}
283
 
284
bool set_dbgaction(enum dbgaction_t action)
285
{
286
    if (dbgaction != DBGACTION_IDLE)
287
    {
288
        dbgsendbuf[0] = 3;
289
        usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16);
290
        return true;
291
    }
292
    dbgaction = action;
293
    wakeup_signal(&dbgwakeup);
294
    return false;
295
}
296
 
297
void reset() __attribute__((noreturn));
298
 
299
void usb_handle_transfer_complete(int endpoint, int dir, int status, int length)
300
{
301
    void* addr = dbgsendbuf;
302
    int size = 0;
303
    if (endpoint == dbgendpoints[0])
304
    {
305
        switch (dbgrecvbuf[0])
306
        {
307
        case 1:  // GET VERSION
308
            dbgsendbuf[0] = 1;
309
            dbgsendbuf[1] = 0x01010000;
310
            dbgsendbuf[2] = PLATFORM_ID;
25 theseven 311
            dbgsendbuf[3] = 0x02000200;
15 theseven 312
            size = 16;
313
            break;
314
        case 2:  // RESET
315
            reset();
316
            break;
317
        case 3:  // POWER OFF
318
            set_dbgaction(DBGACTION_POWEROFF);
319
            break;
320
        case 4:  // READ MEMORY
321
            dbgsendbuf[0] = 1;
322
            memcpy(&dbgsendbuf[4], (const void*)dbgrecvbuf[1], dbgrecvbuf[2]);
323
            size = dbgrecvbuf[2] + 16;
324
            break;
325
        case 5:  // WRITE MEMORY
326
            dbgsendbuf[0] = 1;
327
            memcpy((void*)dbgrecvbuf[1], &dbgrecvbuf[4], dbgrecvbuf[2]);
328
            size = 16;
329
            break;
330
        case 6:  // READ DMA
331
            dbgsendbuf[0] = 1;
332
            usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16);
333
            usb_drv_send_nonblocking(dbgendpoints[3], (const void*)dbgrecvbuf[1], dbgrecvbuf[2]);
334
            break;
335
        case 7:  // WRITE DMA
336
            dbgsendbuf[0] = 1;
337
            size = 16;
338
            usb_drv_recv(dbgendpoints[2], (void*)dbgrecvbuf[1], dbgrecvbuf[2]);
339
            break;
340
        case 8:  // READ I2C
341
            if (set_dbgaction(DBGACTION_I2CRECV)) break;
342
            dbgi2cbus = dbgrecvbuf[1] & 0xff;
343
            dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
344
            dbgi2caddr = (dbgrecvbuf[1] >> 16) & 0xff;
345
            dbgi2clen = dbgrecvbuf[1] >> 24;
346
            break;
347
        case 9:  // WRITE I2C
348
            if (set_dbgaction(DBGACTION_I2CSEND)) break;
349
            dbgi2cbus = dbgrecvbuf[1] & 0xff;
350
            dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
351
            dbgi2caddr = (dbgrecvbuf[1] >> 16) & 0xff;
352
            dbgi2clen = dbgrecvbuf[1] >> 24;
353
            memcpy(dbgasyncsendbuf, &dbgsendbuf[4], dbgi2clen);
354
            break;
25 theseven 355
        case 10:  // READ CONSOLE
356
            dbgconsoleattached = true;
357
            wakeup_signal(&dbgconsendwakeup);
358
            int bytes = dbgconsendwriteidx - dbgconsendreadidx;
359
            if (bytes >= sizeof(dbgconsendbuf)) bytes -= sizeof(dbgconsendbuf);
360
            if (bytes)
361
            {
362
                if (bytes < 0) bytes += sizeof(dbgconsendbuf);
363
                if (bytes > dbgrecvbuf[1]) bytes = dbgrecvbuf[1];
364
                int readbytes = bytes;
365
                char* outptr = (char*)&dbgsendbuf[4];
366
                if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
367
                {
368
                    readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
369
                    memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
370
                    dbgconsendreadidx = 0;
371
                    outptr = &outptr[readbytes];
372
                    readbytes = bytes - readbytes;
373
                }
374
                if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
375
                dbgconsendreadidx += readbytes;
376
            }
377
            dbgsendbuf[0] = 1;
378
            dbgsendbuf[1] = bytes;
379
            dbgsendbuf[2] = sizeof(dbgconsendbuf);
380
            dbgsendbuf[3] = dbgconsendwriteidx - dbgconsendreadidx;
381
            size = 16 + dbgrecvbuf[1];
382
            break;
15 theseven 383
        default:
384
            dbgsendbuf[0] = 2;
385
            size = 16;
386
        }
387
        usb_setup_dbg_listener();
388
        if (size) usb_drv_send_nonblocking(dbgendpoints[1], addr, size);
389
    }
390
}
391
 
392
void usb_handle_bus_reset(void)
393
{
394
    dbgendpoints[0] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
395
    dbgendpoints[1] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
396
    dbgendpoints[2] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
397
    dbgendpoints[3] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
398
    config_bundle.endpoint1_descriptor.bEndpointAddress = dbgendpoints[0];
399
    config_bundle.endpoint2_descriptor.bEndpointAddress = dbgendpoints[1];
400
    config_bundle.endpoint3_descriptor.bEndpointAddress = dbgendpoints[2];
401
    config_bundle.endpoint4_descriptor.bEndpointAddress = dbgendpoints[3];
402
    usb_setup_dbg_listener();
403
}
404
 
405
void dbgthread(void)
406
{
407
    int i;
25 theseven 408
    int t;
15 theseven 409
    while (1)
410
    {
411
        wakeup_wait(&dbgwakeup, TIMEOUT_BLOCK);
412
        for (i = 0; i < MAX_THREADS; i++)
413
            if (scheduler_threads[i].state == THREAD_DEFUNCT)
414
            {
415
                if (scheduler_threads[i].block_type == THREAD_DEFUNCT_STKOV)
416
                    cprintf(1, "\n*PANIC*\nStack overflow! (%s)\n",
417
                            scheduler_threads[i].name);
418
                scheduler_threads[i].state = THREAD_DEFUNCT_ACK;
419
            }
420
        if (dbgaction != DBGACTION_IDLE)
421
        {
422
            switch (dbgaction)
423
            {
424
            case DBGACTION_I2CSEND:
425
                i2c_send(dbgi2cbus, dbgi2cslave, dbgi2caddr, (uint8_t*)dbgasyncsendbuf, dbgi2clen);
426
                dbgasyncsendbuf[0] = 1;
427
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
428
                break;
429
            case DBGACTION_I2CRECV:
430
                i2c_recv(dbgi2cbus, dbgi2cslave, dbgi2caddr,
431
                         (uint8_t*)(&dbgasyncsendbuf[4]), dbgi2clen);
432
                dbgasyncsendbuf[0] = 1;
433
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16 + dbgi2clen);
434
                break;
435
            case DBGACTION_POWEROFF:
436
                break;
437
            }
438
            dbgaction = DBGACTION_IDLE;
439
        }
440
    }
441
}
442
 
443
void usb_init(void)
444
{
445
    dbgaction = DBGACTION_IDLE;
446
    wakeup_init(&dbgwakeup);
25 theseven 447
    dbgconsendreadidx = 0;
448
    dbgconsendwriteidx = 0;
449
    dbgconrecvreadidx = 0;
450
    dbgconrecvwriteidx = 0;
451
    wakeup_init(&dbgconsendwakeup);
452
    wakeup_init(&dbgconrecvwakeup);
453
    dbgconsoleattached = false;;
15 theseven 454
    thread_create("Debugger", dbgthread, dbgstack, sizeof(dbgstack), 255, SYSTEM_THREAD, true);
455
    usb_drv_init();
456
}
25 theseven 457
 
458
int dbgconsole_getfree() ICODE_ATTR;
459
int dbgconsole_getfree()
460
{
461
    int free = dbgconsendreadidx - dbgconsendwriteidx - 1;
462
    if (free < 0) free += sizeof(dbgconsendbuf);
463
    return free;
464
}
465
 
466
int dbgconsole_makespace(int length) ICODE_ATTR;
467
int dbgconsole_makespace(int length)
468
{
469
    int free = dbgconsole_getfree();
470
    while (!free && dbgconsoleattached)
471
    {
472
        if (wakeup_wait(&dbgconsendwakeup, 2000000) == THREAD_TIMEOUT)
473
            dbgconsoleattached = false;
474
        free = dbgconsole_getfree();
475
    }
476
    if (free) return free > length ? length : free;
477
    if (length > sizeof(dbgconsendbuf) - 17) length = sizeof(dbgconsendbuf) - 17;
478
    uint32_t mode = enter_critical_section();
479
    dbgconsendreadidx += length;
480
    if (dbgconsendreadidx >= sizeof(dbgconsendbuf))
481
        dbgconsendreadidx -= sizeof(dbgconsendbuf);
482
    int offset = 0;
483
    int idx = dbgconsendreadidx;
484
    if (idx + 16 >= sizeof(dbgconsendbuf))
485
    {
486
        offset = sizeof(dbgconsendbuf) - dbgconsendreadidx;
487
        memcpy(&dbgconsendbuf[dbgconsendreadidx], dbgconoverflowstr, offset);
488
        idx = 0;
489
    }
490
    if (offset != 16) memcpy(&dbgconsendbuf[idx], &dbgconoverflowstr[offset], 16 - offset);
491
    leave_critical_section(mode);
492
    return length;
493
}
494
 
495
void dbgconsole_putc(char string)
496
{
497
    dbgconsole_makespace(1);
498
    dbgconsendbuf[dbgconsendwriteidx++] = string;
499
    if (dbgconsendwriteidx >= sizeof(dbgconsendbuf))
500
        dbgconsendwriteidx -= sizeof(dbgconsendbuf);
501
}
502
 
503
void dbgconsole_write(const char* string, size_t length)
504
{
505
    while (length)
506
    {
507
        int space = dbgconsole_makespace(length);
508
        if (dbgconsendwriteidx + space >= sizeof(dbgconsendbuf))
509
        {
510
            int bytes = sizeof(dbgconsendbuf) - dbgconsendwriteidx;
511
            memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, bytes);
512
            dbgconsendwriteidx = 0;
513
            string = &string[bytes];
514
            space -= bytes;
515
            length -= bytes;
516
        }
517
        if (space) memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, space);
518
        dbgconsendwriteidx += space;
519
        string = &string[space];
520
        length -= space;
521
    }
522
}
523
 
524
void dbgconsole_puts(const char* string)
525
{
526
    dbgconsole_write(string, strlen(string));
527
}