2 * PROJECT: ReactOS USB Port Driver
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: USBPort USB 2.0 functions
5 * COPYRIGHT: Copyright 2017 Vadim Galyant <vgal@rambler.ru>
15 USB2_AllocateCheck(IN OUT PULONG OutTimeUsed
,
17 IN ULONG LimitAllocation
)
20 BOOLEAN Result
= TRUE
;
22 BusTime
= *OutTimeUsed
+ CalcBusTime
;
23 *OutTimeUsed
+= CalcBusTime
;
25 if (BusTime
> LimitAllocation
)
27 DPRINT("USB2_AllocateCheck: BusTime > LimitAllocation\n");
36 USB2_AddDataBitStuff(IN USHORT DataTime
)
38 return (DataTime
+ (DataTime
/ 16));
43 USB2_IncMicroFrame(OUT PUCHAR frame
,
48 if (*uframe
> (USB2_MICROFRAMES
- 1))
51 *frame
= (*frame
+ 1) & (USB2_FRAMES
- 1);
57 USB2_GetPrevMicroFrame(OUT PUCHAR frame
,
60 *uframe
= USB2_MICROFRAMES
- 1;
65 *frame
= USB2_FRAMES
- 1;
70 USB2_CheckTtEndpointInsert(IN PUSB2_TT_ENDPOINT nextTtEndpoint
,
71 IN PUSB2_TT_ENDPOINT TtEndpoint
)
75 DPRINT("USB2_CheckTtEndpointInsert: nextTtEndpoint - %p, TtEndpoint - %p\n",
81 if (TtEndpoint
->CalcBusTime
>= (USB2_FS_MAX_PERIODIC_ALLOCATION
/ 2))
83 DPRINT1("USB2_CheckTtEndpointInsert: Result - FALSE\n");
89 DPRINT("USB2_CheckTtEndpointInsert: Result - TRUE\n");
93 TransferType
= TtEndpoint
->TtEndpointParams
.TransferType
;
95 if (nextTtEndpoint
->ActualPeriod
< TtEndpoint
->ActualPeriod
&&
96 TransferType
== USBPORT_TRANSFER_TYPE_INTERRUPT
)
98 DPRINT("USB2_CheckTtEndpointInsert: Result - TRUE\n");
102 if ((nextTtEndpoint
->ActualPeriod
<= TtEndpoint
->ActualPeriod
&&
103 TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
) ||
104 nextTtEndpoint
== TtEndpoint
)
106 DPRINT("USB2_CheckTtEndpointInsert: Result - TRUE\n");
110 DPRINT("USB2_CheckTtEndpointInsert: Result - FALSE\n");
116 USB2_GetOverhead(IN PUSB2_TT_ENDPOINT TtEndpoint
)
124 TransferType
= TtEndpoint
->TtEndpointParams
.TransferType
;
125 Direction
= TtEndpoint
->TtEndpointParams
.Direction
;
126 DeviceSpeed
= TtEndpoint
->TtEndpointParams
.Direction
;
128 HostDelay
= TtEndpoint
->Tt
->HcExtension
->HcDelayTime
;
130 if (DeviceSpeed
== UsbHighSpeed
)
132 if (Direction
== USBPORT_TRANSFER_DIRECTION_OUT
)
134 if (TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
)
135 Overhead
= HostDelay
+ USB2_HS_ISOCHRONOUS_OUT_OVERHEAD
;
137 Overhead
= HostDelay
+ USB2_HS_INTERRUPT_OUT_OVERHEAD
;
141 if (TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
)
142 Overhead
= HostDelay
+ USB2_HS_ISOCHRONOUS_IN_OVERHEAD
;
144 Overhead
= HostDelay
+ USB2_HS_ISOCHRONOUS_OUT_OVERHEAD
;
147 else if (DeviceSpeed
== UsbFullSpeed
)
149 if (TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
)
150 Overhead
= HostDelay
+ USB2_FS_ISOCHRONOUS_OVERHEAD
;
152 Overhead
= HostDelay
+ USB2_FS_INTERRUPT_OVERHEAD
;
156 Overhead
= HostDelay
+ USB2_LS_INTERRUPT_OVERHEAD
;
164 USB2_GetHsOverhead(IN PUSB2_TT_ENDPOINT TtEndpoint
,
165 IN PULONG OverheadSS
,
166 IN PULONG OverheadCS
)
172 TransferType
= TtEndpoint
->TtEndpointParams
.TransferType
;
173 Direction
= TtEndpoint
->TtEndpointParams
.Direction
;
175 HostDelay
= TtEndpoint
->Tt
->HcExtension
->HcDelayTime
;
177 if (Direction
== USBPORT_TRANSFER_DIRECTION_OUT
)
179 if (TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
)
181 *OverheadSS
= HostDelay
+ USB2_HS_SS_ISOCHRONOUS_OUT_OVERHEAD
;
186 *OverheadSS
= HostDelay
+ USB2_HS_SS_INTERRUPT_OUT_OVERHEAD
;
187 *OverheadCS
= HostDelay
+ USB2_HS_CS_INTERRUPT_OUT_OVERHEAD
;
192 if (TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
)
194 *OverheadSS
= HostDelay
+ USB2_HS_SS_ISOCHRONOUS_IN_OVERHEAD
;
195 *OverheadCS
= HostDelay
+ USB2_HS_CS_ISOCHRONOUS_IN_OVERHEAD
;
199 *OverheadSS
= HostDelay
+ USB2_HS_SS_INTERRUPT_IN_OVERHEAD
;
200 *OverheadCS
= HostDelay
+ USB2_HS_CS_INTERRUPT_IN_OVERHEAD
;
203 DPRINT("USB2_GetHsOverhead: *OverheadSS - %X, *OverheadCS - %X\n",
211 USB2_GetLastIsoTime(IN PUSB2_TT_ENDPOINT TtEndpoint
,
214 PUSB2_TT_ENDPOINT nextTtEndpoint
;
217 DPRINT("USB2_GetLastIsoTime: TtEndpoint - %p, Frame - %X\n",
221 nextTtEndpoint
= TtEndpoint
->Tt
->FrameBudget
[Frame
].IsoEndpoint
->NextTtEndpoint
;
223 if (nextTtEndpoint
||
224 (nextTtEndpoint
= TtEndpoint
->Tt
->FrameBudget
[Frame
].AltEndpoint
) != NULL
)
226 Result
= nextTtEndpoint
->StartTime
+ nextTtEndpoint
->CalcBusTime
;
230 Result
= USB2_FS_SOF_TIME
;
238 USB2_GetStartTime(IN PUSB2_TT_ENDPOINT nextTtEndpoint
,
239 IN PUSB2_TT_ENDPOINT TtEndpoint
,
240 IN PUSB2_TT_ENDPOINT prevTtEndpoint
,
243 PUSB2_TT_ENDPOINT ttEndpoint
;
246 DPRINT("USB2_GetStartTime: nextTtEndpoint - %p, TtEndpoint - %p, prevTtEndpoint - %p, Frame - %X\n",
252 TransferType
= TtEndpoint
->TtEndpointParams
.TransferType
;
254 if (TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
)
257 return nextTtEndpoint
->StartTime
+ nextTtEndpoint
->CalcBusTime
;
259 ttEndpoint
= TtEndpoint
->Tt
->FrameBudget
[Frame
].AltEndpoint
;
262 return ttEndpoint
->StartTime
+ ttEndpoint
->CalcBusTime
;
264 return USB2_FS_SOF_TIME
;
268 ttEndpoint
= prevTtEndpoint
;
270 if (ttEndpoint
== TtEndpoint
->Tt
->FrameBudget
[Frame
].IntEndpoint
)
271 return USB2_GetLastIsoTime(TtEndpoint
, Frame
);
273 return ttEndpoint
->StartTime
+ ttEndpoint
->CalcBusTime
;
279 USB2_InitTtEndpoint(IN PUSB2_TT_ENDPOINT TtEndpoint
,
280 IN UCHAR TransferType
,
282 IN UCHAR DeviceSpeed
,
284 IN USHORT MaxPacketSize
,
287 TtEndpoint
->TtEndpointParams
.TransferType
= TransferType
;
288 TtEndpoint
->TtEndpointParams
.Direction
= Direction
;
289 TtEndpoint
->TtEndpointParams
.DeviceSpeed
= DeviceSpeed
;
291 TtEndpoint
->Period
= Period
;
292 TtEndpoint
->MaxPacketSize
= MaxPacketSize
;
295 TtEndpoint
->CalcBusTime
= 0;
296 TtEndpoint
->StartTime
= 0;
297 TtEndpoint
->ActualPeriod
= 0;
298 TtEndpoint
->StartFrame
= 0;
299 TtEndpoint
->StartMicroframe
= 0;
301 TtEndpoint
->Nums
.AsULONG
= 0;
302 TtEndpoint
->NextTtEndpoint
= NULL
;
303 TtEndpoint
->Reserved2
= 0;
304 TtEndpoint
->PreviosPeriod
= 0;
305 TtEndpoint
->IsPromoted
= FALSE
;
310 USB2_AllocateHS(IN PUSB2_TT_ENDPOINT TtEndpoint
,
313 PUSB2_HC_EXTENSION HcExtension
;
318 ULONG RemainDataTime
;
327 DPRINT("USB2_AllocateHS: TtEndpoint - %p, Frame - %X, TtEndpoint->StartFrame - %X\n",
330 TtEndpoint
->StartFrame
);
333 HcExtension
= Tt
->HcExtension
;
335 TransferType
= TtEndpoint
->TtEndpointParams
.TransferType
;
336 Direction
= TtEndpoint
->TtEndpointParams
.Direction
;
340 TtEndpoint
->StartMicroframe
=
341 TtEndpoint
->StartTime
/ USB2_FS_RAW_BYTES_IN_MICROFRAME
- 1;
343 DPRINT("USB2_AllocateHS: TtEndpoint->StartMicroframe - %X\n",
344 TtEndpoint
->StartMicroframe
);
347 USB2_GetHsOverhead(TtEndpoint
, &OverheadSS
, &OverheadCS
);
349 if (TransferType
== USBPORT_TRANSFER_TYPE_INTERRUPT
)
353 TtEndpoint
->Nums
.NumStarts
= 1;
355 if ((CHAR
)TtEndpoint
->StartMicroframe
< 5)
357 TtEndpoint
->Nums
.NumCompletes
= 3;
361 TtEndpoint
->Nums
.NumCompletes
= 2;
367 if (Direction
== USBPORT_TRANSFER_DIRECTION_OUT
)
369 DPRINT("USB2_AllocateHS: ISO UNIMPLEMENTED\n");
374 DPRINT("USB2_AllocateHS: ISO UNIMPLEMENTED\n");
379 frame
= TtEndpoint
->StartFrame
+ Frame
;
380 uframe
= TtEndpoint
->StartMicroframe
;
382 if (TtEndpoint
->StartMicroframe
== 0xFF)
383 USB2_GetPrevMicroFrame(&frame
, &uframe
);
385 for (ix
= 0; ix
< TtEndpoint
->Nums
.NumStarts
; ix
++)
387 if (!USB2_AllocateCheck(&HcExtension
->TimeUsed
[frame
][uframe
],
389 USB2_MAX_MICROFRAME_ALLOCATION
))
394 if (Tt
->NumStartSplits
[frame
][uframe
] > (USB2_MAX_FS_LS_TRANSACTIONS_IN_UFRAME
- 1))
396 DPRINT1("USB2_AllocateHS: Num Start Splits - %X\n",
397 Tt
->NumStartSplits
[frame
][uframe
] + 1);
403 ++Tt
->NumStartSplits
[frame
][uframe
];
404 USB2_IncMicroFrame(&frame
, &uframe
);
407 frame
= TtEndpoint
->StartFrame
+ Frame
;
408 uframe
= TtEndpoint
->StartMicroframe
+ TtEndpoint
->Nums
.NumStarts
+ 1;
410 for (ix
= 0; ix
< TtEndpoint
->Nums
.NumCompletes
; ix
++)
412 if (!USB2_AllocateCheck(&HcExtension
->TimeUsed
[frame
][uframe
],
414 USB2_MAX_MICROFRAME_ALLOCATION
))
419 USB2_IncMicroFrame(&frame
, &uframe
);
422 if (Direction
== USBPORT_TRANSFER_DIRECTION_OUT
)
424 DPRINT("USB2_AllocateHS: DIRECTION OUT UNIMPLEMENTED\n");
429 frame
= TtEndpoint
->StartFrame
+ Frame
;
430 uframe
= TtEndpoint
->StartMicroframe
+ TtEndpoint
->Nums
.NumStarts
+ 1;
432 for (ix
= 0; ix
< TtEndpoint
->Nums
.NumCompletes
; ix
++)
434 if (Tt
->TimeCS
[frame
][uframe
] < USB2_FS_RAW_BYTES_IN_MICROFRAME
)
436 if (Tt
->TimeCS
[frame
][uframe
] < USB2_FS_RAW_BYTES_IN_MICROFRAME
)
438 RemainDataTime
= USB2_FS_RAW_BYTES_IN_MICROFRAME
-
439 Tt
->TimeCS
[frame
][uframe
];
446 PktSize
= TtEndpoint
->MaxPacketSize
;
448 if (RemainDataTime
>= USB2_AddDataBitStuff(PktSize
))
450 DataTime
= USB2_AddDataBitStuff(PktSize
);
454 DataTime
= RemainDataTime
;
457 if (!USB2_AllocateCheck(&HcExtension
->TimeUsed
[frame
][uframe
],
459 USB2_MAX_MICROFRAME_ALLOCATION
))
465 PktSize
= TtEndpoint
->MaxPacketSize
;
467 if (USB2_AddDataBitStuff(PktSize
) < USB2_FS_RAW_BYTES_IN_MICROFRAME
)
469 Tt
->TimeCS
[frame
][uframe
] += USB2_AddDataBitStuff(PktSize
);
473 Tt
->TimeCS
[frame
][uframe
] += USB2_FS_RAW_BYTES_IN_MICROFRAME
;
476 USB2_IncMicroFrame(&frame
, &uframe
);
480 DPRINT("USB2_AllocateHS: Result - %X\n", Result
);
486 USB2_DeallocateHS(IN PUSB2_TT_ENDPOINT TtEndpoint
,
489 DPRINT("USB2_DeallocateHS: UNIMPLEMENTED FIXME\n");
495 USB2_DeallocateEndpointBudget(IN PUSB2_TT_ENDPOINT TtEndpoint
,
496 IN PUSB2_REBALANCE Rebalance
,
497 IN PULONG RebalanceListEntries
,
500 DPRINT("USB2_DeallocateEndpointBudget: UNIMPLEMENTED FIXME\n");
507 USB2_AllocateTimeForEndpoint(IN PUSB2_TT_ENDPOINT TtEndpoint
,
508 IN PUSB2_REBALANCE Rebalance
,
509 IN PULONG RebalanceListEntries
)
512 PUSB2_HC_EXTENSION HcExtension
;
523 PUSB2_TT_ENDPOINT prevTtEndpoint
;
524 PUSB2_TT_ENDPOINT nextTtEndpoint
;
525 PUSB2_TT_ENDPOINT IntEndpoint
;
528 BOOLEAN Result
= TRUE
;
530 DPRINT("USB2_AllocateTimeForEndpoint: TtEndpoint - %p\n", TtEndpoint
);
533 HcExtension
= Tt
->HcExtension
;
535 TtEndpoint
->Nums
.NumStarts
= 0;
536 TtEndpoint
->Nums
.NumCompletes
= 0;
538 TtEndpoint
->StartFrame
= 0;
539 TtEndpoint
->StartMicroframe
= 0;
541 if (TtEndpoint
->CalcBusTime
)
543 DPRINT("USB2_AllocateTimeForEndpoint: TtEndpoint already allocated!\n");
547 Speed
= TtEndpoint
->TtEndpointParams
.DeviceSpeed
;
549 if (Speed
== UsbHighSpeed
)
551 if (TtEndpoint
->Period
> USB2_MAX_MICROFRAMES
)
552 TtEndpoint
->ActualPeriod
= USB2_MAX_MICROFRAMES
;
554 TtEndpoint
->ActualPeriod
= TtEndpoint
->Period
;
556 MinTimeUsed
= HcExtension
->TimeUsed
[0][0];
558 for (ix
= 1; ix
< TtEndpoint
->ActualPeriod
; ix
++)
560 frame
= ix
/ USB2_MICROFRAMES
;
561 uframe
= ix
% (USB2_MICROFRAMES
- 1);
563 TimeUsed
= HcExtension
->TimeUsed
[frame
][uframe
];
565 if (TimeUsed
< MinTimeUsed
)
567 MinTimeUsed
= TimeUsed
;
568 TtEndpoint
->StartFrame
= frame
;
569 TtEndpoint
->StartMicroframe
= uframe
;
573 TtEndpoint
->CalcBusTime
= USB2_GetOverhead(TtEndpoint
) +
574 USB2_AddDataBitStuff(TtEndpoint
->MaxPacketSize
);
576 DPRINT("USB2_AllocateTimeForEndpoint: StartFrame - %X, StartMicroframe - %X, CalcBusTime - %X\n",
577 TtEndpoint
->StartFrame
,
578 TtEndpoint
->StartMicroframe
,
579 TtEndpoint
->CalcBusTime
);
581 Microframe
= TtEndpoint
->StartFrame
* USB2_MICROFRAMES
+
582 TtEndpoint
->StartMicroframe
;
584 if (Microframe
>= USB2_MAX_MICROFRAMES
)
586 DPRINT("USB2_AllocateTimeForEndpoint: Microframe >= 256. Result - TRUE\n");
590 for (ix
= Microframe
;
591 ix
< USB2_MAX_MICROFRAMES
;
592 ix
+= TtEndpoint
->ActualPeriod
)
594 frame
= ix
/ USB2_MICROFRAMES
;
595 uframe
= ix
% (USB2_MICROFRAMES
- 1);
597 DPRINT("USB2_AllocateTimeForEndpoint: frame - %X, uframe - %X, TimeUsed[f][uf] - %X\n",
600 HcExtension
->TimeUsed
[frame
][uframe
]);
602 if (!USB2_AllocateCheck(&HcExtension
->TimeUsed
[frame
][uframe
],
603 TtEndpoint
->CalcBusTime
,
604 USB2_MAX_MICROFRAME_ALLOCATION
))
606 DPRINT("USB2_AllocateTimeForEndpoint: Result = FALSE\n");
613 for (ix
= Microframe
;
614 ix
< USB2_MAX_MICROFRAMES
;
615 ix
+= TtEndpoint
->ActualPeriod
)
617 frame
= ix
/ USB2_MICROFRAMES
;
618 uframe
= ix
% (USB2_MICROFRAMES
- 1);
620 HcExtension
->TimeUsed
[frame
][uframe
] -= TtEndpoint
->CalcBusTime
;
624 DPRINT("USB2_AllocateTimeForEndpoint: Result - TRUE\n");
628 /* Speed != UsbHighSpeed (FS/LS) */
630 if (TtEndpoint
->Period
> USB2_FRAMES
)
631 TtEndpoint
->ActualPeriod
= USB2_FRAMES
;
633 TtEndpoint
->ActualPeriod
= TtEndpoint
->Period
;
635 MinTimeUsed
= Tt
->FrameBudget
[0].TimeUsed
;
637 for (ix
= 1; ix
< TtEndpoint
->ActualPeriod
; ix
++)
639 if ((Tt
->FrameBudget
[ix
].TimeUsed
) < MinTimeUsed
)
641 MinTimeUsed
= Tt
->FrameBudget
[ix
].TimeUsed
;
642 TtEndpoint
->StartFrame
= ix
;
646 TransferType
= TtEndpoint
->TtEndpointParams
.TransferType
;
648 if (TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
)
650 if (Speed
== UsbFullSpeed
)
652 Overhead
= USB2_FS_ISOCHRONOUS_OVERHEAD
+ Tt
->DelayTime
;
656 DPRINT("USB2_AllocateTimeForEndpoint: ISO can not be on a LS bus!\n");
662 if (Speed
== UsbFullSpeed
)
663 Overhead
= USB2_FS_INTERRUPT_OVERHEAD
+ Tt
->DelayTime
;
665 Overhead
= USB2_LS_INTERRUPT_OVERHEAD
+ Tt
->DelayTime
;
668 if (Speed
== UsbLowSpeed
)
670 TtEndpoint
->CalcBusTime
= TtEndpoint
->MaxPacketSize
* 8 + Overhead
;
674 TtEndpoint
->CalcBusTime
= TtEndpoint
->MaxPacketSize
+ Overhead
;
677 LatestStart
= USB2_HUB_DELAY
+ USB2_FS_SOF_TIME
;
680 (TtEndpoint
->StartFrame
+ ix
) < USB2_FRAMES
;
681 ix
+= TtEndpoint
->ActualPeriod
)
683 frame
= TtEndpoint
->StartFrame
+ ix
;
685 if (Tt
->FrameBudget
[frame
].AltEndpoint
&&
686 TtEndpoint
->CalcBusTime
>= (USB2_FS_MAX_PERIODIC_ALLOCATION
/ 2))
688 DPRINT("USB2_AllocateTimeForEndpoint: return FALSE\n");
692 if (TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
)
693 prevTtEndpoint
= Tt
->FrameBudget
[frame
].IsoEndpoint
;
695 prevTtEndpoint
= Tt
->FrameBudget
[frame
].IntEndpoint
;
697 for (nextTtEndpoint
= prevTtEndpoint
->NextTtEndpoint
;
699 nextTtEndpoint
= nextTtEndpoint
->NextTtEndpoint
)
701 if (USB2_CheckTtEndpointInsert(nextTtEndpoint
, TtEndpoint
))
706 prevTtEndpoint
= nextTtEndpoint
;
709 StartTime
= USB2_GetStartTime(nextTtEndpoint
,
714 if (StartTime
> LatestStart
)
715 LatestStart
= StartTime
;
718 TtEndpoint
->StartTime
= LatestStart
;
720 if ((LatestStart
+ TtEndpoint
->CalcBusTime
) > USB2_FS_MAX_PERIODIC_ALLOCATION
)
722 TtEndpoint
->CalcBusTime
= 0;
723 DPRINT("USB2_AllocateTimeForEndpoint: return FALSE\n");
727 for (ix
= 0, frame
= -TtEndpoint
->StartFrame
;
731 DPRINT("USB2_AllocateTimeForEndpoint: ix - %X, frame - %X, StartFrame - %X\n",
734 TtEndpoint
->StartFrame
);
736 if (TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
)
738 DPRINT1("USB2_AllocateTimeForEndpoint: Iso Ep UNIMPLEMENTED. FIXME\n");
743 IntEndpoint
= Tt
->FrameBudget
[ix
].IntEndpoint
;
744 nextTtEndpoint
= IntEndpoint
->NextTtEndpoint
;
746 for (nextTtEndpoint
= IntEndpoint
->NextTtEndpoint
;
748 nextTtEndpoint
= nextTtEndpoint
->NextTtEndpoint
)
750 if (USB2_CheckTtEndpointInsert(nextTtEndpoint
, TtEndpoint
))
752 IntEndpoint
= nextTtEndpoint
;
755 if ((frame
% TtEndpoint
->ActualPeriod
) == 0)
763 calcBusTime
= LatestStart
+ TtEndpoint
->CalcBusTime
-
764 nextTtEndpoint
->StartTime
;
768 calcBusTime
= TtEndpoint
->CalcBusTime
;
773 TimeUsed
= Tt
->FrameBudget
[ix
].TimeUsed
;
775 if (!USB2_AllocateCheck(&TimeUsed
,
777 USB2_FS_MAX_PERIODIC_ALLOCATION
))
779 DPRINT("USB2_AllocateTimeForEndpoint: Result = FALSE\n");
785 if (nextTtEndpoint
!= TtEndpoint
)
787 if ((frame
% TtEndpoint
->ActualPeriod
) == 0)
791 DPRINT("USB2_AllocateTimeForEndpoint: frame == 0\n");
792 TtEndpoint
->NextTtEndpoint
= nextTtEndpoint
;
795 IntEndpoint
->NextTtEndpoint
= TtEndpoint
;
797 DPRINT("USB2_AllocateTimeForEndpoint: TtEndpoint - %p, nextTtEndpoint - %p\n",
807 DPRINT("USB2_AllocateTimeForEndpoint: nextTtEndpoint - %p, calcBusTime - %X\n",
813 nextTtEndpoint
= nextTtEndpoint
->NextTtEndpoint
)
815 MoveResult
= USB2_MoveTtEndpoint(nextTtEndpoint
,
818 *RebalanceListEntries
,
823 DPRINT("USB2_AllocateTimeForEndpoint: Result = FALSE\n");
834 if ((frame
% TtEndpoint
->ActualPeriod
) == 0)
836 if (!USB2_AllocateHS(TtEndpoint
, frame
))
838 DPRINT1("USB2_AllocateTimeForEndpoint: USB2_AllocateHS return FALSE\n");
842 Tt
->FrameBudget
[ix
].TimeUsed
+= TtEndpoint
->CalcBusTime
;
847 USB2_DeallocateEndpointBudget(TtEndpoint
,
849 RebalanceListEntries
,
852 DPRINT("USB2_AllocateTimeForEndpoint: return FALSE\n");
857 DPRINT("USB2_AllocateTimeForEndpoint: Result - %X\n", Result
);
863 USB2_PromotePeriods(IN PUSB2_TT_ENDPOINT TtEndpoint
,
864 IN PUSB2_REBALANCE Rebalance
,
865 IN PULONG RebalanceListEntries
)
867 DPRINT1("USB2_PromotePeriods: UNIMPLEMENTED. FIXME\n");
874 USBPORT_UpdateAllocatedBwTt(IN PUSB2_TT_EXTENSION TtExtension
)
877 ULONG NewBusBandwidth
;
878 ULONG MaxBusBandwidth
= 0;
879 ULONG MinBusBandwidth
;
882 DPRINT("USBPORT_UpdateAllocatedBwTt: TtExtension - %p\n", TtExtension
);
884 BusBandwidth
= TtExtension
->BusBandwidth
;
885 MinBusBandwidth
= BusBandwidth
;
887 for (ix
= 0; ix
< USB2_FRAMES
; ix
++)
889 NewBusBandwidth
= BusBandwidth
- TtExtension
->Bandwidth
[ix
];
891 if (NewBusBandwidth
> MaxBusBandwidth
)
892 MaxBusBandwidth
= NewBusBandwidth
;
894 if (NewBusBandwidth
< MinBusBandwidth
)
895 MinBusBandwidth
= NewBusBandwidth
;
898 TtExtension
->MaxBandwidth
= MaxBusBandwidth
;
900 if (MinBusBandwidth
== BusBandwidth
)
901 TtExtension
->MinBandwidth
= 0;
903 TtExtension
->MinBandwidth
= MinBusBandwidth
;
908 USBPORT_AllocateBandwidthUSB2(IN PDEVICE_OBJECT FdoDevice
,
909 IN PUSBPORT_ENDPOINT Endpoint
)
911 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
912 PUSBPORT_ENDPOINT_PROPERTIES EndpointProperties
;
913 PUSB2_TT_EXTENSION TtExtension
;
915 PUSB2_REBALANCE Rebalance
;
916 LIST_ENTRY RebalanceList
;
917 ULONG RebalanceListEntries
;
918 PUSB2_TT_ENDPOINT TtEndpoint
;
919 PUSB2_TT_ENDPOINT RebalanceTtEndpoint
;
921 USB_DEVICE_SPEED DeviceSpeed
;
923 ULONG AllocedBusTime
;
924 ULONG EndpointBandwidth
;
925 ULONG ScheduleOffset
;
935 DPRINT("USBPORT_AllocateBandwidthUSB2: FdoDevice - %p, Endpoint - %p\n",
939 EndpointProperties
= &Endpoint
->EndpointProperties
;
940 EndpointProperties
->ScheduleOffset
= 0;
942 if (Endpoint
->Flags
& ENDPOINT_FLAG_ROOTHUB_EP0
)
944 DPRINT("USBPORT_AllocateBandwidthUSB2: ENDPOINT_FLAG_ROOTHUB_EP0\n");
948 FdoExtension
= FdoDevice
->DeviceExtension
;
950 TransferType
= EndpointProperties
->TransferType
;
951 DPRINT("USBPORT_AllocateBandwidthUSB2: TransferType - %X\n", TransferType
);
953 if (TransferType
== USBPORT_TRANSFER_TYPE_CONTROL
||
954 TransferType
== USBPORT_TRANSFER_TYPE_BULK
)
959 if (Endpoint
->TtExtension
)
960 TtExtension
= Endpoint
->TtExtension
;
964 InitializeListHead(&RebalanceList
);
966 Rebalance
= ExAllocatePoolWithTag(NonPagedPool
,
967 sizeof(USB2_REBALANCE
),
970 DPRINT("USBPORT_AllocateBandwidthUSB2: Rebalance - %p, TtExtension - %p\n",
976 RtlZeroMemory(Rebalance
, sizeof(USB2_REBALANCE
));
978 TtEndpoint
= Endpoint
->TtEndpoint
;
979 TtEndpoint
->Endpoint
= Endpoint
;
981 Direction
= EndpointProperties
->Direction
== USBPORT_TRANSFER_DIRECTION_OUT
;
982 DeviceSpeed
= EndpointProperties
->DeviceSpeed
;
989 Tt
= &TtExtension
->Tt
;
991 Period
= USB2_FRAMES
;
993 while (Period
> 0 && Period
> EndpointProperties
->Period
)
998 DPRINT("USBPORT_AllocateBandwidthUSB2: Period - %X\n", Period
);
1004 Tt
= &FdoExtension
->Usb2Extension
->HcTt
;
1006 if (EndpointProperties
->Period
> USB2_MAX_MICROFRAMES
)
1007 Period
= USB2_MAX_MICROFRAMES
;
1009 Period
= EndpointProperties
->Period
;
1016 DPRINT1("USBPORT_AllocateBandwidthUSB2: DeviceSpeed - %X!\n",
1021 Tt
= &TtExtension
->Tt
;
1026 USB2_InitTtEndpoint(TtEndpoint
,
1031 EndpointProperties
->MaxPacketSize
,
1034 RebalanceListEntries
= USB2_FRAMES
- 2;
1036 Result
= USB2_AllocateTimeForEndpoint(TtEndpoint
,
1038 &RebalanceListEntries
);
1042 Result
= USB2_PromotePeriods(TtEndpoint
,
1044 &RebalanceListEntries
);
1047 RebalanceListEntries
= 0;
1049 for (ix
= 0; Rebalance
->RebalanceEndpoint
[ix
]; ix
++)
1051 RebalanceListEntries
= ix
+ 1;
1056 RebalanceListEntries
= 0;
1060 DPRINT("USBPORT_AllocateBandwidthUSB2: RebalanceListEntries - %X, Result - %X\n",
1061 RebalanceListEntries
,
1064 for (ix
= 0; ix
< RebalanceListEntries
; ix
++)
1066 RebalanceTtEndpoint
= Rebalance
->RebalanceEndpoint
[ix
];
1068 DPRINT("USBPORT_AllocateBandwidthUSB2: RebalanceTtEndpoint[%X] - %p, RebalanceTtEndpoint - %p, RebalanceLink - %p\n",
1070 RebalanceTtEndpoint
,
1071 &RebalanceTtEndpoint
->Endpoint
->RebalanceLink
);
1073 InsertTailList(&RebalanceList
,
1074 &RebalanceTtEndpoint
->Endpoint
->RebalanceLink
);
1078 ExFreePoolWithTag(Rebalance
, USB_PORT_TAG
);
1082 SMask
= USB2_GetSMASK(Endpoint
->TtEndpoint
);
1083 EndpointProperties
->InterruptScheduleMask
= SMask
;
1085 CMask
= USB2_GetCMASK(Endpoint
->TtEndpoint
);
1086 EndpointProperties
->SplitCompletionMask
= CMask
;
1088 AllocedBusTime
= TtEndpoint
->CalcBusTime
;
1090 EndpointBandwidth
= USB2_MICROFRAMES
* AllocedBusTime
;
1091 EndpointProperties
->UsbBandwidth
= EndpointBandwidth
;
1093 ActualPeriod
= Endpoint
->TtEndpoint
->ActualPeriod
;
1094 EndpointProperties
->Period
= ActualPeriod
;
1096 ScheduleOffset
= Endpoint
->TtEndpoint
->StartFrame
;
1097 EndpointProperties
->ScheduleOffset
= ScheduleOffset
;
1099 Factor
= USB2_FRAMES
/ ActualPeriod
;
1102 n
= ScheduleOffset
* Factor
;
1106 for (ix
= 0; ix
< Factor
; ix
++)
1108 TtExtension
->Bandwidth
[n
+ ix
] -= EndpointBandwidth
;
1113 for (ix
= 1; ix
< Factor
; ix
++)
1115 FdoExtension
->Bandwidth
[n
+ ix
] -= EndpointBandwidth
;
1119 USBPORT_DumpingEndpointProperties(EndpointProperties
);
1120 USBPORT_DumpingTtEndpoint(Endpoint
->TtEndpoint
);
1122 if (AllocedBusTime
>= (USB2_FS_MAX_PERIODIC_ALLOCATION
/ 2))
1124 DPRINT1("USBPORT_AllocateBandwidthUSB2: AllocedBusTime >= 0.5 * MAX_ALLOCATION \n");
1128 //USB2_Rebalance(FdoDevice, &RebalanceList);
1132 DPRINT("USBPORT_AllocateBandwidthUSB2: Result - %X\n", Result
);
1136 for (ix
= 0; ix
< USB2_FRAMES
; ix
++)
1138 FdoExtension
->Bandwidth
[ix
] += TtExtension
->MaxBandwidth
;
1141 USBPORT_UpdateAllocatedBwTt(TtExtension
);
1143 for (ix
= 0; ix
< USB2_FRAMES
; ix
++)
1145 FdoExtension
->Bandwidth
[ix
] -= TtExtension
->MaxBandwidth
;
1148 DPRINT("USBPORT_AllocateBandwidthUSB2: Result - %X\n", Result
);
1155 USBPORT_FreeBandwidthUSB2(IN PDEVICE_OBJECT FdoDevice
,
1156 IN PUSBPORT_ENDPOINT Endpoint
)
1158 DPRINT1("USBPORT_FreeBandwidthUSB2: UNIMPLEMENTED. FIXME. \n");
1163 USB2_InitTT(IN PUSB2_HC_EXTENSION HcExtension
,
1169 DPRINT("USB2_InitTT: HcExtension - %p, Tt - %p\n", HcExtension
, Tt
);
1171 Tt
->HcExtension
= HcExtension
;
1173 Tt
->MaxTime
= USB2_FS_MAX_PERIODIC_ALLOCATION
;
1175 for (ix
= 0; ix
< USB2_FRAMES
; ix
++)
1177 Tt
->FrameBudget
[ix
].TimeUsed
= USB2_MAX_MICROFRAMES
;
1178 Tt
->FrameBudget
[ix
].AltEndpoint
= NULL
;
1180 for (jx
= 0; jx
< USB2_MICROFRAMES
; jx
++)
1182 Tt
->TimeCS
[ix
][jx
] = 0;
1183 Tt
->NumStartSplits
[ix
][jx
] = 0;
1186 Tt
->FrameBudget
[ix
].IsoEndpoint
= &Tt
->IsoEndpoint
[ix
];
1188 USB2_InitTtEndpoint(&Tt
->IsoEndpoint
[ix
],
1189 USBPORT_TRANSFER_TYPE_ISOCHRONOUS
,
1190 USBPORT_TRANSFER_DIRECTION_OUT
,
1196 Tt
->IsoEndpoint
[ix
].ActualPeriod
= USB2_FRAMES
;
1197 Tt
->IsoEndpoint
[ix
].CalcBusTime
= USB2_FS_SOF_TIME
+ USB2_HUB_DELAY
;
1198 Tt
->IsoEndpoint
[ix
].StartFrame
= ix
;
1199 Tt
->IsoEndpoint
[ix
].StartMicroframe
= 0xFF;
1201 Tt
->FrameBudget
[ix
].IntEndpoint
= &Tt
->IntEndpoint
[ix
];
1203 USB2_InitTtEndpoint(&Tt
->IntEndpoint
[ix
],
1204 USBPORT_TRANSFER_TYPE_INTERRUPT
,
1205 USBPORT_TRANSFER_DIRECTION_OUT
,
1211 Tt
->IntEndpoint
[ix
].ActualPeriod
= USB2_FRAMES
;
1212 Tt
->IntEndpoint
[ix
].CalcBusTime
= USB2_FS_SOF_TIME
+ USB2_HUB_DELAY
;
1213 Tt
->IntEndpoint
[ix
].StartFrame
= ix
;
1214 Tt
->IntEndpoint
[ix
].StartMicroframe
= 0xFF;
1220 USB2_InitController(IN PUSB2_HC_EXTENSION HcExtension
)
1225 DPRINT("USB2_InitController: HcExtension - %p\n", HcExtension
);
1227 HcExtension
->MaxHsBusAllocation
= USB2_MAX_MICROFRAME_ALLOCATION
;
1229 for (ix
= 0; ix
< USB2_FRAMES
; ix
++)
1231 for (jx
= 0; jx
< USB2_MICROFRAMES
; jx
++)
1233 HcExtension
->TimeUsed
[ix
][jx
] = 0;
1237 HcExtension
->HcDelayTime
= USB2_CONTROLLER_DELAY
;
1239 USB2_InitTT(HcExtension
, &HcExtension
->HcTt
);