📄 whusb20res.c
字号:
//////////////////////////////////////////////////////////////////////////////
//文件名称:Whceb01Res.cpp
//文件功能:与资源分配相关的例程
//文件作者:张伟标
//工作部门:研究一室
//创建时间:2003年8月25日
//修改记录:
//版权所有:维豪信息技术有限公司
//
//Copyright 2003 WellHope Information Technology Corporation, Ltd.
//All rights reserved.
/////////////////////////////////////////////////////////////////////////////
// StartDevice Start the device
//* RetrieveResources Get resources from given list
// StopDevice Stop device
// SetPowerState Set power state
//* DisableDeviceInterrupts Disable device interrupts
//* EnableDeviceInterrupts Enable device interrupts
//* InterruptHandler Handle interrupts
/////////////////////////////////////////////////////////////////////////////
#include "Whusb20.h"
#pragma code_seg("PAGE") // start PAGE section
/////////////////////////////////////////////////////////////////////////////
NTSTATUS RetrieveResources(IN PWHCEB01_DEVICE_EXTENSION dx,IN PCM_RESOURCE_LIST AllocatedResourcesTranslated);
//bool DisableDeviceInterrupts( IN PWHCEB01_DEVICE_EXTENSION dx);
//bool EnableDeviceInterrupts( IN PWHCEB01_DEVICE_EXTENSION dx);
//BOOLEAN InterruptHandler(IN PKINTERRUPT Interrupt, IN PWHCEB01_DEVICE_EXTENSION fdo);
/////////////////////////////////////////////////////////////////////////////
// StartDevice: Start the device
NTSTATUS StartDevice( IN PWHCEB01_DEVICE_EXTENSION dx, IN PCM_RESOURCE_LIST AllocatedResourcesTranslated)
{
NTSTATUS status;
if( dx->GotResources)
return STATUS_SUCCESS;
status = RetrieveResources(dx,AllocatedResourcesTranslated);
if( !NT_SUCCESS(status))
return status;
status = UsbSelectConfiguration(dx); //选择配置
if( NT_SUCCESS( status ) )
{
TRACE0("*****选择配置成功!*****");
//liusf add
status = Whceb01ResetPipe( dx, dx->hWhceb01InterruptInHandle[0]);
if(!NT_SUCCESS(status))
{
TRACE1("*****复位中断IN管道失败!,status=%d*****",status);
}
status = Whceb01ResetPipe( dx, dx->hWhceb01InterruptInHandle[1]);
if(!NT_SUCCESS(status))
{
TRACE1("*****复位中断IN管道失败!,status=%d*****",status);
}
status = Whceb01ResetPipe( dx, dx->hWhceb01InterruptOutHandle[0]);
if(!NT_SUCCESS(status))
{
TRACE1("*****复位中断Out管道失败!,status=%d*****",status);
}
status = Whceb01ResetPipe( dx, dx->hWhceb01InterruptOutHandle[1]);
if(!NT_SUCCESS(status))
{
TRACE1("*****复位中断Out管道失败!,status=%d*****",status);
}
}
/*
// Map memory
if( dx->PortNeedsMapping)
{
dx->PortBase = (PUCHAR)MmMapIoSpace( dx->PortStartAddress, dx->PortLength, MmNonCached);
if( !dx->PortBase)
return STATUS_NO_MEMORY;
}
else
dx->PortBase = (PUCHAR)dx->PortStartAddress.LowPart;
// Disable device
if( !KeSynchronizeExecution( dx->InterruptObject, (PKSYNCHRONIZE_ROUTINE)DisableDeviceInterrupts, (PVOID)dx))
{
if( dx->PortNeedsMapping)
MmUnmapIoSpace( dx->PortBase, dx->PortLength);
return STATUS_INVALID_DEVICE_STATE;
}
// Connect to interrupts
if( dx->GotInterrupt)
{
status = IoConnectInterrupt( &dx->InterruptObject, (PKSERVICE_ROUTINE)InterruptHandler,
(PVOID)dx, NULL, dx->Vector, dx->Irql, dx->Irql, dx->Mode, FALSE, dx->Affinity, FALSE);
if( !NT_SUCCESS(status))
{
if( dx->PortNeedsMapping)
MmUnmapIoSpace( dx->PortBase, dx->PortLength);
return status;
}
}
// Enable device
if( !KeSynchronizeExecution( dx->InterruptObject, (PKSYNCHRONIZE_ROUTINE)EnableDeviceInterrupts, (PVOID)dx))
{
if( dx->GotInterrupt)
IoDisconnectInterrupt(dx->InterruptObject);
if( dx->PortNeedsMapping)
MmUnmapIoSpace( dx->PortBase, dx->PortLength);
return STATUS_INVALID_DEVICE_STATE;
}
*/
// Device is now started
dx->GotResources = TRUE;
return STATUS_SUCCESS;
}
/////////////////////////////////////////////////////////////////////////////
// RetrieveResources: Get resources from given list
NTSTATUS RetrieveResources( IN PWHCEB01_DEVICE_EXTENSION dx, IN PCM_RESOURCE_LIST AllocatedResourcesTranslated)
{
PCM_PARTIAL_RESOURCE_LIST list;
PCM_PARTIAL_RESOURCE_DESCRIPTOR resource ;
ULONG NumResources ;
BOOL GotError ;
ULONG i=0;
if( AllocatedResourcesTranslated==NULL ||
AllocatedResourcesTranslated->Count==0)
{
#ifdef DEBUG_INTERFACE
TRACE0("RetrieveResources: No allocated translated resources");
#endif//DEBUG_INTERFACE
return STATUS_SUCCESS; // or whatever
}
// Get to actual resources
list = &AllocatedResourcesTranslated->List[0].PartialResourceList;
resource = list->PartialDescriptors;
NumResources = list->Count;
#ifdef DEBUG_INTERFACE
TRACE2("RetrieveResources: %d resource lists %d resources", AllocatedResourcesTranslated->Count, NumResources);
#endif//DEBUG_INTERFACE
GotError = FALSE;
// Clear dx
dx->GotInterrupt = FALSE;
dx->GotPortOrMemory = FALSE;
dx->PortInIOSpace = FALSE;
dx->PortNeedsMapping = FALSE;
// Go through each allocated resource
for( i=0; i<NumResources; i++,resource++)
{
switch( resource->Type)
{
case CmResourceTypePort:
if( dx->GotPortOrMemory) { GotError = TRUE; break; }
dx->GotPortOrMemory = TRUE;
dx->PortStartAddress = resource->u.Port.Start;
dx->PortLength = resource->u.Port.Length;
dx->PortNeedsMapping = (resource->Flags & CM_RESOURCE_PORT_IO)==0;
dx->PortInIOSpace = !dx->PortNeedsMapping;
TRACE3("RetrieveResources: Port %L Length %d NeedsMapping %d",
dx->PortStartAddress,
dx->PortLength, dx->PortNeedsMapping);
break;
case CmResourceTypeInterrupt:
dx->GotInterrupt = TRUE;
dx->Irql = (KIRQL)resource->u.Interrupt.Level;
dx->Vector = resource->u.Interrupt.Vector;
dx->Affinity = resource->u.Interrupt.Affinity;
dx->Mode = (resource->Flags == CM_RESOURCE_INTERRUPT_LATCHED)
? Latched : LevelSensitive;
TRACE4("RetrieveResources: Interrupt vector %x IRQL %d Affinity %d Mode %d",
dx->Vector, dx->Irql, dx->Affinity, dx->Mode);
break;
case CmResourceTypeMemory:
if( dx->GotPortOrMemory) { GotError = TRUE; break; }
dx->GotPortOrMemory = TRUE;
dx->PortStartAddress = resource->u.Memory.Start;
dx->PortLength = resource->u.Memory.Length;
dx->PortNeedsMapping = TRUE;
TRACE2("RetrieveResources: Memory %L Length %d",
dx->PortStartAddress, dx->PortLength);
break;
case CmResourceTypeDma:
case CmResourceTypeDeviceSpecific:
case CmResourceTypeBusNumber:
default:
TRACE1("RetrieveResources: Unrecognised resource type %d", resource->Type);
GotError = TRUE;
break;
}
}
// Check we've got the resources we need
if( GotError /*|| !GotPortOrMemory || !GotInterrupt*/)
return STATUS_DEVICE_CONFIGURATION_ERROR;
return STATUS_SUCCESS;
}
/////////////////////////////////////////////////////////////////////////////
// StopDevice: Stop device
VOID StopDevice( IN PWHCEB01_DEVICE_EXTENSION dx)
{
TRACE0("Enter StopDevice");
if( !dx->GotResources)
return;
UsbDeselectConfiguration(dx);
dx->GotResources = FALSE;
/*
// Disable device
KeSynchronizeExecution( dx->InterruptObject, (PKSYNCHRONIZE_ROUTINE)DisableDeviceInterrupts, (PVOID)dx);
// Disconnect from interrupt
if( dx->GotInterrupt)
IoDisconnectInterrupt( dx->InterruptObject);
dx->InterruptObject = NULL;
// Unmap memory
if (dx->PortNeedsMapping)
MmUnmapIoSpace( (PVOID)dx->PortBase, dx->PortLength);
*/
}
/////////////////////////////////////////////////////////////////////////////
/*
void WriteByte( IN PWHCEB01_DEVICE_EXTENSION dx, IN UCHAR byte, IN ULONG offset)
{
if (dx->PortInIOSpace)
WRITE_PORT_UCHAR(dx->PortBase+offset, byte);
else
WRITE_REGISTER_UCHAR(dx->PortBase+offset, byte);
}
*/
/////////////////////////////////////////////////////////////////////////////
#pragma code_seg() // end PAGE section
/////////////////////////////////////////////////////////////////////////////
// DisableDeviceInterrupts: Disable device interrupts
/*
bool DisableDeviceInterrupts( IN PWHCEB01_DEVICE_EXTENSION dx)
{
DebugPrintMsg("DisableDeviceInterrupts");
WriteByte( CONTROL_REGISTER, ~INTERRUPTS_ENABLED);
dx->Enabled = false;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// EnableDeviceInterrupts: Enable device interrupts
bool EnableDeviceInterrupts( IN PWHCEB01_DEVICE_EXTENSION dx)
{
DebugPrintMsg("EnableDeviceInterrupts");
WriteByte( CONTROL_REGISTER, INTERRUPTS_ENABLED);
dx->Enabled = true;
return true;
}
/////////////////////////////////////////////////////////////////////////////
// InterruptHandler: Handle interrupts
BOOLEAN InterruptHandler(IN PKINTERRUPT Interrupt, IN PWHCEB01_DEVICE_EXTENSION dx)
{
// Do not call DebugPrint here
return FALSE;
}
*/
/////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -