⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isp1581device.cpp

📁 这是一个用ISP1581 USB2.0接口芯片作为图像采集接口的驱动程序。该驱动程序支持Endpoint1 Bulk方式 数据输入传输。实际传输速度可以达到24MBYTE/S
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		
		if (pUrb == NULL)
		{
			I.Information() = 0;
			return I.PnpComplete(this, STATUS_INSUFFICIENT_RESOURCES);
		}

		// Allow a short transfer
		pUrb->UrbBulkOrInterruptTransfer.TransferFlags =
			(USBD_TRANSFER_DIRECTION_OUT | USBD_SHORT_TRANSFER_OK);

		// Submit the URB to our USB device
		status = m_Endpoint4.SubmitUrb( pUrb, NULL, NULL, 1000);
	
		// if successful then calculate the amount of the read bytes
		if ( NT_SUCCESS(status) ) 
		{
			dwBytesWritten = dwBytesWritten + 
						 pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;

			delete pUrb;
			// adjust the buffer pointer
			pBuffer = pBuffer + tempWriteSize;

		}
		else
		{
			break;
		}
	}

	I.Information() = dwBytesWritten;
	return I.PnpComplete(this, status);
}

////////////////////////////////////////////////////////////////////////
// WriteComplete (member of GnganfgDevice)		Completion Handler for IRP_MJ_WRITE
// Input		I			IRP just completed by USB
//				pContext	Context structure containing pointer to Urb
// Output  		NTSTATUS	STATUS_SUCCESS
// Note   		This routine is called when USBD completes the write request

NTSTATUS GnganfgDevice::WriteComplete(KIrp I, USB_COMPLETION_INFO* pContext)
{
	NTSTATUS status = I.Status();
	PURB pUrb = pContext->m_pUrb;
	ULONG nBytesWritten = 0;

	if ( NT_SUCCESS(status) ) 
	{
		nBytesWritten = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;
		if (nBytesWritten > 0) 
			t << "Wrote " << nBytesWritten	<< " bytes to USB\n";
    }

	// Deallocate Urb and context structure
	delete pUrb;
	delete pContext;

	// set returned count
	I.Information() = nBytesWritten;
	
	// Plug and Play accounting
	DecrementOutstandingRequestCount();

	// allow IRP completion processing
	return STATUS_SUCCESS;
}

////////////////////////////////////////////////////////////////////////
// DeviceControl (member of GnganfgDevice)
//
//		Handler for IRP_MJ_DEVICE_CONTROL
//
// Input
//		I		Current IRP
// 
// Output
//		None
//
// Notes:
//		This routine is the first handler for Device Control requests.
//		The KPnpDevice class handles restricting IRP flow
//		if the device is stopping or being removed.
//
NTSTATUS GnganfgDevice::DeviceControl(KIrp I) 
{
	NTSTATUS status;

	t << "Entering GnganfgDevice::Device Control, " << I << EOL;
	switch (I.IoctlCode())
	{
		// Vendor request.
		case FzGdi_IOCTL_SetDeviceCommands:
			status = KBUSBDEV_IOCTL_SetDeviceCommands_Handler(I);
			break;

		// Vendor request.
			case FzGdi_IOCTL_GetDeviceStatus:
			status = KBUSBDEV_IOCTL_GetDeviceStatus_Handler(I);
			break;

		// Calss request.	
		case FzGdi_IOCTL_GetDeviceID:
			status = KBUSBDEV_IOCTL_GetDeviceID_Handler(I);
			break;

		// Calss request.	
		case FzGdi_IOCTL_GetPortStatus:
			status = KBUSBDEV_IOCTL_GetPortStatus_Handler(I);
			break;

		// Calss request.	
		case FzGdi_IOCTL_SoftReset:
			status = KBUSBDEV_IOCTL_SoftReset_Handler(I);
			break;


		default:
			// Unrecognized IOCTL request
			status = STATUS_INVALID_PARAMETER;
			break;
	}

	// If the IRP's IOCTL handler deferred processing using some driver
	// specific scheme, the status variable is set to STATUS_PENDING.
	// In this case we simply return that status, and the IRP will be
	// completed later.  Otherwise, complete the IRP using the status
	// returned by the IOCTL handler.
	if (status == STATUS_PENDING)
	{
		return status;
	}
	else
	{
		return I.PnpComplete(this, status);
	}
}

////////////////////////////////////////////////////////////////////////
//	Routine Description:	Handler for IO Control Code FzGdi_IOCTL_SetDeviceCommands
NTSTATUS GnganfgDevice::KBUSBDEV_IOCTL_SetDeviceCommands_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;
	PURB   pUrb = NULL;
	USHORT wSetCommandParameter;
	UCHAR  bSetCommandData, bSetCommandIndex;
	USHORT  wIndex;
	wIndex  = KAIBO_USB_DEVICE_INDEX; 
	
	if( I.IoctlInputBufferSize(CURRENT) != sizeof(USHORT) )
	{
		status = STATUS_INVALID_BUFFER_SIZE;
		return status;
	}

	wSetCommandParameter = *(USHORT*)(I.IoctlBuffer());
	bSetCommandData = (CHAR)wSetCommandParameter;
	wSetCommandParameter >>= 8;
	bSetCommandIndex = (CHAR)wSetCommandParameter;

	if ( ( bSetCommandIndex == 0 ) || ( bSetCommandIndex == 1 ) )
	{
		I.Information() = sizeof(USHORT);
		return status;
	}

	if ( ( bSetCommandIndex == 2 ) || ( bSetCommandIndex == 3 ) )
	{
		I.Information() = sizeof(USHORT);
		return status;
	}

	wIndex  |= bSetCommandIndex; 

	pUrb = m_Lower.BuildVendorRequest(
						&bSetCommandData,				// transfer buffer
						1,								// transfer buffer size
						0,								// request reserved bits(The recipient is Device)
						0x0C,							// request
						0,								// Value
						FALSE,							// Out 
						TRUE,							// Short Ok
						NULL,							// link urb
						wIndex,							// index
						URB_FUNCTION_VENDOR_DEVICE		// function
						);

	if ( pUrb == NULL)
	{
		I.Information() = 0;
		return I.PnpComplete(this, STATUS_INSUFFICIENT_RESOURCES);
	}

	status = m_Lower.SubmitUrb( pUrb );
	delete pUrb; // end of " setup DMA request "  

	I.Information() = sizeof(USHORT);
	return status;
}

///////////////////////////////////////////////////////////////////////////////////
//	Routine Description:	Handler for IO Control Code FzGdi_IOCTL_GetDeviceStatus
NTSTATUS GnganfgDevice::KBUSBDEV_IOCTL_GetDeviceStatus_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;
	PURB	pUrb = NULL;
	UCHAR   bGetStatusIndex; 
	USHORT  wIndex;
	wIndex  = KAIBO_USB_DEVICE_INDEX; 

	if( (I.IoctlInputBufferSize(CURRENT) != sizeof(UCHAR)) || (I.IoctlOutputBufferSize(CURRENT) != sizeof(UCHAR)) )
	{
		status = STATUS_INVALID_BUFFER_SIZE;
		return status;
	}

	bGetStatusIndex = *(UCHAR*)(I.IoctlBuffer());

	wIndex |= bGetStatusIndex;

	pUrb = m_Lower.BuildVendorRequest(
						(PUCHAR)(I.IoctlBuffer()),		// transfer buffer
						1,								// transfer buffer size
						0,								// request reserved bits(The recipient is Device)
						0x0C,							// request
						0,								// Value
						TRUE,							// In 
						TRUE,							// Short Ok
						NULL,							// link urb
						wIndex,							// index
						URB_FUNCTION_VENDOR_DEVICE		// function
						);

	if ( pUrb == NULL)
	{
		I.Information() = 0;
		return I.PnpComplete(this, STATUS_INSUFFICIENT_RESOURCES);
	}

	status = m_Lower.SubmitUrb( pUrb );
	delete pUrb; // end of " get status request "  

	I.Information() = sizeof(UCHAR);

	return status;
}

//	Routine Description:	Handler for IO Control Code FzGdi_IOCTL_GetDeviceID
NTSTATUS GnganfgDevice::KBUSBDEV_IOCTL_GetDeviceID_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;
	PURB	pUrb = NULL;
	USHORT	wCofigIdex = GET_DEVICE_ID_TOTAL;
	ULONG  	dwGetIdLength = I.IoctlOutputBufferSize(CURRENT);

	if ( I.IoctlInputBufferSize(CURRENT) == sizeof(USHORT) )
	{
		wCofigIdex = *(USHORT*)(I.IoctlBuffer());
	}

	if( dwGetIdLength == 0 )
	{
		I.Information() = 0;
		return I.PnpComplete(this, STATUS_SUCCESS);
	}

	pUrb = m_Lower.BuildClassRequest(
					(PUCHAR)(I.IoctlBuffer()),	// PUCHAR TransferBuffer,
					dwGetIdLength,				// ULONG TransferBufferLength,
					1,							// UCHAR RequestTypeReservedBits(USB Device Class-Print),
					0,							// UCHAR Request(USB Device Class-Print),	
					wCofigIdex,					// USHORT Value(See IEEE-1284),
					TRUE,						// BOOLEAN bIn=FALSE,
					TRUE,						// BOOLEAN bShortOk=FALSE,
					NULL,						// PURB Link=NULL,
					0,							// ** USHORT Index=0 (Our Printer controller only an interface),
					URB_FUNCTION_CLASS_DEVICE,  // USHORT Function=URB_FUNCTION_CLASS_DEVICE,
					NULL						// PURB pUrb=NULL
					);

	if ( pUrb == NULL)
	{
		I.Information() = 0;
		return I.PnpComplete(this, STATUS_INSUFFICIENT_RESOURCES);
	}

	status = m_Lower.SubmitUrb( pUrb );
	delete pUrb; // end of " get status request "  

	I.Information() = pUrb->UrbControlTransfer.TransferBufferLength;
	return status;
}

//	Routine Description:	Handler for IO Control Code FzGdi_IOCTL_GetPortStatus
NTSTATUS GnganfgDevice::KBUSBDEV_IOCTL_GetPortStatus_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;
	PURB	pUrb = NULL;

	if ( I.IoctlOutputBufferSize(CURRENT) != sizeof(UCHAR) )
	{
		status = STATUS_INVALID_BUFFER_SIZE;
		return status;
	}

	// No data is transferred for this request.
	pUrb = m_Lower.BuildClassRequest(
					(PUCHAR)(I.IoctlBuffer()),	// PUCHAR TransferBuffer,	
					sizeof(UCHAR),				// ULONG TransferBufferLength,
					1,							// UCHAR RequestTypeReservedBits(USB Device Class-Print),
					1,							// UCHAR Request(USB Device Class-Print),	
					0,							// USHORT Value(See IEEE-1284),
					FALSE,						// BOOLEAN bIn=FALSE,
					TRUE,						// BOOLEAN bShortOk=FALSE,
					NULL,						// PURB Link=NULL,
					0,							// ** USHORT Index=0 (Our Printer controller only an interface),
					URB_FUNCTION_CLASS_DEVICE,  // USHORT Function=URB_FUNCTION_CLASS_DEVICE,
					NULL						// PURB pUrb=NULL
					);

	if ( pUrb == NULL)
	{
		I.Information() = 0;
		return I.PnpComplete(this, STATUS_INSUFFICIENT_RESOURCES);
	}

	status = m_Lower.SubmitUrb( pUrb );
	delete pUrb; // end of " get status request "  

	I.Information() = sizeof(UCHAR);
	return status;

}

//	Routine Description:	Handler for IO Control Code FzGdi_IOCTL_SoftReset
NTSTATUS GnganfgDevice::KBUSBDEV_IOCTL_SoftReset_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;
	PURB	pUrb = NULL;

	// No data is transferred for this request.
	pUrb = m_Lower.BuildClassRequest(
					NULL,						// PUCHAR TransferBuffer,	
					0,							// ULONG TransferBufferLength,
					3,							// UCHAR RequestTypeReservedBits(USB Device Class-Print),
					2,							// UCHAR Request(USB Device Class-Print),	
					0,							// USHORT Value(See IEEE-1284),
					FALSE,						// BOOLEAN bIn=FALSE,
					TRUE,						// BOOLEAN bShortOk=FALSE,
					NULL,						// PURB Link=NULL,
					0,							// ** USHORT Index=0 (Our Printer controller only an interface),
					URB_FUNCTION_CLASS_DEVICE,  // USHORT Function=URB_FUNCTION_CLASS_DEVICE,
					NULL						// PURB pUrb=NULL
					);

	if ( pUrb == NULL)
	{
		I.Information() = 0;
		return I.PnpComplete(this, STATUS_INSUFFICIENT_RESOURCES);
	}

	status = m_Lower.SubmitUrb( pUrb );
	delete pUrb; // end of " get status request "  

	I.Information() = 0;
	return status;
}

// KernelModeSetDeviceCommand(), Set command that is dependented on device( USB-D12 ).
// wIndex from 0x0400  to 0x0403;
// wIndex = 0x0400 ( SET_FPGA_CONFIG_REQUEST )
// wIndex = 0x0401 ( SET_FPGA_CONFIG_COMPLETE )
// wIndex = 0x0402 ( READ_PRINTER_STATUS_PACKET )
// wIndex = 0x0403 ( READ_VIDEO_FIFO_DATA )
NTSTATUS GnganfgDevice::KernelModeSetDeviceCommand(USHORT wIndex )
{
	NTSTATUS status = STATUS_SUCCESS;
	PURB   pUrb = NULL;

	UCHAR  bSetCommandData = 0;

	pUrb = m_Lower.BuildVendorRequest(
						&bSetCommandData,				// transfer buffer
						1,								// transfer buffer size
						0,								// request reserved bits(The recipient is Device)
						0x0C,							// request
						0,								// Value
						FALSE,							// Out 
						TRUE,							// Short Ok
						NULL,							// link urb
						wIndex,							// index
						URB_FUNCTION_VENDOR_DEVICE		// function
						);

	if ( pUrb == NULL)
	{
		status = STATUS_INSUFFICIENT_RESOURCES;
		return status;
	}

	status = m_Lower.SubmitUrb( pUrb );
	delete pUrb;  

	return status;
}

////////////////////////////////////////////////////////////////////////
// KernelModeGetDeviceStatus Get status that is dependented on device( USB-D12 ).
// wIndex = 0x0400 ( GET_FPGA_CONFIG_REQUEST )
NTSTATUS GnganfgDevice::KernelModeGetDeviceStatus( USHORT wIndex, PUCHAR bDevStatus )
{
	NTSTATUS status = STATUS_SUCCESS;
	PURB	pUrb = NULL;

	pUrb = m_Lower.BuildVendorRequest(
						bDevStatus,			// transfer buffer
						1,								// transfer buffer size
						0,								// request reserved bits(The recipient is Device)
						0x0C,							// request
						0,								// Value
						TRUE,							// In 
						TRUE,							// Short Ok
						NULL,							// link urb
						wIndex,							// index
						URB_FUNCTION_VENDOR_DEVICE		// function
						);

	if ( pUrb == NULL)
	{
		status = STATUS_INSUFFICIENT_RESOURCES;
		return status;
	}

	status = m_Lower.SubmitUrb( pUrb );
	delete pUrb;  

	return status;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -