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, Qcoalesce, 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){
134 if(p && p->opens && p->iq)
135 uartdir[1+3*i].length = qlen(p->iq);
136 } else for(i = 0; i < uartnuart; i++){
138 if(p && p->opens && p->iq)
139 uartdir[1+3*i].length = qlen(p->iq);
144 * set up the '#t' directory
154 for(i = 0; physuart[i] != nil; i++){
155 if(physuart[i]->pnp == nil)
157 if((p = physuart[i]->pnp()) == nil)
163 for(tail = p; tail->next != nil; tail = tail->next)
169 uart = xalloc(uartnuart*sizeof(Uart*));
171 uartndir = 1 + 3*uartnuart;
172 uartdir = xalloc(uartndir * sizeof(Dirtab));
173 if (uart == nil || uartdir == nil)
174 panic("uartreset: no memory");
176 strcpy(dp->name, ".");
177 mkqid(&dp->qid, 0, 0, QTDIR);
179 dp->perm = DMDIR|0555;
182 for(i = 0; i < uartnuart; i++){
183 /* 3 directory entries per port */
184 snprint(dp->name, sizeof dp->name, "eia%d", i);
185 dp->qid.path = NETQID(i, Ndataqid);
188 snprint(dp->name, sizeof dp->name, "eia%dctl", i);
189 dp->qid.path = NETQID(i, Nctlqid);
192 snprint(dp->name, sizeof dp->name, "eia%dstatus", i);
193 dp->qid.path = NETQID(i, Nstatqid);
199 if(p->console || p->special){
200 if(uartenable(p) != nil){
211 * at 115200 baud, the 1024 char buffer takes 56 ms to process,
212 * processing it every 22 ms should be fine.
214 uarttimer = addclock0link(uartclock, 22);
220 uartattach(char *spec)
222 return devattach('t', spec);
226 uartwalk(Chan *c, Chan *nc, char **name, int nname)
228 return devwalk(c, nc, name, nname, uartdir, uartndir, devgen);
232 uartstat(Chan *c, uchar *dp, int n)
234 if(NETTYPE(c->qid.path) == Ndataqid)
235 setlength(NETID(c->qid.path));
236 return devstat(c, dp, n, uartdir, uartndir, devgen);
240 uartopen(Chan *c, int omode)
244 c = devopen(c, omode, uartdir, uartndir, devgen);
246 switch(NETTYPE(c->qid.path)){
249 p = uart[NETID(c->qid.path)];
251 if(p->opens++ == 0 && uartenable(p) == nil){
260 c->iounit = qiomaxatomic;
265 uartdrained(void* arg)
270 return qlen(p->oq) == 0 && p->op == p->oe;
274 uartdrainoutput(Uart *p)
276 if(!p->enabled || up == nil)
284 sleep(&p->r, uartdrained, p);
293 if(c->qid.type & QTDIR)
295 if((c->flag & COPEN) == 0)
297 switch(NETTYPE(c->qid.path)){
300 p = uart[NETID(c->qid.path)];
302 if(--(p->opens) == 0){
305 p->ir = p->iw = p->istage;
317 p->dcd = p->dsr = p->dohup = 0;
325 uartread(Chan *c, void *buf, long n, vlong off)
330 if(c->qid.type & QTDIR){
332 return devdirread(c, buf, n, uartdir, uartndir, devgen);
335 p = uart[NETID(c->qid.path)];
336 switch(NETTYPE(c->qid.path)){
338 return qread(p->iq, buf, n);
340 return readnum(offset, buf, n, NETID(c->qid.path), NUMSIZE);
342 return (*p->phys->status)(p, buf, n, offset);
349 uartctl(Uart *p, char *cmd)
354 nf = tokenize(cmd, f, nelem(f));
355 for(i = 0; i < nf; i++){
356 if(strncmp(f[i], "break", 5) == 0){
357 (*p->phys->dobreak)(p, 0);
366 if((*p->phys->baud)(p, n) < 0)
376 (*p->phys->dtr)(p, n);
397 (*p->phys->fifo)(p, n);
402 (*p->phys->dobreak)(p, n);
407 if((*p->phys->bits)(p, n) < 0)
413 (*p->phys->modemctl)(p, n);
423 if((*p->phys->parity)(p, *(f[i]+1)) < 0)
436 (*p->phys->rts)(p, n);
441 if((*p->phys->stop)(p, n) < 0)
446 if(uarttimer == nil || n < 1)
448 uarttimer->tns = (vlong)n * 100000LL;
464 uartwrite(Chan *c, void *buf, long n, vlong)
469 if(c->qid.type & QTDIR)
472 p = uart[NETID(c->qid.path)];
474 switch(NETTYPE(c->qid.path)){
482 n = qwrite(p->oq, buf, n);
489 memmove(cmd, buf, n);
498 /* let output drain */
499 if(uartctl(p, cmd) < 0)
512 uartwstat(Chan *c, uchar *dp, int n)
519 if(QTDIR & c->qid.type)
521 if(NETTYPE(c->qid.path) == Nstatqid)
524 dt = &uartdir[1 + 3 * NETID(c->qid.path)];
525 n = convM2D(dp, n, &d, nil);
529 dt[0].perm = dt[1].perm = d.mode;
538 for(p = uartlist; p != nil; p = p->next) {
540 (*p->phys->power)(p, on);
567 * restart input if it's off
576 (*p->phys->rts)(p, 1);
580 * put some bytes into the local queue to avoid calling
581 * qconsume for every character
584 uartstageoutput(Uart *p)
588 n = qconsume(p->oq, p->ostage, Stagesize);
590 // n = 0; /* experiment */
593 p->oe = p->ostage + n;
612 if(p->drain && uartdrained(p)){
619 * Move data from the interrupt staging area to
623 uartstageinput(Uart *p)
628 while(p->ir != p->iw){
638 if((n = qproduce(p->iq, ir, iw - ir)) < 0){
640 (*p->phys->rts)(p, 0);
648 * receive a character at interrupt time
651 uartrecv(Uart *p, char ch)
655 /* software flow control */
659 }else if(ch == CTLQ){
661 p->ctsbackoff = 2; /* clock gets output going again */
665 /* receive the character */
668 else if (p->iw) { /* maybe the line isn't enabled yet */
684 * we save up input characters till clock time to reduce
685 * per character interrupt overhead.
693 for(p = uartalloc.elist; p; p = p->elist){
695 /* this hopefully amortizes cost of qproduce to many chars */
702 /* hang up if requested */
709 /* this adds hysteresis to hardware/software flow control */
713 if(--(p->ctsbackoff) == 0)
718 uartkick(p); /* keep it moving */
724 * polling console input, output
732 if(consuart == nil || consuart->phys->getc == nil)
734 return consuart->phys->getc(consuart);
740 if(consuart == nil || consuart->phys->putc == nil)
742 consuart->phys->putc(consuart, c);
746 uartputs(char *s, int n)
750 if(consuart == nil || consuart->phys->putc == nil)
756 consuart->phys->putc(consuart, '\r');
757 consuart->phys->putc(consuart, *s);