Skip to content

Commit df6d845

Browse files
committed
[USBPORT][USBEHCI]
- Some iso transfer fixes
1 parent 2d6bd9a commit df6d845

3 files changed

Lines changed: 43 additions & 29 deletions

File tree

drivers/usb/usbehci/usbehci.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,9 @@ EHCI_OpenInterruptEndpoint(IN PEHCI_EXTENSION EhciExtension,
505505
return MP_STATUS_SUCCESS;
506506
}
507507

508+
#define ROUND_DOWN(n, align) (((ULONG)n) & ~((align) - 1l))
509+
#define ROUND_UP(n, align) ROUND_DOWN(((ULONG)n) + (align) - 1, (align))
510+
508511
MPSTATUS
509512
NTAPI
510513
EHCI_OpenHsIsoEndpoint(IN PEHCI_EXTENSION EhciExtension,
@@ -516,7 +519,7 @@ EHCI_OpenHsIsoEndpoint(IN PEHCI_EXTENSION EhciExtension,
516519
ULONG ItdCount;
517520
PEHCI_HCD_ITD ITD;
518521
ULONG ix;
519-
522+
ULONG Size;
520523
DPRINT("EHCI_OpenHsIsoEndpoint: EhciEndpoint - %p\n", EhciEndpoint);
521524

522525
RtlCopyMemory(&EhciEndpoint->EndpointProperties,
@@ -528,7 +531,7 @@ EHCI_OpenHsIsoEndpoint(IN PEHCI_EXTENSION EhciExtension,
528531

529532
/* Calculate iTD count and pointers */
530533
ItdCount = EndpointProperties->BufferLength / sizeof(EHCI_HCD_ITD);
531-
534+
532535
if (ItdCount == 0)
533536
{
534537
return MP_STATUS_NO_RESOURCES;
@@ -541,6 +544,7 @@ EHCI_OpenHsIsoEndpoint(IN PEHCI_EXTENSION EhciExtension,
541544
EhciEndpoint->MaxITDs = ItdCount;
542545
EhciEndpoint->RemainITDs = ItdCount;
543546

547+
Size = ROUND_UP(sizeof(EHCI_HCD_ITD), 32);
544548
/* Initialize all iTDs in the buffer */
545549
for (ix = 0; ix < ItdCount; ix++)
546550
{
@@ -554,7 +558,7 @@ EHCI_OpenHsIsoEndpoint(IN PEHCI_EXTENSION EhciExtension,
554558
ITD->EhciEndpoint = EhciEndpoint;
555559
ITD->EhciTransfer = NULL;
556560

557-
FirstItdPA += sizeof(EHCI_HCD_ITD);
561+
FirstItdPA += Size;
558562
}
559563

560564
/* Initialize isochronous endpoint specific fields */
@@ -592,7 +596,7 @@ EHCI_OpenIsoEndpoint(IN PEHCI_EXTENSION EhciExtension,
592596

593597
/* Calculate siTD count and pointers */
594598
SitdCount = EndpointProperties->BufferLength / sizeof(EHCI_HCD_SITD);
595-
599+
596600
if (SitdCount == 0)
597601
{
598602
return MP_STATUS_NO_RESOURCES;
@@ -2636,7 +2640,7 @@ EHCI_SubmitIsoTransfer(IN PVOID ehciExtension,
26362640

26372641
MaxPacketSize = EhciEndpoint->EndpointProperties.MaxPacketSize;
26382642
TransferLength = TransferParameters->TransferBufferLength;
2639-
2643+
26402644
/* For ISO transfers, we'll need to use the DMA buffer from the endpoint
26412645
* or extract from isoParameters - simplified for now */
26422646
BufferPA = EhciEndpoint->DmaBufferPA;
@@ -2677,12 +2681,12 @@ EHCI_SubmitIsoTransfer(IN PVOID ehciExtension,
26772681
/* Link iTD into the frame list */
26782682
LinkPointer.AsULONG = HcResourcesVA->PeriodicFrameList[FrameIndex];
26792683
ITD->HwTD.NextLink = LinkPointer;
2680-
2684+
26812685
LinkPointer.AsULONG = ITD->PhysicalAddress;
26822686
LinkPointer.Type = EHCI_LINK_TYPE_iTD;
26832687
LinkPointer.Terminate = 0;
26842688
LinkPointer.Reserved = 0;
2685-
2689+
26862690
HcResourcesVA->PeriodicFrameList[FrameIndex] = LinkPointer.AsULONG;
26872691

26882692
/* Store scheduling information for later cleanup */
@@ -2730,7 +2734,7 @@ EHCI_AbortIsoTransfer(IN PEHCI_EXTENSION EhciExtension,
27302734
ULONG ix;
27312735

27322736
DPRINT("EHCI_AbortIsoTransfer: Aborting high-speed ISO transfer\n");
2733-
2737+
27342738
/* Find iTDs belonging to this transfer and abort them */
27352739
ITD = EhciEndpoint->FirstITD;
27362740
for (ix = 0; ix < EhciEndpoint->MaxITDs; ix++)
@@ -2775,7 +2779,7 @@ EHCI_AbortIsoTransfer(IN PEHCI_EXTENSION EhciExtension,
27752779
{
27762780
/* Abort full-speed/low-speed isochronous transfer using siTD */
27772781
DPRINT("EHCI_AbortIsoTransfer: Aborting full/low-speed ISO transfer\n");
2778-
2782+
27792783
/* TODO: Implement siTD abort logic similar to iTD */
27802784
}
27812785

@@ -4054,7 +4058,7 @@ EHCI_InitializeITD(IN PEHCI_EXTENSION EhciExtension,
40544058
ITD->HwTD.Buffer[0].AsULONG = BufferPhysicalAddress & ~0xFFF;
40554059
ITD->HwTD.Buffer[0].DeviceAddress = DeviceAddress;
40564060
ITD->HwTD.Buffer[0].EndpointNumber = EndpointNumber;
4057-
4061+
40584062
/* Setup buffer 1 with additional info */
40594063
ITD->HwTD.Buffer[1].MaximumPacketSize = EndpointProperties->MaxPacketSize;
40604064
ITD->HwTD.Buffer[1].Direction = Direction;
@@ -4109,7 +4113,7 @@ EHCI_ProcessCompletedITD(IN PEHCI_EXTENSION EhciExtension,
41094113
/* Check for errors */
41104114
if (Status & 0x8) /* Transaction Error */
41114115
{
4112-
DPRINT1("EHCI_ProcessCompletedITD: Transaction %d error, status 0x%x\n",
4116+
DPRINT1("EHCI_ProcessCompletedITD: Transaction %d error, status 0x%x\n",
41134117
TransactionIndex, Status);
41144118
EhciTransfer->USBDStatus = USBD_STATUS_ERROR;
41154119
}
@@ -4166,7 +4170,7 @@ EHCI_UnlinkITDFromFrameList(IN PEHCI_EXTENSION EhciExtension,
41664170

41674171
/* Search and unlink from frame list - simplified version */
41684172
CurrentLink = (EHCI_LINK_POINTER*)&HcResourcesVA->PeriodicFrameList[FrameIndex];
4169-
4173+
41704174
if ((CurrentLink->AsULONG & LINK_POINTER_MASK) == TargetPhysicalAddress &&
41714175
CurrentLink->Type == EHCI_LINK_TYPE_iTD)
41724176
{
@@ -4215,14 +4219,14 @@ EHCI_CheckIsoBandwidth(IN PEHCI_EXTENSION EhciExtension,
42154219
{
42164220
/* High-speed: bandwidth per microframe */
42174221
RequiredBandwidth = MaxPacketSize;
4218-
4222+
42194223
/* Add overhead and safety margin */
42204224
RequiredBandwidth += 64; /* Protocol overhead */
4221-
4225+
42224226
/* Simple check: ensure we don't exceed 80% of frame time */
42234227
if (RequiredBandwidth > (188 * 8 * 80 / 100)) /* 80% of max HS bandwidth */
42244228
{
4225-
DPRINT1("EHCI_CheckIsoBandwidth: High-speed bandwidth requirement too high: %d\n",
4229+
DPRINT1("EHCI_CheckIsoBandwidth: High-speed bandwidth requirement too high: %d\n",
42264230
RequiredBandwidth);
42274231
return FALSE;
42284232
}
@@ -4231,14 +4235,14 @@ EHCI_CheckIsoBandwidth(IN PEHCI_EXTENSION EhciExtension,
42314235
{
42324236
/* Full/Low-speed: bandwidth per frame */
42334237
RequiredBandwidth = MaxPacketSize;
4234-
4238+
42354239
/* Add split transaction overhead */
42364240
RequiredBandwidth += 128; /* Split transaction overhead */
4237-
4241+
42384242
/* Simple check: ensure we don't exceed 90% of frame time for FS */
42394243
if (RequiredBandwidth > (1023 * 90 / 100)) /* 90% of max FS bandwidth per frame */
42404244
{
4241-
DPRINT1("EHCI_CheckIsoBandwidth: Full-speed bandwidth requirement too high: %d\n",
4245+
DPRINT1("EHCI_CheckIsoBandwidth: Full-speed bandwidth requirement too high: %d\n",
42424246
RequiredBandwidth);
42434247
return FALSE;
42444248
}

drivers/usb/usbport/endpoint.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ USBPORT_AllocateBandwidth(IN PDEVICE_OBJECT FdoDevice,
115115
DPRINT1("USBPORT_AllocateBandwidth: Period is zero\n");
116116
return FALSE;
117117
}
118-
118+
119119
if (Period > USB2_FRAMES)
120120
{
121-
DPRINT1("USBPORT_AllocateBandwidth: Period %lu exceeds maximum %d\n",
121+
DPRINT1("USBPORT_AllocateBandwidth: Period %lu exceeds maximum %d\n",
122122
Period, USB2_FRAMES);
123123
return FALSE;
124124
}
@@ -128,14 +128,14 @@ USBPORT_AllocateBandwidth(IN PDEVICE_OBJECT FdoDevice,
128128
for (Offset = 0; Offset < Period; Offset++)
129129
{
130130
MinBandwidth = TotalBusBandwidth;
131-
131+
132132
// Bounds check before accessing bandwidth array
133133
if ((Offset * Factor) >= USB2_FRAMES)
134134
{
135135
DPRINT1("USBPORT_AllocateBandwidth: Array index out of bounds\n");
136136
continue;
137137
}
138-
138+
139139
Bandwidth = &FdoExtension->Bandwidth[Offset * Factor];
140140

141141
for (ix = 1; *Bandwidth >= EndpointBandwidth; ix++)
@@ -327,7 +327,7 @@ USBPORT_EndpointHasQueuedTransfers(IN PDEVICE_OBJECT FdoDevice,
327327
}
328328

329329
Entry = Entry->Flink;
330-
330+
331331
// Prevent infinite loop in case of circular corruption
332332
if (*TransferCount > 1000)
333333
{
@@ -562,7 +562,7 @@ USBPORT_DeleteEndpoint(IN PDEVICE_OBJECT FdoDevice,
562562

563563
FdoExtension = FdoDevice->DeviceExtension;
564564

565-
/*
565+
/*
566566
* Remove endpoint from EpStateChangeList if it's still there.
567567
* This prevents the DPC handler from accessing a deleted endpoint.
568568
*/
@@ -574,10 +574,10 @@ USBPORT_DeleteEndpoint(IN PDEVICE_OBJECT FdoDevice,
574574
{
575575
RemoveEntryList(&Endpoint->StateChangeLink);
576576
}
577-
577+
578578
Endpoint->StateChangeLink.Flink = NULL;
579579
Endpoint->StateChangeLink.Blink = NULL;
580-
580+
581581
KeReleaseSpinLock(&FdoExtension->EpStateChangeSpinLock, StateChangeOldIrql);
582582

583583
if ((Endpoint->WorkerLink.Flink && Endpoint->WorkerLink.Blink) ||
@@ -1027,17 +1027,21 @@ USBPORT_OpenPipe(IN PDEVICE_OBJECT FdoDevice,
10271027
}
10281028
}
10291029

1030-
if (EndpointProperties->TransferType == USB_ENDPOINT_TYPE_ISOCHRONOUS)
1030+
if (EndpointProperties->TransferType == USBPORT_TRANSFER_TYPE_ISOCHRONOUS)
10311031
{
10321032
if (EndpointProperties->DeviceSpeed == UsbHighSpeed)
10331033
{
10341034
EndpointProperties->Period =
10351035
USBPORT_NormalizeHsInterval(EndpointDescriptor->bInterval);
1036+
ASSERT(EndpointProperties->Period);
1037+
if (!EndpointProperties->Period)
1038+
EndpointProperties->Period = ENDPOINT_INTERRUPT_1ms;
10361039
}
10371040
else
10381041
{
10391042
EndpointProperties->Period = ENDPOINT_INTERRUPT_1ms;
10401043
}
1044+
DPRINT1("Endpoint %p Period %u\n", EndpointProperties, EndpointProperties->Period);
10411045
}
10421046

10431047
if ((DeviceHandle->Flags & DEVICE_HANDLE_FLAG_ROOTHUB) != 0)

drivers/usb/usbport/usb2.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "usbport.h"
99

10-
#define NDEBUG
10+
#define YDEBUG
1111
#include <debug.h>
1212

1313
static const UCHAR CMASKS[USB2_MICROFRAMES] = {
@@ -1334,7 +1334,10 @@ USB2_AllocateTimeForEndpoint(IN PUSB2_TT_ENDPOINT TtEndpoint,
13341334
if (TtEndpoint->Period > USB2_MAX_MICROFRAMES)
13351335
TtEndpoint->ActualPeriod = USB2_MAX_MICROFRAMES;
13361336
else
1337+
{
1338+
ASSERT(TtEndpoint->Period);
13371339
TtEndpoint->ActualPeriod = TtEndpoint->Period;
1340+
}
13381341

13391342
MinTimeUsed = HcExtension->TimeUsed[0][0];
13401343

@@ -1369,7 +1372,7 @@ USB2_AllocateTimeForEndpoint(IN PUSB2_TT_ENDPOINT TtEndpoint,
13691372
DPRINT("USB2_AllocateTimeForEndpoint: Microframe >= 256. Result - TRUE\n");
13701373
return TRUE;
13711374
}
1372-
1375+
ASSERT(TtEndpoint->ActualPeriod);
13731376
for (ix = Microframe;
13741377
ix < USB2_MAX_MICROFRAMES;
13751378
ix += TtEndpoint->ActualPeriod)
@@ -1388,6 +1391,7 @@ USB2_AllocateTimeForEndpoint(IN PUSB2_TT_ENDPOINT TtEndpoint,
13881391
{
13891392
DPRINT("USB2_AllocateTimeForEndpoint: Result = FALSE\n");
13901393
Result = FALSE;
1394+
break;
13911395
}
13921396
}
13931397

@@ -1893,7 +1897,9 @@ USBPORT_AllocateBandwidthUSB2(IN PDEVICE_OBJECT FdoDevice,
18931897
{
18941898
Tt = &FdoExtension->Usb2Extension->HcTt;
18951899
Period = EndpointProperties->Period;
1900+
DPRINT("USBPORT_AllocateBandwidthUSB2: Endpoint %p Period - %X\n", EndpointProperties, Period);
18961901

1902+
ASSERT(Period);
18971903
break;
18981904
}
18991905

0 commit comments

Comments
 (0)