Skip to content

Commit 08904d5

Browse files
committed
[USBEHCI]
- Try fix alignment of USBEHCI_HCD_ITD (needs 32 byte aligned) - Store active ITD in EHCI_TRANSFER and use it for completion checks - Implement USBD_START_ISO_TRANSFER_ASAP flag support - Fix USBPORT_AllocateTransfer for iso packets with MDL (unused) [USBVIDEO] - Silence traces - Readd frame patching
1 parent 958df48 commit 08904d5

9 files changed

Lines changed: 198 additions & 128 deletions

File tree

drivers/usb/usbehci/usbehci.c

Lines changed: 129 additions & 95 deletions
Large diffs are not rendered by default.

drivers/usb/usbehci/usbehci.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ extern USBPORT_REGISTRATION_PACKET RegPacket;
3737

3838
#define EHCI_MAX_HC_SYSTEM_ERRORS 256
3939

40+
#define ROUND_DOWN(n, align) (((ULONG)n) & ~((align) - 1l))
41+
#define ROUND_UP(n, align) ROUND_DOWN(((ULONG)n) + (align) - 1, (align))
42+
4043
typedef struct _EHCI_PERIOD {
4144
UCHAR Period;
4245
UCHAR PeriodIdx;
@@ -107,12 +110,12 @@ typedef struct _EHCI_HCD_ITD {
107110
LIST_ENTRY DoneLink;
108111
ULONG ScheduledFrame; /* Frame list index where this iTD was inserted */
109112
#ifdef _WIN64
110-
ULONG Pad[7];
113+
ULONG Pad[23];
111114
#else
112-
ULONG Pad[13];
115+
ULONG Pad[41];
113116
#endif
114117
} EHCI_HCD_ITD, *PEHCI_HCD_ITD;
115-
118+
C_ASSERT(sizeof(EHCI_HCD_ITD) == ROUND_UP(sizeof(EHCI_HCD_ITD), 32));
116119

117120
/* Split Isochronous Transfer Descriptor */
118121
typedef struct _EHCI_HCD_SITD {
@@ -220,6 +223,7 @@ typedef struct _EHCI_TRANSFER {
220223
ULONG USBDStatus;
221224
ULONG TransferLen;
222225
PEHCI_ENDPOINT EhciEndpoint;
226+
PEHCI_HCD_ITD ActiveITD;
223227
ULONG PendingTDs;
224228
ULONG TransferOnAsyncList;
225229
} EHCI_TRANSFER, *PEHCI_TRANSFER;
@@ -406,12 +410,13 @@ NTAPI
406410
EHCI_CheckIsoBandwidth(IN PEHCI_EXTENSION EhciExtension,
407411
IN PEHCI_ENDPOINT EhciEndpoint,
408412
IN ULONG TransferLength);
409-
413+
410414
VOID
411415
NTAPI
412416
EHCI_UnlinkITDFromFrameList(IN PEHCI_EXTENSION EhciExtension,
413417
IN PEHCI_HCD_ITD ITD,
414-
IN ULONG Frame);
418+
IN ULONG Frame,
419+
IN PEHCI_TRANSFER EhciTransfer);
415420

416421
VOID
417422
NTAPI
@@ -423,5 +428,5 @@ EHCI_InitializeITD(IN PEHCI_EXTENSION EhciExtension,
423428
IN ULONG BufferPhysicalAddress,
424429
IN ULONG TransferLength,
425430
IN ULONG MicroframeNumber);
426-
431+
427432
#endif /* USBEHCI_H__ */

drivers/usb/usbport/iso.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ USBPORT_InitializeIsoTransfer(PDEVICE_OBJECT FdoDevice,
6060
ULONG TotalPackets, Idx;
6161
ULONG Period;
6262
BOOLEAN IsHighSpeed;
63+
PUSBPORT_DEVICE_EXTENSION FdoExtension;
64+
PUSBPORT_REGISTRATION_PACKET Packet;
6365

6466
DPRINT("USBPORT_InitializeIsoTransfer: FdoDevice - %p, Urb - %p Irp - %p\n", FdoDevice, Urb, Transfer->Irp);
6567

@@ -98,6 +100,14 @@ USBPORT_InitializeIsoTransfer(PDEVICE_OBJECT FdoDevice,
98100
IsoBlock->TotalPackets = TotalPackets;
99101
IsoBlock->MappedBuffer = (PVOID)SgTable->MappedSystemVa;
100102

103+
if (Urb->TransferFlags & USBD_START_ISO_TRANSFER_ASAP)
104+
{
105+
FdoExtension = FdoDevice->DeviceExtension;
106+
Packet = &FdoExtension->MiniPortInterface->Packet;
107+
Urb->StartFrame = (Packet->Get32BitFrameNumber(FdoExtension->MiniPortExt) + 64) & 0xFFFFFFF0;
108+
DPRINT("Urb StartFrame %u\n", Urb->StartFrame);
109+
}
110+
101111
/*
102112
* Walk each URB packet descriptor, compute its actual byte length
103113
* from the offset array, resolve the physical scatter/gather mapping,
@@ -208,7 +218,6 @@ USBPORT_CompleteIsoTransfer(IN PVOID MiniPortExtension,
208218
IN PVOID TransferParameters,
209219
IN ULONG TransferLength)
210220
{
211-
PUSBPORT_DEVICE_EXTENSION FdoExtension;
212221
PUSBPORT_ENDPOINT Endpoint;
213222
PUSBPORT_TRANSFER Transfer;
214223
struct _URB_ISOCH_TRANSFER *IsoUrb;

drivers/usb/usbport/usbport.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2664,14 +2664,29 @@ USBPORT_AllocateTransfer(IN PDEVICE_OBJECT FdoDevice,
26642664

26652665
if (TransferLength)
26662666
{
2667-
Mdl = Urb->UrbControlTransfer.TransferBufferMDL;
2668-
VirtualAddr = (ULONG_PTR)MmGetMdlVirtualAddress(Mdl);
2667+
if (Urb->UrbHeader.Function == URB_FUNCTION_CONTROL_TRANSFER)
2668+
{
2669+
Mdl = Urb->UrbControlTransfer.TransferBufferMDL;
2670+
VirtualAddr = (ULONG_PTR)MmGetMdlVirtualAddress(Mdl);
26692671

2670-
PagesNeed = ADDRESS_AND_SIZE_TO_SPAN_PAGES(VirtualAddr,
2672+
PagesNeed = ADDRESS_AND_SIZE_TO_SPAN_PAGES(VirtualAddr,
26712673
TransferLength);
2672-
if (PagesNeed > 0)
2674+
if (PagesNeed > 0)
2675+
{
2676+
PagesNeed--;
2677+
}
2678+
}
2679+
else if (Urb->UrbHeader.Function == URB_FUNCTION_ISOCH_TRANSFER)
26732680
{
2674-
PagesNeed--;
2681+
Mdl = Urb->UrbIsochronousTransfer.TransferBufferMDL;
2682+
VirtualAddr = (ULONG_PTR)MmGetMdlVirtualAddress(Mdl);
2683+
2684+
PagesNeed = ADDRESS_AND_SIZE_TO_SPAN_PAGES(VirtualAddr,
2685+
TransferLength);
2686+
if (PagesNeed > 0)
2687+
{
2688+
PagesNeed--;
2689+
}
26752690
}
26762691
}
26772692

drivers/usb/usbvideo/bulk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* PROGRAMMERS:
77
* Johannes Anderwald (johannes.anderwald@reactos.org)
88
*/
9-
#define YDEBUG
9+
#define NDEBUG
1010
#include "usbvideo.h"
1111

1212

drivers/usb/usbvideo/iso.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ USBVideoIsoReadComplete(
105105
USBVideoQueueIsoRead(Pin,
106106
DeviceExtension->hPipe,
107107
(PUCHAR)Urb->UrbIsochronousTransfer.TransferBuffer,
108-
32 * 1024,
108+
ISO_TRANSFER_SIZE,
109109
Irp,
110110
Urb,
111111
Frame);
@@ -184,7 +184,7 @@ USBVideoIsoReadComplete(
184184
USBVideoQueueIsoRead(Pin,
185185
DeviceExtension->hPipe,
186186
(PUCHAR)Urb->UrbIsochronousTransfer.TransferBuffer,
187-
32 * 1024,
187+
ISO_TRANSFER_SIZE,
188188
Irp,
189189
Urb,
190190
Frame);

drivers/usb/usbvideo/pin.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ USBVideoPinSetDeviceState(
5555
Pin,
5656
DeviceExtension->hPipe,
5757
DeviceExtension->BulkBuffer[Index],
58-
32* 1024,
58+
ISO_TRANSFER_SIZE,
5959
DeviceExtension->Irp[Index],
6060
DeviceExtension->Urb[Index],
6161
&DeviceExtension->FrameCtx[Index]);
@@ -146,11 +146,9 @@ USBVideoGetDataRangeIndexForFormat(
146146

147147
}
148148
//HACK return first available format
149-
*FormatIndex = DeviceExtension->VideoFormatInfo[0].bFormatIndex;
150-
*FrameIndex = DeviceExtension->VideoFormatInfo[0].bFrameIndex;
151-
*dwFrameInterval = DeviceExtension->VideoFormatInfo[0].dwFrameInterval;
152-
153-
ASSERT(FALSE);
149+
*FormatIndex = DeviceExtension->VideoFormatInfo[2].bFormatIndex;
150+
*FrameIndex = DeviceExtension->VideoFormatInfo[2].bFrameIndex;
151+
*dwFrameInterval = DeviceExtension->VideoFormatInfo[2].dwFrameInterval;
154152
return STATUS_SUCCESS;
155153
}
156154

@@ -543,7 +541,7 @@ USBVideoPinCreate(
543541
return STATUS_INSUFFICIENT_RESOURCES;
544542
}
545543

546-
DeviceExtension->BulkBuffer[i] = AllocFunction(32 * 1024);
544+
DeviceExtension->BulkBuffer[i] = AllocFunction(ISO_TRANSFER_SIZE);
547545
if (!DeviceExtension->BulkBuffer[i])
548546
{
549547
/* no memory */

drivers/usb/usbvideo/usbvideo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ typedef struct
6060
}STILL_PROBE_COMMIT, *PSTILL_PROBE_COMMIT;;
6161

6262

63-
#define ISO_PACKET_COUNT 8
63+
#define ISO_PACKET_COUNT 24 // FIXME determine packet count by bInterval
6464
#define BULK_TRANSFER_SIZE (64*1024)
65+
#define ISO_TRANSFER_SIZE (64*1024)
6566
#define URB_POOL_COUNT 4
6667

6768
typedef struct _FRAME_CONTEXT {

drivers/usb/usbvideo/uvc.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* PROGRAMMERS:
77
* Johannes Anderwald (johannes.anderwald@reactos.org)
88
*/
9-
#define YDEBUG
9+
#define NDEBUG
1010
#include "usbvideo.h"
1111

1212
static const JFIF_APP0 g_JfifApp0 = {
@@ -37,20 +37,20 @@ UvcPatchAvi1ToJfif(
3737

3838
if (pFrame[0] != 0xFF || pFrame[1] != 0xD8)
3939
{
40-
DPRINT1("invalid soi\n");
40+
DPRINT("invalid soi\n");
4141
return STATUS_INVALID_PARAMETER;
4242
}
4343

4444
if (pFrame[2] != 0xFF || pFrame[3] != 0xE0)
4545
{
46-
DPRINT1("no app maker\n");
46+
DPRINT("no app maker\n");
4747
return STATUS_INVALID_PARAMETER;
4848
}
4949

5050
USHORT app0Len = (pFrame[4] << 8) | pFrame[5];
5151
if (frameSize < (ULONG)(2 + 2 + app0Len))
5252
{
53-
DPRINT1("unexpected app0len\n");
53+
DPRINT("unexpected app0len\n");
5454
return STATUS_INVALID_PARAMETER;
5555
}
5656

@@ -122,6 +122,7 @@ USBVideoDeliverFrame(
122122
{
123123
PKSSTREAM_POINTER StreamPointer;
124124
PKSPIN Pin;
125+
NTSTATUS Status;
125126

126127
if (!DeviceExtension || !DeviceExtension->Pin)
127128
{
@@ -151,12 +152,19 @@ USBVideoDeliverFrame(
151152
if (BytesToCopy > 0)
152153
{
153154
DPRINT("USBVideoDeliverFrame: Copying %u bytes to user buffer (frame size %u)\n", BytesToCopy, FrameSize);
154-
RtlCopyMemory(StreamPointer->StreamHeader->Data, FrameBuffer, BytesToCopy);
155-
StreamPointer->StreamHeader->DataUsed = BytesToCopy;
156-
157-
/* Advance the stream pointer to deliver the buffer */
158-
KsStreamPointerAdvance(StreamPointer);
159-
DPRINT("USBVideoDeliverFrame: Frame delivered\n");
155+
Status = UvcPatchAvi1ToJfif(FrameBuffer, BytesToCopy, StreamPointer->StreamHeader->Data, &BytesToCopy);
156+
//RtlCopyMemory(StreamPointer->StreamHeader->Data, FrameBuffer, BytesToCopy);
157+
//Status = STATUS_SUCCESS;
158+
if (NT_SUCCESS(Status))
159+
{
160+
/* Advance the stream pointer to deliver the buffer */
161+
KsStreamPointerAdvanceOffsetsAndUnlock(StreamPointer, 0, BytesToCopy, FALSE);
162+
KsStreamPointerUnlock(StreamPointer, FALSE);
163+
}
164+
else
165+
{
166+
KsStreamPointerUnlock(StreamPointer, FALSE);
167+
}
160168
}
161169
else
162170
{

0 commit comments

Comments
 (0)