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

📄 diskio.c

📁 基于RM9200主芯片
💻 C
📖 第 1 页 / 共 2 页
字号:
//
// 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:  

    diskio.c

Abstract:  

    WINCE driver for MultiMediaCard      

Functions:

Notes:


--*/
#include <windows.h>
#include <types.h>
#include <tchar.h>
#include <excpt.h>
#include <devload.h>
#include <pkfuncs.h>

#include "diskio.h"
#include "sddisk.h"
#include "sdmmc.h"
#include "plx9054.h"




#if (USE_MEMMODE)
    volatile PUCHAR virtreg;
#endif
SDGLOBAL ADAPTER myAdapters[N_CONTROLLERS];


SDGLOBAL SDBOOL scan_PCI_config(SDVOID);



//
// This module contains the functions:
//  CloseDisk
//  CheckMedia
//  MMCREAD
//  MMCWRITE
//  DoDiskIO
//  GetDiskInfo
//  SetDiskInfo
//  InitDisk
//

bool IsCardInserted(PDISK pDisk);
HKEY OpenDriverKey(LPTSTR ActiveKey);
BOOL GetFolderName(PDISK pDisk, LPWSTR FolderName, DWORD cBytes, DWORD * pcBytes);
BOOL GetStorageID(PDISK pDisk, PSTORAGE_IDENTIFICATION pOutBuf, DWORD nOutBufSize, DWORD * pBytesReturned);

//
// CloseDisk - finish and free all outstanding requests and free other resources
// associated with the specified disk
//
VOID
CloseDisk(
    PDISK pDisk
    )
{
    PDISK pd;

    DEBUGMSG(ZONE_IO, (TEXT("MMCDISK:CloseDisk closing 0x%x\r\n"), pDisk));

    //
    // Remove it from the global list of disks
    //
    EnterCriticalSection(&v_DiskCrit);
    if (pDisk == v_DiskList) {
    v_DiskList = pDisk->d_next;
    } else {
    pd = v_DiskList;
    while (pd->d_next != NULL) {
        if (pd->d_next == pDisk) {
        pd->d_next = pDisk->d_next;
        break;
        }
        pd = pd->d_next;
    }
    }
    LeaveCriticalSection(&v_DiskCrit);

    DEBUGMSG(ZONE_IO, (TEXT("MMCDISK:CloseDisk - freeing resources\r\n")));

    DeleteCriticalSection(&(pDisk->d_DiskCardCrit));

    if (pDisk->d_ActivePath) {
    LocalFree(pDisk->d_ActivePath);
    }
        
    LocalFree(pDisk);

#if USE_MEM_MODE
    if (virtreg) {
    VirtualFree(virtreg, 0, MEM_RELEASE);
    }

#endif


    DEBUGMSG(ZONE_IO, (TEXT("MMCDISK:CloseDisk done with 0x%x\r\n"), pDisk));
}    // CloseDisk


//
// CheckMedia - Simply check to see if media is present
//       
//       
//
DWORD
CheckMedia(PDISK pDisk)
{

    if (!IsCardInserted(pDisk)) {
    DEBUGMSG(ZONE_WARNING|ZONE_ERROR|ZONE_IO, 
        (TEXT("MMCDISK:CheckMedia - Built-in CF Card no longer present!\r\n")));
    if (pDisk->d_DiskCardState != STATE_DEAD) {
        pDisk->d_DiskCardState = STATE_REMOVED;
    }
    return DISK_REMOVED_ERROR;
    }

    return ERROR_SUCCESS;
}    // CheckMedia


//
// MMCREAD - fulfill one scatter/gather read request
//
DWORD
MMCREAD(
    PDISK pDisk,
    PSG_REQ pSgr
    )
{
    DWORD i;
    DWORD num_sg;
    DWORD bytes_this_int;
    DWORD bytes_this_sg;
    PSG_BUF pSg;
    PBYTE pBuf;
    DWORD sectno;
    DWORD endsect;
    DWORD error;
    BYTE WorkBuf[512];     
    PBYTE pTmp;

    num_sg = pSgr->sr_num_sg;
    pSg = &(pSgr->sr_sglist[0]);
    bytes_this_sg = pSg->sb_len;
    pBuf = MapPtrToProcess((LPVOID)pSg->sb_buf, GetCallerProcess());

    sectno = pSgr->sr_start;
    endsect = sectno + pSgr->sr_num_sec;
	if(endsect > pDisk->d_DiskInfo.di_total_sectors)
		return(ERROR_SECTOR_NOT_FOUND);

    error = ERROR_SUCCESS;
    //
    // This loop reads multiple sectors into multiple scatter/gather buffers.
    // The scatter/gather buffers may be bigger or smaller or same size as a
    // sector.
    //
    while (num_sg) {

#ifdef USE_INTERRUPT
    ResetEvent(pDisk->d_IRQEvent);
#endif

    try {
        error = CheckMedia(pDisk);

    if ( error == ERROR_SUCCESS)


#if (USE_SPI || USE_SPI_EMULATION)
        if (spi_read(0, sectno, WorkBuf, 1) == 0)
                     error = ERROR_READ_FAULT;

#else
        if (mmc_read(0, sectno, WorkBuf, 1) == 0)
                     error = ERROR_READ_FAULT;

#endif
        pTmp = WorkBuf;

    } except (GetExceptionCode() == STATUS_ACCESS_VIOLATION ?
        EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
        error = ERROR_READ_FAULT;
    }
    if (error != ERROR_SUCCESS) {
        return error;
    }

#ifdef USE_INTERRUPT
    if (pDisk->d_DiskCardState == STATE_OPENED) {
        WaitForSingleObject(pDisk->d_IRQEvent, DISK_IO_TIME_OUT);
    }
    if (pDisk->d_DiskCardState != STATE_OPENED) {
        return GetDiskStateError(pDisk->d_DiskCardState);
    }
#endif

    sectno++;
    bytes_this_int = pDisk->d_DiskInfo.di_bytes_per_sect;

ar_continue_sector:
    i = (bytes_this_sg < bytes_this_int) ? bytes_this_sg : bytes_this_int;
    bytes_this_sg -= i;
    bytes_this_int -= i;

    try {
        memcpy( pBuf, pTmp, i);
        pBuf += i;
        pTmp += i;
    } except (GetExceptionCode() == STATUS_ACCESS_VIOLATION ?
        EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
        error = GetDiskStateError(pDisk->d_DiskCardState);
    }
    if (error != ERROR_SUCCESS) {
        return error;
    }

    if (bytes_this_int == 0) {
        if (sectno < endsect) {
        continue;       // next sector
        }
    }

    //
    // Use the next scatter/gather buffer
    //
    num_sg--;
    if (num_sg == 0) {
        break;
    }
    pSg++;
    pBuf = MapPtrToProcess((LPVOID)pSg->sb_buf, GetCallerProcess());
    bytes_this_sg = pSg->sb_len;
    if (bytes_this_int) {
        goto ar_continue_sector;
    }

    }   // while 
    return 0;
}    // MMCREAD


//
// MMCWRITE
//
DWORD
MMCWRITE(
    PDISK pDisk,
    PSG_REQ pSgr
    )
{
    DWORD i;
    DWORD num_sg;
    DWORD bytes_this_int;
    DWORD bytes_this_sg;
    PSG_BUF pSg;
    PBYTE pBuf;
    DWORD sectno;
    DWORD endsect;
    DWORD error;
    

    num_sg = pSgr->sr_num_sg;
    pSg = &(pSgr->sr_sglist[0]);
    bytes_this_sg = pSg->sb_len;
    bytes_this_int = pDisk->d_DiskInfo.di_bytes_per_sect;
    pBuf = MapPtrToProcess((LPVOID)pSg->sb_buf, GetCallerProcess());

    sectno = pSgr->sr_start;
    endsect = sectno + pSgr->sr_num_sec;
	if(endsect > pDisk->d_DiskInfo.di_total_sectors)
		return(ERROR_SECTOR_NOT_FOUND);
    error = ERROR_SUCCESS;

    //
    // This loop writes data from multiple scatter/gather buffers to multiple
    // sectors.
    //
    while (num_sg) {

    try {
        error = CheckMedia(pDisk);

    if ( error == ERROR_SUCCESS)

#if (USE_SPI || USE_SPI_EMULATION)

        if (spi_write(0, sectno, (UTINY *) pBuf , 1) == 0)
                     error = ERROR_WRITE_FAULT;

#else
        if (mmc_write(0, sectno, (UTINY *) pBuf, 1) == 0)
                     error = ERROR_WRITE_FAULT;
#endif

    } except (GetExceptionCode() == STATUS_ACCESS_VIOLATION ?
        EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
        error = ERROR_WRITE_FAULT;
    }
    if (error != ERROR_SUCCESS) {
        return GetDiskStateError(pDisk->d_DiskCardState);
    }

    sectno++;
    bytes_this_int = pDisk->d_DiskInfo.di_bytes_per_sect;

aw_continue_sector:
    i = (bytes_this_sg < bytes_this_int) ? bytes_this_sg : bytes_this_int;
    bytes_this_sg -= i;
    bytes_this_int -= i;

    try {

        if (i >= 512) 
            i -=512;
        else
            i=0;

        pBuf += 512;

    } except (GetExceptionCode() == STATUS_ACCESS_VIOLATION ?
        EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) {
        error = GetDiskStateError(pDisk->d_DiskCardState);
    }
    if (error != ERROR_SUCCESS) {
        return error;
    }

    if (bytes_this_int == 0) {
        if (pDisk->d_DiskCardState != STATE_OPENED) {
        return GetDiskStateError(pDisk->d_DiskCardState);
        }
        if (sectno < endsect) {
        continue;       // next sector
        }
    }

    //
    // Use the next scatter/gather buffer
    //
    num_sg--;
    if (num_sg == 0) {
        break;
    }
    pSg++;
    pBuf =  MapPtrToProcess((LPVOID)pSg->sb_buf, GetCallerProcess());
    bytes_this_sg = pSg->sb_len;
    if (bytes_this_int) {
        goto aw_continue_sector;
    }
    }   // while

    return 0;
}   // MMCWRITE



//
// DoDiskIO - fulfill I/O requests
//
DWORD
DoDiskIO(
    PDISK pDisk,
    DWORD Opcode,
    PSG_REQ pSgr
    )
{
    DWORD status;
    DEBUGMSG(ZONE_IO, (TEXT("MMCDISK:DoDiskIO entered\r\n")));
    pSgr->sr_status = ERROR_IO_PENDING;

    if (pSgr->sr_num_sg > MAX_SG_BUF) {
    status = ERROR_INVALID_PARAMETER;
    goto ddi_exit;
    }

    if (pDisk->d_DiskCardState != STATE_OPENED) {
    status = GetDiskStateError(pDisk->d_DiskCardState);
    goto ddi_exit;
    }


    EnterCriticalSection(&(pDisk->d_DiskCardCrit));

    if (pDisk->d_DiskCardState != STATE_OPENED) {
    status = GetDiskStateError(pDisk->d_DiskCardState);
    goto ddi_leave;
    }

    if (!IsCardInserted(pDisk)) {
    DEBUGMSG(ZONE_WARNING|ZONE_ERROR|ZONE_IO, 
        (TEXT("MMCDISK:DoDiskIO - CF Card no longer present!\r\n")));
    if (pDisk->d_DiskCardState != STATE_DEAD) {
        pDisk->d_DiskCardState = STATE_REMOVED;
    }
    status = DISK_REMOVED_ERROR;

⌨️ 快捷键说明

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