[USBEHCI]
[reactos.git] / reactos / drivers / usb / usbehci / 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 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 NTSTATUS BuildTransferDescriptorChain(IN PQUEUE_HEAD QueueHead, IN PVOID TransferBuffer, IN ULONG TransferBufferLength, IN UCHAR PidCode, IN UCHAR InitialDataToggle, IN PQUEUE_TRANSFER_DESCRIPTOR AlternativeDescriptor, OUT PQUEUE_TRANSFER_DESCRIPTOR * OutFirstDescriptor, OUT PQUEUE_TRANSFER_DESCRIPTOR * OutLastDescriptor, OUT PUCHAR OutDataToggle, OUT PULONG OutTransferBufferOffset);
66 VOID InitDescriptor(IN PQUEUE_TRANSFER_DESCRIPTOR CurrentDescriptor, IN PVOID TransferBuffer, IN ULONG TransferBufferLength, IN UCHAR PidCode, IN UCHAR DataToggle, OUT PULONG OutDescriptorLength);
67 VOID DumpQueueHead(IN PQUEUE_HEAD QueueHead);
68
69 // constructor / destructor
70 CUSBRequest(IUnknown *OuterUnknown){}
71 virtual ~CUSBRequest(){}
72
73 protected:
74 LONG m_Ref;
75
76 //
77 // memory manager for allocating setup packet / queue head / transfer descriptors
78 //
79 PDMAMEMORYMANAGER m_DmaManager;
80
81 //
82 // caller provided irp packet containing URB request
83 //
84 PIRP m_Irp;
85
86 //
87 // transfer buffer length
88 //
89 ULONG m_TransferBufferLength;
90
91 //
92 // current transfer length
93 //
94 ULONG m_TransferBufferLengthCompleted;
95
96 //
97 // Total Transfer Length
98 //
99 ULONG m_TotalBytesTransferred;
100
101 //
102 // transfer buffer MDL
103 //
104 PMDL m_TransferBufferMDL;
105
106 //
107 // caller provided setup packet
108 //
109 PUSB_DEFAULT_PIPE_SETUP_PACKET m_SetupPacket;
110
111 //
112 // completion event for callers who initialized request with setup packet
113 //
114 PKEVENT m_CompletionEvent;
115
116 //
117 // device address for callers who initialized it with device address
118 //
119 UCHAR m_DeviceAddress;
120
121 //
122 // store end point address
123 //
124 PUSB_ENDPOINT m_EndpointDescriptor;
125
126 //
127 // DMA queue head
128 //
129 PQUEUE_HEAD m_QueueHead;
130
131 //
132 // allocated setup packet from the DMA pool
133 //
134 PUSB_DEFAULT_PIPE_SETUP_PACKET m_DescriptorPacket;
135 PHYSICAL_ADDRESS m_DescriptorSetupPacket;
136
137 //
138 // stores the result of the operation
139 //
140 NTSTATUS m_NtStatusCode;
141 ULONG m_UrbStatusCode;
142
143 PVOID m_Base;
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 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)Urb->UrbBulkOrInterruptTransfer.PipeHandle;
325
326 }
327 break;
328 }
329 default:
330 DPRINT1("URB Function: not supported %x\n", Urb->UrbHeader.Function);
331 //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->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 ASSERT(m_EndpointDescriptor == FALSE);
599 }
600
601 //
602 // done
603 //
604 return TransferType;
605 }
606
607 UCHAR
608 CUSBRequest::InternalGetPidDirection()
609 {
610 if (m_EndpointDescriptor)
611 {
612 //
613 // end point direction is highest bit in bEndpointAddress
614 //
615 return (m_EndpointDescriptor->EndPointDescriptor.bEndpointAddress & USB_ENDPOINT_DIRECTION_MASK) >> 7;
616 }
617 else
618 {
619 //
620 // request arrives on the control pipe, extract direction from setup packet
621 //
622 ASSERT(m_DescriptorPacket);
623 return (m_DescriptorPacket->bmRequestType.B >> 7);
624 }
625 }
626
627 VOID
628 CUSBRequest::InitDescriptor(
629 IN PQUEUE_TRANSFER_DESCRIPTOR CurrentDescriptor,
630 IN PVOID TransferBuffer,
631 IN ULONG TransferBufferLength,
632 IN UCHAR PidCode,
633 IN UCHAR DataToggle,
634 OUT PULONG OutDescriptorLength)
635 {
636 ULONG Index, Length = 0, PageOffset, BufferLength;
637 PHYSICAL_ADDRESS Address;
638
639 //
640 // init transfer descriptor
641 //
642 CurrentDescriptor->Token.Bits.PIDCode = PidCode;
643 CurrentDescriptor->Token.Bits.TotalBytesToTransfer = 0;
644 CurrentDescriptor->Token.Bits.DataToggle = DataToggle;
645
646 //
647 // sanity check
648 //
649 ASSERT(TransferBufferLength);
650
651 //
652 // store buffers
653 //
654 Index = 0;
655 do
656 {
657 //
658 // get address
659 //
660 Address = MmGetPhysicalAddress(TransferBuffer);
661
662 //
663 // use physical address
664 //
665 CurrentDescriptor->BufferPointer[Index] = Address.LowPart;
666 CurrentDescriptor->ExtendedBufferPointer[Index] = Address.HighPart;
667
668 //
669 // Get the offset from page size
670 //
671 PageOffset = BYTE_OFFSET(CurrentDescriptor->BufferPointer[Index]);
672 if (PageOffset != 0)
673 {
674 //
675 // move to next page
676 //
677 TransferBuffer = (PVOID)ROUND_TO_PAGES(TransferBuffer);
678 }
679 else
680 {
681 //
682 // move to next page
683 //
684 TransferBuffer = (PVOID)((ULONG_PTR)TransferBuffer + PAGE_SIZE);
685 }
686
687 //
688 // calculate buffer length
689 //
690 BufferLength = min(TransferBufferLength, PAGE_SIZE - PageOffset);
691
692 //
693 // increment transfer bytes
694 //
695 CurrentDescriptor->Token.Bits.TotalBytesToTransfer += BufferLength;
696 CurrentDescriptor->TotalBytesToTransfer += BufferLength;
697 Length += BufferLength;
698 DPRINT("Index %lu TransferBufferLength %lu PageOffset %x BufferLength %lu Buffer Phy %p TransferBuffer %p\n", Index, TransferBufferLength, PageOffset, BufferLength, CurrentDescriptor->BufferPointer[Index], TransferBuffer);
699
700 //
701 // decrement available byte count
702 //
703 TransferBufferLength -= BufferLength;
704 if (TransferBufferLength == 0)
705 {
706 //
707 // end reached
708 //
709 break;
710 }
711
712 //
713 // sanity check
714 //
715 if (Index > 1)
716 {
717 //
718 // no equal buffers
719 //
720 ASSERT(CurrentDescriptor->BufferPointer[Index] != CurrentDescriptor->BufferPointer[Index-1]);
721 }
722
723 //
724 // next descriptor index
725 //
726 Index++;
727 }while(Index < 5);
728
729 //
730 // store result
731 //
732 *OutDescriptorLength = Length;
733 }
734
735
736 NTSTATUS
737 CUSBRequest::BuildTransferDescriptorChain(
738 IN PQUEUE_HEAD QueueHead,
739 IN PVOID TransferBuffer,
740 IN ULONG TransferBufferLength,
741 IN UCHAR PidCode,
742 IN UCHAR InitialDataToggle,
743 IN PQUEUE_TRANSFER_DESCRIPTOR AlternativeDescriptor,
744 OUT PQUEUE_TRANSFER_DESCRIPTOR * OutFirstDescriptor,
745 OUT PQUEUE_TRANSFER_DESCRIPTOR * OutLastDescriptor,
746 OUT PUCHAR OutDataToggle,
747 OUT PULONG OutTransferBufferOffset)
748 {
749 PQUEUE_TRANSFER_DESCRIPTOR FirstDescriptor = NULL, CurrentDescriptor, LastDescriptor = NULL;
750 NTSTATUS Status;
751 ULONG DescriptorLength, TransferBufferOffset = 0;
752 ULONG MaxPacketSize = 0, TransferSize;
753
754 //
755 // is there an endpoint descriptor
756 //
757 if (m_EndpointDescriptor)
758 {
759 //
760 // use endpoint packet size
761 //
762 MaxPacketSize = m_EndpointDescriptor->EndPointDescriptor.wMaxPacketSize;
763 }
764
765 do
766 {
767 //
768 // allocate transfer descriptor
769 //
770 Status = CreateDescriptor(&CurrentDescriptor);
771 if (!NT_SUCCESS(Status))
772 {
773 //
774 // failed to allocate transfer descriptor
775 //
776 return STATUS_INSUFFICIENT_RESOURCES;
777 }
778
779 if (MaxPacketSize)
780 {
781 //
782 // transfer size is minimum available buffer or endpoint size
783 //
784 TransferSize = min(TransferBufferLength - TransferBufferOffset, MaxPacketSize);
785 }
786 else
787 {
788 //
789 // use available buffer
790 //
791 TransferSize = TransferBufferLength - TransferBufferOffset;
792 }
793
794 //
795 // now init the descriptor
796 //
797 InitDescriptor(CurrentDescriptor,
798 (PVOID)((ULONG_PTR)TransferBuffer + TransferBufferOffset),
799 TransferSize,
800 PidCode,
801 InitialDataToggle,
802 &DescriptorLength);
803
804 //
805 // insert into queue head
806 //
807 InsertTailList(&QueueHead->TransferDescriptorListHead, &CurrentDescriptor->DescriptorEntry);
808
809 //
810 // adjust offset
811 //
812 TransferBufferOffset += DescriptorLength;
813
814 if (LastDescriptor)
815 {
816 //
817 // link to current descriptor
818 //
819 LastDescriptor->NextPointer = CurrentDescriptor->PhysicalAddr;
820 LastDescriptor = CurrentDescriptor;
821
822 if (AlternativeDescriptor)
823 {
824 //
825 // link to alternative next pointer
826 //
827 LastDescriptor->AlternateNextPointer = AlternativeDescriptor->PhysicalAddr;
828 }
829
830 }
831 else
832 {
833 //
834 // first descriptor in chain
835 //
836 LastDescriptor = FirstDescriptor = CurrentDescriptor;
837 }
838
839 //
840 // flip data toggle
841 //
842 InitialDataToggle = !InitialDataToggle;
843
844 if(TransferBufferLength == TransferBufferOffset)
845 {
846 //
847 // end reached
848 //
849 break;
850 }
851
852 }while(TRUE);
853
854 if (OutFirstDescriptor)
855 {
856 //
857 // store first descriptor
858 //
859 *OutFirstDescriptor = FirstDescriptor;
860 }
861
862 if (OutLastDescriptor)
863 {
864 //
865 // store last descriptor
866 //
867 *OutLastDescriptor = CurrentDescriptor;
868 }
869
870 if (OutDataToggle)
871 {
872 //
873 // store result data toggle
874 //
875 *OutDataToggle = InitialDataToggle;
876 }
877
878 if (OutTransferBufferOffset)
879 {
880 //
881 // store offset
882 //
883 *OutTransferBufferOffset = TransferBufferOffset;
884 }
885
886 //
887 // done
888 //
889 return STATUS_SUCCESS;
890 }
891
892 //----------------------------------------------------------------------------------------
893 NTSTATUS
894 CUSBRequest::BuildControlTransferQueueHead(
895 PQUEUE_HEAD * OutHead)
896 {
897 NTSTATUS Status;
898 ULONG DescriptorChainLength;
899 PQUEUE_HEAD QueueHead;
900 PQUEUE_TRANSFER_DESCRIPTOR SetupDescriptor, StatusDescriptor, FirstDescriptor, LastDescriptor;
901
902 //
903 // first allocate the queue head
904 //
905 Status = CreateQueueHead(&QueueHead);
906 if (!NT_SUCCESS(Status))
907 {
908 //
909 // failed to allocate queue head
910 //
911 return STATUS_INSUFFICIENT_RESOURCES;
912 }
913
914 //
915 // sanity check
916 //
917 PC_ASSERT(QueueHead);
918
919 //
920 // create setup packet
921 //
922 Status = BuildSetupPacket();
923 if (!NT_SUCCESS(Status))
924 {
925 //
926 // failed to allocate setup packet
927 //
928 ASSERT(FALSE);
929 return STATUS_INSUFFICIENT_RESOURCES;
930 }
931
932 //
933 // create setup descriptor
934 //
935 Status = CreateDescriptor(&SetupDescriptor);
936 if (!NT_SUCCESS(Status))
937 {
938 //
939 // failed to allocate transfer descriptor
940 //
941 ASSERT(FALSE);
942 return Status;
943 }
944
945 //
946 // create status descriptor
947 //
948 Status = CreateDescriptor(&StatusDescriptor);
949 if (!NT_SUCCESS(Status))
950 {
951 //
952 // failed to allocate transfer descriptor
953 //
954 ASSERT(FALSE);
955 return Status;
956 }
957
958 //
959 // now initialize the queue head
960 //
961 QueueHead->EndPointCharacteristics.DeviceAddress = GetDeviceAddress();
962
963 ASSERT(m_EndpointDescriptor == FALSE);
964
965 //
966 // init setup descriptor
967 //
968 SetupDescriptor->Token.Bits.PIDCode = PID_CODE_SETUP_TOKEN;
969 SetupDescriptor->Token.Bits.TotalBytesToTransfer = sizeof(USB_DEFAULT_PIPE_SETUP_PACKET);
970 SetupDescriptor->Token.Bits.DataToggle = FALSE;
971 SetupDescriptor->BufferPointer[0] = m_DescriptorSetupPacket.LowPart;
972 SetupDescriptor->ExtendedBufferPointer[0] = m_DescriptorSetupPacket.HighPart;
973 InsertTailList(&QueueHead->TransferDescriptorListHead, &SetupDescriptor->DescriptorEntry);
974
975
976 //
977 // init status descriptor
978 //
979 StatusDescriptor->Token.Bits.TotalBytesToTransfer = 0;
980 StatusDescriptor->Token.Bits.DataToggle = TRUE;
981 StatusDescriptor->Token.Bits.InterruptOnComplete = TRUE;
982
983 //
984 // is there data
985 //
986 if (m_TransferBufferLength)
987 {
988 Status = BuildTransferDescriptorChain(QueueHead,
989 MmGetMdlVirtualAddress(m_TransferBufferMDL),
990 m_TransferBufferLength,
991 InternalGetPidDirection(),
992 TRUE,
993 NULL,
994 &FirstDescriptor,
995 &LastDescriptor,
996 NULL,
997 &DescriptorChainLength);
998
999 //
1000 // FIXME handle errors
1001 //
1002 ASSERT(Status == STATUS_SUCCESS);
1003 ASSERT(DescriptorChainLength == m_TransferBufferLength);
1004
1005 //
1006 // now link the descriptors
1007 //
1008 SetupDescriptor->NextPointer = FirstDescriptor->PhysicalAddr;
1009 SetupDescriptor->AlternateNextPointer = FirstDescriptor->PhysicalAddr;
1010 LastDescriptor->NextPointer = StatusDescriptor->PhysicalAddr;
1011 LastDescriptor->AlternateNextPointer = StatusDescriptor->PhysicalAddr;
1012
1013
1014 //
1015 // pid code is flipped for ops with data stage
1016 //
1017 StatusDescriptor->Token.Bits.PIDCode = !InternalGetPidDirection();
1018 }
1019 else
1020 {
1021 //
1022 // direct link
1023 //
1024 SetupDescriptor->NextPointer = StatusDescriptor->PhysicalAddr;
1025 SetupDescriptor->AlternateNextPointer = StatusDescriptor->PhysicalAddr;
1026
1027 //
1028 // retrieve result of operation
1029 //
1030 StatusDescriptor->Token.Bits.PIDCode = PID_CODE_IN_TOKEN;
1031 }
1032
1033 //
1034 // insert status descriptor
1035 //
1036 InsertTailList(&QueueHead->TransferDescriptorListHead, &StatusDescriptor->DescriptorEntry);
1037
1038
1039 //
1040 // link transfer descriptors to queue head
1041 //
1042 QueueHead->NextPointer = SetupDescriptor->PhysicalAddr;
1043
1044 //
1045 // store result
1046 //
1047 *OutHead = QueueHead;
1048
1049 //
1050 // displays the current request
1051 //
1052 //DumpQueueHead(QueueHead);
1053
1054 DPRINT("BuildControlTransferQueueHead done\n");
1055 //
1056 // done
1057 //
1058 return STATUS_SUCCESS;
1059 }
1060
1061 VOID
1062 CUSBRequest::DumpQueueHead(
1063 IN PQUEUE_HEAD QueueHead)
1064 {
1065 PLIST_ENTRY Entry;
1066 PQUEUE_TRANSFER_DESCRIPTOR Descriptor;
1067 ULONG Index = 0;
1068
1069 DPRINT1("QueueHead %p Addr %x\n", QueueHead, QueueHead->PhysicalAddr);
1070 DPRINT1("QueueHead AlternateNextPointer %x\n", QueueHead->AlternateNextPointer);
1071 DPRINT1("QueueHead NextPointer %x\n", QueueHead->NextPointer);
1072
1073 DPRINT1("QueueHead HubAddr %x\n", QueueHead->EndPointCharacteristics.ControlEndPointFlag);
1074 DPRINT1("QueueHead DeviceAddress %x\n", QueueHead->EndPointCharacteristics.DeviceAddress);
1075 DPRINT1("QueueHead EndPointNumber %x\n", QueueHead->EndPointCharacteristics.EndPointNumber);
1076 DPRINT1("QueueHead EndPointSpeed %x\n", QueueHead->EndPointCharacteristics.EndPointSpeed);
1077 DPRINT1("QueueHead HeadOfReclamation %x\n", QueueHead->EndPointCharacteristics.HeadOfReclamation);
1078 DPRINT1("QueueHead InactiveOnNextTransaction %x\n", QueueHead->EndPointCharacteristics.InactiveOnNextTransaction);
1079 DPRINT1("QueueHead MaximumPacketLength %x\n", QueueHead->EndPointCharacteristics.MaximumPacketLength);
1080 DPRINT1("QueueHead NakCountReload %x\n", QueueHead->EndPointCharacteristics.NakCountReload);
1081 DPRINT1("QueueHead QEDTDataToggleControl %x\n", QueueHead->EndPointCharacteristics.QEDTDataToggleControl);
1082 DPRINT1("QueueHead HubAddr %x\n", QueueHead->EndPointCapabilities.HubAddr);
1083 DPRINT1("QueueHead InterruptScheduleMask %x\n", QueueHead->EndPointCapabilities.InterruptScheduleMask);
1084 DPRINT1("QueueHead NumberOfTransactionPerFrame %x\n", QueueHead->EndPointCapabilities.NumberOfTransactionPerFrame);
1085 DPRINT1("QueueHead PortNumber %x\n", QueueHead->EndPointCapabilities.PortNumber);
1086 DPRINT1("QueueHead SplitCompletionMask %x\n", QueueHead->EndPointCapabilities.SplitCompletionMask);
1087
1088 Entry = QueueHead->TransferDescriptorListHead.Flink;
1089 while(Entry != &QueueHead->TransferDescriptorListHead)
1090 {
1091 //
1092 // get transfer descriptor
1093 //
1094 Descriptor = (PQUEUE_TRANSFER_DESCRIPTOR)CONTAINING_RECORD(Entry, QUEUE_TRANSFER_DESCRIPTOR, DescriptorEntry);
1095
1096 DPRINT1("TransferDescriptor %lu Addr %x\n", Index, Descriptor->PhysicalAddr);
1097 DPRINT1("TransferDescriptor %lu Next %x\n", Index, Descriptor->NextPointer);
1098 DPRINT1("TransferDescriptor %lu AlternateNextPointer %x\n", Index, Descriptor->AlternateNextPointer);
1099 DPRINT1("TransferDescriptor %lu Active %lu\n", Index, Descriptor->Token.Bits.Active);
1100 DPRINT1("TransferDescriptor %lu BabbleDetected %lu\n", Index, Descriptor->Token.Bits.BabbleDetected);
1101 DPRINT1("TransferDescriptor %lu CurrentPage %lu\n", Index, Descriptor->Token.Bits.CurrentPage);
1102 DPRINT1("TransferDescriptor %lu DataBufferError %lu\n", Index, Descriptor->Token.Bits.DataBufferError);
1103 DPRINT1("TransferDescriptor %lu DataToggle %lu\n", Index, Descriptor->Token.Bits.DataToggle);
1104 DPRINT1("TransferDescriptor %lu ErrorCounter %lu\n", Index, Descriptor->Token.Bits.ErrorCounter);
1105 DPRINT1("TransferDescriptor %lu Halted %lu\n", Index, Descriptor->Token.Bits.Halted);
1106 DPRINT1("TransferDescriptor %lu InterruptOnComplete %x\n", Index, Descriptor->Token.Bits.InterruptOnComplete);
1107 DPRINT1("TransferDescriptor %lu MissedMicroFrame %lu\n", Index, Descriptor->Token.Bits.MissedMicroFrame);
1108 DPRINT1("TransferDescriptor %lu PIDCode %lu\n", Index, Descriptor->Token.Bits.PIDCode);
1109 DPRINT1("TransferDescriptor %lu PingState %lu\n", Index, Descriptor->Token.Bits.PingState);
1110 DPRINT1("TransferDescriptor %lu SplitTransactionState %lu\n", Index, Descriptor->Token.Bits.SplitTransactionState);
1111 DPRINT1("TransferDescriptor %lu TotalBytesToTransfer %lu\n", Index, Descriptor->Token.Bits.TotalBytesToTransfer);
1112 DPRINT1("TransferDescriptor %lu TransactionError %lu\n", Index, Descriptor->Token.Bits.TransactionError);
1113
1114 DPRINT1("TransferDescriptor %lu Buffer Pointer 0 %x\n", Index, Descriptor->BufferPointer[0]);
1115 DPRINT1("TransferDescriptor %lu Buffer Pointer 1 %x\n", Index, Descriptor->BufferPointer[1]);
1116 DPRINT1("TransferDescriptor %lu Buffer Pointer 2 %x\n", Index, Descriptor->BufferPointer[2]);
1117 DPRINT1("TransferDescriptor %lu Buffer Pointer 3 %x\n", Index, Descriptor->BufferPointer[3]);
1118 DPRINT1("TransferDescriptor %lu Buffer Pointer 4 %x\n", Index, Descriptor->BufferPointer[4]);
1119 Entry = Entry->Flink;
1120 Index++;
1121 }
1122 }
1123
1124
1125 //----------------------------------------------------------------------------------------
1126 NTSTATUS
1127 CUSBRequest::BuildBulkTransferQueueHead(
1128 PQUEUE_HEAD * OutHead)
1129 {
1130 NTSTATUS Status;
1131 PQUEUE_HEAD QueueHead;
1132 PVOID Base;
1133 ULONG ChainDescriptorLength;
1134 PQUEUE_TRANSFER_DESCRIPTOR FirstDescriptor, LastDescriptor;
1135
1136 //
1137 // Allocate the queue head
1138 //
1139 Status = CreateQueueHead(&QueueHead);
1140
1141 if (!NT_SUCCESS(Status))
1142 {
1143 //
1144 // failed to allocate queue heads
1145 //
1146 return STATUS_INSUFFICIENT_RESOURCES;
1147 }
1148
1149 //
1150 // sanity checks
1151 //
1152 PC_ASSERT(QueueHead);
1153 PC_ASSERT(m_TransferBufferLength);
1154
1155 if (!m_Base)
1156 {
1157 //
1158 // get virtual base of mdl
1159 //
1160 m_Base = MmGetSystemAddressForMdlSafe(m_TransferBufferMDL, NormalPagePriority);
1161 }
1162
1163 //
1164 // Increase the size of last transfer, 0 in case this is the first
1165 //
1166 Base = (PVOID)((ULONG_PTR)m_Base + m_TransferBufferLengthCompleted);
1167
1168 PC_ASSERT(m_EndpointDescriptor);
1169 PC_ASSERT(Base);
1170
1171 //
1172 // sanity check
1173 //
1174 ASSERT(m_EndpointDescriptor);
1175
1176 //
1177 // use 4 * PAGE_SIZE at max for each new request
1178 //
1179 ULONG MaxTransferLength = min(4 * PAGE_SIZE, m_TransferBufferLength - m_TransferBufferLengthCompleted);
1180
1181 //
1182 // build bulk transfer descriptor chain
1183 //
1184 Status = BuildTransferDescriptorChain(QueueHead,
1185 Base,
1186 MaxTransferLength,
1187 InternalGetPidDirection(),
1188 m_EndpointDescriptor->DataToggle,
1189 NULL,
1190 &FirstDescriptor,
1191 &LastDescriptor,
1192 &m_EndpointDescriptor->DataToggle,
1193 &ChainDescriptorLength);
1194
1195 //
1196 // FIXME: handle errors
1197 //
1198 //ASSERT(ChainDescriptorLength == m_TransferBufferLength);
1199
1200 //
1201 // move to next offset
1202 //
1203 m_TransferBufferLengthCompleted += ChainDescriptorLength;
1204
1205 ASSERT(Status == STATUS_SUCCESS);
1206
1207 //
1208 // init queue head
1209 //
1210 QueueHead->EndPointCharacteristics.DeviceAddress = GetDeviceAddress();
1211 QueueHead->EndPointCharacteristics.EndPointNumber = m_EndpointDescriptor->EndPointDescriptor.bEndpointAddress & 0x0F;
1212 QueueHead->EndPointCharacteristics.MaximumPacketLength = m_EndpointDescriptor->EndPointDescriptor.wMaxPacketSize;
1213 QueueHead->NextPointer = FirstDescriptor->PhysicalAddr;
1214 QueueHead->CurrentLinkPointer = FirstDescriptor->PhysicalAddr;
1215 QueueHead->AlternateNextPointer = TERMINATE_POINTER;
1216
1217 ASSERT(QueueHead->EndPointCharacteristics.DeviceAddress);
1218 ASSERT(QueueHead->EndPointCharacteristics.EndPointNumber);
1219 ASSERT(QueueHead->EndPointCharacteristics.MaximumPacketLength);
1220 ASSERT(QueueHead->NextPointer);
1221
1222 //
1223 // interrupt on last descriptor
1224 //
1225 LastDescriptor->Token.Bits.InterruptOnComplete = TRUE;
1226
1227 //
1228 // store result
1229 //
1230 *OutHead = QueueHead;
1231
1232 //
1233 // dump status
1234 //
1235 //DumpQueueHead(QueueHead);
1236
1237 //
1238 // done
1239 //
1240 return STATUS_SUCCESS;
1241 }
1242
1243 //----------------------------------------------------------------------------------------
1244 NTSTATUS
1245 CUSBRequest::CreateDescriptor(
1246 PQUEUE_TRANSFER_DESCRIPTOR *OutDescriptor)
1247 {
1248 PQUEUE_TRANSFER_DESCRIPTOR Descriptor;
1249 NTSTATUS Status;
1250 PHYSICAL_ADDRESS TransferDescriptorPhysicalAddress;
1251
1252 //
1253 // allocate descriptor
1254 //
1255 Status = m_DmaManager->Allocate(sizeof(QUEUE_TRANSFER_DESCRIPTOR), (PVOID*)&Descriptor, &TransferDescriptorPhysicalAddress);
1256 if (!NT_SUCCESS(Status))
1257 {
1258 //
1259 // failed to allocate transfer descriptor
1260 //
1261 return STATUS_INSUFFICIENT_RESOURCES;
1262 }
1263
1264 //
1265 // initialize transfer descriptor
1266 //
1267 Descriptor->NextPointer = TERMINATE_POINTER;
1268 Descriptor->AlternateNextPointer = TERMINATE_POINTER;
1269 Descriptor->Token.Bits.DataToggle = TRUE;
1270 Descriptor->Token.Bits.ErrorCounter = 0x03;
1271 Descriptor->Token.Bits.Active = TRUE;
1272 Descriptor->PhysicalAddr = TransferDescriptorPhysicalAddress.LowPart;
1273
1274 //
1275 // store result
1276 //
1277 *OutDescriptor = Descriptor;
1278
1279 //
1280 // done
1281 //
1282 return Status;
1283 }
1284
1285 //----------------------------------------------------------------------------------------
1286 NTSTATUS
1287 CUSBRequest::CreateQueueHead(
1288 PQUEUE_HEAD *OutQueueHead)
1289 {
1290 PQUEUE_HEAD QueueHead;
1291 PHYSICAL_ADDRESS QueueHeadPhysicalAddress;
1292 NTSTATUS Status;
1293
1294 //
1295 // allocate queue head
1296 //
1297 Status = m_DmaManager->Allocate(sizeof(QUEUE_HEAD), (PVOID*)&QueueHead, &QueueHeadPhysicalAddress);
1298
1299 if (!NT_SUCCESS(Status))
1300 {
1301 //
1302 // failed to allocate queue head
1303 //
1304 return STATUS_INSUFFICIENT_RESOURCES;
1305 }
1306
1307 //
1308 // initialize queue head
1309 //
1310 QueueHead->HorizontalLinkPointer = TERMINATE_POINTER;
1311 QueueHead->AlternateNextPointer = TERMINATE_POINTER;
1312 QueueHead->NextPointer = TERMINATE_POINTER;
1313 InitializeListHead(&QueueHead->TransferDescriptorListHead);
1314
1315 //
1316 // 1 for non high speed, 0 for high speed device
1317 //
1318 QueueHead->EndPointCharacteristics.ControlEndPointFlag = 0;
1319 QueueHead->EndPointCharacteristics.HeadOfReclamation = FALSE;
1320 QueueHead->EndPointCharacteristics.MaximumPacketLength = 64;
1321
1322 //
1323 // Set NakCountReload to max value possible
1324 //
1325 QueueHead->EndPointCharacteristics.NakCountReload = 0x3;
1326
1327 //
1328 // Get the Initial Data Toggle from the QEDT
1329 //
1330 QueueHead->EndPointCharacteristics.QEDTDataToggleControl = TRUE;
1331
1332 //
1333 // FIXME: check if High Speed Device
1334 //
1335 QueueHead->EndPointCharacteristics.EndPointSpeed = QH_ENDPOINT_HIGHSPEED;
1336 QueueHead->EndPointCapabilities.NumberOfTransactionPerFrame = 0x01;
1337 QueueHead->Token.DWord = 0;
1338 QueueHead->Token.Bits.InterruptOnComplete = FALSE;
1339
1340 //
1341 // store address
1342 //
1343 QueueHead->PhysicalAddr = QueueHeadPhysicalAddress.LowPart;
1344
1345 //
1346 // output queue head
1347 //
1348 *OutQueueHead = QueueHead;
1349
1350 //
1351 // done
1352 //
1353 return STATUS_SUCCESS;
1354 }
1355
1356 //----------------------------------------------------------------------------------------
1357 UCHAR
1358 CUSBRequest::GetDeviceAddress()
1359 {
1360 PIO_STACK_LOCATION IoStack;
1361 PURB Urb;
1362 PUSBDEVICE UsbDevice;
1363
1364 //
1365 // check if there is an irp provided
1366 //
1367 if (!m_Irp)
1368 {
1369 //
1370 // used provided address
1371 //
1372 return m_DeviceAddress;
1373 }
1374
1375 //
1376 // get current stack location
1377 //
1378 IoStack = IoGetCurrentIrpStackLocation(m_Irp);
1379
1380 //
1381 // get contained urb
1382 //
1383 Urb = (PURB)IoStack->Parameters.Others.Argument1;
1384
1385 //
1386 // check if there is a pipe handle provided
1387 //
1388 if (Urb->UrbHeader.UsbdDeviceHandle)
1389 {
1390 //
1391 // there is a device handle provided
1392 //
1393 UsbDevice = (PUSBDEVICE)Urb->UrbHeader.UsbdDeviceHandle;
1394
1395 //
1396 // return device address
1397 //
1398 return UsbDevice->GetDeviceAddress();
1399 }
1400
1401 //
1402 // no device handle provided, it is the host root bus
1403 //
1404 return 0;
1405 }
1406
1407 //----------------------------------------------------------------------------------------
1408 NTSTATUS
1409 CUSBRequest::BuildSetupPacket()
1410 {
1411 NTSTATUS Status;
1412
1413 //
1414 // allocate common buffer setup packet
1415 //
1416 Status = m_DmaManager->Allocate(sizeof(USB_DEFAULT_PIPE_SETUP_PACKET), (PVOID*)&m_DescriptorPacket, &m_DescriptorSetupPacket);
1417 if (!NT_SUCCESS(Status))
1418 {
1419 //
1420 // no memory
1421 //
1422 return Status;
1423 }
1424
1425 if (m_SetupPacket)
1426 {
1427 //
1428 // copy setup packet
1429 //
1430 RtlCopyMemory(m_DescriptorPacket, m_SetupPacket, sizeof(USB_DEFAULT_PIPE_SETUP_PACKET));
1431 }
1432 else
1433 {
1434 //
1435 // build setup packet from urb
1436 //
1437 Status = BuildSetupPacketFromURB();
1438 }
1439
1440 //
1441 // done
1442 //
1443 return Status;
1444 }
1445
1446
1447 NTSTATUS
1448 CUSBRequest::BuildSetupPacketFromURB()
1449 {
1450 PIO_STACK_LOCATION IoStack;
1451 PURB Urb;
1452 NTSTATUS Status = STATUS_NOT_IMPLEMENTED;
1453
1454 //
1455 // sanity checks
1456 //
1457 PC_ASSERT(m_Irp);
1458 PC_ASSERT(m_DescriptorPacket);
1459
1460 //
1461 // get stack location
1462 //
1463 IoStack = IoGetCurrentIrpStackLocation(m_Irp);
1464
1465 //
1466 // get urb
1467 //
1468 Urb = (PURB)IoStack->Parameters.Others.Argument1;
1469
1470 //
1471 // zero descriptor packet
1472 //
1473 RtlZeroMemory(m_DescriptorPacket, sizeof(USB_DEFAULT_PIPE_SETUP_PACKET));
1474
1475
1476 switch (Urb->UrbHeader.Function)
1477 {
1478 /* CLEAR FEATURE */
1479 case URB_FUNCTION_CLEAR_FEATURE_TO_DEVICE:
1480 case URB_FUNCTION_CLEAR_FEATURE_TO_INTERFACE:
1481 case URB_FUNCTION_CLEAR_FEATURE_TO_ENDPOINT:
1482 UNIMPLEMENTED
1483 break;
1484
1485 /* GET CONFIG */
1486 case URB_FUNCTION_GET_CONFIGURATION:
1487 m_DescriptorPacket->bRequest = USB_REQUEST_GET_CONFIGURATION;
1488 m_DescriptorPacket->bmRequestType.B = 0x80;
1489 m_DescriptorPacket->wLength = 1;
1490 break;
1491
1492 /* GET DESCRIPTOR */
1493 case URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE:
1494 m_DescriptorPacket->bRequest = USB_REQUEST_GET_DESCRIPTOR;
1495 m_DescriptorPacket->wValue.LowByte = Urb->UrbControlDescriptorRequest.Index;
1496 m_DescriptorPacket->wValue.HiByte = Urb->UrbControlDescriptorRequest.DescriptorType;
1497 m_DescriptorPacket->wIndex.W = Urb->UrbControlDescriptorRequest.LanguageId;
1498 m_DescriptorPacket->wLength = Urb->UrbControlDescriptorRequest.TransferBufferLength;
1499 m_DescriptorPacket->bmRequestType.B = 0x80;
1500 break;
1501
1502 /* GET INTERFACE */
1503 case URB_FUNCTION_GET_INTERFACE:
1504 m_DescriptorPacket->bRequest = USB_REQUEST_GET_CONFIGURATION;
1505 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1506 m_DescriptorPacket->bmRequestType.B = 0x80;
1507 m_DescriptorPacket->wLength = 1;
1508 break;
1509
1510 /* GET STATUS */
1511 case URB_FUNCTION_GET_STATUS_FROM_DEVICE:
1512 m_DescriptorPacket->bRequest = USB_REQUEST_GET_STATUS;
1513 ASSERT(Urb->UrbControlGetStatusRequest.Index == 0);
1514 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1515 m_DescriptorPacket->bmRequestType.B = 0x80;
1516 m_DescriptorPacket->wLength = 2;
1517 break;
1518
1519 case URB_FUNCTION_GET_STATUS_FROM_INTERFACE:
1520 m_DescriptorPacket->bRequest = USB_REQUEST_GET_STATUS;
1521 ASSERT(Urb->UrbControlGetStatusRequest.Index != 0);
1522 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1523 m_DescriptorPacket->bmRequestType.B = 0x81;
1524 m_DescriptorPacket->wLength = 2;
1525 break;
1526
1527 case URB_FUNCTION_GET_STATUS_FROM_ENDPOINT:
1528 m_DescriptorPacket->bRequest = USB_REQUEST_GET_STATUS;
1529 ASSERT(Urb->UrbControlGetStatusRequest.Index != 0);
1530 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1531 m_DescriptorPacket->bmRequestType.B = 0x82;
1532 m_DescriptorPacket->wLength = 2;
1533 break;
1534
1535 /* SET ADDRESS */
1536
1537 /* SET CONFIG */
1538 case URB_FUNCTION_SELECT_CONFIGURATION:
1539 m_DescriptorPacket->bRequest = USB_REQUEST_SET_CONFIGURATION;
1540 m_DescriptorPacket->wValue.W = Urb->UrbSelectConfiguration.ConfigurationDescriptor->bConfigurationValue;
1541 m_DescriptorPacket->wIndex.W = 0;
1542 m_DescriptorPacket->wLength = 0;
1543 m_DescriptorPacket->bmRequestType.B = 0x00;
1544 break;
1545
1546 /* SET DESCRIPTOR */
1547 case URB_FUNCTION_SET_DESCRIPTOR_TO_DEVICE:
1548 case URB_FUNCTION_SET_DESCRIPTOR_TO_INTERFACE:
1549 case URB_FUNCTION_SET_DESCRIPTOR_TO_ENDPOINT:
1550 UNIMPLEMENTED
1551 break;
1552
1553 /* SET FEATURE */
1554 case URB_FUNCTION_SET_FEATURE_TO_DEVICE:
1555 m_DescriptorPacket->bRequest = USB_REQUEST_SET_FEATURE;
1556 ASSERT(Urb->UrbControlGetStatusRequest.Index == 0);
1557 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1558 m_DescriptorPacket->bmRequestType.B = 0x80;
1559 break;
1560
1561 case URB_FUNCTION_SET_FEATURE_TO_INTERFACE:
1562 m_DescriptorPacket->bRequest = USB_REQUEST_SET_FEATURE;
1563 ASSERT(Urb->UrbControlGetStatusRequest.Index == 0);
1564 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1565 m_DescriptorPacket->bmRequestType.B = 0x81;
1566 break;
1567
1568 case URB_FUNCTION_SET_FEATURE_TO_ENDPOINT:
1569 m_DescriptorPacket->bRequest = USB_REQUEST_SET_FEATURE;
1570 ASSERT(Urb->UrbControlGetStatusRequest.Index == 0);
1571 m_DescriptorPacket->wIndex.W = Urb->UrbControlGetStatusRequest.Index;
1572 m_DescriptorPacket->bmRequestType.B = 0x82;
1573 break;
1574
1575 /* SET INTERFACE*/
1576 case URB_FUNCTION_SELECT_INTERFACE:
1577 m_DescriptorPacket->bRequest = USB_REQUEST_SET_INTERFACE;
1578 m_DescriptorPacket->wValue.W = Urb->UrbSelectInterface.Interface.AlternateSetting;
1579 m_DescriptorPacket->wIndex.W = Urb->UrbSelectInterface.Interface.InterfaceNumber;
1580 m_DescriptorPacket->wLength = 0;
1581 m_DescriptorPacket->bmRequestType.B = 0x01;
1582 break;
1583
1584 /* SYNC FRAME */
1585 case URB_FUNCTION_SYNC_RESET_PIPE_AND_CLEAR_STALL:
1586 UNIMPLEMENTED
1587 break;
1588 default:
1589 UNIMPLEMENTED
1590 break;
1591 }
1592
1593 return Status;
1594 }
1595
1596 //----------------------------------------------------------------------------------------
1597 VOID
1598 CUSBRequest::GetResultStatus(
1599 OUT OPTIONAL NTSTATUS * NtStatusCode,
1600 OUT OPTIONAL PULONG UrbStatusCode)
1601 {
1602 //
1603 // sanity check
1604 //
1605 PC_ASSERT(m_CompletionEvent);
1606
1607 //
1608 // wait for the operation to complete
1609 //
1610 KeWaitForSingleObject(m_CompletionEvent, Executive, KernelMode, FALSE, NULL);
1611
1612 //
1613 // copy status
1614 //
1615 if (NtStatusCode)
1616 {
1617 *NtStatusCode = m_NtStatusCode;
1618 }
1619
1620 //
1621 // copy urb status
1622 //
1623 if (UrbStatusCode)
1624 {
1625 *UrbStatusCode = m_UrbStatusCode;
1626 }
1627
1628 }
1629
1630
1631 //-----------------------------------------------------------------------------------------
1632 BOOLEAN
1633 CUSBRequest::IsRequestInitialized()
1634 {
1635 if (m_Irp || m_SetupPacket)
1636 {
1637 //
1638 // request is initialized
1639 //
1640 return TRUE;
1641 }
1642
1643 //
1644 // request is not initialized
1645 //
1646 return FALSE;
1647 }
1648
1649 //-----------------------------------------------------------------------------------------
1650 BOOLEAN
1651 CUSBRequest::ShouldReleaseRequestAfterCompletion()
1652 {
1653 if (m_Irp)
1654 {
1655 //
1656 // the request is completed, release it
1657 //
1658 return TRUE;
1659 }
1660 else
1661 {
1662 //
1663 // created with an setup packet, don't release
1664 //
1665 return FALSE;
1666 }
1667 }
1668
1669 //-----------------------------------------------------------------------------------------
1670 VOID
1671 CUSBRequest::FreeQueueHead(
1672 IN struct _QUEUE_HEAD * QueueHead)
1673 {
1674 PLIST_ENTRY Entry;
1675 PQUEUE_TRANSFER_DESCRIPTOR Descriptor;
1676
1677 //
1678 // sanity checks
1679 //
1680 ASSERT(m_DmaManager);
1681 ASSERT(QueueHead);
1682 ASSERT(!IsListEmpty(&QueueHead->TransferDescriptorListHead));
1683
1684 do
1685 {
1686 //
1687 // get transfer descriptors
1688 //
1689 Entry = RemoveHeadList(&QueueHead->TransferDescriptorListHead);
1690 ASSERT(Entry);
1691
1692 //
1693 // obtain descriptor from entry
1694 //
1695 Descriptor = (PQUEUE_TRANSFER_DESCRIPTOR)CONTAINING_RECORD(Entry, QUEUE_TRANSFER_DESCRIPTOR, DescriptorEntry);
1696 ASSERT(Descriptor);
1697
1698 //
1699 // add transfer count
1700 //
1701 m_TotalBytesTransferred += (Descriptor->TotalBytesToTransfer - Descriptor->Token.Bits.TotalBytesToTransfer);
1702 DPRINT("TotalBytes Transferred in Descriptor %p Phys Addr %x TotalBytesSoftware %lu Length %lu\n", Descriptor, Descriptor->PhysicalAddr, Descriptor->TotalBytesToTransfer, Descriptor->TotalBytesToTransfer - Descriptor->Token.Bits.TotalBytesToTransfer);
1703
1704 //
1705 // release transfer descriptors
1706 //
1707 m_DmaManager->Release(Descriptor, sizeof(QUEUE_TRANSFER_DESCRIPTOR));
1708
1709 }while(!IsListEmpty(&QueueHead->TransferDescriptorListHead));
1710
1711 if (m_DescriptorPacket)
1712 {
1713 //
1714 // release packet descriptor
1715 //
1716 m_DmaManager->Release(m_DescriptorPacket, sizeof(USB_DEFAULT_PIPE_SETUP_PACKET));
1717 }
1718
1719 //
1720 // release queue head
1721 //
1722 m_DmaManager->Release(QueueHead, sizeof(QUEUE_HEAD));
1723
1724 //
1725 // nullify pointers
1726 //
1727 m_QueueHead = 0;
1728 m_DescriptorPacket = 0;
1729 }
1730
1731 //-----------------------------------------------------------------------------------------
1732 BOOLEAN
1733 CUSBRequest::IsQueueHeadComplete(
1734 struct _QUEUE_HEAD * QueueHead)
1735 {
1736 PLIST_ENTRY Entry;
1737 PQUEUE_TRANSFER_DESCRIPTOR Descriptor;
1738
1739 //
1740 // first check - is the queue head currently active
1741 //
1742 if (QueueHead->Token.Bits.Active)
1743 {
1744 //
1745 // queue head is active (currently processed)
1746 //
1747 return FALSE;
1748 }
1749
1750 if (QueueHead->Token.Bits.Halted)
1751 {
1752 //
1753 // error occured
1754 //
1755 DPRINT1("Found halted queue head %p\n", QueueHead);
1756 DumpQueueHead(QueueHead);
1757 //ASSERT(FALSE);
1758 return TRUE;
1759 }
1760
1761 //
1762 // loop list and see if there are any active descriptors
1763 //
1764 Entry = QueueHead->TransferDescriptorListHead.Flink;
1765 while(Entry != &QueueHead->TransferDescriptorListHead)
1766 {
1767 //
1768 // obtain descriptor from entry
1769 //
1770 Descriptor = (PQUEUE_TRANSFER_DESCRIPTOR)CONTAINING_RECORD(Entry, QUEUE_TRANSFER_DESCRIPTOR, DescriptorEntry);
1771 ASSERT(Descriptor);
1772 if (Descriptor->Token.Bits.Active)
1773 {
1774 //
1775 // descriptor is still active
1776 //
1777 return FALSE;
1778 }
1779
1780 //
1781 // move to next entry
1782 //
1783 Entry = Entry->Flink;
1784 }
1785
1786 DPRINT("QueueHead %p Addr %x is complete\n", QueueHead, QueueHead->PhysicalAddr);
1787
1788 //
1789 // no active descriptors found, queue head is finished
1790 //
1791 return TRUE;
1792 }
1793
1794 //-----------------------------------------------------------------------------------------
1795 VOID
1796 CUSBRequest::GetTransferBuffer(
1797 OUT PMDL * OutMDL,
1798 OUT PULONG TransferLength)
1799 {
1800 // sanity checks
1801 PC_ASSERT(OutMDL);
1802 PC_ASSERT(TransferLength);
1803
1804 *OutMDL = m_TransferBufferMDL;
1805 *TransferLength = m_TransferBufferLength;
1806 }
1807 //-----------------------------------------------------------------------------------------
1808 ULONG
1809 CUSBRequest::InternalCalculateTransferLength()
1810 {
1811 if (!m_Irp)
1812 {
1813 //
1814 // FIXME: get length for control request
1815 //
1816 return m_TransferBufferLength;
1817 }
1818
1819 //
1820 // sanity check
1821 //
1822 ASSERT(m_EndpointDescriptor);
1823 if (USB_ENDPOINT_DIRECTION_IN(m_EndpointDescriptor->EndPointDescriptor.bEndpointAddress))
1824 {
1825 //
1826 // bulk in request
1827 // HACK: Properly determine transfer length
1828 //
1829 return m_TransferBufferLength;//m_TotalBytesTransferred;
1830 }
1831
1832 //
1833 // bulk out transfer
1834 //
1835 return m_TransferBufferLength;
1836 }
1837
1838 //-----------------------------------------------------------------------------------------
1839 NTSTATUS
1840 InternalCreateUSBRequest(
1841 PUSBREQUEST *OutRequest)
1842 {
1843 PUSBREQUEST This;
1844
1845 //
1846 // allocate requests
1847 //
1848 This = new(NonPagedPool, TAG_USBEHCI) CUSBRequest(0);
1849 if (!This)
1850 {
1851 //
1852 // failed to allocate
1853 //
1854 return STATUS_INSUFFICIENT_RESOURCES;
1855 }
1856
1857 //
1858 // add reference count
1859 //
1860 This->AddRef();
1861
1862 //
1863 // return result
1864 //
1865 *OutRequest = (PUSBREQUEST)This;
1866
1867 //
1868 // done
1869 //
1870 return STATUS_SUCCESS;
1871 }