gonzui


Format: Advanced Search

t2ex/t2ex_source/driver/tef_em1d/netdrv/src/main.cbare sourcepermlink (0.02 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T2EX Software Package
    4:  *
    5:  *    Copyright 2012 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 2012/12/12.
   10:  *    Modified by T-Engine Forum at 2013/03/08.
   11:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/04.
   12:  *
   13:  *----------------------------------------------------------------------
   14:  */
   15: /*
   16:  * This software package is available for use, modification, 
   17:  * and redistribution in accordance with the terms of the attached 
   18:  * T-License 2.x.
   19:  * If you want to redistribute the source code, you need to attach 
   20:  * the T-License 2.x document.
   21:  * There's no obligation to publish the content, and no obligation 
   22:  * to disclose it to the TRON Forum if you have modified the 
   23:  * software package.
   24:  * You can also distribute the modified source code. In this case, 
   25:  * please register the modification to T-Kernel traceability service.
   26:  * People can know the history of modifications by the service, 
   27:  * and can be sure that the version you have inherited some 
   28:  * modification of a particular version or not.
   29:  *
   30:  *    http://trace.tron.org/tk/?lang=en
   31:  *    http://trace.tron.org/tk/?lang=ja
   32:  *
   33:  * As per the provisions of the T-License 2.x, TRON Forum ensures that 
   34:  * the portion of the software that is copyrighted by Ken Sakamura or 
   35:  * the TRON Forum does not infringe the copyrights of a third party.
   36:  * However, it does not make any warranty other than this.
   37:  * DISCLAIMER: TRON Forum and Ken Sakamura shall not be held
   38:  * responsible for any consequences or damages caused directly or
   39:  * indirectly by the use of this software package.
   40:  *
   41:  * The source codes in bsd_source.tar.gz in this software package are 
   42:  * derived from NetBSD or OpenBSD and not covered under T-License 2.x.
   43:  * They need to be changed or redistributed according to the 
   44:  * representation of each source header.
   45:  */
   46: 
   47: /*
   48:  *      @(#)main.c
   49:  *
   50:  */
   51: 
   52: #include        "netdrv.h"
   53: 
   54: /* Get "DEVCONF" entry */
   55: IMPORT  W        GetDevConf(UB *name, W *val);
   56: 
   57: /*
   58:  *      Network driver information
   59:  */
   60: EXPORT  NetInf   Netinf[MAX_NETDEV];
   61: EXPORT  PRI      TskPri;
   62: 
   63: /*
   64:  *      Driver definition table
   65:  */
   66: IMPORT  DrvDef   DrvTab[];
   67: 
   68: /*
   69:  *      Network device name
   70:  */
   71: LOCAL   const     GDefDev     NETDRV_DDev = {
   72:         NULL,          /* exinf */
   73:         "Neta",                /* devnm */
   74:         1,             /* maxreqq */
   75:         0,             /* drvatr */
   76:         0,             /* devatr */
   77:         0,             /* nsub */
   78:         1,             /* blksz */
   79:         OpenProc,      /* open */
   80:         CloseProc,     /* close */
   81:         NULL,          /* abort */
   82:         EventProc,     /* event */
   83: };
   84: #define P_NETUNIT       3     /* Position of unit name 'a'                */
   85: 
   86: /*
   87:  *      Network device DEVCONF entry name
   88:  *              NETDEV#  kind IO-base IRQ-number
   89:  */
   90: LOCAL   B NetDevEnt[] = "NETDEV0";
   91: #define P_NETNO         6      /* Position of device number '0'     */
   92: 
   93: /*
   94:  *      Register a device
   95:  */
   96: LOCAL   ER        RegistDevice(NetInf *inf)
   97: {
   98:         ER     er;
   99:         GDefDev        devDef;
  100: 
  101:         /* Registration info. */
  102:         devDef = NETDRV_DDev;
  103:         devDef.exinf = inf;
  104:         devDef.devnm[P_NETUNIT] = inf->netno + 'a';
  105: 
  106:         /* Registration of device */
  107:         if ((er = GDefDevice(&devDef, NULL, &inf->Gdi)) >= E_OK) {
  108:                 inf->devid = GDI_devid(inf->Gdi);
  109:                 return E_OK;
  110:         }
  111: 
  112: DP(("Net%c: RegistDevice [%#x]\n", NetUnit(inf), er));
  113: 
  114:         inf->devid = INVALID_DEVID;
  115:         return er;
  116: }
  117: 
  118: /*
  119:  *      Creates and starts a task
  120:  */
  121: EXPORT  ID       CreTask(FP entry, PRI pri, W name, UW par, W stksz)
  122: {
  123:         T_CTSK ctsk;
  124:         ID     tskid;
  125:         ER     er;
  126: 
  127:         /* Creates a task */
  128:         ctsk.exinf = (VP)name;
  129:         ctsk.task = entry;
  130:         ctsk.itskpri = pri;
  131:         ctsk.stksz = stksz;
  132:         ctsk.tskatr = TA_HLNG | TA_RNG0;
  133: 
  134:         tskid = er = tk_cre_tsk(&ctsk);
  135:         if (er >= E_OK) {
  136:                 /* Starts a task */
  137:                 er = tk_sta_tsk(tskid, par);
  138:                 if (er < E_OK) tk_del_tsk(tskid);
  139:         }
  140:         return (er >= E_OK) ? tskid : er;
  141: }
  142: 
  143: /*
  144:  *      Task for request acceptance (Normal & special requests)
  145:  */
  146: LOCAL   void      MainTask(NetInf *inf)
  147: {
  148:         ER     er;
  149: 
  150:         /* Register a device */
  151:         er = RegistDevice(inf);
  152:         if (er < E_OK) goto EEXIT1;
  153: 
  154:         inf->exist = TRUE;             /* device exists */
  155: 
  156:         /* Register an interrupt handler */
  157:         if (inf->di.stat >= E_OK) {
  158:                 if (DefIntHdr(inf, TRUE) < E_OK) {
  159:                         inf->di.stat = E_LIMIT;
  160:                 } else if (isFORCE_OPEN(inf)) {       /* Force open */
  161:                         OpenProc(inf->devid, 0, inf->Gdi);
  162:                 }
  163:         }
  164: 
  165:         /* Start acceptance of device request */
  166:         AcceptRequest(inf);
  167: 
  168:         /* Never reach here */
  169: EEXIT1:
  170:         if (inf->initfn != NULL) (*(inf->initfn))(inf, -1);
  171: 
  172: DP(("Net%c: MainTask [%#x]\n", NetUnit(inf), er));
  173: 
  174:         tk_exd_tsk();
  175: }
  176: 
  177: /*
  178:  *      Network driver main
  179:  */
  180: EXPORT  ER       NetDrv(INT ac, B *av[])
  181: {
  182:         W      i, n, k, kk;
  183:         VP     tskname;
  184:         W      v[L_DEVCONF_VAL];
  185:         NetInf *inf;
  186:         B      *arg;
  187: static  W        ndrv = 0;
  188: 
  189:         if (ac < 0) {          /* Finish */
  190:                 /* Do minimum processing for shutdown.
  191:                    Hardware interrupts should be disabled, because when the
  192:                    interrupts are shared, a trouble may occur at next startup.
  193:                 */
  194:                 for (inf = &Netinf[0]; ndrv > 0; ndrv--, inf++) {
  195:                         inf->opencnt = 0;
  196:                         CloseProc(inf->devid, 0, inf->Gdi);
  197:                         if (inf->di.stat >= E_OK) DefIntHdr(inf, FALSE);
  198:                         tk_ter_tsk(inf->tskid);
  199:                         GDelDevice(inf->Gdi);
  200:                         if (inf->initfn != NULL) (*(inf->initfn))(inf, -1);
  201:                 }
  202:                 return E_OK;
  203:         }
  204: 
  205:         TskPri = DEFAULT_PRI;
  206:         SetOBJNAME(tskname, "netd");
  207: 
  208:         /* get parameters */
  209:         memset(v, 0, sizeof(v));
  210:         if (ac > 1 && (arg = av[1]) != NULL) {
  211:                 switch (*arg++) {
  212:                 case '!':             /* priority */
  213:                         TskPri = strtol(arg, (VP)&arg, 0);
  214:                         break;
  215:                 case '=':
  216:                         v[0] = strtol(arg, (VP)&arg, 0);
  217:                         if (*arg++ == '.') {
  218:                                 v[1] = strtol(arg, (VP)&arg, 0);
  219:                                 if (*arg++ == '.') {
  220:                                         v[2] = strtol(arg, (VP)&arg, 0);
  221:                                 }
  222:                         }
  223:                         break;
  224:                 default:
  225:                         return E_PAR;
  226:                 }
  227:         }
  228: 
  229:         /* Clear network device information */
  230:         memset(Netinf, 0, sizeof(Netinf));
  231:         for (i = 0; i < MAX_NETDEV; i++) Netinf[i].pciadr = -1;
  232: 
  233:         /* Create network driver task */
  234:         for (ndrv = i = 0; i < MAX_NETDEV; i++) {
  235: 
  236:                 /* get setting on DEVCONF */
  237:                 NetDevEnt[P_NETNO] = i + '0';
  238:                 if (i != 0 || v[0] == 0) {
  239:                         memset(v, 0, sizeof(v));
  240:                         if (GetDevConf(NetDevEnt, v) <= 0 || v[0] <= 0)
  241:                                         continue;
  242:                 }
  243:                 /* initialize driver information area */
  244:                 inf = &Netinf[i];
  245:                 inf->netno = i;                       /* Network number   */
  246:                 inf->di.kind = v[0];          /* Hardware kind        */
  247:                 inf->di.iobase =
  248:                         inf->orgbase = v[1]; /* I/O Base             */
  249:                 inf->di.intno = v[2];         /* IRQ Number          */
  250:                 inf->bufsz.minsz = MINPKTLEN; /* Receive buffer size  */
  251:                 inf->bufsz.maxsz = MAXPKTLEN;
  252: 
  253:                 /* Check hardware */
  254:                 if ((kk = v[0] & HK_MASK) == HK_PCI) {        /* PCI auto detect */
  255:                         for (n = 0; (k = DrvTab[n].kind) != 0; n++) {
  256:                                 if ((k & HK_PCI) != 0 && PciProbe(inf,
  257:                                         DrvTab[n].pcitab) >= E_OK) break;
  258:                         }
  259:                 } else {                      /* Fixed setting */
  260:                         for (n = 0; (k = DrvTab[n].kind) != 0 && k != kk; n++);
  261:                         if ((k & HK_PCI) != 0 &&
  262:                                 PciProbe(inf, DrvTab[n].pcitab) < E_OK) k = 0;
  263:                 }
  264:                 if (k != 0 && (*(DrvTab[n].initfn))(inf, 0) >= E_OK) {
  265:                         inf->initfn = DrvTab[n].initfn;
  266: 
  267:                         /* Create & start driver task */
  268:                         if ((inf->tskid = CreTask((FP)MainTask, TskPri,
  269:                                 (W)tskname, (UW)inf, TASK_STKSZ)) >= E_OK) {
  270:                                 ndrv++;             /* OK */
  271:                                 continue;
  272:                         }
  273: 
  274:                         (*(inf->initfn))(inf, -1);
  275:                         inf->initfn = NULL;
  276:                 }
  277:         }
  278:         return (ndrv > 0) ? E_OK : E_NOMDA;
  279: }
  280: