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

📄 init.c

📁 driver wdk
💻 C
📖 第 1 页 / 共 4 页
字号:
		pDE->DataCommonBuffer1.Length = 
			 NUMOFAUDIODESCRIPTOR *sizeof(AUDIOSTREAMSECTOR);
		pDE->DataCommonBuffer1.BaseAddress = 
#ifdef WIN_NT
			HalAllocateCommonBuffer
#else
		   (((pDE->pAdapter)->DmaOperations)->AllocateCommonBuffer)
#endif
			(
			pDE->pAdapter,
			pDE->DataCommonBuffer1.Length,
			&pDE->DataCommonBuffer1.LogicalAddress,
			FALSE //CacheEnabled
			);
#ifdef WIN_NT
		ASSERT(pDE->DataCommonBuffer1.BaseAddress != NULL);
#else
		if(pDE->DataCommonBuffer1.BaseAddress != NULL)
		{
		  DebugPrintMsg("Allocate CommmBuffer1 Ok!");
		}
		else
		{
		 DebugPrintMsg("Allocate CommmBuffer1 Error!");
		 return FALSE;
		}
#endif
		pDE->DataCommonBuffer1.DMAAddress=
			pDE->DataCommonBuffer1.LogicalAddress.u.LowPart;

		InitAudioDespList(
			&pDE->AudioBufferList1,
 			pDE->DataCommonBuffer1.BaseAddress,
			pDE->DespBuffer1,
			NUMOFAUDIODESCRIPTOR);
		RtlZeroMemory(&pDE->Stat[0],sizeof(AVE2K_STATISTICS));
	}
	if(pDE->Compress && pDE->Double){
	//Allocate common buffer for DMA of channel 2
		pDE->DataCommonBuffer2.Length = 
			 NUMOFAUDIODESCRIPTOR *sizeof(AUDIOSTREAMSECTOR);
		pDE->DataCommonBuffer2.BaseAddress = 
#ifdef WIN_NT
			HalAllocateCommonBuffer
#else
		   (((pDE->pAdapter)->DmaOperations)->AllocateCommonBuffer)
#endif
			(
			pDE->pAdapter,
			pDE->DataCommonBuffer2.Length,
			&pDE->DataCommonBuffer2.LogicalAddress,
			FALSE //CacheEnabled
			);
#ifdef WIN_NT
		ASSERT(pDE->DataCommonBuffer2.BaseAddress != NULL);
#else
		if(pDE->DataCommonBuffer2.BaseAddress != NULL)
		{
			DebugPrintMsg("Allocate CommmBuffer2 Ok!");
		}
		else
		{
			DebugPrintMsg("Allocate CommmBuffer2 Error!");
			return FALSE;
		}
#endif
		pDE->DataCommonBuffer2.DMAAddress=
			pDE->DataCommonBuffer2.LogicalAddress.u.LowPart;

		InitAudioDespList(
			&pDE->AudioBufferList2,
 			pDE->DataCommonBuffer2.BaseAddress,
			pDE->DespBuffer2,
			NUMOFAUDIODESCRIPTOR);
		RtlZeroMemory(&pDE->Stat[1],sizeof(AVE2K_STATISTICS));
	}

	//Allocate common buffer for bitmap capture DMA
	pDE->BitmapCommonBuffer.Length = 
			 720*576*4;
	pDE->BitmapCommonBuffer.BaseAddress = 
#ifdef WIN_NT
		HalAllocateCommonBuffer
#else
	   (((pDE->pAdapter)->DmaOperations)->AllocateCommonBuffer)
#endif
			(
			pDE->pAdapter,
			pDE->BitmapCommonBuffer.Length,
			&pDE->BitmapCommonBuffer.LogicalAddress,
			FALSE //CacheEnabled
			);
	if(pDE->BitmapCommonBuffer.BaseAddress == NULL)
		return FALSE;

	pDE->BitmapCommonBuffer.DMAAddress=
		pDE->BitmapCommonBuffer.LogicalAddress.u.LowPart;

	//
	// Prepare the DPC and Device Queue objects
	// used for second channel operations
	//
	KeInitializeDpc(
		&pDE->AlternateDpc,
		Ave2kDpcForChannel2,
		pDE->DeviceObject);

	if(pDE->ExtraFunctions&0x02)
		KeInitializeDpc(
		&pDE->OSDDpc,
		Ave2kDpcForOSD,
		pDE->DeviceObject);
	
	KeInitializeTimer(&pDE->OSDTimer);
//added on 03-03-31 for audio volume report
	KeInitializeDpc(&pDE->AudioDpc,Ave2kDpcForAudioReport,pDE->DeviceObject);
	KeInitializeTimer(&pDE->AudioTimer);
	
	KeInitializeDeviceQueue( 
		&pDE->AlternateIrpQueue );

	//other data
#if(TARGET_DEVICE==TD_AVEIII)
	pDE->VideoDataRate[0] = 1151600/400;
  	pDE->VideoDataRate[1] = 1151600/400;
#else
	pDE->VideoDataRate[0] = 1130800/400;
  	pDE->VideoDataRate[1] = 1130800/400;
#endif
	pDE->AudioDataRate[0] = ARATE_224;
	pDE->AudioDataRate[1] = ARATE_224;
	pDE->DataFilter[0] = AVE2K_STREAM_MULTIP;
	pDE->DataFilter[1] = AVE2K_STREAM_MULTIP;
	pDE->IERValue = 0L;
	pDE->FrameRate=FRMRATE_FULL;
    KeInitializeSpinLock(&pDE->ReadLock);
	KeInitializeSpinLock(&pDE->ReadLock2);
	KeInitializeSpinLock(&pDE->DebiLock);
	KeInitializeSpinLock(&pDE->RPSLock);
	pDE->ExStat[0]=0;
	pDE->ExStat[1]=0;
	pDE->nErrorRecover[0]=ERR_NONE;
	pDE->nErrorRecover[1]=ERR_NONE;
	pDE->TestCount[0]=0;
	pDE->TestCount[1]=0;
	pDE->VideoType[0]=VIDEOTYPE_UNKNOWN;
	pDE->VideoType[1]=VIDEOTYPE_UNKNOWN;
	pDE->bBitmapCaptureLock=FALSE;
	KeInitializeEvent(
		&pDE->EventVDone, 
		SynchronizationEvent, 
		FALSE);
	pDE->bLogoEnable[0]=0;
	pDE->bLogoEnable[1]=0;
	pDE->nOffFrame[0]=0;
	pDE->nOffFrame[1]=0;
	pDE->bBitmapOK=FALSE;
	pDE->nOSDMode=0;
	pDE->nOSDCorner=0;
	pDE->bOSDMaskEnable=FALSE;
	pDE->lMaxLevel=3;
	//if(pDE->BoardVersion==REVD1)
	//	pDE->bFirst=TRUE;
	//else
	//	pDE->bFirst=FALSE;
	pDE->VideoType[0] = GetVideoType(pDE, 0);	
	pDE->nPreVideoType[0]=VIDEOTYPE_NONE;
	pDE->nPreVideoType[1]=VIDEOTYPE_NONE;
	
	/*switch(pDE->VideoType[Channel]){
	case VIDEOTYPE_NONE:
		Format=AVE2K_VIDEO_FORMAT_NONE;
		break;
	case VIDEOTYPE_PAL:
		Format=AVE2K_VIDEO_FORMAT_PAL;
		break;
	case VIDEOTYPE_NTSC:
		Format=AVE2K_VIDEO_FORMAT_NTSC;
		break;
	}*/
	return TRUE;
}
void InitializeHardware(PDEVICE_EXTENSION pDE)
{
	int bFirst=TRUE;
RESTART:
	//Now, reset the chip
	Ave2kWriteRegister(pDE, MC1, 0x80000000); //master reset
	ave2kDelay(1);
	Ave2kWriteRegister(pDE, PCI_BT_A, 0x07070707); //audio burst length
	//Ave2kWriteRegister(pDE, PCI_BT_V1, 0x1d1d1d1d); //video1 burst length
	Ave2kWriteRegister(pDE, PCI_BT_V1, 0x1f1f1f1f); //video1 burst length
	Ave2kWriteRegister(pDE, MC2, 0x00040004); //Upload video1
	Ave2kWriteRegister(pDE, GPIO_CTRL, 0x10101010); 
	//init Dual D1 port (A&B)
	Ave2kWriteRegister(pDE, DD1_INIT, 0x07000700);
	InitDebiPort(pDE);

   	//This function must be called before ReInit7111
	OpenIIC(pDE);

	//Read EEPROM Information
	GetBoardInformation(pDE);
	if(pDE->Compress){
		//Config audio channel
		ConfigAudio(pDE);
		if(!pDE->Double)
			DisableDataTSL(pDE);
		if(bFirst){
			bFirst=FALSE;
			goto RESTART;
		}
		//Load code to DSP
		Ave2kLoadDSPCode(pDE);
	}
	if(pDE->VolumeControl==VOL_M62429){
		SetM62429Volume(pDE, 0, 90);
		SetM62429Volume(pDE, 1, 90);
	}
	if(pDE->VolumeControl==VOL_ANASWTCH)
		SetAnalogSwitchVolume(pDE, 2);
	if(pDE->ExtraFunctions){
		SetCompressFrameRate(pDE, 1, 0);
		SetAudioMonitor(pDE, 0);
		SetAudioSource(pDE, 1);
	}

	//Initialize video decoder
	ReInit7111(pDE, 0);
	if(pDE->Double)
		ReInit7111(pDE, 1);
	if(pDE->ExtraFunctions&0x02)
		InitOSD(pDE, TRUE);
}
#if _WIN32_WINNT==0x0500
/*************************pnp support*****************************/
NTSTATUS
Ave2kAddDevice(
    IN PDRIVER_OBJECT DriverObject,
    IN PDEVICE_OBJECT PhysicalDeviceObject
    )
{
    NTSTATUS                status = STATUS_SUCCESS;
    PDEVICE_OBJECT          deviceObject = NULL;
    PDEVICE_EXTENSION      deviceInfo; 

	UNICODE_STRING deviceName;
	WCHAR deviceNameBuffer[ AVE2K_MAX_NAME_LENGTH ];
	
	UNICODE_STRING linkName;
	WCHAR linkNameBuffer[ AVE2K_MAX_NAME_LENGTH ];
	
	UNICODE_STRING	number;
	WCHAR numberBuffer[10];
    
 	PAGED_CODE();
    	
	 DebugPrintMsg("In the AddDevice!");
    DebugPrint("DevNum:%d",NtDeviceNumber); 
#if(TARGET_DEVICE==TD_AVEIII)   
	if(NtDeviceNumber>3){
		 DebugPrintMsg("More than 1 devices!");
		return STATUS_UNSUCCESSFUL;
	 }
#endif
	 number.Buffer = numberBuffer;
	number.MaximumLength = 10;
	
	//
   	// Form the base  device name...
   	//
	deviceName.Buffer = deviceNameBuffer;
	deviceName.MaximumLength = AVE2K_MAX_NAME_LENGTH;
	deviceName.Length = 0;
   	RtlAppendUnicodeToString( 
		&deviceName,
   		AVE2K_NT_DEVICE_NAME );

    
   	//
	// Convert the device number into a string and
	// attach it to the end of the device name.
	//
	number.Length = 0;
	RtlIntegerToUnicodeString( 
		NtDeviceNumber, 
		10, 
		&number ); 
	RtlAppendUnicodeStringToString( 
		&deviceName,
		&number );
    //
    // Create a device object.
    //
    GetDeviceInfo(PhysicalDeviceObject);
    if(BoardVersion==0xffff)//unwanted card
    {
      DebugPrintMsg("Unexpected card!");
	  return STATUS_UNSUCCESSFUL;    
    }
	status = IoCreateDevice  (DriverObject,
                             sizeof (DEVICE_EXTENSION),
                             &deviceName,
                             FILE_DEVICE_UNKNOWN,
                             0,
                             FALSE,
                             &deviceObject);

    if (!NT_SUCCESS (status)) {
        //
        // Either not enough memory to create a deviceobject or another
        // deviceobject with the same name exits. This could happen
        // if you install another instance of this device.
        //
        return status;
    }
    
    	//
   	// Form the Win32 symbolic link name.
   	//
	linkName.Buffer = linkNameBuffer;
	linkName.MaximumLength = AVE2K_MAX_NAME_LENGTH;
	linkName.Length = 0;
	RtlAppendUnicodeToString( 
		&linkName,
		AVE2K_WIN32_DEVICE_NAME );
	//
	// Reset the number string and do another
	// conversion. Win32 device numbers are
	// one greater than the NT equivalent.
	//
	number.Length = 0;
	RtlIntegerToUnicodeString( 
		NtDeviceNumber + 1,
		10,
		&number ); 
	RtlAppendUnicodeStringToString( 
		&linkName,
		&number );
	    
	//        
	// Create a symbolic link so our device is
	// visible to Win32...
	//
 	status = IoCreateSymbolicLink(
 				&linkName, 
				&deviceName );
    
	//
    // See if the symbolic link was created...
    //
	if( !NT_SUCCESS( status )) 
	{
    	IoDeleteDevice( deviceObject );
		return status;
    }
	
    deviceInfo = (PDEVICE_EXTENSION) deviceObject->DeviceExtension;

	deviceInfo->NextLowerDriver = IoAttachDeviceToDeviceStack (
                                       deviceObject,
                                       PhysicalDeviceObject);

    
	if(NULL == deviceInfo->NextLowerDriver) {
    	IoDeleteSymbolicLink(&linkName);
        IoDeleteDevice(deviceObject);
        return STATUS_NO_SUCH_DEVICE;
    }

    //
    // Set the flag if the device is not holding a pagefile
    // crashdump file or hibernate file. 
    // 
    
   #ifdef USING_DIRECT_IO
	deviceObject->Flags |= DO_DIRECT_IO;
   #else
	deviceObject->Flags |= DO_BUFFERED_IO; 
   #endif

	deviceObject->Flags |=  DO_POWER_PAGABLE;

    deviceInfo->DeviceObject = deviceObject;
    deviceInfo->PDO=PhysicalDeviceObject;
	deviceInfo->Removed = FALSE;
    deviceInfo->Started = FALSE;
    deviceInfo->Occupyed=FALSE;
	deviceInfo->BoardVersion=BoardVersion;
	deviceInfo->NtDeviceNumber=NtDeviceNumber;
	NtDeviceNumber=InterlockedIncrement(&NtDeviceNumber);
    DebugPrint("BoardVersion:%d DevNum:%d",BoardVersion,NtDeviceNumber); 
   
	KeInitializeEvent(&deviceInfo->RemoveEvent, 
                      SynchronizationEvent, 
                      FALSE);
    
    deviceInfo->UsageCount = 1; // biased to 1.  Transition to zero during
                                        // remove device means IO is finished.
                                        
	deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;

	RtlWriteRegistryValue(
		RTL_REGISTRY_SERVICES,
		L"ave2k\\",
		L"TotalCard",
		REG_DWORD,
		&NtDeviceNumber,
		4);
	
#if TD_WIN98
	RtlWriteRegistryValue(
		RTL_REGISTRY_SERVICES,
		L"ave2k\\Enum",
		L"Count",
		REG_DWORD,
		&NtDeviceNumber,
		4);

#endif

    return STATUS_SUCCESS;

}

NTSTATUS 
Ave2kCompletionRoutine(
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp,
    IN PVOID            Context
    )
{
    PKEVENT             event;

    event = (PKEVENT) Context;

    UNREFERENCED_PARAMETER(DeviceObject);

    if (Irp->PendingReturned) {
        IoMarkIrpPending(Irp);
    }

    //
    // We could switch on the major and minor functions of the IRP to perform
    // different functions, but we know that Context is an event that needs
    // to be set.
    //
    KeSetEvent(event, 0, FALSE);

    //
    // Allows the caller to reuse the IRP
    //
    return STATUS_MORE_PROCESSING_REQUIRED;
}


NTSTATUS
Ave2kDispatchPnp (
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp
    )
{
    PIO_STACK_LOCATION          irpStack;
    NTSTATUS                    status = STATUS_SUCCESS;
    KEVENT                      event;        
    PDEVICE_EXTENSION          deviceInfo;
  	UNICODE_STRING linkName;
	WCHAR linkNameBuffer[ AVE2K_MAX_NAME_LENGTH ];
	
	UNICODE_STRING	number;
	WCHAR numberBuffer[10];


    PAGED_CODE();

    deviceInfo = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
    irpStack = IoGetCurrentIrpStackLocation(Irp);
    DebugPrint("%s\n", PnPMinorFunctionString(irpStack->MinorFunction));

	/*if (deviceInfo->Removed == TRUE) {
        //
        // Since the device is removed, we will not hold any IRPs.
        // We just fail it.
        //
        Irp->IoStatus.Status = STATUS_DELETE_PENDING;
        IoCompleteRequest (Irp, IO_NO_INCREMENT);
        return STATUS_DELETE_PENDING;
    }
    Ave2kIoIncrement (deviceInfo);*/
	switch (irpStack->MinorFunction) {
    case IRP_MN_START_DEVICE:
        IoCopyCurrentIrpStackLocationToNext(Irp);
        KeInitializeEvent(&event,
                          NotificationEvent,
                          FALSE
                          );
        IoSetCompletionRoutine(Irp,
                               (PIO_COMPLETION_ROUTINE) Ave2kCompletionRoutine, 
                               &event,
                               TRUE,
                               TRUE,
                               TRUE);
                               
        status = IoCallDriver(deviceInfo->NextLowerDriver, Irp);

        if (STATUS_PENDING == status) {
            KeWaitForSingleObject(
               &event,
               Executive, // Waiting for reason of a driver
               KernelMode, // Must be kernelmode if event memory is in stack
               FALSE, // No allert
               NULL); // No timeout
        }

        if (NT_SUCCESS(status) && NT_SUCCESS(Irp->IoStatus.Status)) {

            status = Ave2kStartDevice(DeviceObject, Irp);
            if(NT_SUCCESS(status))
            {
                //
                // As we are successfully now back from our start device
                // we can do work.
                //
                deviceInfo->Started = TRUE;
                deviceInfo->Removed = FALSE;
            }
        }

        //
        // We must now complete the IRP, since we stopped it in the
        // completion routine with STATUS_MORE_PROCESSING_REQUIRED.
        //
        Irp->IoStatus.Status = status;
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
        break;

    case IRP_MN_QUERY_STOP_DEVICE:

        Irp->IoStatus.Status = status = STATUS_UNSUCCESSFUL;
        IoCompleteRequest(Irp, IO_NO_INCREMENT);
       break;
     case IRP_MN_QUERY_REMOVE_DEVICE:
        if(deviceInfo->Occupyed==TRUE)
		{
           DebugPrintMsg("the device is being used,can't be removed!");
		   Irp->IoStatus.Status = status = STATUS_UNSUCCESSFUL;
           IoCompleteRequest(Irp, IO_NO_INCREMENT);
	    }
		else
		{
         deviceInfo->Started=FALSE;
		 Irp->IoStatus.Status = STATUS_SUCCESS;
         IoSkipCurrentIrpStackLocation(Irp);
         status = IoCallDriver(deviceInfo->NextLowerDriver, Irp);
        }    
        break;

    case IRP_MN_SURPRISE_REMOVAL:
         //deviceInfo->Removed = TRUE;
         deviceInfo->Started = FALSE;
         IoSkipCurrentIrpStackLocation(Irp);
         status = IoCallDriver(deviceInfo->NextLowerDriver, Irp);
	break;

	case IRP_MN_REMOVE_DEVICE:
	   deviceInfo->Removed = TRUE;
       deviceInfo->Started = FALSE;
	   IoSkipCurrentIrpStackLocation(Irp);
       status = IoCallDriver(deviceInfo->NextLowerDriver, Irp);
	   IoDetachDevice (deviceInfo->NextLowerDriver);       
       Ave2kPnPReleaseHardware(deviceInfo);
	   IoDeleteDevice(DeviceObject);
     break;     
    case IRP_MN_STOP_DEVICE:

⌨️ 快捷键说明

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