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

📄 id3_cd.c

📁 小型操作系统,以VC为开发环境,需要boachs调试
💻 C
字号:
/***************************************************************************
**     File name   : id2_cd.c
**     Author      : x.cheng
**     Create date :
**
**	   Comment:
**			This is the low-level cd interrupt support.
**
**     Revisions: 
**     $Log: id3_cd.c,v $
**     Revision 1.2  2005/08/11 14:41:36  x.cheng
**     ide channel 1 supurted, attach cdrom to channel 1, master
**
**     Revision 1.1  2005/08/09 16:09:33  x.cheng
**     add into repositories
**
**
***************************************************************************/
#include "const.h"
#include "type.h"
#include "string.h"

#define __IDE_HD_SRC__ 
#include "..\..\Inc\i386\io.h"
#include "..\..\Inc\i386\system.h"
#include "..\..\Inc\i386\x86.h"
#include "..\..\Inc\tui.h"
#include "..\..\Inc\ide.h"
#include "..\..\Inc\debug.h"

#include "..\inc\def_hd.h"
#include "..\inc\def_ide.h"
#include "..\inc\def_ata.h"
#include "..\inc\def_atapi.h"

/* debug preprocessor instrument
	#ifdef _DEBUG__
	#ifdef _DEBUG_IDE__
				
	#endif
	#endif	
***************************/

/*************global variable******************/
	//-------------------------------------------
	// The request-struct contains all necessary data
	// to load a nr of sectors into memory
extern ts_IdeRequest g_astRequest[MAX_REQUEST_NR];
	//-------------------------------------------
	// used to wait on when there are no free requests
extern ts_Task* g_pstWaitForRequest;
	//-------------------------------------------
extern ts_IdeDevice g_astDevice[IDE_DEVICE_NR];


/************local function prototype**************/
static int iId3SendPacketCdrom();
unsigned char ucId3SendPacketResult( ts_AtapiPacket* pstCommand) ;
//static int Id3CdromIsReady(void);
static void Id3CdromReset(void);

/************************************************************
*************************************************************
**      Function Name:			DoCdRequest
**      Author:                 x.cheng
**
**      Comment:
**			每次ide控制器(CDrom)中断会调用这个函数,由IdeAddRequest()
**		 第一次调用激活处理流程。
**
**      List of parameters:
**			no
**
**      Return value:   
**          no
**
**      Revisions:
**
*************************************************************
*************************************************************/
void DoCdRequest(void)
{
	ts_AtapiPacket stCommand;
	int iError;

	ts_IdeRequest* pstCurrent;

	if ( !(pstCurrent=g_astDevice[IDE_DEVICE_CD].pstCurrentRequest) ) {
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
		kprintf("\n%s:%s:%d line, no request...\n", __FILE__,__FUNCTION__,__LINE__);
#endif
#endif
		return;
	}
			
	if (pstCurrent->ucCmd == IDE_READ_BLK) {
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
		POSITION1("handle a cd read request....\n");
#endif
#endif
		stCommand.iCount = 1* 2048;	// one bufferblock size = 2048.
		stCommand.aucPacket[ 0 ] = CMD_READ_10;
	    stCommand.aucPacket[ 2 ] = ( pstCurrent->ulStartOfSector >> 24 ) & 0xFF;
		stCommand.aucPacket[ 3 ] = ( pstCurrent->ulStartOfSector >> 16 ) & 0xFF;
		stCommand.aucPacket[ 4 ] = ( pstCurrent->ulStartOfSector >>  8 ) & 0xFF;
		stCommand.aucPacket[ 5 ] = ( pstCurrent->ulStartOfSector >>  0 ) & 0xFF;
		stCommand.aucPacket[ 6 ] = ( 1   >> 16 ) & 0xFF;
		stCommand.aucPacket[ 7 ] = ( 1   >>  8 ) & 0xFF;
		stCommand.aucPacket[ 8 ] = ( 1   >>  0 ) & 0xFF;

		iError = iId3SendPacketCdrom(&stCommand);
/*
		if ( ucId3SendPacketResult(&stCommand) & DRQ_STATUS ) {
			iId3ReadCdData(pstCurrent);
		}
*/
	} else if (pstCurrent->ucCmd == IDE_WRITE_BLK){
		kprintf("No cd writing with this way is possible!\n");
	} else {
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
		kprintf("unexpected request....\n");
#endif
#endif
	}

	return;
}

/************************************************************
*************************************************************
**      Function Name:			iId3ReadCdData
**      Author:                 x.cheng
**
**      Comment:
**			
**
**      List of parameters:
**			pstRequest - 
**
**      Return value:   
**          no
**
**      Revisions:
**
*************************************************************
*************************************************************/
int iId3ReadCdData(ts_IdeRequest* pstRequest)
{
	int iLen;

#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
	kprintf("Reading ATAPI reponse data...\n");
#endif
#endif
	iLen = ucInPortDelay(ATAPI_PRIMARY_LOCYL) + 256*ucInPortDelay(ATAPI_PRIMARY_HICYL);

	ReadPortString(ATAPI_PRIMARY_DATA, pstRequest->pucBuffer, iLen/2);
//	HexDump( pstRequest->pucBuffer, 128);

	pstRequest->ucError = 0;

	return (-1);
}

/************************************************************
*************************************************************
**      Function Name:			Id3CdromSetup
**      Author:                 x.cheng
**
**      Comment:
**			设置cdrom信息以及其他配置信息,
**
**      List of parameters:
**			no
**
**      Return value:   
**          no
**
**      Revisions:
**
*************************************************************
*************************************************************/
void Id3CdromSetup(void)
{
	Id3CdromReset();
}




//----------------------------------------------------------------------------------
//==============================local function======================================
//----------------------------------------------------------------------------------




/************************************************************
*************************************************************
**      Function Name:			iId3SendPacketCdrom
**      Author:                 x.cheng
**
**      Comment:
**			向cdrom发送命令, 读写扇区
**
**      List of parameters:
**			
**
**      Return value:   
**          
**
**      Revisions:
**
*************************************************************
*************************************************************/
static int iId3SendPacketCdrom( ts_AtapiPacket* pstCommand )
{
	int iLen;
	int iTotal;
	unsigned short* puiOffset = (unsigned short*)pstCommand->aucPacket;

	if ( !iId5ControllerReady(IDE_CHANNEL_1) )
		PANIC1("ide controller is too busy...\n");

	vOutPortDelay( ATAPI_PRIMARY_FEATURE, 0 );	//may be bug
	vOutPortDelay( ATAPI_PRIMARY_IRR, 0);
	vOutPortDelay( ATAPI_PRIMARY_SAMTAG, 0);
	vOutPortDelay( ATAPI_PRIMARY_LOCYL, (pstCommand->iCount>>0)&0xFF);
	vOutPortDelay( ATAPI_PRIMARY_HICYL, (pstCommand->iCount>>8)&0xFF);
	
		//none dma
	//vOutPortDelay( ATA_CTL, CTL_EIGHTHEADS | CTL_INTDISABLE );
	//vOutPortDelay( ATAPI_PRIMARY_CURRENT, 0xb0);	//device 1
	vOutPortDelay( ATAPI_PRIMARY_CURRENT, 0xa0);	//channel 1, device 0		

    vOutPortDelay( ATAPI_PRIMARY_COMMAND, CMD_ATAPI_PACKET );

	if ( ucId3SendPacketResult(pstCommand) & DRQ_STATUS ) {
		//writing atapi packet...
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
		kprintf("Writing ATAPI packet...\n");
		HexDump(&pstCommand->aucPacket[0], 12); 
#endif
#endif
		WritePortString(ATAPI_PRIMARY_DATA, &pstCommand->aucPacket[0], 12);
	} else {
		POSITION1("error or timeout encountered.\n");
		return (-1);
	}

	return 0;
}

/************************************************************
*************************************************************
**      Function Name:			ucId3SendPacketResult
**      Author:                 x.cheng
**
**      Comment:
**			判断ATAPI执行命令后的状态
**
**      List of parameters:
**			
**
**      Return value:   
**          no
**
**      Revisions:
**
*************************************************************
*************************************************************/
unsigned char ucId3SendPacketResult( ts_AtapiPacket* pstCommand) 
{
	unsigned long iRetries = 5000;

	while( --iRetries ) {
		pstCommand->ucStatus = ucInPortDelay(ATAPI_PRIMARY_STATUS);
		if ( !(pstCommand->ucStatus&DRQ_STATUS) && !(pstCommand->ucStatus&ERR_STATUS) )
			continue;
		else
			break;
	}
	
	if ( pstCommand->ucStatus&ERR_STATUS ) {
		pstCommand->ucError = ucInPortDelay( ATAPI_PRIMARY_ERROR );
		pstCommand->ucSenseKey = pstCommand->ucError >> 4;
	}

#ifdef _DEBUG__
#ifdef _DEBUG_IDE__	
	kprintf("pstCommand->ucStatus = %x, ucSenseKey= %x\n", pstCommand->ucStatus, pstCommand->ucSenseKey);
#endif
#endif
	return (pstCommand->ucStatus);

}

/************************************************************
*************************************************************
**      Function Name:			Id3CdromReset
**      Author:                 x.cheng
**
**      Comment:
**			
**
**      List of parameters:
**			no
**
**      Return value:   
**          no
**
**      Revisions:
**
*************************************************************
*************************************************************/
static void Id3CdromReset(void)
{
	if ( iId5WaitIdeStatus(IDE_CHANNEL_1, BUSY_STATUS|DRQ_STATUS, 0) ) {
		POSITION1("ATAPI drive not ready\n");
		return;
	}

	vOutPortDelay( ATAPI_PRIMARY_FEATURE, 0);
	vOutPortDelay( ATAPI_PRIMARY_IRR, 0);
	vOutPortDelay( ATAPI_PRIMARY_SAMTAG, 0);
	vOutPortDelay( ATAPI_PRIMARY_LOCYL, 0);
	vOutPortDelay( ATAPI_PRIMARY_HICYL, 0);
	vOutPortDelay( ATAPI_PRIMARY_CMD, CMD_ATAPI_RESET);

    if ( iId5WaitIdeStatus(IDE_CHANNEL_1, BUSY_STATUS, 0) ) {
		POSITION1("Reset failed\n");
		return;
    }
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
    POSITION1( "reset ok\n" );
#endif
#endif
//	PANIC();
    return;
}

⌨️ 快捷键说明

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