home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / tcpipsrc / Drivers / c / arc_io next >
Encoding:
Text File  |  1995-01-13  |  4.7 KB  |  202 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "internet.h"
  8. #include "iface.h"
  9. #include "cmdparse.h"
  10. #include "misc.h"
  11. #include "arc.h"
  12. #include "os.h"
  13. #include "swis.h"
  14. #include "driver.h"
  15.  
  16. struct asy asy[ASY_MAX];
  17. unsigned int nasy;
  18. struct interface *ifaces;
  19.  
  20. /* Called just before exiting to restore console state */
  21. void iostop(void)
  22. {
  23.   while(ifaces != NULLIF)
  24.   {
  25.     if (ifaces->stop != NULLFP)
  26.       (*ifaces->stop)(ifaces); /*hf pass ifaces not ifaces->dev otherwise we can't do it */
  27.     ifaces = ifaces->next;
  28.   }
  29. }
  30.  
  31. int asy_scan(void)
  32. {
  33.     struct interface *ifp;
  34.     int state = NULL;
  35.  
  36.     ifp = ifaces;
  37.  
  38.     while(ifp != NULLIF)
  39.     {
  40.       if (ifp->driver == NULL)
  41.         state |= 0x100;
  42.       else
  43.         state |= ((*ifp->driver)(DRIVER_MODEMCONTROL, ifp->subdevice) & 8);
  44.       ifp = ifp->next;
  45.     }
  46.     return(state);
  47. }
  48.  
  49. /* Initialize asynch port "dev" */
  50. int asy_init(int dev, struct interface *iface, unsigned int bufsize)
  51. {
  52.   bufsize = bufsize;
  53.  
  54.   asy[dev].iface      = iface;
  55.   asy[dev].speed      = 0;
  56.   asy[dev].rxchars    = 0;
  57.   asy[dev].rxserviced = 0;
  58.   asy[dev].txchars    = 0;
  59.   asy[dev].txoverruns = 0;
  60.   asy[dev].txserviced = 0;
  61.  
  62.   /* Initialise port to 9600 Baud, 8 Bits etc */
  63.   (*iface->driver)(DRIVER_INITIALISE,iface->subdevice);
  64.   (*iface->driver)(DRIVER_WORDFORMAT,iface->subdevice,0);
  65.   (*iface->driver)(DRIVER_CONTROLLINES,iface->subdevice,3);
  66. /*  (*iface->driver)(DRIVER_FLOWCONTROL,iface->subdevice,1); */
  67.  
  68.   return (0);
  69. }
  70.  
  71. int asy_stop(struct interface *interface)
  72. {
  73.   time_t when;
  74.   when = clock();
  75.   /* Drop RTS/DTR */
  76.   (*interface->driver)(DRIVER_CONTROLLINES,interface->subdevice,0);
  77.   while(clock() - when < 100);
  78.   (*interface->driver)(DRIVER_CONTROLLINES, interface->subdevice, 3);
  79.   /* Closedown */
  80.   (*interface->driver)(DRIVER_CLOSEDOWN,interface->subdevice);
  81.   return(0);
  82. }
  83.  
  84. /* Set asynch line speed */
  85. int asy_speed(int dev, int speed)
  86. {
  87.     if (speed==0)
  88.       return 0;
  89.  
  90.     if (dev >= nasy)
  91.       return(-1);
  92.  
  93.     asy[dev].speed = speed;
  94.  
  95.     /* Set speed */
  96.     (*asy[dev].iface->driver) (DRIVER_TXSPEED,asy[dev].iface->subdevice, speed);
  97.     (*asy[dev].iface->driver) (DRIVER_RXSPEED,asy[dev].iface->subdevice, speed);
  98.  
  99.     /* Check it's been set */
  100.     if ((*asy[dev].iface->driver) (DRIVER_RXSPEED,asy[dev].iface->subdevice,-1) != speed)
  101.     {
  102.       cwprintf(NULL, "asy_speed: Unknown speed (%d)\r\n", speed);
  103.       return(-1);
  104.     }
  105.  
  106.     return(0);
  107. }
  108.  
  109. int asy_flowctrl(int dev, int mode)
  110. {
  111.   if (mode > 1 || dev >= nasy)
  112.           return(-1);
  113.  
  114.   (*asy[dev].iface->driver) (DRIVER_CONTROLLINES,asy[dev].iface->subdevice,3);
  115.   (*asy[dev].iface->driver) (DRIVER_FLOWCONTROL,asy[dev].iface->subdevice,mode);
  116.   return 0;
  117. }
  118.  
  119.  
  120. /* Send a buffer to serial transmitter */
  121. int asy_output(int dev, char *buf, register int cnt)
  122. {
  123.         register struct asy *asyp;
  124.         register int i;
  125.  
  126.         if (dev >= nasy)
  127.         {
  128.                 cwprintf(NULL, "asy_output: invalid parameters\r\n");
  129.                 return(0);
  130.         }
  131.  
  132.         for (i = 0; i < cnt && (*asy[dev].iface->driver)(DRIVER_PUTBYTE, asy[dev].iface->subdevice, *buf)==0; i++, buf++);
  133.  
  134.         asyp = asy + dev;
  135.         asyp->txserviced++;
  136.         asyp->txchars += i;
  137.         if (i < cnt) asyp->txoverruns++;
  138.  
  139.         return(i);
  140. }
  141.  
  142. /* Receive characters from asynch line
  143.  * Returns count of characters read */
  144.  
  145. int asy_recv(int dev, char *buf)
  146. {
  147.   register int c;
  148.  
  149.   if (dev >= nasy)
  150.   {
  151.     cwprintf(NULL, "asy_recv: invalid parameters\r\n");
  152.     return(0);
  153.   }
  154.  
  155.   asy[dev].rxserviced++;
  156.   if ((c = (*asy[dev].iface->driver)(DRIVER_GETBYTE,
  157.              asy[dev].iface->subdevice)) >= 0)
  158.   {
  159.     *buf = c;
  160.     asy[dev].rxchars++;
  161.     return(-1);
  162.   }
  163.  
  164.   return(0);
  165. }
  166.  
  167. int asy_ioctl(struct interface *interface, int argc, char *argv[])
  168. {
  169.         int n;
  170.         if (argc < 1)
  171.         {
  172.                 cwprintf(NULL, "%d\n\n", asy[interface->dev].speed);
  173.                 return 0;
  174.         }
  175.         n = asy_speed(interface->dev, atoi(argv[0]));
  176.         if (n>=0 && argc>1)
  177.           n = asy_flowctrl(interface->dev, atoi(argv[1]));
  178.         return n;
  179. }
  180.  
  181. int doasystat(int argc, char **argv)
  182. {
  183.         struct asy *asyp;
  184.  
  185.         argc = argc;
  186.         argv = argv;
  187.  
  188.         for (asyp = asy; asyp < asy + nasy; asyp++)
  189.         {
  190.                 cwprintf(NULL, "interface %s:\r\n", asyp->iface->name);
  191.                 cwprintf(NULL, "RX: serviced: %8u  chars: %8u\r\n",
  192.                         asyp->rxserviced,
  193.                         asyp->rxchars);
  194.                 cwprintf(NULL, "TX: serviced: %8u  chars: %8u  overruns: %8u\r\n",
  195.                         asyp->txserviced,
  196.                         asyp->txchars,
  197.                         asyp->txoverruns);
  198.         }
  199.  
  200.         return(0);
  201. }
  202.