whmjack
驱动小牛
驱动小牛
  • 注册日期2003-09-17
  • 最后登录2007-04-26
  • 粉丝0
  • 关注0
  • 积分290分
  • 威望29点
  • 贡献值0点
  • 好评度29点
  • 原创分0分
  • 专家分0分
阅读:686回复:0

描述符的疑惑

楼主#
更多 发布于:2004-01-05 10:30
各位大侠,在看机械工业出版设的wdm的书中的一个例子时碰到了一个小问题,就是在读取
设备配置描述符时,为什么在sizeof(configue_descriptor)后还要加一个16呢?

descriptors = (PUSB_CONFIGURATION_DESCRIPTOR)ExAllocatePool(NonPagedPool, Desc
riptorsSize+16);

源程序如下:
NTSTATUS UsbGetConfigurationDescriptors( IN PUSBKBD_DEVICE_EXTENSION dx,  
OUT PUSB_CONFIGURATION_DESCRIPTOR& descriptors,
IN UCHAR ConfigIndex,
OUT ULONG& DescriptorsSize)
{
// Allocate memory for URB
USHORT UrbSize = sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST);
PURB urb = (PURB)ExAllocatePool(NonPagedPool, UrbSize);
if( urb==NULL)
{
DebugPrintMsg(\"No URB memory\");
return STATUS_INSUFFICIENT_RESOURCES;
}

// Allocate memory just for basic config descriptor
DescriptorsSize = sizeof(USB_CONFIGURATION_DESCRIPTOR);
descriptors = (PUSB_CONFIGURATION_DESCRIPTOR)ExAllocatePool(NonPagedPool, Desc
riptorsSize+16);
if( descriptors==NULL)
{
DebugPrintMsg(\"No initial descriptor memory\");
return STATUS_INSUFFICIENT_RESOURCES;
}

// Build the Get Descriptor URB
UsbBuildGetDescriptorRequest(
urb, UrbSize,
USB_CONFIGURATION_DESCRIPTOR_TYPE, ConfigIndex, 0,
descriptors, NULL, DescriptorsSize,
NULL);

// Call the USB driver
DebugPrintMsg(\"Getting basic configuration descriptor\");
NTSTATUS status = CallUSBDI( dx, urb);
// Check statuses
if( !NT_SUCCESS(status) || !USBD_SUCCESS( urb->UrbHeader.Status))
{
DebugPrint(\"status %x URB status %x\", status, urb->UrbHeader.Status);
status = STATUS_UNSUCCESSFUL;
goto fail;
}
DebugPrint(\"Got basic config descr. MaxPower %d units of 2mA\", descriptors->Ma
xPower);

// Reallocate memory for config descriptor and associated descriptors
DescriptorsSize = descriptors->wTotalLength;
ExFreePool(descriptors);
descriptors = (PUSB_CONFIGURATION_DESCRIPTOR)ExAllocatePool(NonPagedPool, Desc
riptorsSize+16);
if( descriptors==NULL)
{
DebugPrintMsg(\"No full descriptors memory\");
return STATUS_INSUFFICIENT_RESOURCES;
}

// Build the Get Descriptor URB
UsbBuildGetDescriptorRequest(
urb, UrbSize,  
USB_CONFIGURATION_DESCRIPTOR_TYPE, ConfigIndex, 0,
descriptors, NULL, DescriptorsSize,
NULL);

// Call the USB driver
DebugPrintMsg(\"Getting full configuration descriptors\");
status = CallUSBDI( dx, urb);
// Check statuses
if( !NT_SUCCESS(status) || !USBD_SUCCESS( urb->UrbHeader.Status))
{
DebugPrint(\"status %x URB status %x\", status, urb->UrbHeader.Status);
status = STATUS_UNSUCCESSFUL;
goto fail;
}
// Remember count of bytes actually transferred
DescriptorsSize = urb->UrbControlDescriptorRequest.TransferBufferLength;

fail:
ExFreePool(urb);
return STATUS_SUCCESS;
}





游客

返回顶部