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
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
|
/*
* bdc_ep.c - BRCM BDC USB3.0 device controller endpoint related functions
*
* Copyright (C) 2014 Broadcom Corporation
*
* Author: Ashwini Pahuja
*
* Based on drivers under drivers/usb/
*
* 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.
*
*/
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/dma-mapping.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/dmapool.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/moduleparam.h>
#include <linux/device.h>
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
#include <linux/usb/otg.h>
#include <linux/pm.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <asm/unaligned.h>
#include <linux/platform_device.h>
#include <linux/usb/composite.h>
#include "bdc.h"
#include "bdc_ep.h"
#include "bdc_cmd.h"
#include "bdc_dbg.h"
static const char * const ep0_state_string[] = {
"WAIT_FOR_SETUP",
"WAIT_FOR_DATA_START",
"WAIT_FOR_DATA_XMIT",
"WAIT_FOR_STATUS_START",
"WAIT_FOR_STATUS_XMIT",
"STATUS_PENDING"
};
/* Free the bdl during ep disable */
static void ep_bd_list_free(struct bdc_ep *ep, u32 num_tabs)
{
struct bd_list *bd_list = &ep->bd_list;
struct bdc *bdc = ep->bdc;
struct bd_table *bd_table;
int index;
dev_dbg(bdc->dev, "%s ep:%s num_tabs:%d\n",
__func__, ep->name, num_tabs);
if (!bd_list->bd_table_array) {
dev_dbg(bdc->dev, "%s already freed\n", ep->name);
return;
}
for (index = 0; index < num_tabs; index++) {
/*
* check if the bd_table struct is allocated ?
* if yes, then check if bd memory has been allocated, then
* free the dma_pool and also the bd_table struct memory
*/
bd_table = bd_list->bd_table_array[index];
dev_dbg(bdc->dev, "bd_table:%p index:%d\n", bd_table, index);
if (!bd_table) {
dev_dbg(bdc->dev, "bd_table not allocated\n");
continue;
}
if (!bd_table->start_bd) {
dev_dbg(bdc->dev, "bd dma pool not allocated\n");
continue;
}
dev_dbg(bdc->dev,
"Free dma pool start_bd:%p dma:%llx\n",
bd_table->start_bd,
(unsigned long long)bd_table->dma);
dma_pool_free(bdc->bd_table_pool,
bd_table->start_bd,
bd_table->dma);
/* Free the bd_table structure */
kfree(bd_table);
}
/* Free the bd table array */
kfree(ep->bd_list.bd_table_array);
}
/*
* chain the tables, by insteting a chain bd at the end of prev_table, pointing
* to next_table
*/
static inline void chain_table(struct bd_table *prev_table,
struct bd_table *next_table,
u32 bd_p_tab)
{
/* Chain the prev table to next table */
prev_table->start_bd[bd_p_tab-1].offset[0] =
cpu_to_le32(lower_32_bits(next_table->dma));
prev_table->start_bd[bd_p_tab-1].offset[1] =
cpu_to_le32(upper_32_bits(next_table->dma));
prev_table->start_bd[bd_p_tab-1].offset[2] =
0x0;
prev_table->start_bd[bd_p_tab-1].offset[3] =
cpu_to_le32(MARK_CHAIN_BD);
}
/* Allocate the bdl for ep, during config ep */
static int ep_bd_list_alloc(struct bdc_ep *ep)
{
struct bd_table *prev_table = NULL;
int index, num_tabs, bd_p_tab;
struct bdc *bdc = ep->bdc;
struct bd_table *bd_table;
dma_addr_t dma;
if (usb_endpoint_xfer_isoc(ep->desc))
num_tabs = NUM_TABLES_ISOCH;
else
num_tabs = NUM_TABLES;
bd_p_tab = NUM_BDS_PER_TABLE;
/* if there is only 1 table in bd list then loop chain to self */
dev_dbg(bdc->dev,
"%s ep:%p num_tabs:%d\n",
__func__, ep, num_tabs);
/* Allocate memory for table array */
ep->bd_list.bd_table_array = kzalloc(
num_tabs * sizeof(struct bd_table *),
GFP_ATOMIC);
if (!ep->bd_list.bd_table_array)
return -ENOMEM;
/* Allocate memory for each table */
for (index = 0; index < num_tabs; index++) {
/* Allocate memory for bd_table structure */
bd_table = kzalloc(sizeof(struct bd_table), GFP_ATOMIC);
if (!bd_table)
goto fail;
bd_table->start_bd = dma_pool_alloc(bdc->bd_table_pool,
GFP_ATOMIC,
&dma);
if (!bd_table->start_bd) {
kfree(bd_table);
goto fail;
}
bd_table->dma = dma;
dev_dbg(bdc->dev,
"index:%d start_bd:%p dma=%08llx prev_table:%p\n",
index, bd_table->start_bd,
(unsigned long long)bd_table->dma, prev_table);
ep->bd_list.bd_table_array[index] = bd_table;
memset(bd_table->start_bd, 0, bd_p_tab * sizeof(struct bdc_bd));
if (prev_table)
chain_table(prev_table, bd_table, bd_p_tab);
prev_table = bd_table;
}
chain_table(prev_table, ep->bd_list.bd_table_array[0], bd_p_tab);
/* Memory allocation is successful, now init the internal fields */
ep->bd_list.num_tabs = num_tabs;
ep->bd_list.max_bdi = (num_tabs * bd_p_tab) - 1;
ep->bd_list.num_tabs = num_tabs;
ep->bd_list.num_bds_table = bd_p_tab;
ep->bd_list.eqp_bdi = 0;
ep->bd_list.hwd_bdi = 0;
return 0;
fail:
/* Free the bd_table_array, bd_table struct, bd's */
ep_bd_list_free(ep, num_tabs);
return -ENOMEM;
}
/* returns how many bd's are need for this transfer */
static inline int bd_needed_req(struct bdc_req *req)
{
int bd_needed = 0;
int remaining;
/* 1 bd needed for 0 byte transfer */
if (req->usb_req.length == 0)
return 1;
/* remaining bytes after tranfering all max BD size BD's */
remaining = req->usb_req.length % BD_MAX_BUFF_SIZE;
if (remaining)
bd_needed++;
/* How many maximum BUFF size BD's ? */
remaining = req->usb_req.length / BD_MAX_BUFF_SIZE;
bd_needed += remaining;
return bd_needed;
}
/* returns the bd index(bdi) corresponding to bd dma address */
static int bd_add_to_bdi(struct bdc_ep *ep, dma_addr_t bd_dma_addr)
{
struct bd_list *bd_list = &ep->bd_list;
dma_addr_t dma_first_bd, dma_last_bd;
struct bdc *bdc = ep->bdc;
struct bd_table *bd_table;
bool found = false;
int tbi, bdi;
dma_first_bd = dma_last_bd = 0;
dev_dbg(bdc->dev, "%s %llx\n",
__func__, (unsigned long long)bd_dma_addr);
/*
* Find in which table this bd_dma_addr belongs?, go through the table
* array and compare addresses of first and last address of bd of each
* table
*/
for (tbi = 0; tbi < bd_list->num_tabs; tbi++) {
bd_table = bd_list->bd_table_array[tbi];
dma_first_bd = bd_table->dma;
dma_last_bd = bd_table->dma +
(sizeof(struct bdc_bd) *
(bd_list->num_bds_table - 1));
dev_dbg(bdc->dev, "dma_first_bd:%llx dma_last_bd:%llx\n",
(unsigned long long)dma_first_bd,
(unsigned long long)dma_last_bd);
if (bd_dma_addr >= dma_first_bd && bd_dma_addr <= dma_last_bd) {
found = true;
break;
}
}
if (unlikely(!found)) {
dev_err(bdc->dev, "%s FATAL err, bd not found\n", __func__);
return -EINVAL;
}
/* Now we know the table, find the bdi */
bdi = (bd_dma_addr - dma_first_bd) / sizeof(struct bdc_bd);
/* return the global bdi, to compare with ep eqp_bdi */
return (bdi + (tbi * bd_list->num_bds_table));
}
/* returns the table index(tbi) of the given bdi */
static int bdi_to_tbi(struct bdc_ep *ep, int bdi)
{
int tbi;
tbi = bdi / ep->bd_list.num_bds_table;
dev_vdbg(ep->bdc->dev,
"bdi:%d num_bds_table:%d tbi:%d\n",
bdi, ep->bd_list.num_bds_table, tbi);
return tbi;
}
/* Find the bdi last bd in the transfer */
static inline int find_end_bdi(struct bdc_ep *ep, int next_hwd_bdi)
{
int end_bdi;
end_bdi = next_hwd_bdi - 1;
if (end_bdi < 0)
end_bdi = ep->bd_list.max_bdi - 1;
else if ((end_bdi % (ep->bd_list.num_bds_table-1)) == 0)
end_bdi--;
return end_bdi;
}
/*
* How many transfer bd's are available on this ep bdl, chain bds are not
* counted in available bds
*/
static int bd_available_ep(struct bdc_ep *ep)
{
struct bd_list *bd_list = &ep->bd_list;
int available1, available2;
struct bdc *bdc = ep->bdc;
int chain_bd1, chain_bd2;
int available_bd = 0;
available1 = available2 = chain_bd1 = chain_bd2 = 0;
/* if empty then we have all bd's available - number of chain bd's */
if (bd_list->eqp_bdi == bd_list->hwd_bdi)
return bd_list->max_bdi - bd_list->num_tabs;
/*
* Depending upon where eqp and dqp pointers are, caculate number
* of avaialble bd's
*/
if (bd_list->hwd_bdi < bd_list->eqp_bdi) {
/* available bd's are from eqp..max_bds + 0..dqp - chain_bds */
available1 = bd_list->max_bdi - bd_list->eqp_bdi;
available2 = bd_list->hwd_bdi;
chain_bd1 = available1 / bd_list->num_bds_table;
chain_bd2 = available2 / bd_list->num_bds_table;
dev_vdbg(bdc->dev, "chain_bd1:%d chain_bd2:%d\n",
chain_bd1, chain_bd2);
available_bd = available1 + available2 - chain_bd1 - chain_bd2;
} else {
/* available bd's are from eqp..dqp - number of chain bd's */
available1 = bd_list->hwd_bdi - bd_list->eqp_bdi;
/* if gap between eqp and dqp is less than NUM_BDS_PER_TABLE */
if ((bd_list->hwd_bdi - bd_list->eqp_bdi)
<= bd_list->num_bds_table) {
/* If there any chain bd in between */
if (!(bdi_to_tbi(ep, bd_list->hwd_bdi)
== bdi_to_tbi(ep, bd_list->eqp_bdi))) {
available_bd = available1 - 1;
}
} else {
chain_bd1 = available1 / bd_list->num_bds_table;
available_bd = available1 - chain_bd1;
}
}
/*
* we need to keep one extra bd to check if ring is full or empty so
* reduce by 1
*/
available_bd--;
dev_vdbg(bdc->dev, "available_bd:%d\n", available_bd);
return available_bd;
}
/* Notify the hardware after queueing the bd to bdl */
void bdc_notify_xfr(struct bdc *bdc, u32 epnum)
{
struct bdc_ep *ep = bdc->bdc_ep_array[epnum];
dev_vdbg(bdc->dev, "%s epnum:%d\n", __func__, epnum);
/*
* We don't have anyway to check if ep state is running,
* except the software flags.
*/
if (unlikely(ep->flags & BDC_EP_STOP))
ep->flags &= ~BDC_EP_STOP;
bdc_writel(bdc->regs, BDC_XSFNTF, epnum);
}
/* returns the bd corresponding to bdi */
static struct bdc_bd *bdi_to_bd(struct bdc_ep *ep, int bdi)
{
int tbi = bdi_to_tbi(ep, bdi);
int local_bdi = 0;
local_bdi = bdi - (tbi * ep->bd_list.num_bds_table);
dev_vdbg(ep->bdc->dev,
"%s bdi:%d local_bdi:%d\n",
__func__, bdi, local_bdi);
return (ep->bd_list.bd_table_array[tbi]->start_bd + local_bdi);
}
/* Advance the enqueue pointer */
static void ep_bdlist_eqp_adv(struct bdc_ep *ep)
{
ep->bd_list.eqp_bdi++;
/* if it's chain bd, then move to next */
if (((ep->bd_list.eqp_bdi + 1) % ep->bd_list.num_bds_table) == 0)
ep->bd_list.eqp_bdi++;
/* if the eqp is pointing to last + 1 then move back to 0 */
if (ep->bd_list.eqp_bdi == (ep->bd_list.max_bdi + 1))
ep->bd_list.eqp_bdi = 0;
}
/* Setup the first bd for ep0 transfer */
static int setup_first_bd_ep0(struct bdc *bdc, struct bdc_req *req, u32 *dword3)
{
u16 wValue;
u32 req_len;
req->ep->dir = 0;
req_len = req->usb_req.length;
switch (bdc->ep0_state) {
case WAIT_FOR_DATA_START:
*dword3 |= BD_TYPE_DS;
if (bdc->setup_pkt.bRequestType & USB_DIR_IN)
*dword3 |= BD_DIR_IN;
/* check if zlp will be needed */
wValue = le16_to_cpu(bdc->setup_pkt.wValue);
if ((wValue > req_len) &&
(req_len % bdc->gadget.ep0->maxpacket == 0)) {
dev_dbg(bdc->dev, "ZLP needed wVal:%d len:%d MaxP:%d\n",
wValue, req_len,
bdc->gadget.ep0->maxpacket);
bdc->zlp_needed = true;
}
break;
case WAIT_FOR_STATUS_START:
*dword3 |= BD_TYPE_SS;
if (!le16_to_cpu(bdc->setup_pkt.wLength) ||
!(bdc->setup_pkt.bRequestType & USB_DIR_IN))
*dword3 |= BD_DIR_IN;
break;
default:
dev_err(bdc->dev,
"Unknown ep0 state for queueing bd ep0_state:%s\n",
ep0_state_string[bdc->ep0_state]);
return -EINVAL;
}
return 0;
}
/* Setup the bd dma descriptor for a given request */
static int setup_bd_list_xfr(struct bdc *bdc, struct bdc_req *req, int num_bds)
{
dma_addr_t buf_add = req->usb_req.dma;
u32 maxp, tfs, dword2, dword3;
struct bd_transfer *bd_xfr;
struct bd_list *bd_list;
struct bdc_ep *ep;
struct bdc_bd *bd;
int ret, bdnum;
u32 req_len;
ep = req->ep;
bd_list = &ep->bd_list;
bd_xfr = &req->bd_xfr;
bd_xfr->req = req;
bd_xfr->start_bdi = bd_list->eqp_bdi;
bd = bdi_to_bd(ep, bd_list->eqp_bdi);
req_len = req->usb_req.length;
maxp = usb_endpoint_maxp(ep->desc);
tfs = roundup(req->usb_req.length, maxp);
tfs = tfs/maxp;
dev_vdbg(bdc->dev, "%s ep:%s num_bds:%d tfs:%d r_len:%d bd:%p\n",
__func__, ep->name, num_bds, tfs, req_len, bd);
for (bdnum = 0; bdnum < num_bds; bdnum++) {
dword2 = dword3 = 0;
/* First bd */
if (!bdnum) {
dword3 |= BD_SOT|BD_SBF|(tfs<<BD_TFS_SHIFT);
dword2 |= BD_LTF;
/* format of first bd for ep0 is different than other */
if (ep->ep_num == 1) {
ret = setup_first_bd_ep0(bdc, req, &dword3);
if (ret)
return ret;
}
}
if (!req->ep->dir)
dword3 |= BD_ISP;
if (req_len > BD_MAX_BUFF_SIZE) {
dword2 |= BD_MAX_BUFF_SIZE;
req_len -= BD_MAX_BUFF_SIZE;
} else {
/* this should be the last bd */
dword2 |= req_len;
dword3 |= BD_IOC;
dword3 |= BD_EOT;
}
/* Currently only 1 INT target is supported */
dword2 |= BD_INTR_TARGET(0);
bd = bdi_to_bd(ep, ep->bd_list.eqp_bdi);
if (unlikely(!bd)) {
dev_err(bdc->dev, "Err bd pointing to wrong addr\n");
return -EINVAL;
}
/* write bd */
bd->offset[0] = cpu_to_le32(lower_32_bits(buf_add));
bd->offset[1] = cpu_to_le32(upper_32_bits(buf_add));
bd->offset[2] = cpu_to_le32(dword2);
bd->offset[3] = cpu_to_le32(dword3);
/* advance eqp pointer */
ep_bdlist_eqp_adv(ep);
/* advance the buff pointer */
buf_add += BD_MAX_BUFF_SIZE;
dev_vdbg(bdc->dev, "buf_add:%08llx req_len:%d bd:%p eqp:%d\n",
(unsigned long long)buf_add, req_len, bd,
ep->bd_list.eqp_bdi);
bd = bdi_to_bd(ep, ep->bd_list.eqp_bdi);
bd->offset[3] = cpu_to_le32(BD_SBF);
}
/* clear the STOP BD fetch bit from the first bd of this xfr */
bd = bdi_to_bd(ep, bd_xfr->start_bdi);
bd->offset[3] &= cpu_to_le32(~BD_SBF);
/* the new eqp will be next hw dqp */
bd_xfr->num_bds = num_bds;
bd_xfr->next_hwd_bdi = ep->bd_list.eqp_bdi;
/* everything is written correctly before notifying the HW */
wmb();
return 0;
}
/* Queue the xfr */
static int bdc_queue_xfr(struct bdc *bdc, struct bdc_req *req)
{
int num_bds, bd_available;
struct bdc_ep *ep;
int ret;
ep = req->ep;
dev_dbg(bdc->dev, "%s req:%p\n", __func__, req);
dev_dbg(bdc->dev, "eqp_bdi:%d hwd_bdi:%d\n",
ep->bd_list.eqp_bdi, ep->bd_list.hwd_bdi);
num_bds = bd_needed_req(req);
bd_available = bd_available_ep(ep);
/* how many bd's are avaialble on ep */
if (num_bds > bd_available)
return -ENOMEM;
ret = setup_bd_list_xfr(bdc, req, num_bds);
if (ret)
return ret;
list_add_tail(&req->queue, &ep->queue);
bdc_dbg_bd_list(bdc, ep);
bdc_notify_xfr(bdc, ep->ep_num);
return 0;
}
/* callback to gadget layer when xfr completes */
static void bdc_req_complete(struct bdc_ep *ep, struct bdc_req *req,
int status)
{
struct bdc *bdc = ep->bdc;
if (req == NULL || &req->queue == NULL || &req->usb_req == NULL)
return;
dev_dbg(bdc->dev, "%s ep:%s status:%d\n", __func__, ep->name, status);
list_del(&req->queue);
req->usb_req.status = status;
usb_gadget_unmap_request(&bdc->gadget, &req->usb_req, ep->dir);
if (req->usb_req.complete) {
spin_unlock(&bdc->lock);
usb_gadget_giveback_request(&ep->usb_ep, &req->usb_req);
spin_lock(&bdc->lock);
}
}
/* Disable the endpoint */
int bdc_ep_disable(struct bdc_ep *ep)
{
struct bdc_req *req;
struct bdc *bdc;
int ret;
ret = 0;
bdc = ep->bdc;
dev_dbg(bdc->dev, "%s() ep->ep_num=%d\n", __func__, ep->ep_num);
/* Stop the endpoint */
ret = bdc_stop_ep(bdc, ep->ep_num);
/*
* Intentionally don't check the ret value of stop, it can fail in
* disconnect scenarios, continue with dconfig
*/
/* de-queue any pending requests */
while (!list_empty(&ep->queue)) {
req = list_entry(ep->queue.next, struct bdc_req,
queue);
bdc_req_complete(ep, req, -ESHUTDOWN);
}
/* deconfigure the endpoint */
ret = bdc_dconfig_ep(bdc, ep);
if (ret)
dev_warn(bdc->dev,
"dconfig fail but continue with memory free");
ep->flags = 0;
/* ep0 memory is not freed, but reused on next connect sr */
if (ep->ep_num == 1)
return 0;
/* Free the bdl memory */
ep_bd_list_free(ep, ep->bd_list.num_tabs);
ep->desc = NULL;
ep->comp_desc = NULL;
ep->usb_ep.desc = NULL;
ep->ep_type = 0;
return ret;
}
/* Enable the ep */
int bdc_ep_enable(struct bdc_ep *ep)
{
struct bdc *bdc;
int ret = 0;
bdc = ep->bdc;
dev_dbg(bdc->dev, "%s NUM_TABLES:%d %d\n",
__func__, NUM_TABLES, NUM_TABLES_ISOCH);
ret = ep_bd_list_alloc(ep);
if (ret) {
dev_err(bdc->dev, "ep bd list allocation failed:%d\n", ret);
return -ENOMEM;
}
bdc_dbg_bd_list(bdc, ep);
/* only for ep0: config ep is called for ep0 from connect event */
ep->flags |= BDC_EP_ENABLED;
if (ep->ep_num == 1)
return ret;
/* Issue a configure endpoint command */
ret = bdc_config_ep(bdc, ep);
if (ret)
return ret;
ep->usb_ep.maxpacket = usb_endpoint_maxp(ep->desc);
ep->usb_ep.desc = ep->desc;
ep->usb_ep.comp_desc = ep->comp_desc;
ep->ep_type = usb_endpoint_type(ep->desc);
ep->flags |= BDC_EP_ENABLED;
return 0;
}
/* EP0 related code */
/* Queue a status stage BD */
static int ep0_queue_status_stage(struct bdc *bdc)
{
struct bdc_req *status_req;
struct bdc_ep *ep;
status_req = &bdc->status_req;
ep = bdc->bdc_ep_array[1];
status_req->ep = ep;
status_req->usb_req.length = 0;
status_req->usb_req.status = -EINPROGRESS;
status_req->usb_req.actual = 0;
status_req->usb_req.complete = NULL;
bdc_queue_xfr(bdc, status_req);
return 0;
}
/* Queue xfr on ep0 */
static int ep0_queue(struct bdc_ep *ep, struct bdc_req *req)
{
struct bdc *bdc;
int ret;
bdc = ep->bdc;
dev_dbg(bdc->dev, "%s()\n", __func__);
req->usb_req.actual = 0;
req->usb_req.status = -EINPROGRESS;
req->epnum = ep->ep_num;
if (bdc->delayed_status) {
bdc->delayed_status = false;
/* if status stage was delayed? */
if (bdc->ep0_state == WAIT_FOR_STATUS_START) {
/* Queue a status stage BD */
ep0_queue_status_stage(bdc);
bdc->ep0_state = WAIT_FOR_STATUS_XMIT;
return 0;
}
} else {
/*
* if delayed status is false and 0 length transfer is requested
* i.e. for status stage of some setup request, then just
* return from here the status stage is queued independently
*/
if (req->usb_req.length == 0)
return 0;
}
ret = usb_gadget_map_request(&bdc->gadget, &req->usb_req, ep->dir);
if (ret) {
dev_err(bdc->dev, "dma mapping failed %s\n", ep->name);
return ret;
}
return bdc_queue_xfr(bdc, req);
}
/* Queue data stage */
static int ep0_queue_data_stage(struct bdc *bdc)
{
struct bdc_ep *ep;
dev_dbg(bdc->dev, "%s\n", __func__);
ep = bdc->bdc_ep_array[1];
bdc->ep0_req.ep = ep;
bdc->ep0_req.usb_req.complete = NULL;
return ep0_queue(ep, &bdc->ep0_req);
}
/* Queue req on ep */
static int ep_queue(struct bdc_ep *ep, struct bdc_req *req)
{
struct bdc *bdc;
int ret = 0;
if (!req || !ep->usb_ep.desc)
return -EINVAL;
bdc = ep->bdc;
req->usb_req.actual = 0;
req->usb_req.status = -EINPROGRESS;
req->epnum = ep->ep_num;
ret = usb_gadget_map_request(&bdc->gadget, &req->usb_req, ep->dir);
if (ret) {
dev_err(bdc->dev, "dma mapping failed\n");
return ret;
}
return bdc_queue_xfr(bdc, req);
}
/* Dequeue a request from ep */
static int ep_dequeue(struct bdc_ep *ep, struct bdc_req *req)
{
int start_bdi, end_bdi, tbi, eqp_bdi, curr_hw_dqpi;
bool start_pending, end_pending;
bool first_remove = false;
struct bdc_req *first_req;
struct bdc_bd *bd_start;
struct bd_table *table;
dma_addr_t next_bd_dma;
u64 deq_ptr_64 = 0;
struct bdc *bdc;
u32 tmp_32;
int ret;
bdc = ep->bdc;
start_pending = end_pending = false;
eqp_bdi = ep->bd_list.eqp_bdi - 1;
if (eqp_bdi < 0)
eqp_bdi = ep->bd_list.max_bdi;
start_bdi = req->bd_xfr.start_bdi;
end_bdi = find_end_bdi(ep, req->bd_xfr.next_hwd_bdi);
dev_dbg(bdc->dev, "%s ep:%s start:%d end:%d\n",
__func__, ep->name, start_bdi, end_bdi);
dev_dbg(bdc->dev, "ep_dequeue ep=%p ep->desc=%p\n",
ep, (void *)ep->usb_ep.desc);
/* Stop the ep to see where the HW is ? */
ret = bdc_stop_ep(bdc, ep->ep_num);
/* if there is an issue with stopping ep, then no need to go further */
if (ret)
return 0;
/*
* After endpoint is stopped, there can be 3 cases, the request
* is processed, pending or in the middle of processing
*/
/* The current hw dequeue pointer */
tmp_32 = bdc_readl(bdc->regs, BDC_EPSTS0);
deq_ptr_64 = tmp_32;
tmp_32 = bdc_readl(bdc->regs, BDC_EPSTS1);
deq_ptr_64 |= ((u64)tmp_32 << 32);
/* we have the dma addr of next bd that will be fetched by hardware */
curr_hw_dqpi = bd_add_to_bdi(ep, deq_ptr_64);
if (curr_hw_dqpi < 0)
return curr_hw_dqpi;
/*
* curr_hw_dqpi points to actual dqp of HW and HW owns bd's from
* curr_hw_dqbdi..eqp_bdi.
*/
/* Check if start_bdi and end_bdi are in range of HW owned BD's */
if (curr_hw_dqpi > eqp_bdi) {
/* there is a wrap from last to 0 */
if (start_bdi >= curr_hw_dqpi || start_bdi <= eqp_bdi) {
start_pending = true;
end_pending = true;
} else if (end_bdi >= curr_hw_dqpi || end_bdi <= eqp_bdi) {
end_pending = true;
}
} else {
if (start_bdi >= curr_hw_dqpi) {
start_pending = true;
end_pending = true;
} else if (end_bdi >= curr_hw_dqpi) {
end_pending = true;
}
}
dev_dbg(bdc->dev,
"start_pending:%d end_pending:%d speed:%d\n",
start_pending, end_pending, bdc->gadget.speed);
/* If both start till end are processes, we cannot deq req */
if (!start_pending && !end_pending)
return -EINVAL;
/*
* if ep_dequeue is called after disconnect then just return
* success from here
*/
if (bdc->gadget.speed == USB_SPEED_UNKNOWN)
return 0;
tbi = bdi_to_tbi(ep, req->bd_xfr.next_hwd_bdi);
table = ep->bd_list.bd_table_array[tbi];
next_bd_dma = table->dma +
sizeof(struct bdc_bd)*(req->bd_xfr.next_hwd_bdi -
tbi * ep->bd_list.num_bds_table);
first_req = list_first_entry(&ep->queue, struct bdc_req,
queue);
if (req == first_req)
first_remove = true;
/*
* Due to HW limitation we need to bypadd chain bd's and issue ep_bla,
* incase if start is pending this is the first request in the list
* then issue ep_bla instead of marking as chain bd
*/
if (start_pending && !first_remove) {
/*
* Mark the start bd as Chain bd, and point the chain
* bd to next_bd_dma
*/
bd_start = bdi_to_bd(ep, start_bdi);
bd_start->offset[0] = cpu_to_le32(lower_32_bits(next_bd_dma));
bd_start->offset[1] = cpu_to_le32(upper_32_bits(next_bd_dma));
bd_start->offset[2] = 0x0;
bd_start->offset[3] = cpu_to_le32(MARK_CHAIN_BD);
bdc_dbg_bd_list(bdc, ep);
} else if (end_pending) {
/*
* The transfer is stopped in the middle, move the
* HW deq pointer to next_bd_dma
*/
ret = bdc_ep_bla(bdc, ep, next_bd_dma);
if (ret) {
dev_err(bdc->dev, "error in ep_bla:%d\n", ret);
return ret;
}
}
return 0;
}
/* Halt/Clear the ep based on value */
static int ep_set_halt(struct bdc_ep *ep, u32 value)
{
struct bdc *bdc;
int ret;
bdc = ep->bdc;
dev_dbg(bdc->dev, "%s ep:%s value=%d\n", __func__, ep->name, value);
if (value) {
dev_dbg(bdc->dev, "Halt\n");
if (ep->ep_num == 1)
bdc->ep0_state = WAIT_FOR_SETUP;
ret = bdc_ep_set_stall(bdc, ep->ep_num);
if (ret)
dev_err(bdc->dev, "failed to set STALL on %s\n",
ep->name);
else
ep->flags |= BDC_EP_STALL;
} else {
/* Clear */
dev_dbg(bdc->dev, "Before Clear\n");
ret = bdc_ep_clear_stall(bdc, ep->ep_num);
if (ret)
dev_err(bdc->dev, "failed to clear STALL on %s\n",
ep->name);
else
ep->flags &= ~BDC_EP_STALL;
dev_dbg(bdc->dev, "After Clear\n");
}
return ret;
}
/* Free all the ep */
void bdc_free_ep(struct bdc *bdc)
{
struct bdc_ep *ep;
u8 epnum;
dev_dbg(bdc->dev, "%s\n", __func__);
for (epnum = 1; epnum < bdc->num_eps; epnum++) {
ep = bdc->bdc_ep_array[epnum];
if (!ep)
continue;
if (ep->flags & BDC_EP_ENABLED)
ep_bd_list_free(ep, ep->bd_list.num_tabs);
/* ep0 is not in this gadget list */
if (epnum != 1)
list_del(&ep->usb_ep.ep_list);
kfree(ep);
}
}
/* USB2 spec, section 7.1.20 */
static int bdc_set_test_mode(struct bdc *bdc)
{
u32 usb2_pm;
usb2_pm = bdc_readl(bdc->regs, BDC_USPPM2);
usb2_pm &= ~BDC_PTC_MASK;
dev_dbg(bdc->dev, "%s\n", __func__);
switch (bdc->test_mode) {
case TEST_J:
case TEST_K:
case TEST_SE0_NAK:
case TEST_PACKET:
case TEST_FORCE_EN:
usb2_pm |= bdc->test_mode << 28;
break;
default:
return -EINVAL;
}
dev_dbg(bdc->dev, "usb2_pm=%08x", usb2_pm);
bdc_writel(bdc->regs, BDC_USPPM2, usb2_pm);
return 0;
}
/*
* Helper function to handle Transfer status report with status as either
* success or short
*/
static void handle_xsr_succ_status(struct bdc *bdc, struct bdc_ep *ep,
struct bdc_sr *sreport)
{
int short_bdi, start_bdi, end_bdi, max_len_bds, chain_bds;
struct bd_list *bd_list = &ep->bd_list;
int actual_length, length_short;
struct bd_transfer *bd_xfr;
struct bdc_bd *short_bd;
struct bdc_req *req;
u64 deq_ptr_64 = 0;
int status = 0;
int sr_status;
u32 tmp_32;
dev_dbg(bdc->dev, "%s ep:%p\n", __func__, ep);
bdc_dbg_srr(bdc, 0);
/* do not process thie sr if ignore flag is set */
if (ep->ignore_next_sr) {
ep->ignore_next_sr = false;
return;
}
if (unlikely(list_empty(&ep->queue))) {
dev_warn(bdc->dev, "xfr srr with no BD's queued\n");
return;
}
req = list_entry(ep->queue.next, struct bdc_req,
queue);
bd_xfr = &req->bd_xfr;
sr_status = XSF_STS(le32_to_cpu(sreport->offset[3]));
/*
* sr_status is short and this transfer has more than 1 bd then it needs
* special handling, this is only applicable for bulk and ctrl
*/
if (sr_status == XSF_SHORT && bd_xfr->num_bds > 1) {
/*
* This is multi bd xfr, lets see which bd
* caused short transfer and how many bytes have been
* transferred so far.
*/
tmp_32 = le32_to_cpu(sreport->offset[0]);
deq_ptr_64 = tmp_32;
tmp_32 = le32_to_cpu(sreport->offset[1]);
deq_ptr_64 |= ((u64)tmp_32 << 32);
short_bdi = bd_add_to_bdi(ep, deq_ptr_64);
if (unlikely(short_bdi < 0))
dev_warn(bdc->dev, "bd doesn't exist?\n");
start_bdi = bd_xfr->start_bdi;
/*
* We know the start_bdi and short_bdi, how many xfr
* bds in between
*/
if (start_bdi <= short_bdi) {
max_len_bds = short_bdi - start_bdi;
if (max_len_bds <= bd_list->num_bds_table) {
if (!(bdi_to_tbi(ep, start_bdi) ==
bdi_to_tbi(ep, short_bdi)))
max_len_bds--;
} else {
chain_bds = max_len_bds/bd_list->num_bds_table;
max_len_bds -= chain_bds;
}
} else {
/* there is a wrap in the ring within a xfr */
chain_bds = (bd_list->max_bdi - start_bdi)/
bd_list->num_bds_table;
chain_bds += short_bdi/bd_list->num_bds_table;
max_len_bds = bd_list->max_bdi - start_bdi;
max_len_bds += short_bdi;
max_len_bds -= chain_bds;
}
/* max_len_bds is the number of full length bds */
end_bdi = find_end_bdi(ep, bd_xfr->next_hwd_bdi);
if (!(end_bdi == short_bdi))
ep->ignore_next_sr = true;
actual_length = max_len_bds * BD_MAX_BUFF_SIZE;
short_bd = bdi_to_bd(ep, short_bdi);
/* length queued */
length_short = le32_to_cpu(short_bd->offset[2]) & 0x1FFFFF;
/* actual length trensfered */
length_short -= SR_BD_LEN(le32_to_cpu(sreport->offset[2]));
actual_length += length_short;
req->usb_req.actual = actual_length;
} else {
req->usb_req.actual = req->usb_req.length -
SR_BD_LEN(le32_to_cpu(sreport->offset[2]));
dev_dbg(bdc->dev,
"len=%d actual=%d bd_xfr->next_hwd_bdi:%d\n",
req->usb_req.length, req->usb_req.actual,
bd_xfr->next_hwd_bdi);
}
/* Update the dequeue pointer */
ep->bd_list.hwd_bdi = bd_xfr->next_hwd_bdi;
if (req->usb_req.actual < req->usb_req.length) {
dev_dbg(bdc->dev, "short xfr on %d\n", ep->ep_num);
if (req->usb_req.short_not_ok)
status = -EREMOTEIO;
}
bdc_req_complete(ep, bd_xfr->req, status);
}
/* EP0 setup related packet handlers */
/*
* Setup packet received, just store the packet and process on next DS or SS
* started SR
*/
void bdc_xsf_ep0_setup_recv(struct bdc *bdc, struct bdc_sr *sreport)
{
struct usb_ctrlrequest *setup_pkt;
u32 len;
dev_dbg(bdc->dev,
"%s ep0_state:%s\n",
__func__, ep0_state_string[bdc->ep0_state]);
/* Store received setup packet */
setup_pkt = &bdc->setup_pkt;
memcpy(setup_pkt, &sreport->offset[0], sizeof(*setup_pkt));
len = le16_to_cpu(setup_pkt->wLength);
if (!len)
bdc->ep0_state = WAIT_FOR_STATUS_START;
else
bdc->ep0_state = WAIT_FOR_DATA_START;
dev_dbg(bdc->dev,
"%s exit ep0_state:%s\n",
__func__, ep0_state_string[bdc->ep0_state]);
}
/* Stall ep0 */
static void ep0_stall(struct bdc *bdc)
{
struct bdc_ep *ep = bdc->bdc_ep_array[1];
struct bdc_req *req;
dev_dbg(bdc->dev, "%s\n", __func__);
bdc->delayed_status = false;
ep_set_halt(ep, 1);
/* de-queue any pendig requests */
while (!list_empty(&ep->queue)) {
req = list_entry(ep->queue.next, struct bdc_req,
queue);
bdc_req_complete(ep, req, -ESHUTDOWN);
}
}
/* SET_ADD handlers */
static int ep0_set_address(struct bdc *bdc, struct usb_ctrlrequest *ctrl)
{
enum usb_device_state state = bdc->gadget.state;
int ret = 0;
u32 addr;
addr = le16_to_cpu(ctrl->wValue);
dev_dbg(bdc->dev,
"%s addr:%d dev state:%d\n",
__func__, addr, state);
if (addr > 127)
return -EINVAL;
switch (state) {
case USB_STATE_DEFAULT:
case USB_STATE_ADDRESS:
/* Issue Address device command */
ret = bdc_address_device(bdc, addr);
if (ret)
return ret;
if (addr)
usb_gadget_set_state(&bdc->gadget, USB_STATE_ADDRESS);
else
usb_gadget_set_state(&bdc->gadget, USB_STATE_DEFAULT);
bdc->dev_addr = addr;
break;
default:
dev_warn(bdc->dev,
"SET Address in wrong device state %d\n",
state);
ret = -EINVAL;
}
return ret;
}
/* Handler for SET/CLEAR FEATURE requests for device */
static int ep0_handle_feature_dev(struct bdc *bdc, u16 wValue,
u16 wIndex, bool set)
{
enum usb_device_state state = bdc->gadget.state;
u32 usppms = 0;
dev_dbg(bdc->dev, "%s set:%d dev state:%d\n",
__func__, set, state);
switch (wValue) {
case USB_DEVICE_REMOTE_WAKEUP:
dev_dbg(bdc->dev, "USB_DEVICE_REMOTE_WAKEUP\n");
if (set)
bdc->devstatus |= REMOTE_WAKE_ENABLE;
else
bdc->devstatus &= ~REMOTE_WAKE_ENABLE;
break;
case USB_DEVICE_TEST_MODE:
dev_dbg(bdc->dev, "USB_DEVICE_TEST_MODE\n");
if ((wIndex & 0xFF) ||
(bdc->gadget.speed != USB_SPEED_HIGH) || !set)
return -EINVAL;
bdc->test_mode = wIndex >> 8;
break;
case USB_DEVICE_U1_ENABLE:
dev_dbg(bdc->dev, "USB_DEVICE_U1_ENABLE\n");
if (bdc->gadget.speed != USB_SPEED_SUPER ||
state != USB_STATE_CONFIGURED)
return -EINVAL;
usppms = bdc_readl(bdc->regs, BDC_USPPMS);
if (set) {
/* clear previous u1t */
usppms &= ~BDC_U1T(BDC_U1T_MASK);
usppms |= BDC_U1T(U1_TIMEOUT);
usppms |= BDC_U1E | BDC_PORT_W1S;
bdc->devstatus |= (1 << USB_DEV_STAT_U1_ENABLED);
} else {
usppms &= ~BDC_U1E;
usppms |= BDC_PORT_W1S;
bdc->devstatus &= ~(1 << USB_DEV_STAT_U1_ENABLED);
}
bdc_writel(bdc->regs, BDC_USPPMS, usppms);
break;
case USB_DEVICE_U2_ENABLE:
dev_dbg(bdc->dev, "USB_DEVICE_U2_ENABLE\n");
if (bdc->gadget.speed != USB_SPEED_SUPER ||
state != USB_STATE_CONFIGURED)
return -EINVAL;
usppms = bdc_readl(bdc->regs, BDC_USPPMS);
if (set) {
usppms |= BDC_U2E;
usppms |= BDC_U2A;
bdc->devstatus |= (1 << USB_DEV_STAT_U2_ENABLED);
} else {
usppms &= ~BDC_U2E;
usppms &= ~BDC_U2A;
bdc->devstatus &= ~(1 << USB_DEV_STAT_U2_ENABLED);
}
bdc_writel(bdc->regs, BDC_USPPMS, usppms);
break;
case USB_DEVICE_LTM_ENABLE:
dev_dbg(bdc->dev, "USB_DEVICE_LTM_ENABLE?\n");
if (bdc->gadget.speed != USB_SPEED_SUPER ||
state != USB_STATE_CONFIGURED)
return -EINVAL;
break;
default:
dev_err(bdc->dev, "Unknown wValue:%d\n", wValue);
return -EOPNOTSUPP;
} /* USB_RECIP_DEVICE end */
return 0;
}
/* SET/CLEAR FEATURE handler */
static int ep0_handle_feature(struct bdc *bdc,
struct usb_ctrlrequest *setup_pkt, bool set)
{
enum usb_device_state state = bdc->gadget.state;
struct bdc_ep *ep;
u16 wValue;
u16 wIndex;
int epnum;
wValue = le16_to_cpu(setup_pkt->wValue);
wIndex = le16_to_cpu(setup_pkt->wIndex);
dev_dbg(bdc->dev,
"%s wValue=%d wIndex=%d devstate=%08x speed=%d set=%d",
__func__, wValue, wIndex, state,
bdc->gadget.speed, set);
switch (setup_pkt->bRequestType & USB_RECIP_MASK) {
case USB_RECIP_DEVICE:
return ep0_handle_feature_dev(bdc, wValue, wIndex, set);
case USB_RECIP_INTERFACE:
dev_dbg(bdc->dev, "USB_RECIP_INTERFACE\n");
/* USB3 spec, sec 9.4.9 */
if (wValue != USB_INTRF_FUNC_SUSPEND)
return -EINVAL;
/* USB3 spec, Table 9-8 */
if (set) {
if (wIndex & USB_INTRF_FUNC_SUSPEND_RW) {
dev_dbg(bdc->dev, "SET REMOTE_WAKEUP\n");
bdc->devstatus |= REMOTE_WAKE_ENABLE;
} else {
dev_dbg(bdc->dev, "CLEAR REMOTE_WAKEUP\n");
bdc->devstatus &= ~REMOTE_WAKE_ENABLE;
}
}
break;
case USB_RECIP_ENDPOINT:
dev_dbg(bdc->dev, "USB_RECIP_ENDPOINT\n");
if (wValue != USB_ENDPOINT_HALT)
return -EINVAL;
epnum = wIndex & USB_ENDPOINT_NUMBER_MASK;
if (epnum) {
if ((wIndex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
epnum = epnum * 2 + 1;
else
epnum *= 2;
} else {
epnum = 1; /*EP0*/
}
/*
* If CLEAR_FEATURE on ep0 then don't do anything as the stall
* condition on ep0 has already been cleared when SETUP packet
* was received.
*/
if (epnum == 1 && !set) {
dev_dbg(bdc->dev, "ep0 stall already cleared\n");
return 0;
}
dev_dbg(bdc->dev, "epnum=%d\n", epnum);
ep = bdc->bdc_ep_array[epnum];
if (!ep)
return -EINVAL;
return ep_set_halt(ep, set);
default:
dev_err(bdc->dev, "Unknown recipient\n");
return -EINVAL;
}
return 0;
}
/* GET_STATUS request handler */
static int ep0_handle_status(struct bdc *bdc,
struct usb_ctrlrequest *setup_pkt)
{
enum usb_device_state state = bdc->gadget.state;
struct bdc_ep *ep;
u16 usb_status = 0;
u32 epnum;
u16 wIndex;
/* USB2.0 spec sec 9.4.5 */
if (state == USB_STATE_DEFAULT)
return -EINVAL;
wIndex = le16_to_cpu(setup_pkt->wIndex);
dev_dbg(bdc->dev, "%s\n", __func__);
usb_status = bdc->devstatus;
switch (setup_pkt->bRequestType & USB_RECIP_MASK) {
case USB_RECIP_DEVICE:
dev_dbg(bdc->dev,
"USB_RECIP_DEVICE devstatus:%08x\n",
bdc->devstatus);
/* USB3 spec, sec 9.4.5 */
if (bdc->gadget.speed == USB_SPEED_SUPER)
usb_status &= ~REMOTE_WAKE_ENABLE;
break;
case USB_RECIP_INTERFACE:
dev_dbg(bdc->dev, "USB_RECIP_INTERFACE\n");
if (bdc->gadget.speed == USB_SPEED_SUPER) {
/*
* This should come from func for Func remote wkup
* usb_status |=1;
*/
if (bdc->devstatus & REMOTE_WAKE_ENABLE)
usb_status |= REMOTE_WAKE_ENABLE;
} else {
usb_status = 0;
}
break;
case USB_RECIP_ENDPOINT:
dev_dbg(bdc->dev, "USB_RECIP_ENDPOINT\n");
epnum = wIndex & USB_ENDPOINT_NUMBER_MASK;
if (epnum) {
if ((wIndex & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
epnum = epnum*2 + 1;
else
epnum *= 2;
} else {
epnum = 1; /* EP0 */
}
ep = bdc->bdc_ep_array[epnum];
if (!ep) {
dev_err(bdc->dev, "ISSUE, GET_STATUS for invalid EP ?");
return -EINVAL;
}
if (ep->flags & BDC_EP_STALL)
usb_status |= 1 << USB_ENDPOINT_HALT;
break;
default:
dev_err(bdc->dev, "Unknown recipient for get_status\n");
return -EINVAL;
}
/* prepare a data stage for GET_STATUS */
dev_dbg(bdc->dev, "usb_status=%08x\n", usb_status);
*(__le16 *)bdc->ep0_response_buff = cpu_to_le16(usb_status);
bdc->ep0_req.usb_req.length = 2;
bdc->ep0_req.usb_req.buf = &bdc->ep0_response_buff;
ep0_queue_data_stage(bdc);
return 0;
}
static void ep0_set_sel_cmpl(struct usb_ep *_ep, struct usb_request *_req)
{
/* ep0_set_sel_cmpl */
}
/* Queue data stage to handle 6 byte SET_SEL request */
static int ep0_set_sel(struct bdc *bdc,
struct usb_ctrlrequest *setup_pkt)
{
struct bdc_ep *ep;
u16 wLength;
dev_dbg(bdc->dev, "%s\n", __func__);
wLength = le16_to_cpu(setup_pkt->wLength);
if (unlikely(wLength != 6)) {
dev_err(bdc->dev, "%s Wrong wLength:%d\n", __func__, wLength);
return -EINVAL;
}
ep = bdc->bdc_ep_array[1];
bdc->ep0_req.ep = ep;
bdc->ep0_req.usb_req.length = 6;
bdc->ep0_req.usb_req.buf = bdc->ep0_response_buff;
bdc->ep0_req.usb_req.complete = ep0_set_sel_cmpl;
ep0_queue_data_stage(bdc);
return 0;
}
/*
* Queue a 0 byte bd only if wLength is more than the length and and length is
* a multiple of MaxPacket then queue 0 byte BD
*/
static int ep0_queue_zlp(struct bdc *bdc)
{
int ret;
dev_dbg(bdc->dev, "%s\n", __func__);
bdc->ep0_req.ep = bdc->bdc_ep_array[1];
bdc->ep0_req.usb_req.length = 0;
bdc->ep0_req.usb_req.complete = NULL;
bdc->ep0_state = WAIT_FOR_DATA_START;
ret = bdc_queue_xfr(bdc, &bdc->ep0_req);
if (ret) {
dev_err(bdc->dev, "err queueing zlp :%d\n", ret);
return ret;
}
bdc->ep0_state = WAIT_FOR_DATA_XMIT;
return 0;
}
/* Control request handler */
static int handle_control_request(struct bdc *bdc)
{
enum usb_device_state state = bdc->gadget.state;
struct usb_ctrlrequest *setup_pkt;
int delegate_setup = 0;
int ret = 0;
int config = 0;
setup_pkt = &bdc->setup_pkt;
dev_dbg(bdc->dev, "%s\n", __func__);
if ((setup_pkt->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
switch (setup_pkt->bRequest) {
case USB_REQ_SET_ADDRESS:
dev_dbg(bdc->dev, "USB_REQ_SET_ADDRESS\n");
ret = ep0_set_address(bdc, setup_pkt);
bdc->devstatus &= DEVSTATUS_CLEAR;
break;
case USB_REQ_SET_CONFIGURATION:
dev_dbg(bdc->dev, "USB_REQ_SET_CONFIGURATION\n");
if (state == USB_STATE_ADDRESS) {
usb_gadget_set_state(&bdc->gadget,
USB_STATE_CONFIGURED);
} else if (state == USB_STATE_CONFIGURED) {
/*
* USB2 spec sec 9.4.7, if wValue is 0 then dev
* is moved to addressed state
*/
config = le16_to_cpu(setup_pkt->wValue);
if (!config)
usb_gadget_set_state(
&bdc->gadget,
USB_STATE_ADDRESS);
}
delegate_setup = 1;
break;
case USB_REQ_SET_FEATURE:
dev_dbg(bdc->dev, "USB_REQ_SET_FEATURE\n");
ret = ep0_handle_feature(bdc, setup_pkt, 1);
break;
case USB_REQ_CLEAR_FEATURE:
dev_dbg(bdc->dev, "USB_REQ_CLEAR_FEATURE\n");
ret = ep0_handle_feature(bdc, setup_pkt, 0);
break;
case USB_REQ_GET_STATUS:
dev_dbg(bdc->dev, "USB_REQ_GET_STATUS\n");
ret = ep0_handle_status(bdc, setup_pkt);
break;
case USB_REQ_SET_SEL:
dev_dbg(bdc->dev, "USB_REQ_SET_SEL\n");
ret = ep0_set_sel(bdc, setup_pkt);
break;
case USB_REQ_SET_ISOCH_DELAY:
dev_warn(bdc->dev,
"USB_REQ_SET_ISOCH_DELAY not handled\n");
ret = 0;
break;
default:
delegate_setup = 1;
}
} else {
delegate_setup = 1;
}
if (delegate_setup) {
spin_unlock(&bdc->lock);
ret = bdc->gadget_driver->setup(&bdc->gadget, setup_pkt);
spin_lock(&bdc->lock);
}
return ret;
}
/* EP0: Data stage started */
void bdc_xsf_ep0_data_start(struct bdc *bdc, struct bdc_sr *sreport)
{
struct bdc_ep *ep;
int ret = 0;
dev_dbg(bdc->dev, "%s\n", __func__);
ep = bdc->bdc_ep_array[1];
/* If ep0 was stalled, the clear it first */
if (ep->flags & BDC_EP_STALL) {
ret = ep_set_halt(ep, 0);
if (ret)
goto err;
}
if (bdc->ep0_state != WAIT_FOR_DATA_START)
dev_warn(bdc->dev,
"Data stage not expected ep0_state:%s\n",
ep0_state_string[bdc->ep0_state]);
ret = handle_control_request(bdc);
if (ret == USB_GADGET_DELAYED_STATUS) {
/*
* The ep0 state will remain WAIT_FOR_DATA_START till
* we received ep_queue on ep0
*/
bdc->delayed_status = true;
return;
}
if (!ret) {
bdc->ep0_state = WAIT_FOR_DATA_XMIT;
dev_dbg(bdc->dev,
"ep0_state:%s", ep0_state_string[bdc->ep0_state]);
return;
}
err:
ep0_stall(bdc);
}
/* EP0: status stage started */
void bdc_xsf_ep0_status_start(struct bdc *bdc, struct bdc_sr *sreport)
{
struct usb_ctrlrequest *setup_pkt;
struct bdc_ep *ep;
int ret = 0;
dev_dbg(bdc->dev,
"%s ep0_state:%s",
__func__, ep0_state_string[bdc->ep0_state]);
ep = bdc->bdc_ep_array[1];
/* check if ZLP was queued? */
if (bdc->zlp_needed)
bdc->zlp_needed = false;
if (ep->flags & BDC_EP_STALL) {
ret = ep_set_halt(ep, 0);
if (ret)
goto err;
}
if ((bdc->ep0_state != WAIT_FOR_STATUS_START) &&
(bdc->ep0_state != WAIT_FOR_DATA_XMIT))
dev_err(bdc->dev,
"Status stage recv but ep0_state:%s\n",
ep0_state_string[bdc->ep0_state]);
/* check if data stage is in progress ? */
if (bdc->ep0_state == WAIT_FOR_DATA_XMIT) {
bdc->ep0_state = STATUS_PENDING;
/* Status stage will be queued upon Data stage transmit event */
dev_dbg(bdc->dev,
"status started but data not transmitted yet\n");
return;
}
setup_pkt = &bdc->setup_pkt;
/*
* 2 stage setup then only process the setup, for 3 stage setup the date
* stage is already handled
*/
if (!le16_to_cpu(setup_pkt->wLength)) {
ret = handle_control_request(bdc);
if (ret == USB_GADGET_DELAYED_STATUS) {
bdc->delayed_status = true;
/* ep0_state will remain WAIT_FOR_STATUS_START */
return;
}
}
if (!ret) {
/* Queue a status stage BD */
ep0_queue_status_stage(bdc);
bdc->ep0_state = WAIT_FOR_STATUS_XMIT;
dev_dbg(bdc->dev,
"ep0_state:%s", ep0_state_string[bdc->ep0_state]);
return;
}
err:
ep0_stall(bdc);
}
/* Helper function to update ep0 upon SR with xsf_succ or xsf_short */
static void ep0_xsf_complete(struct bdc *bdc, struct bdc_sr *sreport)
{
dev_dbg(bdc->dev, "%s\n", __func__);
switch (bdc->ep0_state) {
case WAIT_FOR_DATA_XMIT:
bdc->ep0_state = WAIT_FOR_STATUS_START;
break;
case WAIT_FOR_STATUS_XMIT:
bdc->ep0_state = WAIT_FOR_SETUP;
if (bdc->test_mode) {
int ret;
dev_dbg(bdc->dev, "test_mode:%d\n", bdc->test_mode);
ret = bdc_set_test_mode(bdc);
if (ret < 0) {
dev_err(bdc->dev, "Err in setting Test mode\n");
return;
}
bdc->test_mode = 0;
}
break;
case STATUS_PENDING:
bdc_xsf_ep0_status_start(bdc, sreport);
break;
default:
dev_err(bdc->dev,
"Unknown ep0_state:%s\n",
ep0_state_string[bdc->ep0_state]);
}
}
/* xfr completion status report handler */
void bdc_sr_xsf(struct bdc *bdc, struct bdc_sr *sreport)
{
struct bdc_ep *ep;
u32 sr_status;
u8 ep_num;
ep_num = (le32_to_cpu(sreport->offset[3])>>4) & 0x1f;
ep = bdc->bdc_ep_array[ep_num];
if (!ep || !(ep->flags & BDC_EP_ENABLED)) {
dev_err(bdc->dev, "xsf for ep not enabled\n");
return;
}
/*
* check if this transfer is after link went from U3->U0 due
* to remote wakeup
*/
if (bdc->devstatus & FUNC_WAKE_ISSUED) {
bdc->devstatus &= ~(FUNC_WAKE_ISSUED);
dev_dbg(bdc->dev, "%s clearing FUNC_WAKE_ISSUED flag\n",
__func__);
}
sr_status = XSF_STS(le32_to_cpu(sreport->offset[3]));
dev_dbg_ratelimited(bdc->dev, "%s sr_status=%d ep:%s\n",
__func__, sr_status, ep->name);
switch (sr_status) {
case XSF_SUCC:
case XSF_SHORT:
handle_xsr_succ_status(bdc, ep, sreport);
if (ep_num == 1)
ep0_xsf_complete(bdc, sreport);
break;
case XSF_SETUP_RECV:
case XSF_DATA_START:
case XSF_STATUS_START:
if (ep_num != 1) {
dev_err(bdc->dev,
"ep0 related packets on non ep0 endpoint");
return;
}
bdc->sr_xsf_ep0[sr_status - XSF_SETUP_RECV](bdc, sreport);
break;
case XSF_BABB:
if (ep_num == 1) {
dev_dbg(bdc->dev, "Babble on ep0 zlp_need:%d\n",
bdc->zlp_needed);
/*
* If the last completed transfer had wLength >Data Len,
* and Len is multiple of MaxPacket,then queue ZLP
*/
if (bdc->zlp_needed) {
/* queue 0 length bd */
ep0_queue_zlp(bdc);
return;
}
}
dev_warn(bdc->dev, "Babble on ep not handled\n");
break;
default:
dev_warn(bdc->dev, "sr status not handled:%x\n", sr_status);
break;
}
}
static int bdc_gadget_ep_queue(struct usb_ep *_ep,
struct usb_request *_req, gfp_t gfp_flags)
{
struct bdc_req *req;
unsigned long flags;
struct bdc_ep *ep;
struct bdc *bdc;
int ret;
if (!_ep || !_ep->desc)
return -ESHUTDOWN;
if (!_req || !_req->complete || !_req->buf)
return -EINVAL;
ep = to_bdc_ep(_ep);
req = to_bdc_req(_req);
bdc = ep->bdc;
dev_dbg(bdc->dev, "%s ep:%p req:%p\n", __func__, ep, req);
dev_dbg(bdc->dev, "queuing request %p to %s length %d zero:%d\n",
_req, ep->name, _req->length, _req->zero);
if (!ep->usb_ep.desc) {
dev_warn(bdc->dev,
"trying to queue req %p to disabled %s\n",
_req, ep->name);
return -ESHUTDOWN;
}
if (_req->length > MAX_XFR_LEN) {
dev_warn(bdc->dev,
"req length > supported MAX:%d requested:%d\n",
MAX_XFR_LEN, _req->length);
return -EOPNOTSUPP;
}
spin_lock_irqsave(&bdc->lock, flags);
if (ep == bdc->bdc_ep_array[1])
ret = ep0_queue(ep, req);
else
ret = ep_queue(ep, req);
spin_unlock_irqrestore(&bdc->lock, flags);
return ret;
}
static int bdc_gadget_ep_dequeue(struct usb_ep *_ep,
struct usb_request *_req)
{
struct bdc_req *req;
unsigned long flags;
struct bdc_ep *ep;
struct bdc *bdc;
int ret;
if (!_ep || !_req)
return -EINVAL;
ep = to_bdc_ep(_ep);
req = to_bdc_req(_req);
bdc = ep->bdc;
dev_dbg(bdc->dev, "%s ep:%s req:%p\n", __func__, ep->name, req);
bdc_dbg_bd_list(bdc, ep);
spin_lock_irqsave(&bdc->lock, flags);
/* make sure it's still queued on this endpoint */
list_for_each_entry(req, &ep->queue, queue) {
if (&req->usb_req == _req)
break;
}
if (&req->usb_req != _req) {
spin_unlock_irqrestore(&bdc->lock, flags);
dev_err(bdc->dev, "usb_req !=req n");
return -EINVAL;
}
ret = ep_dequeue(ep, req);
if (ret) {
ret = -EOPNOTSUPP;
goto err;
}
bdc_req_complete(ep, req, -ECONNRESET);
err:
bdc_dbg_bd_list(bdc, ep);
spin_unlock_irqrestore(&bdc->lock, flags);
return ret;
}
static int bdc_gadget_ep_set_halt(struct usb_ep *_ep, int value)
{
unsigned long flags;
struct bdc_ep *ep;
struct bdc *bdc;
int ret;
ep = to_bdc_ep(_ep);
bdc = ep->bdc;
dev_dbg(bdc->dev, "%s ep:%s value=%d\n", __func__, ep->name, value);
spin_lock_irqsave(&bdc->lock, flags);
if (usb_endpoint_xfer_isoc(ep->usb_ep.desc))
ret = -EINVAL;
else if (!list_empty(&ep->queue))
ret = -EAGAIN;
else
ret = ep_set_halt(ep, value);
spin_unlock_irqrestore(&bdc->lock, flags);
return ret;
}
static struct usb_request *bdc_gadget_alloc_request(struct usb_ep *_ep,
gfp_t gfp_flags)
{
struct bdc_req *req;
struct bdc_ep *ep;
req = kzalloc(sizeof(*req), gfp_flags);
if (!req)
return NULL;
ep = to_bdc_ep(_ep);
req->ep = ep;
req->epnum = ep->ep_num;
req->usb_req.dma = DMA_ADDR_INVALID;
dev_dbg(ep->bdc->dev, "%s ep:%s req:%p\n", __func__, ep->name, req);
return &req->usb_req;
}
static void bdc_gadget_free_request(struct usb_ep *_ep,
struct usb_request *_req)
{
struct bdc_req *req;
req = to_bdc_req(_req);
kfree(req);
}
/* endpoint operations */
/* configure endpoint and also allocate resources */
static int bdc_gadget_ep_enable(struct usb_ep *_ep,
const struct usb_endpoint_descriptor *desc)
{
unsigned long flags;
struct bdc_ep *ep;
struct bdc *bdc;
int ret;
if (!_ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
pr_debug("bdc_gadget_ep_enable invalid parameters\n");
return -EINVAL;
}
if (!desc->wMaxPacketSize) {
pr_debug("bdc_gadget_ep_enable missing wMaxPacketSize\n");
return -EINVAL;
}
ep = to_bdc_ep(_ep);
bdc = ep->bdc;
/* Sanity check, upper layer will not send enable for ep0 */
if (ep == bdc->bdc_ep_array[1])
return -EINVAL;
if (!bdc->gadget_driver
|| bdc->gadget.speed == USB_SPEED_UNKNOWN) {
return -ESHUTDOWN;
}
dev_dbg(bdc->dev, "%s Enabling %s\n", __func__, ep->name);
spin_lock_irqsave(&bdc->lock, flags);
ep->desc = desc;
ep->comp_desc = _ep->comp_desc;
ret = bdc_ep_enable(ep);
spin_unlock_irqrestore(&bdc->lock, flags);
return ret;
}
static int bdc_gadget_ep_disable(struct usb_ep *_ep)
{
unsigned long flags;
struct bdc_ep *ep;
struct bdc *bdc;
int ret;
if (!_ep) {
pr_debug("bdc: invalid parameters\n");
return -EINVAL;
}
ep = to_bdc_ep(_ep);
bdc = ep->bdc;
/* Upper layer will not call this for ep0, but do a sanity check */
if (ep == bdc->bdc_ep_array[1]) {
dev_warn(bdc->dev, "%s called for ep0\n", __func__);
return -EINVAL;
}
dev_dbg(bdc->dev,
"%s() ep:%s ep->flags:%08x\n",
__func__, ep->name, ep->flags);
if (!(ep->flags & BDC_EP_ENABLED)) {
dev_warn(bdc->dev, "%s is already disabled\n", ep->name);
return 0;
}
spin_lock_irqsave(&bdc->lock, flags);
ret = bdc_ep_disable(ep);
spin_unlock_irqrestore(&bdc->lock, flags);
return ret;
}
static const struct usb_ep_ops bdc_gadget_ep_ops = {
.enable = bdc_gadget_ep_enable,
.disable = bdc_gadget_ep_disable,
.alloc_request = bdc_gadget_alloc_request,
.free_request = bdc_gadget_free_request,
.queue = bdc_gadget_ep_queue,
.dequeue = bdc_gadget_ep_dequeue,
.set_halt = bdc_gadget_ep_set_halt
};
/* dir = 1 is IN */
static int init_ep(struct bdc *bdc, u32 epnum, u32 dir)
{
struct bdc_ep *ep;
dev_dbg(bdc->dev, "%s epnum=%d dir=%d\n", __func__, epnum, dir);
ep = kzalloc(sizeof(*ep), GFP_KERNEL);
if (!ep)
return -ENOMEM;
ep->bdc = bdc;
ep->dir = dir;
if (dir)
ep->usb_ep.caps.dir_in = true;
else
ep->usb_ep.caps.dir_out = true;
/* ep->ep_num is the index inside bdc_ep */
if (epnum == 1) {
ep->ep_num = 1;
bdc->bdc_ep_array[ep->ep_num] = ep;
snprintf(ep->name, sizeof(ep->name), "ep%d", epnum - 1);
usb_ep_set_maxpacket_limit(&ep->usb_ep, EP0_MAX_PKT_SIZE);
ep->usb_ep.caps.type_control = true;
ep->comp_desc = NULL;
bdc->gadget.ep0 = &ep->usb_ep;
} else {
if (dir)
ep->ep_num = epnum * 2 - 1;
else
ep->ep_num = epnum * 2 - 2;
bdc->bdc_ep_array[ep->ep_num] = ep;
snprintf(ep->name, sizeof(ep->name), "ep%d%s", epnum - 1,
dir & 1 ? "in" : "out");
usb_ep_set_maxpacket_limit(&ep->usb_ep, 1024);
ep->usb_ep.caps.type_iso = true;
ep->usb_ep.caps.type_bulk = true;
ep->usb_ep.caps.type_int = true;
ep->usb_ep.max_streams = 0;
list_add_tail(&ep->usb_ep.ep_list, &bdc->gadget.ep_list);
}
ep->usb_ep.ops = &bdc_gadget_ep_ops;
ep->usb_ep.name = ep->name;
ep->flags = 0;
ep->ignore_next_sr = false;
dev_dbg(bdc->dev, "ep=%p ep->usb_ep.name=%s epnum=%d ep->epnum=%d\n",
ep, ep->usb_ep.name, epnum, ep->ep_num);
INIT_LIST_HEAD(&ep->queue);
return 0;
}
/* Init all ep */
int bdc_init_ep(struct bdc *bdc)
{
u8 epnum;
int ret;
dev_dbg(bdc->dev, "%s()\n", __func__);
INIT_LIST_HEAD(&bdc->gadget.ep_list);
/* init ep0 */
ret = init_ep(bdc, 1, 0);
if (ret) {
dev_err(bdc->dev, "init ep ep0 fail %d\n", ret);
return ret;
}
for (epnum = 2; epnum <= bdc->num_eps / 2; epnum++) {
/* OUT */
ret = init_ep(bdc, epnum, 0);
if (ret) {
dev_err(bdc->dev,
"init ep failed for:%d error: %d\n",
epnum, ret);
return ret;
}
/* IN */
ret = init_ep(bdc, epnum, 1);
if (ret) {
dev_err(bdc->dev,
"init ep failed for:%d error: %d\n",
epnum, ret);
return ret;
}
}
return 0;
}
|