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 uartmouse(Uart* p, int (*putc)(Queue*, int), int setb1200)
128 if(p->opens++ == 0 && uartenable(p) == nil){
140 uartsetmouseputc(Uart* p, int (*putc)(Queue*, int))
143 if(p->opens == 0 || p->special == 0){
158 if(p && p->opens && p->iq)
159 uartdir[1+3*i].length = qlen(p->iq);
160 } else for(i = 0; i < uartnuart; i++){
162 if(p && p->opens && p->iq)
163 uartdir[1+3*i].length = qlen(p->iq);
168 * set up the '#t' directory
178 for(i = 0; physuart[i] != nil; i++){
179 if(physuart[i]->pnp == nil)
181 if((p = physuart[i]->pnp()) == nil)
187 for(tail = p; tail->next != nil; tail = tail->next)
193 uart = xalloc(uartnuart*sizeof(Uart*));
195 uartndir = 1 + 3*uartnuart;
196 uartdir = xalloc(uartndir * sizeof(Dirtab));
197 if(uartnuart && uart == nil || uartdir == nil)
198 panic("uartreset: no memory");
200 strcpy(dp->name, ".");
201 mkqid(&dp->qid, 0, 0, QTDIR);
203 dp->perm = DMDIR|0555;
206 for(i = 0; i < uartnuart; i++){
207 /* 3 directory entries per port */
208 snprint(dp->name, sizeof dp->name, "eia%d", i);
209 dp->qid.path = NETQID(i, Ndataqid);
212 snprint(dp->name, sizeof dp->name, "eia%dctl", i);
213 dp->qid.path = NETQID(i, Nctlqid);
216 snprint(dp->name, sizeof dp->name, "eia%dstatus", i);
217 dp->qid.path = NETQID(i, Nstatqid);
223 if(p->console || p->special){
224 if(uartenable(p) != nil){
236 * at 115200 baud, the 1024 char buffer takes 56 ms to process,
237 * processing it every 22 ms should be fine.
239 uarttimer = addclock0link(uartclock, 22);
245 uartattach(char *spec)
247 return devattach('t', spec);
251 uartwalk(Chan *c, Chan *nc, char **name, int nname)
253 return devwalk(c, nc, name, nname, uartdir, uartndir, devgen);
257 uartstat(Chan *c, uchar *dp, int n)
259 if(NETTYPE(c->qid.path) == Ndataqid)
260 setlength(NETID(c->qid.path));
261 return devstat(c, dp, n, uartdir, uartndir, devgen);
265 uartopen(Chan *c, int omode)
269 c = devopen(c, omode, uartdir, uartndir, devgen);
271 switch(NETTYPE(c->qid.path)){
274 p = uart[NETID(c->qid.path)];
276 if(p->opens++ == 0 && uartenable(p) == nil){
285 c->iounit = qiomaxatomic;
290 uartdrained(void* arg)
295 return qlen(p->oq) == 0 && p->op == p->oe;
299 uartdrainoutput(Uart *p)
309 sleep(&p->r, uartdrained, p);
318 if(c->qid.type & QTDIR)
320 if((c->flag & COPEN) == 0)
322 switch(NETTYPE(c->qid.path)){
325 p = uart[NETID(c->qid.path)];
327 if(--(p->opens) == 0){
330 p->ir = p->iw = p->istage;
342 p->dcd = p->dsr = p->dohup = 0;
350 uartread(Chan *c, void *buf, long n, vlong off)
355 if(c->qid.type & QTDIR){
357 return devdirread(c, buf, n, uartdir, uartndir, devgen);
360 p = uart[NETID(c->qid.path)];
361 switch(NETTYPE(c->qid.path)){
363 return qread(p->iq, buf, n);
365 return readnum(offset, buf, n, NETID(c->qid.path), NUMSIZE);
367 return (*p->phys->status)(p, buf, n, offset);
374 uartctl(Uart *p, char *cmd)
379 nf = tokenize(cmd, f, nelem(f));
380 for(i = 0; i < nf; i++){
381 if(strncmp(f[i], "break", 5) == 0){
382 (*p->phys->dobreak)(p, 0);
391 if((*p->phys->baud)(p, n) < 0)
401 (*p->phys->dtr)(p, n);
422 (*p->phys->fifo)(p, n);
427 (*p->phys->dobreak)(p, n);
432 if((*p->phys->bits)(p, n) < 0)
438 (*p->phys->modemctl)(p, n);
448 if((*p->phys->parity)(p, *(f[i]+1)) < 0)
461 (*p->phys->rts)(p, n);
466 if((*p->phys->stop)(p, n) < 0)
471 if(uarttimer == nil || n < 1)
473 uarttimer->tns = (vlong)n * 100000LL;
488 uartwrite(Chan *c, void *buf, long n, vlong)
493 if(c->qid.type & QTDIR)
496 p = uart[NETID(c->qid.path)];
498 switch(NETTYPE(c->qid.path)){
506 n = qwrite(p->oq, buf, n);
513 memmove(cmd, buf, n);
522 /* let output drain */
523 if(uartctl(p, cmd) < 0)
536 uartwstat(Chan *c, uchar *dp, int n)
543 if(QTDIR & c->qid.type)
545 if(NETTYPE(c->qid.path) == Nstatqid)
548 dt = &uartdir[1 + 3 * NETID(c->qid.path)];
549 n = convM2D(dp, n, &d, nil);
553 dt[0].perm = dt[1].perm = d.mode;
562 for(p = uartlist; p != nil; p = p->next) {
564 (*p->phys->power)(p, on);
591 * restart input if it's off
600 (*p->phys->rts)(p, 1);
604 * put some bytes into the local queue to avoid calling
605 * qconsume for every character
608 uartstageoutput(Uart *p)
612 n = qconsume(p->oq, p->ostage, Stagesize);
616 p->oe = p->ostage + n;
635 if(p->drain && uartdrained(p)){
642 * Move data from the interrupt staging area to
646 uartstageinput(Uart *p)
651 while(p->ir != p->iw){
661 if((n = qproduce(p->iq, ir, iw - ir)) < 0){
663 (*p->phys->rts)(p, 0);
671 * receive a character at interrupt time
674 uartrecv(Uart *p, char ch)
678 /* software flow control */
682 }else if(ch == CTLQ){
684 p->ctsbackoff = 2; /* clock gets output going again */
688 /* receive the character */
691 else if (p->iw) { /* maybe the line isn't enabled yet */
707 * we save up input characters till clock time to reduce
708 * per character interrupt overhead.
716 for(p = uartalloc.elist; p; p = p->elist){
718 /* this hopefully amortizes cost of qproduce to many chars */
725 /* hang up if requested */
732 /* this adds hysteresis to hardware/software flow control */
736 if(--(p->ctsbackoff) == 0)
746 * polling console input, output
754 if(consuart == nil || consuart->phys->getc == nil)
756 return consuart->phys->getc(consuart);
762 if(consuart == nil || consuart->phys->putc == nil)
764 consuart->phys->putc(consuart, c);
768 uartputs(char *s, int n)
772 if(consuart == nil || consuart->phys->putc == nil)
778 consuart->phys->putc(consuart, '\r');
779 consuart->phys->putc(consuart, *s);