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

📄 window.c

📁 YLP270的Windows CE5.0 bsp源码。
💻 C
📖 第 1 页 / 共 3 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Module Name:

    window.c

Abstract:

    This file implements the PCMCIA model device driver memory and I/O window
    functions.  This is provided as a sample to platform writers and is
    expected to be able to be used without modification on most (if not
    all) hardware platforms.

Functions:

    IsValidWindow()
    FindPhysWindow()
    CardRequestWindow()
    CardReleaseWindow()
    CardModifyWindow()
    CardMapWindow()
    CardReadAttrByte()
    CardReadCmnByte()
    CardWriteAttrByte()

Notes:


--*/


#include <windows.h>
#include <types.h>
#include <excpt.h>
#include <cardserv.h>
#include <sockserv.h>
#include <pcmcia.h>
#include <extern.h>
#include <resmgr.h>
#include <ceddk.h>

#ifdef INSTRUM_DEV
#include <instrumd.h>
#endif

DWORD g_IORM_ID[MAX_SOCKETS] = { RESMGR_IOSPACE, RESMGR_IOSPACE };

BOOL
IsValidWindow(
    PLOG_WINDOW pWin
    )
{
    PPHYS_WINDOW pPhys;
    PLOG_WINDOW pLog;

    //
    // Both the physical and logical windows must exist.
    //
    EnterCriticalSection(&v_WindowCrit);
    pPhys = v_pWinList;
    while (pPhys) {
        pLog = pPhys->pLog;
        while (pLog) {
            if (pWin == pLog) {
                LeaveCriticalSection(&v_WindowCrit);
                return TRUE;
            }
            pLog = pLog->Next;
        }
        pPhys = pPhys->Next;
    }
    LeaveCriticalSection(&v_WindowCrit);
    return FALSE;
}

//
// FindPhysWindow - Find a physical window that matches requested attributes
//
// If pPhys is NULL, then find any matching physical window.
// If pPhys is not NULL, then find a matching physical window with a
// programmable offset that is not being used (no attached logical windows).
//
// Returns NULL if no matching physical window was found
//
PPHYS_WINDOW
FindPhysWindow(
    UINT8 uSocket,
    UINT fAttributes,
    UINT uSize,
    PPHYS_WINDOW pPhys
    )
{
    PPHYS_WINDOW pWin;
    UINT16 fDesiredCaps;
    UINT16 fOtherCaps;

    //
    // Map the window attributes the client wants to the corresponding window
    // capabilities.
    //
    fDesiredCaps = WIN_CAP_COMMON;
    if (fAttributes & WIN_ATTR_IO_SPACE) {
        fDesiredCaps = WIN_CAP_IO;
    } else if (fAttributes & WIN_ATTR_ATTRIBUTE) {
        fDesiredCaps = WIN_CAP_ATTRIBUTE;
    }

    fOtherCaps = 0;
    if (fAttributes & WIN_ATTR_16BIT) {
        fOtherCaps |= MEM_CAP_16BIT;
    } else {
        fOtherCaps |= MEM_CAP_8BIT;
    }

    if (pPhys != NULL) {
        fOtherCaps |= MEM_CAP_PRG_BASE;
    }
    DEBUGMSG(ZONE_MEM, (TEXT("FindPhysWindow: Size=%d, socket=%d, desired caps=0x%x, other caps=0x%x\r\n"),
        uSize, uSocket, fDesiredCaps, fOtherCaps));

    //
    // Find a window that matches what the client wants.
    //
    EnterCriticalSection(&v_WindowCrit);
    pWin = v_pWinList;
    while (pWin) {
        if ((pWin->uMaxSize >= (uSize * v_pAdapterInfo->uMemGranularity)) &&
            (pWin->uSock == uSocket) &&
            ((pWin->fWindowCaps & fDesiredCaps) == fDesiredCaps) &&
            ((pWin->fOtherCaps & fOtherCaps) == fOtherCaps)) {
            //
            // CardRequestWindow doesn't need an unused physical window
            //
            if (pPhys == NULL) {
                break;
            }
            //
            // When CardMapWindow remaps, it needs an unused physical window
            //
            if (pWin->pLog == NULL) {
                break;
            }
        }
        pWin = pWin->Next;
    }
    LeaveCriticalSection(&v_WindowCrit);
    DEBUGMSG(ZONE_MEM, (TEXT("FindPhysWindow: returning 0x%x\r\n"), pWin));
    return pWin;
}   // FindPhysWindow


#ifdef DEBUG_TESTING
VOID DisplayAllWindows(VOID)
{
    PPHYS_WINDOW pPhys;
    PLOG_WINDOW pLog;

    //
    // Print out all the windows.
    //
    EnterCriticalSection(&v_WindowCrit);
    pPhys = v_pWinList;
    while (pPhys) {
        DEBUGMSG(1,(TEXT("Physical window %d at %x\r\n"), pPhys->uWindow, pPhys->uBase));
        pLog = pPhys->pLog;
        while (pLog) {
            DEBUGMSG(1,(TEXT("\tLogical window of %d at %x\r\n"), pLog->uReqSize, pLog->pVirtMem));
            pLog = pLog->Next;
        }
        pPhys = pPhys->Next;
    }
    LeaveCriticalSection(&v_WindowCrit);
}
#endif  // DEBUG_TESTING


//
// CardRequestWindow
//
// @doc DRIVERS
//
// @func    CARD_WINDOW_HANDLE | CardRequestWindow | Allocate a memory or I/O window with the desired attributes.
// @rdesc   Returns a CARD_WINDOW_HANDLE on success. On failure the return value is NULL and
//          GetLastError will return one of CERR_SUCCESS, CERR_BAD_ARGS, CERR_BAD_SOCKET or
//          CERR_OUT_OF_RESOURCE.
//
// @comm    Allocates a memory or I/O window that matches the characteristics specified in
//          the <t CARD_WINDOW_PARMS> structure.  The returned handle is used in subsequent calls
//          to <f CardMapWindow> and <f CardModifyWindow>.
// @xref     <f CardReleaseWindow>
//
CARD_WINDOW_HANDLE
CardRequestWindow(
    CARD_CLIENT_HANDLE hCardClient, // @parm Handle from <f CardRegisterClient>
    PCARD_WINDOW_PARMS pParms       // @parm Pointer to a <t CARD_WINDOW_PARMS> structure
    )
{
    PPHYS_WINDOW pWin;
    PLOG_WINDOW  pLog;
    PCLIENT_DRIVER pClient;
    STATUS status = CERR_SUCCESS;

    DEBUGMSG(ZONE_FUNCTION, (TEXT("CardRequestWindow entered\r\n")));
    pLog = NULL;

    //
    // Perform some parameter checks first
    //
    if ((pClient = FindClient(hCardClient, TRUE, TRUE)) == NULL) {
        status = CERR_BAD_HANDLE;
        goto req_win_exit;
    }

    if (pParms == NULL) {
        status = CERR_BAD_ARGS;
        goto req_win_exit;
    }

    if (I_FindSocket(pParms->hSocket) == NULL) {
        status = CERR_BAD_SOCKET;
        goto req_win_exit;
    }

    pWin = FindPhysWindow(
                pParms->hSocket.uSocket,
                pParms->fAttributes,
                pParms->uWindowSize,
                NULL);
    if (pWin == NULL) {
        status = CERR_OUT_OF_RESOURCE;
        goto req_win_exit;
    }

    pLog = I_CreateLogicalWindow(pWin);
    if (pLog == NULL) {
        status = CERR_OUT_OF_RESOURCE;
    } else {
        pLog->hOwner = pClient;
        pLog->fAttributes = pParms->fAttributes;
        pLog->uReqSize = 0;
        pLog->uReqOffset = 0;
    }

    //DisplayAllWindows();

req_win_exit:
    if (status) {
        DEBUGMSG(ZONE_FUNCTION|ZONE_ERROR,
            (TEXT("CardRequestWindow failed %d\r\n"), status));
        SetLastError(status);
        return NULL;
    } else {
        DEBUGMSG(ZONE_FUNCTION,
            (TEXT("CardRequestWindow succeeded\r\n")));
        return (CARD_WINDOW_HANDLE)pLog;
    }
}   // CardRequestWindow



//
// CardReleaseWindow
//
// @func    STATUS | CardReleaseWindow | Release a memory or I/O window
// @rdesc   Returns one of CERR_SUCCESS, CERR_BAD_HANDLE, CERR_BAD_WINDOW or
//          CERR_CONFIGURATION_LOCKED.
//
// @comm    Releases a memory or I/O window previously allocated by <f CardRequestWindow>.
// @xref    <f CardReleaseConfiguration>
//
STATUS
CardReleaseWindow(
    CARD_WINDOW_HANDLE hWin // @parm Memory window handle from <f CardRequestWindow>
    )
{
    PLOG_WINDOW pWin = (PLOG_WINDOW)hWin;
    PCLIENT_DRIVER pClient;
    PLOG_SOCKET pLsock;
    STATUS status = CERR_SUCCESS;
#ifdef x86
    PDCARD_WINDOW_STATE WinState;
#endif
    DWORD uCardAddress, uSize;

    DEBUGMSG(ZONE_FUNCTION, (TEXT("CardReleaseWindow entered\r\n")));

    //
    // Perform some parameter checks first
    //
    if (!IsValidWindow(pWin)) {
        status = CERR_BAD_WINDOW;
        goto rel_win_exit;
    }

    pClient = (PCLIENT_DRIVER)pWin->hOwner;
    if (!IsValidClient(pClient)) {
        status = CERR_BAD_HANDLE;
        goto rel_win_exit;
    }

    //
    // If it's an I/O window, then the client must call CardReleaseConfiguration
    // first.
    //
    if (pWin->pPhys->fWindowCaps & WIN_CAP_IO) {
        //
        // Check if this client owns the configuration for any function on this socket.
        //
        EnterCriticalSection(&v_SocketCrit);
        pLsock = v_Sockets[pWin->pPhys->uSock].pLsock;
        while (pLsock) {
            if ((pLsock->hOwner == pClient) &&
                (pLsock->fFlags & OWNER_FLAG_CONFIG)) {
                status = CERR_CONFIGURATION_LOCKED;
                LeaveCriticalSection(&v_SocketCrit);
                goto rel_win_exit;
            }
            pLsock = pLsock->Next;
        }
        LeaveCriticalSection(&v_SocketCrit);

#ifdef x86
        //
        // Disable this window so new map requests don't collide.
        //
        status = PDCardGetWindow(pWin->pPhys->uWindow, &WinState);
        if (status == CERR_SUCCESS) {
            pWin->pPhys->fFlags &= ~PHYS_WIN_FLAG_ENABLED;
            WinState.fState &= ~WIN_STATE_ENABLED;
            PDCardSetWindow(pWin->pPhys->uWindow, &WinState);
        }
#endif

        //
        // release the resources for non-PCMCIA devices.
        //
        uCardAddress = pWin->uReqOffset;
        uSize = pWin->uReqSize;
        if (uSize != 0 && g_IORM_ID[pWin->pPhys->uSock] != 0)
            ResourceRelease(g_IORM_ID[pWin->pPhys->uSock], uCardAddress, uSize);
    }

    //
    // Disassociate memory window from client and free the mapped region
    //

⌨️ 快捷键说明

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