📄 wd_misc.c
字号:
/*
**********************************************************************
* Micrium, Inc.
* 949 Crestview Circle
* Weston, FL 33327-1848
*
* uC/FS
*
* (c) Copyright 2001 - 2003, Micrium, Inc.
* All rights reserved.
*
***********************************************************************
----------------------------------------------------------------------
File : wd_misc.c
Purpose : Device Driver using Windows I/O function for logical sector
access.
---------------------------END-OF-HEADER------------------------------
*/
/*********************************************************************
*
* #include Section
*
**********************************************************************
*/
#include "fs_dev.h"
#include "fs_lbl.h"
#include "FS_ConfDefaults.h" /* FS Configuration */
#include "fs_api.h"
#include "fs_clib.h"
#if FS_USE_WINDRIVE_DRIVER
#ifndef _WIN32
#error Driver requires Windows API
#endif /* _WIN32 */
#include <windows.h>
#include <winioctl.h>
/*********************************************************************
*
* Local data types
*
**********************************************************************
*/
/*********************************************************************
*
* Local variables
*
**********************************************************************
*/
static HANDLE _hDrive [FS_WD_MAXDEV];
/*********************************************************************
*
* Local functions
*
**********************************************************************
*/
/*********************************************************************
*
* _InitMedium
*
* Description:
* Initializes the medium.
* In this driver, this simply means opening the device.
*
* Parameters:
* Unit - Unit number.
*
* Return value:
*
*/
static void _InitMedium(FS_U8 Unit) {
if (_hDrive[Unit] == INVALID_HANDLE_VALUE) {
_hDrive[Unit] = CreateFile(Unit ? FS_WD_DEV1NAME : FS_WD_DEV0NAME,
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
}
}
/*********************************************************************
*
* _GetStatus
*
* Description:
* FS driver function. Get status of the media.
*
* Parameters:
* Unit - Device number.
*
* Return value:
* FS_MEDIA_STATEUNKNOWN if the state of the media is unknown.
* FS_MEDIA_ISNOTPRESENT if no card is present.
* FS_MEDIA_ISPRESENT if a card is present.
*/
static int _GetStatus(FS_U8 Unit) {
if (_hDrive[Unit] == INVALID_HANDLE_VALUE) {
_InitMedium(Unit);
}
return (_hDrive[Unit] == INVALID_HANDLE_VALUE) ? FS_MEDIA_ISNOTPRESENT : FS_MEDIA_ISPRESENT;
}
/*********************************************************************
*
* _ReadBurst
*
* Description:
* FS driver function. Write sector to the media.
*
* Parameters:
* Unit - Device number.
* Sector - Sector to be read from the device.
* pBuffer - Pointer to data.
*
* Return value:
* ==0 - Sector has been written to the device.
* <0 - An error has occured.
*/
static int _ReadBurst(FS_U8 Unit, FS_U32 SectorNo, FS_U32 NumSectors, void *pBuffer) {
FS_U32 NumBytesTransfered;
FS_U32 NumBytes;
LARGE_INTEGER FilePos;
NumBytes = FS_SEC_SIZE * NumSectors;
FilePos.QuadPart = (__int64)SectorNo * (__int64)FS_SEC_SIZE;
SetFilePointer(_hDrive[Unit], FilePos.LowPart, &FilePos.HighPart, FILE_BEGIN);
ReadFile(_hDrive[Unit], pBuffer, NumBytes, &NumBytesTransfered, NULL);
return (NumBytesTransfered == NumBytes) ? 0 : -1;
}
/*********************************************************************
*
* _WriteBurst
*
* Description:
* FS driver function. Write sector to the media.
*
* Parameters:
* Unit - Device number.
* Sector - Sector to be written to the device.
* pBuffer - Pointer to data to be stored.
*
* Return value:
* ==0 - Sector has been written to the device.
* <0 - An error has occured.
*/
static int _WriteBurst(FS_U8 Unit, FS_U32 SectorNo, FS_U32 NumSectors, const void *pBuffer) {
FS_U32 NumBytesTransfered;
FS_U32 NumBytes;
LARGE_INTEGER FilePos;
NumBytes = FS_SEC_SIZE * NumSectors;
FilePos.QuadPart = (__int64)SectorNo * (__int64)FS_SEC_SIZE;
SetFilePointer(_hDrive[Unit], FilePos.LowPart, &FilePos.HighPart, FILE_BEGIN);
WriteFile(_hDrive[Unit], pBuffer, NumBytes, &NumBytesTransfered, NULL);
return (NumBytesTransfered == NumBytes) ? 0 : -1;
}
/*********************************************************************
*
* _Read
*
* Description:
* FS driver function. Read a sector from the media.
*
* Parameters:
* Unit - Device number.
* Sector - Sector to be read from the device.
* pBuffer - Pointer to buffer for storing the data.
*
* Return value:
* ==0 - Sector has been read and copied to pBuffer.
* <0 - An error has occured.
*/
static int _Read(FS_U8 Unit, FS_U32 Sector, void *pBuffer) {
return _ReadBurst(Unit, Sector, 1, pBuffer);
}
/*********************************************************************
*
* _Write
*
* Description:
* FS driver function. Write sector to the media.
*
* Parameters:
* Unit - Device number.
* Sector - Sector to be written to the device.
* pBuffer - Pointer to data to be stored.
*
* Return value:
* ==0 - Sector has been written to the device.
* <0 - An error has occured.
*/
static int _Write(FS_U8 Unit, FS_U32 Sector, void *pBuffer) {
return _WriteBurst(Unit, Sector, 1, pBuffer);
}
/*********************************************************************
*
* _IoCtl
*
* Description:
* FS driver function. Execute device command.
*
* Parameters:
* Unit - Unit number.
* Cmd - Command to be executed.
* Aux - Parameter depending on command.
* pBuffer - Pointer to a buffer used for the command.
*
* Return value:
* Command specific. In general a negative value means an error.
*/
static int _IoCtl(FS_U8 Unit, FS_I32 Cmd, FS_I32 Aux, void *pBuffer) {
FS_DEV_INFO *pInfo;
FS_U32 NumCylinders;
FS_U32 NumSectors;
FS_USE_PARA(Aux);
switch (Cmd) {
case FS_CMD_GET_DEVINFO:
if (pBuffer) {
DISK_GEOMETRY DiskGeometry;
DWORD Size;
_InitMedium(Unit);
Size = sizeof(DiskGeometry);
DeviceIoControl(_hDrive[Unit], IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &DiskGeometry, Size, &Size, NULL);
pInfo = (FS_DEV_INFO *)pBuffer;
NumCylinders = (FS_U32)DiskGeometry.Cylinders.QuadPart;
if (DiskGeometry.SectorsPerTrack == 63) {
/* Some media report inaccurate values (MMC/SD).
* Since we can not read the Number of sectors from the card info structure, we have to estimate: -6%
*/
NumCylinders = (NumCylinders + 1) & ~1;
NumSectors = NumCylinders * DiskGeometry.SectorsPerTrack * DiskGeometry.TracksPerCylinder;
NumSectors = (FS_U32)(((__int64)NumSectors * 94) / 100);
} else {
NumSectors = NumCylinders * DiskGeometry.SectorsPerTrack * DiskGeometry.TracksPerCylinder;
}
pInfo->NumSectors = NumSectors;
pInfo->BytesPerSector = (FS_U16)DiskGeometry.BytesPerSector;
pInfo->NumHeads = (FS_U16)DiskGeometry.TracksPerCylinder;
pInfo->SectorsPerTrack = (FS_U16)DiskGeometry.SectorsPerTrack;
return 0;
}
break;
}
return -1;
}
/*********************************************************************
*
* _InitDevice
*
* Description:
* Initializes the device.
* In this driver, this simply means marking the handle as invalid.
*
* Parameters:
* Unit - Unit number.
*
* Return value:
*
*/
static int _InitDevice(FS_U8 Unit) {
_hDrive[Unit] = INVALID_HANDLE_VALUE;
return 0;
}
/*********************************************************************
*
* Global variables
*
**********************************************************************
*/
const FS_DEVICE_TYPE FS__windrive_driver = {
"windrv",
FS_WD_MAXDEV,
_GetStatus,
_Read,
_Write,
_IoCtl,
_ReadBurst,
_WriteBurst,
_InitDevice,
_InitMedium
};
#endif /* END OF FS_USE_WINDRIVE_DRIVER */
/*************************** End of file ****************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -