devuart.c 11 KB

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