2 #include "../port/lib.h"
7 #include "../port/error.h"
9 #include "../port/netif.h"
13 /* soft flow control chars */
18 extern Dev uartdevtab;
19 extern PhysUart* physuart[];
21 static Uart* uartlist;
24 static Dirtab *uartdir;
26 static Timer *uarttimer;
30 Uart *elist; /* list of enabled interfaces */
33 static void uartclock(void);
34 static void uartflow(void*);
37 * enable/disable uart and add/remove to list of enabled uarts
46 return p; /* too soon; try again later */
50 if((p->iq = qopen(8*1024, 0, uartflow, p)) == nil)
56 if((p->oq = qopen(8*1024, 0, uartkick, p)) == nil){
67 p->ie = &p->istage[Stagesize];
71 p->hup_dsr = p->hup_dcd = 0;
74 /* assume we can send */
87 (*p->phys->enable)(p, 1);
91 * use ilock because uartclock can otherwise interrupt here
92 * and would hang on an attempt to lock uartalloc.
95 for(l = &uartalloc.elist; *l; l = &(*l)->elist){
100 p->elist = uartalloc.elist;
114 (*p->phys->disable)(p);
117 for(l = &uartalloc.elist; *l; l = &(*l)->elist){
128 uartmouse(Uart* p, int (*putc)(Queue*, int), int setb1200)
131 if(p->opens++ == 0 && uartenable(p) == nil){
143 uartsetmouseputc(Uart* p, int (*putc)(Queue*, int))
146 if(p->opens == 0 || p->special == 0){
161 if(p && p->opens && p->iq)
162 uartdir[1+3*i].length = qlen(p->iq);
163 } else for(i = 0; i < uartnuart; i++){
165 if(p && p->opens && p->iq)
166 uartdir[1+3*i].length = qlen(p->iq);
171 * set up the '#t' directory
181 for(i = 0; physuart[i] != nil; i++){
182 if(physuart[i]->pnp == nil)
184 if((p = physuart[i]->pnp()) == nil)
190 for(tail = p; tail->next != nil; tail = tail->next)
196 uart = xalloc(uartnuart*sizeof(Uart*));
198 uartndir = 1 + 3*uartnuart;
199 uartdir = xalloc(uartndir * sizeof(Dirtab));
200 if (uart == nil || uartdir == nil)
201 panic("uartreset: no memory");
203 strcpy(dp->name, ".");
204 mkqid(&dp->qid, 0, 0, QTDIR);
206 dp->perm = DMDIR|0555;
209 for(i = 0; i < uartnuart; i++){
210 /* 3 directory entries per port */
211 snprint(dp->name, sizeof dp->name, "eia%d", i);
212 dp->qid.path = NETQID(i, Ndataqid);
215 snprint(dp->name, sizeof dp->name, "eia%dctl", i);
216 dp->qid.path = NETQID(i, Nctlqid);
219 snprint(dp->name, sizeof dp->name, "eia%dstatus", i);
220 dp->qid.path = NETQID(i, Nstatqid);
226 if(p->console || p->special){
227 if(uartenable(p) != nil){
238 * at 115200 baud, the 1024 char buffer takes 56 ms to process,
239 * processing it every 22 ms should be fine.
241 uarttimer = addclock0link(uartclock, 22);
247 uartattach(char *spec)
249 return devattach('t', spec);
253 uartwalk(Chan *c, Chan *nc, char **name, int nname)
255 return devwalk(c, nc, name, nname, uartdir, uartndir, devgen);
259 uartstat(Chan *c, uchar *dp, int n)
261 if(NETTYPE(c->qid.path) == Ndataqid)
262 setlength(NETID(c->qid.path));
263 return devstat(c, dp, n, uartdir, uartndir, devgen);
267 uartopen(Chan *c, int omode)
271 c = devopen(c, omode, uartdir, uartndir, devgen);
273 switch(NETTYPE(c->qid.path)){
276 p = uart[NETID(c->qid.path)];
278 if(p->opens++ == 0 && uartenable(p) == nil){
287 c->iounit = qiomaxatomic;
292 uartdrained(void* arg)
297 return qlen(p->oq) == 0 && p->op == p->oe;
301 uartdrainoutput(Uart *p)
303 if(!p->enabled || up == nil)
311 sleep(&p->r, uartdrained, p);
320 if(c->qid.type & QTDIR)
322 if((c->flag & COPEN) == 0)
324 switch(NETTYPE(c->qid.path)){
327 p = uart[NETID(c->qid.path)];
329 if(--(p->opens) == 0){
332 p->ir = p->iw = p->istage;
344 p->dcd = p->dsr = p->dohup = 0;
352 uartread(Chan *c, void *buf, long n, vlong off)
357 if(c->qid.type & QTDIR){
359 return devdirread(c, buf, n, uartdir, uartndir, devgen);
362 p = uart[NETID(c->qid.path)];
363 switch(NETTYPE(c->qid.path)){
365 return qread(p->iq, buf, n);
367 return readnum(offset, buf, n, NETID(c->qid.path), NUMSIZE);
369 return (*p->phys->status)(p, buf, n, offset);
376 uartctl(Uart *p, char *cmd)
381 nf = tokenize(cmd, f, nelem(f));
382 for(i = 0; i < nf; i++){
383 if(strncmp(f[i], "break", 5) == 0){
384 (*p->phys->dobreak)(p, 0);
393 if((*p->phys->baud)(p, n) < 0)
403 (*p->phys->dtr)(p, n);
424 (*p->phys->fifo)(p, n);
429 (*p->phys->dobreak)(p, n);
434 if((*p->phys->bits)(p, n) < 0)
440 (*p->phys->modemctl)(p, n);
450 if((*p->phys->parity)(p, *(f[i]+1)) < 0)
463 (*p->phys->rts)(p, n);
468 if((*p->phys->stop)(p, n) < 0)
473 if(uarttimer == nil || n < 1)
475 uarttimer->tns = (vlong)n * 100000LL;
491 uartwrite(Chan *c, void *buf, long n, vlong)
496 if(c->qid.type & QTDIR)
499 p = uart[NETID(c->qid.path)];
501 switch(NETTYPE(c->qid.path)){
509 n = qwrite(p->oq, buf, n);
516 memmove(cmd, buf, n);
525 /* let output drain */
526 if(uartctl(p, cmd) < 0)
539 uartwstat(Chan *c, uchar *dp, int n)
546 if(QTDIR & c->qid.type)
548 if(NETTYPE(c->qid.path) == Nstatqid)
551 dt = &uartdir[1 + 3 * NETID(c->qid.path)];
552 n = convM2D(dp, n, &d, nil);
556 dt[0].perm = dt[1].perm = d.mode;
565 for(p = uartlist; p != nil; p = p->next) {
567 (*p->phys->power)(p, on);
594 * restart input if it's off
603 (*p->phys->rts)(p, 1);
607 * put some bytes into the local queue to avoid calling
608 * qconsume for every character
611 uartstageoutput(Uart *p)
615 n = qconsume(p->oq, p->ostage, Stagesize);
617 // n = 0; /* experiment */
620 p->oe = p->ostage + n;
639 if(p->drain && uartdrained(p)){
646 * Move data from the interrupt staging area to
650 uartstageinput(Uart *p)
655 while(p->ir != p->iw){
665 if((n = qproduce(p->iq, ir, iw - ir)) < 0){
667 (*p->phys->rts)(p, 0);
675 * receive a character at interrupt time
678 uartrecv(Uart *p, char ch)
682 /* software flow control */
686 }else if(ch == CTLQ){
688 p->ctsbackoff = 2; /* clock gets output going again */
692 /* receive the character */
695 else if (p->iw) { /* maybe the line isn't enabled yet */
711 * we save up input characters till clock time to reduce
712 * per character interrupt overhead.
720 for(p = uartalloc.elist; p; p = p->elist){
722 /* this hopefully amortizes cost of qproduce to many chars */
729 /* hang up if requested */
736 /* this adds hysteresis to hardware/software flow control */
740 if(--(p->ctsbackoff) == 0)
745 uartkick(p); /* keep it moving */
751 * polling console input, output
759 if(consuart == nil || consuart->phys->getc == nil)
761 return consuart->phys->getc(consuart);
767 if(consuart == nil || consuart->phys->putc == nil)
769 consuart->phys->putc(consuart, c);
773 uartputs(char *s, int n)
777 if(consuart == nil || consuart->phys->putc == nil)
783 consuart->phys->putc(consuart, '\r');
784 consuart->phys->putc(consuart, *s);