gonzui


Format: Advanced Search

mtkernel_3/device/common/drvif/msdrvif.cbare sourcepermlink (0.02 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    Device Driver for micro T-Kernel for 亮T-Kernel 3.0
    4:  *
    5:  *    Copyright (C) 2020 by Ken Sakamura.
    6:  *    This software is distributed under the T-License 2.2.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by TRON Forum(http://www.tron.org) at 2020/10/21.
   10:  *
   11:  *----------------------------------------------------------------------
   12:  */
   13: 
   14: 
   15: /*
   16:  *      msdrvif.c
   17:  *
   18:  *      mSDI: Simple Device driver I/F layer for micro T-Kernel
   19:  */
   20: 
   21: #include <tk/tkernel.h>
   22: #include "msdrvif.h"
   23: 
   24: /* ------------------------------------------------------------------------ */
   25: /*
   26:  * FastLock fot driver I/F access
   27:  */
   28: 
   29: #define LockMSDI(msdi)          Lock(&msdi->lock)
   30: #define UnlockMSDI(msdi)        Unlock(&msdi->lock)
   31: 
   32: /* ------------------------------------------------------------------------ */
   33: /*
   34:  *      Processing function
   35:  */
   36: 
   37: /*
   38:  * Open function
   39:  */
   40: LOCAL ER msdi_openfn( ID devid, UINT omode, T_MSDI *msdi )
   41: {
   42:         ER     err;
   43: 
   44:         if ( msdi->dmsdi.openfn == NULL ) return E_OK;
   45: 
   46:         Lock(&msdi->lock);
   47:         err = (*msdi->dmsdi.openfn)(devid, omode, msdi);
   48:         Unlock(&msdi->lock);
   49: 
   50:         return err;
   51: }
   52: 
   53: /*
   54:  * Close cunction
   55:  */
   56: LOCAL ER msdi_closefn( ID devid, UINT option, T_MSDI *msdi )
   57: {
   58:         ER     err;
   59: 
   60:         if ( msdi->dmsdi.closefn == NULL ) return E_OK;
   61: 
   62:         Lock(&msdi->lock);
   63:         err = (*msdi->dmsdi.closefn)(devid, option, msdi);
   64:         Unlock(&msdi->lock);
   65: 
   66:         return err;
   67: }
   68: 
   69: /*
   70:  * Processing start function
   71:  */
   72: LOCAL ER msdi_execfn( T_DEVREQ *req, TMO tmout, T_MSDI *msdi )
   73: {
   74:         INT    (*fp)( T_DEVREQ*, T_MSDI* );
   75:         ER     err;
   76: 
   77:         fp = ( req->cmd == TDC_READ )? msdi->dmsdi.readfn: msdi->dmsdi.writefn;
   78:         if ( fp == NULL ) return E_NOSPT;
   79: 
   80:         if ( req->start >= 0 && msdi->dmsdi.blksz <= 0) return E_NOSPT;
   81: 
   82:         /* I/O processing */
   83:         Unlock(&msdi->lock);
   84:         err = (*fp)(req, msdi);
   85:         Unlock(&msdi->lock);
   86: 
   87:         req->error = err;
   88:         return E_OK;
   89: }
   90: 
   91: /*
   92:  * Wait-for-completion function
   93:  */
   94: LOCAL INT msdi_waitfn( T_DEVREQ *req, INT nreq, TMO tmout, T_MSDI *msdi )
   95: {
   96:         return 0;
   97: }
   98: 
   99: /*
  100:  * Abort processing function
  101:  */
  102: LOCAL ER msdi_abortfn( ID tskid, T_DEVREQ *req, INT nreq, T_MSDI *msdi )
  103: {
  104:         return E_OK;
  105: }
  106: 
  107: /*
  108:  * Event function
  109:  */
  110: LOCAL INT msdi_eventfn( INT evttyp, void *evtinf, T_MSDI *msdi )
  111: {
  112:         INT    ret;
  113: 
  114:         if ( msdi->dmsdi.eventfn == NULL ) return E_OK;
  115: 
  116:         Lock(&msdi->lock);
  117:         ret = (*msdi->dmsdi.eventfn)(evttyp, evtinf, msdi);
  118:         Unlock(&msdi->lock);
  119: 
  120:         return ret;
  121: }
  122: 
  123: /* ------------------------------------------------------------------------ */
  124: /*
  125:  *      Device registration
  126:  */
  127: 
  128: /*
  129:  * SDI Device registration
  130:  */
  131: EXPORT ER msdi_def_dev( T_DMSDI *dmsdi, T_IDEV *idev, T_MSDI **p_msdi )
  132: {
  133:         T_MSDI *msdi;
  134:         T_DDEV ddev;
  135:         ER     err;
  136: 
  137:         /* Create "SDI"*/
  138:         msdi = Kmalloc(sizeof(T_MSDI));
  139:         if ( msdi == NULL ) {
  140:                 err = E_NOMEM;
  141:                 goto err_ret1;
  142:         }
  143: 
  144:         msdi->dmsdi = *dmsdi;          /* Structure Copy */
  145: 
  146:         /* Create the lock for exclusive access control */
  147:         err = CreateLock(&msdi->lock, dmsdi->devnm);
  148:         if ( err < E_OK ) goto err_ret2;
  149: 
  150:         /* Device registration */
  151:         ddev.exinf   = msdi;
  152:         ddev.drvatr  = dmsdi->drvatr;
  153:         ddev.devatr  = dmsdi->devatr;
  154:         ddev.nsub    = dmsdi->nsub;
  155:         ddev.blksz   = dmsdi->blksz;
  156:         ddev.openfn  = (FP)msdi_openfn;
  157:         ddev.closefn = (FP)msdi_closefn;
  158:         ddev.execfn  = (FP)msdi_execfn;
  159:         ddev.waitfn  = (FP)msdi_waitfn;
  160:         ddev.abortfn = (FP)msdi_abortfn;
  161:         ddev.eventfn = (FP)msdi_eventfn;
  162: 
  163:         err = tk_def_dev(msdi->dmsdi.devnm, &ddev, idev);
  164:         if ( err < E_OK )      goto err_ret3;
  165:         
  166:         msdi->devid = (ID)err;
  167:         if(p_msdi != NULL) {
  168:                 *p_msdi = msdi;
  169:         }
  170: 
  171:         return E_OK;
  172: 
  173: err_ret3:
  174:         DeleteLock(&msdi->lock);
  175: err_ret2:
  176:         Kfree(msdi);
  177: err_ret1:
  178:         return err;
  179: }
  180: 
  181: /*
  182:  * Deregistration
  183:  */
  184: EXPORT ER msdi_del_dev( T_MSDI *msdi )
  185: {
  186:         ER     err;
  187: 
  188:         /* Deregister device */
  189:         err = tk_def_dev(msdi->dmsdi.devnm, NULL, NULL);
  190:         if ( err > E_OK ) {
  191:                 DeleteLock(&msdi->lock);      /* Delete the lock for exclusive access control */
  192:                 Kfree(msdi);                  /* Delete "SDI" */
  193:                 err = E_OK;
  194:         }
  195:         return err;
  196: }
  197: