/
mt6260_usbdev.c
2671 lines (2183 loc) · 76.9 KB
/
mt6260_usbdev.c
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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* arch/arm/src/mt6260/mt6260_usbdev.c
*
* Copyright (C) 2008-2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*******************************************************************************/
/*******************************************************************************
* Included Files
*******************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/kmalloc.h>
#include <nuttx/usb/usb.h>
#include <nuttx/usb/usbdev.h>
#include <nuttx/usb/usbdev_trace.h>
#include <arch/irq.h>
#include <arch/board/board.h>
#include "chip.h"
#include "up_arch.h"
#include "up_internal.h"
#include "mt6260_usb.h"
/*******************************************************************************
* Pre-Processor Definitions
*******************************************************************************/
/* Configuration ***************************************************************/
#ifndef CONFIG_USBDEV_MAXPOWER
# define CONFIG_USBDEV_MAXPOWER 100 /* mA */
#endif
/* Verify the selected USB attach interrupt GIO line */
//#if CONFIG_MT6260_GIO_USBATTACH < 0 || CONFIG_MT6260_GIO_USBATTACH > 15
//# error "CONFIG_MT6260_GIO_USBATTACH invalid"
//#endif
/* Calculate the IRQ number associated with this GIO */
//#define IRQ_USBATTACH (MT6260_IRQ_EXT0+CONFIG_MT6260_GIO_USBATTACH)
/* Vendor ID & Product ID of the USB device */
#ifndef CONFIG_MT6260_VENDORID
# define CONFIG_MT6260_VENDORID 0xd320
#endif
#ifndef CONFIG_MT6260_PRODUCTID
# define CONFIG_MT6260_PRODUCTID 0x3211
#endif
/* Extremely detailed register debug that you would normally never want
* enabled.
*/
#undef CONFIG_MT6260_USBDEV_REGDEBUG
/* Debug ***********************************************************************/
/* Trace error codes */
#define MT6260_TRACEERR_ALLOCFAIL 0x0001
#define MT6260_TRACEERR_ATTACHIRQREG 0x0002
#define MT6260_TRACEERR_BINDFAILED 0x0003
#define MT6260_TRACEERR_COREIRQREG 0x0004
#define MT6260_TRACEERR_DRIVER 0x0005
#define MT6260_TRACEERR_DRIVERREGISTERED 0x0006
#define MT6260_TRACEERR_EPREAD 0x0007
#define MT6260_TRACEERR_EWRITE 0x0008
#define MT6260_TRACEERR_INVALIDPARMS 0x0009
#define MT6260_TRACEERR_NOEP 0x000a
#define MT6260_TRACEERR_NOTCONFIGURED 0x000b
#define MT6260_TRACEERR_NULLPACKET 0x000c
#define MT6260_TRACEERR_NULLREQUEST 0x000d
#define MT6260_TRACEERR_REQABORTED 0x000e
#define MT6260_TRACEERR_STALLEDCLRFEATURE 0x000f
#define MT6260_TRACEERR_STALLEDISPATCH 0x0010
#define MT6260_TRACEERR_STALLEDGETST 0x0011
#define MT6260_TRACEERR_STALLEDGETSTEP 0x0012
#define MT6260_TRACEERR_STALLEDGETSTRECIP 0x0013
#define MT6260_TRACEERR_STALLEDREQUEST 0x0014
#define MT6260_TRACEERR_STALLEDSETFEATURE 0x0015
#ifdef CONFIG_USBDEV_TRACE_STRINGS
const struct trace_msg_t g_usb_trace_strings_deverror[] =
{
TRACE_STR(MT6260_TRACEERR_ALLOCFAIL ),
TRACE_STR(MT6260_TRACEERR_ATTACHIRQREG ),
TRACE_STR(MT6260_TRACEERR_BINDFAILED ),
TRACE_STR(MT6260_TRACEERR_COREIRQREG ),
TRACE_STR(MT6260_TRACEERR_DRIVER ),
TRACE_STR(MT6260_TRACEERR_DRIVERREGISTERED ),
TRACE_STR(MT6260_TRACEERR_EPREAD ),
TRACE_STR(MT6260_TRACEERR_EWRITE ),
TRACE_STR(MT6260_TRACEERR_INVALIDPARMS ),
TRACE_STR(MT6260_TRACEERR_NOEP ),
TRACE_STR(MT6260_TRACEERR_NOTCONFIGURED ),
TRACE_STR(MT6260_TRACEERR_NULLPACKET ),
TRACE_STR(MT6260_TRACEERR_NULLREQUEST ),
TRACE_STR(MT6260_TRACEERR_REQABORTED ),
TRACE_STR(MT6260_TRACEERR_STALLEDCLRFEATURE ),
TRACE_STR(MT6260_TRACEERR_STALLEDISPATCH ),
TRACE_STR(MT6260_TRACEERR_STALLEDGETST ),
TRACE_STR(MT6260_TRACEERR_STALLEDGETSTEP ),
TRACE_STR(MT6260_TRACEERR_STALLEDGETSTRECIP ),
TRACE_STR(MT6260_TRACEERR_STALLEDREQUEST ),
TRACE_STR(MT6260_TRACEERR_STALLEDSETFEATURE ),
TRACE_STR_END
};
#endif
/* Trace interrupt codes */
#define MT6260_TRACEINTID_ATTACHED 0x0001
#define MT6260_TRACEINTID_ATTACH 0x0002
#define MT6260_TRACEINTID_CLEARFEATURE 0x0003
#define MT6260_TRACEINTID_POWERDOWN 0x0004
#define MT6260_TRACEINTID_CONTROL 0x0005
#define MT6260_TRACEINTID_DETACHED 0x0006
#define MT6260_TRACEINTID_DISCONNECTED 0x0007
#define MT6260_TRACEINTID_DISPATCH 0x0008
#define MT6260_TRACEINTID_GETENDPOINT 0x0009
#define MT6260_TRACEINTID_GETIFDEV 0x000a
#define MT6260_TRACEINTID_GETSETDESC 0x000b
#define MT6260_TRACEINTID_GETSETIFCONFIG 0x000c
#define MT6260_TRACEINTID_GETSTATUS 0x000d
#define MT6260_TRACEINTID_RESET 0x000e
#define MT6260_TRACEINTID_RESUME 0x000f
#define MT6260_TRACEINTID_OUTFIFO 0x0010
#define MT6260_TRACEINTID_OUTPKTRDY 0x0011
#define MT6260_TRACEINTID_SESSRQ 0x0012
#define MT6260_TRACEINTID_SETADDRESS 0x0013
#define MT6260_TRACEINTID_SETFEATURE 0x0014
#define MT6260_TRACEINTID_SOF 0x0015
#define MT6260_TRACEINTID_SUSPEND 0x0016
#define MT6260_TRACEINTID_SYNCHFRAME 0x0017
#define MT6260_TRACEINTID_TESTMODE 0x0018
#define MT6260_TRACEINTID_INFIFO 0x0019
#define MT6260_TRACEINTID_INFIFOSETEND 0x001a
#define MT6260_TRACEINTID_INFIFOSTALL 0x001b
#define MT6260_TRACEINTID_INPKTRDY 0x001c
#define MT6260_TRACEINTID_UNKNOWN 0x001d
#define MT6260_TRACEINTID_USBCTLR 0x001d
#define MT6260_TRACEINTID_VBUSERR 0x001f
#ifdef CONFIG_USBDEV_TRACE_STRINGS
const struct trace_msg_t g_usb_trace_strings_intdecode[] =
{
TRACE_STR(MT6260_TRACEINTID_ATTACHED ),
TRACE_STR(MT6260_TRACEINTID_ATTACH ),
TRACE_STR(MT6260_TRACEINTID_CLEARFEATURE ),
TRACE_STR(MT6260_TRACEINTID_POWERDOWN ),
TRACE_STR(MT6260_TRACEINTID_CONTROL ),
TRACE_STR(MT6260_TRACEINTID_DETACHED ),
TRACE_STR(MT6260_TRACEINTID_DISCONNECTED ),
TRACE_STR(MT6260_TRACEINTID_DISPATCH ),
TRACE_STR(MT6260_TRACEINTID_GETENDPOINT ),
TRACE_STR(MT6260_TRACEINTID_GETIFDEV ),
TRACE_STR(MT6260_TRACEINTID_GETSETDESC ),
TRACE_STR(MT6260_TRACEINTID_GETSETIFCONFIG ),
TRACE_STR(MT6260_TRACEINTID_GETSTATUS ),
TRACE_STR(MT6260_TRACEINTID_RESET ),
TRACE_STR(MT6260_TRACEINTID_RESUME ),
TRACE_STR(MT6260_TRACEINTID_OUTFIFO ),
TRACE_STR(MT6260_TRACEINTID_OUTPKTRDY ),
TRACE_STR(MT6260_TRACEINTID_SESSRQ ),
TRACE_STR(MT6260_TRACEINTID_SETADDRESS ),
TRACE_STR(MT6260_TRACEINTID_SETFEATURE ),
TRACE_STR(MT6260_TRACEINTID_SOF ),
TRACE_STR(MT6260_TRACEINTID_SUSPEND ),
TRACE_STR(MT6260_TRACEINTID_SYNCHFRAME ),
TRACE_STR(MT6260_TRACEINTID_TESTMODE ),
TRACE_STR(MT6260_TRACEINTID_INFIFO ),
TRACE_STR(MT6260_TRACEINTID_INFIFOSETEND ),
TRACE_STR(MT6260_TRACEINTID_INFIFOSTALL ),
TRACE_STR(MT6260_TRACEINTID_INPKTRDY ),
TRACE_STR(MT6260_TRACEINTID_UNKNOWN ),
TRACE_STR(MT6260_TRACEINTID_USBCTLR ),
TRACE_STR(MT6260_TRACEINTID_VBUSERR ),
TRACE_STR_END
};
#endif
/* Hardware interface **********************************************************/
/* The MT6260 hardware supports 8 configurable endpoints EP1-4, IN and OUT
* (in addition to EP0 IN and OUT). This driver, however, does not exploit
* the full configuratability of the hardware at this time but, instead,
* supports the one interrupt IN, one bulk IN and one bulk OUT endpoint.
*/
/* Hardware dependent sizes and numbers */
#define MT6260_EP0MAXPACKET 64 /* EP0 max packet size */
#define MT6260_BULKMAXPACKET 64 /* Bulk endpoint max packet */
#define MT6260_INTRMAXPACKET 16 /* Interrupt endpoint max packet */
#define MT6260_NENDPOINTS 4 /* Includes EP0 */
/* Endpoint numbers */
#define MT6260_EP0 0 /* Control endpoint */
#define MT6260_EPBULKIN 1 /* Bulk EP for send to host */
#define MT6260_EPBULKOUT 2 /* Bulk EP for recv to host */
#define MT6260_EPINTRIN 3 /* Intr EP for host poll */
/* Request queue operations ****************************************************/
#define mt6260_rqempty(ep) ((ep)->head == NULL)
#define mt6260_rqpeek(ep) ((ep)->head)
/*******************************************************************************
* Private Types
*******************************************************************************/
/* A container for a request so that the request make be retained in a list */
struct mt6260_req_s
{
struct usbdev_req_s req; /* Standard USB request */
struct mt6260_req_s *flink; /* Supports a singly linked list */
};
/* This is the internal representation of an endpoint */
struct mt6260_ep_s
{
/* Common endpoint fields. This must be the first thing defined in the
* structure so that it is possible to simply cast from struct usbdev_ep_s
* to struct mt6260_ep_s.
*/
struct usbdev_ep_s ep; /* Standard endpoint structure */
/* MT6260-specific fields */
struct mt6260_usbdev_s *dev; /* Reference to private driver data */
struct mt6260_req_s *head; /* Request list for this endpoint */
struct mt6260_req_s *tail;
uint8_t epphy; /* Physical EP address/index */
uint8_t stalled:1; /* Endpoint is halted */
uint8_t in:1; /* Endpoint is IN only */
uint8_t halted:1; /* Endpoint feature halted */
uint8_t txnullpkt:1; /* Null packet needed at end of transfer */
};
/* This structure encapsulates the overall driver state */
struct mt6260_usbdev_s
{
/* Common device fields. This must be the first thing defined in the
* structure so that it is possible to simply cast from struct usbdev_s
* to struct mt6260_usbdev_s.
*/
struct usbdev_s usbdev;
/* The bound device class driver */
struct usbdevclass_driver_s *driver;
/* MT6260-specific fields */
uint8_t stalled:1; /* 1: Protocol stalled */
uint8_t selfpowered:1; /* 1: Device is self powered */
uint8_t paddrset:1; /* 1: Peripheral addr has been set */
uint8_t attached:1; /* 1: Host attached */
uint8_t rxpending:1; /* 1: RX pending */
uint8_t paddr; /* Peripheral address */
/* The endpoint list */
struct mt6260_ep_s eplist[MT6260_NENDPOINTS];
};
/* For maintaining tables of endpoint info */
struct mt6260_epinfo_s
{
uint8_t addr; /* Logical endpoint address */
uint8_t attr; /* Endpoint attributes */
uint8_t maxpacket; /* Max packet size */
};
/*******************************************************************************
* Private Function Prototypes
*******************************************************************************/
/* Register operations */
#if defined(CONFIG_MT6260_USBDEV_REGDEBUG) && defined(CONFIG_DEBUG)
static uint8_t mt6260_getreg8(uint32_t addr);
static uint16_t mt6260_getreg16(uint32_t addr);
static uint32_t mt6260_getreg32(uint32_t addr);
static void mt6260_putreg8(uint8_t val, uint32_t addr);
static void mt6260_putreg16(uint16_t val, uint32_t addr);
static void mt6260_putreg32(uint32_t val, uint32_t addr);
#else
# define mt6260_getreg8(addr) getreg8(addr)
# define mt6260_getreg16(addr) getreg16(addr)
# define mt6260_getreg32(addr) getreg32(addr)
# define mt6260_putreg8(val,addr) putreg8(val,addr)
# define mt6260_putreg16(val,addr) putreg16(val,addr)
# define mt6260_putreg32(val,addr) putreg32(val,addr)
#endif
/* Request queue operations ****************************************************/
static FAR struct mt6260_req_s *mt6260_rqdequeue(FAR struct mt6260_ep_s *privep);
static void mt6260_rqenqueue(FAR struct mt6260_ep_s *privep, FAR struct mt6260_req_s *req);
/* Low level data transfers and request operations */
static int mt6260_ep0write(uint8_t *buf, uint16_t nbytes);
static int mt6260_epwrite(uint8_t epphy, uint8_t *buf, uint16_t nbytes);
static int mt6260_epread(uint8_t epphy, uint8_t *buf, uint16_t nbytes);
static inline void mt6260_abortrequest(struct mt6260_ep_s *privep,
struct mt6260_req_s *privreq, int16_t result);
static void mt6260_reqcomplete(struct mt6260_ep_s *privep, int16_t result);
static int mt6260_wrrequest(struct mt6260_ep_s *privep);
static int mt6260_rdrequest(struct mt6260_ep_s *privep);
static void mt6260_cancelrequests(struct mt6260_ep_s *privep);
/* Interrupt handling */
static struct mt6260_ep_s *mt6260_epfindbyaddr(struct mt6260_usbdev_s *priv,
uint16_t eplog);
static void mt6260_dispatchrequest(struct mt6260_usbdev_s *priv,
const struct usb_ctrlreq_s *ctrl);
static inline void mt6260_ep0setup(struct mt6260_usbdev_s *priv);
static inline uint32_t mt6260_highestpriinterrupt(int intstatus);
static int mt6260_ctlrinterrupt(int irq, FAR void *context);
//static int mt6260_attachinterrupt(int irq, FAR void *context);
/* Initialization operations */
static void mt6260_epreset(unsigned int index);
static inline void mt6260_epinitialize(struct mt6260_usbdev_s *priv);
static void mt6260_ctrlinitialize(struct mt6260_usbdev_s *priv);
/* Endpoint methods */
static int mt6260_epconfigure(FAR struct usbdev_ep_s *ep,
const struct usb_epdesc_s *desc, bool last);
static int mt6260_epdisable(FAR struct usbdev_ep_s *ep);
static FAR struct usbdev_req_s *mt6260_epallocreq(FAR struct usbdev_ep_s *ep);
static void mt6260_epfreereq(FAR struct usbdev_ep_s *ep, FAR struct usbdev_req_s *req);
#ifdef CONFIG_USBDEV_DMA
static FAR void *mt6260_epallocbuffer(FAR struct usbdev_ep_s *ep, uint16_t nbytes);
static void mt6260_epfreebuffer(FAR struct usbdev_ep_s *ep, void *buf);
#endif
static int mt6260_epsubmit(FAR struct usbdev_ep_s *ep, struct usbdev_req_s *privreq);
static int mt6260_epcancel(FAR struct usbdev_ep_s *ep, struct usbdev_req_s *privreq);
/* USB device controller methods */
static FAR struct usbdev_ep_s *mt6260_allocep(FAR struct usbdev_s *dev,
uint8_t epno, bool in, uint8_t eptype);
static void mt6260_freeep(FAR struct usbdev_s *dev, FAR struct usbdev_ep_s *ep);
static int mt6260_getframe(struct usbdev_s *dev);
static int mt6260_wakeup(struct usbdev_s *dev);
static int mt6260_selfpowered(struct usbdev_s *dev, bool selfpowered);
static int mt6260_pullup(struct usbdev_s *dev, bool enable);
/*******************************************************************************
* Private Data
*******************************************************************************/
/* Endpoint methods */
static const struct usbdev_epops_s g_epops =
{
.configure = mt6260_epconfigure,
.disable = mt6260_epdisable,
.allocreq = mt6260_epallocreq,
.freereq = mt6260_epfreereq,
#ifdef CONFIG_USBDEV_DMA
.allocbuffer = mt6260_epallocbuffer,
.freebuffer = mt6260_epfreebuffer,
#endif
.submit = mt6260_epsubmit,
.cancel = mt6260_epcancel,
};
/* USB controller device methods */
static const struct usbdev_ops_s g_devops =
{
.allocep = mt6260_allocep,
.freeep = mt6260_freeep,
.getframe = mt6260_getframe,
.wakeup = mt6260_wakeup,
.selfpowered = mt6260_selfpowered,
.pullup = mt6260_pullup,
};
/* There is only one, single, pre-allocated instance of the driver structure */
static struct mt6260_usbdev_s g_usbdev;
/* Summarizes information about all MT6260 endpoints */
static const struct mt6260_epinfo_s g_epinfo[MT6260_NENDPOINTS] =
{
{
0, /* EP0 */
USB_EP_ATTR_XFER_CONTROL, /* Type: Control IN/OUT */
MT6260_EP0MAXPACKET /* Max packet size */
},
{
MT6260_EPBULKIN | USB_DIR_IN, /* Logical endpoint number: 1 IN */
USB_EP_ATTR_XFER_BULK, /* Type: Bulk */
MT6260_BULKMAXPACKET, /* Max packet size */
},
{
MT6260_EPBULKOUT | USB_DIR_OUT, /* Logical endpoint number: 2 OUT */
USB_EP_ATTR_XFER_BULK, /* Type: Bulk */
MT6260_BULKMAXPACKET /* Max packet size */
},
{
MT6260_EPINTRIN | USB_DIR_IN, /* Logical endpoint number: 3 IN */
USB_EP_ATTR_XFER_INT, /* Type: Interrupt */
MT6260_INTRMAXPACKET /* Max packet size */
}
};
/*******************************************************************************
* Private Functions
*******************************************************************************/
/*******************************************************************************
* Name: mt6260_getreg8
*
* Description:
* Get the contents of an MT6260 8-bit register
*
*******************************************************************************/
#if defined(CONFIG_DEBUG)
#define HEX_CHARS "0123456789abcdef"
void serial_puth(uint32_t pair, int digits)
{
if (digits >= 8)
up_lowputc(HEX_CHARS[(pair >> 28) & 0xf]);
if (digits >= 7)
up_lowputc(HEX_CHARS[(pair >> 24) & 0xf]);
if (digits >= 6)
up_lowputc(HEX_CHARS[(pair >> 20) & 0xf]);
if (digits >= 5)
up_lowputc(HEX_CHARS[(pair >> 16) & 0xf]);
if (digits >= 4)
up_lowputc(HEX_CHARS[(pair >> 12) & 0xf]);
if (digits >= 3)
up_lowputc(HEX_CHARS[(pair >> 8) & 0xf]);
if (digits >= 2)
up_lowputc(HEX_CHARS[(pair >> 4) & 0xf]);
if (digits >= 1)
up_lowputc(HEX_CHARS[(pair >> 0) & 0xf]);
}
#endif
#if defined(CONFIG_MT6260_USBDEV_REGDEBUG) && defined(CONFIG_DEBUG)
static uint8_t mt6260_getreg8(uint32_t addr)
{
static uint32_t prevaddr = 0;
static uint8_t preval = 0;
static uint32_t count = 0;
/* Read the value from the register */
uint8_t val = getreg8(addr);
/* Is this the same value that we read from the same registe last time? Are
* we polling the register? If so, suppress some of the output.
*/
if (addr == prevaddr || val == preval)
{
if (count == 0xffffffff || ++count > 3)
{
if (count == 4)
{
//lldbg("...\n");
up_lowputs("...\n");
}
return val;
}
}
/* No this is a new address or value */
else
{
/* Did we print "..." for the previous value? */
if (count > 3)
{
/* Yes.. then show how many times the value repeated */
//lldbg("[repeats %d more times]\n", count-3);
up_lowputs("[repeats 0x");
serial_puth(count - 3, 8);
up_lowputs(" more times]\n");
}
/* Save the new address, value, and count */
prevaddr = addr;
preval = val;
count = 1;
}
/* Show the register value read */
//lldbg("%08x->%02x\n", addr, val);
serial_puth(addr, 8);
up_lowputs("->");
serial_puth(val, 2);
up_lowputs("\n");
return val;
}
#endif
/*******************************************************************************
* Name: mt6260_getreg16
*
* Description:
* Get the contents of an MT6260 16-bit register
*
*******************************************************************************/
#if defined(CONFIG_MT6260_USBDEV_REGDEBUG) && defined(CONFIG_DEBUG)
static uint16_t mt6260_getreg16(uint32_t addr)
{
static uint32_t prevaddr = 0;
static uint16_t preval = 0;
static uint32_t count = 0;
/* Read the value from the register */
uint16_t val = getreg16(addr);
/* Is this the same value that we read from the same registe last time? Are
* we polling the register? If so, suppress some of the output.
*/
if (addr == prevaddr || val == preval)
{
if (count == 0xffffffff || ++count > 3)
{
if (count == 4)
{
//lldbg("...\n");
up_lowputs("...\n");
}
return val;
}
}
/* No this is a new address or value */
else
{
/* Did we print "..." for the previous value? */
if (count > 3)
{
/* Yes.. then show how many times the value repeated */
//lldbg("[repeats %d more times]\n", count-3);
up_lowputs("[repeats 0x");
serial_puth(count - 3, 8);
up_lowputs(" more times]\n");
}
/* Save the new address, value, and count */
prevaddr = addr;
preval = val;
count = 1;
}
/* Show the register value read */
//lldbg("%08x->%04x\n", addr, val);
serial_puth(addr, 8);
up_lowputs("->");
serial_puth(val, 4);
up_lowputs("\n");
return val;
}
#endif
/*******************************************************************************
* Name: mt6260_getreg32
*
* Description:
* Get the contents of an MT6260 32-bit register
*
*******************************************************************************/
#if defined(CONFIG_MT6260_USBDEV_REGDEBUG) && defined(CONFIG_DEBUG)
static uint32_t mt6260_getreg32(uint32_t addr)
{
static uint32_t prevaddr = 0;
static uint32_t preval = 0;
static uint32_t count = 0;
/* Read the value from the register */
uint32_t val = getreg32(addr);
/* Is this the same value that we read from the same registe last time? Are
* we polling the register? If so, suppress some of the output.
*/
if (addr == prevaddr || val == preval)
{
if (count == 0xffffffff || ++count > 3)
{
if (count == 4)
{
//lldbg("...\n");
up_lowputs("...\n");
}
return val;
}
}
/* No this is a new address or value */
else
{
/* Did we print "..." for the previous value? */
if (count > 3)
{
/* Yes.. then show how many times the value repeated */
//lldbg("[repeats %d more times]\n", count-3);
up_lowputs("[repeats 0x");
serial_puth(count - 3, 8);
up_lowputs(" more times]\n");
}
/* Save the new address, value, and count */
prevaddr = addr;
preval = val;
count = 1;
}
/* Show the register value read */
//lldbg("%08x->%08x\n", addr, val);
serial_puth(addr, 8);
up_lowputs("->");
serial_puth(val, 8);
up_lowputs("\n");
return val;
}
#endif
/*******************************************************************************
* Name: mt6260_putreg8
*
* Description:
* Set the contents of an MT6260 8-bit register to a value
*
*******************************************************************************/
#if defined(CONFIG_MT6260_USBDEV_REGDEBUG) && defined(CONFIG_DEBUG)
static void mt6260_putreg8(uint8_t val, uint32_t addr)
{
/* Show the register value being written */
//lldbg("%08x<-%02x\n", addr, val);
serial_puth(addr, 8);
up_lowputs("<-");
serial_puth(val, 2);
up_lowputs("\n");
/* Write the value */
putreg8(val, addr);
}
#endif
/*******************************************************************************
* Name: mt6260_putreg16
*
* Description:
* Set the contents of an MT6260 16-bit register to a value
*
*******************************************************************************/
#if defined(CONFIG_MT6260_USBDEV_REGDEBUG) && defined(CONFIG_DEBUG)
static void mt6260_putreg16(uint16_t val, uint32_t addr)
{
/* Show the register value being written */
//lldbg("%08x<-%04x\n", addr, val);
serial_puth(addr, 8);
up_lowputs("<-");
serial_puth(val, 4);
up_lowputs("\n");
/* Write the value */
putreg16(val, addr);
}
#endif
/*******************************************************************************
* Name: mt6260_putreg32
*
* Description:
* Set the contents of an MT6260 32-bit register to a value
*
*******************************************************************************/
#if defined(CONFIG_MT6260_USBDEV_REGDEBUG) && defined(CONFIG_DEBUG)
static void mt6260_putreg32(uint32_t val, uint32_t addr)
{
/* Show the register value being written */
//lldbg("%08x<-%08x\n", addr, val);
serial_puth(addr, 8);
up_lowputs("<-");
serial_puth(val, 8);
up_lowputs("\n");
/* Write the value */
putreg32(val, addr);
}
#endif
/*******************************************************************************
* Name: mt6260_rqdequeue
*
* Description:
* Remove a request from an endpoint request queue
*
*******************************************************************************/
static FAR struct mt6260_req_s *mt6260_rqdequeue(FAR struct mt6260_ep_s *privep)
{
FAR struct mt6260_req_s *ret = privep->head;
if (ret)
{
privep->head = ret->flink;
if (!privep->head)
{
privep->tail = NULL;
}
ret->flink = NULL;
}
return ret;
}
/*******************************************************************************
* Name: mt6260_rqenqueue
*
* Description:
* Add a request from an endpoint request queue
*
*******************************************************************************/
static void mt6260_rqenqueue(FAR struct mt6260_ep_s *privep,
FAR struct mt6260_req_s *req)
{
req->flink = NULL;
if (!privep->head)
{
privep->head = req;
privep->tail = req;
}
else
{
privep->tail->flink = req;
privep->tail = req;
}
}
/*******************************************************************************
* Name: mt6260_ep0write
*
* Description:
* Control endpoint write (IN)
*
*******************************************************************************/
static int mt6260_ep0write(uint8_t *buf, uint16_t nbytes)
{
uint8_t csr0 = USB_CSR0_INPKTRDY; /* XMT packet ready bit */
uint16_t bytesleft;
uint16_t nwritten;
if ( nbytes <= MT6260_EP0MAXPACKET)
{
bytesleft = nbytes;
csr0 |= USB_CSR0_DATAEND; /* Transaction end bit */
}
else
{
bytesleft = MT6260_EP0MAXPACKET;
}
nwritten = bytesleft;
while (bytesleft > 0)
{
mt6260_putreg8(*buf++, MT6260_USB_FIFO0);
bytesleft--;
}
mt6260_putreg8(csr0, MT6260_USB_CSR0);
return nwritten;
}
/*******************************************************************************
* Name: mt6260_epwrite
*
* Description:
* Endpoint write (IN)
*
*******************************************************************************/
static int mt6260_epwrite(uint8_t epphy, uint8_t *buf, uint16_t nbytes)
{
volatile uint8_t *fifo;
uint16_t bytesleft;
int ret = ERROR;
if (/*epphy < USB_EP0_SELECT || */ epphy >= MT6260_NENDPOINTS)
{
return ret;
}
mt6260_putreg8(epphy, MT6260_USB_INDEX);
if (epphy == USB_EP0_SELECT)
{
return mt6260_ep0write(buf, nbytes);
}
bytesleft = MT6260_BULKMAXPACKET;
if (bytesleft > nbytes)
{
bytesleft = nbytes;
}
ret = bytesleft;
fifo = (volatile uint8_t *)MT6260_USB_FIFO0;
fifo = fifo + (epphy << 2);
/* If the FIFO is not empty, queue a transmission and wait for it to empty out. */
if (mt6260_getreg8(MT6260_USB_INCSR1) & USB_INCSR1_FIFONOTEMP)
{
/*
* The FIFO has data in it. Set the INPKTRDY bit, which should queue
* a transfer, then wait for the INPKTRDY bit to be cleared by the
* USB system.
*/
mt6260_putreg8(USB_INCSR1_INPKTRDY, MT6260_USB_INCSR1);
while (mt6260_getreg8(MT6260_USB_INCSR1) & USB_INCSR1_INPKTRDY);
mt6260_putreg8((mt6260_getreg8(MT6260_USB_INCSR1) | USB_INCSR1_FLFIFO),
MT6260_USB_INCSR1);
}
while (bytesleft > 0)
{
*fifo = *buf++;
bytesleft--;
}
mt6260_putreg8(mt6260_getreg8(MT6260_USB_INCSR1) | USB_INCSR1_INPKTRDY,
MT6260_USB_INCSR1);
return ret;
}
/*******************************************************************************
* Name: mt6260_epread
*
* Description:
* Endpoint read (OUT)
*
*******************************************************************************/
static int mt6260_epread(uint8_t epphy, uint8_t *buf, uint16_t nbytes)
{
volatile uint8_t *fifo;
int bytesleft;
int ret = ERROR;
if (/*epphy < USB_EP0_SELECT || */ epphy >= MT6260_NENDPOINTS)
{
return ret;
}
mt6260_putreg8(epphy, MT6260_USB_INDEX);
if (epphy == USB_EP0_SELECT)
{
bytesleft = mt6260_getreg8(MT6260_USB_COUNT0);
if (bytesleft > nbytes)
{
bytesleft = nbytes;
}
}
else
{
bytesleft = mt6260_getreg8(MT6260_USB_RXCOUNT2);
bytesleft = (bytesleft << 8) + mt6260_getreg8(MT6260_USB_RXCOUNT1);
if (bytesleft > nbytes)
{
bytesleft = nbytes;
}
}
ret = bytesleft;
fifo = (uint8_t*)MT6260_USB_FIFO0;
fifo = fifo + (epphy << 2);
while (bytesleft > 0)
{
*buf++ = *fifo;
bytesleft--;
}
/* Clear OUTPKTRDY bit in _OUTCSR1 */
mt6260_putreg8(mt6260_getreg8(MT6260_USB_OUTCSR1) & ~(USB_OUTCSR1_OUTPKTRDY), MT6260_USB_OUTCSR1);
return ret;
}
/*******************************************************************************
* Name: mt6260_abortrequest
*
* Description:
* Discard a request
*
*******************************************************************************/
static inline void mt6260_abortrequest(struct mt6260_ep_s *privep,
struct mt6260_req_s *privreq,
int16_t result)
{
usbtrace(TRACE_DEVERROR(MT6260_TRACEERR_REQABORTED), (uint16_t)privep->epphy);
/* Save the result in the request structure */
privreq->req.result = result;
/* Callback to the request completion handler */