Advertisement
yaqwsx

MDL version of sending routine

Apr 11th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1.     //Create request
  2.     if (context->request == NULL)
  3.     {
  4.         ntStatus = WdfRequestCreate(&objectAttributes, NULL, &context->request);
  5.         if (!NT_SUCCESS(ntStatus))
  6.         {
  7.             DbgPrint("WdfRequestCreate failed: %x\n", ntStatus);
  8.             return;
  9.         }
  10.     }
  11.     else
  12.     {
  13.         //Reuse request
  14.         WDF_REQUEST_REUSE_PARAMS  paramsR;
  15.         WDF_REQUEST_REUSE_PARAMS_INIT(&paramsR, WDF_REQUEST_REUSE_NO_FLAGS, STATUS_SUCCESS);
  16.  
  17.         ntStatus = WdfRequestReuse(context->request, &paramsR);
  18.         if (!NT_SUCCESS(ntStatus))
  19.             DbgPrint("sendAudioData: WdfRequestReuse failed: %x\n", ntStatus);
  20.         ASSERT(NT_SUCCESS(ntStatus));
  21.     }
  22.  
  23.     //Create URB structure
  24.     if (context->urbMemory == NULL)
  25.     {
  26.         WDF_OBJECT_ATTRIBUTES_INIT(&objectAttributes);
  27.         objectAttributes.ParentObject = usbDevice;
  28.         ntStatus = WdfUsbTargetDeviceCreateIsochUrb(usbDevice, &objectAttributes, MAX_PAKETS, &context->urbMemory, NULL);
  29.         if (!NT_SUCCESS(ntStatus))
  30.         {
  31.             DbgPrint("sendAudioData: WdfTargetDeviceCreateIsochUrb failed: %x\n", ntStatus);
  32.             return;
  33.         }
  34.     }
  35.  
  36.     //Create outputBuffer
  37.     ULONG bufferLen = numPackets * numOfSamplesPerPacket * getAudioBufferForChannel(channel)->getSampleSize();
  38.     if (context->dataBuffer == NULL)
  39.     {
  40.         AudioBuffer* buffer = getAudioBufferForChannel(channel);
  41.         PHYSICAL_ADDRESS low;
  42.         RtlZeroMemory(&low, sizeof(PHYSICAL_ADDRESS));
  43.         PHYSICAL_ADDRESS high;
  44.         high.QuadPart = _UI64_MAX;
  45.         PHYSICAL_ADDRESS skip;
  46.         skip.QuadPart = PAGE_SIZE;
  47.         context->dataBuffer = MmAllocatePagesForMdlEx(low, high, skip, buffer->getSampleSize()*buffer->getSize(), MmCached, MM_ALLOCATE_PREFER_CONTIGUOUS);
  48.     }
  49.  
  50.     PURB urb = (PURB)WdfMemoryGetBuffer(context->urbMemory, NULL);
  51.  
  52.     urb->UrbIsochronousTransfer.Hdr.Length = GET_ISO_URB_SIZE(numPackets);
  53.     urb->UrbIsochronousTransfer.Hdr.Function = URB_FUNCTION_ISOCH_TRANSFER;
  54.     urb->UrbIsochronousTransfer.PipeHandle = WdfUsbTargetPipeWdmGetPipeHandle(channelEndpoints[channel].dataPipe);
  55.     urb->UrbIsochronousTransfer.TransferFlags = USBD_TRANSFER_DIRECTION_OUT | USBD_START_ISO_TRANSFER_ASAP;
  56.     urb->UrbIsochronousTransfer.TransferBufferLength = bufferLen;
  57.     urb->UrbIsochronousTransfer.NumberOfPackets = numPackets;
  58.     urb->UrbIsochronousTransfer.TransferBufferMDL = context->dataBuffer;
  59.     for (ULONG i = 0; i != numPackets; i++)
  60.         urb->UrbIsochronousTransfer.IsoPacket[i].Offset = i * numOfSamplesPerPacket * getAudioBufferForChannel(channel)->getSampleSize();
  61.  
  62.     //Copy audio data into buffer
  63.     AudioBuffer* buffer = getAudioBufferForChannel(channel);
  64.     //ToDo: Temporary - in final release shoud be removed - add dummy samples
  65.     buffer->fillBufferWithSine(currentFrequency);
  66.     //I've tried both possibilities - both with the same result
  67.     //BYTE* ioBuff = (BYTE*)MmGetMdlVirtualAddress(context->dataBuffer);
  68.     BYTE* ioBuff = (BYTE*)MmGetSystemAddressForMdlSafe(context->dataBuffer, HighPagePriority);
  69.  
  70.     //Following 2 lines couses probelm
  71.     memset(ioBuff, 0, bufferLen);
  72.     buffer->popSamples(ioBuff, numOfSamplesPerPacket * numPackets);
  73.  
  74.    
  75.     ntStatus = WdfUsbTargetPipeFormatRequestForUrb(channelEndpoints[channel].dataPipe, context->request, context->urbMemory, 0);
  76.     if (!NT_SUCCESS(ntStatus))
  77.     {
  78.         DbgPrint("WdfUsbTargetDeviceFormatRequestForUrb failed: %x\n", ntStatus);
  79.         return;
  80.     }
  81.  
  82.     SendContext* sContext = &channelEndpoints[channel].sendSpecificContext;
  83.     sContext->device = this;
  84.     sContext->channel = channel;
  85.     WdfRequestSetCompletionRoutine(context->request, sendRequestComplete, sContext);
  86.    
  87.     if (WdfRequestSend(context->request, WdfUsbTargetPipeGetIoTarget(channelEndpoints[channel].dataPipe), WDF_NO_SEND_OPTIONS) == FALSE)
  88.     {
  89.         ntStatus = WdfRequestGetStatus(context->request);
  90.         DbgPrint("WdfRequestSend failed with status code 0x%x\n", ntStatus);
  91.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement