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
|
/* $NetBSD: usbdi.c,v 1.182.4.5 2020/07/18 15:09:28 martin Exp $ */
/*
* Copyright (c) 1998, 2012, 2015 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Lennart Augustsson (lennart@augustsson.net) at
* Carlstedt Research & Technology, Matthew R. Green (mrg@eterna.com.au),
* and Nick Hudson.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.182.4.5 2020/07/18 15:09:28 martin Exp $");
#ifdef _KERNEL_OPT
#include "opt_usb.h"
#include "opt_compat_netbsd.h"
#include "usb_dma.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/kmem.h>
#include <sys/proc.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usb_mem.h>
#include <dev/usb/usb_quirks.h>
#include <dev/usb/usbhist.h>
/* UTF-8 encoding stuff */
#include <fs/unicode.h>
extern int usbdebug;
Static usbd_status usbd_ar_pipe(struct usbd_pipe *);
Static void usbd_start_next(struct usbd_pipe *);
Static usbd_status usbd_open_pipe_ival
(struct usbd_interface *, uint8_t, uint8_t, struct usbd_pipe **, int);
static void *usbd_alloc_buffer(struct usbd_xfer *, uint32_t);
static void usbd_free_buffer(struct usbd_xfer *);
static struct usbd_xfer *usbd_alloc_xfer(struct usbd_device *, unsigned int);
static usbd_status usbd_free_xfer(struct usbd_xfer *);
static void usbd_request_async_cb(struct usbd_xfer *, void *, usbd_status);
static void usbd_xfer_timeout(void *);
static void usbd_xfer_timeout_task(void *);
static bool usbd_xfer_probe_timeout(struct usbd_xfer *);
static void usbd_xfer_cancel_timeout_async(struct usbd_xfer *);
#if defined(USB_DEBUG)
void
usbd_dump_iface(struct usbd_interface *iface)
{
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "iface %#jx", (uintptr_t)iface, 0, 0, 0);
if (iface == NULL)
return;
USBHIST_LOG(usbdebug, " device = %#jx idesc = %#jx index = %d",
(uintptr_t)iface->ui_dev, (uintptr_t)iface->ui_idesc,
iface->ui_index, 0);
USBHIST_LOG(usbdebug, " altindex=%d priv=%#jx",
iface->ui_altindex, (uintptr_t)iface->ui_priv, 0, 0);
}
void
usbd_dump_device(struct usbd_device *dev)
{
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "dev = %#jx", (uintptr_t)dev, 0, 0, 0);
if (dev == NULL)
return;
USBHIST_LOG(usbdebug, " bus = %#jx default_pipe = %#jx",
(uintptr_t)dev->ud_bus, (uintptr_t)dev->ud_pipe0, 0, 0);
USBHIST_LOG(usbdebug, " address = %jd config = %jd depth = %jd ",
dev->ud_addr, dev->ud_config, dev->ud_depth, 0);
USBHIST_LOG(usbdebug, " speed = %jd self_powered = %jd "
"power = %jd langid = %jd",
dev->ud_speed, dev->ud_selfpowered, dev->ud_power, dev->ud_langid);
}
void
usbd_dump_endpoint(struct usbd_endpoint *endp)
{
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "endp = %#jx", (uintptr_t)endp, 0, 0, 0);
if (endp == NULL)
return;
USBHIST_LOG(usbdebug, " edesc = %#jx refcnt = %jd",
(uintptr_t)endp->ue_edesc, endp->ue_refcnt, 0, 0);
if (endp->ue_edesc)
USBHIST_LOG(usbdebug, " bEndpointAddress=0x%02x",
endp->ue_edesc->bEndpointAddress, 0, 0, 0);
}
void
usbd_dump_queue(struct usbd_pipe *pipe)
{
struct usbd_xfer *xfer;
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
SIMPLEQ_FOREACH(xfer, &pipe->up_queue, ux_next) {
USBHIST_LOG(usbdebug, " xfer = %#jx", (uintptr_t)xfer,
0, 0, 0);
}
}
void
usbd_dump_pipe(struct usbd_pipe *pipe)
{
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
if (pipe == NULL)
return;
usbd_dump_iface(pipe->up_iface);
usbd_dump_device(pipe->up_dev);
usbd_dump_endpoint(pipe->up_endpoint);
USBHIST_LOG(usbdebug, "(usbd_dump_pipe)", 0, 0, 0, 0);
USBHIST_LOG(usbdebug, " running = %jd aborting = %jd",
pipe->up_running, pipe->up_aborting, 0, 0);
USBHIST_LOG(usbdebug, " intrxfer = %#jx, repeat = %jd, "
"interval = %jd", (uintptr_t)pipe->up_intrxfer, pipe->up_repeat,
pipe->up_interval, 0);
}
#endif
usbd_status
usbd_open_pipe(struct usbd_interface *iface, uint8_t address,
uint8_t flags, struct usbd_pipe **pipe)
{
return (usbd_open_pipe_ival(iface, address, flags, pipe,
USBD_DEFAULT_INTERVAL));
}
usbd_status
usbd_open_pipe_ival(struct usbd_interface *iface, uint8_t address,
uint8_t flags, struct usbd_pipe **pipe, int ival)
{
struct usbd_pipe *p;
struct usbd_endpoint *ep;
usbd_status err;
int i;
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "iface = %#jx address = 0x%jx flags = 0x%jx",
(uintptr_t)iface, address, flags, 0);
for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
ep = &iface->ui_endpoints[i];
if (ep->ue_edesc == NULL)
return USBD_IOERROR;
if (ep->ue_edesc->bEndpointAddress == address)
goto found;
}
return USBD_BAD_ADDRESS;
found:
if ((flags & USBD_EXCLUSIVE_USE) && ep->ue_refcnt != 0)
return USBD_IN_USE;
err = usbd_setup_pipe_flags(iface->ui_dev, iface, ep, ival, &p, flags);
if (err)
return err;
LIST_INSERT_HEAD(&iface->ui_pipes, p, up_next);
*pipe = p;
return USBD_NORMAL_COMPLETION;
}
usbd_status
usbd_open_pipe_intr(struct usbd_interface *iface, uint8_t address,
uint8_t flags, struct usbd_pipe **pipe,
void *priv, void *buffer, uint32_t len,
usbd_callback cb, int ival)
{
usbd_status err;
struct usbd_xfer *xfer;
struct usbd_pipe *ipipe;
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "address = 0x%jx flags = 0x%jx len = %jd",
address, flags, len, 0);
err = usbd_open_pipe_ival(iface, address,
USBD_EXCLUSIVE_USE | (flags & USBD_MPSAFE),
&ipipe, ival);
if (err)
return err;
err = usbd_create_xfer(ipipe, len, flags, 0, &xfer);
if (err)
goto bad1;
usbd_setup_xfer(xfer, priv, buffer, len, flags, USBD_NO_TIMEOUT, cb);
ipipe->up_intrxfer = xfer;
ipipe->up_repeat = 1;
err = usbd_transfer(xfer);
*pipe = ipipe;
if (err != USBD_IN_PROGRESS)
goto bad3;
return USBD_NORMAL_COMPLETION;
bad3:
ipipe->up_intrxfer = NULL;
ipipe->up_repeat = 0;
usbd_destroy_xfer(xfer);
bad1:
usbd_close_pipe(ipipe);
return err;
}
usbd_status
usbd_close_pipe(struct usbd_pipe *pipe)
{
USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
KASSERT(pipe != NULL);
usbd_lock_pipe(pipe);
if (!SIMPLEQ_EMPTY(&pipe->up_queue)) {
printf("WARNING: pipe closed with active xfers on addr %d\n",
pipe->up_dev->ud_addr);
usbd_ar_pipe(pipe);
}
KASSERT(SIMPLEQ_EMPTY(&pipe->up_queue));
LIST_REMOVE(pipe, up_next);
pipe->up_endpoint->ue_refcnt--;
pipe->up_methods->upm_close(pipe);
if (pipe->up_intrxfer != NULL) {
usbd_unlock_pipe(pipe);
usbd_destroy_xfer(pipe->up_intrxfer);
usbd_lock_pipe(pipe);
}
usbd_unlock_pipe(pipe);
kmem_free(pipe, pipe->up_dev->ud_bus->ub_pipesize);
return USBD_NORMAL_COMPLETION;
}
usbd_status
usbd_transfer(struct usbd_xfer *xfer)
{
struct usbd_pipe *pipe = xfer->ux_pipe;
usbd_status err;
unsigned int size, flags;
USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
"xfer = %#jx, flags = %#jx, pipe = %#jx, running = %jd",
(uintptr_t)xfer, xfer->ux_flags, (uintptr_t)pipe, pipe->up_running);
KASSERT(xfer->ux_status == USBD_NOT_STARTED);
#ifdef USB_DEBUG
if (usbdebug > 5)
usbd_dump_queue(pipe);
#endif
xfer->ux_done = 0;
if (pipe->up_aborting) {
USBHIST_LOG(usbdebug, "<- done xfer %#jx, aborting",
(uintptr_t)xfer, 0, 0, 0);
return USBD_CANCELLED;
}
KASSERT(xfer->ux_length == 0 || xfer->ux_buf != NULL);
size = xfer->ux_length;
flags = xfer->ux_flags;
if (size != 0) {
/*
* Use the xfer buffer if none specified in transfer setup.
* isoc transfers always use the xfer buffer, i.e.
* ux_buffer is always NULL for isoc.
*/
if (xfer->ux_buffer == NULL) {
xfer->ux_buffer = xfer->ux_buf;
}
/*
* If not using the xfer buffer copy data to the
* xfer buffer for OUT transfers of >0 length
*/
if (xfer->ux_buffer != xfer->ux_buf) {
KASSERT(xfer->ux_buf);
if (!usbd_xfer_isread(xfer)) {
memcpy(xfer->ux_buf, xfer->ux_buffer, size);
}
}
}
/* xfer is not valid after the transfer method unless synchronous */
err = pipe->up_methods->upm_transfer(xfer);
if (err != USBD_IN_PROGRESS && err) {
/*
* The transfer made it onto the pipe queue, but didn't get
* accepted by the HCD for some reason. It needs removing
* from the pipe queue.
*/
USBHIST_LOG(usbdebug, "xfer failed: %s, reinserting",
err, 0, 0, 0);
usbd_lock_pipe(pipe);
#ifdef DIAGNOSTIC
xfer->ux_state = XFER_BUSY;
#endif
SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
if (pipe->up_serialise)
usbd_start_next(pipe);
usbd_unlock_pipe(pipe);
}
if (!(flags & USBD_SYNCHRONOUS)) {
USBHIST_LOG(usbdebug, "<- done xfer %#jx, not sync (err %jd)",
(uintptr_t)xfer, err, 0, 0);
return err;
}
if (err != USBD_IN_PROGRESS) {
USBHIST_LOG(usbdebug, "<- done xfer %#jx, sync (err %jd)",
(uintptr_t)xfer, err, 0, 0);
return err;
}
/* Sync transfer, wait for completion. */
usbd_lock_pipe(pipe);
while (!xfer->ux_done) {
if (pipe->up_dev->ud_bus->ub_usepolling)
panic("usbd_transfer: not done");
USBHIST_LOG(usbdebug, "<- sleeping on xfer %#jx",
(uintptr_t)xfer, 0, 0, 0);
err = 0;
if ((flags & USBD_SYNCHRONOUS_SIG) != 0) {
err = cv_wait_sig(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
} else {
cv_wait(&xfer->ux_cv, pipe->up_dev->ud_bus->ub_lock);
}
if (err) {
if (!xfer->ux_done)
pipe->up_methods->upm_abort(xfer);
break;
}
}
usbd_unlock_pipe(pipe);
return xfer->ux_status;
}
/* Like usbd_transfer(), but waits for completion. */
usbd_status
usbd_sync_transfer(struct usbd_xfer *xfer)
{
xfer->ux_flags |= USBD_SYNCHRONOUS;
return usbd_transfer(xfer);
}
/* Like usbd_transfer(), but waits for completion and listens for signals. */
usbd_status
usbd_sync_transfer_sig(struct usbd_xfer *xfer)
{
xfer->ux_flags |= USBD_SYNCHRONOUS | USBD_SYNCHRONOUS_SIG;
return usbd_transfer(xfer);
}
static void *
usbd_alloc_buffer(struct usbd_xfer *xfer, uint32_t size)
{
KASSERT(xfer->ux_buf == NULL);
KASSERT(size != 0);
xfer->ux_bufsize = 0;
#if NUSB_DMA > 0
struct usbd_bus *bus = xfer->ux_bus;
if (bus->ub_usedma) {
usb_dma_t *dmap = &xfer->ux_dmabuf;
int err = usb_allocmem_flags(bus, size, 0, dmap, bus->ub_dmaflags);
if (err) {
return NULL;
}
xfer->ux_buf = KERNADDR(&xfer->ux_dmabuf, 0);
xfer->ux_bufsize = size;
return xfer->ux_buf;
}
#endif
KASSERT(xfer->ux_bus->ub_usedma == false);
xfer->ux_buf = kmem_alloc(size, KM_SLEEP);
xfer->ux_bufsize = size;
return xfer->ux_buf;
}
static void
usbd_free_buffer(struct usbd_xfer *xfer)
{
KASSERT(xfer->ux_buf != NULL);
KASSERT(xfer->ux_bufsize != 0);
void *buf = xfer->ux_buf;
uint32_t size = xfer->ux_bufsize;
xfer->ux_buf = NULL;
xfer->ux_bufsize = 0;
#if NUSB_DMA > 0
struct usbd_bus *bus = xfer->ux_bus;
if (bus->ub_usedma) {
usb_dma_t *dmap = &xfer->ux_dmabuf;
usb_freemem(bus, dmap);
return;
}
#endif
KASSERT(xfer->ux_bus->ub_usedma == false);
kmem_free(buf, size);
}
void *
usbd_get_buffer(struct usbd_xfer *xfer)
{
return xfer->ux_buf;
}
struct usbd_pipe *
usbd_get_pipe0(struct usbd_device *dev)
{
return dev->ud_pipe0;
}
static struct usbd_xfer *
usbd_alloc_xfer(struct usbd_device *dev, unsigned int nframes)
{
struct usbd_xfer *xfer;
USBHIST_FUNC();
ASSERT_SLEEPABLE();
xfer = dev->ud_bus->ub_methods->ubm_allocx(dev->ud_bus, nframes);
if (xfer == NULL)
goto out;
xfer->ux_bus = dev->ud_bus;
callout_init(&xfer->ux_callout, CALLOUT_MPSAFE);
callout_setfunc(&xfer->ux_callout, usbd_xfer_timeout, xfer);
cv_init(&xfer->ux_cv, "usbxfer");
usb_init_task(&xfer->ux_aborttask, usbd_xfer_timeout_task, xfer,
USB_TASKQ_MPSAFE);
out:
USBHIST_CALLARGS(usbdebug, "returns %#jx", (uintptr_t)xfer, 0, 0, 0);
return xfer;
}
static usbd_status
usbd_free_xfer(struct usbd_xfer *xfer)
{
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "%#jx", (uintptr_t)xfer, 0, 0, 0);
if (xfer->ux_buf) {
usbd_free_buffer(xfer);
}
/* Wait for any straggling timeout to complete. */
mutex_enter(xfer->ux_bus->ub_lock);
xfer->ux_timeout_reset = false; /* do not resuscitate */
callout_halt(&xfer->ux_callout, xfer->ux_bus->ub_lock);
usb_rem_task_wait(xfer->ux_pipe->up_dev, &xfer->ux_aborttask,
USB_TASKQ_HC, xfer->ux_bus->ub_lock);
mutex_exit(xfer->ux_bus->ub_lock);
cv_destroy(&xfer->ux_cv);
xfer->ux_bus->ub_methods->ubm_freex(xfer->ux_bus, xfer);
return USBD_NORMAL_COMPLETION;
}
int
usbd_create_xfer(struct usbd_pipe *pipe, size_t len, unsigned int flags,
unsigned int nframes, struct usbd_xfer **xp)
{
KASSERT(xp != NULL);
void *buf = NULL;
struct usbd_xfer *xfer = usbd_alloc_xfer(pipe->up_dev, nframes);
if (xfer == NULL)
return ENOMEM;
xfer->ux_pipe = pipe;
xfer->ux_flags = flags;
xfer->ux_nframes = nframes;
xfer->ux_methods = pipe->up_methods;
if (len) {
buf = usbd_alloc_buffer(xfer, len);
if (!buf) {
usbd_free_xfer(xfer);
return ENOMEM;
}
}
if (xfer->ux_methods->upm_init) {
int err = xfer->ux_methods->upm_init(xfer);
if (err) {
usbd_free_xfer(xfer);
return err;
}
}
*xp = xfer;
return 0;
}
void
usbd_destroy_xfer(struct usbd_xfer *xfer)
{
if (xfer->ux_methods->upm_fini) {
xfer->ux_methods->upm_fini(xfer);
}
usbd_free_xfer(xfer);
}
void
usbd_setup_xfer(struct usbd_xfer *xfer, void *priv, void *buffer,
uint32_t length, uint16_t flags, uint32_t timeout, usbd_callback callback)
{
KASSERT(xfer->ux_pipe);
xfer->ux_priv = priv;
xfer->ux_buffer = buffer;
xfer->ux_length = length;
xfer->ux_actlen = 0;
xfer->ux_flags = flags;
xfer->ux_timeout = timeout;
xfer->ux_status = USBD_NOT_STARTED;
xfer->ux_callback = callback;
xfer->ux_rqflags &= ~URQ_REQUEST;
xfer->ux_nframes = 0;
}
void
usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
void *priv, uint32_t timeout, usb_device_request_t *req, void *buffer,
uint32_t length, uint16_t flags, usbd_callback callback)
{
KASSERT(xfer->ux_pipe == dev->ud_pipe0);
xfer->ux_priv = priv;
xfer->ux_buffer = buffer;
xfer->ux_length = length;
xfer->ux_actlen = 0;
xfer->ux_flags = flags;
xfer->ux_timeout = timeout;
xfer->ux_status = USBD_NOT_STARTED;
xfer->ux_callback = callback;
xfer->ux_request = *req;
xfer->ux_rqflags |= URQ_REQUEST;
xfer->ux_nframes = 0;
}
void
usbd_setup_isoc_xfer(struct usbd_xfer *xfer, void *priv, uint16_t *frlengths,
uint32_t nframes, uint16_t flags, usbd_callback callback)
{
xfer->ux_priv = priv;
xfer->ux_buffer = NULL;
xfer->ux_length = 0;
xfer->ux_actlen = 0;
xfer->ux_flags = flags;
xfer->ux_timeout = USBD_NO_TIMEOUT;
xfer->ux_status = USBD_NOT_STARTED;
xfer->ux_callback = callback;
xfer->ux_rqflags &= ~URQ_REQUEST;
xfer->ux_frlengths = frlengths;
xfer->ux_nframes = nframes;
}
void
usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
void **buffer, uint32_t *count, usbd_status *status)
{
if (priv != NULL)
*priv = xfer->ux_priv;
if (buffer != NULL)
*buffer = xfer->ux_buffer;
if (count != NULL)
*count = xfer->ux_actlen;
if (status != NULL)
*status = xfer->ux_status;
}
usb_config_descriptor_t *
usbd_get_config_descriptor(struct usbd_device *dev)
{
KASSERT(dev != NULL);
return dev->ud_cdesc;
}
usb_interface_descriptor_t *
usbd_get_interface_descriptor(struct usbd_interface *iface)
{
KASSERT(iface != NULL);
return iface->ui_idesc;
}
usb_device_descriptor_t *
usbd_get_device_descriptor(struct usbd_device *dev)
{
KASSERT(dev != NULL);
return &dev->ud_ddesc;
}
usb_endpoint_descriptor_t *
usbd_interface2endpoint_descriptor(struct usbd_interface *iface, uint8_t index)
{
if (index >= iface->ui_idesc->bNumEndpoints)
return NULL;
return iface->ui_endpoints[index].ue_edesc;
}
/* Some drivers may wish to abort requests on the default pipe, *
* but there is no mechanism for getting a handle on it. */
usbd_status
usbd_abort_default_pipe(struct usbd_device *device)
{
return usbd_abort_pipe(device->ud_pipe0);
}
usbd_status
usbd_abort_pipe(struct usbd_pipe *pipe)
{
usbd_status err;
KASSERT(pipe != NULL);
usbd_lock_pipe(pipe);
err = usbd_ar_pipe(pipe);
usbd_unlock_pipe(pipe);
return err;
}
usbd_status
usbd_clear_endpoint_stall(struct usbd_pipe *pipe)
{
struct usbd_device *dev = pipe->up_dev;
usb_device_request_t req;
usbd_status err;
USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
/*
* Clearing en endpoint stall resets the endpoint toggle, so
* do the same to the HC toggle.
*/
pipe->up_methods->upm_cleartoggle(pipe);
req.bmRequestType = UT_WRITE_ENDPOINT;
req.bRequest = UR_CLEAR_FEATURE;
USETW(req.wValue, UF_ENDPOINT_HALT);
USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
USETW(req.wLength, 0);
err = usbd_do_request(dev, &req, 0);
#if 0
XXX should we do this?
if (!err) {
pipe->state = USBD_PIPE_ACTIVE;
/* XXX activate pipe */
}
#endif
return err;
}
void
usbd_clear_endpoint_stall_task(void *arg)
{
struct usbd_pipe *pipe = arg;
struct usbd_device *dev = pipe->up_dev;
usb_device_request_t req;
pipe->up_methods->upm_cleartoggle(pipe);
req.bmRequestType = UT_WRITE_ENDPOINT;
req.bRequest = UR_CLEAR_FEATURE;
USETW(req.wValue, UF_ENDPOINT_HALT);
USETW(req.wIndex, pipe->up_endpoint->ue_edesc->bEndpointAddress);
USETW(req.wLength, 0);
(void)usbd_do_request(dev, &req, 0);
}
void
usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe)
{
usb_add_task(pipe->up_dev, &pipe->up_async_task, USB_TASKQ_DRIVER);
}
void
usbd_clear_endpoint_toggle(struct usbd_pipe *pipe)
{
pipe->up_methods->upm_cleartoggle(pipe);
}
usbd_status
usbd_endpoint_count(struct usbd_interface *iface, uint8_t *count)
{
KASSERT(iface != NULL);
KASSERT(iface->ui_idesc != NULL);
*count = iface->ui_idesc->bNumEndpoints;
return USBD_NORMAL_COMPLETION;
}
usbd_status
usbd_interface_count(struct usbd_device *dev, uint8_t *count)
{
if (dev->ud_cdesc == NULL)
return USBD_NOT_CONFIGURED;
*count = dev->ud_cdesc->bNumInterface;
return USBD_NORMAL_COMPLETION;
}
void
usbd_interface2device_handle(struct usbd_interface *iface,
struct usbd_device **dev)
{
*dev = iface->ui_dev;
}
usbd_status
usbd_device2interface_handle(struct usbd_device *dev,
uint8_t ifaceno, struct usbd_interface **iface)
{
if (dev->ud_cdesc == NULL)
return USBD_NOT_CONFIGURED;
if (ifaceno >= dev->ud_cdesc->bNumInterface)
return USBD_INVAL;
*iface = &dev->ud_ifaces[ifaceno];
return USBD_NORMAL_COMPLETION;
}
struct usbd_device *
usbd_pipe2device_handle(struct usbd_pipe *pipe)
{
KASSERT(pipe != NULL);
return pipe->up_dev;
}
/* XXXX use altno */
usbd_status
usbd_set_interface(struct usbd_interface *iface, int altidx)
{
usb_device_request_t req;
usbd_status err;
void *endpoints;
USBHIST_FUNC();
if (LIST_FIRST(&iface->ui_pipes) != NULL)
return USBD_IN_USE;
endpoints = iface->ui_endpoints;
int nendpt = iface->ui_idesc->bNumEndpoints;
USBHIST_CALLARGS(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
(uintptr_t)iface, (uintptr_t)endpoints,
iface->ui_idesc->bNumEndpoints, 0);
err = usbd_fill_iface_data(iface->ui_dev, iface->ui_index, altidx);
if (err)
return err;
/* new setting works, we can free old endpoints */
if (endpoints != NULL) {
USBHIST_LOG(usbdebug, "iface %#jx endpoints = %#jx nendpt %jd",
(uintptr_t)iface, (uintptr_t)endpoints, nendpt, 0);
kmem_free(endpoints, nendpt * sizeof(struct usbd_endpoint));
}
KASSERT(iface->ui_idesc != NULL);
req.bmRequestType = UT_WRITE_INTERFACE;
req.bRequest = UR_SET_INTERFACE;
USETW(req.wValue, iface->ui_idesc->bAlternateSetting);
USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
USETW(req.wLength, 0);
return usbd_do_request(iface->ui_dev, &req, 0);
}
int
usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno)
{
char *p = (char *)cdesc;
char *end = p + UGETW(cdesc->wTotalLength);
usb_interface_descriptor_t *d;
int n;
for (n = 0; p < end; p += d->bLength) {
d = (usb_interface_descriptor_t *)p;
if (p + d->bLength <= end &&
d->bDescriptorType == UDESC_INTERFACE &&
d->bInterfaceNumber == ifaceno)
n++;
}
return n;
}
int
usbd_get_interface_altindex(struct usbd_interface *iface)
{
return iface->ui_altindex;
}
usbd_status
usbd_get_interface(struct usbd_interface *iface, uint8_t *aiface)
{
usb_device_request_t req;
req.bmRequestType = UT_READ_INTERFACE;
req.bRequest = UR_GET_INTERFACE;
USETW(req.wValue, 0);
USETW(req.wIndex, iface->ui_idesc->bInterfaceNumber);
USETW(req.wLength, 1);
return usbd_do_request(iface->ui_dev, &req, aiface);
}
/*** Internal routines ***/
/* Dequeue all pipe operations, called with bus lock held. */
Static usbd_status
usbd_ar_pipe(struct usbd_pipe *pipe)
{
struct usbd_xfer *xfer;
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "pipe = %#jx", (uintptr_t)pipe, 0, 0, 0);
KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
#ifdef USB_DEBUG
if (usbdebug > 5)
usbd_dump_queue(pipe);
#endif
pipe->up_repeat = 0;
pipe->up_running = 0;
pipe->up_aborting = 1;
while ((xfer = SIMPLEQ_FIRST(&pipe->up_queue)) != NULL) {
USBHIST_LOG(usbdebug, "pipe = %#jx xfer = %#jx "
"(methods = %#jx)", (uintptr_t)pipe, (uintptr_t)xfer,
(uintptr_t)pipe->up_methods, 0);
if (xfer->ux_status == USBD_NOT_STARTED) {
#ifdef DIAGNOSTIC
xfer->ux_state = XFER_BUSY;
#endif
SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
} else {
/* Make the HC abort it (and invoke the callback). */
pipe->up_methods->upm_abort(xfer);
/* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */
}
}
pipe->up_aborting = 0;
return USBD_NORMAL_COMPLETION;
}
/* Called with USB lock held. */
void
usb_transfer_complete(struct usbd_xfer *xfer)
{
struct usbd_pipe *pipe = xfer->ux_pipe;
struct usbd_bus *bus = pipe->up_dev->ud_bus;
int sync = xfer->ux_flags & USBD_SYNCHRONOUS;
int erred;
int polling = bus->ub_usepolling;
int repeat = pipe->up_repeat;
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "pipe = %#jx xfer = %#jx status = %jd "
"actlen = %jd", (uintptr_t)pipe, (uintptr_t)xfer, xfer->ux_status,
xfer->ux_actlen);
KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
KASSERTMSG(xfer->ux_state == XFER_ONQU, "xfer %p state is %x", xfer,
xfer->ux_state);
KASSERT(pipe != NULL);
/*
* If device is known to miss out ack, then pretend that
* output timeout is a success. Userland should handle
* the logic to verify that the operation succeeded.
*/
if (pipe->up_dev->ud_quirks &&
pipe->up_dev->ud_quirks->uq_flags & UQ_MISS_OUT_ACK &&
xfer->ux_status == USBD_TIMEOUT &&
!usbd_xfer_isread(xfer)) {
USBHIST_LOG(usbdebug, "Possible output ack miss for xfer %#jx: "
"hiding write timeout to %d.%s for %d bytes written",
(uintptr_t)xfer, curlwp->l_proc->p_pid, curlwp->l_lid,
xfer->ux_length);
xfer->ux_status = USBD_NORMAL_COMPLETION;
xfer->ux_actlen = xfer->ux_length;
}
erred = xfer->ux_status == USBD_CANCELLED ||
xfer->ux_status == USBD_TIMEOUT;
if (!repeat) {
/* Remove request from queue. */
KASSERTMSG(!SIMPLEQ_EMPTY(&pipe->up_queue),
"pipe %p is empty, but xfer %p wants to complete", pipe,
xfer);
KASSERTMSG(xfer == SIMPLEQ_FIRST(&pipe->up_queue),
"xfer %p is not start of queue (%p is at start)", xfer,
SIMPLEQ_FIRST(&pipe->up_queue));
#ifdef DIAGNOSTIC
xfer->ux_state = XFER_BUSY;
#endif
SIMPLEQ_REMOVE_HEAD(&pipe->up_queue, ux_next);
}
USBHIST_LOG(usbdebug, "xfer %#jx: repeat %jd new head = %#jx",
(uintptr_t)xfer, repeat, (uintptr_t)SIMPLEQ_FIRST(&pipe->up_queue),
0);
/* Count completed transfers. */
++pipe->up_dev->ud_bus->ub_stats.uds_requests
[pipe->up_endpoint->ue_edesc->bmAttributes & UE_XFERTYPE];
xfer->ux_done = 1;
if (!xfer->ux_status && xfer->ux_actlen < xfer->ux_length &&
!(xfer->ux_flags & USBD_SHORT_XFER_OK)) {
USBHIST_LOG(usbdebug, "short transfer %jd < %jd",
xfer->ux_actlen, xfer->ux_length, 0, 0);
xfer->ux_status = USBD_SHORT_XFER;
}
USBHIST_LOG(usbdebug, "xfer %#jx doing done %#jx", (uintptr_t)xfer,
(uintptr_t)pipe->up_methods->upm_done, 0, 0);
pipe->up_methods->upm_done(xfer);
if (xfer->ux_length != 0 && xfer->ux_buffer != xfer->ux_buf) {
KDASSERTMSG(xfer->ux_actlen <= xfer->ux_length,
"actlen %d length %d",xfer->ux_actlen, xfer->ux_length);
/* Only if IN transfer */
if (usbd_xfer_isread(xfer)) {
memcpy(xfer->ux_buffer, xfer->ux_buf, xfer->ux_actlen);
}
}
USBHIST_LOG(usbdebug, "xfer %#jx doing callback %#jx status %jd",
(uintptr_t)xfer, (uintptr_t)xfer->ux_callback, xfer->ux_status, 0);
if (xfer->ux_callback) {
if (!polling) {
mutex_exit(pipe->up_dev->ud_bus->ub_lock);
if (!(pipe->up_flags & USBD_MPSAFE))
KERNEL_LOCK(1, curlwp);
}
xfer->ux_callback(xfer, xfer->ux_priv, xfer->ux_status);
if (!polling) {
if (!(pipe->up_flags & USBD_MPSAFE))
KERNEL_UNLOCK_ONE(curlwp);
mutex_enter(pipe->up_dev->ud_bus->ub_lock);
}
}
if (sync && !polling) {
USBHIST_LOG(usbdebug, "<- done xfer %#jx, wakeup",
(uintptr_t)xfer, 0, 0, 0);
cv_broadcast(&xfer->ux_cv);
}
if (repeat) {
xfer->ux_actlen = 0;
xfer->ux_status = USBD_NOT_STARTED;
} else {
/* XXX should we stop the queue on all errors? */
if (erred && pipe->up_iface != NULL) /* not control pipe */
pipe->up_running = 0;
}
if (pipe->up_running && pipe->up_serialise)
usbd_start_next(pipe);
}
/* Called with USB lock held. */
usbd_status
usb_insert_transfer(struct usbd_xfer *xfer)
{
struct usbd_pipe *pipe = xfer->ux_pipe;
usbd_status err;
USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug,
"xfer = %#jx pipe = %#jx running = %jd timeout = %jd",
(uintptr_t)xfer, (uintptr_t)pipe,
pipe->up_running, xfer->ux_timeout);
KASSERT(mutex_owned(pipe->up_dev->ud_bus->ub_lock));
KASSERTMSG(xfer->ux_state == XFER_BUSY, "xfer %p state is %x", xfer,
xfer->ux_state);
#ifdef DIAGNOSTIC
xfer->ux_state = XFER_ONQU;
#endif
SIMPLEQ_INSERT_TAIL(&pipe->up_queue, xfer, ux_next);
if (pipe->up_running && pipe->up_serialise)
err = USBD_IN_PROGRESS;
else {
pipe->up_running = 1;
err = USBD_NORMAL_COMPLETION;
}
USBHIST_LOG(usbdebug, "<- done xfer %#jx, err %jd", (uintptr_t)xfer,
err, 0, 0);
return err;
}
/* Called with USB lock held. */
void
usbd_start_next(struct usbd_pipe *pipe)
{
struct usbd_xfer *xfer;
usbd_status err;
USBHIST_FUNC();
KASSERT(pipe != NULL);
KASSERT(pipe->up_methods != NULL);
KASSERT(pipe->up_methods->upm_start != NULL);
KASSERT(pipe->up_serialise == true);
int polling = pipe->up_dev->ud_bus->ub_usepolling;
KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
/* Get next request in queue. */
xfer = SIMPLEQ_FIRST(&pipe->up_queue);
USBHIST_CALLARGS(usbdebug, "pipe = %#jx, xfer = %#jx", (uintptr_t)pipe,
(uintptr_t)xfer, 0, 0);
if (xfer == NULL) {
pipe->up_running = 0;
} else {
if (!polling)
mutex_exit(pipe->up_dev->ud_bus->ub_lock);
err = pipe->up_methods->upm_start(xfer);
if (!polling)
mutex_enter(pipe->up_dev->ud_bus->ub_lock);
if (err != USBD_IN_PROGRESS) {
USBHIST_LOG(usbdebug, "error = %jd", err, 0, 0, 0);
pipe->up_running = 0;
/* XXX do what? */
}
}
KASSERT(polling || mutex_owned(pipe->up_dev->ud_bus->ub_lock));
}
usbd_status
usbd_do_request(struct usbd_device *dev, usb_device_request_t *req, void *data)
{
return usbd_do_request_flags(dev, req, data, 0, 0,
USBD_DEFAULT_TIMEOUT);
}
usbd_status
usbd_do_request_flags(struct usbd_device *dev, usb_device_request_t *req,
void *data, uint16_t flags, int *actlen, uint32_t timeout)
{
size_t len = UGETW(req->wLength);
return usbd_do_request_len(dev, req, len, data, flags, actlen, timeout);
}
usbd_status
usbd_do_request_len(struct usbd_device *dev, usb_device_request_t *req,
size_t len, void *data, uint16_t flags, int *actlen, uint32_t timeout)
{
struct usbd_xfer *xfer;
usbd_status err;
KASSERT(len >= UGETW(req->wLength));
USBHIST_FUNC();
USBHIST_CALLARGS(usbdebug, "dev=%#jx req=%jx flags=%jx len=%jx",
(uintptr_t)dev, (uintptr_t)req, flags, len);
ASSERT_SLEEPABLE();
int error = usbd_create_xfer(dev->ud_pipe0, len, 0, 0, &xfer);
if (error)
return error;
usbd_setup_default_xfer(xfer, dev, 0, timeout, req, data,
UGETW(req->wLength), flags, NULL);
KASSERT(xfer->ux_pipe == dev->ud_pipe0);
err = usbd_sync_transfer(xfer);
#if defined(USB_DEBUG) || defined(DIAGNOSTIC)
if (xfer->ux_actlen > xfer->ux_length) {
USBHIST_LOG(usbdebug, "overrun addr = %jd type = 0x%02jx",
dev->ud_addr, xfer->ux_request.bmRequestType, 0, 0);
USBHIST_LOG(usbdebug, " req = 0x%02jx val = %jd "
"index = %jd",
xfer->ux_request.bRequest, UGETW(xfer->ux_request.wValue),
UGETW(xfer->ux_request.wIndex), 0);
USBHIST_LOG(usbdebug, " rlen = %jd length = %jd "
"actlen = %jd",
UGETW(xfer->ux_request.wLength),
xfer->ux_length, xfer->ux_actlen, 0);
}
#endif
if (actlen != NULL)
*actlen = xfer->ux_actlen;
usbd_destroy_xfer(xfer);
if (err) {
USBHIST_LOG(usbdebug, "returning err = %jd", err, 0, 0, 0);
}
return err;
}
static void
usbd_request_async_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
{
usbd_free_xfer(xfer);
}
/*
* Execute a request without waiting for completion.
* Can be used from interrupt context.
*/
usbd_status
usbd_request_async(struct usbd_device *dev, struct usbd_xfer *xfer,
usb_device_request_t *req, void *priv, usbd_callback callback)
{
usbd_status err;
if (callback == NULL)
callback = usbd_request_async_cb;
usbd_setup_default_xfer(xfer, dev, priv,
USBD_DEFAULT_TIMEOUT, req, NULL, UGETW(req->wLength), 0,
callback);
err = usbd_transfer(xfer);
if (err != USBD_IN_PROGRESS) {
usbd_free_xfer(xfer);
return (err);
}
return (USBD_NORMAL_COMPLETION);
}
const struct usbd_quirks *
usbd_get_quirks(struct usbd_device *dev)
{
#ifdef DIAGNOSTIC
if (dev == NULL) {
printf("usbd_get_quirks: dev == NULL\n");
return 0;
}
#endif
return dev->ud_quirks;
}
/* XXX do periodic free() of free list */
/*
* Called from keyboard driver when in polling mode.
*/
void
usbd_dopoll(struct usbd_interface *iface)
{
iface->ui_dev->ud_bus->ub_methods->ubm_dopoll(iface->ui_dev->ud_bus);
}
/*
* This is for keyboard driver as well, which only operates in polling
* mode from the ask root, etc., prompt and from DDB.
*/
void
usbd_set_polling(struct usbd_device *dev, int on)
{
if (on)
dev->ud_bus->ub_usepolling++;
else
dev->ud_bus->ub_usepolling--;
/* Kick the host controller when switching modes */
mutex_enter(dev->ud_bus->ub_lock);
dev->ud_bus->ub_methods->ubm_softint(dev->ud_bus);
mutex_exit(dev->ud_bus->ub_lock);
}
usb_endpoint_descriptor_t *
usbd_get_endpoint_descriptor(struct usbd_interface *iface, uint8_t address)
{
struct usbd_endpoint *ep;
int i;
for (i = 0; i < iface->ui_idesc->bNumEndpoints; i++) {
ep = &iface->ui_endpoints[i];
if (ep->ue_edesc->bEndpointAddress == address)
return iface->ui_endpoints[i].ue_edesc;
}
return NULL;
}
/*
* usbd_ratecheck() can limit the number of error messages that occurs.
* When a device is unplugged it may take up to 0.25s for the hub driver
* to notice it. If the driver continuously tries to do I/O operations
* this can generate a large number of messages.
*/
int
usbd_ratecheck(struct timeval *last)
{
static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/
return ratecheck(last, &errinterval);
}
/*
* Search for a vendor/product pair in an array. The item size is
* given as an argument.
*/
const struct usb_devno *
usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz,
uint16_t vendor, uint16_t product)
{
while (nentries-- > 0) {
uint16_t tproduct = tbl->ud_product;
if (tbl->ud_vendor == vendor &&
(tproduct == product || tproduct == USB_PRODUCT_ANY))
return tbl;
tbl = (const struct usb_devno *)((const char *)tbl + sz);
}
return NULL;
}
void
usb_desc_iter_init(struct usbd_device *dev, usbd_desc_iter_t *iter)
{
const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
iter->cur = (const uByte *)cd;
iter->end = (const uByte *)cd + UGETW(cd->wTotalLength);
}
const usb_descriptor_t *
usb_desc_iter_next(usbd_desc_iter_t *iter)
{
const usb_descriptor_t *desc;
if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) {
if (iter->cur != iter->end)
printf("usb_desc_iter_next: bad descriptor\n");
return NULL;
}
desc = (const usb_descriptor_t *)iter->cur;
if (desc->bLength == 0) {
printf("usb_desc_iter_next: descriptor length = 0\n");
return NULL;
}
iter->cur += desc->bLength;
if (iter->cur > iter->end) {
printf("usb_desc_iter_next: descriptor length too large\n");
return NULL;
}
return desc;
}
usbd_status
usbd_get_string(struct usbd_device *dev, int si, char *buf)
{
return usbd_get_string0(dev, si, buf, 1);
}
usbd_status
usbd_get_string0(struct usbd_device *dev, int si, char *buf, int unicode)
{
int swap = dev->ud_quirks->uq_flags & UQ_SWAP_UNICODE;
usb_string_descriptor_t us;
char *s;
int i, n;
uint16_t c;
usbd_status err;
int size;
USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
buf[0] = '\0';
if (si == 0)
return USBD_INVAL;
if (dev->ud_quirks->uq_flags & UQ_NO_STRINGS)
return USBD_STALLED;
if (dev->ud_langid == USBD_NOLANG) {
/* Set up default language */
err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us,
&size);
if (err || size < 4) {
USBHIST_LOG(usbdebug, "getting lang failed, using 0",
0, 0, 0, 0);
dev->ud_langid = 0; /* Well, just pick something then */
} else {
/* Pick the first language as the default. */
dev->ud_langid = UGETW(us.bString[0]);
}
}
err = usbd_get_string_desc(dev, si, dev->ud_langid, &us, &size);
if (err)
return err;
s = buf;
n = size / 2 - 1;
if (unicode) {
for (i = 0; i < n; i++) {
c = UGETW(us.bString[i]);
if (swap)
c = (c >> 8) | (c << 8);
s += wput_utf8(s, 3, c);
}
*s++ = 0;
}
#ifdef COMPAT_30
else {
for (i = 0; i < n; i++) {
c = UGETW(us.bString[i]);
if (swap)
c = (c >> 8) | (c << 8);
*s++ = (c < 0x80) ? c : '?';
}
*s++ = 0;
}
#endif
return USBD_NORMAL_COMPLETION;
}
/*
* usbd_xfer_trycomplete(xfer)
*
* Try to claim xfer for completion. Return true if successful,
* false if the xfer has been synchronously aborted or has timed
* out.
*
* If this returns true, caller is responsible for setting
* xfer->ux_status and calling usb_transfer_complete. To be used
* in a host controller interrupt handler.
*
* Caller must either hold the bus lock or have the bus in polling
* mode.
*/
bool
usbd_xfer_trycomplete(struct usbd_xfer *xfer)
{
struct usbd_bus *bus __diagused = xfer->ux_bus;
KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
/*
* If software has completed it, either by synchronous abort or
* by timeout, too late.
*/
if (xfer->ux_status != USBD_IN_PROGRESS)
return false;
/*
* We are completing the xfer. Cancel the timeout if we can,
* but only asynchronously. See usbd_xfer_cancel_timeout_async
* for why we need not wait for the callout or task here.
*/
usbd_xfer_cancel_timeout_async(xfer);
/* Success! Note: Caller must set xfer->ux_status afterwar. */
return true;
}
/*
* usbd_xfer_abort(xfer)
*
* Try to claim xfer to abort. If successful, mark it completed
* with USBD_CANCELLED and call the bus-specific method to abort
* at the hardware level.
*
* To be called in thread context from struct
* usbd_pipe_methods::upm_abort.
*
* Caller must hold the bus lock.
*/
void
usbd_xfer_abort(struct usbd_xfer *xfer)
{
struct usbd_bus *bus = xfer->ux_bus;
KASSERT(mutex_owned(bus->ub_lock));
/*
* If host controller interrupt or timer interrupt has
* completed it, too late. But the xfer cannot be
* cancelled already -- only one caller can synchronously
* abort.
*/
KASSERT(xfer->ux_status != USBD_CANCELLED);
if (xfer->ux_status != USBD_IN_PROGRESS)
return;
/*
* Cancel the timeout if we can, but only asynchronously; see
* usbd_xfer_cancel_timeout_async for why we need not wait for
* the callout or task here.
*/
usbd_xfer_cancel_timeout_async(xfer);
/*
* We beat everyone else. Claim the status as cancelled and do
* the bus-specific dance to abort the hardware.
*/
xfer->ux_status = USBD_CANCELLED;
bus->ub_methods->ubm_abortx(xfer);
}
/*
* usbd_xfer_timeout(xfer)
*
* Called at IPL_SOFTCLOCK when too much time has elapsed waiting
* for xfer to complete. Since we can't abort the xfer at
* IPL_SOFTCLOCK, defer to a usb_task to run it in thread context,
* unless the xfer has completed or aborted concurrently -- and if
* the xfer has also been resubmitted, take care of rescheduling
* the callout.
*/
static void
usbd_xfer_timeout(void *cookie)
{
struct usbd_xfer *xfer = cookie;
struct usbd_bus *bus = xfer->ux_bus;
struct usbd_device *dev = xfer->ux_pipe->up_dev;
/* Acquire the lock so we can transition the timeout state. */
mutex_enter(bus->ub_lock);
/*
* Use usbd_xfer_probe_timeout to check whether the timeout is
* still valid, or to reschedule the callout if necessary. If
* it is still valid, schedule the task.
*/
if (usbd_xfer_probe_timeout(xfer))
usb_add_task(dev, &xfer->ux_aborttask, USB_TASKQ_HC);
/*
* Notify usbd_xfer_cancel_timeout_async that we may have
* scheduled the task. This causes callout_invoking to return
* false in usbd_xfer_cancel_timeout_async so that it can tell
* which stage in the callout->task->abort process we're at.
*/
callout_ack(&xfer->ux_callout);
/* All done -- release the lock. */
mutex_exit(bus->ub_lock);
}
/*
* usbd_xfer_timeout_task(xfer)
*
* Called in thread context when too much time has elapsed waiting
* for xfer to complete. Abort the xfer with USBD_TIMEOUT, unless
* it has completed or aborted concurrently -- and if the xfer has
* also been resubmitted, take care of rescheduling the callout.
*/
static void
usbd_xfer_timeout_task(void *cookie)
{
struct usbd_xfer *xfer = cookie;
struct usbd_bus *bus = xfer->ux_bus;
/* Acquire the lock so we can transition the timeout state. */
mutex_enter(bus->ub_lock);
/*
* Use usbd_xfer_probe_timeout to check whether the timeout is
* still valid, or to reschedule the callout if necessary. If
* it is not valid -- the timeout has been asynchronously
* cancelled, or the xfer has already been resubmitted -- then
* we're done here.
*/
if (!usbd_xfer_probe_timeout(xfer))
goto out;
/*
* May have completed or been aborted, but we're the only one
* who can time it out. If it has completed or been aborted,
* no need to timeout.
*/
KASSERT(xfer->ux_status != USBD_TIMEOUT);
if (xfer->ux_status != USBD_IN_PROGRESS)
goto out;
/*
* We beat everyone else. Claim the status as timed out and do
* the bus-specific dance to abort the hardware.
*/
xfer->ux_status = USBD_TIMEOUT;
bus->ub_methods->ubm_abortx(xfer);
out: /* All done -- release the lock. */
mutex_exit(bus->ub_lock);
}
/*
* usbd_xfer_probe_timeout(xfer)
*
* Probe the status of xfer's timeout. Acknowledge and process a
* request to reschedule. Return true if the timeout is still
* valid and the caller should take further action (queueing a
* task or aborting the xfer), false if it must stop here.
*/
static bool
usbd_xfer_probe_timeout(struct usbd_xfer *xfer)
{
struct usbd_bus *bus = xfer->ux_bus;
bool valid;
KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
/* The timeout must be set. */
KASSERT(xfer->ux_timeout_set);
/*
* Neither callout nor task may be pending; they execute
* alternately in lock step.
*/
KASSERT(!callout_pending(&xfer->ux_callout));
KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
/* There are a few cases... */
if (bus->ub_methods->ubm_dying(bus)) {
/* Host controller dying. Drop it all on the floor. */
xfer->ux_timeout_set = false;
xfer->ux_timeout_reset = false;
valid = false;
} else if (xfer->ux_timeout_reset) {
/*
* The xfer completed _and_ got resubmitted while we
* waited for the lock. Acknowledge the request to
* reschedule, and reschedule it if there is a timeout
* and the bus is not polling.
*/
xfer->ux_timeout_reset = false;
if (xfer->ux_timeout && !bus->ub_usepolling) {
KASSERT(xfer->ux_timeout_set);
callout_schedule(&xfer->ux_callout,
mstohz(xfer->ux_timeout));
} else {
/* No more callout or task scheduled. */
xfer->ux_timeout_set = false;
}
valid = false;
} else if (xfer->ux_status != USBD_IN_PROGRESS) {
/*
* The xfer has completed by hardware completion or by
* software abort, and has not been resubmitted, so the
* timeout must be unset, and is no longer valid for
* the caller.
*/
xfer->ux_timeout_set = false;
valid = false;
} else {
/*
* The xfer has not yet completed, so the timeout is
* valid.
*/
valid = true;
}
/* Any reset must have been processed. */
KASSERT(!xfer->ux_timeout_reset);
/*
* Either we claim the timeout is set, or the callout is idle.
* If the timeout is still set, we may be handing off to the
* task instead, so this is an if but not an iff.
*/
KASSERT(xfer->ux_timeout_set || !callout_pending(&xfer->ux_callout));
/*
* The task must be idle now.
*
* - If the caller is the callout, _and_ the timeout is still
* valid, the caller will schedule it, but it hasn't been
* scheduled yet. (If the timeout is not valid, the task
* should not be scheduled.)
*
* - If the caller is the task, it cannot be scheduled again
* until the callout runs again, which won't happen until we
* next release the lock.
*/
KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
return valid;
}
/*
* usbd_xfer_schedule_timeout(xfer)
*
* Ensure that xfer has a timeout. If the callout is already
* queued or the task is already running, request that they
* reschedule the callout. If not, and if we're not polling,
* schedule the callout anew.
*
* To be called in thread context from struct
* usbd_pipe_methods::upm_start.
*/
void
usbd_xfer_schedule_timeout(struct usbd_xfer *xfer)
{
struct usbd_bus *bus = xfer->ux_bus;
KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
if (xfer->ux_timeout_set) {
/*
* Callout or task has fired from a prior completed
* xfer but has not yet noticed that the xfer is done.
* Ask it to reschedule itself to ux_timeout.
*/
xfer->ux_timeout_reset = true;
} else if (xfer->ux_timeout && !bus->ub_usepolling) {
/* Callout is not scheduled. Schedule it. */
KASSERT(!callout_pending(&xfer->ux_callout));
callout_schedule(&xfer->ux_callout, mstohz(xfer->ux_timeout));
xfer->ux_timeout_set = true;
}
KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
}
/*
* usbd_xfer_cancel_timeout_async(xfer)
*
* Cancel the callout and the task of xfer, which have not yet run
* to completion, but don't wait for the callout or task to finish
* running.
*
* If they have already fired, at worst they are waiting for the
* bus lock. They will see that the xfer is no longer in progress
* and give up, or they will see that the xfer has been
* resubmitted with a new timeout and reschedule the callout.
*
* If a resubmitted request completed so fast that the callout
* didn't have time to process a timer reset, just cancel the
* timer reset.
*/
static void
usbd_xfer_cancel_timeout_async(struct usbd_xfer *xfer)
{
struct usbd_bus *bus __diagused = xfer->ux_bus;
KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
/*
* If the timer wasn't running anyway, forget about it. This
* can happen if we are completing an isochronous transfer
* which doesn't use the same timeout logic.
*/
if (!xfer->ux_timeout_set)
return;
xfer->ux_timeout_reset = false;
if (!callout_stop(&xfer->ux_callout)) {
/*
* We stopped the callout before it ran. The timeout
* is no longer set.
*/
xfer->ux_timeout_set = false;
} else if (callout_invoking(&xfer->ux_callout)) {
/*
* The callout has begun to run but it has not yet
* acquired the lock and called callout_ack. The task
* cannot be queued yet, and the callout cannot have
* been rescheduled yet.
*
* By the time the callout acquires the lock, we will
* have transitioned from USBD_IN_PROGRESS to a
* completed status, and possibly also resubmitted the
* xfer and set xfer->ux_timeout_reset = true. In both
* cases, the callout will DTRT, so no further action
* is needed here.
*/
} else if (usb_rem_task(xfer->ux_pipe->up_dev, &xfer->ux_aborttask)) {
/*
* The callout had fired and scheduled the task, but we
* stopped the task before it could run. The timeout
* is therefore no longer set -- the next resubmission
* of the xfer must schedule a new timeout.
*
* The callout should not be be pending at this point:
* it is scheduled only under the lock, and only when
* xfer->ux_timeout_set is false, or by the callout or
* task itself when xfer->ux_timeout_reset is true.
*/
xfer->ux_timeout_set = false;
}
/*
* The callout cannot be scheduled and the task cannot be
* queued at this point. Either we cancelled them, or they are
* already running and waiting for the bus lock.
*/
KASSERT(!callout_pending(&xfer->ux_callout));
KASSERT(!usb_task_pending(xfer->ux_pipe->up_dev, &xfer->ux_aborttask));
KASSERT(bus->ub_usepolling || mutex_owned(bus->ub_lock));
}
|