devuart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "../port/error.h"
  8. #include "../port/netif.h"
  9. enum
  10. {
  11. /* soft flow control chars */
  12. CTLS= 023,
  13. CTLQ= 021,
  14. };
  15. extern Dev uartdevtab;
  16. extern PhysUart* physuart[];
  17. static Uart* uartlist;
  18. static Uart** uart;
  19. static int uartnuart;
  20. static Dirtab *uartdir;
  21. static int uartndir;
  22. static Timer *uarttimer;
  23. struct Uartalloc {
  24. Lock;
  25. Uart *elist; /* list of enabled interfaces */
  26. } uartalloc;
  27. static void uartclock(void);
  28. static void uartflow(void*);
  29. /*
  30. * enable/disable uart and add/remove to list of enabled uarts
  31. */
  32. static Uart*
  33. uartenable(Uart *p)
  34. {
  35. Uart **l;
  36. if(p->iq == nil){
  37. if((p->iq = qopen(8*1024, 0, uartflow, p)) == nil)
  38. return nil;
  39. }
  40. else
  41. qreopen(p->iq);
  42. if(p->oq == nil){
  43. if((p->oq = qopen(8*1024, 0, uartkick, p)) == nil){
  44. qfree(p->iq);
  45. p->iq = nil;
  46. return nil;
  47. }
  48. }
  49. else
  50. qreopen(p->oq);
  51. p->ir = p->istage;
  52. p->iw = p->istage;
  53. p->ie = &p->istage[Stagesize];
  54. p->op = p->ostage;
  55. p->oe = p->ostage;
  56. p->hup_dsr = p->hup_dcd = 0;
  57. p->dsr = p->dcd = 0;
  58. /* assume we can send */
  59. p->cts = 1;
  60. p->ctsbackoff = 0;
  61. if(p->bits == 0)
  62. uartctl(p, "l8");
  63. if(p->stop == 0)
  64. uartctl(p, "s1");
  65. if(p->parity == 0)
  66. uartctl(p, "pn");
  67. if(p->baud == 0)
  68. uartctl(p, "b9600");
  69. (*p->phys->enable)(p, 1);
  70. lock(&uartalloc);
  71. for(l = &uartalloc.elist; *l; l = &(*l)->elist){
  72. if(*l == p)
  73. break;
  74. }
  75. if(*l == 0){
  76. p->elist = uartalloc.elist;
  77. uartalloc.elist = p;
  78. }
  79. p->enabled = 1;
  80. unlock(&uartalloc);
  81. return p;
  82. }
  83. static void
  84. uartdisable(Uart *p)
  85. {
  86. Uart **l;
  87. (*p->phys->disable)(p);
  88. lock(&uartalloc);
  89. for(l = &uartalloc.elist; *l; l = &(*l)->elist){
  90. if(*l == p){
  91. *l = p->elist;
  92. break;
  93. }
  94. }
  95. p->enabled = 0;
  96. unlock(&uartalloc);
  97. }
  98. void
  99. uartmouse(Uart* p, int (*putc)(Queue*, int), int setb1200)
  100. {
  101. qlock(p);
  102. if(p->opens++ == 0 && uartenable(p) == nil){
  103. qunlock(p);
  104. error(Enodev);
  105. }
  106. if(setb1200)
  107. uartctl(p, "b1200");
  108. p->putc = putc;
  109. p->special = 1;
  110. qunlock(p);
  111. }
  112. void
  113. uartsetmouseputc(Uart* p, int (*putc)(Queue*, int))
  114. {
  115. qlock(p);
  116. if(p->opens == 0 || p->special == 0){
  117. qunlock(p);
  118. error(Enodev);
  119. }
  120. p->putc = putc;
  121. qunlock(p);
  122. }
  123. static void
  124. setlength(int i)
  125. {
  126. Uart *p;
  127. if(i > 0){
  128. p = uart[i];
  129. if(p && p->opens && p->iq)
  130. uartdir[1+3*i].length = qlen(p->iq);
  131. } else for(i = 0; i < uartnuart; i++){
  132. p = uart[i];
  133. if(p && p->opens && p->iq)
  134. uartdir[1+3*i].length = qlen(p->iq);
  135. }
  136. }
  137. /*
  138. * set up the '#t' directory
  139. */
  140. static void
  141. uartreset(void)
  142. {
  143. int i;
  144. Dirtab *dp;
  145. Uart *p, *tail;
  146. tail = nil;
  147. for(i = 0; physuart[i] != nil; i++){
  148. if(physuart[i]->pnp == nil)
  149. continue;
  150. if((p = physuart[i]->pnp()) == nil)
  151. continue;
  152. if(uartlist != nil)
  153. tail->next = p;
  154. else
  155. uartlist = p;
  156. for(tail = p; tail->next != nil; tail = tail->next)
  157. uartnuart++;
  158. uartnuart++;
  159. }
  160. if(uartnuart)
  161. uart = xalloc(uartnuart*sizeof(Uart*));
  162. uartndir = 1 + 3*uartnuart;
  163. uartdir = xalloc(uartndir * sizeof(Dirtab));
  164. dp = uartdir;
  165. strcpy(dp->name, ".");
  166. mkqid(&dp->qid, 0, 0, QTDIR);
  167. dp->length = 0;
  168. dp->perm = DMDIR|0555;
  169. dp++;
  170. p = uartlist;
  171. for(i = 0; i < uartnuart; i++){
  172. /* 3 directory entries per port */
  173. sprint(dp->name, "eia%d", i);
  174. dp->qid.path = NETQID(i, Ndataqid);
  175. dp->perm = 0660;
  176. dp++;
  177. sprint(dp->name, "eia%dctl", i);
  178. dp->qid.path = NETQID(i, Nctlqid);
  179. dp->perm = 0660;
  180. dp++;
  181. sprint(dp->name, "eia%dstatus", i);
  182. dp->qid.path = NETQID(i, Nstatqid);
  183. dp->perm = 0444;
  184. dp++;
  185. uart[i] = p;
  186. p->dev = i;
  187. if(p->console || p->special){
  188. if(uartenable(p) != nil){
  189. if(p->console){
  190. kbdq = p->iq;
  191. serialoq = p->oq;
  192. p->putc = kbdcr2nl;
  193. }
  194. p->opens++;
  195. }
  196. }
  197. p = p->next;
  198. }
  199. if(uartnuart){
  200. /*
  201. * at 115200 baud, the 1024 char buffer takes 56 ms to process,
  202. * processing it every 22 ms should be fine.
  203. */
  204. uarttimer = addclock0link(uartclock, 22);
  205. }
  206. }
  207. static Chan*
  208. uartattach(char *spec)
  209. {
  210. return devattach('t', spec);
  211. }
  212. static Walkqid*
  213. uartwalk(Chan *c, Chan *nc, char **name, int nname)
  214. {
  215. return devwalk(c, nc, name, nname, uartdir, uartndir, devgen);
  216. }
  217. static int
  218. uartstat(Chan *c, uchar *dp, int n)
  219. {
  220. if(NETTYPE(c->qid.path) == Ndataqid)
  221. setlength(NETID(c->qid.path));
  222. return devstat(c, dp, n, uartdir, uartndir, devgen);
  223. }
  224. static Chan*
  225. uartopen(Chan *c, int omode)
  226. {
  227. Uart *p;
  228. c = devopen(c, omode, uartdir, uartndir, devgen);
  229. switch(NETTYPE(c->qid.path)){
  230. case Nctlqid:
  231. case Ndataqid:
  232. p = uart[NETID(c->qid.path)];
  233. qlock(p);
  234. if(p->opens++ == 0 && uartenable(p) == nil){
  235. qunlock(p);
  236. c->flag &= ~COPEN;
  237. error(Enodev);
  238. }
  239. qunlock(p);
  240. break;
  241. }
  242. c->iounit = qiomaxatomic;
  243. return c;
  244. }
  245. static int
  246. uartdrained(void* arg)
  247. {
  248. Uart *p;
  249. p = arg;
  250. return qlen(p->oq) == 0 && p->op == p->oe;
  251. }
  252. static void
  253. uartdrainoutput(Uart *p)
  254. {
  255. if(!p->enabled)
  256. return;
  257. p->drain = 1;
  258. if(waserror()){
  259. p->drain = 0;
  260. nexterror();
  261. }
  262. sleep(&p->r, uartdrained, p);
  263. poperror();
  264. }
  265. static void
  266. uartclose(Chan *c)
  267. {
  268. Uart *p;
  269. if(c->qid.type & QTDIR)
  270. return;
  271. if((c->flag & COPEN) == 0)
  272. return;
  273. switch(NETTYPE(c->qid.path)){
  274. case Ndataqid:
  275. case Nctlqid:
  276. p = uart[NETID(c->qid.path)];
  277. qlock(p);
  278. if(--(p->opens) == 0){
  279. qclose(p->iq);
  280. ilock(&p->rlock);
  281. p->ir = p->iw = p->istage;
  282. iunlock(&p->rlock);
  283. /*
  284. */
  285. qhangup(p->oq, nil);
  286. if(!waserror()){
  287. uartdrainoutput(p);
  288. poperror();
  289. }
  290. qclose(p->oq);
  291. uartdisable(p);
  292. p->dcd = p->dsr = p->dohup = 0;
  293. }
  294. qunlock(p);
  295. break;
  296. }
  297. }
  298. static long
  299. uartread(Chan *c, void *buf, long n, vlong off)
  300. {
  301. Uart *p;
  302. ulong offset = off;
  303. if(c->qid.type & QTDIR){
  304. setlength(-1);
  305. return devdirread(c, buf, n, uartdir, uartndir, devgen);
  306. }
  307. p = uart[NETID(c->qid.path)];
  308. switch(NETTYPE(c->qid.path)){
  309. case Ndataqid:
  310. return qread(p->iq, buf, n);
  311. case Nctlqid:
  312. return readnum(offset, buf, n, NETID(c->qid.path), NUMSIZE);
  313. case Nstatqid:
  314. return (*p->phys->status)(p, buf, n, offset);
  315. }
  316. return 0;
  317. }
  318. int
  319. uartctl(Uart *p, char *cmd)
  320. {
  321. char *f[16];
  322. int i, n, nf;
  323. nf = tokenize(cmd, f, nelem(f));
  324. for(i = 0; i < nf; i++){
  325. if(strncmp(f[i], "break", 5) == 0){
  326. (*p->phys->dobreak)(p, 0);
  327. continue;
  328. }
  329. n = atoi(f[i]+1);
  330. switch(*f[i]){
  331. case 'B':
  332. case 'b':
  333. uartdrainoutput(p);
  334. if((*p->phys->baud)(p, n) < 0)
  335. return -1;
  336. break;
  337. case 'C':
  338. case 'c':
  339. p->hup_dcd = n;
  340. break;
  341. case 'D':
  342. case 'd':
  343. uartdrainoutput(p);
  344. (*p->phys->dtr)(p, n);
  345. break;
  346. case 'E':
  347. case 'e':
  348. p->hup_dsr = n;
  349. break;
  350. case 'f':
  351. case 'F':
  352. if(p->oq != nil)
  353. qflush(p->oq);
  354. break;
  355. case 'H':
  356. case 'h':
  357. if(p->iq != nil)
  358. qhangup(p->iq, 0);
  359. if(p->oq != nil)
  360. qhangup(p->oq, 0);
  361. break;
  362. case 'i':
  363. case 'I':
  364. uartdrainoutput(p);
  365. (*p->phys->fifo)(p, n);
  366. break;
  367. case 'K':
  368. case 'k':
  369. uartdrainoutput(p);
  370. (*p->phys->dobreak)(p, n);
  371. break;
  372. case 'L':
  373. case 'l':
  374. uartdrainoutput(p);
  375. if((*p->phys->bits)(p, n) < 0)
  376. return -1;
  377. break;
  378. case 'm':
  379. case 'M':
  380. uartdrainoutput(p);
  381. (*p->phys->modemctl)(p, n);
  382. break;
  383. case 'n':
  384. case 'N':
  385. if(p->oq != nil)
  386. qnoblock(p->oq, n);
  387. break;
  388. case 'P':
  389. case 'p':
  390. uartdrainoutput(p);
  391. if((*p->phys->parity)(p, *(f[i]+1)) < 0)
  392. return -1;
  393. break;
  394. case 'Q':
  395. case 'q':
  396. if(p->iq != nil)
  397. qsetlimit(p->iq, n);
  398. if(p->oq != nil)
  399. qsetlimit(p->oq, n);
  400. break;
  401. case 'R':
  402. case 'r':
  403. uartdrainoutput(p);
  404. (*p->phys->rts)(p, n);
  405. break;
  406. case 'S':
  407. case 's':
  408. uartdrainoutput(p);
  409. if((*p->phys->stop)(p, n) < 0)
  410. return -1;
  411. break;
  412. case 'W':
  413. case 'w':
  414. if(uarttimer == nil || n < 1)
  415. return -1;
  416. uarttimer->tns = (vlong)n * 100000LL;
  417. break;
  418. case 'X':
  419. case 'x':
  420. if(p->enabled){
  421. ilock(&p->tlock);
  422. p->xonoff = n;
  423. iunlock(&p->tlock);
  424. }
  425. break;
  426. }
  427. }
  428. return 0;
  429. }
  430. static long
  431. uartwrite(Chan *c, void *buf, long n, vlong)
  432. {
  433. Uart *p;
  434. char *cmd;
  435. if(c->qid.type & QTDIR)
  436. error(Eperm);
  437. p = uart[NETID(c->qid.path)];
  438. switch(NETTYPE(c->qid.path)){
  439. case Ndataqid:
  440. qlock(p);
  441. if(waserror()){
  442. qunlock(p);
  443. nexterror();
  444. }
  445. n = qwrite(p->oq, buf, n);
  446. qunlock(p);
  447. poperror();
  448. break;
  449. case Nctlqid:
  450. cmd = malloc(n+1);
  451. memmove(cmd, buf, n);
  452. cmd[n] = 0;
  453. qlock(p);
  454. if(waserror()){
  455. qunlock(p);
  456. free(cmd);
  457. nexterror();
  458. }
  459. /* let output drain */
  460. if(uartctl(p, cmd) < 0)
  461. error(Ebadarg);
  462. qunlock(p);
  463. poperror();
  464. free(cmd);
  465. break;
  466. }
  467. return n;
  468. }
  469. static int
  470. uartwstat(Chan *c, uchar *dp, int n)
  471. {
  472. Dir d;
  473. Dirtab *dt;
  474. if(!iseve())
  475. error(Eperm);
  476. if(QTDIR & c->qid.type)
  477. error(Eperm);
  478. if(NETTYPE(c->qid.path) == Nstatqid)
  479. error(Eperm);
  480. dt = &uartdir[1 + 3 * NETID(c->qid.path)];
  481. n = convM2D(dp, n, &d, nil);
  482. if(n == 0)
  483. error(Eshortstat);
  484. if(d.mode != ~0UL)
  485. dt[0].perm = dt[1].perm = d.mode;
  486. return n;
  487. }
  488. void
  489. uartpower(int on)
  490. {
  491. Uart *p;
  492. for(p = uartlist; p != nil; p = p->next) {
  493. if(p->phys->power)
  494. (*p->phys->power)(p, on);
  495. }
  496. }
  497. Dev uartdevtab = {
  498. 't',
  499. "uart",
  500. uartreset,
  501. devinit,
  502. devshutdown,
  503. uartattach,
  504. uartwalk,
  505. uartstat,
  506. uartopen,
  507. devcreate,
  508. uartclose,
  509. uartread,
  510. devbread,
  511. uartwrite,
  512. devbwrite,
  513. devremove,
  514. uartwstat,
  515. uartpower,
  516. };
  517. /*
  518. * restart input if it's off
  519. */
  520. static void
  521. uartflow(void *v)
  522. {
  523. Uart *p;
  524. p = v;
  525. if(p->modem)
  526. (*p->phys->rts)(p, 1);
  527. }
  528. /*
  529. * put some bytes into the local queue to avoid calling
  530. * qconsume for every character
  531. */
  532. int
  533. uartstageoutput(Uart *p)
  534. {
  535. int n;
  536. n = qconsume(p->oq, p->ostage, Stagesize);
  537. if(n <= 0)
  538. return 0;
  539. p->op = p->ostage;
  540. p->oe = p->ostage + n;
  541. return n;
  542. }
  543. /*
  544. * restart output
  545. */
  546. void
  547. uartkick(void *v)
  548. {
  549. Uart *p = v;
  550. if(p->blocked)
  551. return;
  552. ilock(&p->tlock);
  553. (*p->phys->kick)(p);
  554. iunlock(&p->tlock);
  555. if(p->drain && uartdrained(p)){
  556. p->drain = 0;
  557. wakeup(&p->r);
  558. }
  559. }
  560. /*
  561. * Move data from the interrupt staging area to
  562. * the input Queue.
  563. */
  564. static void
  565. uartstageinput(Uart *p)
  566. {
  567. int n;
  568. uchar *ir, *iw;
  569. while(p->ir != p->iw){
  570. ir = p->ir;
  571. if(p->ir > p->iw){
  572. iw = p->ie;
  573. p->ir = p->istage;
  574. }
  575. else{
  576. iw = p->iw;
  577. p->ir = p->iw;
  578. }
  579. if((n = qproduce(p->iq, ir, iw - ir)) < 0){
  580. p->serr++;
  581. (*p->phys->rts)(p, 0);
  582. }
  583. else if(n == 0)
  584. p->berr++;
  585. }
  586. }
  587. /*
  588. * receive a character at interrupt time
  589. */
  590. void
  591. uartrecv(Uart *p, char ch)
  592. {
  593. uchar *next;
  594. /* software flow control */
  595. if(p->xonoff){
  596. if(ch == CTLS){
  597. p->blocked = 1;
  598. }else if(ch == CTLQ){
  599. p->blocked = 0;
  600. p->ctsbackoff = 2; /* clock gets output going again */
  601. }
  602. }
  603. /* receive the character */
  604. if(p->putc)
  605. p->putc(p->iq, ch);
  606. else{
  607. ilock(&p->rlock);
  608. next = p->iw + 1;
  609. if(next == p->ie)
  610. next = p->istage;
  611. if(next == p->ir)
  612. uartstageinput(p);
  613. if(next != p->ir){
  614. *p->iw = ch;
  615. p->iw = next;
  616. }
  617. iunlock(&p->rlock);
  618. }
  619. }
  620. /*
  621. * we save up input characters till clock time to reduce
  622. * per character interrupt overhead.
  623. */
  624. static void
  625. uartclock(void)
  626. {
  627. Uart *p;
  628. lock(&uartalloc);
  629. for(p = uartalloc.elist; p; p = p->elist){
  630. /* this hopefully amortizes cost of qproduce to many chars */
  631. if(p->iw != p->ir){
  632. ilock(&p->rlock);
  633. uartstageinput(p);
  634. iunlock(&p->rlock);
  635. }
  636. /* hang up if requested */
  637. if(p->dohup){
  638. qhangup(p->iq, 0);
  639. qhangup(p->oq, 0);
  640. p->dohup = 0;
  641. }
  642. /* this adds hysteresis to hardware/software flow control */
  643. if(p->ctsbackoff){
  644. ilock(&p->tlock);
  645. if(p->ctsbackoff){
  646. if(--(p->ctsbackoff) == 0)
  647. (*p->phys->kick)(p);
  648. }
  649. iunlock(&p->tlock);
  650. }
  651. }
  652. unlock(&uartalloc);
  653. }
  654. /*
  655. * polling console input, output
  656. */
  657. Uart* consuart;
  658. int
  659. uartgetc(void)
  660. {
  661. if(consuart == nil || consuart->phys->getc == nil)
  662. return -1;
  663. return consuart->phys->getc(consuart);
  664. }
  665. void
  666. uartputc(int c)
  667. {
  668. if(consuart == nil || consuart->phys->putc == nil)
  669. return;
  670. consuart->phys->putc(consuart, c);
  671. }
  672. void
  673. uartputs(char *s, int n)
  674. {
  675. char *e;
  676. if(consuart == nil || consuart->phys->putc == nil)
  677. return;
  678. e = s+n;
  679. for(; s<e; s++){
  680. if(*s == '\n')
  681. consuart->phys->putc(consuart, '\r');
  682. consuart->phys->putc(consuart, *s);
  683. }
  684. }