dd_core.c 9.73 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
/*---------------------------------------------------------------------*
        Copyright (C) 1998 Nintendo.
        
        $RCSfile: dd_core.c,v $
        $Revision: 1.1.1.1 $
        $Date: 2002/05/02 03:27:21 $
 *---------------------------------------------------------------------*/

/*
 * DD スレッドの生成
 */ 

/* implementation */
extern u8 heap_rwbuf[];
#ifdef USE_TOKEN
s32 _shared_stack = 0;
#endif

static s32 _virtualQ = 0;

s32 
dd_create(void) /* constructor */
{
  static OSThread 		diskThread;
  static u64      		diskStack[STACKSIZE/sizeof(u64)];
  TMessage4dd           *msg = &dd_firstMsg;

  if( _created ) return 0;

  osCreateMesgQueue(&diskMsgQ, diskMsgBuf, DISK_MSG_NUM);
  osCreateMesgQueue(&diskQ, diskQBuf, 1);				   
  osCreateMesgQueue(&ddflushQ, ddflushQBuf, 1);				   
  osCreateMesgQueue(&frozenQ, frozenQBuf, 1);				   
  osCreateMesgQueue(&ddfirstQ, ddfirstQBuf, 1);				   
  osCreateMesgQueue(&recDiskMsgQ, recDiskMsgBuf, DISK_MSG_NUM);

#ifdef USE_TOKEN
  osCreateMesgQueue(&dd_tokenQ, dd_tokenQBuf, 1);
#if 0 /* 他のスレッドから dd_flush() を呼んでいる場合。できればこっちを使いたい。 */
  _created = 1;
  while( _virtualQ ) dd_wait();
#else
  if( _virtualQ )  return 0;   /* dd_create 失敗 */
  _created = 1;
//   osSendMesg(&dd_tokenQ,NULL,OS_MESG_BLOCK);
#endif
#endif

  dd_getHandle();

  osCreateThread(&diskThread, 8, (void *)coreObjectOfdd, 0
				 ,diskStack+STACKSIZE/sizeof(u64), DD_THREAD_PRIORITY);

  bzero( heap_rwbuf, READWRITE_BYTESIZE );/* default heap */
  bzero( &dd_stat , sizeof(TDDStatus) );

  osStartThread(&diskThread);
  dd_stat.mode = DDSTAT_BUSY;
  msg->retQ    = &ddfirstQ;
  msg->command = LEOCMD_INITIALIZE;
  dd_exec(msg);

  _unlockToken();

  return -1;
}

/*
 *  DDプロセス処理用のスレッド。
 * 
 *  命令発行は dd_exec にて行う。 
 *  dd_exec は dd_checkGate の返り値が真であることを確認してから発行する必要がある。
 *  そうでない場合 dd_execの実行結果は保証されない。
 *  ただし TMessage4dd の recQ が Nullの場合dd_execは同期関数になるので
 *  この場合 dd_checkGate は必要ない。
 */


static void
coreObjectOfdd( void * arg )
{
  s32	next_mode,_cmd,_retval;
  u8    unit_status=0;
  TMessage4dd *msg = arg; /* kill warning */

  while (1){

      /* modified */
    osRecvMesg( &diskMsgQ ,(OSMesg *)&msg , OS_MESG_BLOCK);

	_cmd = msg->command;
    next_mode = DDSTAT_BUSY;

	if( dd_stat.mode & DDSTAT_KILLED ) /*  KILLED 状態の復帰条件 */
	  {
		switch( dd_stat.errno )
		  {
#ifdef START_FROM_CASSETTE
		  case LEO_ERROR_REAL_TIME_CLOCK_FAILURE:
			if( _not_initialized && ( _cmd == LEOCMD_SETRTC ) )
			  {
			    dd_closeDialog(0);
				do{
				  next_mode = dd_setRTC((LEODiskTime *)&msg->param);
				}while(	next_mode == DDSTAT_CONTINUE );

				if( next_mode == DDSTAT_GOOD )
				  {
					dd_stat.mode = DDSTAT_CONTINUE;
					_cmd = LEOCMD_INITIALIZE;
					break;
				  }
				else
				  dd_stat.mode = DDSTAT_DEAD;
			  }
#endif
		  default:
			osSendMesg(msg->retQ, (OSMesg)0 , OS_MESG_BLOCK);			
			continue;
		  }
	  }
	
	_retval = -1; /* default */
	dd_stat.errmes = 0;
	_lockToken();

	do{
	  dd_stat.mode = next_mode;
      dd_wait(); // 文字表示を完了させる
	  switch ( _cmd )
		{
		case LEOCMD_INITIALIZE:
		  next_mode = dd_init();
		  if( next_mode == DDSTAT_GOOD )  _not_initialized = 0;
		  break;
		case LEOCMD_IDLE:
		  next_mode = DDSTAT_IDLE;
		  break;
		case LEOCMD_REZERO:
		  next_mode = dd_rezero();
		  break;
		case LEOCMD_SEEK:
		  next_mode = dd_seek( msg->param.hi , UNITMODE_LBA );
		  if( next_mode == DDSTAT_GOOD )
			dd_stat.position = msg->param.hi;
		  break;
		case LEOCMD_LBAREAD:
		  _setBuffer((u8 *)msg->param.rw);  
		  next_mode = dd_read(msg->param.hi , msg->param.lo , UNITMODE_LBA);
		  if( next_mode == DDSTAT_GOOD )
			{
			  msg->param.rw = (u32)_rwbuf;
			  _retval = dd_stat.buffer_size;
			}
		  break;
	    case LEOCMD_BYTEREAD:
          _setBuffer((u8 *)msg->param.rw);  
          next_mode = dd_read(msg->param.hi , msg->param.lo , UNITMODE_BYTE);
          if( next_mode == DDSTAT_GOOD )
            {
              msg->param.rw = (u32)_rwbuf;
			  msg->param.offs     = dd_stat.read_offset;
              _retval = dd_stat.buffer_size;		/* >= param.lo */
            }
          break;
		case LEOCMD_LBAWRITE:
		  _setBuffer((u8 *)msg->param.rw);  
		  next_mode = dd_lbaWrite(msg->param.hi ,msg->param.lo );
		  if( next_mode == DDSTAT_GOOD )
			{
              msg->param.rw = (u32)_rwbuf;
			  _retval = dd_stat.buffer_size;
			  dd_stat.position = msg->param.hi + msg->param.lo;
			}
		  break;
		case LEOCMD_ACTIVE:
		  next_mode = dd_activate( LEO_MOTOR_ACTIVE );
		  break;
		case LEOCMD_STANDBY:
		  next_mode = dd_activate( LEO_MOTOR_STANDBY);
		  break;
		case LEOCMD_SLEEP:
		  next_mode = dd_calmDown( LEO_MOTOR_SLEEP  );
		  break;
		case LEOCMD_BRAKE:
		  next_mode = dd_calmDown( LEO_MOTOR_BRAKE  );
		  break;
		case LEOCMD_GETID:
		  next_mode = msg->param.lo ? dd_checkInsert() : dd_errSeqDiskChange();
		  if( next_mode == DDSTAT_GOOD )
			{
			  if( (msg->param.hi) & 0x80000000 )
				bcopy( &_diskID , (void *)msg->param.hi ,sizeof(LEODiskID) );
			}
		  break;	
		case LEOCMD_EJECT:
		  next_mode = dd_checkEject();
		  break;	
		case LEOCMD_SETMOTORTIME:
		  next_mode = dd_setMotorTime(msg->param.hi,msg->param.lo);
		  break;
		case LEOCMD_GETRTC:
		  next_mode = dd_readRTC();
		  if( next_mode == DDSTAT_GOOD ) bcopy(&_rtc,&msg->param,sizeof(LEODiskTime));
		  break;
		case LEOCMD_SETRTC:
		  next_mode = dd_setRTC((LEODiskTime *)&msg->param);
		  break;
		  
		case LEOCMD_TESTUNIT:
		  next_mode = dd_testUnit( (LEOStatus *)&unit_status );
		  _retval = 0x100 | unit_status;
		  break;
		  
		default:
		  dd_pushError(LEO_ERROR_USER_DEFINED);
		  next_mode = dd_defaultErrors();
		}
    }while( next_mode & DDSTAT_CONTINUE );

	_unlockToken();

	dd_stat.mode = next_mode; 
	if ( next_mode == DDSTAT_GOOD )
	  dd_closeDialog(0);
	else
	  _retval = 0;
 
	osSendMesg(msg->retQ, (OSMesg)_retval , OS_MESG_BLOCK); /*  処理の終了を通知 */
  }
}

/*  DDスレッドへのコマンド発行用関数。 */
/*  引数のメッセ−ジ構造体に retQ が指定されていれば非同期 */
/*  0なら同期関数になる。 */

s32
dd_exec( TMessage4dd * msg )
{

  if(!(dd_stat.mode & DDSTAT_DEAD ) && msg->locked == FALSE )
	{
	  msg->locked = TRUE;
	  if(!msg->retQ)
		{
		  msg->retQ = &recDiskMsgQ;  /*  テンポラリのメッセ−ジQ */
		  osSendMesg(&diskMsgQ, (OSMesg) msg , OS_MESG_BLOCK);
		  osRecvMesg( msg->retQ , (OSMesg *)&(msg->retVal), OS_MESG_BLOCK);
		  msg->locked = false;
		  msg->retQ = NULL;
		}
	  else
		osSendMesg(&diskMsgQ, (OSMesg) msg , OS_MESG_BLOCK);
	}
  else
	{
	  msg->retVal = 0;
	  return false;
	}
  return true;
}

s32 
dd_checkGate( TMessage4dd *ddmsg ) /*  DD への命令を非同期に行うためのフィルタ。 */
{
  if( dd_stat.mode & ( DDSTAT_DEAD | DDSTAT_BUSY | DDSTAT_CONTINUE ) )
	return 0;

  if ( ddmsg->locked == true && ddmsg->retQ )
	{
	  if(osRecvMesg( ddmsg->retQ,(OSMesg *)&(ddmsg->retVal) ,OS_MESG_NOBLOCK))
		return 0;
	  ddmsg->locked = false; 
	}
  return -1;
}

void           
dd_change_mode_USA(void)
{
  _CA = 1;
}

u32
dd_getVersion(void)
{
  LEOVersion ver;
  u32 ret=0;
 
  if( !LeoInquiry(&ver) )
	{
	   ret  = (ver.drive<<24)    & 0xff000000;
	   ret |= (ver.driver<<16)   & 0x00ff0000;
	   ret |= (ver.deviceType<<8)& 0x0000ff00;
	   ret |= (ver.ndevices<<0)  & 0x000000ff;
	}
  return ret;
}

s32
dd_getBlockSize(s32 lba , s32 size)
{
  s32 ret;
  LeoLBAToByte( lba ,size ,&ret);
  return ret;
}

s32
dd_saveID(LEODiskID * det)
{
  bcopy( det, &_savedID ,sizeof(LEODiskID));
  return -1;
}

void
dd_wait(void) /*  次のリトレースまで待つ  */
{
 _unlockToken();
  while (!osRecvMesg(&ddflushQ, NULL, OS_MESG_NOBLOCK)); /*  <- セミコロン必要! */
  osRecvMesg(&ddflushQ,(OSMesg *)NULL, OS_MESG_BLOCK);
  _lockToken();
}

void
dd_flush(void)
{
/*  from core */
  if( _created ) osSendMesg(&ddflushQ, (OSMesg)NULL, OS_MESG_NOBLOCK);
}

void
dd_errHook(s32 func_no , s32 err_no , s32 count )
{
  _hook.target = func_no;
  _hook.errno  = err_no;
  _hook.cnt    = count;
}

s32
dd_thaw(s32 target_err) /*  中断したスレッドの再開。 */
{
  if( !_created ) return 0;
  if( _already_frozen && ( dd_stat.errmes == target_err ) )
	{
	  _lockToken();
	  _already_frozen = 0;
	  osSendMesg(&frozenQ,(OSMesg *)NULL, OS_MESG_BLOCK);
	  return -1;
	}
  return 0;
}


TDDToken
dd_getToken(TDDToken token ,s32 _block)
{
#ifdef USE_TOKEN
  if( _shared_stack > 0 )
	{
	  _shared_stack |= token;
	  return token; 
	}
  else
	{
	  if( _created )
		_shared_stack =
		  osRecvMesg( &dd_tokenQ,(OSMesg *)NULL ,_block) ? 0 : token;
	  else
		{
		  _shared_stack = token;
		  _virtualQ = 1;
		}
	}
  return _shared_stack;
#else
  _block = _block;
  return token;
#endif
}

void
dd_freeToken(TDDToken token)
{
  if(token > 0 && _shared_stack > 0 )
	{
	  _shared_stack &= ~token;
	  if( _shared_stack == 0 )
		{
#ifdef USE_TOKEN
		  if( _virtualQ ) 
			_virtualQ = 0;
		  else 
			osSendMesg( &dd_tokenQ,(OSMesg)NULL ,OS_MESG_BLOCK);
#endif
		}
	}
}

OSPiHandle *
dd_getHandle(void)
{
  if(!_dev_handler) _dev_handler = osDriveRomInit();
  return _dev_handler;
}

TDDStatus *
dd_getStatus(void)
{
  return &dd_stat;
}

void
_lockToken(void)
{
#ifdef USE_TOKEN
  osRecvMesg( &dd_tokenQ,(OSMesg *)NULL ,OS_MESG_BLOCK);/*  PIバス使用権の取得   */
#endif
}

void
_unlockToken(void)
{
#ifdef USE_TOKEN
  osSendMesg( &dd_tokenQ,(OSMesg)NULL ,OS_MESG_BLOCK ); /*  PIバス使用権の解放   */
#endif
}

void
_setBuffer( u8 *buf )
{
  u32 chopper = ~0xF;

  if( buf == NULL )
	{
	  _rwbuf = heap_rwbuf;
	  _rwbuf_size = READWRITE_BYTESIZE;
	}
  else
	{
	  _rwbuf = (u8 *)(((u32)buf + 7 ) & chopper);
	  _rwbuf_size = DD_MAX_BUFSIZE;
	}
}

u8 *
dd_getDefaultBuffer( void )
{
  return _created ? heap_rwbuf : 0;
}

s32
dd_driveExist( void )
{
  static s32 _flag = 0;
  _flag = _flag ? _flag : LeoDriveExist();
  return _flag;
}

void
dd_killCommand(void)
{
//  LeoClearQueue();
  dd_errHook(-1,LEO_ERROR_COMMAND_TERMINATED,1);
}