Something went wrong. Try again.
Reactos
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418/* * PROJECT: ReactOS USB Port Driver * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+) * PURPOSE: USBPort queue implementation * COPYRIGHT: Copyright 2017 Vadim Galyant <vgal@rambler.ru> */
#include "usbport.h"
#define NDEBUG#include <debug.h>
#define NDEBUG_USBPORT_CORE#define NDEBUG_USBPORT_QUEUE#define NDEBUG_USBPORT_URB#include "usbdebug.h"
VOIDNTAPIUSBPORT_InsertIdleIrp(IN PIO_CSQ Csq, IN PIRP Irp){ PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_QUEUE("USBPORT_InsertIdleIrp: Irp - %p\n", Irp);
FdoExtension = CONTAINING_RECORD(Csq, USBPORT_DEVICE_EXTENSION, IdleIoCsq);
InsertTailList(&FdoExtension->IdleIrpList, &Irp->Tail.Overlay.ListEntry);}
VOIDNTAPIUSBPORT_RemoveIdleIrp(IN PIO_CSQ Csq, IN PIRP Irp){ DPRINT_QUEUE("USBPORT_RemoveIdleIrp: Irp - %p\n", Irp); RemoveEntryList(&Irp->Tail.Overlay.ListEntry);}
PIRPNTAPIUSBPORT_PeekNextIdleIrp(IN PIO_CSQ Csq, IN PIRP Irp, IN PVOID PeekContext){ PUSBPORT_DEVICE_EXTENSION FdoExtension; PLIST_ENTRY NextEntry; PLIST_ENTRY ListHead; PIRP NextIrp = NULL;
DPRINT_QUEUE("USBPORT_PeekNextIdleIrp: Irp - %p, PeekContext - %p\n", Irp, PeekContext);
FdoExtension = CONTAINING_RECORD(Csq, USBPORT_DEVICE_EXTENSION, IdleIoCsq);
ListHead = &FdoExtension->IdleIrpList;
if (Irp) { NextEntry = Irp->Tail.Overlay.ListEntry.Flink; } else { NextEntry = ListHead->Flink; }
while (NextEntry != ListHead) { NextIrp = CONTAINING_RECORD(NextEntry, IRP, Tail.Overlay.ListEntry);
if (!PeekContext) break;
NextEntry = NextEntry->Flink; }
return NextIrp;}
VOIDNTAPIUSBPORT_AcquireIdleLock(IN PIO_CSQ Csq, IN PKIRQL Irql){ PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_QUEUE("USBPORT_AcquireIdleLock: ... \n");
FdoExtension = CONTAINING_RECORD(Csq, USBPORT_DEVICE_EXTENSION, IdleIoCsq);
KeAcquireSpinLock(&FdoExtension->IdleIoCsqSpinLock, Irql);}
VOIDNTAPIUSBPORT_ReleaseIdleLock(IN PIO_CSQ Csq, IN KIRQL Irql){ PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_QUEUE("USBPORT_ReleaseIdleLock: ... \n");
FdoExtension = CONTAINING_RECORD(Csq, USBPORT_DEVICE_EXTENSION, IdleIoCsq);
KeReleaseSpinLock(&FdoExtension->IdleIoCsqSpinLock, Irql);}
VOIDNTAPIUSBPORT_CompleteCanceledIdleIrp(IN PIO_CSQ Csq, IN PIRP Irp){ PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_QUEUE("USBPORT_CompleteCanceledIdleIrp: ... \n");
FdoExtension = CONTAINING_RECORD(Csq, USBPORT_DEVICE_EXTENSION, IdleIoCsq);
InterlockedDecrement(&FdoExtension->IdleLockCounter);
Irp->IoStatus.Status = STATUS_CANCELLED; Irp->IoStatus.Information = 0; IoCompleteRequest(Irp, IO_NO_INCREMENT);}
VOIDNTAPIUSBPORT_InsertBadRequest(IN PIO_CSQ Csq, IN PIRP Irp){ PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_QUEUE("USBPORT_InsertBadRequest: Irp - %p\n", Irp);
FdoExtension = CONTAINING_RECORD(Csq, USBPORT_DEVICE_EXTENSION, BadRequestIoCsq);
InsertTailList(&FdoExtension->BadRequestList, &Irp->Tail.Overlay.ListEntry);}
VOIDNTAPIUSBPORT_RemoveBadRequest(IN PIO_CSQ Csq, IN PIRP Irp){ DPRINT_QUEUE("USBPORT_RemoveBadRequest: Irp - %p\n", Irp); RemoveEntryList(&Irp->Tail.Overlay.ListEntry);}
PIRPNTAPIUSBPORT_PeekNextBadRequest(IN PIO_CSQ Csq, IN PIRP Irp, IN PVOID PeekContext){ PUSBPORT_DEVICE_EXTENSION FdoExtension; PLIST_ENTRY NextEntry; PLIST_ENTRY ListHead; PIRP NextIrp = NULL;
DPRINT_QUEUE("USBPORT_PeekNextBadRequest: Irp - %p, PeekContext - %p\n", Irp, PeekContext);
FdoExtension = CONTAINING_RECORD(Csq, USBPORT_DEVICE_EXTENSION, BadRequestIoCsq);
ListHead = &FdoExtension->BadRequestList;
if (Irp) { NextEntry = Irp->Tail.Overlay.ListEntry.Flink; } else { NextEntry = ListHead->Flink; }
while (NextEntry != ListHead) { NextIrp = CONTAINING_RECORD(NextEntry, IRP, Tail.Overlay.ListEntry);
if (!PeekContext) break;
NextEntry = NextEntry->Flink; }
return NextIrp;}
VOIDNTAPIUSBPORT_AcquireBadRequestLock(IN PIO_CSQ Csq, IN PKIRQL Irql){ PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_QUEUE("USBPORT_AcquireBadRequestLock: ... \n");
FdoExtension = CONTAINING_RECORD(Csq, USBPORT_DEVICE_EXTENSION, BadRequestIoCsq);
KeAcquireSpinLock(&FdoExtension->BadRequestIoCsqSpinLock, Irql);}
VOIDNTAPIUSBPORT_ReleaseBadRequestLock(IN PIO_CSQ Csq, IN KIRQL Irql){ PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_QUEUE("USBPORT_ReleaseBadRequestLock: ... \n");
FdoExtension = CONTAINING_RECORD(Csq, USBPORT_DEVICE_EXTENSION, BadRequestIoCsq);
KeReleaseSpinLock(&FdoExtension->BadRequestIoCsqSpinLock, Irql);}
VOIDNTAPIUSBPORT_CompleteCanceledBadRequest(IN PIO_CSQ Csq, IN PIRP Irp){ PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_QUEUE("USBPORT_CompleteCanceledBadRequest: Irp - %p\n", Irp);
FdoExtension = CONTAINING_RECORD(Csq, USBPORT_DEVICE_EXTENSION, BadRequestIoCsq);
InterlockedDecrement(&FdoExtension->BadRequestLockCounter);
Irp->IoStatus.Status = STATUS_CANCELLED; Irp->IoStatus.Information = 0; IoCompleteRequest(Irp, IO_NO_INCREMENT);}
VOIDNTAPIUSBPORT_InsertIrpInTable(IN PUSBPORT_IRP_TABLE IrpTable, IN PIRP Irp){ ULONG ix;
DPRINT_CORE("USBPORT_InsertIrpInTable: IrpTable - %p, Irp - %p\n", IrpTable, Irp);
ASSERT(IrpTable != NULL);
while (TRUE) { for (ix = 0; ix < 0x200; ix++) { if (IrpTable->irp[ix] == NULL) { IrpTable->irp[ix] = Irp;
if (ix > 0) { DPRINT_CORE("USBPORT_InsertIrpInTable: ix - %x\n", ix); }
return; } }
if (ix != 0x200) { KeBugCheckEx(BUGCODE_USB_DRIVER, 1, 0, 0, 0); }
IrpTable->LinkNextTable = ExAllocatePoolWithTag(NonPagedPool, sizeof(USBPORT_IRP_TABLE), USB_PORT_TAG);
if (IrpTable->LinkNextTable == NULL) { KeBugCheckEx(BUGCODE_USB_DRIVER, 1, 0, 0, 0); }
RtlZeroMemory(IrpTable->LinkNextTable, sizeof(USBPORT_IRP_TABLE));
IrpTable = IrpTable->LinkNextTable; }}
PIRPNTAPIUSBPORT_RemoveIrpFromTable(IN PUSBPORT_IRP_TABLE IrpTable, IN PIRP Irp){ ULONG ix;
DPRINT_CORE("USBPORT_RemoveIrpFromTable: IrpTable - %p, Irp - %p\n", IrpTable, Irp);
ASSERT(IrpTable != NULL);
while (TRUE) { for (ix = 0; ix < 0x200; ix++) { if (IrpTable->irp[ix] == Irp) { IrpTable->irp[ix] = NULL;
if (ix > 0) { DPRINT_CORE("USBPORT_RemoveIrpFromTable: ix - %x\n", ix); }
return Irp; } }
if (IrpTable->LinkNextTable == NULL) break;
IrpTable = IrpTable->LinkNextTable; continue; }
DPRINT1("USBPORT_RemoveIrpFromTable: return NULL. ix - %x\n", ix); return NULL;}
PIRPNTAPIUSBPORT_RemoveActiveTransferIrp(IN PDEVICE_OBJECT FdoDevice, IN PIRP Irp){ PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_CORE("USBPORT_RemoveActiveTransferIrp: Irp - %p\n", Irp); FdoExtension = FdoDevice->DeviceExtension; return USBPORT_RemoveIrpFromTable(FdoExtension->ActiveIrpTable, Irp);}
PIRPNTAPIUSBPORT_RemovePendingTransferIrp(IN PDEVICE_OBJECT FdoDevice, IN PIRP Irp){ PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_CORE("USBPORT_RemovePendingTransferIrp: Irp - %p\n", Irp); FdoExtension = FdoDevice->DeviceExtension; return USBPORT_RemoveIrpFromTable(FdoExtension->PendingIrpTable, Irp);}
VOIDNTAPIUSBPORT_FindUrbInIrpTable(IN PUSBPORT_IRP_TABLE IrpTable, IN PURB Urb, IN PIRP Irp){ ULONG ix; PIRP irp; PURB urbIn;
DPRINT_CORE("USBPORT_FindUrbInIrpTable: IrpTable - %p, Urb - %p, Irp - %p\n", IrpTable, Urb, Irp);
ASSERT(IrpTable != NULL);
do { for (ix = 0; ix < 0x200; ix++) { irp = IrpTable->irp[ix];
if (irp) { urbIn = URB_FROM_IRP(irp);
if (urbIn == Urb) { if (irp == Irp) { KeBugCheckEx(BUGCODE_USB_DRIVER, 4, (ULONG_PTR)irp, (ULONG_PTR)urbIn, 0); }
KeBugCheckEx(BUGCODE_USB_DRIVER, 2, (ULONG_PTR)irp, (ULONG_PTR)Irp, (ULONG_PTR)urbIn); } } }
IrpTable = IrpTable->LinkNextTable; } while (IrpTable);}
PIRPNTAPIUSBPORT_FindIrpInTable(IN PUSBPORT_IRP_TABLE IrpTable, IN PIRP Irp){ ULONG ix; PIRP irp;
DPRINT_CORE("USBPORT_FindIrpInTable: IrpTable - %p, Irp - %p\n", IrpTable, Irp);
ASSERT(IrpTable != NULL);
do { for (ix = 0; ix < 0x200; ix++) { irp = IrpTable->irp[ix];
if (irp && irp == Irp) { return irp; } }
IrpTable = IrpTable->LinkNextTable; } while (IrpTable->LinkNextTable);
DPRINT_CORE("USBPORT_FindIrpInTable: Not found!!!\n"); return NULL;}
PIRPNTAPIUSBPORT_FindActiveTransferIrp(IN PDEVICE_OBJECT FdoDevice, IN PIRP Irp){ PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_CORE("USBPORT_FindActiveTransferIrp: Irp - %p\n", Irp); FdoExtension = FdoDevice->DeviceExtension; return USBPORT_FindIrpInTable(FdoExtension->ActiveIrpTable, Irp);}
VOIDNTAPIUSBPORT_CancelPendingTransferIrp(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp){ PURB Urb; PUSBPORT_TRANSFER Transfer; PUSBPORT_ENDPOINT Endpoint; PDEVICE_OBJECT FdoDevice; PUSBPORT_DEVICE_EXTENSION FdoExtension; KIRQL OldIrql; PIRP irp;
DPRINT_CORE("USBPORT_CancelPendingTransferIrp: DeviceObject - %p, Irp - %p\n", DeviceObject, Irp);
Urb = URB_FROM_IRP(Irp); Transfer = Urb->UrbControlTransfer.hca.Reserved8[0]; Endpoint = Transfer->Endpoint;
FdoDevice = Endpoint->FdoDevice; FdoExtension = DeviceObject->DeviceExtension;
IoReleaseCancelSpinLock(Irp->CancelIrql);
KeAcquireSpinLock(&FdoExtension->FlushPendingTransferSpinLock, &OldIrql);
irp = USBPORT_RemovePendingTransferIrp(FdoDevice, Irp);
if (!irp) { KeReleaseSpinLock(&FdoExtension->FlushPendingTransferSpinLock, OldIrql); return; }
KeAcquireSpinLockAtDpcLevel(&Endpoint->EndpointSpinLock);
RemoveEntryList(&Transfer->TransferLink);
Transfer->TransferLink.Flink = NULL; Transfer->TransferLink.Blink = NULL;
KeReleaseSpinLockFromDpcLevel(&Endpoint->EndpointSpinLock); KeReleaseSpinLock(&FdoExtension->FlushPendingTransferSpinLock, OldIrql);
USBPORT_CompleteTransfer(Transfer->Urb, USBD_STATUS_CANCELED);}
VOIDNTAPIUSBPORT_CancelActiveTransferIrp(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp){ PUSBPORT_RHDEVICE_EXTENSION PdoExtension; PDEVICE_OBJECT FdoDevice; PUSBPORT_DEVICE_EXTENSION FdoExtension; PURB Urb; PUSBPORT_TRANSFER Transfer; PUSBPORT_ENDPOINT Endpoint; PIRP irp; PUSBPORT_TRANSFER SplitTransfer; PLIST_ENTRY Entry; KIRQL OldIrql;
DPRINT_CORE("USBPORT_CancelActiveTransferIrp: Irp - %p\n", Irp);
PdoExtension = DeviceObject->DeviceExtension; FdoDevice = PdoExtension->FdoDevice; FdoExtension = FdoDevice->DeviceExtension;
IoReleaseCancelSpinLock(Irp->CancelIrql);
KeAcquireSpinLock(&FdoExtension->FlushTransferSpinLock, &OldIrql);
irp = USBPORT_FindActiveTransferIrp(FdoDevice, Irp);
if (!irp) { KeReleaseSpinLock(&FdoExtension->FlushTransferSpinLock, OldIrql); return; }
Urb = URB_FROM_IRP(irp); Transfer = Urb->UrbControlTransfer.hca.Reserved8[0]; Endpoint = Transfer->Endpoint;
DPRINT_CORE("USBPORT_CancelActiveTransferIrp: irp - %p, Urb - %p, Transfer - %p\n", irp, Urb, Transfer);
KeAcquireSpinLockAtDpcLevel(&Endpoint->EndpointSpinLock);
Transfer->Flags |= TRANSFER_FLAG_CANCELED;
if (Transfer->Flags & TRANSFER_FLAG_PARENT) { KeAcquireSpinLockAtDpcLevel(&Transfer->TransferSpinLock);
Entry = Transfer->SplitTransfersList.Flink;
while (Entry && Entry != &Transfer->SplitTransfersList) { SplitTransfer = CONTAINING_RECORD(Entry, USBPORT_TRANSFER, SplitLink);
SplitTransfer->Flags |= TRANSFER_FLAG_CANCELED;
Entry = Entry->Flink; }
KeReleaseSpinLockFromDpcLevel(&Transfer->TransferSpinLock); }
KeReleaseSpinLockFromDpcLevel(&Endpoint->EndpointSpinLock); KeReleaseSpinLock(&FdoExtension->FlushTransferSpinLock, OldIrql);
USBPORT_InvalidateEndpointHandler(FdoDevice, Endpoint, INVALIDATE_ENDPOINT_WORKER_THREAD); return;}
VOIDNTAPIUSBPORT_FlushAbortList(IN PUSBPORT_ENDPOINT Endpoint){ PLIST_ENTRY Entry; PUSBPORT_TRANSFER Transfer; PLIST_ENTRY AbortList; LIST_ENTRY List; NTSTATUS Status; PIRP Irp; PURB Urb; PUSBPORT_DEVICE_HANDLE DeviceHandle = NULL;
DPRINT_CORE("USBPORT_FlushAbortList: Endpoint - %p\n", Endpoint);
InitializeListHead(&List);
KeAcquireSpinLock(&Endpoint->EndpointSpinLock, &Endpoint->EndpointOldIrql);
if (IsListEmpty(&Endpoint->AbortList)) { KeReleaseSpinLock(&Endpoint->EndpointSpinLock, Endpoint->EndpointOldIrql); return; }
Entry = Endpoint->PendingTransferList.Flink;
while (Entry && Entry != &Endpoint->PendingTransferList) { Transfer = CONTAINING_RECORD(Entry, USBPORT_TRANSFER, TransferLink);
if (Transfer->Flags & TRANSFER_FLAG_ABORTED) { DPRINT_CORE("USBPORT_FlushAbortList: Aborted PendingTransfer - %p\n", Transfer);
KeReleaseSpinLock(&Endpoint->EndpointSpinLock, Endpoint->EndpointOldIrql); return; }
Entry = Transfer->TransferLink.Flink; }
Entry = Endpoint->TransferList.Flink;
while (Entry && Entry != &Endpoint->TransferList) { Transfer = CONTAINING_RECORD(Entry, USBPORT_TRANSFER, TransferLink);
if (Transfer->Flags & TRANSFER_FLAG_ABORTED) { DPRINT_CORE("USBPORT_FlushAbortList: Aborted ActiveTransfer - %p\n", Transfer);
KeReleaseSpinLock(&Endpoint->EndpointSpinLock, Endpoint->EndpointOldIrql); return; }
Entry = Transfer->TransferLink.Flink; }
AbortList = &Endpoint->AbortList;
while (!IsListEmpty(AbortList)) { //DbgBreakPoint();
Irp = CONTAINING_RECORD(AbortList->Flink, IRP, Tail.Overlay.ListEntry);
RemoveHeadList(AbortList); InsertTailList(&List, &Irp->Tail.Overlay.ListEntry); }
KeReleaseSpinLock(&Endpoint->EndpointSpinLock, Endpoint->EndpointOldIrql);
while (!IsListEmpty(&List)) { //DbgBreakPoint();
Irp = CONTAINING_RECORD(List.Flink, IRP, Tail.Overlay.ListEntry);
RemoveHeadList(&List);
Urb = URB_FROM_IRP(Irp);
DeviceHandle = Urb->UrbHeader.UsbdDeviceHandle; InterlockedDecrement(&DeviceHandle->DeviceHandleLock);
Status = USBPORT_USBDStatusToNtStatus(Urb, USBD_STATUS_SUCCESS);
DPRINT_CORE("USBPORT_FlushAbortList: complete Irp - %p\n", Irp);
Irp->IoStatus.Status = Status; Irp->IoStatus.Information = 0; IoCompleteRequest(Irp, IO_NO_INCREMENT); }}
VOIDNTAPIUSBPORT_FlushCancelList(IN PUSBPORT_ENDPOINT Endpoint){ PDEVICE_OBJECT FdoDevice; PUSBPORT_DEVICE_EXTENSION FdoExtension; PUSBPORT_TRANSFER Transfer; PIRP Irp; KIRQL OldIrql; KIRQL PrevIrql;
DPRINT_CORE("USBPORT_FlushCancelList: ... \n");
FdoDevice = Endpoint->FdoDevice; FdoExtension = FdoDevice->DeviceExtension;
KeAcquireSpinLock(&FdoExtension->FlushTransferSpinLock, &OldIrql); KeAcquireSpinLockAtDpcLevel(&Endpoint->EndpointSpinLock);
while (!IsListEmpty(&Endpoint->CancelList)) { Transfer = CONTAINING_RECORD(Endpoint->CancelList.Flink, USBPORT_TRANSFER, TransferLink);
RemoveHeadList(&Endpoint->CancelList);
Irp = Transfer->Irp;
if (Irp) { DPRINT("USBPORT_FlushCancelList: Irp - %p\n", Irp);
IoAcquireCancelSpinLock(&PrevIrql); IoSetCancelRoutine(Irp, NULL); IoReleaseCancelSpinLock(PrevIrql);
USBPORT_RemoveActiveTransferIrp(FdoDevice, Irp); }
KeReleaseSpinLockFromDpcLevel(&Endpoint->EndpointSpinLock); KeReleaseSpinLock(&FdoExtension->FlushTransferSpinLock, OldIrql);
if (Endpoint->Flags & ENDPOINT_FLAG_NUKE) { USBPORT_CompleteTransfer(Transfer->Urb, USBD_STATUS_DEVICE_GONE); } else { if (Transfer->Flags & TRANSFER_FLAG_DEVICE_GONE) { USBPORT_CompleteTransfer(Transfer->Urb, USBD_STATUS_DEVICE_GONE); } else { USBPORT_CompleteTransfer(Transfer->Urb, USBD_STATUS_CANCELED); } }
KeAcquireSpinLock(&FdoExtension->FlushTransferSpinLock, &OldIrql); KeAcquireSpinLockAtDpcLevel(&Endpoint->EndpointSpinLock); }
KeReleaseSpinLockFromDpcLevel(&Endpoint->EndpointSpinLock); KeReleaseSpinLock(&FdoExtension->FlushTransferSpinLock, OldIrql);
USBPORT_FlushAbortList(Endpoint);}
VOIDNTAPIUSBPORT_FlushPendingTransfers(IN PUSBPORT_ENDPOINT Endpoint){ PDEVICE_OBJECT FdoDevice; PUSBPORT_DEVICE_EXTENSION FdoExtension; BOOLEAN IsMapTransfer; BOOLEAN IsEnd = FALSE; PLIST_ENTRY List; PUSBPORT_TRANSFER Transfer; PURB Urb; PIRP Irp; KIRQL OldIrql; BOOLEAN Result;
DPRINT_CORE("USBPORT_FlushPendingTransfers: Endpoint - %p\n", Endpoint);
FdoDevice = Endpoint->FdoDevice; FdoExtension = FdoDevice->DeviceExtension;
if (InterlockedCompareExchange(&Endpoint->FlushPendingLock, 1, 0)) { DPRINT_CORE("USBPORT_FlushPendingTransfers: Endpoint Locked \n"); return; }
while (TRUE) { IsMapTransfer = 0;
KeAcquireSpinLock(&FdoExtension->FlushPendingTransferSpinLock, &OldIrql);
KeAcquireSpinLockAtDpcLevel(&Endpoint->EndpointSpinLock);
if (FdoExtension->Flags & USBPORT_FLAG_HC_SUSPEND) { IsEnd = TRUE;
KeReleaseSpinLockFromDpcLevel(&Endpoint->EndpointSpinLock); KeReleaseSpinLock(&FdoExtension->FlushPendingTransferSpinLock, OldIrql); goto Next; }
if (!(Endpoint->Flags & ENDPOINT_FLAG_ROOTHUB_EP0)) { if (!IsListEmpty(&Endpoint->TransferList)) { List = Endpoint->TransferList.Flink;
while (List && List != &Endpoint->TransferList) { Transfer = CONTAINING_RECORD(List, USBPORT_TRANSFER, TransferLink);
if (!(Transfer->Flags & TRANSFER_FLAG_SUBMITED)) { KeReleaseSpinLockFromDpcLevel(&Endpoint->EndpointSpinLock); KeReleaseSpinLock(&FdoExtension->FlushPendingTransferSpinLock, OldIrql);
IsEnd = TRUE; goto Worker; }
List = Transfer->TransferLink.Flink; } } }
List = Endpoint->PendingTransferList.Flink;
if (List == NULL || IsListEmpty(&Endpoint->PendingTransferList)) { KeReleaseSpinLockFromDpcLevel(&Endpoint->EndpointSpinLock); KeReleaseSpinLock(&FdoExtension->FlushPendingTransferSpinLock, OldIrql);
IsEnd = TRUE; goto Worker; }
Transfer = CONTAINING_RECORD(List, USBPORT_TRANSFER, TransferLink);
if (Transfer->Irp) { DPRINT_CORE("USBPORT_FlushPendingTransfers: Transfer->Irp->CancelRoutine - %p\n", Transfer->Irp->CancelRoutine); }
if (Transfer->Irp && (IoSetCancelRoutine(Transfer->Irp, NULL) == NULL)) { DPRINT_CORE("USBPORT_FlushPendingTransfers: Transfer->Irp - %p\n", Transfer->Irp);
Transfer = NULL; IsEnd = TRUE; }
if (!Transfer) { KeReleaseSpinLockFromDpcLevel(&Endpoint->EndpointSpinLock); KeReleaseSpinLock(&FdoExtension->FlushPendingTransferSpinLock, OldIrql);
if (IsMapTransfer) { USBPORT_FlushMapTransfers(FdoDevice); goto Next; }
goto Worker; }
Irp = Transfer->Irp; Urb = Transfer->Urb;
RemoveEntryList(&Transfer->TransferLink); Transfer->TransferLink.Flink = NULL; Transfer->TransferLink.Blink = NULL;
if (Irp) { Irp = USBPORT_RemovePendingTransferIrp(FdoDevice, Irp); }
KeReleaseSpinLockFromDpcLevel(&Endpoint->EndpointSpinLock); KeReleaseSpinLock(&FdoExtension->FlushPendingTransferSpinLock, OldIrql);
KeAcquireSpinLock(&FdoExtension->FlushTransferSpinLock, &OldIrql);
if (Irp) { IoSetCancelRoutine(Irp, USBPORT_CancelActiveTransferIrp);
if (Irp->Cancel && IoSetCancelRoutine(Irp, NULL)) { DPRINT_CORE("USBPORT_FlushPendingTransfers: irp - %p\n", Irp);
KeReleaseSpinLock(&FdoExtension->FlushTransferSpinLock, OldIrql);
USBPORT_CompleteTransfer(Transfer->Urb, USBD_STATUS_CANCELED); goto Worker; }
USBPORT_FindUrbInIrpTable(FdoExtension->ActiveIrpTable, Urb, Irp); USBPORT_InsertIrpInTable(FdoExtension->ActiveIrpTable, Irp); }
IsMapTransfer = USBPORT_QueueActiveUrbToEndpoint(Endpoint, Urb);
KeReleaseSpinLock(&FdoExtension->FlushTransferSpinLock, OldIrql);
if (IsMapTransfer) { USBPORT_FlushMapTransfers(FdoDevice); goto Next; }
Worker: KeRaiseIrql(DISPATCH_LEVEL, &OldIrql); Result = USBPORT_EndpointWorker(Endpoint, FALSE); KeLowerIrql(OldIrql);
if (Result) USBPORT_InvalidateEndpointHandler(FdoDevice, Endpoint, INVALIDATE_ENDPOINT_WORKER_THREAD);
Next: if (IsEnd) { InterlockedDecrement(&Endpoint->FlushPendingLock); DPRINT_CORE("USBPORT_FlushPendingTransfers: Endpoint Unlocked. Exit\n"); return; } }}
VOIDNTAPIUSBPORT_QueuePendingUrbToEndpoint(IN PUSBPORT_ENDPOINT Endpoint, IN PURB Urb){ PUSBPORT_TRANSFER Transfer;
DPRINT_CORE("USBPORT_QueuePendingUrbToEndpoint: Endpoint - %p, Urb - %p\n", Endpoint, Urb);
Transfer = Urb->UrbControlTransfer.hca.Reserved8[0]; //FIXME USBPORT_ResetEndpointIdle(); InsertTailList(&Endpoint->PendingTransferList, &Transfer->TransferLink); Urb->UrbHeader.Status = USBD_STATUS_PENDING;}
BOOLEANNTAPIUSBPORT_QueueActiveUrbToEndpoint(IN PUSBPORT_ENDPOINT Endpoint, IN PURB Urb){ PUSBPORT_TRANSFER Transfer; PDEVICE_OBJECT FdoDevice; PUSBPORT_DEVICE_EXTENSION FdoExtension; PUSBPORT_DEVICE_HANDLE DeviceHandle; KIRQL OldIrql;
DPRINT_CORE("USBPORT_QueueActiveUrbToEndpoint: Endpoint - %p, Urb - %p\n", Endpoint, Urb);
Transfer = Urb->UrbControlTransfer.hca.Reserved8[0]; FdoDevice = Endpoint->FdoDevice; FdoExtension = FdoDevice->DeviceExtension;
KeAcquireSpinLock(&Endpoint->EndpointSpinLock, &Endpoint->EndpointOldIrql);
if ((Endpoint->Flags & ENDPOINT_FLAG_NUKE) || (Transfer->Flags & TRANSFER_FLAG_ABORTED)) { InsertTailList(&Endpoint->CancelList, &Transfer->TransferLink);
KeReleaseSpinLock(&Endpoint->EndpointSpinLock, Endpoint->EndpointOldIrql);
//DPRINT_CORE("USBPORT_QueueActiveUrbToEndpoint: return FALSE\n"); return FALSE; }
if (Transfer->TransferParameters.TransferBufferLength == 0 || !(Endpoint->Flags & ENDPOINT_FLAG_DMA_TYPE)) { InsertTailList(&Endpoint->TransferList, &Transfer->TransferLink);
KeReleaseSpinLock(&Endpoint->EndpointSpinLock, Endpoint->EndpointOldIrql);
//DPRINT_CORE("USBPORT_QueueActiveUrbToEndpoint: return FALSE\n"); return FALSE; }
KeReleaseSpinLock(&Endpoint->EndpointSpinLock, Endpoint->EndpointOldIrql);
KeAcquireSpinLock(&FdoExtension->MapTransferSpinLock, &OldIrql);
InsertTailList(&FdoExtension->MapTransferList, &Transfer->TransferLink);
DeviceHandle = Transfer->Urb->UrbHeader.UsbdDeviceHandle; InterlockedIncrement(&DeviceHandle->DeviceHandleLock);
KeReleaseSpinLock(&FdoExtension->MapTransferSpinLock, OldIrql);
//DPRINT_CORE("USBPORT_QueueActiveUrbToEndpoint: return TRUE\n"); return TRUE;}
VOIDNTAPIUSBPORT_QueuePendingTransferIrp(IN PIRP Irp){ PURB Urb; PUSBPORT_TRANSFER Transfer; PUSBPORT_ENDPOINT Endpoint; PDEVICE_OBJECT FdoDevice; PUSBPORT_DEVICE_EXTENSION FdoExtension;
DPRINT_CORE("USBPORT_QueuePendingTransferIrp: Irp - %p\n", Irp);
Urb = URB_FROM_IRP(Irp);
Transfer = Urb->UrbControlTransfer.hca.Reserved8[0]; Endpoint = Transfer->Endpoint;
FdoDevice = Endpoint->FdoDevice; FdoExtension = FdoDevice->DeviceExtension;
Irp->IoStatus.Status = STATUS_PENDING; IoMarkIrpPending(Irp);
IoSetCancelRoutine(Irp, USBPORT_CancelPendingTransferIrp);
if (Irp->Cancel && IoSetCancelRoutine(Irp, NULL)) { USBPORT_CompleteTransfer(Urb, USBD_STATUS_CANCELED); } else { USBPORT_InsertIrpInTable(FdoExtension->PendingIrpTable, Irp); USBPORT_QueuePendingUrbToEndpoint(Endpoint, Urb); }}
VOIDNTAPIUSBPORT_QueueTransferUrb(IN PURB Urb){ PUSBPORT_TRANSFER Transfer; PUSBPORT_ENDPOINT Endpoint; PIRP Irp; PUSBPORT_DEVICE_HANDLE DeviceHandle; PUSBPORT_TRANSFER_PARAMETERS Parameters;
DPRINT_CORE("USBPORT_QueueTransferUrb: Urb - %p\n", Urb);
if (Urb->UrbControlTransfer.TransferFlags & USBD_DEFAULT_PIPE_TRANSFER) Urb->UrbHeader.Function = URB_FUNCTION_CONTROL_TRANSFER;
Transfer = Urb->UrbControlTransfer.hca.Reserved8[0]; Parameters = &Transfer->TransferParameters;
Endpoint = Transfer->Endpoint; Endpoint->Flags &= ~ENDPOINT_FLAG_QUEUENE_EMPTY;
Parameters->TransferBufferLength = Urb->UrbControlTransfer.TransferBufferLength; Parameters->TransferFlags = Urb->UrbControlTransfer.TransferFlags;
Transfer->TransferBufferMDL = Urb->UrbControlTransfer.TransferBufferMDL;
if (Urb->UrbControlTransfer.TransferFlags & USBD_TRANSFER_DIRECTION_IN) { Transfer->Direction = USBPORT_DMA_DIRECTION_FROM_DEVICE; } else { Transfer->Direction = USBPORT_DMA_DIRECTION_TO_DEVICE; }
if (Endpoint->EndpointProperties.TransferType == USBPORT_TRANSFER_TYPE_CONTROL) { RtlCopyMemory(&Parameters->SetupPacket, Urb->UrbControlTransfer.SetupPacket, sizeof(USB_DEFAULT_PIPE_SETUP_PACKET)); }
DPRINT_URB("... URB TransferBufferLength - %x\n", Urb->UrbControlTransfer.TransferBufferLength);
Urb->UrbControlTransfer.TransferBufferLength = 0;
Irp = Transfer->Irp;
if (Irp) { USBPORT_QueuePendingTransferIrp(Irp); } else { USBPORT_QueuePendingUrbToEndpoint(Endpoint, Urb); }
DeviceHandle = Urb->UrbHeader.UsbdDeviceHandle; InterlockedDecrement(&DeviceHandle->DeviceHandleLock);
USBPORT_FlushPendingTransfers(Endpoint);
DPRINT_URB("... URB TransferBufferLength - %x\n", Urb->UrbControlTransfer.TransferBufferLength);
if (Urb->UrbControlTransfer.TransferBufferLength) { PULONG Buffer; ULONG BufferLength; ULONG_PTR BufferEnd; ULONG ix;
Buffer = Urb->UrbControlTransfer.TransferBuffer; BufferLength = Urb->UrbControlTransfer.TransferBufferLength; BufferEnd = (ULONG_PTR)Buffer + BufferLength;
DPRINT_URB("URB TransferBuffer - %p\n", Buffer);
for (ix = 0; (ULONG_PTR)(Buffer + ix) < BufferEnd; ix++) { DPRINT_URB("Buffer[%02X] - %p\n", ix, Buffer[ix]); } }}
VOIDNTAPIUSBPORT_FlushAllEndpoints(IN PDEVICE_OBJECT FdoDevice){ PUSBPORT_DEVICE_EXTENSION FdoExtension; PLIST_ENTRY Entry; PUSBPORT_ENDPOINT Endpoint; LIST_ENTRY List; KIRQL OldIrql;
DPRINT_CORE("USBPORT_FlushAllEndpoints: ... \n");
FdoExtension = FdoDevice->DeviceExtension;
KeAcquireSpinLock(&FdoExtension->EndpointListSpinLock, &OldIrql);
InitializeListHead(&List);
Entry = FdoExtension->EndpointList.Flink;
while (Entry && Entry != &FdoExtension->EndpointList) { Endpoint = CONTAINING_RECORD(Entry, USBPORT_ENDPOINT, EndpointLink);
if (USBPORT_GetEndpointState(Endpoint) != USBPORT_ENDPOINT_CLOSED) { InsertTailList(&List, &Endpoint->FlushLink); }
Entry = Endpoint->EndpointLink.Flink; }
KeReleaseSpinLock(&FdoExtension->EndpointListSpinLock, OldIrql);
while (!IsListEmpty(&List)) { Endpoint = CONTAINING_RECORD(List.Flink, USBPORT_ENDPOINT, FlushLink);
RemoveHeadList(&List);
Endpoint->FlushLink.Flink = NULL; Endpoint->FlushLink.Blink = NULL;
if (!IsListEmpty(&Endpoint->PendingTransferList)) { USBPORT_FlushPendingTransfers(Endpoint); } }
DPRINT_CORE("USBPORT_FlushAllEndpoints: exit\n");}
ULONGNTAPIUSBPORT_KillEndpointActiveTransfers(IN PDEVICE_OBJECT FdoDevice, IN PUSBPORT_ENDPOINT Endpoint){ PLIST_ENTRY ActiveList; PUSBPORT_TRANSFER Transfer; ULONG KilledTransfers = 0;
DPRINT_CORE("USBPORT_KillEndpointActiveTransfers \n");
ActiveList = Endpoint->TransferList.Flink;
while (ActiveList && ActiveList != &Endpoint->TransferList) { ++KilledTransfers;
Transfer = CONTAINING_RECORD(ActiveList, USBPORT_TRANSFER, TransferLink);
Transfer->Flags |= TRANSFER_FLAG_ABORTED;
ActiveList = Transfer->TransferLink.Flink; }
USBPORT_FlushPendingTransfers(Endpoint); USBPORT_FlushCancelList(Endpoint);
return KilledTransfers;}
VOIDNTAPIUSBPORT_FlushController(IN PDEVICE_OBJECT FdoDevice){ PUSBPORT_DEVICE_EXTENSION FdoExtension; PLIST_ENTRY Entry; PUSBPORT_ENDPOINT Endpoint; ULONG KilledTransfers; PLIST_ENTRY EndpointList; KIRQL OldIrql; LIST_ENTRY FlushList;
DPRINT_CORE("USBPORT_FlushController \n");
FdoExtension = FdoDevice->DeviceExtension;
EndpointList = &FdoExtension->EndpointList;
while (TRUE) { KeAcquireSpinLock(&FdoExtension->EndpointListSpinLock, &OldIrql);
InitializeListHead(&FlushList);
Entry = EndpointList->Flink;
if (!IsListEmpty(EndpointList)) { while (Entry && Entry != EndpointList) { Endpoint = CONTAINING_RECORD(Entry, USBPORT_ENDPOINT, EndpointLink);
if (Endpoint->StateLast != USBPORT_ENDPOINT_REMOVE && Endpoint->StateLast != USBPORT_ENDPOINT_CLOSED) { InterlockedIncrement(&Endpoint->LockCounter); InsertTailList(&FlushList, &Endpoint->FlushControllerLink); }
Entry = Endpoint->EndpointLink.Flink; } }
KeReleaseSpinLock(&FdoExtension->EndpointListSpinLock, OldIrql);
KilledTransfers = 0; while (!IsListEmpty(&FlushList)) { Endpoint = CONTAINING_RECORD(FlushList.Flink, USBPORT_ENDPOINT, FlushControllerLink);
RemoveHeadList(&FlushList);
KilledTransfers += USBPORT_KillEndpointActiveTransfers(FdoDevice, Endpoint);
InterlockedDecrement(&Endpoint->LockCounter); }
if (!KilledTransfers) break;
USBPORT_Wait(FdoDevice, 100); }}
VOIDNTAPIUSBPORT_BadRequestFlush(IN PDEVICE_OBJECT FdoDevice){ PUSBPORT_DEVICE_EXTENSION FdoExtension; PIRP Irp;
DPRINT_QUEUE("USBPORT_BadRequestFlush: ... \n");
FdoExtension = FdoDevice->DeviceExtension;
while (TRUE) { Irp = IoCsqRemoveNextIrp(&FdoExtension->BadRequestIoCsq, 0);
if (!Irp) break;
DPRINT1("USBPORT_BadRequestFlush: Irp - %p\n", Irp);
Irp->IoStatus.Status = STATUS_DEVICE_NOT_CONNECTED; Irp->IoStatus.Information = 0; IoCompleteRequest(Irp, IO_NO_INCREMENT); }}
VOIDNTAPIUSBPORT_AbortEndpoint(IN PDEVICE_OBJECT FdoDevice, IN PUSBPORT_ENDPOINT Endpoint, IN PIRP Irp){ PLIST_ENTRY PendingList; PUSBPORT_TRANSFER PendingTransfer; PLIST_ENTRY ActiveList; PUSBPORT_TRANSFER ActiveTransfer;
DPRINT_CORE("USBPORT_AbortEndpoint: Irp - %p\n", Irp);
KeAcquireSpinLock(&Endpoint->EndpointSpinLock, &Endpoint->EndpointOldIrql);
if (Irp) { InsertTailList(&Endpoint->AbortList, &Irp->Tail.Overlay.ListEntry); }
PendingList = Endpoint->PendingTransferList.Flink;
while (PendingList && PendingList != &Endpoint->PendingTransferList) { PendingTransfer = CONTAINING_RECORD(PendingList, USBPORT_TRANSFER, TransferLink);
DPRINT_CORE("USBPORT_AbortEndpoint: Abort PendingTransfer - %p\n", PendingTransfer);
PendingTransfer->Flags |= TRANSFER_FLAG_ABORTED;
PendingList = PendingTransfer->TransferLink.Flink; }
ActiveList = Endpoint->TransferList.Flink;
while (ActiveList && ActiveList != &Endpoint->TransferList) { ActiveTransfer = CONTAINING_RECORD(ActiveList, USBPORT_TRANSFER, TransferLink);
DPRINT_CORE("USBPORT_AbortEndpoint: Abort ActiveTransfer - %p\n", ActiveTransfer);
ActiveTransfer->Flags |= TRANSFER_FLAG_ABORTED;
if (Endpoint->Flags & ENDPOINT_FLAG_ABORTING) { ActiveTransfer->Flags |= TRANSFER_FLAG_DEVICE_GONE; }
ActiveList = ActiveTransfer->TransferLink.Flink; }
KeReleaseSpinLock(&Endpoint->EndpointSpinLock, Endpoint->EndpointOldIrql);
USBPORT_InvalidateEndpointHandler(FdoDevice, Endpoint, INVALIDATE_ENDPOINT_INT_NEXT_SOF);
USBPORT_FlushPendingTransfers(Endpoint); USBPORT_FlushCancelList(Endpoint);}