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

📄 diskio.c

📁 S3C2443 WINCE6.0 BSP
💻 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 "atadisk.h"
#include "atapiapi.h"

extern PDEVICE_CONTROLLER g_pcPC;
//SDGLOBAL int scan_PCI_config(void);



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

int IsCardInserted(PDISK pDisk);
HKEY OpenDriverKey(LPTSTR ActiveKey);
int GetFolderName(PDISK pDisk, LPWSTR FolderName, DWORD cBytes, DWORD * pcBytes);
int GetStorageID(PDISK pDisk, PSTORAGE_IDENTIFICATION pOutBuf, DWORD nOutBufSize, DWORD * pBytesReturned);
int mmc_read(INT16 driveno, ULONG sector, UCHAR * buffer, UCOUNT count);
int mmc_write(INT16 driveno, ULONG sector, UCHAR * buffer, UCOUNT count);


//
// 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
//
#if 1
DWORD
ATAREAD(
    PDISK pDisk,
    PSG_REQ pSgr
    )
{
	
    DWORD num_sg;
    DWORD bytes_this_sg;
    PSG_BUF pSg;
    PUSHORT pBuf16;
    DWORD sectno;
    DWORD endsect;
    DWORD error;

    num_sg = pSgr->sr_num_sg;
    pSg = &(pSgr->sr_sglist[0]);
    bytes_this_sg = pSg->sb_len;
    pBuf16 = (PUSHORT)  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 (AtaDiskRead(sectno, (BYTE *)pBuf16, (UCOUNT)pSgr->sr_num_sec) == 0)
       	         	error = ERROR_READ_FAULT;
    		}

	    }
	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
    	
		//
		// Use the next scatter/gather buffer
		//
		num_sg--;
		if(num_sg == 0)
		{
	    	break;
		}
		pSg++;
    	pBuf16 = (PUSHORT)  MapPtrToProcess((LPVOID)pSg->sb_buf, GetCallerProcess());
		bytes_this_sg = pSg->sb_len;


	}   // while
 
 
    return 0;
}    // ma


//
// MMCWRITE
//
DWORD
ATAWRITE(
    PDISK pDisk,
    PSG_REQ pSgr
    )
{
	
    DWORD num_sg;
    //DWORD bytes_this_int;
    DWORD bytes_this_sg;
    PSG_BUF pSg;
    PUSHORT pBuf16;
    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;
    pBuf16 = (PUSHORT) 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 (AtaDiskWrite(sectno, (UTINY *) pBuf16 , (UCOUNT)pSgr->sr_num_sec) == 0)
	                    error = ERROR_WRITE_FAULT;
		}
		//RETAILMSG(RTL_MSG,(TEXT("+++ ATAPI:: Error Check point\r\n")));			
    	}
    	except (GetExceptionCode() == STATUS_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) 
    	{
        	error = ERROR_WRITE_FAULT;
    	}
    	if (error != ERROR_SUCCESS) 
    	{
        	return GetDiskStateError(pDisk->d_DiskCardState);
    	}

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

    }   // while

    return 0;
}   // MMCWRITE


#endif
#if 1
//
// DoDiskIO - fulfill I/O requests
//
DWORD
DoDiskIO(
    PDISK pDisk,
    DWORD Opcode,
    PSG_REQ pSgr
    )
{
    DWORD status;
    
    //RETAILMSG(RTL_MSG, (TEXT("### ATAPI:DoDiskIO entered with Code : 0x%x\r\n"),Opcode));
    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;
    }

⌨️ 快捷键说明

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