aboutsummaryrefslogtreecommitdiff
path: root/drivers/usb/gadget/imx_udc.c
blob: 77c5d0a8a06ebdc42ec09970dc684c3f96ba2737 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
/*
 *	driver/usb/gadget/imx_udc.c
 *
 *	Copyright (C) 2005 Mike Lee(eemike@gmail.com)
 *	Copyright (C) 2008 Darius Augulis <augulis.darius@gmail.com>
 *
 *	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 program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 */

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/clk.h>
#include <linux/delay.h>

#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>

#include <mach/usb.h>
#include <mach/hardware.h>

#include "imx_udc.h"

static const char driver_name[] = "imx_udc";
static const char ep0name[] = "ep0";

void ep0_chg_stat(const char *label, struct imx_udc_struct *imx_usb,
							enum ep0_state stat);

/*******************************************************************************
 * IMX UDC hardware related functions
 *******************************************************************************
 */

void imx_udc_enable(struct imx_udc_struct *imx_usb)
{
	int temp = __raw_readl(imx_usb->base + USB_CTRL);
	__raw_writel(temp | CTRL_FE_ENA | CTRL_AFE_ENA, imx_usb->base + USB_CTRL);
	imx_usb->gadget.speed = USB_SPEED_FULL;
}

void imx_udc_disable(struct imx_udc_struct *imx_usb)
{
	int temp = __raw_readl(imx_usb->base + USB_CTRL);

	__raw_writel(temp & ~(CTRL_FE_ENA | CTRL_AFE_ENA),
		 imx_usb->base + USB_CTRL);

	ep0_chg_stat(__func__, imx_usb, EP0_IDLE);
	imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
}

void imx_udc_reset(struct imx_udc_struct *imx_usb)
{
	int temp = __raw_readl(imx_usb->base + USB_ENAB);

	/* set RST bit */
	__raw_writel(temp | ENAB_RST, imx_usb->base + USB_ENAB);

	/* wait RST bit to clear */
	do {} while (__raw_readl(imx_usb->base + USB_ENAB) & ENAB_RST);

	/* wait CFG bit to assert */
	do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG));

	/* udc module is now ready */
}

void imx_udc_config(struct imx_udc_struct *imx_usb)
{
	u8 ep_conf[5];
	u8 i, j, cfg;
	struct imx_ep_struct *imx_ep;

	/* wait CFG bit to assert */
	do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG));

	/* Download the endpoint buffer for endpoint 0. */
	for (j = 0; j < 5; j++) {
		i = (j == 2 ? imx_usb->imx_ep[0].fifosize : 0x00);
		__raw_writeb(i, imx_usb->base + USB_DDAT);
		do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_BSY);
	}

	/* Download the endpoint buffers for endpoints 1-5.
	 * We specify two configurations, one interface
	 */
	for (cfg = 1; cfg < 3; cfg++) {
		for (i = 1; i < IMX_USB_NB_EP; i++) {
			imx_ep = &imx_usb->imx_ep[i];
			/* EP no | Config no */
			ep_conf[0] = (i << 4) | (cfg << 2);
			/* Type | Direction */
			ep_conf[1] = (imx_ep->bmAttributes << 3) |
					(EP_DIR(imx_ep) << 2);
			/* Max packet size */
			ep_conf[2] = imx_ep->fifosize;
			/* TRXTYP */
			ep_conf[3] = 0xC0;
			/* FIFO no */
			ep_conf[4] = i;

			D_INI(imx_usb->dev,
				"<%s> ep%d_conf[%d]:"
				"[%02x-%02x-%02x-%02x-%02x]\n",
				__func__, i, cfg,
				ep_conf[0], ep_conf[1], ep_conf[2],
				ep_conf[3], ep_conf[4]);

			for (j = 0; j < 5; j++) {
				__raw_writeb(ep_conf[j],
					imx_usb->base + USB_DDAT);
				do {} while (__raw_readl(imx_usb->base + USB_DADR)
					& DADR_BSY);
			}
		}
	}

	/* wait CFG bit to clear */
	do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG);
}

void imx_udc_init_irq(struct imx_udc_struct *imx_usb)
{
	int i;

	/* Mask and clear all irqs */
	__raw_writel(0xFFFFFFFF, imx_usb->base + USB_MASK);
	__raw_writel(0xFFFFFFFF, imx_usb->base + USB_INTR);
	for (i = 0; i < IMX_USB_NB_EP; i++) {
		__raw_writel(0x1FF, imx_usb->base + USB_EP_MASK(i));
		__raw_writel(0x1FF, imx_usb->base + USB_EP_INTR(i));
	}

	/* Enable USB irqs */
	__raw_writel(INTR_MSOF | INTR_FRAME_MATCH, imx_usb->base + USB_MASK);

	/* Enable EP0 irqs */
	__raw_writel(0x1FF & ~(EPINTR_DEVREQ | EPINTR_MDEVREQ | EPINTR_EOT
		| EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL),
		imx_usb->base + USB_EP_MASK(0));
}

void imx_udc_init_ep(struct imx_udc_struct *imx_usb)
{
	int i, max, temp;
	struct imx_ep_struct *imx_ep;
	for (i = 0; i < IMX_USB_NB_EP; i++) {
		imx_ep = &imx_usb->imx_ep[i];
		switch (imx_ep->fifosize) {
		case 8:
			max = 0;
			break;
		case 16:
			max = 1;
			break;
		case 32:
			max = 2;
			break;
		case 64:
			max = 3;
			break;
		default:
			max = 1;
			break;
		}
		temp = (EP_DIR(imx_ep) << 7) | (max << 5)
			| (imx_ep->bmAttributes << 3);
		__raw_writel(temp, imx_usb->base + USB_EP_STAT(i));
		__raw_writel(temp | EPSTAT_FLUSH, imx_usb->base + USB_EP_STAT(i));
		D_INI(imx_usb->dev, "<%s> ep%d_stat %08x\n", __func__, i,
			__raw_readl(imx_usb->base + USB_EP_STAT(i)));
	}
}

void imx_udc_init_fifo(struct imx_udc_struct *imx_usb)
{
	int i, temp;
	struct imx_ep_struct *imx_ep;
	for (i = 0; i < IMX_USB_NB_EP; i++) {
		imx_ep = &imx_usb->imx_ep[i];

		/* Fifo control */
		temp = EP_DIR(imx_ep) ? 0x0B000000 : 0x0F000000;
		__raw_writel(temp, imx_usb->base + USB_EP_FCTRL(i));
		D_INI(imx_usb->dev, "<%s> ep%d_fctrl %08x\n", __func__, i,
			__raw_readl(imx_usb->base + USB_EP_FCTRL(i)));

		/* Fifo alarm */
		temp = (i ? imx_ep->fifosize / 2 : 0);
		__raw_writel(temp, imx_usb->base + USB_EP_FALRM(i));
		D_INI(imx_usb->dev, "<%s> ep%d_falrm %08x\n", __func__, i,
			__raw_readl(imx_usb->base + USB_EP_FALRM(i)));
	}
}

static void imx_udc_init(struct imx_udc_struct *imx_usb)
{
	/* Reset UDC */
	imx_udc_reset(imx_usb);

	/* Download config to enpoint buffer */
	imx_udc_config(imx_usb);

	/* Setup interrups */
	imx_udc_init_irq(imx_usb);

	/* Setup endpoints */
	imx_udc_init_ep(imx_usb);

	/* Setup fifos */
	imx_udc_init_fifo(imx_usb);
}

void imx_ep_irq_enable(struct imx_ep_struct *imx_ep)
{

	int i = EP_NO(imx_ep);

	__raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i));
	__raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i));
	__raw_writel(0x1FF & ~(EPINTR_EOT | EPINTR_EOF),
		imx_ep->imx_usb->base + USB_EP_MASK(i));
}

void imx_ep_irq_disable(struct imx_ep_struct *imx_ep)
{

	int i = EP_NO(imx_ep);

	__raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i));
	__raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i));
}

int imx_ep_empty(struct imx_ep_struct *imx_ep)
{
	struct imx_udc_struct *imx_usb = imx_ep->imx_usb;

	return __raw_readl(imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep)))
			& FSTAT_EMPTY;
}

unsigned imx_fifo_bcount(struct imx_ep_struct *imx_ep)
{
	struct imx_udc_struct *imx_usb = imx_ep->imx_usb;

	return (__raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)))
			& EPSTAT_BCOUNT) >> 16;
}

void imx_flush(struct imx_ep_struct *imx_ep)
{
	struct imx_udc_struct *imx_usb = imx_ep->imx_usb;

	int temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
	__raw_writel(temp | EPSTAT_FLUSH,
		imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
}

void imx_ep_stall(struct imx_ep_struct *imx_ep)
{
	struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
	int temp, i;

	D_ERR(imx_usb->dev, "<%s> Forced stall on %s\n", __func__, imx_ep->ep.name);

	imx_flush(imx_ep);

	/* Special care for ep0 */
	if (EP_NO(imx_ep)) {
		temp = __raw_readl(imx_usb->base + USB_CTRL);
		__raw_writel(temp | CTRL_CMDOVER | CTRL_CMDERROR, imx_usb->base + USB_CTRL);
		do { } while (__raw_readl(imx_usb->base + USB_CTRL) & CTRL_CMDOVER);
		temp = __raw_readl(imx_usb->base + USB_CTRL);
		__raw_writel(temp & ~CTRL_CMDERROR, imx_usb->base + USB_CTRL);
	}
	else {
		temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
		__raw_writel(temp | EPSTAT_STALL,
			imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));

		for (i = 0; i < 100; i ++) {
			temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
			if (!(temp & EPSTAT_STALL))
	 			break;
	 		udelay(20);
	 	}
		if (i == 50)
			D_ERR(imx_usb->dev, "<%s> Non finished stall on %s\n",
				__func__, imx_ep->ep.name);
	}
}

static int imx_udc_get_frame(struct usb_gadget *_gadget)
{
	struct imx_udc_struct *imx_usb = container_of(_gadget,
			struct imx_udc_struct, gadget);

	return __raw_readl(imx_usb->base + USB_FRAME) & 0x7FF;
}

static int imx_udc_wakeup(struct usb_gadget *_gadget)
{
	return 0;
}

/*******************************************************************************
 * USB request control functions
 *******************************************************************************
 */

static void ep_add_request(struct imx_ep_struct *imx_ep, struct imx_request *req)
{
	if (unlikely(!req))
		return;

	req->in_use = 1;
	list_add_tail(&req->queue, &imx_ep->queue);
}

static void ep_del_request(struct imx_ep_struct *imx_ep, struct imx_request *req)
{
	if (unlikely(!req))
		return;

	list_del_init(&req->queue);
	req->in_use = 0;
}

static void done(struct imx_ep_struct *imx_ep, struct imx_request *req, int status)
{
	ep_del_request(imx_ep, req);

	if (likely(req->req.status == -EINPROGRESS))
		req->req.status = status;
	else
		status = req->req.status;

	if (status && status != -ESHUTDOWN)
		D_ERR(imx_ep->imx_usb->dev,
			"<%s> complete %s req %p stat %d len %u/%u\n", __func__,
			imx_ep->ep.name, &req->req, status,
			req->req.actual, req->req.length);

	req->req.complete(&imx_ep->ep, &req->req);
}

static void nuke(struct imx_ep_struct *imx_ep, int status)
{
	struct imx_request *req;

	while (!list_empty(&imx_ep->queue)) {
		req = list_entry(imx_ep->queue.next, struct imx_request, queue);
		done(imx_ep, req, status);
	}
}

/*******************************************************************************
 * Data tansfer over USB functions
 *******************************************************************************
 */
static int read_packet(struct imx_ep_struct *imx_ep, struct imx_request *req)
{
	u8	*buf;
	int	bytes_ep, bufferspace, count, i;

	bytes_ep = imx_fifo_bcount(imx_ep);
	bufferspace = req->req.length - req->req.actual;

	buf = req->req.buf + req->req.actual;
	prefetchw(buf);

	if (unlikely(imx_ep_empty(imx_ep)))
		count = 0;	/* zlp */
	else
		count = min(bytes_ep, bufferspace);

	for (i = count; i > 0; i--)
		*buf++ = __raw_readb(imx_ep->imx_usb->base
						+ USB_EP_FDAT0(EP_NO(imx_ep)));
	req->req.actual += count;

	return count;
}

static int write_packet(struct imx_ep_struct *imx_ep, struct imx_request *req)
{
	u8	*buf;
	int	length, count, temp;

	buf = req->req.buf + req->req.actual;
	prefetch(buf);

	length = min(req->req.length - req->req.actual, (u32)imx_ep->fifosize);

	if (imx_fifo_bcount(imx_ep) + length > imx_ep->fifosize) {
		D_TRX(imx_ep->imx_usb->dev, "<%s> packet overfill %s fifo\n",
			__func__, imx_ep->ep.name);
		return -1;
	}

	req->req.actual += length;
	count = length;

	if (!count && req->req.zero) {	/* zlp */
		temp = __raw_readl(imx_ep->imx_usb->base
			+ USB_EP_STAT(EP_NO(imx_ep)));
		__raw_writel(temp | EPSTAT_ZLPS, imx_ep->imx_usb->base
			+ USB_EP_STAT(EP_NO(imx_ep)));
		D_TRX(imx_ep->imx_usb->dev, "<%s> zero packet\n", __func__);
		return 0;
	}

	while (count--) {
		if (count == 0) {	/* last byte */
			temp = __raw_readl(imx_ep->imx_usb->base
				+ USB_EP_FCTRL(EP_NO(imx_ep)));
			__raw_writel(temp | FCTRL_WFR, imx_ep->imx_usb->base
				+ USB_EP_FCTRL(EP_NO(imx_ep)));
		}
		__raw_writeb(*buf++,
			imx_ep->imx_usb->base + USB_EP_FDAT0(EP_NO(imx_ep)));
	}

	return length;
}

static int read_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req)
{
	int 	bytes = 0,
		count,
		completed = 0;

	while (__raw_readl(imx_ep->imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep)))
		& FSTAT_FR) {
			count = read_packet(imx_ep, req);
			bytes += count;

			completed = (count != imx_ep->fifosize);
			if (completed || req->req.actual == req->req.length) {
				completed = 1;
				break;
			}
	}

	if (completed || !req->req.length) {
		done(imx_ep, req, 0);
		D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n",
			__func__, imx_ep->ep.name, req,
			completed ? "completed" : "not completed");
		if (!EP_NO(imx_ep))
			ep0_chg_stat(__func__, imx_ep->imx_usb, EP0_IDLE);
	}

	D_TRX(imx_ep->imx_usb->dev, "<%s> bytes read: %d\n", __func__, bytes);

	return completed;
}

static int write_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req)
{
	int	bytes = 0,
		count,
		completed = 0;

	while (!completed) {
		count = write_packet(imx_ep, req);
		if (count < 0)
			break; /* busy */
		bytes += count;

		/* last packet "must be" short (or a zlp) */
		completed = (count != imx_ep->fifosize);

		if (unlikely(completed)) {
			done(imx_ep, req, 0);
			D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n",
				__func__, imx_ep->ep.name, req,
				completed ? "completed" : "not completed");
			if (!EP_NO(imx_ep))
				ep0_chg_stat(__func__, imx_ep->imx_usb, EP0_IDLE);
		}
	}

	D_TRX(imx_ep->imx_usb->dev, "<%s> bytes sent: %d\n", __func__, bytes);

	return completed;
}

/*******************************************************************************
 * Endpoint handlers
 *******************************************************************************
 */
static int handle_ep(struct imx_ep_struct *imx_ep)
{
	struct imx_request *req;
	int completed = 0;

	do {
		if (!list_empty(&imx_ep->queue))
			req = list_entry(imx_ep->queue.next,
				struct imx_request, queue);
		else {
			D_REQ(imx_ep->imx_usb->dev, "<%s> no request on %s\n",
				__func__, imx_ep->ep.name);
			return 0;
		}

		if (EP_DIR(imx_ep))	/* to host */
			completed = write_fifo(imx_ep, req);
		else			/* to device */
			completed = read_fifo(imx_ep, req);

		dump_ep_stat(__func__, imx_ep);

	} while (completed);

	return 0;
}

static int handle_ep0(struct imx_ep_struct *imx_ep)
{
	struct imx_request *req = NULL;
	int ret = 0;

	if (!list_empty(&imx_ep->queue))
		req = list_entry(imx_ep->queue.next, struct imx_request, queue);

	if (req) {
		switch (imx_ep->imx_usb->ep0state) {

		case EP0_IN_DATA_PHASE:			/* GET_DESCRIPTOR */
			write_fifo(imx_ep, req);
			break;
		case EP0_OUT_DATA_PHASE:		/* SET_DESCRIPTOR */
			read_fifo(imx_ep, req);
			break;
		default:
			D_EP0(imx_ep->imx_usb->dev,
				"<%s> ep0 i/o, odd state %d\n",
				__func__, imx_ep->imx_usb->ep0state);
			ep_del_request(imx_ep, req);
			ret = -EL2HLT;
			break;
		}
	}

	return ret;
}

static void handle_ep0_devreq(struct imx_udc_struct *imx_usb)
{
	struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[0];
	union {
		struct usb_ctrlrequest	r;
		u8			raw[8];
		u32			word[2];
	} u;
	int temp, i;

	nuke(imx_ep, -EPROTO);

	/* read SETUP packet */
	for (i = 0; i < 2; i++) {
		if (imx_ep_empty(imx_ep)) {
			D_ERR(imx_usb->dev,
				"<%s> no setup packet received\n", __func__);
			goto stall;
		}
		u.word[i] = __raw_readl(imx_usb->base +	USB_EP_FDAT(EP_NO(imx_ep)));
	}

	temp = imx_ep_empty(imx_ep);
	while (!imx_ep_empty(imx_ep)) {
		i = __raw_readl(imx_usb->base +	USB_EP_FDAT(EP_NO(imx_ep)));
		D_ERR(imx_usb->dev,
			"<%s> wrong to have extra bytes for setup : 0x%08x\n",
			__func__, i);
	}
	if (!temp)
		goto stall;

	le16_to_cpus(&u.r.wValue);
	le16_to_cpus(&u.r.wIndex);
	le16_to_cpus(&u.r.wLength);

	D_REQ(imx_usb->dev, "<%s> SETUP %02x.%02x v%04x i%04x l%04x\n",
		__func__, u.r.bRequestType, u.r.bRequest,
		u.r.wValue, u.r.wIndex, u.r.wLength);

	if (imx_usb->set_config) {
		/* NACK the host by using CMDOVER */
		temp = __raw_readl(imx_usb->base + USB_CTRL);
		__raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);

		D_ERR(imx_usb->dev,
			"<%s> set config req is pending, NACK the host\n",
			__func__);
		return;
	}

	if (u.r.bRequestType & USB_DIR_IN)
		ep0_chg_stat(__func__, imx_usb, EP0_IN_DATA_PHASE);
	else
		ep0_chg_stat(__func__, imx_usb, EP0_OUT_DATA_PHASE);

	i = imx_usb->driver->setup(&imx_usb->gadget, &u.r);
	if (i < 0) {
		D_ERR(imx_usb->dev, "<%s> device setup error %d\n",
			__func__, i);
		goto stall;
	}

	return;
stall:
	D_ERR(imx_usb->dev, "<%s> protocol STALL\n", __func__);
	imx_ep_stall(imx_ep);
	ep0_chg_stat(__func__, imx_usb, EP0_STALL);
	return;
}

/*******************************************************************************
 * USB gadget callback functions
 *******************************************************************************
 */

static int imx_ep_enable(struct usb_ep *usb_ep,
				const struct usb_endpoint_descriptor *desc)
{
	struct imx_ep_struct *imx_ep = container_of(usb_ep,
						struct imx_ep_struct, ep);
	struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
	unsigned long flags;

	if (!usb_ep
		|| !desc
		|| !EP_NO(imx_ep)
		|| desc->bDescriptorType != USB_DT_ENDPOINT
		|| imx_ep->bEndpointAddress != desc->bEndpointAddress) {
			D_ERR(imx_usb->dev,
				"<%s> bad ep or descriptor\n", __func__);
			return -EINVAL;
	}

	if (imx_ep->bmAttributes != desc->bmAttributes) {
		D_ERR(imx_usb->dev,
			"<%s> %s type mismatch\n", __func__, usb_ep->name);
		return -EINVAL;
	}

	if (imx_ep->fifosize < le16_to_cpu(desc->wMaxPacketSize)) {
		D_ERR(imx_usb->dev,
			"<%s> bad %s maxpacket\n", __func__, usb_ep->name);
		return -ERANGE;
	}

	if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
		D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
		return -ESHUTDOWN;
	}

	local_irq_save(flags);

	imx_ep->stopped = 0;
	imx_flush(imx_ep);
	imx_ep_irq_enable(imx_ep);

	local_irq_restore(flags);

	D_EPX(imx_usb->dev, "<%s> ENABLED %s\n", __func__, usb_ep->name);
	return 0;
}

static int imx_ep_disable(struct usb_ep *usb_ep)
{
	struct imx_ep_struct *imx_ep = container_of(usb_ep,
						struct imx_ep_struct, ep);
	unsigned long flags;

	if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
		D_ERR(imx_ep->imx_usb->dev, "<%s> %s can not be disabled\n",
			__func__, usb_ep ? imx_ep->ep.name : NULL);
		return -EINVAL;
	}

	local_irq_save(flags);

	imx_ep->stopped = 1;
	nuke(imx_ep, -ESHUTDOWN);
	imx_flush(imx_ep);
	imx_ep_irq_disable(imx_ep);

	local_irq_restore(flags);

	D_EPX(imx_ep->imx_usb->dev,
		"<%s> DISABLED %s\n", __func__, usb_ep->name);
	return 0;
}

static struct usb_request *imx_ep_alloc_request
					(struct usb_ep *usb_ep, gfp_t gfp_flags)
{
	struct imx_request *req;

	req = kzalloc(sizeof *req, gfp_flags);
	if (!req || !usb_ep)
		return 0;

	INIT_LIST_HEAD(&req->queue);
	req->in_use = 0;

	return &req->req;
}

static void imx_ep_free_request
			(struct usb_ep *usb_ep, struct usb_request *usb_req)
{
	struct imx_request *req;

	req = container_of(usb_req, struct imx_request, req);
	WARN_ON(!list_empty(&req->queue));
	kfree(req);
}

static int imx_ep_queue
	(struct usb_ep *usb_ep, struct usb_request *usb_req, gfp_t gfp_flags)
{
	struct imx_ep_struct	*imx_ep;
	struct imx_udc_struct	*imx_usb;
	struct imx_request	*req;
	unsigned long		flags;
	int			ret = 0;

	imx_ep = container_of(usb_ep, struct imx_ep_struct, ep);
	imx_usb = imx_ep->imx_usb;
	req = container_of(usb_req, struct imx_request, req);

	/*
	  Special care on IMX udc.
	  Ignore enqueue when after set configuration from the
	  host. This assume all gadget drivers reply set
	  configuration with the next ep0 req enqueue.
	*/
	if (imx_usb->set_config && !EP_NO(imx_ep)) {
		imx_usb->set_config = 0;
		D_EPX(imx_usb->dev,
			"<%s> gadget reply set config\n", __func__);
		return 0;
	}

	if (unlikely(!usb_req || !req || !usb_req->complete || !usb_req->buf)) {
		D_ERR(imx_usb->dev, "<%s> bad params\n", __func__);
		return -EINVAL;
	}

	if (unlikely(!usb_ep || !imx_ep)) {
		D_ERR(imx_usb->dev, "<%s> bad ep\n", __func__);
		return -EINVAL;
	}

	if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
		D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
		return -ESHUTDOWN;
	}

	local_irq_save(flags);

	/* Debug */
	D_REQ(imx_usb->dev, "<%s> ep%d %s request for [%d] bytes\n",
		__func__, EP_NO(imx_ep),
		((!EP_NO(imx_ep) && imx_ep->imx_usb->ep0state == EP0_IN_DATA_PHASE)
		|| (EP_NO(imx_ep) && EP_DIR(imx_ep))) ? "IN" : "OUT", usb_req->length);
	dump_req(__func__, imx_ep, usb_req);

	if (imx_ep->stopped) {
		usb_req->status = -ESHUTDOWN;
		ret = -ESHUTDOWN;
		goto out;
	}

	if (req->in_use) {
		D_ERR(imx_usb->dev,
			"<%s> refusing to queue req %p (already queued)\n",
			__func__, req);
		goto out;
	}

	usb_req->status = -EINPROGRESS;
	usb_req->actual = 0;

	ep_add_request(imx_ep, req);

	if (!EP_NO(imx_ep))
		ret = handle_ep0(imx_ep);
	else
		ret = handle_ep(imx_ep);
out:
	local_irq_restore(flags);
	return ret;
}

static int imx_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
{

	struct imx_ep_struct *imx_ep = container_of
					(usb_ep, struct imx_ep_struct, ep);
	struct imx_request *req;
	unsigned long flags;

	if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
		D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
		return -EINVAL;
	}

	local_irq_save(flags);

	/* make sure it's actually queued on this endpoint */
	list_for_each_entry(req, &imx_ep->queue, queue) {
		if (&req->req == usb_req)
			break;
	}
	if (&req->req != usb_req) {
		local_irq_restore(flags);
		return -EINVAL;
	}

	done(imx_ep, req, -ECONNRESET);

	local_irq_restore(flags);
	return 0;
}

static int imx_ep_set_halt(struct usb_ep *usb_ep, int value)
{
	struct imx_ep_struct *imx_ep = container_of
					(usb_ep, struct imx_ep_struct, ep);
	unsigned long flags;

	if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
		D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
		return -EINVAL;
	}

	local_irq_save(flags);

	if ((imx_ep->bEndpointAddress & USB_DIR_IN)
		&& !list_empty(&imx_ep->queue)) {
			local_irq_restore(flags);
			return -EAGAIN;
	}

	imx_ep_stall(imx_ep);

	local_irq_restore(flags);

	D_EPX(imx_ep->imx_usb->dev, "<%s> %s halt\n", __func__, usb_ep->name);
	return 0;
}

static int imx_ep_fifo_status(struct usb_ep *usb_ep)
{
	struct imx_ep_struct *imx_ep = container_of
					(usb_ep, struct imx_ep_struct, ep);

	if (!usb_ep) {
		D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
		return -ENODEV;
	}

	if (imx_ep->imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
		return 0;
	else
		return imx_fifo_bcount(imx_ep);
}

static void imx_ep_fifo_flush(struct usb_ep *usb_ep)
{
	struct imx_ep_struct *imx_ep = container_of
					(usb_ep, struct imx_ep_struct, ep);
	unsigned long flags;

	local_irq_save(flags);

	if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
		D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
		local_irq_restore(flags);
		return;
	}

	/* toggle and halt bits stay unchanged */
	imx_flush(imx_ep);

	local_irq_restore(flags);
}

static struct usb_ep_ops imx_ep_ops = {
	.enable		= imx_ep_enable,
	.disable	= imx_ep_disable,

	.alloc_request	= imx_ep_alloc_request,
	.free_request	= imx_ep_free_request,

	.queue		= imx_ep_queue,
	.dequeue	= imx_ep_dequeue,

	.set_halt	= imx_ep_set_halt,
	.fifo_status	= imx_ep_fifo_status,
	.fifo_flush	= imx_ep_fifo_flush,
};

/*******************************************************************************
 * USB endpoint control functions
 *******************************************************************************
 */

void ep0_chg_stat(const char *label,
			struct imx_udc_struct *imx_usb, enum ep0_state stat)
{
	D_EP0(imx_usb->dev, "<%s> from %15s to %15s\n",
		label, state_name[imx_usb->ep0state], state_name[stat]);

	if (imx_usb->ep0state == stat)
		return;

	imx_usb->ep0state = stat;
}

static void usb_init_data(struct imx_udc_struct *imx_usb)
{
	struct imx_ep_struct *imx_ep;
	u8 i;

	/* device/ep0 records init */
	INIT_LIST_HEAD(&imx_usb->gadget.ep_list);
	INIT_LIST_HEAD(&imx_usb->gadget.ep0->ep_list);
	ep0_chg_stat(__func__, imx_usb, EP0_IDLE);

	/* basic endpoint records init */
	for (i = 0; i < IMX_USB_NB_EP; i++) {
		imx_ep = &imx_usb->imx_ep[i];

		if (i) {
			list_add_tail(&imx_ep->ep.ep_list,
				&imx_usb->gadget.ep_list);
			imx_ep->stopped = 1;
		} else
			imx_ep->stopped = 0;

		INIT_LIST_HEAD(&imx_ep->queue);
	}
}

static void udc_stop_activity(struct imx_udc_struct *imx_usb,
					struct usb_gadget_driver *driver)
{
	struct imx_ep_struct *imx_ep;
	int i;

	if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
		driver = NULL;

	/* prevent new request submissions, kill any outstanding requests  */
	for (i = 1; i < IMX_USB_NB_EP; i++) {
		imx_ep = &imx_usb->imx_ep[i];
		imx_flush(imx_ep);
		imx_ep->stopped = 1;
		imx_ep_irq_disable(imx_ep);
		nuke(imx_ep, -ESHUTDOWN);
	}

	imx_usb->cfg = 0;
	imx_usb->intf = 0;
	imx_usb->alt = 0;

	if (driver)
		driver->disconnect(&imx_usb->gadget);
}

/*******************************************************************************
 * Interrupt handlers
 *******************************************************************************
 */

static irqreturn_t imx_udc_irq(int irq, void *dev)
{
	struct imx_udc_struct *imx_usb = dev;
	struct usb_ctrlrequest u;
	int temp, cfg, intf, alt;
	int intr = __raw_readl(imx_usb->base + USB_INTR);

	if (intr & (INTR_WAKEUP | INTR_SUSPEND | INTR_RESUME | INTR_RESET_START
			| INTR_RESET_STOP | INTR_CFG_CHG)) {
				dump_intr(__func__, intr, imx_usb->dev);
				dump_usb_stat(__func__, imx_usb);
	}

	if (!imx_usb->driver) {
		/*imx_udc_disable(imx_usb);*/
		goto end_irq;
	}

	if (intr & INTR_WAKEUP) {
		if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN
			&& imx_usb->driver && imx_usb->driver->resume)
				imx_usb->driver->resume(&imx_usb->gadget);
		imx_usb->set_config = 0;
		imx_usb->gadget.speed = USB_SPEED_FULL;
	}

	if (intr & INTR_SUSPEND) {
		if (imx_usb->gadget.speed != USB_SPEED_UNKNOWN
			&& imx_usb->driver && imx_usb->driver->suspend)
				imx_usb->driver->suspend(&imx_usb->gadget);
		imx_usb->set_config = 0;
		imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
	}

	if (intr & INTR_RESET_START) {
		__raw_writel(intr, imx_usb->base + USB_INTR);
		udc_stop_activity(imx_usb, imx_usb->driver);
		imx_usb->set_config = 0;
		imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
	}

	if (intr & INTR_RESET_STOP)
		imx_usb->gadget.speed = USB_SPEED_FULL;

	if (intr & INTR_CFG_CHG) {
		__raw_writel(INTR_CFG_CHG, imx_usb->base + USB_INTR);
		temp = __raw_readl(imx_usb->base + USB_STAT);
		cfg  = (temp & STAT_CFG) >> 5;
		intf = (temp & STAT_INTF) >> 3;
		alt  =  temp & STAT_ALTSET;

		D_REQ(imx_usb->dev,
			"<%s> orig config C=%d, I=%d, A=%d / "
			"req config C=%d, I=%d, A=%d\n",
			__func__, imx_usb->cfg, imx_usb->intf, imx_usb->alt,
			cfg, intf, alt);

		if (cfg != 1 && cfg != 2)
			goto end_irq;

		imx_usb->set_config = 0;

		/* Config setup */
		if (imx_usb->cfg != cfg) {
			D_REQ(imx_usb->dev, "<%s> Change config start\n",__func__);
			u.bRequest = USB_REQ_SET_CONFIGURATION;
			u.bRequestType = USB_DIR_OUT |
					USB_TYPE_STANDARD |
					USB_RECIP_DEVICE;
			u.wValue = cfg;
			u.wIndex = 0;
			u.wLength = 0;
			imx_usb->cfg = cfg;
			imx_usb->set_config = 1;
			imx_usb->driver->setup(&imx_usb->gadget, &u);
			imx_usb->set_config = 0;
			D_REQ(imx_usb->dev, "<%s> Change config done\n",__func__);

		}
		if (imx_usb->intf != intf || imx_usb->alt != alt) {
			D_REQ(imx_usb->dev, "<%s> Change interface start\n",__func__);
			u.bRequest = USB_REQ_SET_INTERFACE;
			u.bRequestType = USB_DIR_OUT |
					  USB_TYPE_STANDARD |
					  USB_RECIP_INTERFACE;
			u.wValue = alt;
			u.wIndex = intf;
			u.wLength = 0;
			imx_usb->intf = intf;
			imx_usb->alt = alt;
			imx_usb->set_config = 1;
			imx_usb->driver->setup(&imx_usb->gadget, &u);
			imx_usb->set_config = 0;
			D_REQ(imx_usb->dev, "<%s> Change interface done\n",__func__);
		}
	}

	if (intr & INTR_SOF) {
		if (imx_usb->ep0state == EP0_IDLE) {
			temp = __raw_readl(imx_usb->base + USB_CTRL);
			__raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);
		}
	}

end_irq:
	__raw_writel(intr, imx_usb->base + USB_INTR);
	return IRQ_HANDLED;
}

static irqreturn_t imx_udc_ctrl_irq(int irq, void *dev)
{
	struct imx_udc_struct *imx_usb = dev;
	int intr = __raw_readl(imx_usb->base + USB_EP_INTR(0));

	dump_ep_intr(__func__, 0, intr, imx_usb->dev);

	if (!imx_usb->driver) {
		__raw_writel(intr, imx_usb->base + USB_EP_INTR(0));
		return IRQ_HANDLED;
	}

	/* DEVREQ IRQ has highest priority */
	if (intr & (EPINTR_DEVREQ | EPINTR_MDEVREQ))
		handle_ep0_devreq(imx_usb);
	/* Seem i.MX is missing EOF interrupt sometimes.
	 * Therefore we monitor both EOF and FIFO_EMPTY interrups
	 * when transmiting, and both EOF and FIFO_FULL when
	 * receiving data.
	 */
	else if (intr & (EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL))
		handle_ep0(&imx_usb->imx_ep[0]);

	__raw_writel(intr, imx_usb->base + USB_EP_INTR(0));

	return IRQ_HANDLED;
}

static irqreturn_t imx_udc_bulk_irq(int irq, void *dev)
{
	struct imx_udc_struct *imx_usb = dev;
	struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[irq - USBD_INT0];
	int intr = __raw_readl(imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));

	dump_ep_intr(__func__, irq - USBD_INT0, intr, imx_usb->dev);

	if (!imx_usb->driver) {
		__raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
		return IRQ_HANDLED;
	}

	handle_ep(imx_ep);

	__raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));

	return IRQ_HANDLED;
}

irq_handler_t intr_handler(int i)
{
	switch (i) {
	case 0:
		return imx_udc_ctrl_irq;
	case 1:
	case 2:
	case 3:
	case 4:
	case 5:
		return imx_udc_bulk_irq;
	default:
		return imx_udc_irq;
	}
}

/*******************************************************************************
 * Static defined IMX UDC structure
 *******************************************************************************
 */

static const struct usb_gadget_ops imx_udc_ops = {
	.get_frame	 = imx_udc_get_frame,
	.wakeup		 = imx_udc_wakeup,
};

static struct imx_udc_struct controller = {
	.gadget = {
		.ops		= &imx_udc_ops,
		.ep0		= &controller.imx_ep[0].ep,
		.name		= driver_name,
		.dev = {
			 .bus_id	= "gadget",
		 },
	},

	.imx_ep[0] = {
		.ep = {
			.name		= ep0name,
			.ops		= &imx_ep_ops,
			.maxpacket	= 32,
		},
		.imx_usb		= &controller,
		.fifosize		= 32,
		.bEndpointAddress	= 0,
		.bmAttributes		= USB_ENDPOINT_XFER_CONTROL,
	 },
	.imx_ep[1] = {
		.ep = {
			.name		= "ep1in-bulk",
			.ops		= &imx_ep_ops,
			.maxpacket	= 64,
		},
		.imx_usb		= &controller,
		.fifosize		= 64,
		.bEndpointAddress	= USB_DIR_IN | 1,
		.bmAttributes		= USB_ENDPOINT_XFER_BULK,
	 },
	.imx_ep[2] = {
		.ep = {
			.name		= "ep2out-bulk",
			.ops		= &imx_ep_ops,
			.maxpacket	= 64,
		},
		.imx_usb		= &controller,
		.fifosize		= 64,
		.bEndpointAddress	= USB_DIR_OUT | 2,
		.bmAttributes		= USB_ENDPOINT_XFER_BULK,
	 },
	.imx_ep[3] = {
		.ep = {
			.name		= "ep3out-bulk",
			.ops		= &imx_ep_ops,
			.maxpacket	= 32,
		},
		.imx_usb		= &controller,
		.fifosize		= 32,
		.bEndpointAddress 	= USB_DIR_OUT | 3,
		.bmAttributes		= USB_ENDPOINT_XFER_BULK,
	 },
	.imx_ep[4] = {
		.ep = {
			.name		= "ep4in-int",
			.ops		= &imx_ep_ops,
			.maxpacket	= 32,
		 },
		.imx_usb		= &controller,
		.fifosize		= 32,
		.bEndpointAddress 	= USB_DIR_IN | 4,
		.bmAttributes		= USB_ENDPOINT_XFER_INT,
	 },
	.imx_ep[5] = {
		.ep = {
			.name		= "ep5out-int",
			.ops		= &imx_ep_ops,
			.maxpacket	= 32,
		},
		.imx_usb		= &controller,
		.fifosize		= 32,
		.bEndpointAddress 	= USB_DIR_OUT | 5,
		.bmAttributes		= USB_ENDPOINT_XFER_INT,
	 },
};

/*******************************************************************************
 * USB gadged driver functions
 *******************************************************************************
 */
int usb_gadget_register_driver(struct usb_gadget_driver *driver)
{
	struct imx_udc_struct *imx_usb = &controller;
	int retval;

	if (!driver
		|| driver->speed < USB_SPEED_FULL
		|| !driver->bind
		|| !driver->disconnect
		|| !driver->setup)
			return -EINVAL;
	if (!imx_usb)
		return -ENODEV;
	if (imx_usb->driver)
		return -EBUSY;

	/* first hook up the driver ... */
	imx_usb->driver = driver;
	imx_usb->gadget.dev.driver = &driver->driver;

	retval = device_add(&imx_usb->gadget.dev);
	if (retval)
		goto fail;
	retval = driver->bind(&imx_usb->gadget);
	if (retval) {
		D_ERR(imx_usb->dev, "<%s> bind to driver %s --> error %d\n",
			__func__, driver->driver.name, retval);
		device_del(&imx_usb->gadget.dev);

		goto fail;
	}

	D_INI(imx_usb->dev, "<%s> registered gadget driver '%s'\n",
		__func__, driver->driver.name);

	imx_udc_enable(imx_usb);

	return 0;
fail:
	imx_usb->driver = NULL;
	imx_usb->gadget.dev.driver = NULL;
	return retval;
}
EXPORT_SYMBOL(usb_gadget_register_driver);

int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
{
	struct imx_udc_struct *imx_usb = &controller;

	if (!imx_usb)
		return -ENODEV;
	if (!driver || driver != imx_usb->driver || !driver->unbind)
		return -EINVAL;

	udc_stop_activity(imx_usb, driver);
	imx_udc_disable(imx_usb);

	driver->unbind(&imx_usb->gadget);
	imx_usb->gadget.dev.driver = NULL;
	imx_usb->driver = NULL;

	device_del(&imx_usb->gadget.dev);

	D_INI(imx_usb->dev, "<%s> unregistered gadget driver '%s'\n",
		__func__, driver->driver.name);

	return 0;
}
EXPORT_SYMBOL(usb_gadget_unregister_driver);

/*******************************************************************************
 * Module functions
 *******************************************************************************
 */

static int __init imx_udc_probe(struct platform_device *pdev)
{
	struct imx_udc_struct *imx_usb = &controller;
	struct resource *res;
	struct imxusb_platform_data *pdata;
	struct clk *clk;
	void __iomem *base;
	int ret = 0;
	int i, res_size;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev, "can't get device resources\n");
		return -ENODEV;
	}

	pdata = pdev->dev.platform_data;
	if (!pdata) {
		dev_err(&pdev->dev, "driver needs platform data\n");
		return -ENODEV;
	}

	res_size = res->end - res->start + 1;
	if (!request_mem_region(res->start, res_size, res->name)) {
		dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n",
			res_size, res->start);
		return -ENOMEM;
	}

	if (pdata->init) {
		ret = pdata->init(&pdev->dev);
		if (ret)
			goto fail0;
	}

	base = ioremap(res->start, res_size);
	if (!base) {
		dev_err(&pdev->dev, "ioremap failed\n");
		ret = -EIO;
		goto fail1;
	}

	clk = clk_get(NULL, "usbd_clk");
	if (IS_ERR(clk)) {
		ret = PTR_ERR(clk);
		dev_err(&pdev->dev, "can't get USB clock\n");
		goto fail2;
	}
	clk_enable(clk);

	if (clk_get_rate(clk) != 48000000) {
		D_INI(&pdev->dev,
			"Bad USB clock (%d Hz), changing to 48000000 Hz\n",
			(int)clk_get_rate(clk));
		if (clk_set_rate(clk, 48000000)) {
			dev_err(&pdev->dev,
				"Unable to set correct USB clock (48MHz)\n");
			ret = -EIO;
			goto fail3;
		}
	}

	for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
		imx_usb->usbd_int[i] = platform_get_irq(pdev, i);
		if (imx_usb->usbd_int[i] < 0) {
			dev_err(&pdev->dev, "can't get irq number\n");
			ret = -ENODEV;
			goto fail3;
		}
	}

	for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
		ret = request_irq(imx_usb->usbd_int[i], intr_handler(i),
				     IRQF_DISABLED, driver_name, imx_usb);
		if (ret) {
			dev_err(&pdev->dev, "can't get irq %i, err %d\n",
				imx_usb->usbd_int[i], ret);
			for (--i; i >= 0; i--)
				free_irq(imx_usb->usbd_int[i], imx_usb);
			goto fail3;
		}
	}

	imx_usb->res = res;
	imx_usb->base = base;
	imx_usb->clk = clk;
	imx_usb->dev = &pdev->dev;

	device_initialize(&imx_usb->gadget.dev);

	imx_usb->gadget.dev.parent = &pdev->dev;
	imx_usb->gadget.dev.dma_mask = pdev->dev.dma_mask;

	platform_set_drvdata(pdev, imx_usb);

	usb_init_data(imx_usb);
	imx_udc_init(imx_usb);

	return 0;

fail3:
	clk_put(clk);
	clk_disable(clk);
fail2:
	iounmap(base);
fail1:
	if (pdata->exit)
		pdata->exit(&pdev->dev);
fail0:
	release_mem_region(res->start, res_size);
	return ret;
}

static int __exit imx_udc_remove(struct platform_device *pdev)
{
	struct imx_udc_struct *imx_usb = platform_get_drvdata(pdev);
	struct imxusb_platform_data *pdata = pdev->dev.platform_data;
	int i;

	imx_udc_disable(imx_usb);

	for (i = 0; i < IMX_USB_NB_EP + 1; i++)
		free_irq(imx_usb->usbd_int[i], imx_usb);

	clk_put(imx_usb->clk);
	clk_disable(imx_usb->clk);
	iounmap(imx_usb->base);

	release_mem_region(imx_usb->res->start,
		imx_usb->res->end - imx_usb->res->start + 1);

	if (pdata->exit)
		pdata->exit(&pdev->dev);

	platform_set_drvdata(pdev, NULL);

	return 0;
}

/*----------------------------------------------------------------------------*/

#ifdef	CONFIG_PM
#define	imx_udc_suspend	NULL
#define	imx_udc_resume	NULL
#else
#define	imx_udc_suspend	NULL
#define	imx_udc_resume	NULL
#endif

/*----------------------------------------------------------------------------*/

static struct platform_driver udc_driver = {
	.driver		= {
		.name	= driver_name,
		.owner	= THIS_MODULE,
	},
	.remove		= __exit_p(imx_udc_remove),
	.suspend	= imx_udc_suspend,
	.resume		= imx_udc_resume,
};

static int __init udc_init(void)
{
	return platform_driver_probe(&udc_driver, imx_udc_probe);
}
module_init(udc_init);

static void __exit udc_exit(void)
{
	platform_driver_unregister(&udc_driver);
}
module_exit(udc_exit);

MODULE_DESCRIPTION("IMX USB Device Controller driver");
MODULE_AUTHOR("Darius Augulis <augulis.darius@gmail.com>");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:imx_udc");