/*
* Copyright (C) 2004-2010 Kazuyoshi Aizawa. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/****************************************************************************
* ste_local.c
*
* NDIS のエントリポイント以外の Ste.sys ドライバ固有の関数が記述されている
*
* 変更履歴:
* 2006/05/05
* o MAC アドレスをドライバーのインストール時に動的に生成するようにした。
*
*****************************************************************************/
#include "ste.h"
#include <ntstrsafe.h>
/****************************************************************************
* SteCreateAdapter()
*
* STE_ADAPTER 構造体を確保し、その他、必要なリソースを確保、初期化する。
*
* 引数:
*
* Adapter : アダプタ構造体のポインタのアドレス
*
* 戻り値:
*
* 成功時 : NDIS_STATUS_SUCCESS
*
***************************************************************************/
NDIS_STATUS
SteCreateAdapter(
IN OUT STE_ADAPTER **pAdapter
)
{
STE_ADAPTER *Adapter = NULL;
NDIS_STATUS Status;
INT i;
LARGE_INTEGER SystemTime;
DEBUG_PRINT0(3, "SteCreateAdapter called\n");
do {
//
// STE_ADAPTER を確保
//
Status = NdisAllocateMemoryWithTag(
(PVOID)&Adapter, //OUT PVOID *
sizeof(STE_ADAPTER), //IN UINT
(ULONG)'CrtA' //IN ULONG
);
if (Status != NDIS_STATUS_SUCCESS){
DEBUG_PRINT1(1, "SteCreateAdapter: NdisAllocateMemoryWithTag Failed (0x%x)\n",
Status);
break;
}
DEBUG_PRINT1(3, "SteCreateAdapter: Adapter = 0x%p\n", Adapter);
NdisZeroMemory(Adapter, sizeof(STE_ADAPTER));
//
// 受信用バッファープールを確保
//
NdisAllocateBufferPool(
&Status, //OUT PNDIS_STATUS
&Adapter->RxBufferPoolHandle, //OUT PNDIS_HANDLE
STE_MAX_RXDS //IN UINT
);
if (Status != NDIS_STATUS_SUCCESS){
DEBUG_PRINT1(1, "SteCreateAdapter: NdisAllocateBufferPool Failed (0x%x)\n",Status);
break;
}
//
// 受信用パケットプールを確保
//
NdisAllocatePacketPool(
&Status, //OUT PNDIS_STATUS
&Adapter->RxPacketPoolHandle, //OUT PNDIS_HANDLE
STE_MAX_RXDS, //IN UINT
PROTOCOL_RESERVED_SIZE_IN_PACKET //IN UINT
);
if (Status != NDIS_STATUS_SUCCESS){
DEBUG_PRINT1(1, "SteCreateAdapter: NdisAllocatePacketPool Failed (0x%x)\n", Status);
break;
}
// 受信用の Queue を初期化
NdisInitializeListHead(&Adapter->RecvQueue.List);
NdisAllocateSpinLock(&Adapter->RecvQueue.SpinLock);
Adapter->RecvQueue.QueueMax = STE_QUEUE_MAX;
Adapter->RecvQueue.QueueCount = 0;
//送信用の Queue を初期化
NdisInitializeListHead(&Adapter->SendQueue.List);
NdisAllocateSpinLock(&Adapter->SendQueue.SpinLock);
Adapter->SendQueue.QueueMax = STE_QUEUE_MAX;
Adapter->SendQueue.QueueCount = 0;
// アダプター内のロックを初期化
NdisAllocateSpinLock(&Adapter->AdapterSpinLock);
NdisAllocateSpinLock(&Adapter->SendSpinLock);
NdisAllocateSpinLock(&Adapter->RecvSpinLock);
// パケットフィルターの初期値をセット
// 不要なような気もする。
Adapter->PacketFilter |=
(NDIS_PACKET_TYPE_ALL_MULTICAST| NDIS_PACKET_TYPE_BROADCAST |NDIS_PACKET_TYPE_DIRECTED);
// グローバルのアダプタリストに追加
NdisAcquireSpinLock(&SteGlobalSpinLock);
Status = SteInsertAdapterToList(Adapter);
NdisReleaseSpinLock(&SteGlobalSpinLock);
if(Status != NDIS_STATUS_SUCCESS){
DEBUG_PRINT1(1, "SteCreateAdapter: SteInsertAdapterToList Failed (0x%x)\n", Status);
break;
}
//
// MAC アドレスを登録
// 第一オクテットの U/L ビットを 1(=ローカルアドレス)にセットする。
// 後半の 32 bit はドライバがインストールされた時間から決める。
//
KeQuerySystemTime(&SystemTime);
Adapter->EthernetAddress[0] = 0x0A;
Adapter->EthernetAddress[1] = 0x00;
Adapter->EthernetAddress[2] = (UCHAR)((SystemTime.QuadPart >> 24) & 0xff);
Adapter->EthernetAddress[3] = (UCHAR)((SystemTime.QuadPart >> 16) & 0xff);
Adapter->EthernetAddress[4] = (UCHAR)((SystemTime.QuadPart >> 8) & 0xff);
Adapter->EthernetAddress[5] = (UCHAR)((SystemTime.QuadPart + Adapter->Instance) & 0xff);
} while(FALSE);
if(Status != NDIS_STATUS_SUCCESS && Adapter != NULL){
//
// エラー時には確保したリソースを開放する
//
// バッファープールを開放
if(Adapter->RxBufferPoolHandle != NULL)
NdisFreeBufferPool(Adapter->RxBufferPoolHandle);
// パケットプールを開放
if(Adapter->RxPacketPoolHandle != NULL)
NdisFreePacketPool(Adapter->RxPacketPoolHandle);
// STE_ADAPTER 構造体を開放
NdisFreeMemory(
(PVOID)Adapter, //OUT PVOID
sizeof(STE_ADAPTER), //IN UINT
0 //IN UINT
);
} else {
//
// 成功時は確保したアダプターのアドレスをセット
//
*pAdapter = Adapter;
}
return(Status);
}
/****************************************************************************
* SteDeleteAdapter()
*
* STE_ADAPTER 構造体を削除し、リソースを解除する。
*
* 引数:
*
* Adapter : 開放する STE_ADAPTER 構造体
*
* 戻り値:
*
* 正常時 : NDIS_STATUS_SUCCESS
*
***************************************************************************/
NDIS_STATUS
SteDeleteAdapter(
IN STE_ADAPTER *Adapter
)
{
NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
NDIS_PACKET *Packet = NULL;
DEBUG_PRINT0(3, "SteDeleteAdapter called\n");
// 送信キューに入っているパケットを処理する
while (SteGetQueue(&Adapter->SendQueue, &Packet) == NDIS_STATUS_SUCCESS) {
NdisMSendComplete(
Adapter->MiniportAdapterHandle, //IN NDIS_HANDLE
Packet, //IN PNDIS_PACKET
NDIS_STATUS_REQUEST_ABORTED //IN NDIS_STATUS
- 1
- 2
- 3
前往页