gonzui


Format: Advanced Search

tkernel_2/driver/tef_em1d/kbpd/src/main.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:  *      main.C
   18:  *
   19:  *       KB/PD device manager
   20:  *       manager initialization
   21:  */
   22: 
   23: #include "kbpd.h"
   24: 
   25: /* Get "DEVCONF" entry */
   26: IMPORT W GetDevConf ( UB *name, W *val );
   27: 
   28: /*
   29:  * manager information
   30:  */
   31: EXPORT MgrInfo kpMgrInfo;
   32: 
   33: /*
   34:  * register device
   35:  */
   36: LOCAL ER registDevice( BOOL regist )
   37: {
   38:         ER     er;
   39:         T_IDEV idev;
   40:         static const SDefDev   ddev = {
   41:                 NULL,         /* exinf */
   42:                 "kbpd",               /* devnm */
   43:                 0,            /* drvatr */
   44:                 0,            /* devatr */
   45:                 0,            /* nsub */
   46:                 1,            /* blksz */
   47:                 NULL,         /* open */
   48:                 NULL,         /* close */
   49:                 kpReadFn,     /* read */
   50:                 kpWriteFn,    /* write */
   51:                 kpEventFn,    /* event */
   52:         };
   53: 
   54:         if ( regist ) {
   55:                 /* registration */
   56:                 er = SDefDevice(&ddev, &idev, &kpMgrInfo.Sdi);
   57:                 if ( er < E_OK ) {
   58:                         DEBUG_PRINT(("SDefDevice err= %d\n", er));
   59:                         goto err_exit;
   60:                 }
   61:                 /* configure default message buffer for event notification */
   62:                 kpMgrInfo.eventMbf = idev.evtmbfid;
   63:         } else {
   64:                 er = SDelDevice(kpMgrInfo.Sdi);
   65:         }
   66: err_exit:
   67:         return er;
   68: }
   69: 
   70: /*
   71:  * manager initialization
   72:  */
   73: LOCAL ER initialize( PRI mypri )
   74: {
   75:         T_CMBX DataRcvMbx;
   76:         W      val[L_DEVCONF_VAL];
   77:         ER     err;
   78: 
   79:         /*
   80:          * initialization of manager information
   81:          */
   82:         MEMSET(&kpMgrInfo, 0, sizeof(kpMgrInfo));
   83: 
   84:         /*
   85:          * obtain configuration information
   86:          */
   87:         err = GetDevConf("KBMODE", val);
   88:         if ( err >= 1 ) {
   89:                 kpMgrInfo.kpState.stat.mode = val[0] & 0x03;
   90:                 kpMgrInfo.kpState.stat.han = (val[0] & 0x04) ? 1 : 0;
   91:         }
   92: 
   93:         /*
   94:          * create fast lock for exclusive control of manager
   95:          */
   96:         err = CreateLock(&kpMgrInfo.lock, "kbpd");
   97:         if ( err != E_OK ) {
   98:                 DEBUG_PRINT(("initialize fast lock err = %d\n", err));
   99:                 return err;
  100:         }
  101: 
  102:         /*
  103:          * create mailbox for receiving data from I/O driver
  104:          */
  105:         SetOBJNAME(DataRcvMbx.exinf, "kbpd");
  106:         DataRcvMbx.mbxatr = TA_TFIFO | TA_MFIFO;
  107:         err = tk_cre_mbx(&DataRcvMbx);
  108:         if ( err < E_OK ) {
  109:                 DEBUG_PRINT(("initialize DataRcvMbx. er = %d\n", err));
  110:                 return err;
  111:         }
  112:         kpMgrInfo.dataMbx = (ID)err;
  113: 
  114:         /*
  115:          * start the receiving task for data from I/O driver
  116:          */
  117:         err = kpStartDataReceiveTask(mypri-1);
  118:         if ( err != E_OK ) {
  119:                 DEBUG_PRINT(("start DataReceiveTask. err = %d\n", err));
  120:                 return err;
  121:         }
  122: 
  123:         return E_OK;
  124: }
  125: 
  126: /*
  127:  * manager termination processing
  128:  */
  129: LOCAL void finish( void )
  130: {
  131:         ID     id;
  132: 
  133:         /* unregister device */
  134:         registDevice(FALSE);
  135: 
  136:         /* stop the receiving task for data from I/O driver */
  137:         kpStopDataReceiveTask();
  138: 
  139:         /* release other resources */
  140:         if ( (id = kpMgrInfo.dataMbx) != InvalidID )   tk_del_mbx(id);
  141:         if ( (id = kpMgrInfo.acceptPort) != InvalidID )        tk_del_por(id);
  142:         DeleteLock(&kpMgrInfo.lock);
  143: }
  144: 
  145: /*
  146:  * manager initialization processing
  147:  */
  148: LOCAL ER kbpdManager( PRI mypri )
  149: {
  150:         ER             err;
  151: 
  152:         /* manager initialization */
  153:         err = initialize(mypri);
  154:         if ( err != E_OK ) {
  155:                 DEBUG_PRINT(("initialize err = %d\n", err));
  156:                 goto err_exit;
  157:         }
  158: 
  159:         /* register device */
  160:         err = registDevice(TRUE);
  161:         if ( err != E_OK ) {
  162:                 DEBUG_PRINT(("registDevice err = %d\n", err));
  163:                 goto err_exit;
  164:         }
  165: 
  166:         return E_OK;
  167: 
  168: err_exit:
  169:         /* epilog processing */
  170:         finish();
  171:         return err;
  172: }
  173: 
  174: /*
  175:  * entry
  176:  */
  177: EXPORT ER KbPdDrv( INT ac, UB *av[] )
  178: {
  179:         PRI            pri = DefaultPriority;
  180:         char           *arg;
  181:         W      v[L_DEVCONF_VAL];
  182: 
  183:         /* effective? */
  184:         if (GetDevConf("KbPdDrvEnable", v) > 0 && !v[0]) return E_NOSPT;
  185: 
  186:         /* driver never terminates: ignore termination request */
  187:         if ( ac < 0 ) return E_NOSPT;
  188: 
  189:         /* extract parameters */
  190:         if ( ac > 1 && (arg = av[1]) != NULL ) {
  191:                 while ( *arg != NULL ) {
  192:                         switch ( *arg ) {
  193:                         case '!':    /* priority */
  194:                                 arg++;
  195:                                 pri = STRTOUL(arg, &arg, 10);
  196:                                 break;
  197:                         default:
  198:                                 return E_PAR;
  199:                         }
  200:                 }
  201:         }
  202: 
  203:         /* start driver */
  204:         return kbpdManager(pri);
  205: }