📄 crconfig.c
字号:
//
// Copyright (c) 2004 Golden Bits Software, Inc.
// All rights reserved
// www.goldenbits.com
//
// WARNING: For sample/demonstration use only, no warranty
// of any kind.
//
// File: CrConfig.c
//
// Desc: Contains routines to configure USB device. Setup pipes and
// USB device descriptors.
//
#include "ntddk.h"
#include "wmilib.h"
#include "usbdi.h"
#include "usbdlib.h"
#include "CrMain.h"
#include "CrPnp.h"
#include "CrConfig.h"
#include "..\Include\UsbCryptAppInc.h"
// ================== ConfigureDriver() ===============
// Desc: Configures the USB device.
//
// Returns: NTSTATUS
//
NTSTATUS ConfigureDriver(IN PDEVICE_OBJECT pDeviceObject)
{
NTSTATUS RetStatus;
// get device descriptor, one per USB device
// describes USB device as a whole
RetStatus = GetUSBDeviceDescripitor(pDeviceObject);
if(!NT_SUCCESS(RetStatus))
{
USB_TRACE("ConfigureDriver() Error when getting device descriptor.\n");
return RetStatus;
}
// get the configurations, it's possible an
// USB device can have multiple configurations
GetUSBConfigDescriptor(pDeviceObject);
// we should be ready to go!!
return STATUS_SUCCESS;
}
// =============== GetUSBDeviceDescripitor() =============
// Desc: Gets the device descriptor and stores in the device
// extension at pCryptExt->My_USBDeviceDesc. Calls other
// functions to finish configuring the USB device.
//
// Returns: NTSTATUS
//
NTSTATUS GetUSBDeviceDescripitor(IN PDEVICE_OBJECT pDeviceObject)
{
PUSB_CRYPT_EXT pCryptExt;
PURB pURB;
NTSTATUS Status;
// get device extension
pCryptExt = (PUSB_CRYPT_EXT)pDeviceObject->DeviceExtension;
// get memory for URB request
pURB = ExAllocatePool(NonPagedPool,
sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST));
if(pURB == NULL)
{
// out of memory?? log error and exit
USB_TRACE("GetUSBDeviceDescripitor() - Allocate memory failed\n");
return STATUS_NO_MEMORY;
}
// build a descriptor request for our
// USB Device
UsbBuildGetDescriptorRequest(pURB,
(USHORT) sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST),
USB_DEVICE_DESCRIPTOR_TYPE,
0,
0,
&pCryptExt->My_USBDeviceDesc,
NULL,
sizeof(pCryptExt->My_USBDeviceDesc),
NULL);
// Call USB bus driver, with blocking flag set
// to TRUE. This means we'll wait here until we get
// a response
Status = SendUSBRequest(pDeviceObject, pURB, TRUE);
return Status;
}
// =============== GetUSBConfigDescriptor() =============
// Desc: Returns configuration descriptors for this USB device.
// Remember, you can have multiple configuration descriptors
// for a single USB device.
//
// Returns: NTSTATUS
//
NTSTATUS GetUSBConfigDescriptor(IN PDEVICE_OBJECT pDeviceObj)
{
PURB pUrb;
ULONG DescLen;
NTSTATUS ntStatus;
PUSB_CRYPT_EXT pCryptExt;
PUSB_CONFIGURATION_DESCRIPTOR pConfigDescriptor;
//
// initialize the variables
//
pConfigDescriptor = NULL;
pCryptExt = (PUSB_CRYPT_EXT)pDeviceObj->DeviceExtension;
//
// Get the configuration descriptor for this device
// Note that.... this first request is just for the
// the descriptor itself, not including any interfaces.
// The first call get's the lenght required for all of the
// interfaces. From this we can allocate enough memory to
// perform the second call, which actually gets the
// interfaces.
//
pUrb = ExAllocatePool(NonPagedPool,
sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST));
if(pUrb == NULL)
{
// log error message
USB_TRACE("GetUSBConfigDescriptor() Failed to allocate memory for URB\n");
return STATUS_INSUFFICIENT_RESOURCES;
}
DescLen = sizeof(USB_CONFIGURATION_DESCRIPTOR);
pConfigDescriptor = ExAllocatePool(NonPagedPool, DescLen);
if(pConfigDescriptor == NULL)
{
ExFreePool(pUrb);
// log error message
USB_TRACE("GetUSBConfigDescriptor() Failed to allocate memory for config descriptor.\n");
return STATUS_INSUFFICIENT_RESOURCES;
}
// now build the URB to get the device's configuration descriptor
UsbBuildGetDescriptorRequest(pUrb,
(USHORT)sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST),
USB_CONFIGURATION_DESCRIPTOR_TYPE,
0,
0,
pConfigDescriptor,
NULL,
DescLen,
NULL);
// send request to USB bus driver
ntStatus = SendUSBRequest(pDeviceObj, pUrb, TRUE);
if(!NT_SUCCESS(ntStatus))
{
USB_TRACE1("GetUSBConfigDescriptor() device config request failed, status 0x%x\n",
ntStatus);
ExFreePool(pUrb);
ExFreePool(pConfigDescriptor);
return STATUS_UNSUCCESSFUL;
}
// successfully got the descriptor, now let's look
// at it and determine how large we should make
// our next descriptor
DescLen = pConfigDescriptor->wTotalLength;
// free original allocation and re-allocate with
// new config descriptor len
ExFreePool(pConfigDescriptor);
pConfigDescriptor = ExAllocatePool(NonPagedPool, DescLen);
if(pConfigDescriptor == NULL)
{
ExFreePool(pUrb);
// log error message
USB_TRACE("GetUSBConfigDescriptor() Failed to allocate memory for second config descriptor.\n");
return STATUS_INSUFFICIENT_RESOURCES;
}
// build our second request, this will actually
// get the device interfaces
UsbBuildGetDescriptorRequest(pUrb,
(USHORT)sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST),
USB_CONFIGURATION_DESCRIPTOR_TYPE,
0,
0,
pConfigDescriptor,
NULL,
DescLen,
NULL);
// send request to bus driver
ntStatus = SendUSBRequest(pDeviceObj, pUrb, TRUE);
if(!NT_SUCCESS(ntStatus))
{
USB_TRACE1("GetUSBConfigDescriptor() device config second request failed, status 0x%x\n",
ntStatus);
ExFreePool(pUrb);
ExFreePool(pConfigDescriptor);
return STATUS_UNSUCCESSFUL;
}
// free the URB only, the config descriptor we'll keep
// in the device extension
ExFreePool(pUrb);
pCryptExt->pConfigDesc = pConfigDescriptor;
// let's setup our USB interface for this device
ntStatus = SetupInterfaces(pDeviceObj);
return ntStatus;
}
// ================ SetupInterfaces() =======================
// Desc: Sets up interface for this device
//
// Returns: NTSTATUS
//
NTSTATUS SetupInterfaces(IN PDEVICE_OBJECT pDeviceObj)
{
PUSB_CRYPT_EXT pCryptExt;
PUSB_CONFIGURATION_DESCRIPTOR pConfigDescriptor;
PURB pUrb;
long lNumInterfaces, lIFNumber;
long lCount;
NTSTATUS Status;
PUSBD_INTERFACE_LIST_ENTRY pInterfaceList, pBegList;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -