6 #define NDEBUG_USBPORT_CORE
11 USBPORT_CalculateUsbBandwidth(IN PDEVICE_OBJECT FdoDevice
,
12 IN PUSBPORT_ENDPOINT Endpoint
)
14 PUSBPORT_ENDPOINT_PROPERTIES EndpointProperties
;
18 DPRINT("USBPORT_CalculateUsbBandwidth ... \n");
20 EndpointProperties
= &Endpoint
->EndpointProperties
;
22 switch (EndpointProperties
->TransferType
)
24 case USBPORT_TRANSFER_TYPE_ISOCHRONOUS
:
28 case USBPORT_TRANSFER_TYPE_INTERRUPT
:
32 default: //USBPORT_TRANSFER_TYPE_CONTROL or USBPORT_TRANSFER_TYPE_BULK
43 Bandwidth
= (EndpointProperties
->TotalMaxPacketSize
+ Additional
) * 8 * 7 / 6;
46 if (EndpointProperties
->DeviceSpeed
== UsbLowSpeed
)
56 USBPORT_AllocateBandwidth(IN PDEVICE_OBJECT FdoDevice
,
57 IN PUSBPORT_ENDPOINT Endpoint
)
59 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
60 PUSBPORT_ENDPOINT_PROPERTIES EndpointProperties
;
62 ULONG TotalBusBandwidth
;
63 ULONG EndpointBandwidth
;
66 DPRINT("USBPORT_AllocateBandwidth: ... \n");
68 FdoExtension
= FdoDevice
->DeviceExtension
;
69 EndpointProperties
= &Endpoint
->EndpointProperties
;
70 TransferType
= EndpointProperties
->TransferType
;
72 if (TransferType
== USBPORT_TRANSFER_TYPE_BULK
||
73 TransferType
== USBPORT_TRANSFER_TYPE_CONTROL
||
74 Endpoint
->Flags
& ENDPOINT_FLAG_ROOTHUB_EP0
)
76 EndpointProperties
->ScheduleOffset
= 0;
80 TotalBusBandwidth
= FdoExtension
->TotalBusBandwidth
;
81 EndpointBandwidth
= EndpointProperties
->UsbBandwidth
;
82 Period
= EndpointProperties
->Period
;
84 DPRINT1("USBPORT_AllocateBandwidth: FIXME. \n");
85 DPRINT1("USBPORT_AllocateBandwidth: Endpoint - %p, Type - %x, TotalBandwidth - %x, EpBandwidth - %x, Period - %x\n",
97 USBPORT_FreeBandwidth(IN PDEVICE_OBJECT FdoDevice
,
98 IN PUSBPORT_ENDPOINT Endpoint
)
100 DPRINT1("USBPORT_FreeBandwidth: UNIMPLEMENTED. FIXME. \n");
105 USBPORT_NormalizeHsInterval(UCHAR Interval
)
109 DPRINT("USBPORT_NormalizeHsInterval: Interval - %x\n", Interval
);
114 interval
= Interval
- 1;
119 return 1 << interval
;
124 USBPORT_EndpointHasQueuedTransfers(IN PDEVICE_OBJECT FdoDevice
,
125 IN PUSBPORT_ENDPOINT Endpoint
,
126 IN PULONG TransferCount
)
129 PUSBPORT_TRANSFER Transfer
;
130 BOOLEAN Result
= FALSE
;
132 DPRINT_CORE("USBPORT_EndpointHasQueuedTransfers: ... \n");
134 KeAcquireSpinLock(&Endpoint
->EndpointSpinLock
, &Endpoint
->EndpointOldIrql
);
136 if (!IsListEmpty(&Endpoint
->PendingTransferList
))
139 if (!IsListEmpty(&Endpoint
->TransferList
))
147 for (Entry
= Endpoint
->TransferList
.Flink
;
148 Entry
&& Entry
!= &Endpoint
->TransferList
;
149 Entry
= Transfer
->TransferLink
.Flink
)
151 Transfer
= CONTAINING_RECORD(Entry
,
155 if (Transfer
->Flags
& TRANSFER_FLAG_SUBMITED
)
163 KeReleaseSpinLock(&Endpoint
->EndpointSpinLock
, Endpoint
->EndpointOldIrql
);
170 USBPORT_NukeAllEndpoints(IN PDEVICE_OBJECT FdoDevice
)
172 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
173 PLIST_ENTRY EndpointList
;
174 PUSBPORT_ENDPOINT Endpoint
;
177 DPRINT("USBPORT_NukeAllEndpoints \n");
179 FdoExtension
= FdoDevice
->DeviceExtension
;
181 KeAcquireSpinLock(&FdoExtension
->EndpointListSpinLock
, &OldIrql
);
183 EndpointList
= FdoExtension
->EndpointList
.Flink
;
185 while (EndpointList
&& (EndpointList
!= &FdoExtension
->EndpointList
))
187 Endpoint
= CONTAINING_RECORD(EndpointList
,
191 if (!(Endpoint
->Flags
& ENDPOINT_FLAG_ROOTHUB_EP0
))
192 Endpoint
->Flags
|= ENDPOINT_FLAG_NUKE
;
194 EndpointList
= Endpoint
->EndpointLink
.Flink
;
197 KeReleaseSpinLock(&FdoExtension
->EndpointListSpinLock
, OldIrql
);
202 USBPORT_GetEndpointState(IN PUSBPORT_ENDPOINT Endpoint
)
206 //DPRINT("USBPORT_GetEndpointState \n");
208 KeAcquireSpinLockAtDpcLevel(&Endpoint
->StateChangeSpinLock
);
210 if (Endpoint
->StateLast
!= Endpoint
->StateNext
)
212 State
= USBPORT_ENDPOINT_UNKNOWN
;
216 State
= Endpoint
->StateLast
;
219 KeReleaseSpinLockFromDpcLevel(&Endpoint
->StateChangeSpinLock
);
221 if (State
!= USBPORT_ENDPOINT_ACTIVE
)
223 DPRINT("USBPORT_GetEndpointState: Endpoint - %p, State - %x\n",
233 USBPORT_SetEndpointState(IN PUSBPORT_ENDPOINT Endpoint
,
236 PDEVICE_OBJECT FdoDevice
;
237 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
238 PUSBPORT_REGISTRATION_PACKET Packet
;
241 DPRINT("USBPORT_SetEndpointState: Endpoint - %p, State - %x\n",
245 FdoDevice
= Endpoint
->FdoDevice
;
246 FdoExtension
= FdoDevice
->DeviceExtension
;
247 Packet
= &FdoExtension
->MiniPortInterface
->Packet
;
249 KeAcquireSpinLock(&Endpoint
->StateChangeSpinLock
,
250 &Endpoint
->EndpointStateOldIrql
);
252 if (!(Endpoint
->Flags
& ENDPOINT_FLAG_ROOTHUB_EP0
))
254 if (Endpoint
->Flags
& ENDPOINT_FLAG_NUKE
)
256 Endpoint
->StateLast
= State
;
257 Endpoint
->StateNext
= State
;
259 KeReleaseSpinLock(&Endpoint
->StateChangeSpinLock
,
260 Endpoint
->EndpointStateOldIrql
);
262 USBPORT_InvalidateEndpointHandler(FdoDevice
,
264 INVALIDATE_ENDPOINT_WORKER_THREAD
);
268 KeReleaseSpinLock(&Endpoint
->StateChangeSpinLock
,
269 Endpoint
->EndpointStateOldIrql
);
271 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &OldIrql
);
272 Packet
->SetEndpointState(FdoExtension
->MiniPortExt
,
275 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, OldIrql
);
277 Endpoint
->StateNext
= State
;
279 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &OldIrql
);
280 Endpoint
->FrameNumber
= Packet
->Get32BitFrameNumber(FdoExtension
->MiniPortExt
);
281 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, OldIrql
);
283 ExInterlockedInsertTailList(&FdoExtension
->EpStateChangeList
,
284 &Endpoint
->StateChangeLink
,
285 &FdoExtension
->EpStateChangeSpinLock
);
287 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &OldIrql
);
288 Packet
->InterruptNextSOF(FdoExtension
->MiniPortExt
);
289 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, OldIrql
);
293 Endpoint
->StateLast
= State
;
294 Endpoint
->StateNext
= State
;
296 if (State
== USBPORT_ENDPOINT_REMOVE
)
298 KeReleaseSpinLock(&Endpoint
->StateChangeSpinLock
,
299 Endpoint
->EndpointStateOldIrql
);
301 USBPORT_InvalidateEndpointHandler(FdoDevice
,
303 INVALIDATE_ENDPOINT_WORKER_THREAD
);
307 KeReleaseSpinLock(&Endpoint
->StateChangeSpinLock
,
308 Endpoint
->EndpointStateOldIrql
);
314 USBPORT_AddPipeHandle(IN PUSBPORT_DEVICE_HANDLE DeviceHandle
,
315 IN PUSBPORT_PIPE_HANDLE PipeHandle
)
317 DPRINT("USBPORT_AddPipeHandle: DeviceHandle - %p, PipeHandle - %p\n",
321 InsertTailList(&DeviceHandle
->PipeHandleList
, &PipeHandle
->PipeLink
);
326 USBPORT_RemovePipeHandle(IN PUSBPORT_DEVICE_HANDLE DeviceHandle
,
327 IN PUSBPORT_PIPE_HANDLE PipeHandle
)
329 DPRINT("USBPORT_RemovePipeHandle: PipeHandle - %p\n", PipeHandle
);
331 RemoveEntryList(&PipeHandle
->PipeLink
);
333 PipeHandle
->PipeLink
.Flink
= NULL
;
334 PipeHandle
->PipeLink
.Blink
= NULL
;
339 USBPORT_ValidatePipeHandle(IN PUSBPORT_DEVICE_HANDLE DeviceHandle
,
340 IN PUSBPORT_PIPE_HANDLE PipeHandle
)
342 PLIST_ENTRY HandleList
;
343 PUSBPORT_PIPE_HANDLE CurrentHandle
;
345 //DPRINT("USBPORT_ValidatePipeHandle: DeviceHandle - %p, PipeHandle - %p\n",
349 HandleList
= DeviceHandle
->PipeHandleList
.Flink
;
351 while (HandleList
!= &DeviceHandle
->PipeHandleList
)
353 CurrentHandle
= CONTAINING_RECORD(HandleList
,
357 HandleList
= HandleList
->Flink
;
359 if (CurrentHandle
== PipeHandle
)
368 USBPORT_DeleteEndpoint(IN PDEVICE_OBJECT FdoDevice
,
369 IN PUSBPORT_ENDPOINT Endpoint
)
371 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
375 DPRINT("USBPORT_DeleteEndpoint: Endpoint - %p\n", Endpoint
);
377 FdoExtension
= FdoDevice
->DeviceExtension
;
379 if ((Endpoint
->WorkerLink
.Flink
&& Endpoint
->WorkerLink
.Blink
) ||
380 Endpoint
->LockCounter
!= -1)
382 KeAcquireSpinLock(&FdoExtension
->EndpointListSpinLock
, &OldIrql
);
384 ExInterlockedInsertTailList(&FdoExtension
->EndpointClosedList
,
385 &Endpoint
->CloseLink
,
386 &FdoExtension
->EndpointClosedSpinLock
);
388 KeReleaseSpinLock(&FdoExtension
->EndpointListSpinLock
, OldIrql
);
394 KeAcquireSpinLock(&FdoExtension
->EndpointListSpinLock
, &OldIrql
);
396 RemoveEntryList(&Endpoint
->EndpointLink
);
397 Endpoint
->EndpointLink
.Flink
= NULL
;
398 Endpoint
->EndpointLink
.Blink
= NULL
;
400 KeReleaseSpinLock(&FdoExtension
->EndpointListSpinLock
, OldIrql
);
402 MiniportCloseEndpoint(FdoDevice
, Endpoint
);
404 if (Endpoint
->HeaderBuffer
)
406 USBPORT_FreeCommonBuffer(FdoDevice
, Endpoint
->HeaderBuffer
);
409 ExFreePoolWithTag(Endpoint
, USB_PORT_TAG
);
419 MiniportCloseEndpoint(IN PDEVICE_OBJECT FdoDevice
,
420 IN PUSBPORT_ENDPOINT Endpoint
)
422 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
423 PUSBPORT_REGISTRATION_PACKET Packet
;
424 BOOLEAN IsDoDisablePeriodic
;
428 DPRINT("MiniportCloseEndpoint: Endpoint - %p\n", Endpoint
);
430 FdoExtension
= FdoDevice
->DeviceExtension
;
431 Packet
= &FdoExtension
->MiniPortInterface
->Packet
;
433 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &OldIrql
);
435 if (Endpoint
->Flags
& ENDPOINT_FLAG_OPENED
)
437 TransferType
= Endpoint
->EndpointProperties
.TransferType
;
439 if (TransferType
== USBPORT_TRANSFER_TYPE_INTERRUPT
||
440 TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
)
442 --FdoExtension
->PeriodicEndpoints
;
445 IsDoDisablePeriodic
= FdoExtension
->PeriodicEndpoints
== 0;
447 Packet
->CloseEndpoint(FdoExtension
->MiniPortExt
,
449 IsDoDisablePeriodic
);
451 Endpoint
->Flags
&= ~ENDPOINT_FLAG_OPENED
;
452 Endpoint
->Flags
|= ENDPOINT_FLAG_CLOSED
;
455 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, OldIrql
);
460 USBPORT_ClosePipe(IN PUSBPORT_DEVICE_HANDLE DeviceHandle
,
461 IN PDEVICE_OBJECT FdoDevice
,
462 IN PUSBPORT_PIPE_HANDLE PipeHandle
)
464 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
465 PUSBPORT_RHDEVICE_EXTENSION PdoExtension
;
466 PUSBPORT_ENDPOINT Endpoint
;
470 DPRINT("USBPORT_ClosePipe \n");
472 FdoExtension
= FdoDevice
->DeviceExtension
;
474 if (PipeHandle
->Flags
& PIPE_HANDLE_FLAG_CLOSED
)
477 USBPORT_RemovePipeHandle(DeviceHandle
, PipeHandle
);
479 PipeHandle
->Flags
|= PIPE_HANDLE_FLAG_CLOSED
;
481 if (PipeHandle
->Flags
& PIPE_HANDLE_FLAG_NULL_PACKET_SIZE
)
483 PipeHandle
->Flags
&= ~PIPE_HANDLE_FLAG_NULL_PACKET_SIZE
;
487 Endpoint
= PipeHandle
->Endpoint
;
488 DPRINT("USBPORT_ClosePipe: Endpoint - %p\n", Endpoint
);
490 KeAcquireSpinLock(&FdoExtension
->EndpointListSpinLock
, &OldIrql
);
492 if ((Endpoint
->Flags
& ENDPOINT_FLAG_ROOTHUB_EP0
) &&
493 (Endpoint
->EndpointProperties
.TransferType
== USBPORT_TRANSFER_TYPE_INTERRUPT
))
495 PdoExtension
= FdoExtension
->RootHubPdo
->DeviceExtension
;
496 PdoExtension
->Endpoint
= NULL
;
499 KeReleaseSpinLock(&FdoExtension
->EndpointListSpinLock
, OldIrql
);
505 KeAcquireSpinLock(&Endpoint
->EndpointSpinLock
,
506 &Endpoint
->EndpointOldIrql
);
508 if (!IsListEmpty(&Endpoint
->PendingTransferList
))
511 if (!IsListEmpty(&Endpoint
->TransferList
))
514 if (!IsListEmpty(&Endpoint
->CancelList
))
517 if (!IsListEmpty(&Endpoint
->AbortList
))
520 KeAcquireSpinLockAtDpcLevel(&Endpoint
->StateChangeSpinLock
);
521 if (Endpoint
->StateLast
!= Endpoint
->StateNext
)
523 KeReleaseSpinLockFromDpcLevel(&Endpoint
->StateChangeSpinLock
);
525 KeReleaseSpinLock(&Endpoint
->EndpointSpinLock
,
526 Endpoint
->EndpointOldIrql
);
528 if (InterlockedIncrement(&Endpoint
->LockCounter
))
530 InterlockedDecrement(&Endpoint
->LockCounter
);
535 USBPORT_Wait(FdoDevice
, 1);
538 Endpoint
->DeviceHandle
= NULL
;
540 if (FdoExtension
->MiniPortInterface
->Packet
.MiniPortFlags
& USB_MINIPORT_FLAGS_USB2
)
542 DPRINT("USBPORT_ClosePipe: FIXME USBPORT_FreeBandwidthUSB20\n");
543 //USBPORT_FreeBandwidthUSB20();
547 DPRINT("USBPORT_ClosePipe: FIXME USBPORT_FreeBandwidthUSB11\n");
548 //USBPORT_FreeBandwidthUSB11();
551 KeAcquireSpinLock(&Endpoint
->EndpointSpinLock
, &Endpoint
->EndpointOldIrql
);
552 USBPORT_SetEndpointState(Endpoint
, USBPORT_ENDPOINT_REMOVE
);
553 KeReleaseSpinLock(&Endpoint
->EndpointSpinLock
, Endpoint
->EndpointOldIrql
);
555 USBPORT_SignalWorkerThread(FdoDevice
);
560 MiniportOpenEndpoint(IN PDEVICE_OBJECT FdoDevice
,
561 IN PUSBPORT_ENDPOINT Endpoint
)
563 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
564 PUSBPORT_REGISTRATION_PACKET Packet
;
569 DPRINT("MiniportOpenEndpoint: Endpoint - %p\n", Endpoint
);
571 FdoExtension
= FdoDevice
->DeviceExtension
;
572 Packet
= &FdoExtension
->MiniPortInterface
->Packet
;
574 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &OldIrql
);
576 Endpoint
->Flags
&= ~ENDPOINT_FLAG_CLOSED
;
578 MpStatus
= Packet
->OpenEndpoint(FdoExtension
->MiniPortExt
,
579 &Endpoint
->EndpointProperties
,
584 TransferType
= Endpoint
->EndpointProperties
.TransferType
;
586 if (TransferType
== USBPORT_TRANSFER_TYPE_INTERRUPT
||
587 TransferType
== USBPORT_TRANSFER_TYPE_ISOCHRONOUS
)
589 ++FdoExtension
->PeriodicEndpoints
;
592 Endpoint
->Flags
|= ENDPOINT_FLAG_OPENED
;
595 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, OldIrql
);
601 USBPORT_OpenPipe(IN PDEVICE_OBJECT FdoDevice
,
602 IN PUSBPORT_DEVICE_HANDLE DeviceHandle
,
603 IN PUSBPORT_PIPE_HANDLE PipeHandle
,
604 IN OUT PUSBD_STATUS UsbdStatus
)
606 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
607 PUSBPORT_RHDEVICE_EXTENSION PdoExtension
;
608 PUSBPORT_REGISTRATION_PACKET Packet
;
610 PUSBPORT_ENDPOINT Endpoint
;
611 PUSBPORT_ENDPOINT_PROPERTIES EndpointProperties
;
612 PUSB_ENDPOINT_DESCRIPTOR EndpointDescriptor
;
616 ULONG TransferParams
[2] = {0};
617 PUSBPORT_COMMON_BUFFER_HEADER HeaderBuffer
;
619 USBD_STATUS USBDStatus
;
622 USHORT MaxPacketSize
;
623 USHORT AdditionalTransaction
;
624 BOOLEAN IsAllocatedBandwidth
;
626 DPRINT("USBPORT_OpenPipe: DeviceHandle - %p, FdoDevice - %p, PipeHandle - %p\n",
631 FdoExtension
= FdoDevice
->DeviceExtension
;
632 Packet
= &FdoExtension
->MiniPortInterface
->Packet
;
634 EndpointSize
= sizeof(USBPORT_ENDPOINT
) + Packet
->MiniPortEndpointSize
;
636 if (Packet
->MiniPortFlags
& USB_MINIPORT_FLAGS_USB2
)
638 DPRINT1("USBPORT_OpenPipe: FIXME USB2 EndpointSize\n");
641 if (PipeHandle
->EndpointDescriptor
.wMaxPacketSize
== 0)
643 USBPORT_AddPipeHandle(DeviceHandle
, PipeHandle
);
645 PipeHandle
->Flags
= (PipeHandle
->Flags
& ~PIPE_HANDLE_FLAG_CLOSED
) |
646 PIPE_HANDLE_FLAG_NULL_PACKET_SIZE
;
648 PipeHandle
->Endpoint
= (PUSBPORT_ENDPOINT
)-1;
650 return STATUS_SUCCESS
;
653 Endpoint
= ExAllocatePoolWithTag(NonPagedPool
, EndpointSize
, USB_PORT_TAG
);
657 DPRINT1("USBPORT_OpenPipe: Not allocated Endpoint!\n");
658 Status
= STATUS_INSUFFICIENT_RESOURCES
;
662 RtlZeroMemory(Endpoint
, EndpointSize
);
664 Endpoint
->FdoDevice
= FdoDevice
;
665 Endpoint
->DeviceHandle
= DeviceHandle
;
666 Endpoint
->LockCounter
= -1;
668 KeInitializeSpinLock(&Endpoint
->EndpointSpinLock
);
669 KeInitializeSpinLock(&Endpoint
->StateChangeSpinLock
);
671 InitializeListHead(&Endpoint
->PendingTransferList
);
672 InitializeListHead(&Endpoint
->TransferList
);
673 InitializeListHead(&Endpoint
->CancelList
);
674 InitializeListHead(&Endpoint
->AbortList
);
676 EndpointProperties
= &Endpoint
->EndpointProperties
;
677 EndpointDescriptor
= &PipeHandle
->EndpointDescriptor
;
679 MaxPacketSize
= EndpointDescriptor
->wMaxPacketSize
& 0x7FF;
680 AdditionalTransaction
= (EndpointDescriptor
->wMaxPacketSize
>> 11) & 3;
682 EndpointProperties
->DeviceAddress
= DeviceHandle
->DeviceAddress
;
683 EndpointProperties
->DeviceSpeed
= DeviceHandle
->DeviceSpeed
;
684 EndpointProperties
->Period
= 0;
685 EndpointProperties
->EndpointAddress
= EndpointDescriptor
->bEndpointAddress
;
686 EndpointProperties
->TransactionPerMicroframe
= AdditionalTransaction
+ 1;
687 EndpointProperties
->MaxPacketSize
= MaxPacketSize
;
688 EndpointProperties
->TotalMaxPacketSize
= MaxPacketSize
*
689 (AdditionalTransaction
+ 1);
691 switch (EndpointDescriptor
->bmAttributes
& USB_ENDPOINT_TYPE_MASK
)
693 case USB_ENDPOINT_TYPE_CONTROL
:
694 EndpointProperties
->TransferType
= USBPORT_TRANSFER_TYPE_CONTROL
;
696 if (EndpointProperties
->EndpointAddress
== 0)
698 EndpointProperties
->MaxTransferSize
= 0x1000; // OUT Ep0
702 EndpointProperties
->MaxTransferSize
= 0x10000;
707 case USB_ENDPOINT_TYPE_ISOCHRONOUS
:
708 DPRINT1("USBPORT_OpenPipe: USB_ENDPOINT_TYPE_ISOCHRONOUS UNIMPLEMENTED. FIXME. \n");
709 EndpointProperties
->TransferType
= USBPORT_TRANSFER_TYPE_ISOCHRONOUS
;
710 EndpointProperties
->MaxTransferSize
= 0x1000000;
713 case USB_ENDPOINT_TYPE_BULK
:
714 EndpointProperties
->TransferType
= USBPORT_TRANSFER_TYPE_BULK
;
715 EndpointProperties
->MaxTransferSize
= 0x10000;
718 case USB_ENDPOINT_TYPE_INTERRUPT
:
719 EndpointProperties
->TransferType
= USBPORT_TRANSFER_TYPE_INTERRUPT
;
720 EndpointProperties
->MaxTransferSize
= 0x400;
724 if (EndpointProperties
->TransferType
== USBPORT_TRANSFER_TYPE_INTERRUPT
)
726 if (EndpointProperties
->DeviceSpeed
== UsbHighSpeed
)
728 Interval
= USBPORT_NormalizeHsInterval(EndpointDescriptor
->bInterval
);
732 Interval
= EndpointDescriptor
->bInterval
;
735 EndpointProperties
->Period
= 32;
737 if (Interval
&& (Interval
< 32))
739 if ((EndpointProperties
->DeviceSpeed
!= UsbLowSpeed
) ||
742 if (!(Interval
& 0x20))
744 Period
= EndpointProperties
->Period
;
750 while (!(Period
& Interval
));
752 EndpointProperties
->Period
= Period
;
757 EndpointProperties
->Period
= 8;
762 if (EndpointProperties
->TransferType
== USB_ENDPOINT_TYPE_ISOCHRONOUS
)
764 if (EndpointProperties
->DeviceSpeed
== UsbHighSpeed
)
766 EndpointProperties
->Period
=
767 USBPORT_NormalizeHsInterval(EndpointDescriptor
->bInterval
);
771 EndpointProperties
->Period
= 1;
775 if (Packet
->MiniPortFlags
& USB_MINIPORT_FLAGS_USB2
)
777 IsAllocatedBandwidth
= USBPORT_AllocateBandwidthUSB2(FdoDevice
, Endpoint
);
781 EndpointProperties
->UsbBandwidth
= USBPORT_CalculateUsbBandwidth(FdoDevice
,
784 IsAllocatedBandwidth
= USBPORT_AllocateBandwidth(FdoDevice
, Endpoint
);
787 if (!IsAllocatedBandwidth
)
789 Status
= USBPORT_USBDStatusToNtStatus(NULL
, USBD_STATUS_NO_BANDWIDTH
);
793 *UsbdStatus
= USBD_STATUS_NO_BANDWIDTH
;
799 Direction
= USB_ENDPOINT_DIRECTION_OUT(EndpointDescriptor
->bEndpointAddress
);
800 EndpointProperties
->Direction
= Direction
;
802 if (DeviceHandle
->IsRootHub
)
804 Endpoint
->EndpointWorker
= 0; // USBPORT_RootHubEndpointWorker;
806 Endpoint
->Flags
|= ENDPOINT_FLAG_ROOTHUB_EP0
;
808 Endpoint
->StateLast
= USBPORT_ENDPOINT_ACTIVE
;
809 Endpoint
->StateNext
= USBPORT_ENDPOINT_ACTIVE
;
811 PdoExtension
= FdoExtension
->RootHubPdo
->DeviceExtension
;
813 if (EndpointProperties
->TransferType
== USBPORT_TRANSFER_TYPE_INTERRUPT
)
815 PdoExtension
->Endpoint
= Endpoint
;
818 USBDStatus
= USBD_STATUS_SUCCESS
;
822 Endpoint
->EndpointWorker
= 1; // USBPORT_DmaEndpointWorker;
824 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &OldIrql
);
826 Packet
->QueryEndpointRequirements(FdoExtension
->MiniPortExt
,
827 &Endpoint
->EndpointProperties
,
830 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, OldIrql
);
832 if ((EndpointProperties
->TransferType
== USBPORT_TRANSFER_TYPE_BULK
) ||
833 (EndpointProperties
->TransferType
== USBPORT_TRANSFER_TYPE_INTERRUPT
))
835 EndpointProperties
->MaxTransferSize
= TransferParams
[1];
838 if (TransferParams
[0])
840 HeaderBuffer
= USBPORT_AllocateCommonBuffer(FdoDevice
,
848 if (HeaderBuffer
|| (TransferParams
[0] == 0))
850 Endpoint
->HeaderBuffer
= HeaderBuffer
;
854 EndpointProperties
->BufferVA
= HeaderBuffer
->VirtualAddress
;
855 EndpointProperties
->BufferPA
= HeaderBuffer
->PhysicalAddress
;
856 EndpointProperties
->BufferLength
= HeaderBuffer
->BufferLength
; // BufferLength + LengthPadded;
859 MpStatus
= MiniportOpenEndpoint(FdoDevice
, Endpoint
);
861 Endpoint
->Flags
|= ENDPOINT_FLAG_DMA_TYPE
;
862 Endpoint
->Flags
|= ENDPOINT_FLAG_QUEUENE_EMPTY
;
868 KeAcquireSpinLock(&Endpoint
->EndpointSpinLock
,
869 &Endpoint
->EndpointOldIrql
);
871 Endpoint
->StateLast
= USBPORT_ENDPOINT_PAUSED
;
872 Endpoint
->StateNext
= USBPORT_ENDPOINT_PAUSED
;
874 USBPORT_SetEndpointState(Endpoint
, USBPORT_ENDPOINT_ACTIVE
);
876 KeReleaseSpinLock(&Endpoint
->EndpointSpinLock
,
877 Endpoint
->EndpointOldIrql
);
881 KeAcquireSpinLock(&Endpoint
->EndpointSpinLock
,
882 &Endpoint
->EndpointOldIrql
);
884 State
= USBPORT_GetEndpointState(Endpoint
);
886 KeReleaseSpinLock(&Endpoint
->EndpointSpinLock
,
887 Endpoint
->EndpointOldIrql
);
889 if (State
== USBPORT_ENDPOINT_ACTIVE
)
894 USBPORT_Wait(FdoDevice
, 1); // 1 msec.
900 MpStatus
= MP_STATUS_NO_RESOURCES
;
901 Endpoint
->HeaderBuffer
= NULL
;
906 USBDStatus
= USBD_STATUS_INSUFFICIENT_RESOURCES
;
910 USBDStatus
= USBD_STATUS_SUCCESS
;
916 *UsbdStatus
= USBDStatus
;
919 Status
= USBPORT_USBDStatusToNtStatus(NULL
, USBDStatus
);
921 if (NT_SUCCESS(Status
))
923 USBPORT_AddPipeHandle(DeviceHandle
, PipeHandle
);
925 ExInterlockedInsertTailList(&FdoExtension
->EndpointList
,
926 &Endpoint
->EndpointLink
,
927 &FdoExtension
->EndpointListSpinLock
);
929 PipeHandle
->Endpoint
= Endpoint
;
930 PipeHandle
->Flags
&= ~PIPE_HANDLE_FLAG_CLOSED
;
939 if (IsAllocatedBandwidth
)
941 if (Packet
->MiniPortFlags
& USB_MINIPORT_FLAGS_USB2
)
943 USBPORT_FreeBandwidthUSB2(FdoDevice
, Endpoint
);
947 USBPORT_FreeBandwidth(FdoDevice
, Endpoint
);
951 ExFreePoolWithTag(Endpoint
, USB_PORT_TAG
);
954 DPRINT1("USBPORT_OpenPipe: Status - %lx\n", Status
);
960 USBPORT_ReopenPipe(IN PDEVICE_OBJECT FdoDevice
,
961 IN PUSBPORT_ENDPOINT Endpoint
)
963 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
964 PUSBPORT_COMMON_BUFFER_HEADER HeaderBuffer
;
965 ULONG EndpointRequirements
[2] = {0};
966 PUSBPORT_REGISTRATION_PACKET Packet
;
967 KIRQL MiniportOldIrql
;
970 DPRINT("USBPORT_ReopenPipe ... \n");
972 FdoExtension
= FdoDevice
->DeviceExtension
;
973 Packet
= &FdoExtension
->MiniPortInterface
->Packet
;
977 if (!InterlockedIncrement(&Endpoint
->LockCounter
))
980 InterlockedDecrement(&Endpoint
->LockCounter
);
981 USBPORT_Wait(FdoDevice
, 1);
984 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &MiniportOldIrql
);
986 Packet
->SetEndpointState(FdoExtension
->MiniPortExt
,
988 USBPORT_ENDPOINT_REMOVE
);
990 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, MiniportOldIrql
);
992 USBPORT_Wait(FdoDevice
, 2);
994 MiniportCloseEndpoint(FdoDevice
, Endpoint
);
996 RtlZeroMemory(Endpoint
+ 1,
997 Packet
->MiniPortEndpointSize
);
999 if (Endpoint
->HeaderBuffer
)
1001 USBPORT_FreeCommonBuffer(FdoDevice
, Endpoint
->HeaderBuffer
);
1002 Endpoint
->HeaderBuffer
= NULL
;
1005 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &MiniportOldIrql
);
1007 Packet
->QueryEndpointRequirements(FdoExtension
->MiniPortExt
,
1008 &Endpoint
->EndpointProperties
,
1009 EndpointRequirements
);
1011 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, MiniportOldIrql
);
1013 if (EndpointRequirements
[0])
1015 HeaderBuffer
= USBPORT_AllocateCommonBuffer(FdoDevice
,
1016 EndpointRequirements
[0]);
1020 HeaderBuffer
= NULL
;
1023 if (HeaderBuffer
|| EndpointRequirements
[0] == 0)
1025 Endpoint
->HeaderBuffer
= HeaderBuffer
;
1026 Status
= STATUS_SUCCESS
;
1030 Endpoint
->HeaderBuffer
= 0;
1031 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1034 if (Endpoint
->HeaderBuffer
&& HeaderBuffer
)
1036 Endpoint
->EndpointProperties
.BufferVA
= HeaderBuffer
->VirtualAddress
;
1037 Endpoint
->EndpointProperties
.BufferPA
= HeaderBuffer
->PhysicalAddress
;
1038 Endpoint
->EndpointProperties
.BufferLength
= HeaderBuffer
->BufferLength
;
1041 if (NT_SUCCESS(Status
))
1043 MiniportOpenEndpoint(FdoDevice
, Endpoint
);
1045 KeAcquireSpinLock(&Endpoint
->EndpointSpinLock
, &Endpoint
->EndpointOldIrql
);
1046 KeAcquireSpinLockAtDpcLevel(&Endpoint
->StateChangeSpinLock
);
1048 if (Endpoint
->StateLast
== USBPORT_ENDPOINT_ACTIVE
)
1050 KeAcquireSpinLockAtDpcLevel(&FdoExtension
->MiniportSpinLock
);
1052 Packet
->SetEndpointState(FdoExtension
->MiniPortExt
,
1054 USBPORT_ENDPOINT_ACTIVE
);
1056 KeReleaseSpinLockFromDpcLevel(&FdoExtension
->MiniportSpinLock
);
1059 KeReleaseSpinLockFromDpcLevel(&Endpoint
->StateChangeSpinLock
);
1060 KeReleaseSpinLock(&Endpoint
->EndpointSpinLock
, Endpoint
->EndpointOldIrql
);
1063 InterlockedDecrement(&Endpoint
->LockCounter
);
1070 USBPORT_FlushClosedEndpointList(IN PDEVICE_OBJECT FdoDevice
)
1072 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
1074 PLIST_ENTRY ClosedList
;
1075 PUSBPORT_ENDPOINT Endpoint
;
1077 DPRINT("USBPORT_FlushClosedEndpointList: ... \n");
1079 FdoExtension
= FdoDevice
->DeviceExtension
;
1081 KeAcquireSpinLock(&FdoExtension
->EndpointClosedSpinLock
, &OldIrql
);
1082 ClosedList
= &FdoExtension
->EndpointClosedList
;
1084 while (!IsListEmpty(ClosedList
))
1086 Endpoint
= CONTAINING_RECORD(ClosedList
->Flink
,
1090 RemoveHeadList(ClosedList
);
1091 Endpoint
->CloseLink
.Flink
= NULL
;
1092 Endpoint
->CloseLink
.Blink
= NULL
;
1094 KeReleaseSpinLock(&FdoExtension
->EndpointClosedSpinLock
, OldIrql
);
1096 USBPORT_DeleteEndpoint(FdoDevice
, Endpoint
);
1098 KeAcquireSpinLock(&FdoExtension
->EndpointClosedSpinLock
, &OldIrql
);
1101 KeReleaseSpinLock(&FdoExtension
->EndpointClosedSpinLock
, OldIrql
);
1106 USBPORT_InvalidateEndpointHandler(IN PDEVICE_OBJECT FdoDevice
,
1107 IN PUSBPORT_ENDPOINT Endpoint
,
1110 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
1111 PUSBPORT_REGISTRATION_PACKET Packet
;
1113 PLIST_ENTRY WorkerLink
;
1114 PUSBPORT_ENDPOINT endpoint
;
1116 BOOLEAN IsAddEntry
= FALSE
;
1118 DPRINT_CORE("USBPORT_InvalidateEndpointHandler: Endpoint - %p, Type - %x\n",
1122 FdoExtension
= FdoDevice
->DeviceExtension
;
1123 Packet
= &FdoExtension
->MiniPortInterface
->Packet
;
1127 WorkerLink
= &Endpoint
->WorkerLink
;
1128 KeAcquireSpinLock(&FdoExtension
->EndpointListSpinLock
, &OldIrql
);
1129 DPRINT_CORE("USBPORT_InvalidateEndpointHandler: KeAcquireSpinLock \n");
1131 if ((!WorkerLink
->Flink
|| !WorkerLink
->Blink
) &&
1132 !(Endpoint
->Flags
& ENDPOINT_FLAG_IDLE
) &&
1133 USBPORT_GetEndpointState(Endpoint
) != USBPORT_ENDPOINT_CLOSED
)
1135 DPRINT_CORE("USBPORT_InvalidateEndpointHandler: InsertTailList \n");
1136 InsertTailList(&FdoExtension
->WorkerList
, WorkerLink
);
1140 KeReleaseSpinLock(&FdoExtension
->EndpointListSpinLock
, OldIrql
);
1142 if (Endpoint
->Flags
& ENDPOINT_FLAG_ROOTHUB_EP0
)
1143 Type
= INVALIDATE_ENDPOINT_WORKER_THREAD
;
1147 KeAcquireSpinLock(&FdoExtension
->EndpointListSpinLock
, &OldIrql
);
1149 Entry
= &FdoExtension
->EndpointList
;
1151 while (Entry
&& Entry
!= &FdoExtension
->EndpointList
)
1153 endpoint
= CONTAINING_RECORD(Entry
,
1157 if (!endpoint
->WorkerLink
.Flink
|| !endpoint
->WorkerLink
.Blink
)
1159 if (!(endpoint
->Flags
& ENDPOINT_FLAG_IDLE
) &&
1160 !(endpoint
->Flags
& ENDPOINT_FLAG_ROOTHUB_EP0
) &&
1161 USBPORT_GetEndpointState(endpoint
) != USBPORT_ENDPOINT_CLOSED
)
1163 DPRINT_CORE("USBPORT_InvalidateEndpointHandler: InsertTailList \n");
1164 InsertTailList(&FdoExtension
->WorkerList
, &endpoint
->WorkerLink
);
1169 Entry
= endpoint
->EndpointLink
.Flink
;
1172 KeReleaseSpinLock(&FdoExtension
->EndpointListSpinLock
, OldIrql
);
1175 if (FdoExtension
->Flags
& USBPORT_FLAG_HC_SUSPEND
)
1177 Type
= INVALIDATE_ENDPOINT_WORKER_THREAD
;
1179 else if (IsAddEntry
== FALSE
&& Type
== INVALIDATE_ENDPOINT_INT_NEXT_SOF
)
1181 Type
= INVALIDATE_ENDPOINT_ONLY
;
1186 case INVALIDATE_ENDPOINT_WORKER_THREAD
:
1187 USBPORT_SignalWorkerThread(FdoDevice
);
1190 case INVALIDATE_ENDPOINT_WORKER_DPC
:
1191 KeInsertQueueDpc(&FdoExtension
->WorkerRequestDpc
, NULL
, NULL
);
1194 case INVALIDATE_ENDPOINT_INT_NEXT_SOF
:
1195 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &OldIrql
);
1196 Packet
->InterruptNextSOF(FdoExtension
->MiniPortExt
);
1197 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, OldIrql
);
1204 USBPORT_DmaEndpointPaused(IN PDEVICE_OBJECT FdoDevice
,
1205 IN PUSBPORT_ENDPOINT Endpoint
)
1207 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
1208 PUSBPORT_REGISTRATION_PACKET Packet
;
1210 PUSBPORT_TRANSFER Transfer
;
1214 ULONG CompletedLen
= 0;
1217 DPRINT_CORE("USBPORT_DmaEndpointPaused \n");
1219 FdoExtension
= FdoDevice
->DeviceExtension
;
1220 Packet
= &FdoExtension
->MiniPortInterface
->Packet
;
1222 Entry
= Endpoint
->TransferList
.Flink
;
1224 if (Entry
== &Endpoint
->TransferList
)
1225 return USBPORT_ENDPOINT_ACTIVE
;
1227 while (Entry
&& Entry
!= &Endpoint
->TransferList
)
1229 Transfer
= CONTAINING_RECORD(Entry
,
1233 if (Transfer
->Flags
& (TRANSFER_FLAG_CANCELED
| TRANSFER_FLAG_ABORTED
))
1235 if (Transfer
->Flags
& TRANSFER_FLAG_ISO
&&
1236 Transfer
->Flags
& TRANSFER_FLAG_SUBMITED
&&
1237 !(Endpoint
->Flags
& ENDPOINT_FLAG_NUKE
))
1239 Urb
= Transfer
->Urb
;
1241 Frame
= Urb
->UrbIsochronousTransfer
.StartFrame
+
1242 Urb
->UrbIsochronousTransfer
.NumberOfPackets
;
1244 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &OldIrql
);
1245 CurrentFrame
= Packet
->Get32BitFrameNumber(FdoExtension
->MiniPortExt
);
1246 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, OldIrql
);
1248 if (Frame
+ 1 > CurrentFrame
)
1250 return USBPORT_GetEndpointState(Endpoint
);
1254 if ((Transfer
->Flags
& TRANSFER_FLAG_SUBMITED
) &&
1255 !(Endpoint
->Flags
& ENDPOINT_FLAG_NUKE
))
1257 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &OldIrql
);
1259 Packet
->AbortTransfer(FdoExtension
->MiniPortExt
,
1261 Transfer
->MiniportTransfer
,
1264 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, OldIrql
);
1266 if (Transfer
->Flags
& TRANSFER_FLAG_ISO
)
1268 DPRINT1("USBPORT_DmaEndpointActive: FIXME call USBPORT_FlushIsoTransfer\n");
1269 ASSERT(FALSE
); //USBPORT_FlushIsoTransfer();
1273 Transfer
->CompletedTransferLen
= CompletedLen
;
1277 RemoveEntryList(&Transfer
->TransferLink
);
1278 Entry
= Transfer
->TransferLink
.Flink
;
1280 if (Transfer
->Flags
& TRANSFER_FLAG_SPLITED
)
1282 USBPORT_CancelSplitTransfer(Transfer
);
1286 InsertTailList(&Endpoint
->CancelList
, &Transfer
->TransferLink
);
1291 Entry
= Transfer
->TransferLink
.Flink
;
1295 return USBPORT_ENDPOINT_ACTIVE
;
1300 USBPORT_DmaEndpointActive(IN PDEVICE_OBJECT FdoDevice
,
1301 IN PUSBPORT_ENDPOINT Endpoint
)
1303 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
1304 PUSBPORT_REGISTRATION_PACKET Packet
;
1306 PUSBPORT_TRANSFER Transfer
;
1307 LARGE_INTEGER TimeOut
;
1311 DPRINT_CORE("USBPORT_DmaEndpointActive \n");
1313 FdoExtension
= FdoDevice
->DeviceExtension
;
1315 Entry
= Endpoint
->TransferList
.Flink
;
1317 while (Entry
&& Entry
!= &Endpoint
->TransferList
)
1319 Transfer
= CONTAINING_RECORD(Entry
,
1323 if (!(Transfer
->Flags
& TRANSFER_FLAG_SUBMITED
) &&
1324 !(Endpoint
->Flags
& ENDPOINT_FLAG_NUKE
))
1326 KeAcquireSpinLock(&FdoExtension
->MiniportSpinLock
, &OldIrql
);
1328 Packet
= &FdoExtension
->MiniPortInterface
->Packet
;
1330 if (Transfer
->Flags
& TRANSFER_FLAG_ISO
)
1332 DPRINT1("USBPORT_DmaEndpointActive: FIXME call SubmitIsoTransfer\n");
1334 MpStatus
= Packet
->SubmitIsoTransfer(FdoExtension
->MiniPortExt
,
1336 &Transfer
->TransferParameters
,
1337 Transfer
->MiniportTransfer
,
1338 NULL
);//&Transfer->IsoTransferParameters);
1342 MpStatus
= Packet
->SubmitTransfer(FdoExtension
->MiniPortExt
,
1344 &Transfer
->TransferParameters
,
1345 Transfer
->MiniportTransfer
,
1349 KeReleaseSpinLock(&FdoExtension
->MiniportSpinLock
, OldIrql
);
1353 if ((MpStatus
!= MP_STATUS_FAILURE
) && Transfer
->Flags
& TRANSFER_FLAG_ISO
)
1355 DPRINT1("USBPORT_DmaEndpointActive: FIXME call USBPORT_ErrorCompleteIsoTransfer\n");
1356 ASSERT(FALSE
); //USBPORT_ErrorCompleteIsoTransfer();
1359 return USBPORT_ENDPOINT_ACTIVE
;
1362 Transfer
->Flags
|= TRANSFER_FLAG_SUBMITED
;
1363 KeQuerySystemTime(&Transfer
->Time
);
1365 TimeOut
.QuadPart
= 10000 * Transfer
->TimeOut
;
1366 Transfer
->Time
.QuadPart
+= TimeOut
.QuadPart
;
1369 if (Transfer
->Flags
& (TRANSFER_FLAG_CANCELED
| TRANSFER_FLAG_ABORTED
))
1371 return USBPORT_ENDPOINT_PAUSED
;
1374 Entry
= Transfer
->TransferLink
.Flink
;
1377 return USBPORT_ENDPOINT_ACTIVE
;
1382 USBPORT_DmaEndpointWorker(IN PUSBPORT_ENDPOINT Endpoint
)
1384 PDEVICE_OBJECT FdoDevice
;
1386 ULONG EndpointState
;
1387 BOOLEAN IsPaused
= FALSE
;
1389 DPRINT_CORE("USBPORT_DmaEndpointWorker ... \n");
1391 FdoDevice
= Endpoint
->FdoDevice
;
1393 KeAcquireSpinLock(&Endpoint
->EndpointSpinLock
, &Endpoint
->EndpointOldIrql
);
1395 PrevState
= USBPORT_GetEndpointState(Endpoint
);
1397 if (PrevState
== USBPORT_ENDPOINT_PAUSED
)
1399 EndpointState
= USBPORT_DmaEndpointPaused(FdoDevice
, Endpoint
);
1401 else if (PrevState
== USBPORT_ENDPOINT_ACTIVE
)
1403 EndpointState
= USBPORT_DmaEndpointActive(FdoDevice
, Endpoint
);
1407 #ifndef NDEBUG_USBPORT_CORE
1408 DPRINT1("USBPORT_DmaEndpointWorker: DbgBreakPoint. EndpointState - %x\n",
1412 EndpointState
= USBPORT_ENDPOINT_UNKNOWN
;
1415 KeReleaseSpinLock(&Endpoint
->EndpointSpinLock
, Endpoint
->EndpointOldIrql
);
1417 USBPORT_FlushCancelList(Endpoint
);
1419 KeAcquireSpinLock(&Endpoint
->EndpointSpinLock
, &Endpoint
->EndpointOldIrql
);
1421 if (EndpointState
== PrevState
)
1423 if (EndpointState
== USBPORT_ENDPOINT_PAUSED
)
1430 USBPORT_SetEndpointState(Endpoint
, EndpointState
);
1433 KeReleaseSpinLock(&Endpoint
->EndpointSpinLock
, Endpoint
->EndpointOldIrql
);
1437 USBPORT_InvalidateEndpointHandler(FdoDevice
,
1439 INVALIDATE_ENDPOINT_WORKER_THREAD
);
1442 DPRINT_CORE("USBPORT_DmaEndpointWorker exit \n");
1447 USBPORT_EndpointWorker(IN PUSBPORT_ENDPOINT Endpoint
,
1448 IN BOOLEAN LockNotChecked
)
1450 PDEVICE_OBJECT FdoDevice
;
1451 PUSBPORT_DEVICE_EXTENSION FdoExtension
;
1452 PUSBPORT_REGISTRATION_PACKET Packet
;
1453 ULONG EndpointState
;
1455 DPRINT_CORE("USBPORT_EndpointWorker: Endpoint - %p, LockNotChecked - %x\n",
1459 FdoDevice
= Endpoint
->FdoDevice
;
1460 FdoExtension
= FdoDevice
->DeviceExtension
;
1461 Packet
= &FdoExtension
->MiniPortInterface
->Packet
;
1463 if (LockNotChecked
== FALSE
)
1465 if (InterlockedIncrement(&Endpoint
->LockCounter
))
1467 InterlockedDecrement(&Endpoint
->LockCounter
);
1468 DPRINT_CORE("USBPORT_EndpointWorker: LockCounter > 0\n");
1473 ASSERT(KeGetCurrentIrql() == DISPATCH_LEVEL
);
1475 KeAcquireSpinLockAtDpcLevel(&Endpoint
->EndpointSpinLock
);
1477 if (USBPORT_GetEndpointState(Endpoint
) == USBPORT_ENDPOINT_CLOSED
)
1479 KeReleaseSpinLockFromDpcLevel(&Endpoint
->EndpointSpinLock
);
1480 InterlockedDecrement(&Endpoint
->LockCounter
);
1481 DPRINT_CORE("USBPORT_EndpointWorker: State == USBPORT_ENDPOINT_CLOSED. return FALSE\n");
1485 if ((Endpoint
->Flags
& (ENDPOINT_FLAG_ROOTHUB_EP0
| ENDPOINT_FLAG_NUKE
)) == 0)
1487 KeAcquireSpinLockAtDpcLevel(&FdoExtension
->MiniportSpinLock
);
1488 Packet
->PollEndpoint(FdoExtension
->MiniPortExt
, Endpoint
+ 1);
1489 KeReleaseSpinLockFromDpcLevel(&FdoExtension
->MiniportSpinLock
);
1492 EndpointState
= USBPORT_GetEndpointState(Endpoint
);
1494 if (EndpointState
== USBPORT_ENDPOINT_REMOVE
)
1496 KeAcquireSpinLockAtDpcLevel(&Endpoint
->StateChangeSpinLock
);
1497 Endpoint
->StateLast
= USBPORT_ENDPOINT_CLOSED
;
1498 Endpoint
->StateNext
= USBPORT_ENDPOINT_CLOSED
;
1499 KeReleaseSpinLockFromDpcLevel(&Endpoint
->StateChangeSpinLock
);
1501 KeReleaseSpinLockFromDpcLevel(&Endpoint
->EndpointSpinLock
);
1503 KeAcquireSpinLockAtDpcLevel(&FdoExtension
->EndpointListSpinLock
);
1505 ExInterlockedInsertTailList(&FdoExtension
->EndpointClosedList
,
1506 &Endpoint
->CloseLink
,
1507 &FdoExtension
->EndpointClosedSpinLock
);
1509 KeReleaseSpinLockFromDpcLevel(&FdoExtension
->EndpointListSpinLock
);
1511 InterlockedDecrement(&Endpoint
->LockCounter
);
1512 DPRINT_CORE("USBPORT_EndpointWorker: State == USBPORT_ENDPOINT_REMOVE. return FALSE\n");
1516 if (!IsListEmpty(&Endpoint
->PendingTransferList
) ||
1517 !IsListEmpty(&Endpoint
->TransferList
) ||
1518 !IsListEmpty(&Endpoint
->CancelList
))
1520 KeReleaseSpinLockFromDpcLevel(&Endpoint
->EndpointSpinLock
);
1522 EndpointState
= USBPORT_GetEndpointState(Endpoint
);
1524 KeAcquireSpinLockAtDpcLevel(&Endpoint
->StateChangeSpinLock
);
1525 if (EndpointState
== Endpoint
->StateNext
)
1527 KeReleaseSpinLockFromDpcLevel(&Endpoint
->StateChangeSpinLock
);
1529 if (Endpoint
->EndpointWorker
)
1531 USBPORT_DmaEndpointWorker(Endpoint
);
1535 USBPORT_RootHubEndpointWorker(Endpoint
);
1538 USBPORT_FlushAbortList(Endpoint
);
1540 InterlockedDecrement(&Endpoint
->LockCounter
);
1541 DPRINT_CORE("USBPORT_EndpointWorker: return FALSE\n");
1545 KeReleaseSpinLockFromDpcLevel(&Endpoint
->StateChangeSpinLock
);
1546 InterlockedDecrement(&Endpoint
->LockCounter
);
1548 DPRINT_CORE("USBPORT_EndpointWorker: return TRUE\n");
1552 KeReleaseSpinLockFromDpcLevel(&Endpoint
->EndpointSpinLock
);
1554 USBPORT_FlushAbortList(Endpoint
);
1556 InterlockedDecrement(&Endpoint
->LockCounter
);
1557 DPRINT_CORE("USBPORT_EndpointWorker: return FALSE\n");