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

📄 car6koid.cpp

📁 Atheros Communications AR6001 WLAN Driver for SDIO installation Read Me March 26,2007 (based on
💻 CPP
📖 第 1 页 / 共 5 页
字号:

//------------------------------------------------------------------------------
// <copyright file="car6koid.cpp" company="Atheros and Microsoft">
//    Copyright (c) 2006 Microsoft Corporation.  All rights reserved.
//    Copyright (c) 2006 Atheros Corporation.  All rights reserved.
//
//    The use and distribution terms for this software are covered by the
//    Microsoft Limited Permissive License (Ms-LPL) 
//    http://www.microsoft.com/resources/sharedsource/licensingbasics/limitedpermissivelicense.mspx 
//    which can be found in the file MS-LPL.txt at the root of this distribution.
//    By using this software in any fashion, you are agreeing to be bound by
//    the terms of this license.
//
//    You must not remove this notice, or any other, from this software.
// </copyright>
// 
// <summary>
//    Windows CE Wifi Driver for AR-6000
// </summary>
//------------------------------------------------------------------------------
//==============================================================================
// AR6000 NDIS Miniport class oid definitions
//
// Author(s): ="Atheros and Microsoft"
//==============================================================================

#include <windows.h>
#include <ndis.h>

#include "htc_internal.h"
#include "htc.h"
#include "wmi_api.h"
#include "netbuf.h"
#include "ndisnetbuf.h"
#include "athdrv.h"
#include "host_version.h"
extern "C" {
#include "bmi.h"
}

#include "cmini.hpp"
#include "c802_3mini.hpp"
#include "c802_11mini.hpp"
#include "car6k.hpp"
#include "ieee80211.h"
#include "platform.h"
#include "osapi.h"

#ifdef SUPPORT_WPA2
static NDIS_802_11_AUTHENTICATION_ENCRYPTION g_WPA2AuthEncryptPairSupported[] =
{
	{ Ndis802_11AuthModeOpen,    Ndis802_11EncryptionDisabled }, //open no-crypt
	{ Ndis802_11AuthModeOpen,    Ndis802_11Encryption1Enabled }, //open WEP
	{ Ndis802_11AuthModeWPA,     Ndis802_11Encryption2Enabled },
	{ Ndis802_11AuthModeWPA,     Ndis802_11Encryption3Enabled },
	{ Ndis802_11AuthModeWPAPSK,  Ndis802_11Encryption2Enabled },
	{ Ndis802_11AuthModeWPAPSK,  Ndis802_11Encryption3Enabled },
	
	{ (NDIS_802_11_AUTHENTICATION_MODE) Ndis802_11AuthModeWPA2,    Ndis802_11Encryption2Enabled },
	{ (NDIS_802_11_AUTHENTICATION_MODE) Ndis802_11AuthModeWPA2,    Ndis802_11Encryption3Enabled },
	{ (NDIS_802_11_AUTHENTICATION_MODE) Ndis802_11AuthModeWPA2PSK, Ndis802_11Encryption2Enabled },
	{ (NDIS_802_11_AUTHENTICATION_MODE) Ndis802_11AuthModeWPA2PSK, Ndis802_11Encryption3Enabled },
};
#endif //SUPPORT_WPA2

#define ENCRYPT2_GRPWISE_CRYPTOINDEX_MAX 2
#define ENCRYPT3_GRPWISE_CRYPTOINDEX_MAX 3

static CRYPTO_TYPE Encryption2GrpwiseCryptoTable[ENCRYPT2_GRPWISE_CRYPTOINDEX_MAX] =
{
	TKIP_CRYPT,
	WEP_CRYPT
};

static CRYPTO_TYPE Encryption3GrpwiseCryptoTable[ENCRYPT3_GRPWISE_CRYPTOINDEX_MAX] =
{
	AES_CRYPT,
	TKIP_CRYPT,
	WEP_CRYPT
};

#ifdef HTC_RAW_INTERFACE

#define RAW_HTC_READ_BUFFERS_NUM                     16
#define RAW_HTC_WRITE_BUFFERS_NUM                    16

typedef struct {
    int currPtr;
    int length;
    unsigned char data[AR6000_BUFFER_SIZE];
} raw_htc_buffer;

static HANDLE raw_htc_read_sem[HTC_MAILBOX_NUM_MAX];
static HANDLE raw_htc_write_sem[HTC_MAILBOX_NUM_MAX];
static NDIS_EVENT raw_htc_read_queue[HTC_MAILBOX_NUM_MAX];
static NDIS_EVENT raw_htc_write_queue[HTC_MAILBOX_NUM_MAX];

static raw_htc_buffer raw_htc_read_buffer[HTC_MAILBOX_NUM_MAX][RAW_HTC_READ_BUFFERS_NUM];
static raw_htc_buffer raw_htc_write_buffer[HTC_MAILBOX_NUM_MAX][RAW_HTC_WRITE_BUFFERS_NUM];
static A_BOOL write_buffer_available[HTC_MAILBOX_NUM_MAX];
static A_BOOL read_buffer_available[HTC_MAILBOX_NUM_MAX];

static BOOL rawIfInit = false;

static void
ar6000_htc_raw_read_cb(HTC_TARGET *htcTarget, HTC_ENDPOINT_ID endPointId,
                       HTC_EVENT_ID evId, HTC_EVENT_INFO *evInfo, void *arg)
{
    HTC_TARGET *target;
    raw_htc_buffer *busy;

    target = (HTC_TARGET *)arg;
    AR_DEBUG_ASSERT(target != NULL);
    busy = (raw_htc_buffer *)evInfo->cookie;
    AR_DEBUG_ASSERT(busy != NULL);

    if ( WAIT_FAILED == WaitForSingleObject(raw_htc_read_sem[endPointId], INFINITE) ) {

		NDIS_DEBUG_PRINTF(ATH_LOG_ERR, "Unable to wait on the Semaphore\n");
	}

	AR_DEBUG_ASSERT(evId == HTC_BUFFER_RECEIVED);
    AR_DEBUG_ASSERT((evInfo->status != A_OK) || 
             (evInfo->buffer == (busy->data + HTC_HEADER_LEN)));

    if (evInfo->status == A_ECANCELED) {
        /*
         * HTC provides A_ECANCELED status when it doesn't want to be refilled
         * (probably due to a shutdown)
         */
        memset(busy, 0, sizeof(raw_htc_buffer));
        return;
    }

    busy->length = evInfo->actualLength + HTC_HEADER_LEN;
    busy->currPtr = HTC_HEADER_LEN;
    read_buffer_available[endPointId] = TRUE;
	
	ReleaseMutex(raw_htc_read_sem[endPointId]);

    //up(&raw_htc_read_sem[endPointId]);

    /* Signal the waiting process */

	NDIS_DEBUG_PRINTF(ATH_LOG_INF, "Waking up the endpoint(%d) read process\n", endPointId);

    //AR_DEBUG2_PRINTF("Waking up the endpoint(%d) read process\n", endPointId);
	NdisSetEvent(&raw_htc_read_queue[endPointId]);

    //wake_up_interruptible(&raw_htc_read_queue[endPointId]);
}

static void
ar6000_htc_raw_write_cb(HTC_TARGET *htcTarget, HTC_ENDPOINT_ID endPointId,
                        HTC_EVENT_ID evId, HTC_EVENT_INFO *evInfo, void *arg)
{
    HTC_TARGET *target;
    raw_htc_buffer *free;

    target = (HTC_TARGET *)arg;

    AR_DEBUG_ASSERT(target != NULL);
    free = (raw_htc_buffer *)evInfo->cookie;
    AR_DEBUG_ASSERT(free != NULL);

    if ( WAIT_FAILED == WaitForSingleObject(raw_htc_write_sem[endPointId], INFINITE) ) {

		NDIS_DEBUG_PRINTF(ATH_LOG_ERR, "Unable to wait on the Semaphore\n");
	}

    AR_DEBUG_ASSERT(evId == HTC_BUFFER_SENT);
    AR_DEBUG_ASSERT(evInfo->buffer == (free->data + HTC_HEADER_LEN));

    free->length = 0;
    write_buffer_available[endPointId] = TRUE;
	ReleaseMutex(raw_htc_write_sem[endPointId]);

    /* Signal the waiting process */
	NDIS_DEBUG_PRINTF(ATH_LOG_INF, "Waking up the endpoint(%d) write process\n", endPointId);

	NdisSetEvent(&raw_htc_write_queue[endPointId]);
}

static void
ar6000_htc_raw_unread_cb(HTC_TARGET *htcTarget, HTC_ENDPOINT_ID eid,
                         HTC_EVENT_ID evId, HTC_EVENT_INFO *evInfo, void *arg)
{
    HTC_TARGET *target;

    target = (HTC_TARGET *)arg;
    AR_DEBUG_ASSERT(target != NULL);

	NDIS_DEBUG_PRINTF(ATH_LOG_ERR, "Not Implemented\n");
}

static int
ar6000_htc_raw_open(HTC_TARGET *htcTarget)
{
    A_STATUS status;
    unsigned int count1, count2;
    raw_htc_buffer *buffer;

    for (count1 = 0; count1 < HTC_MAILBOX_NUM_MAX; count1 ++) {
        /* Initialize the data structures */
		if (!(raw_htc_read_sem[count1] = CreateMutex(NULL,FALSE,NULL)) ) {
			return -1;
		}
		if (!(raw_htc_write_sem[count1] = CreateMutex(NULL, FALSE, NULL)) ) {
			return -1;
		}
        
		NdisInitializeEvent(&raw_htc_read_queue[count1]);
		NdisInitializeEvent(&raw_htc_write_queue[count1]);

        /* Register the event handlers */
        if ((status = HTCEventReg(htcTarget, (HTC_ENDPOINT_ID)count1, HTC_BUFFER_RECEIVED,
                                  ar6000_htc_raw_read_cb, htcTarget)) != A_OK)
        {
            BMIInit();
            return -1;
        }
        if ((status = HTCEventReg(htcTarget, (HTC_ENDPOINT_ID)count1, HTC_DATA_AVAILABLE,
                                  ar6000_htc_raw_unread_cb, htcTarget)) != A_OK)
        {
            BMIInit();
            return -1;
        }
        if ((status = HTCEventReg(htcTarget, (HTC_ENDPOINT_ID)count1, HTC_BUFFER_SENT,
                                  ar6000_htc_raw_write_cb, htcTarget)) != A_OK)
        {
            BMIInit();
            return -1;
        }

        for (count2 = 0; count2 < RAW_HTC_READ_BUFFERS_NUM; count2 ++) {
            /* Initialize the receive buffers */
            buffer = &raw_htc_write_buffer[count1][count2];
            memset(buffer, 0, sizeof(raw_htc_buffer));
            buffer = &raw_htc_read_buffer[count1][count2];
            memset(buffer, 0, sizeof(raw_htc_buffer));

            /* Queue buffers to HTC for receive */
            if ((status = HTCBufferReceive(htcTarget, (HTC_ENDPOINT_ID)count1, buffer->data,
                                           AR6000_BUFFER_SIZE, buffer)) != A_OK)
            {
                BMIInit();
                return -1;
            }
        }

        for (count2 = 0; count2 < RAW_HTC_WRITE_BUFFERS_NUM; count2 ++) {
            /* Initialize the receive buffers */
            buffer = &raw_htc_write_buffer[count1][count2];
            memset(buffer, 0, sizeof(raw_htc_buffer));
        }

        read_buffer_available[count1] = FALSE;
        write_buffer_available[count1] = TRUE;
    }

    /* Start the HTC component */
    if ((status = HTCStart(htcTarget)) != A_OK) {
        BMIInit();
        return -1;
    }

    return 0;
}

static int
ar6000_htc_raw_close(HTC_TARGET *htcTarget)
{
    int count;
    A_STATUS status;

    /* Stop the HTC */
    HTCStop(htcTarget);

    /* Unregister the event handlers */
    for (count = 0; count < HTC_MAILBOX_NUM_MAX; count ++) {
        status = HTCEventReg(htcTarget, (HTC_ENDPOINT_ID)count, HTC_BUFFER_RECEIVED,
                             NULL, htcTarget);

        status = HTCEventReg(htcTarget, (HTC_ENDPOINT_ID)count, HTC_DATA_AVAILABLE,
                             NULL, htcTarget);

        status = HTCEventReg(htcTarget, (HTC_ENDPOINT_ID)count, HTC_BUFFER_SENT,
                             NULL, htcTarget);

		/* free the NDIS event structures */
		NdisFreeEvent(&raw_htc_read_queue[count]);
		NdisFreeEvent(&raw_htc_write_queue[count]);
    }

	/* Initialize the BMI component */
    BMIInit();

    return 0;
}

raw_htc_buffer *
get_filled_buffer(HTC_ENDPOINT_ID endPointId)
{
    int count;
    raw_htc_buffer *busy;

    /* Check for data */
    for (count = 0; count < RAW_HTC_READ_BUFFERS_NUM; count ++) {
        busy = &raw_htc_read_buffer[endPointId][count];
        if (busy->length) {
            break;
        }
    }
    if (busy->length) {
        read_buffer_available[endPointId] = TRUE;
    } else {
        read_buffer_available[endPointId] = FALSE;
    }

    return busy;
}

static size_t
ar6000_htc_raw_read(HTC_TARGET *htcTarget, HTC_ENDPOINT_ID endPointId, 
                    char *buffer, int length)
{
    int readPtr;
    raw_htc_buffer *busy;

	if ( WAIT_FAILED == WaitForSingleObject(raw_htc_read_sem[endPointId], INFINITE) ) {
		return -1;
	}

    busy = get_filled_buffer(endPointId);
    while (!read_buffer_available[endPointId]) {
        
		ReleaseMutex(raw_htc_read_sem[endPointId]);
		//up(&raw_htc_read_sem[endPointId]);

        /* Wait for the data */
		NDIS_DEBUG_PRINTF(ATH_LOG_INF, "Sleeping endpoint(%d) read process\n", endPointId);

		NdisWaitEvent(&raw_htc_read_queue[endPointId], 0);

		NdisResetEvent(&raw_htc_read_queue[endPointId]);

		if ( WAIT_FAILED == WaitForSingleObject(raw_htc_read_sem[endPointId], INFINITE) ) {
			return -1;
		}
        
        busy = get_filled_buffer(endPointId);
    }

    /* Read the data */
    readPtr = busy->currPtr;
    if (length > busy->length - HTC_HEADER_LEN) {
        length = busy->length - HTC_HEADER_LEN;
    }

	memcpy(buffer, &busy->data[readPtr], length);

    /* if (copy_to_user(buffer, &busy->data[readPtr], length)) {
        up(&raw_htc_read_sem[endPointId]);
        return -EFAULT;
    } */

    busy->currPtr += length;
    if (busy->currPtr == busy->length)
    {
        /* Packet has been completely read. Queue it with HTC */
        memset(busy, 0, sizeof(raw_htc_buffer));
        HTCBufferReceive(htcTarget, endPointId, busy->data,
                         AR6000_BUFFER_SIZE, busy);
    }
    read_buffer_available[endPointId] = FALSE;

	ReleaseMutex(raw_htc_read_sem[endPointId]);
    //up(&raw_htc_read_sem[endPointId]);

    return length;
}

raw_htc_buffer *
get_free_buffer(HTC_ENDPOINT_ID endPointId)
{
    int count;
    raw_htc_buffer *free;

    free = NULL;
    for (count = 0; count < RAW_HTC_WRITE_BUFFERS_NUM; count ++) {
        free = &raw_htc_write_buffer[endPointId][count];
        if (free->length == 0) {
            break;
        }
    }
    if (!free->length) {
        write_buffer_available[endPointId] = TRUE;
    } else {
        write_buffer_available[endPointId] = FALSE;
    }

    return free;
}

static size_t
ar6000_htc_raw_write(HTC_TARGET *htcTarget, HTC_ENDPOINT_ID endPointId,
                     char *buffer, int length)
{
    int writePtr;
    raw_htc_buffer *free;

   	if ( WAIT_FAILED == WaitForSingleObject(raw_htc_write_sem[endPointId], INFINITE) ) {
		return -1;
	}

    /* Search for a free buffer */
    free = get_free_buffer(endPointId);

    /* Check if there is space to write else wait */
    while (!write_buffer_available[endPointId]) {

		ReleaseMutex(raw_htc_write_sem[endPointId]);

        /* Wait for buffer to become free */
		NDIS_DEBUG_PRINTF(ATH_LOG_INF, "Sleeping endpoint(%d) write process\n", endPointId);

		NdisWaitEvent(&raw_htc_write_queue[endPointId], 0);

		NdisResetEvent(&raw_htc_write_queue[endPointId]);
		if ( WAIT_FAILED == WaitForSingleObject(raw_htc_write_sem[endPointId], INFINITE) ) {
			return -1;
		}

        free = get_free_buffer(endPointId);
    }

    /* Send the data */
    writePtr = HTC_HEADER_LEN;
    if (length > (AR6000_BUFFER_SIZE - HTC_HEADER_LEN)) {
        length = AR6000_BUFFER_SIZE - HTC_HEADER_LEN;
    }

	memcpy(&free->data[writePtr], buffer, length);

    /* if (copy_from_user(&free->data[writePtr], buffer, length)) {
		ReleaseMutex(raw_htc_write_sem[endPointId]);
       return -1;
    } */

    free->length = length;
    HTCBufferSend(htcTarget, endPointId, &free->data[writePtr], length, free);
    write_buffer_available[endPointId] = FALSE;

	ReleaseMutex(raw_htc_write_sem[endPointId]);

    return length;
}

#endif //HTC_RAW_INTERFACE

A_STATUS 
CAR6KMini::ar6000_get_target_stats()
{
	A_STATUS status = A_OK;
	NdisResetEvent(&m_tgtStatsEvent);

	status = wmi_get_stats_cmd((wmi_t*)m_pWMI);
	if ( status != A_OK)
		return status;

	if( !NdisWaitEvent(&m_tgtStatsEvent, AR6K_DEFAULT_MS_TO_WAIT_FOR_TGT_GETSTATS) ) {

		NDIS_DEBUG_PRINTF(ATH_LOG_ERR, "AR6K: ERROR - No TGT_GETSTATS event after %u ms\n", AR6K_DEFAULT_MS_TO_WAIT_FOR_TGT_GETSTATS);
		status = A_ERROR;
	}

	return status;
}

⌨️ 快捷键说明

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