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){
228 if(p->console && up){
241 * at 115200 baud, the 1024 char buffer takes 56 ms to process,
242 * processing it every 22 ms should be fine.
244 uarttimer = addclock0link(uartclock, 22);
250 uartattach(char *spec)
252 return devattach('t', spec);
256 uartwalk(Chan *c, Chan *nc, char **name, int nname)
258 return devwalk(c, nc, name, nname, uartdir, uartndir, devgen);
262 uartstat(Chan *c, uchar *dp, int n)
264 if(NETTYPE(c->qid.path) == Ndataqid)
265 setlength(NETID(c->qid.path));
266 return devstat(c, dp, n, uartdir, uartndir, devgen);
270 uartopen(Chan *c, int omode)
274 c = devopen(c, omode, uartdir, uartndir, devgen);
276 switch(NETTYPE(c->qid.path)){
279 p = uart[NETID(c->qid.path)];
281 if(p->opens++ == 0 && uartenable(p) == nil){
290 c->iounit = qiomaxatomic;
295 uartdrained(void* arg)
300 return qlen(p->oq) == 0 && p->op == p->oe;
304 uartdrainoutput(Uart *p)
306 if(!p->enabled || up == nil)
314 sleep(&p->r, uartdrained, p);
323 if(c->qid.type & QTDIR)
325 if((c->flag & COPEN) == 0)
327 switch(NETTYPE(c->qid.path)){
330 p = uart[NETID(c->qid.path)];
332 if(--(p->opens) == 0){
335 p->ir = p->iw = p->istage;
347 p->dcd = p->dsr = p->dohup = 0;
355 uartread(Chan *c, void *buf, long n, vlong off)
360 if(c->qid.type & QTDIR){
362 return devdirread(c, buf, n, uartdir, uartndir, devgen);
365 p = uart[NETID(c->qid.path)];
366 switch(NETTYPE(c->qid.path)){
368 return qread(p->iq, buf, n);
370 return readnum(offset, buf, n, NETID(c->qid.path), NUMSIZE);
372 return (*p->phys->status)(p, buf, n, offset);
379 uartctl(Uart *p, char *cmd)
384 nf = tokenize(cmd, f, nelem(f));
385 for(i = 0; i < nf; i++){
386 if(strncmp(f[i], "break", 5) == 0){
387 (*p->phys->dobreak)(p, 0);
396 if((*p->phys->baud)(p, n) < 0)
406 (*p->phys->dtr)(p, n);
427 (*p->phys->fifo)(p, n);
432 (*p->phys->dobreak)(p, n);
437 if((*p->phys->bits)(p, n) < 0)
443 (*p->phys->modemctl)(p, n);
453 if((*p->phys->parity)(p, *(f[i]+1)) < 0)
466 (*p->phys->rts)(p, n);
471 if((*p->phys->stop)(p, n) < 0)
476 if(uarttimer == nil || n < 1)
478 uarttimer->tns = (vlong)n * 100000LL;
494 uartwrite(Chan *c, void *buf, long n, vlong)
499 if(c->qid.type & QTDIR)
502 p = uart[NETID(c->qid.path)];
504 switch(NETTYPE(c->qid.path)){
512 n = qwrite(p->oq, buf, n);
519 memmove(cmd, buf, n);
528 /* let output drain */
529 if(uartctl(p, cmd) < 0)
542 uartwstat(Chan *c, uchar *dp, int n)
549 if(QTDIR & c->qid.type)
551 if(NETTYPE(c->qid.path) == Nstatqid)
554 dt = &uartdir[1 + 3 * NETID(c->qid.path)];
555 n = convM2D(dp, n, &d, nil);
559 dt[0].perm = dt[1].perm = d.mode;
568 for(p = uartlist; p != nil; p = p->next) {
570 (*p->phys->power)(p, on);
597 * restart input if it's off
606 (*p->phys->rts)(p, 1);
610 * put some bytes into the local queue to avoid calling
611 * qconsume for every character
614 uartstageoutput(Uart *p)
618 n = qconsume(p->oq, p->ostage, Stagesize);
620 // n = 0; /* experiment */
623 p->oe = p->ostage + n;
642 if(p->drain && uartdrained(p)){
649 * Move data from the interrupt staging area to
653 uartstageinput(Uart *p)
658 while(p->ir != p->iw){
668 if((n = qproduce(p->iq, ir, iw - ir)) < 0){
670 (*p->phys->rts)(p, 0);
678 * receive a character at interrupt time
681 uartrecv(Uart *p, char ch)
685 /* software flow control */
689 }else if(ch == CTLQ){
691 p->ctsbackoff = 2; /* clock gets output going again */
695 /* receive the character */
698 else if (p->iw) { /* maybe the line isn't enabled yet */
714 * we save up input characters till clock time to reduce
715 * per character interrupt overhead.
723 for(p = uartalloc.elist; p; p = p->elist){
725 /* this hopefully amortizes cost of qproduce to many chars */
732 /* hang up if requested */
739 /* this adds hysteresis to hardware/software flow control */
743 if(--(p->ctsbackoff) == 0)
748 uartkick(p); /* keep it moving */
754 * polling console input, output
762 if(consuart == nil || consuart->phys->getc == nil)
764 return consuart->phys->getc(consuart);
770 if(consuart == nil || consuart->phys->putc == nil)
772 consuart->phys->putc(consuart, c);
776 uartputs(char *s, int n)
780 if(consuart == nil || consuart->phys->putc == nil)
786 consuart->phys->putc(consuart, '\r');
787 consuart->phys->putc(consuart, *s);