📄 usbh_ide_if.c
字号:
/*
* description: USBH Mass Storage Class Interface
* Maker : Shuichi Yanagihara
* Copyright : (C)2005,SEIKO EPSON Corp. All Rights Reserved.
*/
#include <string.h>
#ifndef TOYA2_C
#include <ap7tdmi.h>
#endif
#include "SPRDEF.h"
#include "SPRSTS.h"
#include "OSCall.h"
#include "Reg72V05.h"
#include "Handler.h"
#include "drvacs.h"
#include "usbh_SampleTask.h"
#include "usbh_ide_api.h"
#include "usbh_ide_if.h"
#include "IDETask.h"
#include "usbh_hcds_user_config.h"
#include <DebugTask.h>
/*****************************************
* Define definition
*****************************************/
#define OFF (0x00)
#define ON (0x01)
#define MASK_FRACTION (0x0001)
#define MEDIA_FIFO_SIZE (64)
#define MASK_RD_REMAIN_VALID (0x8000)
#define ALL_JOIN_CLEAR (0x8F)
#define MODE_PROTECT_OFF (0x0056)
#define IDE_READ (0)
#define IDE_WRITE (1)
#define DMA_NOT_USE (0xFF)
#define CPU_DMA_NUM (1)
#define BIT15 (0x8000)
#define BIT14 (0x4000)
#define BIT13 (0x2000)
#define BIT12 (0x1000)
#define BIT11 (0x0800)
#define BIT10 (0x0400)
#define BIT9 (0x0200)
#define BIT8 (0x0100)
#define BIT7 (0x0080)
#define BIT6 (0x0040)
#define BIT5 (0x0020)
#define BIT4 (0x0010)
#define BIT3 (0x0008)
#define BIT2 (0x0004)
#define BIT1 (0x0002)
#define BIT0 (0x0001)
#define MASK_1BIT (BIT0)
#define MASK_2BIT (BIT1 | BIT0)
/* SCSI/ATAPI OP Code */
#define TEST_UNIT_READY 0x00
#define REQUEST_SENSE 0x03
#define INQUIRY 0x12
#define READ_FORMAT_CAPACITY 0x23
#define READ_CAPACITY 0x25
#define READ10 0x28
#define WRITE10 0x2A
/* RequestSense SenseKey Code */
#define NO_SENSE 0x00
#define RECOVERED_ERROR 0x01
#define NOT_READY 0x02
#define MEDIUM_ERROR 0x03
#define HARDWARE_ERROR 0x04
#define ILLEGAL_REQEEST 0x05
#define UNIT_ATTENTION 0x06
#define DATA_PROTECT 0x07
#define BLANK_CHECK 0x08
#define COPY_ABORTED 0x0A
#define ABORTED_COMMAND 0x0B
#define EQUAL 0x0C
#define VOLUME_OVERFLOW 0x0D
#define MISCOMPARE 0x0E
/* SCSI/ATAPI Command Mode */
#define SCSI (0x00)
#define ATAPI (0x01)
/* SCSI/ATAPI Command Parameter */
#define REQSENSE_LENGTH (18) /* RequestSense Data Length 18Byte */
#define INQUIRY_LENGTH (36) /* Inquiry Data Length 36Byte (Till Product Rev.) */
#define IDENTIFY_LENGTH (512) /* Identify Data Length 512Byte */
#define RDCAPA_LENGTH (8) /* Read Capacity Data Length 9Byte */
#define RDFORMCAPA_LENGTH (252) /* Read Format Capacity Allocation Length 252Byte */
/* Command Status */
#define NO_OP (0x00) /* Command No Operation */
#define WAIT_START (0x02) /* Waiting for start in DirectCopy Mode. */
#define XFER_READY (0x03)
#define COMMAND_OK (0x04)
#define COMMAND_NG (0x05)
/* Host Contoller PowerManagement State */
#define USBH_HCDS_HC_PM_STATE_SLEEP (0x00)
#define USBH_HCDS_HC_PM_STATE_SNOOZE (0x01)
#define USBH_HCDS_HC_PM_STATE_ACTIVE60 (0x03)
#define USBH_HCDS_HC_PM_STATE_ACTDEVICE (0x07)
#define USBH_HCDS_HC_PM_STATE_ACTHOST (0x0B)
#define TOYABOARD
/*****************************************
* structures definition
*****************************************/
typedef struct tagUSBH_IDE_DEVICE_PARA{
UCHAR commandStatus; /* Command Status */
UCHAR commandMode; /* Command Mode (SCSI or ATAPI) */
UCHAR deviceStatus; /* Device Status */
ULONG transferSize; /* Number of transfered data in the data transfer processed just before */
ULONG tranCount;
ULONG xferDataLen;
UCHAR *tranAdrs;
UCHAR direction;
UCHAR xferMode;
UCHAR commandBlock[12]; /* Command Block */
PARAM_MSG_REQ_IDE_XFER_START xferStart;
UCHAR reqSenseData[REQSENSE_LENGTH]; /* Last Execution RequestSense Data */
UCHAR rdCapaData[RDCAPA_LENGTH]; /* Read Capacity Data */
UCHAR identifyData[IDENTIFY_LENGTH]; /* Device Identify Data */
UCHAR usedDMACh; /* Use DMA Ch */
}USBH_IDE_DEVICE_PARA;
typedef struct tagUSBH_IDE_DEVICELIST{
UCHAR deviceCount; /* Number of connected devices */
UCHAR deviceType[USBH_IDE_MAX_DEVICE]; /* Device type */
}USBH_IDE_DEVICELIST;
/*****************************************
* Function prototype declaration
*****************************************/
static LONG USBH_IDE_MakeIdeCmd( USHORT deviceNo, UCHAR opeCode, PARAM_MSG_REQ_IDE_CMD *pIdeCmd);
Inline void USBH_IDE_SendMessage ( OS_ID mbxId, USHORT msgCode, USHORT size, const VOID* buffer );
static ULONG ReadMediaFIFOData( unsigned char *pBufAdrs );
static ULONG WriteMediaFIFOData( unsigned char *pBufAdrs, unsigned long dataSize );
static LONG USBH_IDE_DirectCopyCallback( ULONG event, ULONG param1, void *pParam );
static LONG USBH_IDE_FinishPMCallback( ULONG event, ULONG param1, void *pParam );
static void MsgIdeXferReady ( UCHAR deviceNo, USBH_SAMPLE_MSG *pMsg );
static void MsgIdeXferCmp ( UCHAR deviceNo, USBH_SAMPLE_MSG *pMsg );
static void MsgIdeCmdCmp ( UCHAR deviceNo, USBH_SAMPLE_MSG *pMsg );
static void USBH_IDE_IntEvent( UCHAR deviceNo );
/*****************************************
* Macro definition
*****************************************/
#define DIRECT_B(a) *(unsigned char *)&(a)
/*****************************************
* Variables definition
*****************************************/
static USBH_IDE_DEVICELIST USBH_IDE_DeviceList; /* Device list(Device type & Number of connected devices) */
static USBH_IDE_DEVICELIST *pUSBH_IDE_DeviceList; /* Device list(Device type & Number of connected devices) */
static USBH_IDE_DEVICE_PARA devicePara[USBH_IDE_MAX_DEVICE];
static CALLBACK_PROC USBH_IDE_IFDMACompCBR[USBH_IDE_MAX_DEVICE]; /* DMA Complete CALLBACK TABLE */
static CALLBACK_PROC USBH_IDE_IFIntrqCompCBR[USBH_IDE_MAX_DEVICE]; /* Intrq Complete CALLBACK TABLE */
extern void USBH_HCDS_HCPMControl( unsigned char state, CALLBACK_PROC pfnCallback );
extern LONG IDE_FuncOpen( void );
typedef struct tagDEBUG_PARA{
UCHAR commandBlock[12]; /* Command Block */
}DEBUG_PARA;
/*=============================================================================================
// Function_Name: USBH_IDE_IFReset
//
// description : Initialzie this module
//
// Initialzie the varables used in this module.
//
// argument : void
//
// return : STATUS_SUCCESS Complete the process normally
// STATUS_UNSUCCESSFUL Error occured during the process
//
// global : classid Class management ID number
===============================================================================================*/
LONG USBH_IDE_IFReset( void )
{
UCHAR i;
LONG result;
OS_FLGPTN flgPtn;
USBH_SAMPLE_MSG *pMsg;
pUSBH_IDE_DeviceList = &USBH_IDE_DeviceList;
for(i = 0; i < USBH_IDE_MAX_DEVICE; i++) {
/* DeviceParameter Initialize */
devicePara[i].commandStatus = NO_OP;
devicePara[i].deviceStatus = DRV_FUNC_CMD_COMP;
devicePara[i].transferSize = 0x00000000;
devicePara[i].usedDMACh = DMA_NOT_USE;
/* Request Sensd Data Initialize */
memset(&devicePara[i].reqSenseData, 0x00, sizeof(devicePara[i].reqSenseData));
/* Identify Data Initialize */
memset(&devicePara[i].identifyData, 0x00, sizeof(devicePara[i].identifyData));
/* DeviceList Initialize */
pUSBH_IDE_DeviceList->deviceType[i] = NO_DEVICE;
}
pUSBH_IDE_DeviceList->deviceCount = 0;
/* When PM State is SLEEP or SNOOZE, enter ACTIVE60 */
if( (( RegRead( REG08_PM_Control_1 ) & MASK_PM_State ) == USBH_HCDS_HC_PM_STATE_SLEEP ) ||
(( RegRead( REG08_PM_Control_1 ) & MASK_PM_State ) == USBH_HCDS_HC_PM_STATE_SNOOZE ) ){
USBH_HCDS_HCPMControl( USBH_HCDS_HC_PM_STATE_ACTIVE60, USBH_IDE_FinishPMCallback);
// Waiting for the completion of the PM State transfering
OS_WaiFlg( FLGID_USBH_SAMPLE, FLG_EVENT_FINISH_PM_USBH_SAMPLE, OS_TWF_ORW, &flgPtn );
// clear the flag
OS_ClrFlg( FLGID_USBH_SAMPLE, ~(FLG_EVENT_FINISH_PM_USBH_SAMPLE) );
}
/* Set the IDE initial values */
IDE_FuncOpen();
/* IDE IntStat Clear */
RegWrite( REG08_IDE_IntStat, 0xFF );
/* Join Clear AllMediaFIFO */
RegWrite( REG08_ClrAllMediaFIFO_Join,
( BIT_ClrJoinIDE |
BIT_ClrJoinDMA1 |
BIT_ClrJoinDMA0 |
BIT_ClrJoinCPU_Rd |
BIT_ClrJoinCPU_Wr ) );
/* Join MediaFIFO */
RegSet( REG08_MediaFIFO_Join, BIT_JoinIDE );
/* MediaFIFO Clear */
RegSet( REG08_MediaFIFO_Control, BIT_MediaFIFO_Clr );
// Reset IDE device requirement
USBH_IDE_SendMessage( MBXID_IDE, MSG_REQ_IDE_HRST, 0, NULL );
// Waiting for the reset completion message from the IDEtask
OS_WaiFlg( FLGID_USBH_SAMPLE, FLG_EVENT_MSG_USBH_SAMPLE, OS_TWF_ORW, &flgPtn );
// Clear the flag
OS_ClrFlg( FLGID_USBH_SAMPLE, ~(FLG_EVENT_MSG_USBH_SAMPLE) );
// Receive message from mailbox.
OS_PRcvMbx( FLGID_USBH_SAMPLE, ( T_MSG** )&pMsg );
if( ((PARAM_MSG_NTFY_IDE_SRST *)pMsg->msgData)->result == IDE_REQ_OK) {
result = STATUS_SUCCESS;
} else {
result = STATUS_UNSUCCESSFUL;
}
// Free the message buffer
OS_RelMpf( pMsg->msgHead.useMpfId, pMsg );
return result;
}
/*=============================================================================================
// Function_Name: USBH_IDE_IFDeviceReset
//
// description : Reset the specified device
//
// Output the reset request of the specified device to the lower module.
// When do the process to the device, the process of the device is interrupted.
//
// argument : deviceNo (in)Device No.
//
// return : STATUS_SUCCESS Complete the process normally
// STATUS_UNSUCCESSFUL Error occured during the process
//
// flag : OpenFlag Indicate if the module is usable
//
// global : classid Class management ID number
===============================================================================================*/
LONG USBH_IDE_IFDeviceReset( USHORT deviceNo )
{
LONG result;
OS_FLGPTN flgPtn;
USBH_SAMPLE_MSG *pMsg;
USBH_IDE_SendMessage( MBXID_IDE, MSG_REQ_IDE_SRST, 0, NULL ); // IDE device reset request
// Waiting for the reset completion message from the IDEtask
OS_WaiFlg( FLGID_USBH_SAMPLE, FLG_EVENT_MSG_USBH_SAMPLE, OS_TWF_ORW, &flgPtn );
// Clear flag
OS_ClrFlg( FLGID_USBH_SAMPLE, ~(FLG_EVENT_MSG_USBH_SAMPLE) );
// Receive message from mailbox.
OS_PRcvMbx( FLGID_USBH_SAMPLE, ( T_MSG** )&pMsg );
if( ((PARAM_MSG_NTFY_IDE_SRST *)pMsg->msgData)->result == IDE_REQ_OK) {
result = STATUS_SUCCESS;
} else {
result = STATUS_UNSUCCESSFUL;
}
// Free the message buffer
OS_RelMpf( pMsg->msgHead.useMpfId, pMsg );
return result;
}
/*=============================================================================================
// Function_Name: USBH_IDE_IFGetDeviceList
//
// description : It informs the number of connected devices and each device type to the upper layer
//
// It informs the number of connected devices and each device type to the upper layer
//
// argument : *pList (in)Pointer of the device list
//
// return : STATUS_SUCCESS Complete the process normally
// STATUS_UNSUCCESSFUL Error occured during the process
===============================================================================================*/
LONG USBH_IDE_IFGetDeviceList( UCHAR *pList )
{
UCHAR i;
LONG result;
OS_FLGPTN flgPtn;
USBH_SAMPLE_MSG *pMsg;
PARAM_MSG_REQ_IDE_DEV_LIST deviceList;
deviceList.bufferPtr = (UCHAR *)pUSBH_IDE_DeviceList;
USBH_IDE_SendMessage( MBXID_IDE, MSG_REQ_IDE_DEV_LIST, sizeof(deviceList), &deviceList ); // IDE device reset request
// Waiting for the device acquisition message from the IDEtask
OS_WaiFlg( FLGID_USBH_SAMPLE, FLG_EVENT_MSG_USBH_SAMPLE, OS_TWF_ORW, &flgPtn );
// Clear flag
OS_ClrFlg( FLGID_USBH_SAMPLE, ~(FLG_EVENT_MSG_USBH_SAMPLE) );
// Receive message from mailbox.
OS_PRcvMbx( FLGID_USBH_SAMPLE, ( T_MSG** )&pMsg );
if( ((PARAM_MSG_NTFY_IDE_DEV_LIST *)pMsg->msgData)->result == IDE_REQ_OK) {
/* Change the deviceList to pList */
((DRIVEACCESS_FUNCDEVICELIST *)pList)->deviceCount = pUSBH_IDE_DeviceList->deviceCount;
for(i=0; i < USBH_IDE_MAX_DEVICE; i++){
((DRIVEACCESS_FUNCDEVICELIST *)pList)->deviceType[i] = pUSBH_IDE_DeviceList->deviceType[i];
}
result = STATUS_SUCCESS;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -