阅读:686回复:0
描述符的疑惑
各位大侠,在看机械工业出版设的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; } |
|