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
47 if((p->iq = qopen(8*1024, Qcoalesce, uartflow, p)) == nil)
53 if((p->oq = qopen(8*1024, 0, uartkick, p)) == nil){
64 p->ie = &p->istage[Stagesize];
68 p->hup_dsr = p->hup_dcd = 0;
71 /* assume we can send */
83 (*p->phys->enable)(p, 1);
86 * use ilock because uartclock can otherwise interrupt here
87 * and would hang on an attempt to lock uartalloc.
90 for(l = &uartalloc.elist; *l; l = &(*l)->elist){
95 p->elist = uartalloc.elist;
111 (*p->phys->disable)(p);
114 for(l = &uartalloc.elist; *l; l = &(*l)->elist){
125 uartport(char *which)
130 port = strtol(which, &p, 0);
133 if(port < 0 || port >= uartnuart || uart[port] == nil)
139 uartmouse(char *which, int (*putc)(Queue*, int), int setb1200)
145 if(p->opens++ == 0 && uartenable(p) == nil){
157 uartsetmouseputc(char *which, int (*putc)(Queue*, int))
163 if(p->opens == 0 || p->special == 0){
178 if(p && p->opens && p->iq)
179 uartdir[1+3*i].length = qlen(p->iq);
180 } else for(i = 0; i < uartnuart; i++){
182 if(p && p->opens && p->iq)
183 uartdir[1+3*i].length = qlen(p->iq);
188 * set up the '#t' directory
198 for(i = 0; physuart[i] != nil; i++){
199 if(physuart[i]->pnp == nil)
201 if((p = physuart[i]->pnp()) == nil)
207 for(tail = p; tail->next != nil; tail = tail->next)
213 uart = xalloc(uartnuart*sizeof(Uart*));
215 uartndir = 1 + 3*uartnuart;
216 uartdir = xalloc(uartndir * sizeof(Dirtab));
217 if(uartnuart && uart == nil || uartdir == nil)
218 panic("uartreset: no memory");
220 strcpy(dp->name, ".");
221 mkqid(&dp->qid, 0, 0, QTDIR);
223 dp->perm = DMDIR|0555;
226 for(i = 0; i < uartnuart; i++){
227 /* 3 directory entries per port */
228 snprint(dp->name, sizeof dp->name, "eia%d", i);
229 dp->qid.path = NETQID(i, Ndataqid);
232 snprint(dp->name, sizeof dp->name, "eia%dctl", i);
233 dp->qid.path = NETQID(i, Nctlqid);
236 snprint(dp->name, sizeof dp->name, "eia%dstatus", i);
237 dp->qid.path = NETQID(i, Nstatqid);
243 if(p->console || p->special){
244 if(uartenable(p) != nil){
256 * at 115200 baud, the 1024 char buffer takes 56 ms to process,
257 * processing it every 22 ms should be fine.
259 uarttimer = addclock0link(uartclock, 22);
265 uartattach(char *spec)
267 return devattach('t', spec);
271 uartwalk(Chan *c, Chan *nc, char **name, int nname)
273 return devwalk(c, nc, name, nname, uartdir, uartndir, devgen);
277 uartstat(Chan *c, uchar *dp, int n)
279 if(NETTYPE(c->qid.path) == Ndataqid)
280 setlength(NETID(c->qid.path));
281 return devstat(c, dp, n, uartdir, uartndir, devgen);
285 uartopen(Chan *c, int omode)
289 c = devopen(c, omode, uartdir, uartndir, devgen);
291 switch(NETTYPE(c->qid.path)){
294 p = uart[NETID(c->qid.path)];
296 if(p->opens++ == 0 && uartenable(p) == nil){
305 c->iounit = qiomaxatomic;
310 uartdrained(void* arg)
315 return qlen(p->oq) == 0 && p->op == p->oe;
319 uartdrainoutput(Uart *p)
329 sleep(&p->r, uartdrained, p);
338 if(c->qid.type & QTDIR)
340 if((c->flag & COPEN) == 0)
342 switch(NETTYPE(c->qid.path)){
345 p = uart[NETID(c->qid.path)];
347 if(--(p->opens) == 0){
350 p->ir = p->iw = p->istage;
362 p->dcd = p->dsr = p->dohup = 0;
370 uartread(Chan *c, void *buf, long n, vlong off)
375 if(c->qid.type & QTDIR){
377 return devdirread(c, buf, n, uartdir, uartndir, devgen);
380 p = uart[NETID(c->qid.path)];
381 switch(NETTYPE(c->qid.path)){
383 return qread(p->iq, buf, n);
385 return readnum(offset, buf, n, NETID(c->qid.path), NUMSIZE);
387 return (*p->phys->status)(p, buf, n, offset);
394 uartctl(Uart *p, char *cmd)
399 nf = tokenize(cmd, f, nelem(f));
400 for(i = 0; i < nf; i++){
401 if(strncmp(f[i], "break", 5) == 0){
402 (*p->phys->dobreak)(p, 0);
411 if((*p->phys->baud)(p, n) < 0)
421 (*p->phys->dtr)(p, n);
442 (*p->phys->fifo)(p, n);
447 (*p->phys->dobreak)(p, n);
452 if((*p->phys->bits)(p, n) < 0)
458 (*p->phys->modemctl)(p, n);
468 if((*p->phys->parity)(p, *(f[i]+1)) < 0)
481 (*p->phys->rts)(p, n);
486 if((*p->phys->stop)(p, n) < 0)
491 if(uarttimer == nil || n < 1)
493 uarttimer->tns = (vlong)n * 100000LL;
508 uartwrite(Chan *c, void *buf, long n, vlong)
513 if(c->qid.type & QTDIR)
516 p = uart[NETID(c->qid.path)];
518 switch(NETTYPE(c->qid.path)){
526 n = qwrite(p->oq, buf, n);
533 memmove(cmd, buf, n);
542 /* let output drain */
543 if(uartctl(p, cmd) < 0)
556 uartwstat(Chan *c, uchar *dp, int n)
563 if(QTDIR & c->qid.type)
565 if(NETTYPE(c->qid.path) == Nstatqid)
568 dt = &uartdir[1 + 3 * NETID(c->qid.path)];
569 n = convM2D(dp, n, &d, nil);
573 dt[0].perm = dt[1].perm = d.mode;
582 for(p = uartlist; p != nil; p = p->next) {
584 (*p->phys->power)(p, on);
611 * restart input if it's off
620 (*p->phys->rts)(p, 1);
624 * put some bytes into the local queue to avoid calling
625 * qconsume for every character
628 uartstageoutput(Uart *p)
632 n = qconsume(p->oq, p->ostage, Stagesize);
636 p->oe = p->ostage + n;
655 if(p->drain && uartdrained(p)){
662 * Move data from the interrupt staging area to
666 uartstageinput(Uart *p)
671 while(p->ir != p->iw){
681 if((n = qproduce(p->iq, ir, iw - ir)) < 0){
683 (*p->phys->rts)(p, 0);
691 * receive a character at interrupt time
694 uartrecv(Uart *p, char ch)
698 /* software flow control */
702 }else if(ch == CTLQ){
704 p->ctsbackoff = 2; /* clock gets output going again */
708 /* receive the character */
711 else if (p->iw) { /* maybe the line isn't enabled yet */
727 * we save up input characters till clock time to reduce
728 * per character interrupt overhead.
736 for(p = uartalloc.elist; p; p = p->elist){
738 /* this hopefully amortizes cost of qproduce to many chars */
745 /* hang up if requested */
752 /* this adds hysteresis to hardware/software flow control */
756 if(--(p->ctsbackoff) == 0)
766 * polling console input, output
774 if(consuart == nil || consuart->phys->getc == nil)
776 return consuart->phys->getc(consuart);
782 if(consuart == nil || consuart->phys->putc == nil)
784 consuart->phys->putc(consuart, c);
788 uartputs(char *s, int n)
792 if(consuart == nil || consuart->phys->putc == nil)
798 consuart->phys->putc(consuart, '\r');
799 consuart->phys->putc(consuart, *s);