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#include <linux/gcd.h>
42#include <linux/kernel.h>
43#include <linux/module.h>
44#include <linux/spinlock.h>
45#include <linux/interrupt.h>
46#include <linux/dma-mapping.h>
47#include <linux/io.h>
48#include <linux/slab.h>
49#include <linux/usb.h>
50
51#include <linux/usb/hcd.h>
52#include <linux/usb/ch11.h>
53
54#include "core.h"
55#include "hcd.h"
56
57
58#define DWC2_UNRESERVE_DELAY (msecs_to_jiffies(5))
59
60
61
62
63
64
65
66
67
68static int dwc2_periodic_channel_available(struct dwc2_hsotg *hsotg)
69{
70
71
72
73
74
75 int status;
76 int num_channels;
77
78 num_channels = hsotg->core_params->host_channels;
79 if (hsotg->periodic_channels + hsotg->non_periodic_channels <
80 num_channels
81 && hsotg->periodic_channels < num_channels - 1) {
82 status = 0;
83 } else {
84 dev_dbg(hsotg->dev,
85 "%s: Total channels: %d, Periodic: %d, "
86 "Non-periodic: %d\n", __func__, num_channels,
87 hsotg->periodic_channels, hsotg->non_periodic_channels);
88 status = -ENOSPC;
89 }
90
91 return status;
92}
93
94
95
96
97
98
99
100
101
102
103
104
105
106static int dwc2_check_periodic_bandwidth(struct dwc2_hsotg *hsotg,
107 struct dwc2_qh *qh)
108{
109 int status;
110 s16 max_claimed_usecs;
111
112 status = 0;
113
114 if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
115
116
117
118
119 max_claimed_usecs = 100 - qh->host_us;
120 } else {
121
122
123
124
125 max_claimed_usecs = 900 - qh->host_us;
126 }
127
128 if (hsotg->periodic_usecs > max_claimed_usecs) {
129 dev_err(hsotg->dev,
130 "%s: already claimed usecs %d, required usecs %d\n",
131 __func__, hsotg->periodic_usecs, qh->host_us);
132 status = -ENOSPC;
133 }
134
135 return status;
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
229static int pmap_schedule(unsigned long *map, int bits_per_period,
230 int periods_in_map, int num_bits,
231 int interval, int start, bool only_one_period)
232{
233 int interval_bits;
234 int to_reserve;
235 int first_end;
236 int i;
237
238 if (num_bits > bits_per_period)
239 return -ENOSPC;
240
241
242 interval = gcd(interval, periods_in_map);
243
244 interval_bits = bits_per_period * interval;
245 to_reserve = periods_in_map / interval;
246
247
248 if (start >= interval_bits)
249 return -ENOSPC;
250
251 if (only_one_period)
252
253 first_end = (start / bits_per_period + 1) * bits_per_period;
254 else
255
256 first_end = interval_bits;
257
258
259
260
261
262
263
264 while (start + num_bits <= first_end) {
265 int end;
266
267
268 end = (start / bits_per_period + 1) * bits_per_period;
269
270
271 start = bitmap_find_next_zero_area(map, end, start, num_bits,
272 0);
273
274
275
276
277
278
279 if (start >= end) {
280 start = end;
281 continue;
282 }
283
284
285 for (i = 1; i < to_reserve; i++) {
286 int ith_start = start + interval_bits * i;
287 int ith_end = end + interval_bits * i;
288 int ret;
289
290
291 ret = bitmap_find_next_zero_area(
292 map, ith_start + num_bits, ith_start, num_bits,
293 0);
294
295
296 if (ret == ith_start)
297 continue;
298
299
300 ith_start = bitmap_find_next_zero_area(
301 map, ith_end, ith_start, num_bits, 0);
302 if (ith_start >= ith_end)
303
304 start = end;
305 else
306 start = ith_start - interval_bits * i;
307 break;
308 }
309
310
311 if (i == to_reserve)
312 break;
313 }
314
315 if (start + num_bits > first_end)
316 return -ENOSPC;
317
318 for (i = 0; i < to_reserve; i++) {
319 int ith_start = start + interval_bits * i;
320
321 bitmap_set(map, ith_start, num_bits);
322 }
323
324 return start;
325}
326
327
328
329
330
331
332
333
334
335
336
337static void pmap_unschedule(unsigned long *map, int bits_per_period,
338 int periods_in_map, int num_bits,
339 int interval, int start)
340{
341 int interval_bits;
342 int to_release;
343 int i;
344
345
346 interval = gcd(interval, periods_in_map);
347
348 interval_bits = bits_per_period * interval;
349 to_release = periods_in_map / interval;
350
351 for (i = 0; i < to_release; i++) {
352 int ith_start = start + interval_bits * i;
353
354 bitmap_clear(map, ith_start, num_bits);
355 }
356}
357
358
359
360
361
362
363
364
365
366
367
368
369
370static void cat_printf(char **buf, size_t *size, const char *fmt, ...)
371{
372 va_list args;
373 int i;
374
375 if (*size == 0)
376 return;
377
378 va_start(args, fmt);
379 i = vsnprintf(*buf, *size, fmt, args);
380 va_end(args);
381
382 if (i >= *size) {
383 (*buf)[*size - 1] = '\0';
384 *buf += *size;
385 *size = 0;
386 } else {
387 *buf += i;
388 *size -= i;
389 }
390}
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405static void pmap_print(unsigned long *map, int bits_per_period,
406 int periods_in_map, const char *period_name,
407 const char *units,
408 void (*print_fn)(const char *str, void *data),
409 void *print_data)
410{
411 int period;
412
413 for (period = 0; period < periods_in_map; period++) {
414 char tmp[64];
415 char *buf = tmp;
416 size_t buf_size = sizeof(tmp);
417 int period_start = period * bits_per_period;
418 int period_end = period_start + bits_per_period;
419 int start = 0;
420 int count = 0;
421 bool printed = false;
422 int i;
423
424 for (i = period_start; i < period_end + 1; i++) {
425
426 if (i < period_end &&
427 bitmap_find_next_zero_area(map, i + 1,
428 i, 1, 0) != i) {
429 if (count == 0)
430 start = i - period_start;
431 count++;
432 continue;
433 }
434
435
436 if (count == 0)
437 continue;
438
439 if (!printed)
440 cat_printf(&buf, &buf_size, "%s %d: ",
441 period_name, period);
442 else
443 cat_printf(&buf, &buf_size, ", ");
444 printed = true;
445
446 cat_printf(&buf, &buf_size, "%d %s -%3d %s", start,
447 units, start + count - 1, units);
448 count = 0;
449 }
450
451 if (printed)
452 print_fn(tmp, print_data);
453 }
454}
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469static unsigned long *dwc2_get_ls_map(struct dwc2_hsotg *hsotg,
470 struct dwc2_qh *qh)
471{
472 unsigned long *map;
473
474
475 if (WARN_ON(!qh->dwc_tt))
476 return NULL;
477
478
479 map = qh->dwc_tt->periodic_bitmaps;
480 if (qh->dwc_tt->usb_tt->multi)
481 map += DWC2_ELEMENTS_PER_LS_BITMAP * qh->ttport;
482
483 return map;
484}
485
486struct dwc2_qh_print_data {
487 struct dwc2_hsotg *hsotg;
488 struct dwc2_qh *qh;
489};
490
491
492
493
494
495
496
497static void dwc2_qh_print(const char *str, void *data)
498{
499 struct dwc2_qh_print_data *print_data = data;
500
501 dwc2_sch_dbg(print_data->hsotg, "QH=%p ...%s\n", print_data->qh, str);
502}
503
504
505
506
507
508
509
510static void dwc2_qh_schedule_print(struct dwc2_hsotg *hsotg,
511 struct dwc2_qh *qh)
512{
513 struct dwc2_qh_print_data print_data = { hsotg, qh };
514 int i;
515
516
517
518
519
520
521#ifndef DWC2_PRINT_SCHEDULE
522 return;
523#endif
524
525 if (qh->schedule_low_speed) {
526 unsigned long *map = dwc2_get_ls_map(hsotg, qh);
527
528 dwc2_sch_dbg(hsotg, "QH=%p LS/FS trans: %d=>%d us @ %d us",
529 qh, qh->device_us,
530 DWC2_ROUND_US_TO_SLICE(qh->device_us),
531 DWC2_US_PER_SLICE * qh->ls_start_schedule_slice);
532
533 if (map) {
534 dwc2_sch_dbg(hsotg,
535 "QH=%p Whole low/full speed map %p now:\n",
536 qh, map);
537 pmap_print(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
538 DWC2_LS_SCHEDULE_FRAMES, "Frame ", "slices",
539 dwc2_qh_print, &print_data);
540 }
541 }
542
543 for (i = 0; i < qh->num_hs_transfers; i++) {
544 struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + i;
545 int uframe = trans_time->start_schedule_us /
546 DWC2_HS_PERIODIC_US_PER_UFRAME;
547 int rel_us = trans_time->start_schedule_us %
548 DWC2_HS_PERIODIC_US_PER_UFRAME;
549
550 dwc2_sch_dbg(hsotg,
551 "QH=%p HS trans #%d: %d us @ uFrame %d + %d us\n",
552 qh, i, trans_time->duration_us, uframe, rel_us);
553 }
554 if (qh->num_hs_transfers) {
555 dwc2_sch_dbg(hsotg, "QH=%p Whole high speed map now:\n", qh);
556 pmap_print(hsotg->hs_periodic_bitmap,
557 DWC2_HS_PERIODIC_US_PER_UFRAME,
558 DWC2_HS_SCHEDULE_UFRAMES, "uFrame", "us",
559 dwc2_qh_print, &print_data);
560 }
561
562}
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579static int dwc2_ls_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
580 int search_slice)
581{
582 int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE);
583 unsigned long *map = dwc2_get_ls_map(hsotg, qh);
584 int slice;
585
586 if (map == NULL)
587 return -EINVAL;
588
589
590
591
592
593
594
595
596
597
598
599
600
601 slice = pmap_schedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
602 DWC2_LS_SCHEDULE_FRAMES, slices,
603 qh->device_interval, search_slice, false);
604
605 if (slice < 0)
606 return slice;
607
608 qh->ls_start_schedule_slice = slice;
609 return 0;
610}
611
612
613
614
615
616
617
618static void dwc2_ls_pmap_unschedule(struct dwc2_hsotg *hsotg,
619 struct dwc2_qh *qh)
620{
621 int slices = DIV_ROUND_UP(qh->device_us, DWC2_US_PER_SLICE);
622 unsigned long *map = dwc2_get_ls_map(hsotg, qh);
623
624
625 if (map == NULL)
626 return;
627
628 pmap_unschedule(map, DWC2_LS_PERIODIC_SLICES_PER_FRAME,
629 DWC2_LS_SCHEDULE_FRAMES, slices, qh->device_interval,
630 qh->ls_start_schedule_slice);
631}
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653static int dwc2_hs_pmap_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
654 bool only_one_period, int index)
655{
656 struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index;
657 int us;
658
659 us = pmap_schedule(hsotg->hs_periodic_bitmap,
660 DWC2_HS_PERIODIC_US_PER_UFRAME,
661 DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us,
662 qh->host_interval, trans_time->start_schedule_us,
663 only_one_period);
664
665 if (us < 0)
666 return us;
667
668 trans_time->start_schedule_us = us;
669 return 0;
670}
671
672
673
674
675
676
677
678static void dwc2_hs_pmap_unschedule(struct dwc2_hsotg *hsotg,
679 struct dwc2_qh *qh, int index)
680{
681 struct dwc2_hs_transfer_time *trans_time = qh->hs_transfers + index;
682
683 pmap_unschedule(hsotg->hs_periodic_bitmap,
684 DWC2_HS_PERIODIC_US_PER_UFRAME,
685 DWC2_HS_SCHEDULE_UFRAMES, trans_time->duration_us,
686 qh->host_interval, trans_time->start_schedule_us);
687}
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702static int dwc2_uframe_schedule_split(struct dwc2_hsotg *hsotg,
703 struct dwc2_qh *qh)
704{
705 int bytecount = dwc2_hb_mult(qh->maxp) * dwc2_max_packet(qh->maxp);
706 int ls_search_slice;
707 int err = 0;
708 int host_interval_in_sched;
709
710
711
712
713
714 host_interval_in_sched = gcd(qh->host_interval,
715 DWC2_HS_SCHEDULE_UFRAMES);
716
717
718
719
720
721
722
723
724
725
726
727
728 ls_search_slice = 0;
729
730 while (ls_search_slice < DWC2_LS_SCHEDULE_SLICES) {
731 int start_s_uframe;
732 int ssplit_s_uframe;
733 int second_s_uframe;
734 int rel_uframe;
735 int first_count;
736 int middle_count;
737 int end_count;
738 int first_data_bytes;
739 int other_data_bytes;
740 int i;
741
742 if (qh->schedule_low_speed) {
743 err = dwc2_ls_pmap_schedule(hsotg, qh, ls_search_slice);
744
745
746
747
748
749
750
751 if (err)
752 return err;
753 } else {
754
755 WARN_ON_ONCE(1);
756 }
757
758
759
760
761
762 start_s_uframe = qh->ls_start_schedule_slice /
763 DWC2_SLICES_PER_UFRAME;
764
765
766 rel_uframe = (start_s_uframe % 8);
767
768
769
770
771
772
773
774
775
776 if (rel_uframe == 7) {
777 if (qh->schedule_low_speed)
778 dwc2_ls_pmap_unschedule(hsotg, qh);
779 ls_search_slice =
780 (qh->ls_start_schedule_slice /
781 DWC2_LS_PERIODIC_SLICES_PER_FRAME + 1) *
782 DWC2_LS_PERIODIC_SLICES_PER_FRAME;
783 continue;
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 ssplit_s_uframe = (start_s_uframe +
818 host_interval_in_sched - 1) %
819 host_interval_in_sched;
820 if (qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in)
821 second_s_uframe = start_s_uframe;
822 else
823 second_s_uframe = start_s_uframe + 1;
824
825
826 first_data_bytes = 188 -
827 DIV_ROUND_UP(188 * (qh->ls_start_schedule_slice %
828 DWC2_SLICES_PER_UFRAME),
829 DWC2_SLICES_PER_UFRAME);
830 if (first_data_bytes > bytecount)
831 first_data_bytes = bytecount;
832 other_data_bytes = bytecount - first_data_bytes;
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849 if (!qh->ep_is_in &&
850 (first_data_bytes != min_t(int, 188, bytecount))) {
851 dwc2_sch_dbg(hsotg,
852 "QH=%p avoiding broken 1st xfer (%d, %d)\n",
853 qh, first_data_bytes, bytecount);
854 if (qh->schedule_low_speed)
855 dwc2_ls_pmap_unschedule(hsotg, qh);
856 ls_search_slice = (start_s_uframe + 1) *
857 DWC2_SLICES_PER_UFRAME;
858 continue;
859 }
860
861
862 qh->num_hs_transfers = 1 + DIV_ROUND_UP(other_data_bytes, 188);
863
864
865
866
867
868
869 if (qh->ep_type == USB_ENDPOINT_XFER_INT) {
870 if (rel_uframe == 6)
871 qh->num_hs_transfers += 2;
872 else
873 qh->num_hs_transfers += 3;
874
875 if (qh->ep_is_in) {
876
877
878
879
880 first_count = 4;
881 middle_count = bytecount;
882 end_count = bytecount;
883 } else {
884
885
886
887
888
889 first_count = first_data_bytes;
890 middle_count = max_t(int, 4, other_data_bytes);
891 end_count = 4;
892 }
893 } else {
894 if (qh->ep_is_in) {
895 int last;
896
897
898 qh->num_hs_transfers++;
899
900
901 last = rel_uframe + qh->num_hs_transfers + 1;
902
903
904 if (last <= 6)
905 qh->num_hs_transfers += 2;
906 else
907 qh->num_hs_transfers += 1;
908
909
910 if (last >= 6 && rel_uframe == 0)
911 qh->num_hs_transfers--;
912
913
914 first_count = 4;
915 middle_count = min_t(int, 188, bytecount);
916 end_count = middle_count;
917 } else {
918
919 first_count = first_data_bytes;
920 middle_count = min_t(int, 188,
921 other_data_bytes);
922 end_count = other_data_bytes % 188;
923 }
924 }
925
926
927 qh->hs_transfers[0].duration_us = HS_USECS_ISO(first_count);
928 for (i = 1; i < qh->num_hs_transfers - 1; i++)
929 qh->hs_transfers[i].duration_us =
930 HS_USECS_ISO(middle_count);
931 if (qh->num_hs_transfers > 1)
932 qh->hs_transfers[qh->num_hs_transfers - 1].duration_us =
933 HS_USECS_ISO(end_count);
934
935
936
937
938
939
940 qh->hs_transfers[0].start_schedule_us =
941 ssplit_s_uframe * DWC2_HS_PERIODIC_US_PER_UFRAME;
942 for (i = 1; i < qh->num_hs_transfers; i++)
943 qh->hs_transfers[i].start_schedule_us =
944 ((second_s_uframe + i - 1) %
945 DWC2_HS_SCHEDULE_UFRAMES) *
946 DWC2_HS_PERIODIC_US_PER_UFRAME;
947
948
949 for (i = 0; i < qh->num_hs_transfers; i++) {
950 err = dwc2_hs_pmap_schedule(hsotg, qh, true, i);
951 if (err)
952 break;
953 }
954
955
956 if (i == qh->num_hs_transfers)
957 break;
958
959 for (; i >= 0; i--)
960 dwc2_hs_pmap_unschedule(hsotg, qh, i);
961
962 if (qh->schedule_low_speed)
963 dwc2_ls_pmap_unschedule(hsotg, qh);
964
965
966 ls_search_slice = (start_s_uframe + 1) * DWC2_SLICES_PER_UFRAME;
967 }
968
969 if (ls_search_slice >= DWC2_LS_SCHEDULE_SLICES)
970 return -ENOSPC;
971
972 return 0;
973}
974
975
976
977
978
979
980
981
982
983
984static int dwc2_uframe_schedule_hs(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
985{
986
987 WARN_ON(qh->host_us != qh->device_us);
988 WARN_ON(qh->host_interval != qh->device_interval);
989 WARN_ON(qh->num_hs_transfers != 1);
990
991
992 qh->hs_transfers[0].start_schedule_us = 0;
993 qh->hs_transfers[0].duration_us = qh->host_us;
994
995 return dwc2_hs_pmap_schedule(hsotg, qh, false, 0);
996}
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007static int dwc2_uframe_schedule_ls(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1008{
1009
1010 WARN_ON(qh->host_us != qh->device_us);
1011 WARN_ON(qh->host_interval != qh->device_interval);
1012 WARN_ON(!qh->schedule_low_speed);
1013
1014
1015 return dwc2_ls_pmap_schedule(hsotg, qh, 0);
1016}
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027static int dwc2_uframe_schedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1028{
1029 int ret;
1030
1031 if (qh->dev_speed == USB_SPEED_HIGH)
1032 ret = dwc2_uframe_schedule_hs(hsotg, qh);
1033 else if (!qh->do_split)
1034 ret = dwc2_uframe_schedule_ls(hsotg, qh);
1035 else
1036 ret = dwc2_uframe_schedule_split(hsotg, qh);
1037
1038 if (ret)
1039 dwc2_sch_dbg(hsotg, "QH=%p Failed to schedule %d\n", qh, ret);
1040 else
1041 dwc2_qh_schedule_print(hsotg, qh);
1042
1043 return ret;
1044}
1045
1046
1047
1048
1049
1050
1051
1052static void dwc2_uframe_unschedule(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1053{
1054 int i;
1055
1056 for (i = 0; i < qh->num_hs_transfers; i++)
1057 dwc2_hs_pmap_unschedule(hsotg, qh, i);
1058
1059 if (qh->schedule_low_speed)
1060 dwc2_ls_pmap_unschedule(hsotg, qh);
1061
1062 dwc2_sch_dbg(hsotg, "QH=%p Unscheduled\n", qh);
1063}
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080static void dwc2_pick_first_frame(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1081{
1082 u16 frame_number;
1083 u16 earliest_frame;
1084 u16 next_active_frame;
1085 u16 relative_frame;
1086 u16 interval;
1087
1088
1089
1090
1091
1092 frame_number = dwc2_hcd_get_frame_number(hsotg);
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102 earliest_frame = dwc2_frame_num_inc(frame_number, 1);
1103 next_active_frame = earliest_frame;
1104
1105
1106 if (hsotg->core_params->uframe_sched <= 0) {
1107 if (qh->do_split)
1108
1109 next_active_frame |= 0x7;
1110 goto exit;
1111 }
1112
1113 if (qh->dev_speed == USB_SPEED_HIGH || qh->do_split) {
1114
1115
1116
1117
1118
1119
1120 WARN_ON(qh->num_hs_transfers < 1);
1121
1122 relative_frame = qh->hs_transfers[0].start_schedule_us /
1123 DWC2_HS_PERIODIC_US_PER_UFRAME;
1124
1125
1126 interval = gcd(qh->host_interval, DWC2_HS_SCHEDULE_UFRAMES);
1127
1128 } else {
1129
1130
1131
1132
1133
1134
1135
1136 relative_frame = qh->ls_start_schedule_slice /
1137 DWC2_LS_PERIODIC_SLICES_PER_FRAME;
1138 interval = gcd(qh->host_interval, DWC2_LS_SCHEDULE_FRAMES);
1139 }
1140
1141
1142 WARN_ON(relative_frame >= interval);
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152 next_active_frame = (next_active_frame / interval) * interval;
1153
1154
1155
1156
1157
1158 next_active_frame = dwc2_frame_num_inc(next_active_frame,
1159 relative_frame);
1160
1161
1162
1163
1164
1165
1166 next_active_frame = dwc2_frame_num_dec(next_active_frame, 1);
1167
1168
1169
1170
1171
1172 while (dwc2_frame_num_gt(earliest_frame, next_active_frame))
1173 next_active_frame = dwc2_frame_num_inc(next_active_frame,
1174 interval);
1175
1176exit:
1177 qh->next_active_frame = next_active_frame;
1178 qh->start_active_frame = next_active_frame;
1179
1180 dwc2_sch_vdbg(hsotg, "QH=%p First fn=%04x nxt=%04x\n",
1181 qh, frame_number, qh->next_active_frame);
1182}
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195static int dwc2_do_reserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1196{
1197 int status;
1198
1199 if (hsotg->core_params->uframe_sched > 0) {
1200 status = dwc2_uframe_schedule(hsotg, qh);
1201 } else {
1202 status = dwc2_periodic_channel_available(hsotg);
1203 if (status) {
1204 dev_info(hsotg->dev,
1205 "%s: No host channel available for periodic transfer\n",
1206 __func__);
1207 return status;
1208 }
1209
1210 status = dwc2_check_periodic_bandwidth(hsotg, qh);
1211 }
1212
1213 if (status) {
1214 dev_dbg(hsotg->dev,
1215 "%s: Insufficient periodic bandwidth for periodic transfer\n",
1216 __func__);
1217 return status;
1218 }
1219
1220 if (hsotg->core_params->uframe_sched <= 0)
1221
1222 hsotg->periodic_channels++;
1223
1224
1225 hsotg->periodic_usecs += qh->host_us;
1226
1227 dwc2_pick_first_frame(hsotg, qh);
1228
1229 return 0;
1230}
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241static void dwc2_do_unreserve(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1242{
1243 assert_spin_locked(&hsotg->lock);
1244
1245 WARN_ON(!qh->unreserve_pending);
1246
1247
1248 qh->unreserve_pending = false;
1249
1250 if (WARN_ON(!list_empty(&qh->qh_list_entry)))
1251 list_del_init(&qh->qh_list_entry);
1252
1253
1254 hsotg->periodic_usecs -= qh->host_us;
1255
1256 if (hsotg->core_params->uframe_sched > 0) {
1257 dwc2_uframe_unschedule(hsotg, qh);
1258 } else {
1259
1260 hsotg->periodic_channels--;
1261 }
1262}
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276static void dwc2_unreserve_timer_fn(unsigned long data)
1277{
1278 struct dwc2_qh *qh = (struct dwc2_qh *)data;
1279 struct dwc2_hsotg *hsotg = qh->hsotg;
1280 unsigned long flags;
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291 while (!spin_trylock_irqsave(&hsotg->lock, flags)) {
1292 if (timer_pending(&qh->unreserve_timer))
1293 return;
1294 }
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306 if (qh->unreserve_pending)
1307 dwc2_do_unreserve(hsotg, qh);
1308
1309 spin_unlock_irqrestore(&hsotg->lock, flags);
1310}
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322static int dwc2_check_max_xfer_size(struct dwc2_hsotg *hsotg,
1323 struct dwc2_qh *qh)
1324{
1325 u32 max_xfer_size;
1326 u32 max_channel_xfer_size;
1327 int status = 0;
1328
1329 max_xfer_size = dwc2_max_packet(qh->maxp) * dwc2_hb_mult(qh->maxp);
1330 max_channel_xfer_size = hsotg->core_params->max_transfer_size;
1331
1332 if (max_xfer_size > max_channel_xfer_size) {
1333 dev_err(hsotg->dev,
1334 "%s: Periodic xfer length %d > max xfer length for channel %d\n",
1335 __func__, max_xfer_size, max_channel_xfer_size);
1336 status = -ENOSPC;
1337 }
1338
1339 return status;
1340}
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352static int dwc2_schedule_periodic(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1353{
1354 int status;
1355
1356 status = dwc2_check_max_xfer_size(hsotg, qh);
1357 if (status) {
1358 dev_dbg(hsotg->dev,
1359 "%s: Channel max transfer size too small for periodic transfer\n",
1360 __func__);
1361 return status;
1362 }
1363
1364
1365 if (del_timer(&qh->unreserve_timer))
1366 WARN_ON(!qh->unreserve_pending);
1367
1368
1369
1370
1371
1372
1373
1374
1375 if (!qh->unreserve_pending) {
1376 status = dwc2_do_reserve(hsotg, qh);
1377 if (status)
1378 return status;
1379 } else {
1380
1381
1382
1383
1384
1385
1386 if (dwc2_frame_num_le(qh->next_active_frame,
1387 hsotg->frame_number))
1388 dwc2_pick_first_frame(hsotg, qh);
1389 }
1390
1391 qh->unreserve_pending = 0;
1392
1393 if (hsotg->core_params->dma_desc_enable > 0)
1394
1395 list_add_tail(&qh->qh_list_entry, &hsotg->periodic_sched_ready);
1396 else
1397
1398 list_add_tail(&qh->qh_list_entry,
1399 &hsotg->periodic_sched_inactive);
1400
1401 return 0;
1402}
1403
1404
1405
1406
1407
1408
1409
1410
1411static void dwc2_deschedule_periodic(struct dwc2_hsotg *hsotg,
1412 struct dwc2_qh *qh)
1413{
1414 bool did_modify;
1415
1416 assert_spin_locked(&hsotg->lock);
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433 did_modify = mod_timer(&qh->unreserve_timer,
1434 jiffies + DWC2_UNRESERVE_DELAY + 1);
1435 WARN_ON(did_modify);
1436 qh->unreserve_pending = 1;
1437
1438 list_del_init(&qh->qh_list_entry);
1439}
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450static void dwc2_qh_init(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
1451 struct dwc2_hcd_urb *urb, gfp_t mem_flags)
1452{
1453 int dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
1454 u8 ep_type = dwc2_hcd_get_pipe_type(&urb->pipe_info);
1455 bool ep_is_in = !!dwc2_hcd_is_pipe_in(&urb->pipe_info);
1456 bool ep_is_isoc = (ep_type == USB_ENDPOINT_XFER_ISOC);
1457 bool ep_is_int = (ep_type == USB_ENDPOINT_XFER_INT);
1458 u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
1459 u32 prtspd = (hprt & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
1460 bool do_split = (prtspd == HPRT0_SPD_HIGH_SPEED &&
1461 dev_speed != USB_SPEED_HIGH);
1462 int maxp = dwc2_hcd_get_mps(&urb->pipe_info);
1463 int bytecount = dwc2_hb_mult(maxp) * dwc2_max_packet(maxp);
1464 char *speed, *type;
1465
1466
1467 qh->hsotg = hsotg;
1468 setup_timer(&qh->unreserve_timer, dwc2_unreserve_timer_fn,
1469 (unsigned long)qh);
1470 qh->ep_type = ep_type;
1471 qh->ep_is_in = ep_is_in;
1472
1473 qh->data_toggle = DWC2_HC_PID_DATA0;
1474 qh->maxp = maxp;
1475 INIT_LIST_HEAD(&qh->qtd_list);
1476 INIT_LIST_HEAD(&qh->qh_list_entry);
1477
1478 qh->do_split = do_split;
1479 qh->dev_speed = dev_speed;
1480
1481 if (ep_is_int || ep_is_isoc) {
1482
1483 int host_speed = do_split ? USB_SPEED_HIGH : dev_speed;
1484 struct dwc2_tt *dwc_tt = dwc2_host_get_tt_info(hsotg, urb->priv,
1485 mem_flags,
1486 &qh->ttport);
1487 int device_ns;
1488
1489 qh->dwc_tt = dwc_tt;
1490
1491 qh->host_us = NS_TO_US(usb_calc_bus_time(host_speed, ep_is_in,
1492 ep_is_isoc, bytecount));
1493 device_ns = usb_calc_bus_time(dev_speed, ep_is_in,
1494 ep_is_isoc, bytecount);
1495
1496 if (do_split && dwc_tt)
1497 device_ns += dwc_tt->usb_tt->think_time;
1498 qh->device_us = NS_TO_US(device_ns);
1499
1500
1501 qh->device_interval = urb->interval;
1502 qh->host_interval = urb->interval * (do_split ? 8 : 1);
1503
1504
1505
1506
1507
1508
1509 qh->schedule_low_speed = prtspd != HPRT0_SPD_HIGH_SPEED ||
1510 dwc_tt;
1511
1512 if (do_split) {
1513
1514 qh->num_hs_transfers = -1;
1515 } else if (dev_speed == USB_SPEED_HIGH) {
1516 qh->num_hs_transfers = 1;
1517 } else {
1518 qh->num_hs_transfers = 0;
1519 }
1520
1521
1522 }
1523
1524 switch (dev_speed) {
1525 case USB_SPEED_LOW:
1526 speed = "low";
1527 break;
1528 case USB_SPEED_FULL:
1529 speed = "full";
1530 break;
1531 case USB_SPEED_HIGH:
1532 speed = "high";
1533 break;
1534 default:
1535 speed = "?";
1536 break;
1537 }
1538
1539 switch (qh->ep_type) {
1540 case USB_ENDPOINT_XFER_ISOC:
1541 type = "isochronous";
1542 break;
1543 case USB_ENDPOINT_XFER_INT:
1544 type = "interrupt";
1545 break;
1546 case USB_ENDPOINT_XFER_CONTROL:
1547 type = "control";
1548 break;
1549 case USB_ENDPOINT_XFER_BULK:
1550 type = "bulk";
1551 break;
1552 default:
1553 type = "?";
1554 break;
1555 }
1556
1557 dwc2_sch_dbg(hsotg, "QH=%p Init %s, %s speed, %d bytes:\n", qh, type,
1558 speed, bytecount);
1559 dwc2_sch_dbg(hsotg, "QH=%p ...addr=%d, ep=%d, %s\n", qh,
1560 dwc2_hcd_get_dev_addr(&urb->pipe_info),
1561 dwc2_hcd_get_ep_num(&urb->pipe_info),
1562 ep_is_in ? "IN" : "OUT");
1563 if (ep_is_int || ep_is_isoc) {
1564 dwc2_sch_dbg(hsotg,
1565 "QH=%p ...duration: host=%d us, device=%d us\n",
1566 qh, qh->host_us, qh->device_us);
1567 dwc2_sch_dbg(hsotg, "QH=%p ...interval: host=%d, device=%d\n",
1568 qh, qh->host_interval, qh->device_interval);
1569 if (qh->schedule_low_speed)
1570 dwc2_sch_dbg(hsotg, "QH=%p ...low speed schedule=%p\n",
1571 qh, dwc2_get_ls_map(hsotg, qh));
1572 }
1573}
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585struct dwc2_qh *dwc2_hcd_qh_create(struct dwc2_hsotg *hsotg,
1586 struct dwc2_hcd_urb *urb,
1587 gfp_t mem_flags)
1588{
1589 struct dwc2_qh *qh;
1590
1591 if (!urb->priv)
1592 return NULL;
1593
1594
1595 qh = kzalloc(sizeof(*qh), mem_flags);
1596 if (!qh)
1597 return NULL;
1598
1599 dwc2_qh_init(hsotg, qh, urb, mem_flags);
1600
1601 if (hsotg->core_params->dma_desc_enable > 0 &&
1602 dwc2_hcd_qh_init_ddma(hsotg, qh, mem_flags) < 0) {
1603 dwc2_hcd_qh_free(hsotg, qh);
1604 return NULL;
1605 }
1606
1607 return qh;
1608}
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621void dwc2_hcd_qh_free(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1622{
1623
1624 if (del_timer_sync(&qh->unreserve_timer)) {
1625 unsigned long flags;
1626
1627 spin_lock_irqsave(&hsotg->lock, flags);
1628 dwc2_do_unreserve(hsotg, qh);
1629 spin_unlock_irqrestore(&hsotg->lock, flags);
1630 }
1631 dwc2_host_put_tt_info(hsotg, qh->dwc_tt);
1632
1633 if (qh->desc_list)
1634 dwc2_hcd_qh_free_ddma(hsotg, qh);
1635 kfree(qh);
1636}
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648int dwc2_hcd_qh_add(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1649{
1650 int status;
1651 u32 intr_mask;
1652
1653 if (dbg_qh(qh))
1654 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1655
1656 if (!list_empty(&qh->qh_list_entry))
1657
1658 return 0;
1659
1660
1661 if (dwc2_qh_is_non_per(qh)) {
1662
1663 qh->start_active_frame = hsotg->frame_number;
1664 qh->next_active_frame = qh->start_active_frame;
1665
1666
1667 list_add_tail(&qh->qh_list_entry,
1668 &hsotg->non_periodic_sched_inactive);
1669 return 0;
1670 }
1671
1672 status = dwc2_schedule_periodic(hsotg, qh);
1673 if (status)
1674 return status;
1675 if (!hsotg->periodic_qh_count) {
1676 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
1677 intr_mask |= GINTSTS_SOF;
1678 dwc2_writel(intr_mask, hsotg->regs + GINTMSK);
1679 }
1680 hsotg->periodic_qh_count++;
1681
1682 return 0;
1683}
1684
1685
1686
1687
1688
1689
1690
1691
1692void dwc2_hcd_qh_unlink(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
1693{
1694 u32 intr_mask;
1695
1696 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1697
1698 if (list_empty(&qh->qh_list_entry))
1699
1700 return;
1701
1702 if (dwc2_qh_is_non_per(qh)) {
1703 if (hsotg->non_periodic_qh_ptr == &qh->qh_list_entry)
1704 hsotg->non_periodic_qh_ptr =
1705 hsotg->non_periodic_qh_ptr->next;
1706 list_del_init(&qh->qh_list_entry);
1707 return;
1708 }
1709
1710 dwc2_deschedule_periodic(hsotg, qh);
1711 hsotg->periodic_qh_count--;
1712 if (!hsotg->periodic_qh_count) {
1713 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
1714 intr_mask &= ~GINTSTS_SOF;
1715 dwc2_writel(intr_mask, hsotg->regs + GINTMSK);
1716 }
1717}
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738static int dwc2_next_for_periodic_split(struct dwc2_hsotg *hsotg,
1739 struct dwc2_qh *qh, u16 frame_number)
1740{
1741 u16 old_frame = qh->next_active_frame;
1742 u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
1743 int missed = 0;
1744 u16 incr;
1745
1746
1747
1748
1749
1750
1751
1752 if (old_frame == qh->start_active_frame &&
1753 !(qh->ep_type == USB_ENDPOINT_XFER_ISOC && !qh->ep_is_in))
1754 incr = 2;
1755 else
1756 incr = 1;
1757
1758 qh->next_active_frame = dwc2_frame_num_inc(old_frame, incr);
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768 if (dwc2_frame_num_gt(prev_frame_number, qh->next_active_frame)) {
1769
1770
1771
1772
1773 missed = dwc2_frame_num_dec(prev_frame_number,
1774 qh->next_active_frame);
1775 qh->next_active_frame = frame_number;
1776 }
1777
1778 return missed;
1779}
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801static int dwc2_next_periodic_start(struct dwc2_hsotg *hsotg,
1802 struct dwc2_qh *qh, u16 frame_number)
1803{
1804 int missed = 0;
1805 u16 interval = qh->host_interval;
1806 u16 prev_frame_number = dwc2_frame_num_dec(frame_number, 1);
1807
1808 qh->start_active_frame = dwc2_frame_num_inc(qh->start_active_frame,
1809 interval);
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819 if (interval >= 0x1000)
1820 goto exit;
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 if (qh->start_active_frame == qh->next_active_frame ||
1849 dwc2_frame_num_gt(prev_frame_number, qh->start_active_frame)) {
1850 u16 ideal_start = qh->start_active_frame;
1851 int periods_in_map;
1852
1853
1854
1855
1856
1857 if (qh->do_split || qh->dev_speed == USB_SPEED_HIGH)
1858 periods_in_map = DWC2_HS_SCHEDULE_UFRAMES;
1859 else
1860 periods_in_map = DWC2_LS_SCHEDULE_FRAMES;
1861 interval = gcd(interval, periods_in_map);
1862
1863 do {
1864 qh->start_active_frame = dwc2_frame_num_inc(
1865 qh->start_active_frame, interval);
1866 } while (dwc2_frame_num_gt(prev_frame_number,
1867 qh->start_active_frame));
1868
1869 missed = dwc2_frame_num_dec(qh->start_active_frame,
1870 ideal_start);
1871 }
1872
1873exit:
1874 qh->next_active_frame = qh->start_active_frame;
1875
1876 return missed;
1877}
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892void dwc2_hcd_qh_deactivate(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh,
1893 int sched_next_periodic_split)
1894{
1895 u16 old_frame = qh->next_active_frame;
1896 u16 frame_number;
1897 int missed;
1898
1899 if (dbg_qh(qh))
1900 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1901
1902 if (dwc2_qh_is_non_per(qh)) {
1903 dwc2_hcd_qh_unlink(hsotg, qh);
1904 if (!list_empty(&qh->qtd_list))
1905
1906 dwc2_hcd_qh_add(hsotg, qh);
1907 return;
1908 }
1909
1910
1911
1912
1913
1914
1915
1916 frame_number = dwc2_hcd_get_frame_number(hsotg);
1917
1918 if (sched_next_periodic_split)
1919 missed = dwc2_next_for_periodic_split(hsotg, qh, frame_number);
1920 else
1921 missed = dwc2_next_periodic_start(hsotg, qh, frame_number);
1922
1923 dwc2_sch_vdbg(hsotg,
1924 "QH=%p next(%d) fn=%04x, sch=%04x=>%04x (%+d) miss=%d %s\n",
1925 qh, sched_next_periodic_split, frame_number, old_frame,
1926 qh->next_active_frame,
1927 dwc2_frame_num_dec(qh->next_active_frame, old_frame),
1928 missed, missed ? "MISS" : "");
1929
1930 if (list_empty(&qh->qtd_list)) {
1931 dwc2_hcd_qh_unlink(hsotg, qh);
1932 return;
1933 }
1934
1935
1936
1937
1938
1939
1940
1941
1942 if (dwc2_frame_num_le(qh->next_active_frame, hsotg->frame_number))
1943 list_move_tail(&qh->qh_list_entry,
1944 &hsotg->periodic_sched_ready);
1945 else
1946 list_move_tail(&qh->qh_list_entry,
1947 &hsotg->periodic_sched_inactive);
1948}
1949
1950
1951
1952
1953
1954
1955
1956void dwc2_hcd_qtd_init(struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
1957{
1958 qtd->urb = urb;
1959 if (dwc2_hcd_get_pipe_type(&urb->pipe_info) ==
1960 USB_ENDPOINT_XFER_CONTROL) {
1961
1962
1963
1964
1965
1966 qtd->data_toggle = DWC2_HC_PID_DATA1;
1967 qtd->control_phase = DWC2_CONTROL_SETUP;
1968 }
1969
1970
1971 qtd->complete_split = 0;
1972 qtd->isoc_split_pos = DWC2_HCSPLT_XACTPOS_ALL;
1973 qtd->isoc_split_offset = 0;
1974 qtd->in_process = 0;
1975
1976
1977 urb->qtd = qtd;
1978}
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993int dwc2_hcd_qtd_add(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
1994 struct dwc2_qh *qh)
1995{
1996 int retval;
1997
1998 if (unlikely(!qh)) {
1999 dev_err(hsotg->dev, "%s: Invalid QH\n", __func__);
2000 retval = -EINVAL;
2001 goto fail;
2002 }
2003
2004 retval = dwc2_hcd_qh_add(hsotg, qh);
2005 if (retval)
2006 goto fail;
2007
2008 qtd->qh = qh;
2009 list_add_tail(&qtd->qtd_list_entry, &qh->qtd_list);
2010
2011 return 0;
2012fail:
2013 return retval;
2014}
2015