summaryrefslogtreecommitdiffstats
path: root/firmware/drivers/isp1583.c
blob: c028d2fa0111791e1c20cf8aecb3b71de6615019 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 by Tomasz Malesinski
 * Copyright (C) 2008 by Maurus Cuelenaere
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "config.h"
#include "usb_ch9.h"
#include "usb_drv.h"
#include "usb_core.h"
#include "isp1583.h"
#include "thread.h"
#include "logf.h"
#include "stdio.h"

struct usb_endpoint
{
    unsigned char *out_buf;
    short out_len;
    short out_ptr;
    void (*out_done)(int, unsigned char *, int);
    unsigned char out_in_progress;

    unsigned char *in_buf;
    short in_min_len;
    short in_max_len;
    short in_ptr;
    void (*in_done)(int, unsigned char *, int);
    unsigned char in_ack;

    unsigned char halt[2];
    unsigned char enabled[2];
    short max_pkt_size[2];
    short type;
    char allocation;
};

static unsigned char setup_pkt_buf[8];
static struct usb_endpoint endpoints[USB_NUM_ENDPOINTS];

#if 0
#define ZVM_SPECIFIC                asm volatile( \
                                                 "LDR     R12, =0x50FFC000\n" \
                                                 "LDRH    R12, [R12]\n"  \
                                                 : : : "r12");
#else
#define ZVM_SPECIFIC
#endif

static bool high_speed_mode = false;

static inline void or_int_value(volatile unsigned short *a, volatile unsigned short *b, unsigned long r, unsigned long value)
{
    set_int_value(*a, *b, (r | value));
}
static inline void bc_int_value(volatile unsigned short *a, volatile unsigned short *b, unsigned long r, unsigned long value)
{
    set_int_value(*a, *b, (r & ~value));
}

static inline void nop_f(void)
{
    yield();
}

#define NOP     asm volatile("nop\n");

static inline int ep_index(int n, bool dir)
{
    return (n << 1) | dir;
}

static inline bool epidx_dir(int idx)
{
    return idx & 1;
}

static inline int epidx_n(int idx)
{
    return idx >> 1;
}

static inline void usb_select_endpoint(int idx)
{
    /* Select the endpoint */
    ISP1583_DFLOW_EPINDEX = idx;
    /* The delay time from the Write Endpoint Index register to the Read Data Port register must be at least 190 ns.
     * The delay time from the Write Endpoint Index register to the Write Data Port register must be at least 100 ns.
     */
    NOP;
}

static inline void usb_select_setup_endpoint(void)
{
    /* Select the endpoint */
    ISP1583_DFLOW_EPINDEX = DFLOW_EPINDEX_EP0SETUP;
    /* The delay time from the Write Endpoint Index register to the Read Data Port register must be at least 190 ns.
     * The delay time from the Write Endpoint Index register to the Write Data Port register must be at least 100 ns.
     */
    NOP;
}

static void usb_setup_endpoint(int idx, int max_pkt_size, int type)
{
    if(epidx_n(idx)!=EP_CONTROL)
    {
        usb_select_endpoint(idx);
        ISP1583_DFLOW_MAXPKSZ = max_pkt_size & 0x7FF;
        ISP1583_DFLOW_EPTYPE = (DFLOW_EPTYPE_NOEMPKT | DFLOW_EPTYPE_DBLBUF | (type & 0x3));

        /* clear buffer ... */
        ISP1583_DFLOW_CTRLFUN |= DFLOW_CTRLFUN_CLBUF;
        /* ... twice because of double buffering */
        usb_select_endpoint(idx);
        ISP1583_DFLOW_CTRLFUN |= DFLOW_CTRLFUN_CLBUF;
    }

    struct usb_endpoint *ep;
    ep = &(endpoints[epidx_n(idx)]);
    ep->halt[epidx_dir(idx)] = 0;
    ep->enabled[epidx_dir(idx)] = 0;
    ep->out_in_progress = 0;
    ep->in_min_len = -1;
    ep->in_ack = 0;
    ep->type = type;
    ep->max_pkt_size[epidx_dir(idx)] = max_pkt_size;
}

static void usb_enable_endpoint(int idx)
{
    if(epidx_n(idx)!=EP_CONTROL)
    {
        usb_select_endpoint(idx);
        /* Enable interrupt */
        or_int_value(&ISP1583_INIT_INTEN_A, &ISP1583_INIT_INTEN_B, ISP1583_INIT_INTEN_READ, 1 << (10 + idx));
        /* Enable endpoint */
        ISP1583_DFLOW_EPTYPE |= DFLOW_EPTYPE_ENABLE;
    }

    endpoints[epidx_n(idx)].enabled[epidx_dir(idx)] = 1;
}
/*
static void usb_disable_endpoint(int idx, bool set_struct)
{
    usb_select_endpoint(idx);
    ISP1583_DFLOW_EPTYPE &= ~DFLOW_EPTYPE_ENABLE;
    bc_int_value(&ISP1583_INIT_INTEN_A, &ISP1583_INIT_INTEN_B, ISP1583_INIT_INTEN_READ, 1 << (10 + idx));

    if(set_struct)
        endpoints[epidx_n(idx)].enabled[epidx_dir(idx)] = 0;
}
*/
static int usb_get_packet(unsigned char *buf, int max_len)
{
    int len, i;
    len = ISP1583_DFLOW_BUFLEN;

    if (max_len < 0 || max_len > len)
        max_len = len;

    i = 0;
    while (i < len)
    {
        unsigned short d = ISP1583_DFLOW_DATA;
        if (i < max_len)
            buf[i] = d & 0xff;
        i++;
        if (i < max_len)
            buf[i] = (d >> 8) & 0xff;
        i++;
    }
    return max_len;
}

static int usb_receive(int n)
{
    logf("usb_receive(%d)", n);
    int len;

    if (endpoints[n].halt[DIR_RX]
        || !endpoints[n].enabled[DIR_RX]
        || endpoints[n].in_min_len < 0
        || !endpoints[n].in_ack)
        return -1;

    endpoints[n].in_ack = 0;

    usb_select_endpoint(ep_index(n, DIR_RX));

    len = usb_get_packet(endpoints[n].in_buf + endpoints[n].in_ptr,
                         endpoints[n].in_max_len - endpoints[n].in_ptr);
    endpoints[n].in_ptr += len;

    if (endpoints[n].in_ptr >= endpoints[n].in_min_len)
    {
        endpoints[n].in_min_len = -1;
        if (endpoints[n].in_done)
            (*(endpoints[n].in_done))(n, endpoints[n].in_buf,
                                      endpoints[n].in_ptr);
    }
    logf("receive_end");
    return 0;
}

static bool usb_out_buffer_full(int ep)
{
    usb_select_endpoint(ep_index(ep, DIR_TX));
    if (ISP1583_DFLOW_EPTYPE & 4)   /* Check if type=bulk and double buffering is set */
        return (ISP1583_DFLOW_BUFSTAT & 3) == 3; /* Return true if both buffers are filled */
    else
        return (ISP1583_DFLOW_BUFSTAT & 3) != 0; /* Return true if one of the buffers are filled */
}

static int usb_send(int n)
{
    logf("usb_send(%d)", n);
    int max_pkt_size, len;
    int i;
    unsigned char *p;

    if (endpoints[n].halt[DIR_TX]
        || !endpoints[n].enabled[DIR_TX]
        || !endpoints[n].out_in_progress)
    {
        logf("NOT SEND TO EP!");
        return -1;
    }

    if (endpoints[n].out_ptr < 0)
    {
        endpoints[n].out_in_progress = 0;
        if (endpoints[n].out_done)
            (*(endpoints[n].out_done))(n, endpoints[n].out_buf,
                                       endpoints[n].out_len);
        logf("ALREADY SENT TO EP!");
        return -1;
    }

    if (usb_out_buffer_full(n))
    {
        logf("BUFFER FULL!");
        return -1;
    }

    usb_select_endpoint(ep_index(n, DIR_TX));
    max_pkt_size = endpoints[n].max_pkt_size[DIR_TX];
    len = endpoints[n].out_len - endpoints[n].out_ptr;
    if (len > max_pkt_size)
        len = max_pkt_size;

    if(len < max_pkt_size)
        ISP1583_DFLOW_BUFLEN = len;

    p = endpoints[n].out_buf + endpoints[n].out_ptr;
    i = 0;
    while (len - i >= 2)
    {
        ISP1583_DFLOW_DATA = p[i] | (p[i + 1] << 8);
        i += 2;
    }
    if (i < len)
        ISP1583_DFLOW_DATA = p[i];

    endpoints[n].out_ptr += len;

/*
    if (endpoints[n].out_ptr == endpoints[n].out_len
        && len < max_pkt_size)
*/
    if (endpoints[n].out_ptr == endpoints[n].out_len)
        endpoints[n].out_ptr = -1;

    logf("send_end");
    return 0;
}

static void usb_stall_endpoint(int idx)
{
    usb_select_endpoint(idx);
    ISP1583_DFLOW_CTRLFUN |= DFLOW_CTRLFUN_STALL;
    endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 1;
}

static void usb_unstall_endpoint(int idx)
{
    usb_select_endpoint(idx);
    ISP1583_DFLOW_CTRLFUN &= ~DFLOW_CTRLFUN_STALL;
    ISP1583_DFLOW_EPTYPE &= ~DFLOW_EPTYPE_ENABLE;
    ISP1583_DFLOW_EPTYPE |= DFLOW_EPTYPE_ENABLE;
    ISP1583_DFLOW_CTRLFUN |= DFLOW_CTRLFUN_CLBUF;
    if (epidx_dir(idx) == DIR_TX)
        endpoints[epidx_n(idx)].out_in_progress = 0;
    else
    {
        endpoints[epidx_n(idx)].in_min_len = -1;
        endpoints[epidx_n(idx)].in_ack = 0;
    }
    endpoints[epidx_n(idx)].halt[epidx_dir(idx)] = 0;
}

static void usb_status_ack(int ep, int dir)
{
    logf("usb_status_ack(%d)", dir);
    if(ep == EP_CONTROL)
        usb_select_setup_endpoint();
    else
        usb_select_endpoint(ep_index(ep, dir));

    ISP1583_DFLOW_CTRLFUN |= DFLOW_CTRLFUN_STATUS;
}

static void usb_data_stage_enable(int ep, int dir)
{
    logf("usb_data_stage_enable(%d)", dir);
    usb_select_endpoint(ep_index(ep, dir));
    ISP1583_DFLOW_CTRLFUN |= DFLOW_CTRLFUN_DSEN;
}

static void usb_handle_setup_rx(void)
{
    int len;
    usb_select_setup_endpoint();
    len = usb_get_packet(setup_pkt_buf, 8);

    if (len == 8)
    {
        ISP1583_DFLOW_CTRLFUN |= DFLOW_CTRLFUN_STATUS; /* Acknowledge packet */
        usb_core_control_request((struct usb_ctrlrequest*)setup_pkt_buf);
    }
    else
    {
        usb_drv_stall(EP_CONTROL, true, false);
        usb_drv_stall(EP_CONTROL, true, true);
        logf("usb_handle_setup_rx() failed");
        return;
    }

    logf("usb_handle_setup_rx(): %02x %02x %02x %02x %02x %02x %02x %02x", setup_pkt_buf[0], setup_pkt_buf[1], setup_pkt_buf[2], setup_pkt_buf[3], setup_pkt_buf[4], setup_pkt_buf[5], setup_pkt_buf[6], setup_pkt_buf[7]);
}

static void usb_handle_data_int(int ep, int dir)
{
    int len;
    if (dir == DIR_TX)
        len = usb_send(ep);
    else
    {
        len = usb_receive(ep);
        endpoints[ep].in_ack = 1;
    }
    logf("usb_handle_data_int(%d, %d) finished", ep, dir);
    (void)len;
}

bool usb_drv_powered(void)
{
#if 0
    return (ISP1583_INIT_OTG & INIT_OTG_BSESS_VALID) ? true : false;
#else
    return (ISP1583_INIT_MODE & INIT_MODE_VBUSSTAT) ? true : false;
#endif
}

static void setup_endpoints(void)
{
    int i;
    int max_pkt_size = (high_speed_mode ? 512 : 64);

    usb_setup_endpoint(ep_index(EP_CONTROL, DIR_RX), 64,
            USB_ENDPOINT_XFER_CONTROL);
    usb_setup_endpoint(ep_index(EP_CONTROL, DIR_TX), 64,
            USB_ENDPOINT_XFER_CONTROL);

    for(i = 1; i < USB_NUM_ENDPOINTS-1; i++)
    {
        usb_setup_endpoint(ep_index(i, DIR_RX), max_pkt_size,
                USB_ENDPOINT_XFER_BULK);
        usb_setup_endpoint(ep_index(i, DIR_TX), max_pkt_size,
                USB_ENDPOINT_XFER_BULK);
    }

    usb_enable_endpoint(ep_index(EP_CONTROL, DIR_RX));
    usb_enable_endpoint(ep_index(EP_CONTROL, DIR_TX));

    for (i = 1; i < USB_NUM_ENDPOINTS-1; i++)
    {
        usb_enable_endpoint(ep_index(i, DIR_RX));
        usb_enable_endpoint(ep_index(i, DIR_TX));
    }

    ZVM_SPECIFIC;
}

#if 0 /* currently unused */
static void usb_helper(void)
{
    if(ISP1583_GEN_INT_READ & ISP1583_INIT_INTEN_READ)
    {
        logf("Helper detected interrupt... [%d]", (int)current_tick);
        usb_drv_int();
    }
}
#endif

void usb_drv_init(void)
{
    /* Disable interrupt at CPU level */
    DIS_INT_CPU_TARGET;

    /* Unlock the device's registers */
    ISP1583_GEN_UNLCKDEV = ISP1583_UNLOCK_CODE;

    /* Soft reset the device */
    ISP1583_INIT_MODE = INIT_MODE_SFRESET;
    sleep(10);
    /* Enable CLKAON & GLINTENA */
    ISP1583_INIT_MODE = STANDARD_INIT_MODE;

    /* Disable all OTG functions */
    ISP1583_INIT_OTG = 0;

#ifdef DEBUG
    logf("BUS_CONF/DA0:%d MODE0/DA1: %d MODE1: %d", (bool)(ISP1583_INIT_MODE & INIT_MODE_TEST0), (bool)(ISP1583_INIT_MODE & INIT_MODE_TEST1), (bool)(ISP1583_INIT_MODE & INIT_MODE_TEST2));
    logf("Chip ID: 0x%x", ISP1583_GEN_CHIPID);
    //logf("INV0: 0x% IRQEDGE: 0x%x IRQPORT: 0x%x", IO_GIO_INV0, IO_GIO_IRQEDGE, IO_GIO_IRQPORT);
#endif

    /*Set interrupt generation to target-specific mode +
     * Set the control pipe to ACK only interrupt +
     * Set the IN pipe to ACK only interrupt +
     * Set OUT pipe to ACK and NYET interrupt
     */

    ISP1583_INIT_INTCONF = 0x54 | INT_CONF_TARGET;
    /* Clear all interrupts */
    set_int_value(ISP1583_GEN_INT_A, ISP1583_GEN_INT_B, 0xFFFFFFFF);
    /* Enable USB interrupts */
    set_int_value(ISP1583_INIT_INTEN_A, ISP1583_INIT_INTEN_B, STANDARD_INTEN);

    ZVM_SPECIFIC;

    /* Enable interrupt at CPU level */
    EN_INT_CPU_TARGET;

    setup_endpoints();

    /* Clear device address and disable it */
    ISP1583_INIT_ADDRESS = 0;

    /* Turn SoftConnect on */
    ISP1583_INIT_MODE |= INIT_MODE_SOFTCT;

    ZVM_SPECIFIC;

    //tick_add_task(usb_helper);

    logf("usb_init_device() finished");
}

int usb_drv_port_speed(void)
{
    return (int)high_speed_mode;
}

void usb_drv_exit(void)
{
    logf("usb_drv_exit()");

    /* Disable device */
    ISP1583_INIT_MODE &= ~INIT_MODE_SOFTCT;
    ISP1583_INIT_ADDRESS = 0;

    /* Disable interrupts */
    set_int_value(ISP1583_INIT_INTEN_A, ISP1583_INIT_INTEN_B, 0);
    /* and the CPU's one... */
    DIS_INT_CPU_TARGET;


    /* Send usb controller to suspend mode */
    ISP1583_INIT_MODE = INIT_MODE_GOSUSP;
    ISP1583_INIT_MODE = 0;

    //tick_remove_task(usb_helper);

    ZVM_SPECIFIC;
}

void usb_drv_stall(int endpoint, bool stall, bool in)
{
    logf("%sstall EP%d %s", (stall ? "" : "un"), endpoint, (in ? "RX" : "TX" ));
    if (stall)
        usb_stall_endpoint(ep_index(endpoint, (int)in));
    else
        usb_unstall_endpoint(ep_index(endpoint, (int)in));
}

bool usb_drv_stalled(int endpoint, bool in)
{
    return (endpoints[endpoint].halt[(int)in] == 1);
}

static void out_callback(int ep, unsigned char *buf, int len)
{
    (void)buf;
    logf("out_callback(%d, 0x%x, %d)", ep, (int)buf, len);
    usb_status_ack(ep, DIR_RX);
    usb_core_transfer_complete(ep, true, 0, len); /* 0=>status succeeded, haven't worked out status failed yet... */
}

static void in_callback(int ep, unsigned char *buf, int len)
{
    (void)buf;
    logf("in_callback(%d, 0x%x, %d)", ep, (int)buf, len);
    usb_status_ack(ep, DIR_TX);
    usb_core_transfer_complete(ep, false, 0, len);
}

int usb_drv_recv(int ep, void* ptr, int length)
{
    logf("usb_drv_recv(%d, 0x%x, %d)", ep, (int)ptr, length);
    if(ep == EP_CONTROL && length == 0 && ptr == NULL)
    {
        usb_status_ack(ep, DIR_TX);
        return 0;
    }
    endpoints[ep].in_done = in_callback;
    endpoints[ep].in_buf = ptr;
    endpoints[ep].in_max_len = length;
    endpoints[ep].in_min_len = length;
    endpoints[ep].in_ptr = 0;
    if(ep == EP_CONTROL)
    {
        usb_data_stage_enable(ep, DIR_RX);
        return usb_receive(ep);
    }
    else
        return usb_receive(ep);
}

int usb_drv_send_nonblocking(int ep, void* ptr, int length)
{
    /* First implement DMA... */
    return usb_drv_send(ep, ptr, length);
}

static void usb_drv_wait(int ep, bool send)
{
    logf("usb_drv_wait(%d, %d)", ep, send);
    if(send)
    {
        while (endpoints[ep].out_in_progress)
            nop_f();
    }
    else
    {
        while (endpoints[ep].in_ack)
            nop_f();
    }
}

int usb_drv_send(int ep, void* ptr, int length)
{
    logf("usb_drv_send_nb(%d, 0x%x, %d)", ep, (int)ptr, length);
    if(ep == EP_CONTROL && length == 0 && ptr == NULL)
    {
        usb_status_ack(ep, DIR_RX);
        return 0;
    }
    if(endpoints[ep].out_in_progress == 1)
        return -1;
    endpoints[ep].out_done = out_callback;
    endpoints[ep].out_buf = ptr;
    endpoints[ep].out_len = length;
    endpoints[ep].out_ptr = 0;
    endpoints[ep].out_in_progress = 1;
    if(ep == EP_CONTROL)
    {
        int rc = usb_send(ep);
        usb_data_stage_enable(ep, DIR_TX);
        usb_drv_wait(ep, DIR_TX);
        return rc;
    }
    else
        return usb_send(ep);
}

void usb_drv_reset_endpoint(int ep, bool send)
{
    logf("reset endpoint(%d, %d)", ep, send);
    usb_setup_endpoint(ep_index(ep, (int)send), endpoints[ep].max_pkt_size[(int)send], endpoints[ep].type);
    usb_enable_endpoint(ep_index(ep, (int)send));
}

void usb_drv_cancel_all_transfers(void)
{
    logf("usb_drv_cancel_all_tranfers()");
    int i;

    for(i=0;i<USB_NUM_ENDPOINTS-1;i++)
        endpoints[i].halt[0] = endpoints[i].halt[1] = 1;
}

int usb_drv_request_endpoint(int type, int dir)
{
    int i, bit;

    if (type != USB_ENDPOINT_XFER_BULK)
        return -1;

    bit=(dir & USB_DIR_IN)? 2:1;

    for (i=1; i < USB_NUM_ENDPOINTS; i++) {
        if((endpoints[i].allocation & bit)!=0)
            continue;
        endpoints[i].allocation |= bit;
        return i | dir;
    }

    return -1;
}

void usb_drv_release_endpoint(int ep)
{
    int mask = (ep & USB_DIR_IN)? ~2:~1;
    endpoints[ep & 0x7f].allocation &= mask;
}

static void bus_reset(void)
{
    /* Enable CLKAON & GLINTENA */
    ISP1583_INIT_MODE = STANDARD_INIT_MODE;
    /* Enable USB interrupts */
    ISP1583_INIT_INTCONF = 0x54 | INT_CONF_TARGET;
    set_int_value(ISP1583_INIT_INTEN_A, ISP1583_INIT_INTEN_B, STANDARD_INTEN);

    /* Disable all OTG functions */
    ISP1583_INIT_OTG = 0;

    /* Clear device address and enable it */
    ISP1583_INIT_ADDRESS = INIT_ADDRESS_DEVEN;

    ZVM_SPECIFIC;

    /* Reset endpoints to default */
    setup_endpoints();

    logf("bus reset->done");
}

/* Method for handling interrupts, must be called from usb-<target>.c */
void IRAM_ATTR usb_drv_int(void)
{
    unsigned long ints;
    ints = ISP1583_GEN_INT_READ & ISP1583_INIT_INTEN_READ;

    if(!ints)
        return;

    /* Unlock the device's registers */
    ISP1583_GEN_UNLCKDEV = ISP1583_UNLOCK_CODE;

    //logf(" handling int [0x%lx & 0x%lx = 0x%x]", ISP1583_GEN_INT_READ, ISP1583_INIT_INTEN_READ, (int)ints);

    if(ints & INT_IEBRST) /* Bus reset */
    {
        logf("BRESET");
        high_speed_mode = false;
        bus_reset();
        usb_core_bus_reset();
        /* Mask bus reset interrupt */
        set_int_value(ISP1583_GEN_INT_A, ISP1583_GEN_INT_B, INT_IEBRST);
        return;
    }
    if(ints & INT_IEP0SETUP) /* EP0SETUP interrupt */
    {
        logf("EP0SETUP");
        usb_handle_setup_rx();
    }
    if(ints & INT_IEHS_STA) /* change from full-speed to high-speed mode -> endpoints need to get reconfigured!! */
    {
        logf("HS_STA");
        high_speed_mode = true;
        setup_endpoints();
    }
    if(ints & INT_EP_MASK) /* Endpoints interrupt */
    {
        unsigned long ep_event;
        unsigned short i = 10;
        ep_event = ints & INT_EP_MASK;
        while(ep_event)
        {
            if(i>25)
                break;

            if(ep_event & (1 << i))
            {
                logf("EP%d %s interrupt", (i - 10) / 2, i % 2 ? "RX" : "TX");
                usb_handle_data_int((i - 10) / 2, i % 2);
                ep_event &= ~(1 << i);
            }
            i++;
        }
    }
    if(ints & INT_IERESM && !(ints & INT_IESUSP)) /* Resume status: status change from suspend to resume (active) */
    {
        logf("RESM");
    }
    if(ints & INT_IESUSP && !(ints & INT_IERESM)) /* Suspend status: status change from active to suspend */
    {
        logf("SUSP");
    }
    if(ints & INT_IEDMA) /* change in the DMA Interrupt Reason register */
    {
        logf("DMA");
    }
    if(ints & INT_IEVBUS) /* transition from LOW to HIGH on VBUS */
    {
        logf("VBUS");
    }
    /* Mask all (enabled) interrupts */
    set_int_value(ISP1583_GEN_INT_A, ISP1583_GEN_INT_B, ints);

    ZVM_SPECIFIC;
}

void usb_drv_set_address(int address)
{
    logf("usb_drv_set_address(0x%x)", address);
    ISP1583_INIT_ADDRESS = (address & 0x7F) | INIT_ADDRESS_DEVEN;

    ZVM_SPECIFIC;
}

void usb_drv_set_test_mode(int mode)
{
    logf("usb_drv_set_test_mode(%d)", mode);
    switch(mode){
        case 0:
            ISP1583_GEN_TSTMOD = 0;
            /* Power cycle... */
            break;
        case 1:
            ISP1583_GEN_TSTMOD = GEN_TSTMOD_JSTATE;
            break;
        case 2:
            ISP1583_GEN_TSTMOD = GEN_TSTMOD_KSTATE;
            break;
        case 3:
            ISP1583_GEN_TSTMOD = GEN_TSTMOD_SE0_NAK;
            break;
        case 4:
            //REG_PORTSC1 |= PORTSCX_PTC_PACKET;
            break;
        case 5:
            //REG_PORTSC1 |= PORTSCX_PTC_FORCE_EN;
            break;
    }
}

#ifndef BOOTLOADER
int dbg_usb_num_items(void)
{
    return 2+USB_NUM_ENDPOINTS*2;
}

const char* dbg_usb_item(int selected_item, void *data,
                         char *buffer, size_t buffer_len)
{
    if(selected_item < 2)
    {
        switch(selected_item)
        {
            case 0:
                snprintf(buffer, buffer_len, "USB connected: %s", (usb_drv_connected() ? "Yes" : "No"));
                return buffer;
            case 1:
                snprintf(buffer, buffer_len, "HS mode: %s", (high_speed_mode ? "Yes" : "No"));
                return buffer;
        }
    }
    else
    {
        int n = ep_index((selected_item - 2) / 2, (selected_item - 2) % 2);
        if(endpoints[n].enabled == false)
            snprintf(buffer, buffer_len, "EP%d[%s]: DISABLED", epidx_n(n), (epidx_dir(n) ? "TX" : "RX"));
        else
        {
            if(epidx_dir(n))
            {
                if(endpoints[n].out_in_progress)
                    snprintf(buffer, buffer_len, "EP%d[TX]: TRANSFERRING DATA -> %d bytes/%d bytes", epidx_n(n), (endpoints[n].out_len - endpoints[n].out_ptr), endpoints[n].out_len);
                else
                    snprintf(buffer, buffer_len, "EP%d[TX]: STANDBY", epidx_n(n));
            }
            else
            {
                if(endpoints[n].in_buf && !endpoints[n].in_ack)
                    snprintf(buffer, buffer_len, "EP%d[RX]: RECEIVING DATA -> %d bytes/%d bytes", epidx_n(n), endpoints[n].in_ptr, endpoints[n].in_max_len);
                else
                    snprintf(buffer, buffer_len, "EP%d[RX]: STANDBY", epidx_n(n));
            }
        }
        return buffer;
    }
    return NULL;
    (void)data;
}
#endif