devuart.c 13 KB

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