📄 request.cpp
字号:
//////////////////////////////////////////////////////////////////////////
// Copyright @2003 Peng He,Information Science Insititute,XiDian University
// MyNdis_Wdm example
//Abstract:
// Request.cpp
//
// 查询和设置微端口驱动程序或其NIC信息,微端口维护有关其功能和当前状态的信息,以及
// 有关它所控制的每个NIC的信息,该信息类型都是由一个对象标识符(OID)标识,OID是系统
// 定义的
//
//
//Environment:
// kernel mode only
// Version history:
//
// Peng.He - 04/17/03: this source code for USB to fast Ethernet adapter driver
// show how an NDIS miniport driver can interface with an USB device.
// 17-Apr-2003 creation
/////////////////////////////////////////////////////////////////////////
#define BINARY_COMPATIBLE 1 // 对Win9X的兼容性
#define DOBREAKS // 使调试断点使能
#include <ndis.h>
#include <ntddndis.h> // 定义对象标识符
#include "debug.h"
#include "common.h"
#include "USBNDIS.h"
//
// 该驱动程序所需的对象标识符
//
UINT supportedOIDs[] =
{
//
// 所需的通用对象标识符
//
OID_GEN_SUPPORTED_LIST,
OID_GEN_HARDWARE_STATUS,
OID_GEN_MEDIA_SUPPORTED,
OID_GEN_MEDIA_IN_USE,
OID_GEN_MAXIMUM_LOOKAHEAD,
OID_GEN_MAXIMUM_FRAME_SIZE,
OID_GEN_LINK_SPEED,
OID_GEN_TRANSMIT_BUFFER_SPACE,
OID_GEN_RECEIVE_BUFFER_SPACE,
OID_GEN_TRANSMIT_BLOCK_SIZE,
OID_GEN_RECEIVE_BLOCK_SIZE,
OID_GEN_VENDOR_ID,
OID_GEN_VENDOR_DESCRIPTION,
OID_GEN_CURRENT_PACKET_FILTER,
OID_GEN_CURRENT_LOOKAHEAD,
OID_GEN_DRIVER_VERSION,
OID_GEN_MAXIMUM_TOTAL_SIZE,
OID_GEN_PROTOCOL_OPTIONS,
OID_GEN_MAC_OPTIONS,
OID_GEN_MEDIA_CONNECT_STATUS,
OID_GEN_MAXIMUM_SEND_PACKETS,
OID_GEN_VENDOR_DRIVER_VERSION,
//
// 所需的统计对象标识符
//
OID_GEN_XMIT_OK,
OID_GEN_RCV_OK,
OID_GEN_XMIT_ERROR,
OID_GEN_RCV_ERROR,
OID_GEN_RCV_NO_BUFFER,
OID_PNP_CAPABILITIES/*,
OID_PNP_SET_POWER,
OID_PNP_QUERY_POWER,
OID_PNP_ENABLE_WAKE_UP
OID_PNP_ADD_WAKE_UP_PATTERN
OID_PNP_REMOVE_WAKE_UP_PATTERN
OID_PNP_WAKE_UP_PATTERN_LIST
OID_PNP_WAKE_UP_OK
OID_PNP_WAKE_UP_ERROR */
};
//
// 定义PNP和电源管理PM的通用OID
//
#define OID_PNP_CAPABILITIES 0xFD010100
#define OID_PNP_SET_POWER 0xFD010101
#define OID_PNP_QUERY_POWER 0xFD010102
#define OID_PNP_ADD_WAKE_UP_PATTERN 0xFD010103
#define OID_PNP_REMOVE_WAKE_UP_PATTERN 0xFD010104
#define OID_PNP_WAKE_UP_PATTERN_LIST 0xFD010105
#define OID_PNP_ENABLE_WAKE_UP 0xFD010106
//
// PNP和电源管理PM的统计(可选)OID
//
#define OID_PNP_WAKE_UP_OK 0xFD020200
#define OID_PNP_WAKE_UP_ERROR 0xFD020201
/*****************************************************************************
// MiniPortQueryInformation
//
// 查询微端口驱动程序的能力和当前状态
//
*****************************************************************************/
NDIS_STATUS
MiniportQueryInformation(
IN NDIS_HANDLE MiniportAdapterContext, // 指向适配器环境区的句柄
IN NDIS_OID Oid, // 系统定义的OID'
IN PVOID InformationBuffer, // 指向存放OID'信息的缓冲区的指针
IN ULONG InformationBufferLength, // 缓冲区的大小
OUT PULONG BytesWritten, // 指向缓冲区内信息变量的指针
OUT PULONG BytesNeeded // 缓冲区的大小小于该OID'信息时,所需的额外缓冲区的大小
)
{
PUSB_DEVICE Adapter;
NDIS_STATUS status;
UINT speeds;
UINT i;
UINT infoSizeNeeded;
UINT *infoPtr;
ULONG OidCategory = Oid & 0xFF000000;
BOOLEAN fBusy;
static char vendorDesc[] = "Usb Infrared Port";
DEBUGMSG(DBG_FUNC, ("+MiniportQueryInformation\n"));
Adapter = CONTEXT_TO_DEV(MiniportAdapterContext);
ASSERT( NULL != Adapter );
ASSERT( NULL != BytesWritten );
ASSERT( NULL != BytesNeeded );
status = NDIS_STATUS_SUCCESS;
NdisGetCurrentSystemTime( &Adapter->LastQueryTime ); // 返回当前系统时间,用来设置时间戳
Adapter->fQuerypending = TRUE;
if ( NULL == InformationBuffer && InformationBufferLength > 0 )
{
DEBUGMSG(DBG_ERR, (" MiniportQueryInformation() NULL info buffer passed!,InformationBufferLength = dec %d\n",InformationBufferLength));
status = NDIS_STATUS_NOT_ACCEPTED;
*BytesNeeded =0;
*BytesWritten = 0;
goto release;
}
//
// 确定用来存放OID'信息所需的缓冲区大小
//
switch (Oid)
{
case OID_GEN_SUPPORTED_LIST:
infoSizeNeeded = sizeof(supportedOIDs);
break;
case OID_PNP_CAPABILITIES:
infoSizeNeeded = sizeof(NDIS_PNP_CAPABILITIES);
break;
case OID_GEN_DRIVER_VERSION:
infoSizeNeeded = sizeof(USHORT);
break;
case OID_GEN_VENDOR_DESCRIPTION:
infoSizeNeeded = sizeof(vendorDesc);
break;
default:
infoSizeNeeded = sizeof(UINT);
break;
}
//
// 如果驱动程序提供了一个更大的缓冲区,则我们可以完成整个查询
//
if (InformationBufferLength >= infoSizeNeeded)
{
//
// 设置默认值
//
*BytesWritten = infoSizeNeeded;
*BytesNeeded = 0;
switch (Oid)
{
//
// 系统通用OID'
//
case OID_GEN_SUPPORTED_LIST:
DEBUGONCE(DBG_FUNC, (" MiniportQueryInformation(OID_GEN_SUPPORTED_LIST)\n"));
NdisMoveMemory( // 从调用者提供的位置将指定数量的字节拷贝到另一个地方
InformationBuffer, // 目的缓冲区
(PVOID)supportedOIDs, // 源缓冲区
sizeof(supportedOIDs) // 要拷贝的字节数
);
break;
case OID_GEN_HARDWARE_STATUS:
DEBUGONCE(DBG_FUNC, (" MiniportQueryInformation(OID_GEN_HARDWARE_STATUS)\n"));
//
// If we can be called with a context, then we are
// initialized and ready.
//
*(UINT *)InformationBuffer = NdisHardwareStatusReady;
break;
case OID_GEN_MEDIA_SUPPORTED:
DEBUGONCE(DBG_FUNC, (" MiniportQueryInformation(OID_GEN_MEDIA_SUPPORTED)\n"));
*(UINT *)InformationBuffer = NdisMedium802_3;
break;
case OID_GEN_MEDIA_IN_USE:
DEBUGONCE(DBG_FUNC, (" MiniportQueryInformation(OID_GEN_MEDIA_IN_USE)\n"));
*(UINT *)InformationBuffer = NdisMedium802_3;
break;
case OID_GEN_TRANSMIT_BUFFER_SPACE:
DEBUGONCE(DBG_FUNC, (" MiniportQueryInformation(OID_GEN_TRANSMIT_BUFFER_SPACE)\n"));
/*
The amount of memory, in bytes, on the device available
for buffering transmit data.
*/
*(UINT *)InformationBuffer = MAX_PACKET_SIZE+2;
break;
case OID_GEN_RECEIVE_BUFFER_SPACE:
DEBUGONCE(DBG_FUNC, (" MiniportQueryInformation(OID_GEN_RECEIVE_BUFFER_SPACE)\n"));
/*
The amount of memory on the device available
for buffering receive data.
*/
*(UINT *)InformationBuffer = MAX_PACKET_SIZE+4;
break;
case OID_GEN_TRANSMIT_BLOCK_SIZE:
DEBUGONCE(DBG_FUNC, (" MiniportQueryInformation(OID_GEN_TRANSMIT_BLOCK_SIZE)\n"));
/*
The minimum number of bytes that a single net packet
occupies in the transmit buffer space of the device.
For example, on some devices the transmit space is
divided into 256-byte pieces so such a device's
transmit block size would be 256. To calculate
the total transmit buffer space on such a device,
its driver multiplies the number of transmit
buffers on the device by its transmit block size.
For other devices, the transmit block size is
identical to its maximum packet size.
*/
*(UINT *)InformationBuffer = MAX_PACKET_SIZE+2;
break;
case OID_GEN_RECEIVE_BLOCK_SIZE:
DEBUGONCE(DBG_FUNC, (" MiniportQueryInformation(OID_GEN_RECEIVE_BLOCK_SIZE)\n"));
/*
The amount of storage, in bytes, that a single packet
occupies in the receive buffer space of the device
*/
*(UINT *)InformationBuffer = MAX_PACKET_SIZE+4;
break;
case OID_GEN_MAXIMUM_LOOKAHEAD:
DEBUGONCE(DBG_FUNC, (" MiniportQueryInformation(OID_GEN_MAXIMUM_LOOKAHEAD)\n"));
/*
The maximum number of bytes the device can always provide as lookahead data.
If the underlying driver supports multipacket receive indications,
bound protocols are given full net packets on every indication.
Consequently, this value is identical to that
returned for OID_GEN_RECEIVE_BLOCK_SIZE.
*/
*(UINT *)InformationBuffer = MAX_PACKET_SIZE+4;
break;
case OID_GEN_CURRENT_LOOKAHEAD:
DEBUGONCE(DBG_FUNC, (" MiniportQueryInformation(OID_GEN_CURRENT_LOOKAHEAD)\n"));
/*
The number of bytes of received packet data,
excluding the header, that will be indicated
to the protocol driver. For a query,
NDIS returns the largest lookahead size from
among all the bindings. A protocol driver can
set a suggested value for the number of bytes
to be used in its binding; however,
the underlying device driver is never required
to limit its indications to the value set.
If the underlying driver supports multipacket
receive indications, bound protocols are given
full net packets on every indication. Consequently,
this value is identical to that returned for OID_GEN_RECEIVE_BLOCK_SIZE.
*/
*(UINT *)InformationBuffer = MAX_PACKET_SIZE+4;
break;
case OID_GEN_MAXIMUM_FRAME_SIZE:
DEBUGMSG(DBG_FUNC, (" MiniportQueryInformation(OID_GEN_MAXIMUM_FRAME_SIZE)\n"));
/*
The maximum network packet size in bytes
the device supports, not including a header.
For a binding emulating another medium type,
the device driver must define the maximum frame
size in such a way that it will not transform
a protocol-supplied net packet of this size
to a net packet too large for the true network medium.
*/
*(UINT *)InformationBuffer = MAX_PACKET_SIZE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -