gonzui


Format: Advanced Search

tkernel_2/driver/tef_em1d/sysdsk/src/common.cbare sourcepermlink (0.01 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T-Kernel 2.0 Software Package
    4:  *
    5:  *    Copyright 2011 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2011/05/17.
   10:  *    Modified by T-Engine Forum at 2014/09/10.
   11:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/01.
   12:  *
   13:  *----------------------------------------------------------------------
   14:  */
   15: 
   16: 
   17: /*
   18:  *      common.c     System disk driver
   19:  *
   20:  *      Various common processings
   21:  */
   22: 
   23: #include "sdisk.h"
   24: 
   25: /*
   26:  *      Event notification
   27:  */
   28: EXPORT  void     sdSendEvt(DrvTab *drv, W kind)
   29: {
   30:         W      i;
   31:         DiskEvt        evt;
   32:         ER     er;
   33: 
   34:         evt.h.evttyp = (kind == TDE_EJECT && drv->OpenCnt != 0) ?
   35:                         TDE_ILLEJECT : kind;
   36:         evt.h.devid = drv->DevId;
   37: 
   38:         /* Set the subunit's open status to "info" */
   39:         evt.info = 0;
   40:         for (i = 0; i <= drv->nSUnit; i++) {
   41:                 if (drv->s.SUnit[i].OpenCnt > 0) evt.info |= 1 << i;
   42:         }
   43: 
   44:         /* Send to the message buffer for event notification:ignore error */
   45:         er = tk_snd_mbf(drv->MbfId, (void*)&evt, sizeof(evt), TMO_EVENT);
   46: }
   47: 
   48: /*
   49:  *      Set the device-support processing function
   50:  */
   51: EXPORT  void     sdSetUpAccFn(DrvTab *drv)
   52: {
   53:         switch(drv->Spec.accif) {
   54:         case MEM_ACCIF:
   55:                 drv->Abort = memAbort;
   56:                 drv->ReadWrite = memReadWrite;
   57:                 drv->Format = memFormat;
   58:                 drv->Identify = memIdentify;
   59:                 drv->DiskInit = memDiskInit;
   60:                 drv->Misc = memMisc;
   61:                 drv->DiskFmt = DiskFmt_MEM;
   62:                 drv->IntHdr = NULL;
   63:                 break;
   64:         case ATA_ACCIF:
   65:                 drv->Abort = ataAbort;
   66:                 drv->ReadWrite = ataReadWrite;
   67:                 drv->Format = ataFormat;
   68:                 drv->Identify = ataIdentify;
   69:                 drv->DiskInit = ataDiskInit;
   70:                 drv->Misc = ataMisc;
   71:                 drv->DiskFmt = DiskFmt_STD;
   72:                 drv->IntHdr = ataIntHdr;
   73:                 break;
   74:         case MMC_ACCIF:
   75:                 drv->Abort = mmcAbort;
   76:                 drv->ReadWrite = mmcReadWrite;
   77:                 drv->Format = mmcFormat;
   78:                 drv->Identify = mmcIdentify;
   79:                 drv->DiskInit = mmcDiskInit;
   80:                 drv->Misc = mmcMisc;
   81:                 drv->DiskFmt = DiskFmt_STD;
   82:                 drv->IntHdr = NULL;
   83:                 break;
   84:         }
   85: }
   86: 
   87: /*
   88:  *      Create a disk information (DiskInfo)
   89:  */
   90: EXPORT  void     sdMakeDiskInfo(DrvTab *drv, DiskInfo *inf)
   91: {
   92:         MEMSET(inf, 0, sizeof(DiskInfo));
   93:         inf->format = drv->DiskFmt;
   94:         inf->protect = drv->Spec.readonly ? TRUE : FALSE;
   95:         inf->removable = drv->Spec.eject;
   96:         inf->blocksize = drv->SecSize;
   97:         inf->blockcont = drv->s.SUnit[drv->CurSUnit].EndBlock -
   98:                          drv->s.SUnit[drv->CurSUnit].StartBlock;
   99: }
  100: 
  101: /*
  102:  *      Device registration
  103:  */
  104: EXPORT  ER       sdRegistDevice(DrvTab *drv)
  105: {
  106:         ER             er, err;
  107:         GDefDev                ddev;
  108:         DiskInfo       diskinfo;
  109: 
  110:         /* Disk initialization (read in the partition information)
  111:                 Continue processing to register physical device also when error occurs */
  112:         er = (*(drv->DiskInit))(drv);
  113: 
  114:         /* Set "DiskInfo" */
  115:         sdMakeDiskInfo(drv, &diskinfo);
  116: 
  117:         /* Registration information */
  118:         ddev = *GDI_ddev(drv->Gdi);
  119:         ddev.drvatr = (drv->Spec.lockreq) ? TDA_LOCKREQ : 0;
  120: 
  121:         switch(drv->Spec.accif) {
  122:         case MEM_ACCIF:        ddev.devatr = (drv->Spec.readonly) ?
  123:                                         TDK_DISK_ROM : TDK_DISK_RAM;
  124:                         break;
  125:         case ATA_ACCIF:        ddev.devatr = TDK_DISK_HD;
  126:                         break;
  127:         case MMC_ACCIF:        ddev.devatr = TDK_DISK_FLA;
  128:                         break;
  129:         default:       ddev.devatr = TDK_DISK_UNDEF;
  130:         }
  131: 
  132:         ddev.devatr |= (drv->DrvNo & 0x3F) << 16;
  133:         ddev.devatr |= (drv->Spec.eject) ? TD_REMOVABLE : 0;
  134:         ddev.devatr |= (drv->Spec.readonly) ? TD_PROTECT : 0;
  135: 
  136:         ddev.blksz = drv->SecSize;
  137:         ddev.nsub = drv->nSUnit;
  138: 
  139:         /* (Re)register the device */
  140:         err = GRedefDevice(&ddev, drv->Gdi);
  141:         if (err < E_OK) goto E_EXIT;
  142:         return er;
  143: E_EXIT:
  144:         return err;
  145: }
  146: 
  147: /*
  148:  *      Fetch the current time (msec)
  149:  */
  150: EXPORT  UW       sdGetMsec(void)
  151: {
  152:         SYSTIM ctm;
  153:         UW     ct;
  154: 
  155:         tk_get_otm(&ctm);              /* Current time : monotone increasing       */
  156: 
  157:         ct = ctm.lo;
  158:         return (ct == 0) ? 1 : ct;     /* Avoid 0          */
  159: }
  160: 
  161: /*
  162:  *      Check the time-out (msec unit)
  163:  */
  164: EXPORT  BOOL     sdChkTmout(UW *tm, UW tmo, W delay)
  165: {
  166:         UW     ct;
  167: 
  168:         ct = sdGetMsec();                      /* Current time    */
  169:         if (*tm == 0) *tm = ct;                        /* Start setting */
  170:         else if (ct - *tm >= tmo) return TRUE; /* Time-out */
  171: 
  172:         tk_dly_tsk(delay);                     /* Wait   */
  173:         return FALSE;
  174: }