------------------------------------
// USB30ioctl.h
#ifndef __USB30ioctl__h_
#define __USB30ioctl__h_
#define IOCTL_CTRL_READ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_CTRL_WRITE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_BULK_READ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x802, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_BULK_WRITE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x803, METHOD_BUFFERED, FILE_ANY_ACCESS)
#endif
------------------------------------
// USB30Device.h
....................................
// Unit number for this device (0-9)
ULONG m_Unit;
KUsbLowerDevice m_Lower;
KUsbInterface m_Interface;
// Pipe for USB endpoint 2 IN (address 0x82)
KUsbPipe m_Endpoint2IN;
// Pipe for USB endpoint 6 OUT (address 0x6)
KUsbPipe m_Endpoint6OUT;
....................................
------------------------------------
// USB30Device.CPP
.............................
#define m_buffer_size 1048592;
#define USB_transfer_size 1024
#define USB_WRITE_SIZE 512
............................
m_Unit = Unit;
m_Lower.Initialize(this, Pdo);
m_Interface.Initialize(
m_Lower, //KUsbLowerDevice
0, //InterfaceNumber
1, //ConfigurationValue
0 //Initial Interface Alternate Setting );
// Initialize each Pipe object
m_Endpoint2IN.Initialize(m_Lower, 0x82, 1024);
m_Endpoint6OUT.Initialize(m_Lower, 0x06, 512);
.............................................
...............................................//批量读,每次1024字节
NTSTATUS USB30Device::USB30_BULK_READ(KIrp I)
{
NTSTATUS status = STATUS_INSUFFICIENT_RESOURCES;
t < < "Entering USB30_BULK_READ\n";
PURB pUrb=NULL;
USB_current_buffer=m_buffer1;
pUrb = m_Endpoint2IN.BuildBulkTransfer(
USB_current_buffer, // Where data write to?
USB_transfer_size, // How much data to read?
TRUE, // direction (TRUE = IN)
NULL // Link to next URB);
if ( pUrb != NULL)
{
pUrb->UrbBulkOrInterruptTransfer.TransferFlags =(USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK);
status = m_Endpoint2IN.SubmitUrb(pUrb, NULL, NULL,URB_mSecTimeOut);
t < <"after send SubmitUrb ,status=" < <ULONG(status) < <"\n";
//返回0x00000000
if ( status==0x00000000 )
{
dwBytesRead = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;
}
delete pUrb;
pUrb = NULL;
}
RtlCopyMemory((PUCHAR)Request_Buffer, (PUCHAR)m_buffer1,TotalRead);
I.Information() = TotalRead;
return status;
}
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////批量写,每次512字节
NTSTATUS USB30Device:: USB30_BULK_WRITE(KIrp I)
{ NTSTATUS status = STATUS_INSUFFICIENT_RESOURCES;
t < < "Entering USB30_BULK_WRITE\n";
// Declare a memory object
// KMemory Mem(I.Mdl());
// KMemory Mem( I.IoctlBuffer());
Request_Buffer_char= (PUCHAR) I.IoctlBuffer();
ULONG URB_mSecTimeOut=((PULONG)Request_Buffer_char)[0];
UCHAR WrData_Buf = Request_Buffer_char+4;
ULONG dwBytesSent = 0;
PURB pUrb2=NULL;
pUrb2 = m_Endpoint6OUT.BuildBulkTransfer(
WrData_Buf, // Where the data is from?
USB_WRITE_SIZE, // How much data to write?
FALSE, // direction (FALSE = OUT)
NULL, // Link to next URB
FALSE,
NULL);
t < < "Entering BuildBulkTransfer ok!\n";
if ( pUrb2 != NULL)
{
// pUrb->UrbBulkOrInterruptTransfer.TransferFlags =
// (USBD_TRANSFER_DIRECTION_OUT | USBD_SHORT_TRANSFER_OK);
status = m_Endpoint6OUT.SubmitUrb(pUrb2, NULL, NULL,URB_mSecTimeOut);//
t < <"after send SubmitUrb ,status=" < <ULONG(status) < <"\n";
//返回0x00000102(超时!URB_mSecTimeOut设置为1000ms)
if ( status==0x00000000 )
{
dwBytesSent = pUrb2->UrbBulkOrInterruptTransfer.TransferBufferLength;
}
delete pUrb2;
pUrb2 = NULL;
}
I.Information() = dwBytesSent;
Request_Buffer_char=NULL;
return status;
}
...................................................... |