📄 usbfndrv.cpp
字号:
USB_DEVICE_DESCRIPTOR_TYPE, // bDescriptorType
USB_VERSION, // bcdUSB
0x00, // bDeviceClass
0x00, // bDeviceSubClass
0x00, // bDeviceProtocol
EP0_PACKET_SIZE, // bMaxPacketSize0
0, // idVendor
0, // idProduct
0x0000, // bcdDevice
0x01, // iManufacturer
0x02, // iProduct
0x03, // iSerialNumber
0x01 // bNumConfigurations
};
m_FullSpeedDeviceDesc = FullSpeedDeviceDesc;
const UFN_ENDPOINT HighSpeedEndpoints[INTERFACE_NUM_OF_ENDPOINT] = {
{
sizeof(UFN_ENDPOINT),
{
sizeof(USB_ENDPOINT_DESCRIPTOR),// bLength
USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType
BULK_IN_ENDPOINT_ADDRESS, // bEndpointAddress (endpoint 1, in)
USB_ENDPOINT_TYPE_BULK, // bmAttributes
HIGH_SPEED_BULK_PACKET_SIZES, // wMaxPacketSize
0x00 // bInterval (interrupt only)
},
NULL
},
{
sizeof(UFN_ENDPOINT),
{
sizeof(USB_ENDPOINT_DESCRIPTOR),// bLength
USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType
BULK_OUT_ENDPOINT_ADDRESS, // bEndpointAddress (endpoint 2, out)
USB_ENDPOINT_TYPE_BULK, // bmAttributes
HIGH_SPEED_BULK_PACKET_SIZES, // wMaxPacketSize
0x00 // bInterval (interrupt only)
},
NULL
},
{
sizeof(UFN_ENDPOINT),
{
sizeof(USB_ENDPOINT_DESCRIPTOR),// bLength
USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType
INTERRUPT_IN_ENDPOINT_ADDRESS, // bEndpointAddress (endpoint 2, out)
USB_ENDPOINT_TYPE_INTERRUPT, // bmAttributes
MAX_INTERRUPT_ENDPOINT_PACKET_SIZE, // wMaxPacketSize
0xc // bInterval (interrupt only)
},
NULL
}
};
memcpy(m_HighSpeedEndpoints,HighSpeedEndpoints,sizeof(m_HighSpeedEndpoints));
const UFN_ENDPOINT FullSpeedEndpoints[INTERFACE_NUM_OF_ENDPOINT] = {
{
sizeof(UFN_ENDPOINT),
{
sizeof(USB_ENDPOINT_DESCRIPTOR),// bLength
USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType
BULK_IN_ENDPOINT_ADDRESS, // bEndpointAddress (endpoint 1, in)
USB_ENDPOINT_TYPE_BULK, // bmAttributes
FULL_SPEED_BULK_PACKET_SIZES, // wMaxPacketSize
0x00 // bInterval (interrupt only)
},
NULL
},
{
sizeof(UFN_ENDPOINT),
{
sizeof(USB_ENDPOINT_DESCRIPTOR),// bLength
USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType
BULK_OUT_ENDPOINT_ADDRESS, // bEndpointAddress (endpoint 2, out)
USB_ENDPOINT_TYPE_BULK, // bmAttributes
FULL_SPEED_BULK_PACKET_SIZES, // wMaxPacketSize
0x00 // bInterval (interrupt only)
},
NULL
},
{
sizeof(UFN_ENDPOINT),
{
sizeof(USB_ENDPOINT_DESCRIPTOR),// bLength
USB_ENDPOINT_DESCRIPTOR_TYPE, // bDescriptorType
INTERRUPT_IN_ENDPOINT_ADDRESS, // bEndpointAddress (endpoint 2, out)
USB_ENDPOINT_TYPE_INTERRUPT, // bmAttributes
MAX_INTERRUPT_ENDPOINT_PACKET_SIZE, // wMaxPacketSize
0x20 // bInterval (interrupt only)
},
NULL
}
};
memcpy(m_FullSpeedEndpoints,FullSpeedEndpoints,sizeof(m_FullSpeedEndpoints));
const UFN_INTERFACE HighSpeedInterface1 = {
sizeof(UFN_INTERFACE),
{
sizeof(USB_INTERFACE_DESCRIPTOR), // bLength
USB_INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
0x00, // bInterfaceNumber
0x00, // bAlternateSetting
dim(m_HighSpeedEndpoints), // bNumEndpoints
0xff, // bInterfaceClass
0xff,// bInterfaceSubClass
0xff, // bInterfaceProtocol
0x00 // iInterface
},
NULL, // extended
0,
m_HighSpeedEndpoints // endpoint array
};
m_HighSpeedInterface1 = HighSpeedInterface1;
const UFN_INTERFACE HighSpeedInterface2 = {
sizeof(UFN_INTERFACE),
{
sizeof(USB_INTERFACE_DESCRIPTOR), // bLength
USB_INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
0x00, // bInterfaceNumber
0x00, // bAlternateSetting
dim(m_HighSpeedEndpoints)-1, // bNumEndpoints
0xff, // bInterfaceClass
0xff,// bInterfaceSubClass
0xff, // bInterfaceProtocol
0x00 // iInterface
},
NULL, // extended
0,
m_HighSpeedEndpoints // endpoint array
};
m_HighSpeedInterface2 = HighSpeedInterface2;
const UFN_INTERFACE FullSpeedInterface1 = {
sizeof(UFN_INTERFACE),
{
sizeof(USB_INTERFACE_DESCRIPTOR), // bLength
USB_INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
0x00, // bInterfaceNumber
0x00, // bAlternateSetting
dim(m_FullSpeedEndpoints), // bNumEndpoints
0xff, // bInterfaceClass
0xff,// bInterfaceSubClass
0xff, // bInterfaceProtocol
0x00 // iInterface
},
NULL, // extended
0,
m_FullSpeedEndpoints // endpoint array
};
m_FullSpeedInterface1 = FullSpeedInterface1;
const UFN_INTERFACE FullSpeedInterface2 = {
sizeof(UFN_INTERFACE),
{
sizeof(USB_INTERFACE_DESCRIPTOR), // bLength
USB_INTERFACE_DESCRIPTOR_TYPE, // bDescriptorType
0x00, // bInterfaceNumber
0x00, // bAlternateSetting
dim(m_FullSpeedEndpoints)-1, // bNumEndpoints
0xff, // bInterfaceClass
0xff,// bInterfaceSubClass
0xff, // bInterfaceProtocol
0x00 // iInterface
},
NULL, // extended
0,
m_FullSpeedEndpoints // endpoint array
};
m_FullSpeedInterface2 = FullSpeedInterface2;
const UFN_CONFIGURATION HighSpeedConfig1 = {
sizeof(UFN_CONFIGURATION),
{
sizeof(USB_CONFIGURATION_DESCRIPTOR),// bLength
USB_CONFIGURATION_DESCRIPTOR_TYPE, // bDescriptorType
CB_CONFIG_DESCRIPTOR, // wTotalLength
0x01, // bNumInterfaces
0x01, // bConfigurationValue
0x00, // iConfiguration
USB_CONFIG_RESERVED_ATTRIBUTE | USB_CONFIG_SELF_POWERED, // bmAttributes
0xfa // MaxPower
},
NULL,
0x00,
&m_HighSpeedInterface1 // interface array
};
m_HighSpeedConfig1 = HighSpeedConfig1 ;
const UFN_CONFIGURATION HighSpeedConfig2 = {
sizeof(UFN_CONFIGURATION),
{
sizeof(USB_CONFIGURATION_DESCRIPTOR),// bLength
USB_CONFIGURATION_DESCRIPTOR_TYPE, // bDescriptorType
CB_CONFIG_DESCRIPTOR- sizeof(USB_ENDPOINT_DESCRIPTOR), // wTotalLength
0x01, // bNumInterfaces
0x01, // bConfigurationValue
0x00, // iConfiguration
USB_CONFIG_RESERVED_ATTRIBUTE | USB_CONFIG_SELF_POWERED, // bmAttributes
0xfa // MaxPower
},
NULL,
0x00,
&m_HighSpeedInterface2 // interface array
};
m_HighSpeedConfig2 = HighSpeedConfig2;
const UFN_CONFIGURATION FullSpeedConfig1 = {
sizeof(UFN_CONFIGURATION),
{
sizeof(USB_CONFIGURATION_DESCRIPTOR),// bLength
USB_CONFIGURATION_DESCRIPTOR_TYPE, // bDescriptorType
CB_CONFIG_DESCRIPTOR, // wTotalLength
0x01, // bNumInterfaces
0x01, // bConfigurationValue
0x00, // iConfiguration
USB_CONFIG_RESERVED_ATTRIBUTE | USB_CONFIG_SELF_POWERED, // bmAttributes
0xfa // MaxPower
},
NULL,
0x00,
&m_FullSpeedInterface1 // interface array
};
m_FullSpeedConfig1 = FullSpeedConfig1 ;
const UFN_CONFIGURATION FullSpeedConfig2 = {
sizeof(UFN_CONFIGURATION),
{
sizeof(USB_CONFIGURATION_DESCRIPTOR),// bLength
USB_CONFIGURATION_DESCRIPTOR_TYPE, // bDescriptorType
CB_CONFIG_DESCRIPTOR - sizeof(USB_ENDPOINT_DESCRIPTOR), // wTotalLength
0x01, // bNumInterfaces
0x01, // bConfigurationValue
0x00, // iConfiguration
USB_CONFIG_RESERVED_ATTRIBUTE | USB_CONFIG_SELF_POWERED, // bmAttributes
0xfa // MaxPower
},
NULL,
0x00,
&m_FullSpeedInterface2 // interface array
};
m_FullSpeedConfig2 = FullSpeedConfig2 ;
m_RegInfo.dwSize = sizeof(m_RegInfo);
m_rgpszStrings0409[0]= m_RegInfo.szVendor;
m_rgpszStrings0409[1]= m_RegInfo.szProduct;
m_rgpszStrings0409[2]= m_RegInfo.szSerialNumber;
const UFN_STRING_SET rgStringSets = {
0x0409, m_rgpszStrings0409, dim(m_rgpszStrings0409)
};
m_rgStringSets = rgStringSets ;
}
#define COMLAYER_DRIVER_PATH TEXT("ComLayer")
BOOL CUsbFn::Init()
{
if (m_lpActivePath &&
UfnInitializeInterface(m_lpActivePath, &m_hDevice, &m_UfnFuncs, &m_pvInterface)== ERROR_SUCCESS) {
PREFAST_ASSERT(m_hDevice!=NULL);
PREFAST_ASSERT(m_pUfnFuncs!=NULL);
CRegistryEdit driverKey(m_lpActivePath);
if (driverKey.IsKeyOpened()) {
DWORD dwRet = UfnGetRegistryInfo(m_lpActivePath, &m_RegInfo);
if (dwRet == ERROR_SUCCESS) {
m_FullSpeedDeviceDesc.idVendor = m_HighSpeedDeviceDesc.idVendor = (USHORT) m_RegInfo.idVendor;
m_FullSpeedDeviceDesc.idProduct = m_HighSpeedDeviceDesc.idProduct = (USHORT) m_RegInfo.idProduct;
m_FullSpeedDeviceDesc.bcdDevice = m_HighSpeedDeviceDesc.bcdDevice = (USHORT) m_RegInfo.bcdDevice;
DWORD cStrings = dim(m_rgpszStrings0409);
DWORD iSerialNumber = 3;
if (m_RegInfo.szSerialNumber[0] == 0) {
DWORD dwSuccessSerialNumber = UfnGetSystemSerialNumber(
m_RegInfo.szSerialNumber, dim(m_RegInfo.szSerialNumber));
if (dwSuccessSerialNumber != ERROR_SUCCESS) {
// No serial number
cStrings = dim(m_rgpszStrings0409) - 1;
iSerialNumber = 0;
}
}
m_rgStringSets.cStrings = cStrings;
m_HighSpeedDeviceDesc.iSerialNumber = (UCHAR) iSerialNumber;
m_FullSpeedDeviceDesc.iSerialNumber = (UCHAR) iSerialNumber;
UfnCheckPID(&m_HighSpeedDeviceDesc, &m_FullSpeedDeviceDesc,
PID_MICROSOFT_SERIAL_PROTOTYPE);
// Register the descriptor tree with device controller
m_fInterrupt = TRUE;
dwRet = StartUSBFunction();
}
return (dwRet == ERROR_SUCCESS );
}
}
return FALSE;
}
DWORD CUsbFn::StartUSBFunction()
{
DWORD dwRet = ERROR_GEN_FAILURE;
#ifdef USE_INTERRUPT_ENDPOINT
dwRet = m_pUfnFuncs->lpRegisterDevice(m_hDevice,&m_HighSpeedDeviceDesc, &m_HighSpeedConfig1,
&m_FullSpeedDeviceDesc, &m_FullSpeedConfig1,&m_rgStringSets, 1 );
#endif
if (dwRet != ERROR_SUCCESS) { // Try Second
m_fInterrupt = FALSE;
dwRet = m_pUfnFuncs->lpRegisterDevice(m_hDevice,&m_HighSpeedDeviceDesc, &m_HighSpeedConfig2,
&m_FullSpeedDeviceDesc, &m_FullSpeedConfig2,&m_rgStringSets, 1 );
}
if (dwRet == ERROR_SUCCESS ) { // Do I need Initial Pipe Here?
dwRet = m_pUfnFuncs->lpStart(m_hDevice, DeviceNotifyStub, this, &m_hDefaultPipe);
}
return dwRet;
}
CUsbFn::~CUsbFn()
{
if (m_lpActivePath)
delete m_lpActivePath;
if (m_pvInterface)
UfnDeinitializeInterface(m_pvInterface);
}
BOOL WINAPI CUsbFn::DeviceNotifyStub(PVOID pvNotifyParameter, DWORD dwMsg, DWORD dwParam)
{
PREFAST_ASSERT(pvNotifyParameter!=NULL);
return ((CUsbFn *)pvNotifyParameter)->DeviceNotify(dwMsg,dwParam);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -