gonzui


Format: Advanced Search

tkernel_2/driver/tef_em1d/console/src/line_drv.cbare sourcepermlink (0.04 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:  *      line_drv.c   Console/Low-level serial I/O driver
   18:  *
   19:  *      Serial line low-level driver common part  : system-independent
   20:  */
   21: 
   22: #include "line_drv.h"
   23: #include "svc/ifserialio.h"
   24: #include <tm/tmonitor.h>
   25: 
   26: /* Register/Deregister subsystem */
   27: IMPORT  ER       con_def_subsys(W svc, W pri, void *svcent, void *brkent);
   28: 
   29: /* Port definition (system-dependent)*/
   30: #include "portdef.h"
   31: 
   32: /* Serial line management information */
   33: EXPORT  LINE_INFO        *LineInfo;
   34: EXPORT  W                nPorts = N_PORTS;
   35: 
   36: /*
   37:  *      Serial input
   38:  */
   39: LOCAL ER        _serial_in(SERIAL_SERIAL_IN_PARA *p)
   40: {
   41:         /* Parameter check */
   42:         if (p->port < 0 || p->port >= nPorts) return E_PAR;
   43: 
   44:         return (*(LineInfo[p->port].scdefs.fn->in))(
   45:                 &LineInfo[p->port], (UB*)p->buf, p->len, p->alen,
   46:                 (p->tmout < 0) ? TMO_FEVR : p->tmout);
   47: }
   48: 
   49: /*
   50:  *      Serial port Output
   51:  */
   52: LOCAL ER        _serial_out(SERIAL_SERIAL_OUT_PARA *p)
   53: {
   54:         /* Parameter check */
   55:         if (p->port < 0 || p->port >= nPorts) return E_PAR;
   56:         if (p->len <= 0) return E_OK;
   57: 
   58:         return (*(LineInfo[p->port].scdefs.fn->out))(
   59:                 &LineInfo[p->port], (UB*)p->buf, p->len, p->alen,
   60:                 (p->tmout < 0) ? TMO_FEVR : p->tmout);
   61: }
   62: 
   63: /*
   64:  *      Serial port control
   65:  */
   66: LOCAL ER        _serial_ctl(SERIAL_SERIAL_CTL_PARA *p)
   67: {
   68:         /* Parameter check */
   69:         if (p->port < 0 || p->port >= nPorts) return E_PAR;
   70: 
   71:         return (*(LineInfo[p->port].scdefs.fn->ctl))
   72:                 (&LineInfo[p->port], p->kind, p->arg);
   73: }
   74: 
   75: /*
   76:  *      Serial I/O extended SVC entry
   77:  */
   78: LOCAL ER        serial_io_entry(void *para, W fn)
   79: {
   80:         switch(fn) {
   81:         case SERIAL_SERIAL_IN_FN:      return _serial_in(para);
   82:         case SERIAL_SERIAL_OUT_FN:     return _serial_out(para);
   83:         case SERIAL_SERIAL_CTL_FN:     return _serial_ctl(para);
   84:         }
   85:         return E_RSFN;
   86: }
   87: 
   88: /*
   89:  *      Serial I/O break function
   90:  */
   91: LOCAL void      serial_io_break(ID tskid)
   92: {
   93:         tk_dis_wai(tskid, TTW_FLG);
   94: }
   95: 
   96: /*
   97:  *      Start up the serial interface driver
   98:  */
   99: EXPORT ER       startup_serial_io(BOOL StartUp)
  100: {
  101:         LINE_INFO      *li;
  102:         W              port, baud;
  103: static  union objname    name = {{ "sio0" }};
  104: 
  105:         /* Fetch and set the communication speed from "tmonitor" */
  106:         if ((baud = tm_extsvc(0x00, 0, 0, 0)) < 19200) baud = 0;
  107: 
  108:         /* Initialize the auxiliary port */
  109:         INIT_AUXPORT(StartUp);
  110: 
  111:         if (!StartUp) {
  112:                 /* Deregister the subsystem */
  113:                 con_def_subsys(SERIAL_SVC, SERIAL_PRI, NULL, NULL);
  114: 
  115:                 /* Stop the auxiliary port */
  116:                 START_AUXPORT(FALSE);
  117: 
  118:                 /* Stop the all serial ports */
  119:                 for (port = 0; port < nPorts; port++) {
  120:                         li = &LineInfo[port];
  121:                         li->scdefs = PortDefs[port];
  122: 
  123:                         /* Stop the serial port */
  124:                         (*(li->scdefs.fn->down))(li);
  125: 
  126:                         /* Release the receive buffer */
  127:                         if (li->in_buf) Free(li->in_buf);
  128: 
  129:                         /* Delete the event flag for notification of
  130:                                                 the lock and the interrupt */
  131:                         if (li->flg > 0) consDeleteMLock(&li->lock);
  132:                 }
  133: 
  134:                 /* Release the serial line management information */
  135:                 if (LineInfo) Free(LineInfo);
  136: 
  137:                 return E_OK;
  138:         }
  139: 
  140:         /* Start up the auxiliary port */
  141:         START_AUXPORT(TRUE);
  142: 
  143:         /* Initialize the serial line management information */
  144:         LineInfo = (LINE_INFO *)Malloc(sizeof(LINE_INFO) * nPorts);
  145:         if (! LineInfo) {nPorts = 0; return E_NOMEM;}
  146:         MEMSET(LineInfo, 0, sizeof(LINE_INFO) * nPorts);
  147: 
  148:         /* Start up the serial port */
  149:         for (port = 0; port < nPorts; port++) {
  150:                 li = &LineInfo[port];
  151:                 li->scdefs = PortDefs[port];
  152: 
  153:                 /* Change baud by that from t-monitor */
  154:                 if (baud != 0) li->scdefs.mode.baud = baud;
  155: 
  156:                 name.s[3] = '0' + port;
  157: 
  158:                 /* Allocate the receive buffer */
  159:                 li->in_buf = Malloc(li->in_bufsz = DEF_INBUFSZ);
  160:                 if (li->in_buf == NULL) return E_NOMEM;
  161: 
  162:                 /* Create the event flag for notification of the lock
  163:                                                         and the interrupt */
  164:                 li->flg = consCreateMLock(&li->lock, name.s);
  165: 
  166:                 /* RTS/CTS flow control - ON */
  167:                 li->flow.csflow = 1;
  168:                 li->flow.rsflow = 1;
  169: 
  170:                 /* Set various hardware */
  171:                 (*(li->scdefs.fn->up))(li);
  172:         }
  173: 
  174:         /* Register the subsystem */
  175:         return con_def_subsys(SERIAL_SVC, SERIAL_PRI,
  176:                                 &serial_io_entry, &serial_io_break);
  177: }