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

📄 drvinit.c

📁 ddk编写的usb驱动源代码
💻 C
字号:
#include "zjHMCFUsb.h"

t_Globals Globals;

// start INIT section,这里面的代码只执行一次,执行完以后就释放
// 其占用的所有资源
#pragma code_seg("INIT")	

/************************************************************************************************
* Function Type	:	global
* IRQL			:	PASSIVE_LEVEL
* Parameter		:	DriverObject		-	驱动程序对象地址,由windows创建传给驱动程序
					RegistryPath		-	注册表中驱动程序服务键的路径字符串
* Return Value	:	驱动程序状态常数
* Desc?Desc:""ription		:	驱动程序入口函数
*************************************************************************************************/
NTSTATUS DriverEntry(	IN PDRIVER_OBJECT DriverObject,
						IN PUNICODE_STRING RegistryPath)
{
	PUNICODE_STRING registryPath;
	NTSTATUS ntStatus = STATUS_SUCCESS;

	hwDbgPrint("-->>>>>> DriverEntry()\n");
	hwDbgPrint ( "DriverObject = 0x%x\n",DriverObject );
	hwDbgPrint ( "RegistryPath=%T)\n",RegistryPath );
	PrintCurIrql();

	//把注册表项复制到全局变量Globals.BulkUsb_RegistryPath中
	registryPath = &Globals.zjHMCFUSB_RegistryPath;
	registryPath->MaximumLength	= RegistryPath->Length + sizeof(UNICODE_NULL);
	registryPath->Length			= RegistryPath->Length;
	registryPath->Buffer			= ExAllocatePool(PagedPool,registryPath->MaximumLength);
	if(!registryPath->Buffer)
	{
		ntStatus = STATUS_INSUFFICIENT_RESOURCES;
		return ntStatus;
	}
	RtlZeroMemory(registryPath->Buffer,registryPath->MaximumLength);
	RtlMoveMemory(registryPath->Buffer,RegistryPath->Buffer,RegistryPath->Length);

	DriverObject->DriverExtension->AddDevice				= zjHMCFUsb_AddDevice;
	DriverObject->MajorFunction[IRP_MJ_PNP]				= zjHMCFUsb_PNP;
	DriverObject->MajorFunction[IRP_MJ_POWER]				= zjHMCFUsb_PowerIrp;
	DriverObject->MajorFunction[IRP_MJ_CREATE]			= zjHMCFUsb_Create;
	DriverObject->MajorFunction[IRP_MJ_CLOSE]				= zjHMCFUsb_Close;
	DriverObject->MajorFunction[IRP_MJ_CLEANUP]			= zjHMCFUsb_Cleanup;
	DriverObject->MajorFunction[IRP_MJ_READ]				= zjHMCFUsb_Read;
	DriverObject->MajorFunction[IRP_MJ_WRITE]				= zjHMCFUsb_Write;
	DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL]	= zjHMCFUsb_IOCTL;
	DriverObject->MajorFunction[IRP_MJ_SYSTEM_CONTROL]	= zjHMCFUsb_SysControl;
 	DriverObject->DriverUnload							= zjHMCFUsb_Unload;	

	hwDbgPrint("DriverEntry() -->>>>>>\n");

	return ntStatus;
}

#pragma code_seg() // end INIT section

// start PAGE section,这里面的代码是可分页的代码
#pragma code_seg("PAGE")
/************************************************************************************************
* Function Type	:	global
* IRQL			:	PASSIVE_LEVEL
* Parameter		:	DriverObject		-	驱动程序对象地址,由windows创建传给驱动程序
					RegistryPath		-	注册表中驱动程序服务键的路径字符串
* Return Value	:	驱动程序状态常数
* Desc?Desc:""ription		:	驱动程序入口函数
*************************************************************************************************/
VOID zjHMCFUsb_Unload(IN PDRIVER_OBJECT DriverObject)
{
	PUNICODE_STRING registryPath = &Globals.zjHMCFUSB_RegistryPath;
	hwDbgPrint("-->>>>>> zjHMCFUsb_Unload(DriverObject = 0x%x)\n",DriverObject);
	PrintCurIrql();
	FreeIfAllocated(registryPath->Buffer);
	return;
}

BOOLEAN InitDx(PDEVICE_EXTENSION dx)
{
	RtlZeroMemory(dx,sizeof(DEVICE_EXTENSION));
	//没有任何io处理或并且设备被移走时触发这个事件
	KeInitializeEvent(&dx->RemoveEvent, NotificationEvent, FALSE);
	// this event is triggered when self-requested power irps complete
	KeInitializeEvent(&dx->SelfRequestedPowerIrpEvent, NotificationEvent, FALSE);
	//没有IO处理(pending io count == 1 )时触发这个事件
	KeInitializeEvent(&dx->NoPendingIoEvent, NotificationEvent, FALSE);
	//保护inc/dec iocount logic的自旋锁
	KeInitializeSpinLock (&dx->IoCountSpinLock);
	//保护zjHMCFUsb_StagedReadWrite()中的test of dx->BaseIrp用的自旋锁
	KeInitializeSpinLock(&dx->FastCompleteSpinlock);
	//default maximum transfer size per staged io request
	dx->MaximumTransferSize =  zjHMCFUsb_MAX_TRANSFER_SIZE ;
	// 分配用于读写的urb内存空间
	dx->TransferDataUrb = ExAllocatePool(NonPagedPool, sizeof ( struct _URB_BULK_OR_INTERRUPT_TRANSFER ));
	if ( !dx->TransferDataUrb )
	{
		return FALSE;
	}
	return TRUE;
}

void FreeDx(PDEVICE_EXTENSION dx)
{
	// Free device descriptor structure	
	FreeIfAllocated(dx->UsbDeviceDescriptor);
	// Free pipe info structs
	FreeIfAllocated( dx->PipeState );
	// Free up the UsbInterface structure
	FreeIfAllocated(dx->UsbInterface);
	// free up the USB config discriptor
	FreeIfAllocated(dx->UsbConfigurationDescriptor);
	FreeIfAllocated ( dx->TransferDataUrb );
}

//////////////////////////////////////////////////////////////////////////////
#pragma code_seg() // end PAGE section

#if DBG
VOID PrintCurIrql()
{
/*
	KIRQL		CurIrql;
	CurIrql = KeGetCurrentIrql();
	switch (CurIrql )
	{
	case PASSIVE_LEVEL:
		hwDbgPrint ( "Current Irql equal to PASSIVE_LEVEL \n" );
		break;
	case APC_LEVEL:
		hwDbgPrint ( "Current Irql equal to APC_LEVEL \n" );
		break;
	case DISPATCH_LEVEL:
		hwDbgPrint ( "Current Irql equal to DISPATCH_LEVEL \n" );
		break;
	case PROFILE_LEVEL:
		hwDbgPrint ( "Current Irql equal to PROFILE_LEVEL/PC_LEVEL \n" );
		break;
	case SYNCH_LEVEL:
		hwDbgPrint ( "Current Irql equal to SYNCH_LEVEL \n" );
		break;
	case IPI_LEVEL:
		hwDbgPrint ( "Current Irql equal to IPI_LEVEL \n" );
		break;
	case POWER_LEVEL:
		hwDbgPrint ( "Current Irql equal to POWER_LEVEL \n" );
		break;
	case HIGH_LEVEL:
		hwDbgPrint ( "Current Irql equal to HIGH_LEVEL \n" );
		break;
	default:
		hwDbgPrint ( "Current Irql equal to UNKNOWN \n" );
	}
*/
}
#endif

⌨️ 快捷键说明

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