[USB-BRINGUP-TRUNK]
[reactos.git] / drivers / usb / usbehci_new / usb_request.cpp
1 /*
2 * PROJECT: ReactOS Universal Serial Bus Bulk Enhanced Host Controller Interface
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: drivers/usb/usbehci/usb_request.cpp
5 * PURPOSE: USB EHCI device driver.
6 * PROGRAMMERS:
7 * Michael Martin (michael.martin@reactos.org)
8 * Johannes Anderwald (johannes.anderwald@reactos.org)
9 */
10
11 #define INITGUID
12
13 #include "usbehci.h"
14 #include "hardware.h"
15
16 class CUSBRequest : public IUSBRequest
17 {
18 public:
19 STDMETHODIMP QueryInterface( REFIID InterfaceId, PVOID* Interface);
20
21 STDMETHODIMP_(ULONG) AddRef()
22 {
23 InterlockedIncrement(&m_Ref);
24 return m_Ref;
25 }
26 STDMETHODIMP_(ULONG) Release()
27 {
28 InterlockedDecrement(&m_Ref);
29
30 if (!m_Ref)
31 {
32 delete this;
33 return 0;
34 }
35 return m_Ref;
36 }
37
38 // IUSBRequest interface functions
39 virtual NTSTATUS InitializeWithSetupPacket(IN PDMAMEMORYMANAGER DmaManager, IN PUSB_DEFAULT_PIPE_SETUP_PACKET SetupPacket, IN UCHAR DeviceAddress, IN OPTIONAL PUSB_ENDPOINT_DESCRIPTOR EndpointDescriptor, IN OUT ULONG TransferBufferLength, IN OUT PMDL TransferBuffer);
40 virtual NTSTATUS InitializeWithIrp(IN PDMAMEMORYMANAGER DmaManager, IN OUT PIRP Irp);
41 virtual VOID CompletionCallback(IN NTSTATUS NtStatusCode, IN ULONG UrbStatusCode, IN struct _QUEUE_HEAD *QueueHead);
42 virtual VOID CancelCallback(IN NTSTATUS NtStatusCode, IN struct _QUEUE_HEAD *QueueHead);
43 virtual NTSTATUS GetQueueHead(struct _QUEUE_HEAD ** OutHead);
44 virtual BOOLEAN IsRequestComplete();
45 virtual ULONG GetTransferType();
46 virtual VOID GetResultStatus(OUT OPTIONAL NTSTATUS *NtStatusCode, OUT OPTIONAL PULONG UrbStatusCode);
47 virtual BOOLEAN IsRequestInitialized();
48 virtual BOOLEAN ShouldReleaseRequestAfterCompletion();
49 virtual VOID FreeQueueHead(struct _QUEUE_HEAD * QueueHead);
50 virtual VOID GetTransferBuffer(OUT PMDL * OutMDL, OUT PULONG TransferLength);
51 virtual BOOLEAN IsQueueHeadComplete(struct _QUEUE_HEAD * QueueHead);
52
53
54 // local functions
55 ULONG InternalGetTransferType();
56 UCHAR InternalGetPidDirection();
57 NTSTATUS BuildControlTransferQueueHead(PQUEUE_HEAD * OutHead);
58 NTSTATUS BuildBulkTransferQueueHead(PQUEUE_HEAD * OutHead);
59 NTSTATUS CreateDescriptor(PQUEUE_TRANSFER_DESCRIPTOR *OutDescriptor);
60 NTSTATUS CreateQueueHead(PQUEUE_HEAD *OutQueueHead);
61 UCHAR GetDeviceAddress();
62 NTSTATUS BuildSetupPacket();
63 NTSTATUS BuildSetupPacketFromURB();
64 ULONG InternalCalculateTransferLength();
65
66 // constructor / destructor
67 CUSBRequest(IUnknown *OuterUnknown){}
68 virtual ~CUSBRequest(){}
69
70 protected:
71 LONG m_Ref;
72
73 //
74 // memory manager for allocating setup packet / queue head / transfer descriptors
75 //
76 PDMAMEMORYMANAGER m_DmaManager;
77
78 //
79 // caller provided irp packet containing URB request
80 //
81 PIRP m_Irp;
82
83 //
84 // transfer buffer length
85 //
86 ULONG m_TransferBufferLength;
87
88 //
89 // current transfer length
90 //
91 ULONG m_TransferBufferLengthCompleted;
92
93 //
94 // Total Transfer Length
95 //
96 ULONG m_TotalBytesTransferred;
97
98 //
99 // transfer buffer MDL
100 //
101 PMDL m_TransferBufferMDL;
102
103 //
104 // caller provided setup packet
105 //
106 PUSB_DEFAULT_PIPE_SETUP_PACKET m_SetupPacket;
107
108 //
109 // completion event for callers who initialized request with setup packet
110 //
111 PKEVENT m_CompletionEvent;
112
113 //
114 // device address for callers who initialized it with device address
115 //
116 UCHAR m_DeviceAddress;
117
118 //
119 // store end point address
120 //
121 PUSB_ENDPOINT_DESCRIPTOR m_EndpointDescriptor;
122
123 //
124 // DMA queue head
125 //
126 PQUEUE_HEAD m_QueueHead;
127
128 //
129 // DMA transfer descriptors linked to the queue head
130 //
131 PQUEUE_TRANSFER_DESCRIPTOR m_TransferDescriptors[3];
132
133 //
134 // allocated setup packet from the DMA pool
135 //
136 PUSB_DEFAULT_PIPE_SETUP_PACKET m_DescriptorPacket;
137 PHYSICAL_ADDRESS m_DescriptorSetupPacket;
138
139 //
140 // stores the result of the operation
141 //
142 NTSTATUS m_NtStatusCode;
143 ULONG m_UrbStatusCode;
144
145 };
146
147 //----------------------------------------------------------------------------------------
148 NTSTATUS
149 STDMETHODCALLTYPE
150 CUSBRequest::QueryInterface(
151 IN REFIID refiid,
152 OUT PVOID* Output)
153 {
154 return STATUS_UNSUCCESSFUL;
155 }
156
157 //----------------------------------------------------------------------------------------
158 NTSTATUS
159 CUSBRequest::InitializeWithSetupPacket(
160 IN PDMAMEMORYMANAGER DmaManager,
161 IN PUSB_DEFAULT_PIPE_SETUP_PACKET SetupPacket,
162 IN UCHAR DeviceAddress,
163 IN OPTIONAL PUSB_ENDPOINT_DESCRIPTOR EndpointDescriptor,
164 IN OUT ULONG TransferBufferLength,
165 IN OUT PMDL TransferBuffer)
166 {
167 //
168 // sanity checks
169 //
170 PC_ASSERT(DmaManager);
171 PC_ASSERT(SetupPacket);
172
173 //
174 // initialize packet
175 //
176 m_DmaManager = DmaManager;
177 m_SetupPacket = SetupPacket;
178 m_TransferBufferLength = TransferBufferLength;
179 m_TransferBufferMDL = TransferBuffer;
180 m_DeviceAddress = DeviceAddress;
181 m_EndpointDescriptor = EndpointDescriptor;
182 m_TotalBytesTransferred = 0;
183
184 //
185 // Set Length Completed to 0
186 //
187 m_TransferBufferLengthCompleted = 0;
188
189 //
190 // allocate completion event
191 //
192 m_CompletionEvent = (PKEVENT)ExAllocatePoolWithTag(NonPagedPool, sizeof(KEVENT), TAG_USBEHCI);
193 if (!m_CompletionEvent)
194 {
195 //
196 // failed to allocate completion event
197 //
198 return STATUS_INSUFFICIENT_RESOURCES;
199 }
200
201 //
202 // initialize completion event
203 //
204 KeInitializeEvent(m_CompletionEvent, NotificationEvent, FALSE);
205
206 //
207 // done
208 //
209 return STATUS_SUCCESS;
210 }
211 //----------------------------------------------------------------------------------------
212 NTSTATUS
213 CUSBRequest::InitializeWithIrp(
214 IN PDMAMEMORYMANAGER DmaManager,
215 IN OUT PIRP Irp)
216 {
217 PIO_STACK_LOCATION IoStack;
218 PURB Urb;
219
220 //
221 // sanity checks
222 //
223 PC_ASSERT(DmaManager);
224 PC_ASSERT(Irp);
225
226 m_DmaManager = DmaManager;
227 m_TotalBytesTransferred = 0;
228
229 //
230 // get current irp stack location
231 //
232 IoStack = IoGetCurrentIrpStackLocation(Irp);
233
234 //
235 // sanity check
236 //
237 PC_ASSERT(IoStack->MajorFunction == IRP_MJ_INTERNAL_DEVICE_CONTROL);
238 PC_ASSERT(IoStack->Parameters.DeviceIoControl.IoControlCode == IOCTL_INTERNAL_USB_SUBMIT_URB);
239 PC_ASSERT(IoStack->Parameters.Others.Argument1 != 0);
240
241 //
242 // get urb
243 //
244 Urb = (PURB)IoStack->Parameters.Others.Argument1;
245
246 //
247 // store irp
248 //
249 m_Irp = Irp;
250
251 //
252 // check function type
253 //
254 switch (Urb->UrbHeader.Function)
255 {
256 //
257 // luckily those request have the same structure layout
258 //
259 case URB_FUNCTION_CLASS_INTERFACE:
260 case URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE:
261 case URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER:
262 {
263 //
264 // bulk interrupt transfer
265 //
266 if (Urb->UrbBulkOrInterruptTransfer.TransferBufferLength)
267 {
268 //
269 // Check if there is a MDL
270 //
271 if (!Urb->UrbBulkOrInterruptTransfer.TransferBufferMDL)
272 {
273 //
274 // sanity check
275 //
276 PC_ASSERT(Urb->UrbBulkOrInterruptTransfer.TransferBuffer);
277
278 //
279 // Create one using TransferBuffer
280 //
281 DPRINT("Creating Mdl from Urb Buffer %p Length %lu\n", Urb->UrbBulkOrInterruptTransfer.TransferBuffer, Urb->UrbBulkOrInterruptTransfer.TransferBufferLength);
282 m_TransferBufferMDL = IoAllocateMdl(Urb->UrbBulkOrInterruptTransfer.TransferBuffer,
283 Urb->UrbBulkOrInterruptTransfer.TransferBufferLength,
284 FALSE,
285 FALSE,
286 NULL);
287
288 if (!m_TransferBufferMDL)
289 {
290 //
291 // failed to allocate mdl
292 //
293 return STATUS_INSUFFICIENT_RESOURCES;
294 }
295
296 //
297 // build mdl for non paged pool
298 // FIXME: Does hub driver already do this when passing MDL?
299 //
300 MmBuildMdlForNonPagedPool(m_TransferBufferMDL);
301
302 //
303 // Keep that ehci created the MDL and needs to free it.
304 //
305 }
306 else
307 {
308 m_TransferBufferMDL = Urb->UrbBulkOrInterruptTransfer.TransferBufferMDL;
309 }
310
311 //
312 // save buffer length
313 //
314 m_TransferBufferLength = Urb->UrbBulkOrInterruptTransfer.TransferBufferLength;
315
316 //
317 // Set Length Completed to 0
318 //
319 m_TransferBufferLengthCompleted = 0;
320
321 //
322 // get endpoint descriptor
323 //
324 m_EndpointDescriptor = (PUSB_ENDPOINT_DESCRIPTOR)Urb->UrbBulkOrInterruptTransfer.PipeHandle;
325
326 }
327 break;
328 }
329 default:
330 DPRINT1("URB Function: not supported %x\n", Urb->UrbHeader.Function);
331 PC_ASSERT(FALSE);
332 }
333
334 //
335 // done
336 //
337 return STATUS_SUCCESS;
338
339 }
340
341 //----------------------------------------------------------------------------------------
342 VOID
343 CUSBRequest::CompletionCallback(
344 IN NTSTATUS NtStatusCode,
345 IN ULONG UrbStatusCode,
346 IN struct _QUEUE_HEAD *QueueHead)
347 {
348 PIO_STACK_LOCATION IoStack;
349 PURB Urb;
350
351 //
352 // FIXME: support linked queue heads
353 //
354
355 //
356 // store completion code
357 //
358 m_NtStatusCode = NtStatusCode;
359 m_UrbStatusCode = UrbStatusCode;
360
361 if (m_Irp)
362 {
363 //
364 // set irp completion status
365 //
366 m_Irp->IoStatus.Status = NtStatusCode;
367
368 //
369 // get current irp stack location
370 //
371 IoStack = IoGetCurrentIrpStackLocation(m_Irp);
372
373 //
374 // get urb
375 //
376 Urb = (PURB)IoStack->Parameters.Others.Argument1;
377
378 //
379 // store urb status
380 //
381 Urb->UrbHeader.Status = UrbStatusCode;
382
383 //
384 // Check if the MDL was created
385 //
386 if (!Urb->UrbBulkOrInterruptTransfer.TransferBufferMDL)
387 {
388 //
389 // Free Mdl
390 //
391 IoFreeMdl(m_TransferBufferMDL);
392 }
393
394 //
395 // check if the request was successfull
396 //
397 if (!NT_SUCCESS(NtStatusCode))
398 {
399 //
400 // set returned length to zero in case of error
401 //
402 Urb->UrbHeader.Length = 0;
403 }
404 else
405 {
406 //
407 // calculate transfer length
408 //
409 Urb->UrbBulkOrInterruptTransfer.TransferBufferLength = InternalCalculateTransferLength();
410 }
411
412 DPRINT("Request %p Completing Irp %p NtStatusCode %x UrbStatusCode %x Transferred Length %lu\n", this, m_Irp, NtStatusCode, UrbStatusCode, Urb->UrbBulkOrInterruptTransfer.TransferBufferLength);
413
414 //
415 // FIXME: check if the transfer was split
416 // if yes dont complete irp yet
417 //
418 IoCompleteRequest(m_Irp, IO_NO_INCREMENT);
419 }
420 else
421 {
422 //
423 // signal completion event
424 //
425 PC_ASSERT(m_CompletionEvent);
426 KeSetEvent(m_CompletionEvent, 0, FALSE);
427 }
428 }
429 //----------------------------------------------------------------------------------------
430 VOID
431 CUSBRequest::CancelCallback(
432 IN NTSTATUS NtStatusCode,
433 IN struct _QUEUE_HEAD *QueueHead)
434 {
435 PIO_STACK_LOCATION IoStack;
436 PURB Urb;
437
438 //
439 // FIXME: support linked queue heads
440 //
441
442 //
443 // store cancelleation code
444 //
445 m_NtStatusCode = NtStatusCode;
446
447 if (m_Irp)
448 {
449 //
450 // set irp completion status
451 //
452 m_Irp->IoStatus.Status = NtStatusCode;
453
454 //
455 // get current irp stack location
456 //
457 IoStack = IoGetCurrentIrpStackLocation(m_Irp);
458
459 //
460 // get urb
461 //
462 Urb = (PURB)IoStack->Parameters.Others.Argument1;
463
464 //
465 // store urb status
466 //
467 DPRINT1("Request Cancelled\n");
468 Urb->UrbHeader.Status = USBD_STATUS_CANCELED;
469 Urb->UrbHeader.Length = 0;
470
471 //
472 // FIXME: check if the transfer was split
473 // if yes dont complete irp yet
474 //
475 IoCompleteRequest(m_Irp, IO_NO_INCREMENT);
476 }
477 else
478 {
479 //
480 // signal completion event
481 //
482 PC_ASSERT(m_CompletionEvent);
483 KeSetEvent(m_CompletionEvent, 0, FALSE);
484 }
485 }
486 //----------------------------------------------------------------------------------------
487 NTSTATUS
488 CUSBRequest::GetQueueHead(
489 struct _QUEUE_HEAD ** OutHead)
490 {
491 ULONG TransferType;
492 NTSTATUS Status;
493
494 //
495 // first get transfer type
496 //
497 TransferType = InternalGetTransferType();
498
499 //
500 // build request depending on type
501 //
502 switch(TransferType)
503 {
504 case USB_ENDPOINT_TYPE_CONTROL:
505 Status = BuildControlTransferQueueHead(OutHead);
506 break;
507 case USB_ENDPOINT_TYPE_BULK:
508 Status = BuildBulkTransferQueueHead(OutHead);
509 break;
510 case USB_ENDPOINT_TYPE_INTERRUPT:
511 DPRINT1("USB_ENDPOINT_TYPE_INTERRUPT not implemented\n");
512 Status = STATUS_NOT_IMPLEMENTED;
513 break;
514 case USB_ENDPOINT_TYPE_ISOCHRONOUS:
515 DPRINT1("USB_ENDPOINT_TYPE_ISOCHRONOUS not implemented\n");
516 Status = STATUS_NOT_IMPLEMENTED;
517 break;
518 default:
519 PC_ASSERT(FALSE);
520 Status = STATUS_NOT_IMPLEMENTED;
521 break;
522 }
523
524 if (NT_SUCCESS(Status))
525 {
526 //
527 // store queue head
528 //
529 m_QueueHead = *OutHead;
530
531 //
532 // store request object
533 //
534 (*OutHead)->Request = PVOID(this);
535 }
536
537 //
538 // done
539 //
540 return Status;
541 }
542
543 //----------------------------------------------------------------------------------------
544 BOOLEAN
545 CUSBRequest::IsRequestComplete()
546 {
547 //
548 // FIXME: check if request was split
549 //
550
551 //
552 // Check if the transfer was completed, only valid for Bulk Transfers
553 //
554 if ((m_TransferBufferLengthCompleted < m_TransferBufferLength)
555 && (GetTransferType() == USB_ENDPOINT_TYPE_BULK))
556 {
557 //
558 // Transfer not completed
559 //
560 return FALSE;
561 }
562 return TRUE;
563 }
564 //----------------------------------------------------------------------------------------
565 ULONG
566 CUSBRequest::GetTransferType()
567 {
568 //
569 // call internal implementation
570 //
571 return InternalGetTransferType();
572 }
573
574 //----------------------------------------------------------------------------------------
575 ULONG
576 CUSBRequest::InternalGetTransferType()
577 {
578 ULONG TransferType;
579
580 //
581 // check if an irp is provided
582 //
583 if (m_Irp)
584 {
585 ASSERT(m_EndpointDescriptor);
586
587 //
588 // end point is defined in the low byte of bmAttributes
589 //
590 TransferType = (m_EndpointDescriptor->bmAttributes & USB_ENDPOINT_TYPE_MASK);
591 }
592 else
593 {
594 //
595 // initialized with setup packet, must be a control transfer
596 //
597 TransferType = USB_ENDPOINT_TYPE_CONTROL;
598 }
599
600 //
601 // done
602 //
603 return TransferType;
604 }
605
606 UCHAR
607 CUSBRequest::InternalGetPidDirection()
608 {
609 ASSERT(m_Irp);
610 ASSERT(m_EndpointDescriptor);
611
612 //
613 // end point is defined in the low byte of bEndpointAddress
614 //
615 return (m_EndpointDescriptor->bEndpointAddress & USB_ENDPOINT_DIRECTION_MASK) >> 7;
616 }
617
618 //----------------------------------------------------------------------------------------
619 NTSTATUS
620 CUSBRequest::BuildControlTransferQueueHead(
621 PQUEUE_HEAD * OutHead)
622 {
623 NTSTATUS Status;
624 ULONG NumTransferDescriptors, Index;
625 PQUEUE_HEAD QueueHead;
626
627 //
628 // first allocate the queue head
629 //
630 Status = CreateQueueHead(&QueueHead);
631 if (!NT_SUCCESS(Status))
632 {
633 //
634 // failed to allocate queue head
635 //
636 return STATUS_INSUFFICIENT_RESOURCES;
637 }
638
639 //
640 // sanity check
641 //
642 PC_ASSERT(QueueHead);
643
644 //
645 // create setup packet
646 //
647 Status = BuildSetupPacket();
648 if (!NT_SUCCESS(Status))
649 {
650 //
651 // failed to allocate setup packet
652 //
653 return STATUS_INSUFFICIENT_RESOURCES;
654 }
655
656 //
657 // calculate num of transfer descriptors
658 //
659 NumTransferDescriptors = m_TransferBufferMDL != 0 ? 3 : 2;
660
661 //
662 // allocate transfer descriptors
663 //
664 for(Index = 0; Index < NumTransferDescriptors; Index++)
665 {
666 //
667 // allocate transfer descriptor
668 //
669 Status = CreateDescriptor(&m_TransferDescriptors[Index]);
670 if (!NT_SUCCESS(Status))
671 {
672 //
673 // failed to allocate transfer descriptor
674 //
675 return Status;
676 }
677 }
678
679 //
680 // now initialize the queue head
681 //
682 QueueHead->EndPointCharacteristics.DeviceAddress = GetDeviceAddress();
683
684 if (m_EndpointDescriptor)
685 {
686 //
687 // set endpoint address and max packet length
688 //
689 QueueHead->EndPointCharacteristics.EndPointNumber = m_EndpointDescriptor->bEndpointAddress & 0x0F;
690 QueueHead->EndPointCharacteristics.MaximumPacketLength = m_EndpointDescriptor->wMaxPacketSize;
691 }
692
693 QueueHead->Token.Bits.DataToggle = TRUE;
694
695 //
696 // setup descriptors
697 //
698 m_TransferDescriptors[0]->Token.Bits.PIDCode = PID_CODE_SETUP_TOKEN;
699 m_TransferDescriptors[0]->Token.Bits.TotalBytesToTransfer = sizeof(USB_DEFAULT_PIPE_SETUP_PACKET);
700 m_TransferDescriptors[0]->Token.Bits.DataToggle = FALSE;
701
702 if (m_TransferBufferMDL)
703 {
704 //
705 // setup in descriptor
706 //
707 m_TransferDescriptors[1]->Token.Bits.PIDCode = PID_CODE_IN_TOKEN;
708 m_TransferDescriptors[1]->Token.Bits.TotalBytesToTransfer = m_TransferBufferLength;
709
710 //
711 // FIXME: check if the request spawns over a page -> fill other members
712 //
713 PC_ASSERT(m_TransferBufferLength <= PAGE_SIZE);
714 m_TransferDescriptors[1]->BufferPointer[0] = MmGetPhysicalAddress(MmGetMdlVirtualAddress(m_TransferBufferMDL)).LowPart;
715
716 //
717 // setup out descriptor
718 //
719 m_TransferDescriptors[2]->Token.Bits.PIDCode = PID_CODE_OUT_TOKEN;
720 m_TransferDescriptors[2]->Token.Bits.TotalBytesToTransfer = 0;
721
722 //
723 // link descriptors
724 //
725 m_TransferDescriptors[0]->NextPointer = m_TransferDescriptors[1]->PhysicalAddr;
726
727 //
728 // special case, setup alternative next descriptor in case of error
729 // HAIKU links to dead descriptor
730 //
731 m_TransferDescriptors[0]->AlternateNextPointer = m_TransferDescriptors[2]->PhysicalAddr;
732 m_TransferDescriptors[1]->NextPointer = m_TransferDescriptors[2]->PhysicalAddr;
733 m_TransferDescriptors[1]->AlternateNextPointer = m_TransferDescriptors[2]->PhysicalAddr;
734
735 //
736 // interrupt on completion
737 //
738 m_TransferDescriptors[2]->Token.Bits.InterruptOnComplete = TRUE;
739
740 }
741 else
742 {
743 //
744 // no buffer, setup in descriptor
745 //
746 m_TransferDescriptors[1]->Token.Bits.PIDCode = PID_CODE_IN_TOKEN;
747 m_TransferDescriptors[1]->Token.Bits.TotalBytesToTransfer = 0;
748
749 //
750 // link descriptors
751 //
752 m_TransferDescriptors[0]->NextPointer = m_TransferDescriptors[1]->PhysicalAddr;
753 m_TransferDescriptors[0]->AlternateNextPointer = m_TransferDescriptors[1]->PhysicalAddr;
754
755 //
756 // interrupt on completion
757 //
758 m_TransferDescriptors[1]->Token.Bits.InterruptOnComplete = TRUE;
759 }
760
761 //
762 // link setup packet into buffer - Physical Address!!!
763 //
764 m_TransferDescriptors[0]->BufferPointer[0] = (ULONG)PtrToUlong(m_DescriptorSetupPacket.LowPart);
765
766 //
767 // link transfer descriptors to queue head
768 //
769 QueueHead->NextPointer = m_TransferDescriptors[0]->PhysicalAddr;
770
771 //
772 // store result
773 //
774 *OutHead = QueueHead;
775
776 //
777 // done
778 //
779 return STATUS_SUCCESS;
780 }
781
782 //----------------------------------------------------------------------------------------
783 NTSTATUS
784 CUSBRequest::BuildBulkTransferQueueHead(
785 PQUEUE_HEAD * OutHead)
786 {
787 NTSTATUS Status;
788 PQUEUE_HEAD QueueHead;
789 ULONG TransferDescriptorCount, Index;
790 ULONG BytesAvailable, BufferIndex;
791 PVOID Base;
792 ULONG PageOffset, CurrentTransferBufferLength;
793
794 //
795 // Allocate the queue head
796 //
797 Status = CreateQueueHead(&QueueHead);
798
799 if (!NT_SUCCESS(Status))
800 {
801 //
802 // failed to allocate queue heads
803 //
804 return STATUS_INSUFFICIENT_RESOURCES;
805 }
806
807 //
808 // sanity checks
809 //
810 PC_ASSERT(QueueHead);
811 PC_ASSERT(m_TransferBufferLength);
812
813 //
814 // Max default of 3 descriptors
815 //
816 TransferDescriptorCount = 3;
817
818 //
819 // get virtual base of mdl
820 //
821 Base = MmGetSystemAddressForMdlSafe(m_TransferBufferMDL, NormalPagePriority);
822
823 //
824 // Increase the size of last transfer, 0 in case this is the first
825 //
826 Base = (PVOID)((ULONG_PTR)Base + m_TransferBufferLengthCompleted);
827
828 PC_ASSERT(m_EndpointDescriptor);
829 PC_ASSERT(Base);
830
831 //
832 // Get the offset from page size
833 //
834 PageOffset = BYTE_OFFSET(Base);
835
836 //
837 // PageOffset should only be > 0 if this is the first transfer for this requests
838 //
839 if ((PageOffset != 0) && (m_TransferBufferLengthCompleted != 0))
840 {
841 ASSERT(FALSE);
842 }
843
844 //
845 // Calculate the size of this transfer
846 //
847 if ((PageOffset != 0) && ((m_TransferBufferLength - m_TransferBufferLengthCompleted) >= (PAGE_SIZE * 4) + PageOffset))
848 {
849 CurrentTransferBufferLength = (PAGE_SIZE * 4) + PageOffset;
850 }
851 else if ((m_TransferBufferLength - m_TransferBufferLengthCompleted) >= PAGE_SIZE * 5)
852 {
853 CurrentTransferBufferLength = PAGE_SIZE * 5;
854 }
855 else
856 CurrentTransferBufferLength = (m_TransferBufferLength - m_TransferBufferLengthCompleted);
857
858 //
859 // Add current transfer length to transfer length completed
860 //
861 m_TransferBufferLengthCompleted += CurrentTransferBufferLength;
862 BytesAvailable = CurrentTransferBufferLength;
863 DPRINT("CurrentTransferBufferLength %x, m_TransferBufferLengthCompleted %x\n", CurrentTransferBufferLength, m_TransferBufferLengthCompleted);
864
865 DPRINT("EndPointAddress %x\n", m_EndpointDescriptor->bEndpointAddress);
866 DPRINT("EndPointDirection %x\n", USB_ENDPOINT_DIRECTION_IN(m_EndpointDescriptor->bEndpointAddress));
867
868 DPRINT("Request %p Base Address %p TransferBytesLength %lu MDL %p\n", this, Base, BytesAvailable, m_TransferBufferMDL);
869 DPRINT("InternalGetPidDirection() %d EndPointAddress %x\n", InternalGetPidDirection(), m_EndpointDescriptor->bEndpointAddress & 0x0F);
870 DPRINT("Irp %p QueueHead %p\n", m_Irp, QueueHead);
871
872 //PC_ASSERT(InternalGetPidDirection() == USB_ENDPOINT_DIRECTION_IN(m_EndpointDescriptor->bEndpointAddress));
873
874 //
875 // Allocated transfer descriptors
876 //
877 for (Index = 0; Index < TransferDescriptorCount; Index++)
878 {
879 Status = CreateDescriptor(&m_TransferDescriptors[Index]);
880 if (!NT_SUCCESS(Status))
881 {
882 //
883 // Failed to allocate transfer descriptors
884 //
885
886 //
887 // Free QueueHead
888 //
889 FreeQueueHead(QueueHead);
890
891 //
892 // Free Descriptors
893 // FIXME: Implement FreeDescriptors
894 //
895 return Status;
896 }
897
898 //
899 // sanity check
900 //
901 PC_ASSERT(BytesAvailable);
902
903 //
904 // now setup transfer buffers
905 //
906 for(BufferIndex = 0; BufferIndex < 5; BufferIndex++)
907 {
908 //
909 // If this is the first buffer of the first descriptor and there is a PageOffset
910 //
911 if ((BufferIndex == 0) && (PageOffset != 0) && (Index == 0))
912 {
913 //
914 // use physical address
915 //
916 m_TransferDescriptors[Index]->BufferPointer[0] = MmGetPhysicalAddress(Base).LowPart;
917
918 //
919 // move to next page
920 //
921 Base = (PVOID)ROUND_TO_PAGES(Base);
922
923 //
924 // increment transfer bytes
925 //
926 if (CurrentTransferBufferLength > PAGE_SIZE - PageOffset)
927 m_TransferDescriptors[Index]->Token.Bits.TotalBytesToTransfer = PAGE_SIZE - PageOffset;
928 else
929 m_TransferDescriptors[Index]->Token.Bits.TotalBytesToTransfer = CurrentTransferBufferLength;
930
931 //
932 // decrement available byte count
933 //
934 BytesAvailable -= m_TransferDescriptors[Index]->Token.Bits.TotalBytesToTransfer;
935
936 DPRINT("TransferDescriptor %p BufferPointer %p BufferIndex %lu TotalBytes %lu Remaining %lu\n", m_TransferDescriptors[Index], m_TransferDescriptors[Index]->BufferPointer[BufferIndex],
937 BufferIndex, m_TransferDescriptors[Index]->Token.Bits.TotalBytesToTransfer, BytesAvailable);
938 }
939 else
940 {
941 //
942 // the following pages always start on byte zero of each page
943 //
944 PC_ASSERT(((ULONG_PTR)Base & (PAGE_SIZE-1)) == 0);
945
946 if (BytesAvailable >= PAGE_SIZE)
947 {
948 //
949 // store address
950 //
951 m_TransferDescriptors[Index]->BufferPointer[BufferIndex] = MmGetPhysicalAddress(Base).LowPart;
952
953 //
954 // move to next page
955 //
956 Base = (PVOID)((ULONG_PTR)Base + PAGE_SIZE);
957
958 //
959 // increment transfer descriptor bytes
960 //
961 m_TransferDescriptors[Index]->Token.Bits.TotalBytesToTransfer += PAGE_SIZE;
962
963 //
964 // decrement available byte count
965 //
966 BytesAvailable -= PAGE_SIZE;
967
968 DPRINT("TransferDescriptor %p BufferPointer %p BufferIndex %lu TotalBytes %lu Remaining %lu\n", m_TransferDescriptors[Index], m_TransferDescriptors[Index]->BufferPointer[BufferIndex],
969 BufferIndex, m_TransferDescriptors[Index]->Token.Bits.TotalBytesToTransfer, BytesAvailable);
970 }
971 else
972 {
973 PC_ASSERT(BytesAvailable);
974
975 //
976 // store address
977 //
978 m_TransferDescriptors[Index]->BufferPointer[BufferIndex] = MmGetPhysicalAddress(Base).LowPart;
979
980 //
981 // increment transfer descriptor bytes
982 //
983 m_TransferDescriptors[Index]->Token.Bits.TotalBytesToTransfer += BytesAvailable;
984
985 //
986 // decrement available byte count
987 //
988 BytesAvailable -= BytesAvailable;
989
990 //
991 // done as this is the last partial or full page
992 //
993 DPRINT("TransferDescriptor %p BufferPointer %p BufferIndex %lu TotalBytes %lu Remaining %lu\n", m_TransferDescriptors[Index], m_TransferDescriptors[Index]->BufferPointer[BufferIndex],
994 BufferIndex, m_TransferDescriptors[Index]->Token.Bits.TotalBytesToTransfer, BytesAvailable);
995
996 break;
997 }
998 }
999
1000 //
1001 // Check if all bytes have been consumed
1002 //
1003 if (BytesAvailable == 0)
1004 break;
1005 }
1006
1007 //
1008 // store transfer bytes of descriptor
1009 //
1010 m_TransferDescriptors[Index]->TotalBytesToTransfer = m_TransferDescriptors[Index]->Token.Bits.TotalBytesToTransfer;
1011
1012 //
1013 // Go ahead and link descriptors
1014 //
1015 if (Index > 0)
1016 {
1017 m_TransferDescriptors[Index - 1]->NextPointer = m_TransferDescriptors[Index]->PhysicalAddr;
1018 }
1019
1020 //
1021 // setup direction
1022 //
1023 m_TransferDescriptors[Index]->Token.Bits.PIDCode = InternalGetPidDirection();
1024
1025 //
1026 // FIXME: performance penality?
1027 //
1028 m_TransferDescriptors[Index]->Token.Bits.InterruptOnComplete = TRUE;
1029
1030 //
1031 // FIXME need dead queue transfer descriptor?
1032 //
1033
1034 //
1035 // Check if all bytes have been consumed
1036 //
1037 if (BytesAvailable == 0)
1038 break;
1039 }
1040
1041 //
1042 // all bytes should have been consumed
1043 //
1044 PC_ASSERT(BytesAvailable == 0);
1045
1046 //
1047 // Initialize the QueueHead
1048 //
1049 QueueHead->EndPointCharacteristics.DeviceAddress = GetDeviceAddress();
1050
1051 if (m_EndpointDescriptor)
1052 {
1053 //
1054 // Set endpoint address and max packet length
1055 //
1056 QueueHead->EndPointCharacteristics.EndPointNumber = m_EndpointDescriptor->bEndpointAddress & 0x0F;
1057 QueueHead->EndPointCharacteristics.MaximumPacketLength = m_EndpointDescriptor->wMaxPacketSize;
1058 }
1059
1060 QueueHead->Token.Bits.DataToggle = TRUE;
1061
1062 //
1063 // link descriptor with queue head
1064 //
1065 QueueHead->NextPointer = m_TransferDescriptors[0]->PhysicalAddr;
1066
1067 //
1068 // store result
1069 //
1070 *OutHead = QueueHead;
1071
1072 //
1073 // done
1074 //
1075 return STATUS_SUCCESS;
1076 }
1077
1078 //----------------------------------------------------------------------------------------
1079 NTSTATUS
1080 CUSBRequest::CreateDescriptor(
1081 PQUEUE_TRANSFER_DESCRIPTOR *OutDescriptor)
1082 {
1083 PQUEUE_TRANSFER_DESCRIPTOR Descriptor;
1084 NTSTATUS Status;
1085 PHYSICAL_ADDRESS TransferDescriptorPhysicalAddress;
1086
1087 //
1088 // allocate descriptor
1089 //
1090 Status = m_DmaManager->Allocate(sizeof(QUEUE_TRANSFER_DESCRIPTOR), (PVOID*)&Descriptor, &TransferDescriptorPhysicalAddress);
1091 if (!NT_SUCCESS(Status))
1092 {
1093 //
1094 // failed to allocate transfer descriptor
1095 //
1096 return STATUS_INSUFFICIENT_RESOURCES;
1097 }
1098
1099 //
1100 // initialize transfer descriptor
1101 //
1102 Descriptor->NextPointer = TERMINATE_POINTER;
1103 Descriptor->AlternateNextPointer = TERMINATE_POINTER;
1104 Descriptor->Token.Bits.DataToggle = TRUE;
1105 Descriptor->Token.Bits.ErrorCounter = 0x03;
1106 Descriptor->Token.Bits.Active = TRUE;
1107 Descriptor->PhysicalAddr = TransferDescriptorPhysicalAddress.LowPart;
1108
1109 //
1110 // store result
1111 //
1112 *OutDescriptor = Descriptor;
1113
1114 //
1115 // done
1116 //
1117 return Status;
1118 }
1119
1120 //----------------------------------------------------------------------------------------
1121 NTSTATUS
1122 CUSBRequest::CreateQueueHead(
1123 PQUEUE_HEAD *OutQueueHead)
1124 {
1125 PQUEUE_HEAD QueueHead;
1126 PHYSICAL_ADDRESS QueueHeadPhysicalAddress;
1127 NTSTATUS Status;
1128
1129 //
1130 // allocate queue head
1131 //
1132 Status = m_DmaManager->Allocate(sizeof(QUEUE_HEAD), (PVOID*)&QueueHead, &QueueHeadPhysicalAddress);
1133
1134 if (!NT_SUCCESS(Status))
1135 {
1136 //
1137 // failed to allocate queue head
1138 //
1139 return STATUS_INSUFFICIENT_RESOURCES;
1140 }
1141
1142 //
1143 // initialize queue head
1144 //
1145 QueueHead->HorizontalLinkPointer = TERMINATE_POINTER;
1146 QueueHead->AlternateNextPointer = TERMINATE_POINTER;
1147 QueueHead->NextPointer = TERMINATE_POINTER;
1148
1149 //
1150 // 1 for non high speed, 0 for high speed device
1151 //
1152 QueueHead->EndPointCharacteristics.ControlEndPointFlag = 0;
1153 QueueHead->EndPointCharacteristics.HeadOfReclamation = FALSE;
1154 QueueHead->EndPointCharacteristics.MaximumPacketLength = 64;
1155
1156 //
1157 // Set NakCountReload to max value possible
1158 //
1159 QueueHead->EndPointCharacteristics.NakCountReload = 0xF;
1160
1161 //
1162 // Get the Initial Data Toggle from the QEDT
1163 //
1164 QueueHead->EndPointCharacteristics.QEDTDataToggleControl = FALSE;
1165
1166 //
1167 // FIXME: check if High Speed Device
1168 //
1169 QueueHead->EndPointCharacteristics.EndPointSpeed = QH_ENDPOINT_HIGHSPEED;
1170 QueueHead->EndPointCapabilities.NumberOfTransactionPerFrame = 0x03;
1171 QueueHead->Token.DWord = 0;
1172 QueueHead->Token.Bits.InterruptOnComplete = FALSE;
1173
1174 //
1175 // FIXME check if that is really needed
1176 //
1177 QueueHead->PhysicalAddr = QueueHeadPhysicalAddress.LowPart;
1178
1179 //
1180 // output queue head
1181 //
1182 *OutQueueHead = QueueHead;
1183
1184 //
1185 // done
1186 //
1187 return STATUS_SUCCESS;
1188 }
1189
1190 //----------------------------------------------------------------------------------------
1191 UCHAR
1192 CUSBRequest::GetDeviceAddress()
1193 {
1194 PIO_STACK_LOCATION IoStack;
1195 PURB Urb;
1196 PUSBDEVICE UsbDevice;
1197
1198 //
1199 // check if there is an irp provided
1200 //
1201 if (!m_Irp)
1202 {
1203 //
1204 // used provided address
1205 //
1206 return m_DeviceAddress;
1207 }
1208
1209 //
1210 // get current stack location
1211 //
1212 IoStack = IoGetCurrentIrpStackLocation(m_Irp);
1213
1214 //
1215 // get contained urb
1216 //
1217 Urb = (PURB)IoStack->Parameters.Others.Argument1;
1218
1219 //
1220 // check if there is a pipe handle provided
1221 //
1222 if (Urb->UrbHeader.UsbdDeviceHandle)
1223 {
1224 //
1225 // there is a device handle provided
1226 //
1227 UsbDevice = (PUSBDEVICE)Urb->UrbHeader.UsbdDeviceHandle;
1228
1229 //
1230 // return device address
1231 //
1232 return UsbDevice->GetDeviceAddress();
1233 }
1234
1235 //
1236 // no device handle provided, it is the host root bus
1237 //
1238 return 0;
1239 }
1240
1241 //----------------------------------------------------------------------------------------
1242 NTSTATUS
1243 CUSBRequest::BuildSetupPacket()
1244 {
1245 NTSTATUS Status;
1246
1247 //
1248 // allocate common buffer setup packet
1249 //
1250 Status = m_DmaManager->Allocate(sizeof(USB_DEFAULT_PIPE_SETUP_PACKET), (PVOID*)&m_DescriptorPacket, &m_DescriptorSetupPacket);
1251 if (!NT_SUCCESS(Status))
1252 {
1253 //
1254 // no memory
1255 //
1256 return Status;
1257 }
1258
1259 if (m_SetupPacket)
1260 {
1261 //
1262 // copy setup packet
1263 //
1264 RtlCopyMemory(m_DescriptorPacket, m_SetupPacket, sizeof(USB_DEFAULT_PIPE_SETUP_PACKET));
1265 }
1266 else
1267 {
1268 //
1269 // build setup packet from urb
1270 //
1271 Status = BuildSetupPacketFromURB();
1272 }
1273
1274 //
1275 // done
1276 //
1277 return Status;
1278 }
1279
1280
1281 NTSTATUS
1282 CUSBRequest::BuildSetupPacketFromURB()
1283 {
1284 PIO_STACK_LOCATION IoStack;
1285 PURB Urb;
1286 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
1287
1288 //
1289 // sanity checks
1290 //
1291 PC_ASSERT(m_Irp);
1292 PC_ASSERT(m_DescriptorPacket);
1293
1294 //
1295 // get stack location
1296 //
1297 IoStack = IoGetCurrentIrpStackLocation(m_Irp);
1298
1299 //
1300 // get urb
1301 //
1302 Urb = (PURB)IoStack->Parameters.Others.Argument1;
1303
1304 //
1305 // zero descriptor packet
1306 //
1307 RtlZeroMemory(m_DescriptorPacket, sizeof(USB_DEFAULT_PIPE_SETUP_PACKET));
1308
1309
1310 switch (Urb->UrbHeader.Function)
1311 {
1312 /* CLEAR FEATURE */
1313 case URB_FUNCTION_CLEAR_FEATURE_TO_DEVICE:
1314 case URB_FUNCTION_CLEAR_FEATURE_TO_INTERFACE:
1315 case URB_FUNCTION_CLEAR_FEATURE_TO_ENDPOINT:
1316 UNIMPLEMENTED
1317 break;
1318
1319 /* GET CONFIG */
1320 case URB_FUNCTION_GET_CONFIGURATION:
1321 m_DescriptorPacket->bRequest = USB_REQUEST_GET_CONFIGURATION;
1322 m_DescriptorPacket->bmRequestType.B = 0x80;
1323 m_DescriptorPacket->wLength = 1;
1324 break;
1325
1326 /* GET DESCRIPTOR */
1327 case URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE:
1328 m_DescriptorPacket->bRequest = USB_REQUEST_GET_DESCRIPTOR;
1329 m_DescriptorPacket->wValue.LowByte = Urb->UrbControlDescriptorRequest.Index;
1330 m_DescriptorPacket->wValue.HiByte = Urb->UrbControlDescriptorRequest.DescriptorType;
1331 m_DescriptorPacket->wIndex.W = Urb->UrbControlDescriptorRequest.LanguageId;
1332 m_DescriptorPacket->wLength = Urb->UrbControlDescriptorRequest.TransferBufferLength;
1333 m_DescriptorPacket->bmRequestType.B = 0x80;
1334 break;
1335
1336 /* GET INTERFACE */
1337 case URB_FUNCTION_GET_INTERFACE:
1338 m_DescriptorPacket->bRequest = USB_REQUEST_GET_CONFIGURATION;
1339 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1340 m_DescriptorPacket->bmRequestType.B = 0x80;
1341 m_DescriptorPacket->wLength = 1;
1342 break;
1343
1344 /* GET STATUS */
1345 case URB_FUNCTION_GET_STATUS_FROM_DEVICE:
1346 m_DescriptorPacket->bRequest = USB_REQUEST_GET_STATUS;
1347 ASSERT(Urb->UrbControlGetStatusRequest.Index == 0);
1348 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1349 m_DescriptorPacket->bmRequestType.B = 0x80;
1350 m_DescriptorPacket->wLength = 2;
1351 break;
1352
1353 case URB_FUNCTION_GET_STATUS_FROM_INTERFACE:
1354 m_DescriptorPacket->bRequest = USB_REQUEST_GET_STATUS;
1355 ASSERT(Urb->UrbControlGetStatusRequest.Index != 0);
1356 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1357 m_DescriptorPacket->bmRequestType.B = 0x81;
1358 m_DescriptorPacket->wLength = 2;
1359 break;
1360
1361 case URB_FUNCTION_GET_STATUS_FROM_ENDPOINT:
1362 m_DescriptorPacket->bRequest = USB_REQUEST_GET_STATUS;
1363 ASSERT(Urb->UrbControlGetStatusRequest.Index != 0);
1364 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1365 m_DescriptorPacket->bmRequestType.B = 0x82;
1366 m_DescriptorPacket->wLength = 2;
1367 break;
1368
1369 /* SET ADDRESS */
1370
1371 /* SET CONFIG */
1372 case URB_FUNCTION_SELECT_CONFIGURATION:
1373 m_DescriptorPacket->bRequest = USB_REQUEST_SET_CONFIGURATION;
1374 m_DescriptorPacket->wValue.W = Urb->UrbSelectConfiguration.ConfigurationDescriptor->bConfigurationValue;
1375 m_DescriptorPacket->wIndex.W = 0;
1376 m_DescriptorPacket->wLength = 0;
1377 m_DescriptorPacket->bmRequestType.B = 0x00;
1378 break;
1379
1380 /* SET DESCRIPTOR */
1381 case URB_FUNCTION_SET_DESCRIPTOR_TO_DEVICE:
1382 case URB_FUNCTION_SET_DESCRIPTOR_TO_INTERFACE:
1383 case URB_FUNCTION_SET_DESCRIPTOR_TO_ENDPOINT:
1384 UNIMPLEMENTED
1385 break;
1386
1387 /* SET FEATURE */
1388 case URB_FUNCTION_SET_FEATURE_TO_DEVICE:
1389 m_DescriptorPacket->bRequest = USB_REQUEST_SET_FEATURE;
1390 ASSERT(Urb->UrbControlGetStatusRequest.Index == 0);
1391 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1392 m_DescriptorPacket->bmRequestType.B = 0x80;
1393 break;
1394
1395 case URB_FUNCTION_SET_FEATURE_TO_INTERFACE:
1396 m_DescriptorPacket->bRequest = USB_REQUEST_SET_FEATURE;
1397 ASSERT(Urb->UrbControlGetStatusRequest.Index == 0);
1398 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1399 m_DescriptorPacket->bmRequestType.B = 0x81;
1400 break;
1401
1402 case URB_FUNCTION_SET_FEATURE_TO_ENDPOINT:
1403 m_DescriptorPacket->bRequest = USB_REQUEST_SET_FEATURE;
1404 ASSERT(Urb->UrbControlGetStatusRequest.Index == 0);
1405 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1406 m_DescriptorPacket->bmRequestType.B = 0x82;
1407 break;
1408
1409 /* SET INTERFACE*/
1410 case URB_FUNCTION_SELECT_INTERFACE:
1411 m_DescriptorPacket->bRequest = USB_REQUEST_SET_INTERFACE;
1412 m_DescriptorPacket->wValue.W = Urb->UrbSelectInterface.Interface.AlternateSetting;
1413 m_DescriptorPacket->wIndex.W = Urb->UrbSelectInterface.Interface.InterfaceNumber;
1414 m_DescriptorPacket->wLength = 0;
1415 m_DescriptorPacket->bmRequestType.B = 0x01;
1416 break;
1417
1418 /* SYNC FRAME */
1419 case URB_FUNCTION_SYNC_RESET_PIPE_AND_CLEAR_STALL:
1420 UNIMPLEMENTED
1421 break;
1422 default:
1423 UNIMPLEMENTED
1424 break;
1425 }
1426
1427 return Status;
1428 }
1429
1430 //----------------------------------------------------------------------------------------
1431 VOID
1432 CUSBRequest::GetResultStatus(
1433 OUT OPTIONAL NTSTATUS * NtStatusCode,
1434 OUT OPTIONAL PULONG UrbStatusCode)
1435 {
1436 //
1437 // sanity check
1438 //
1439 PC_ASSERT(m_CompletionEvent);
1440
1441 //
1442 // wait for the operation to complete
1443 //
1444 KeWaitForSingleObject(m_CompletionEvent, Executive, KernelMode, FALSE, NULL);
1445
1446 //
1447 // copy status
1448 //
1449 if (NtStatusCode)
1450 {
1451 *NtStatusCode = m_NtStatusCode;
1452 }
1453
1454 //
1455 // copy urb status
1456 //
1457 if (UrbStatusCode)
1458 {
1459 *UrbStatusCode = m_UrbStatusCode;
1460 }
1461
1462 }
1463
1464
1465 //-----------------------------------------------------------------------------------------
1466 BOOLEAN
1467 CUSBRequest::IsRequestInitialized()
1468 {
1469 if (m_Irp || m_SetupPacket)
1470 {
1471 //
1472 // request is initialized
1473 //
1474 return TRUE;
1475 }
1476
1477 //
1478 // request is not initialized
1479 //
1480 return FALSE;
1481 }
1482
1483 //-----------------------------------------------------------------------------------------
1484 BOOLEAN
1485 CUSBRequest::ShouldReleaseRequestAfterCompletion()
1486 {
1487 if (m_Irp)
1488 {
1489 //
1490 // the request is completed, release it
1491 //
1492 return TRUE;
1493 }
1494 else
1495 {
1496 //
1497 // created with an setup packet, don't release
1498 //
1499 return FALSE;
1500 }
1501 }
1502
1503 //-----------------------------------------------------------------------------------------
1504 VOID
1505 CUSBRequest::FreeQueueHead(
1506 IN struct _QUEUE_HEAD * QueueHead)
1507 {
1508 LONG DescriptorCount;
1509
1510 //
1511 // FIXME: support chained queue heads
1512 //
1513 //PC_ASSERT(QueueHead == m_QueueHead);
1514
1515 //
1516 // release queue head
1517 //
1518 m_DmaManager->Release(QueueHead, sizeof(QUEUE_HEAD));
1519
1520 //
1521 // nullify pointer
1522 //
1523 m_QueueHead = 0;
1524
1525 //
1526 // release transfer descriptors
1527 //
1528 for (DescriptorCount = 0; DescriptorCount < 3; DescriptorCount++)
1529 {
1530 if (m_TransferDescriptors[DescriptorCount])
1531 {
1532 //
1533 // Calculate Total Bytes Transferred
1534 // FIXME: Is this the correct method of determine bytes transferred?
1535 //
1536 if (USB_ENDPOINT_TYPE_BULK == GetTransferType())
1537 {
1538 //
1539 // sanity check
1540 //
1541 ASSERT(m_EndpointDescriptor);
1542
1543 if (USB_ENDPOINT_DIRECTION_IN(m_EndpointDescriptor->bEndpointAddress))
1544 {
1545 DPRINT1("m_TotalBytesTransferred %x, %x - %x\n",
1546 m_TotalBytesTransferred,
1547 m_TransferDescriptors[DescriptorCount]->TotalBytesToTransfer,
1548 m_TransferDescriptors[DescriptorCount]->Token.Bits.TotalBytesToTransfer);
1549
1550 m_TotalBytesTransferred +=
1551 m_TransferDescriptors[DescriptorCount]->TotalBytesToTransfer -
1552 m_TransferDescriptors[DescriptorCount]->Token.Bits.TotalBytesToTransfer;
1553 }
1554 }
1555
1556 //
1557 // release transfer descriptors
1558 //
1559 m_DmaManager->Release(m_TransferDescriptors[DescriptorCount], sizeof(QUEUE_TRANSFER_DESCRIPTOR));
1560 m_TransferDescriptors[DescriptorCount] = 0;
1561 }
1562 }
1563
1564 if (m_DescriptorPacket)
1565 {
1566 //
1567 // release packet descriptor
1568 //
1569 m_DmaManager->Release(m_DescriptorPacket, sizeof(USB_DEFAULT_PIPE_SETUP_PACKET));
1570 m_DescriptorPacket = 0;
1571 }
1572 }
1573
1574 //-----------------------------------------------------------------------------------------
1575 BOOLEAN
1576 CUSBRequest::IsQueueHeadComplete(
1577 struct _QUEUE_HEAD * QueueHead)
1578 {
1579 ULONG Index;
1580
1581 //
1582 // first check - is the queue head currently active
1583 //
1584 if (QueueHead->Token.Bits.Active)
1585 {
1586 //
1587 // queue head is active (currently processed)
1588 //
1589 return FALSE;
1590 }
1591
1592 //
1593 // FIXME: support chained queue heads
1594 //
1595 for(Index = 0; Index < 3; Index++)
1596 {
1597 //
1598 // check transfer descriptors for completion
1599 //
1600 if (m_TransferDescriptors[Index])
1601 {
1602 //
1603 // check for serious error
1604 //
1605 //PC_ASSERT(m_TransferDescriptors[Index]->Token.Bits.Halted == 0);
1606
1607 //
1608 // the transfer descriptor should be in the same state as the queue head
1609 //
1610 //PC_ASSERT(m_TransferDescriptors[Index]->Token.Bits.Active == 0);
1611 }
1612 }
1613
1614 return TRUE;
1615 }
1616
1617 //-----------------------------------------------------------------------------------------
1618 VOID
1619 CUSBRequest::GetTransferBuffer(
1620 OUT PMDL * OutMDL,
1621 OUT PULONG TransferLength)
1622 {
1623 // sanity checks
1624 PC_ASSERT(OutMDL);
1625 PC_ASSERT(TransferLength);
1626
1627 *OutMDL = m_TransferBufferMDL;
1628 *TransferLength = m_TransferBufferLength;
1629 }
1630 //-----------------------------------------------------------------------------------------
1631 ULONG
1632 CUSBRequest::InternalCalculateTransferLength()
1633 {
1634 if (!m_Irp)
1635 {
1636 //
1637 // FIXME: get length for control request
1638 //
1639 return m_TransferBufferLength;
1640 }
1641
1642 //
1643 // sanity check
1644 //
1645 ASSERT(m_EndpointDescriptor);
1646
1647 if (USB_ENDPOINT_DIRECTION_IN(m_EndpointDescriptor->bEndpointAddress))
1648 {
1649 //
1650 // bulk in request
1651 // HACK: Properly determine transfer length
1652 //
1653 return m_TransferBufferLength;//m_TotalBytesTransferred;
1654 }
1655
1656 //
1657 // bulk out transfer
1658 //
1659 return m_TransferBufferLength;
1660 }
1661
1662 //-----------------------------------------------------------------------------------------
1663 NTSTATUS
1664 InternalCreateUSBRequest(
1665 PUSBREQUEST *OutRequest)
1666 {
1667 PUSBREQUEST This;
1668
1669 //
1670 // allocate requests
1671 //
1672 This = new(NonPagedPool, TAG_USBEHCI) CUSBRequest(0);
1673 if (!This)
1674 {
1675 //
1676 // failed to allocate
1677 //
1678 return STATUS_INSUFFICIENT_RESOURCES;
1679 }
1680
1681 //
1682 // add reference count
1683 //
1684 This->AddRef();
1685
1686 //
1687 // return result
1688 //
1689 *OutRequest = (PUSBREQUEST)This;
1690
1691 //
1692 // done
1693 //
1694 return STATUS_SUCCESS;
1695 }