uarti8250.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. /*
  9. * 8250 UART and compatibles.
  10. */
  11. enum {
  12. Uart0 = 0x3F8, /* COM1 */
  13. Uart0IRQ = 4,
  14. Uart1 = 0x2F8, /* COM2 */
  15. Uart1IRQ = 3,
  16. UartFREQ = 1843200,
  17. };
  18. enum { /* I/O ports */
  19. Rbr = 0, /* Receiver Buffer (RO) */
  20. Thr = 0, /* Transmitter Holding (WO) */
  21. Ier = 1, /* Interrupt Enable */
  22. Iir = 2, /* Interrupt Identification (RO) */
  23. Fcr = 2, /* FIFO Control (WO) */
  24. Lcr = 3, /* Line Control */
  25. Mcr = 4, /* Modem Control */
  26. Lsr = 5, /* Line Status */
  27. Msr = 6, /* Modem Status */
  28. Scr = 7, /* Scratch Pad */
  29. Dll = 0, /* Divisor Latch LSB */
  30. Dlm = 1, /* Divisor Latch MSB */
  31. };
  32. enum { /* Ier */
  33. Erda = 0x01, /* Enable Received Data Available */
  34. Ethre = 0x02, /* Enable Thr Empty */
  35. Erls = 0x04, /* Enable Receiver Line Status */
  36. Ems = 0x08, /* Enable Modem Status */
  37. };
  38. enum { /* Iir */
  39. Ims = 0x00, /* Ms interrupt */
  40. Ip = 0x01, /* Interrupt Pending (not) */
  41. Ithre = 0x02, /* Thr Empty */
  42. Irda = 0x04, /* Received Data Available */
  43. Irls = 0x06, /* Receiver Line Status */
  44. Ictoi = 0x0C, /* Character Time-out Indication */
  45. IirMASK = 0x3F,
  46. Ifena = 0xC0, /* FIFOs enabled */
  47. };
  48. enum { /* Fcr */
  49. FIFOena = 0x01, /* FIFO enable */
  50. FIFOrclr = 0x02, /* clear Rx FIFO */
  51. FIFOtclr = 0x04, /* clear Tx FIFO */
  52. FIFO1 = 0x00, /* Rx FIFO trigger level 1 byte */
  53. FIFO4 = 0x40, /* 4 bytes */
  54. FIFO8 = 0x80, /* 8 bytes */
  55. FIFO14 = 0xC0, /* 14 bytes */
  56. };
  57. enum { /* Lcr */
  58. Wls5 = 0x00, /* Word Length Select 5 bits/byte */
  59. Wls6 = 0x01, /* 6 bits/byte */
  60. Wls7 = 0x02, /* 7 bits/byte */
  61. Wls8 = 0x03, /* 8 bits/byte */
  62. WlsMASK = 0x03,
  63. Stb = 0x04, /* 2 stop bits */
  64. Pen = 0x08, /* Parity Enable */
  65. Eps = 0x10, /* Even Parity Select */
  66. Stp = 0x20, /* Stick Parity */
  67. Brk = 0x40, /* Break */
  68. Dlab = 0x80, /* Divisor Latch Access Bit */
  69. };
  70. enum { /* Mcr */
  71. Dtr = 0x01, /* Data Terminal Ready */
  72. Rts = 0x02, /* Ready To Send */
  73. Out1 = 0x04, /* no longer in use */
  74. Ie = 0x08, /* IRQ Enable */
  75. Dm = 0x10, /* Diagnostic Mode loopback */
  76. };
  77. enum { /* Lsr */
  78. Dr = 0x01, /* Data Ready */
  79. Oe = 0x02, /* Overrun Error */
  80. Pe = 0x04, /* Parity Error */
  81. Fe = 0x08, /* Framing Error */
  82. Bi = 0x10, /* Break Interrupt */
  83. Thre = 0x20, /* Thr Empty */
  84. Temt = 0x40, /* Tramsmitter Empty */
  85. FIFOerr = 0x80, /* error in receiver FIFO */
  86. };
  87. enum { /* Msr */
  88. Dcts = 0x01, /* Delta Cts */
  89. Ddsr = 0x02, /* Delta Dsr */
  90. Teri = 0x04, /* Trailing Edge of Ri */
  91. Ddcd = 0x08, /* Delta Dcd */
  92. Cts = 0x10, /* Clear To Send */
  93. Dsr = 0x20, /* Data Set Ready */
  94. Ri = 0x40, /* Ring Indicator */
  95. Dcd = 0x80, /* Data Set Ready */
  96. };
  97. typedef struct Ctlr {
  98. int io;
  99. int irq;
  100. int tbdf;
  101. int iena;
  102. uchar sticky[8];
  103. Lock;
  104. int hasfifo;
  105. int checkfifo;
  106. int fena;
  107. } Ctlr;
  108. extern PhysUart i8250physuart;
  109. static Ctlr i8250ctlr[2] = {
  110. { .io = Uart0,
  111. .irq = Uart0IRQ,
  112. .tbdf = BUSUNKNOWN, },
  113. { .io = Uart1,
  114. .irq = Uart1IRQ,
  115. .tbdf = BUSUNKNOWN, },
  116. };
  117. static Uart i8250uart[2] = {
  118. { .regs = &i8250ctlr[0],
  119. .name = "COM1",
  120. .freq = UartFREQ,
  121. .phys = &i8250physuart,
  122. .special= 0,
  123. .next = &i8250uart[1], },
  124. { .regs = &i8250ctlr[1],
  125. .name = "COM2",
  126. .freq = UartFREQ,
  127. .phys = &i8250physuart,
  128. .special= 0,
  129. .next = nil, },
  130. };
  131. #define csr8r(c, r) inb((c)->io+(r))
  132. #define csr8w(c, r, v) outb((c)->io+(r), (c)->sticky[(r)]|(v))
  133. static long
  134. i8250status(Uart* uart, void* buf, long n, long offset)
  135. {
  136. char *p;
  137. Ctlr *ctlr;
  138. uchar ier, lcr, mcr, msr;
  139. ctlr = uart->regs;
  140. p = malloc(READSTR);
  141. mcr = ctlr->sticky[Mcr];
  142. msr = csr8r(ctlr, Msr);
  143. ier = ctlr->sticky[Ier];
  144. lcr = ctlr->sticky[Lcr];
  145. snprint(p, READSTR,
  146. "b%d c%d d%d e%d l%d m%d p%c r%d s%d i%d ier=%ux\n"
  147. "dev(%d) type(%d) framing(%d) overruns(%d)%s%s%s%s\n",
  148. uart->baud,
  149. uart->hup_dcd,
  150. (msr & Dsr) != 0,
  151. uart->hup_dsr,
  152. (lcr & WlsMASK) + 5,
  153. (ier & Ems) != 0,
  154. (lcr & Pen) ? ((lcr & Eps) ? 'e': 'o'): 'n',
  155. (mcr & Rts) != 0,
  156. (lcr & Stb) ? 2: 1,
  157. ctlr->fena,
  158. ier,
  159. uart->dev,
  160. uart->type,
  161. uart->ferr,
  162. uart->oerr,
  163. (msr & Cts) ? " cts": "",
  164. (msr & Dsr) ? " dsr": "",
  165. (msr & Dcd) ? " dcd": "",
  166. (msr & Ri) ? " ring": ""
  167. );
  168. n = readstr(offset, buf, n, p);
  169. free(p);
  170. return n;
  171. }
  172. static void
  173. i8250fifo(Uart* uart, int level)
  174. {
  175. Ctlr *ctlr;
  176. ctlr = uart->regs;
  177. if(ctlr->hasfifo == 0)
  178. return;
  179. /*
  180. * Changing the FIFOena bit in Fcr flushes data
  181. * from both receive and transmit FIFOs; there's
  182. * no easy way to guarantee not losing data on
  183. * the receive side, but it's possible to wait until
  184. * the transmitter is really empty.
  185. */
  186. ilock(ctlr);
  187. while(!(csr8r(ctlr, Lsr) & Temt))
  188. ;
  189. /*
  190. * Set the trigger level, default is the max.
  191. * value.
  192. * Some UARTs require FIFOena to be set before
  193. * other bits can take effect, so set it twice.
  194. */
  195. ctlr->fena = level;
  196. switch(level){
  197. case 0:
  198. break;
  199. case 1:
  200. level = FIFO1|FIFOena;
  201. break;
  202. case 4:
  203. level = FIFO4|FIFOena;
  204. break;
  205. case 8:
  206. level = FIFO8|FIFOena;
  207. break;
  208. default:
  209. level = FIFO14|FIFOena;
  210. break;
  211. }
  212. csr8w(ctlr, Fcr, level);
  213. csr8w(ctlr, Fcr, level);
  214. iunlock(ctlr);
  215. }
  216. static void
  217. i8250dtr(Uart* uart, int on)
  218. {
  219. Ctlr *ctlr;
  220. /*
  221. * Toggle DTR.
  222. */
  223. ctlr = uart->regs;
  224. if(on)
  225. ctlr->sticky[Mcr] |= Dtr;
  226. else
  227. ctlr->sticky[Mcr] &= ~Dtr;
  228. csr8w(ctlr, Mcr, 0);
  229. }
  230. static void
  231. i8250rts(Uart* uart, int on)
  232. {
  233. Ctlr *ctlr;
  234. /*
  235. * Toggle RTS.
  236. */
  237. ctlr = uart->regs;
  238. if(on)
  239. ctlr->sticky[Mcr] |= Rts;
  240. else
  241. ctlr->sticky[Mcr] &= ~Rts;
  242. csr8w(ctlr, Mcr, 0);
  243. }
  244. static void
  245. i8250modemctl(Uart* uart, int on)
  246. {
  247. Ctlr *ctlr;
  248. ctlr = uart->regs;
  249. ilock(&uart->tlock);
  250. if(on){
  251. ctlr->sticky[Ier] |= Ems;
  252. csr8w(ctlr, Ier, ctlr->sticky[Ier]);
  253. uart->modem = 1;
  254. uart->cts = csr8r(ctlr, Msr) & Cts;
  255. }
  256. else{
  257. ctlr->sticky[Ier] &= ~Ems;
  258. csr8w(ctlr, Ier, ctlr->sticky[Ier]);
  259. uart->modem = 0;
  260. uart->cts = 1;
  261. }
  262. iunlock(&uart->tlock);
  263. /* modem needs fifo */
  264. (*uart->phys->fifo)(uart, on);
  265. }
  266. static int
  267. i8250parity(Uart* uart, int parity)
  268. {
  269. int lcr;
  270. Ctlr *ctlr;
  271. ctlr = uart->regs;
  272. lcr = ctlr->sticky[Lcr] & ~(Eps|Pen);
  273. switch(parity){
  274. case 'e':
  275. lcr |= Eps|Pen;
  276. break;
  277. case 'o':
  278. lcr |= Pen;
  279. break;
  280. case 'n':
  281. default:
  282. break;
  283. }
  284. ctlr->sticky[Lcr] = lcr;
  285. csr8w(ctlr, Lcr, 0);
  286. uart->parity = parity;
  287. return 0;
  288. }
  289. static int
  290. i8250stop(Uart* uart, int stop)
  291. {
  292. int lcr;
  293. Ctlr *ctlr;
  294. ctlr = uart->regs;
  295. lcr = ctlr->sticky[Lcr] & ~Stb;
  296. switch(stop){
  297. case 1:
  298. break;
  299. case 2:
  300. lcr |= Stb;
  301. break;
  302. default:
  303. return -1;
  304. }
  305. ctlr->sticky[Lcr] = lcr;
  306. csr8w(ctlr, Lcr, 0);
  307. uart->stop = stop;
  308. return 0;
  309. }
  310. static int
  311. i8250bits(Uart* uart, int bits)
  312. {
  313. int lcr;
  314. Ctlr *ctlr;
  315. ctlr = uart->regs;
  316. lcr = ctlr->sticky[Lcr] & ~WlsMASK;
  317. switch(bits){
  318. case 5:
  319. lcr |= Wls5;
  320. break;
  321. case 6:
  322. lcr |= Wls6;
  323. break;
  324. case 7:
  325. lcr |= Wls7;
  326. break;
  327. case 8:
  328. lcr |= Wls8;
  329. break;
  330. default:
  331. return -1;
  332. }
  333. ctlr->sticky[Lcr] = lcr;
  334. csr8w(ctlr, Lcr, 0);
  335. uart->bits = bits;
  336. return 0;
  337. }
  338. static int
  339. i8250baud(Uart* uart, int baud)
  340. {
  341. ulong bgc;
  342. Ctlr *ctlr;
  343. /*
  344. * Set the Baud rate by calculating and setting the Baud rate
  345. * Generator Constant. This will work with fairly non-standard
  346. * Baud rates.
  347. */
  348. if(uart->freq == 0 || baud <= 0)
  349. return -1;
  350. bgc = (uart->freq+8*baud-1)/(16*baud);
  351. ctlr = uart->regs;
  352. csr8w(ctlr, Lcr, Dlab);
  353. outb(ctlr->io+Dlm, bgc>>8);
  354. outb(ctlr->io+Dll, bgc);
  355. csr8w(ctlr, Lcr, 0);
  356. uart->baud = baud;
  357. return 0;
  358. }
  359. static void
  360. i8250break(Uart* uart, int ms)
  361. {
  362. Ctlr *ctlr;
  363. /*
  364. * Send a break.
  365. */
  366. if(ms == 0)
  367. ms = 200;
  368. ctlr = uart->regs;
  369. csr8w(ctlr, Lcr, Brk);
  370. tsleep(&up->sleep, return0, 0, ms);
  371. csr8w(ctlr, Lcr, 0);
  372. }
  373. static void
  374. i8250kick(Uart* uart)
  375. {
  376. int i;
  377. Ctlr *ctlr;
  378. if(uart->cts == 0 || uart->blocked)
  379. return;
  380. /*
  381. * 128 here is an arbitrary limit to make sure
  382. * we don't stay in this loop too long. If the
  383. * chip's output queue is longer than 128, too
  384. * bad -- presotto
  385. */
  386. ctlr = uart->regs;
  387. for(i = 0; i < 128; i++){
  388. if(!(csr8r(ctlr, Lsr) & Thre))
  389. break;
  390. if(uart->op >= uart->oe && uartstageoutput(uart) == 0)
  391. break;
  392. outb(ctlr->io+Thr, *(uart->op++));
  393. }
  394. }
  395. static void
  396. i8250interrupt(Ureg*, void* arg)
  397. {
  398. Ctlr *ctlr;
  399. Uart *uart;
  400. int iir, lsr, old, r;
  401. uart = arg;
  402. ctlr = uart->regs;
  403. for(iir = csr8r(ctlr, Iir); !(iir & Ip); iir = csr8r(ctlr, Iir)){
  404. switch(iir & IirMASK){
  405. case Ims: /* Ms interrupt */
  406. r = csr8r(ctlr, Msr);
  407. if(r & Dcts){
  408. ilock(&uart->tlock);
  409. old = uart->cts;
  410. uart->cts = r & Cts;
  411. if(old == 0 && uart->cts)
  412. uart->ctsbackoff = 2;
  413. iunlock(&uart->tlock);
  414. }
  415. if(r & Ddsr){
  416. old = r & Dsr;
  417. if(uart->hup_dsr && uart->dsr && !old)
  418. uart->dohup = 1;
  419. uart->dsr = old;
  420. }
  421. if(r & Ddcd){
  422. old = r & Dcd;
  423. if(uart->hup_dcd && uart->dcd && !old)
  424. uart->dohup = 1;
  425. uart->dcd = old;
  426. }
  427. break;
  428. case Ithre: /* Thr Empty */
  429. uartkick(uart);
  430. break;
  431. case Irda: /* Received Data Available */
  432. case Ictoi: /* Character Time-out Indication */
  433. /*
  434. * Consume any received data.
  435. * If the received byte came in with a break,
  436. * parity or framing error, throw it away;
  437. * overrun is an indication that something has
  438. * already been tossed.
  439. */
  440. while((lsr = csr8r(ctlr, Lsr)) & Dr){
  441. if(lsr & Oe)
  442. uart->oerr++;
  443. if(lsr & Pe)
  444. uart->perr++;
  445. if(lsr & Fe)
  446. uart->ferr++;
  447. r = csr8r(ctlr, Rbr);
  448. if(!(lsr & (Bi|Fe|Pe)))
  449. uartrecv(uart, r);
  450. }
  451. break;
  452. default:
  453. iprint("weird uart interrupt 0x%2.2uX\n", iir);
  454. break;
  455. }
  456. }
  457. }
  458. static void
  459. i8250disable(Uart* uart)
  460. {
  461. Ctlr *ctlr;
  462. /*
  463. * Turn off DTR and RTS, disable interrupts and fifos.
  464. */
  465. (*uart->phys->dtr)(uart, 0);
  466. (*uart->phys->rts)(uart, 0);
  467. (*uart->phys->fifo)(uart, 0);
  468. ctlr = uart->regs;
  469. ctlr->sticky[Ier] = 0;
  470. csr8w(ctlr, Ier, ctlr->sticky[Ier]);
  471. if(ctlr->iena != 0){
  472. if(intrdisable(ctlr->irq, i8250interrupt, uart, ctlr->tbdf, uart->name) == 0)
  473. ctlr->iena = 0;
  474. }
  475. }
  476. static void
  477. i8250enable(Uart* uart, int ie)
  478. {
  479. Ctlr *ctlr;
  480. ctlr = uart->regs;
  481. /*
  482. * Check if there is a FIFO.
  483. * Changing the FIFOena bit in Fcr flushes data
  484. * from both receive and transmit FIFOs; there's
  485. * no easy way to guarantee not losing data on
  486. * the receive side, but it's possible to wait until
  487. * the transmitter is really empty.
  488. * Also, reading the Iir outwith i8250interrupt()
  489. * can be dangerous, but this should only happen
  490. * once, before interrupts are enabled.
  491. */
  492. ilock(ctlr);
  493. if(!ctlr->checkfifo){
  494. /*
  495. * Wait until the transmitter is really empty.
  496. */
  497. while(!(csr8r(ctlr, Lsr) & Temt))
  498. ;
  499. csr8w(ctlr, Fcr, FIFOena);
  500. if(csr8r(ctlr, Iir) & Ifena)
  501. ctlr->hasfifo = 1;
  502. csr8w(ctlr, Fcr, 0);
  503. ctlr->checkfifo = 1;
  504. }
  505. iunlock(ctlr);
  506. /*
  507. * Enable interrupts and turn on DTR and RTS.
  508. * Be careful if this is called to set up a polled serial line
  509. * early on not to try to enable interrupts as interrupt-
  510. * -enabling mechanisms might not be set up yet.
  511. */
  512. if(ie){
  513. if(ctlr->iena == 0){
  514. intrenable(ctlr->irq, i8250interrupt, uart, ctlr->tbdf, uart->name);
  515. ctlr->iena = 1;
  516. }
  517. ctlr->sticky[Ier] = Ethre|Erda;
  518. ctlr->sticky[Mcr] |= Ie;
  519. }
  520. else{
  521. ctlr->sticky[Ier] = 0;
  522. ctlr->sticky[Mcr] = 0;
  523. }
  524. csr8w(ctlr, Ier, ctlr->sticky[Ier]);
  525. csr8w(ctlr, Mcr, ctlr->sticky[Mcr]);
  526. (*uart->phys->dtr)(uart, 1);
  527. (*uart->phys->rts)(uart, 1);
  528. /*
  529. * During startup, the i8259 interrupt controller is reset.
  530. * This may result in a lost interrupt from the i8250 uart.
  531. * The i8250 thinks the interrupt is still outstanding and does not
  532. * generate any further interrupts. The workaround is to call the
  533. * interrupt handler to clear any pending interrupt events.
  534. * Note: this must be done after setting Ier.
  535. */
  536. if(ie)
  537. i8250interrupt(nil, uart);
  538. }
  539. void*
  540. i8250alloc(int io, int irq, int tbdf)
  541. {
  542. Ctlr *ctlr;
  543. if((ctlr = malloc(sizeof(Ctlr))) != nil){
  544. ctlr->io = io;
  545. ctlr->irq = irq;
  546. ctlr->tbdf = tbdf;
  547. }
  548. return ctlr;
  549. }
  550. static Uart*
  551. i8250pnp(void)
  552. {
  553. return i8250uart;
  554. }
  555. static int
  556. i8250getc(Uart *uart)
  557. {
  558. Ctlr *ctlr;
  559. ctlr = uart->regs;
  560. while(!(csr8r(ctlr, Lsr)&Dr))
  561. delay(1);
  562. return csr8r(ctlr, Rbr);
  563. }
  564. static void
  565. i8250putc(Uart *uart, int c)
  566. {
  567. int i;
  568. Ctlr *ctlr;
  569. ctlr = uart->regs;
  570. for(i = 0; !(csr8r(ctlr, Lsr)&Thre) && i < 128; i++)
  571. delay(1);
  572. outb(ctlr->io+Thr, c);
  573. for(i = 0; !(csr8r(ctlr, Lsr)&Thre) && i < 128; i++)
  574. delay(1);
  575. }
  576. PhysUart i8250physuart = {
  577. .name = "i8250",
  578. .pnp = i8250pnp,
  579. .enable = i8250enable,
  580. .disable = i8250disable,
  581. .kick = i8250kick,
  582. .dobreak = i8250break,
  583. .baud = i8250baud,
  584. .bits = i8250bits,
  585. .stop = i8250stop,
  586. .parity = i8250parity,
  587. .modemctl = i8250modemctl,
  588. .rts = i8250rts,
  589. .dtr = i8250dtr,
  590. .status = i8250status,
  591. .fifo = i8250fifo,
  592. .getc = i8250getc,
  593. .putc = i8250putc,
  594. };
  595. void
  596. i8250console(void)
  597. {
  598. Uart *uart;
  599. int n;
  600. char *cmd, *p;
  601. if((p = getconf("console")) == nil)
  602. return;
  603. n = strtoul(p, &cmd, 0);
  604. if(p == cmd)
  605. return;
  606. switch(n){
  607. default:
  608. return;
  609. case 0:
  610. uart = &i8250uart[0];
  611. break;
  612. case 1:
  613. uart = &i8250uart[1];
  614. break;
  615. }
  616. (*uart->phys->enable)(uart, 0);
  617. uartctl(uart, "b9600 l8 pn s1");
  618. if(*cmd != '\0')
  619. uartctl(uart, cmd);
  620. consuart = uart;
  621. uart->console = 1;
  622. }
  623. void
  624. i8250mouse(char* which, int (*putc)(Queue*, int), int setb1200)
  625. {
  626. char *p;
  627. int port;
  628. port = strtol(which, &p, 0);
  629. if(p == which || port < 0 || port > 1)
  630. error(Ebadarg);
  631. uartmouse(&i8250uart[port], putc, setb1200);
  632. }
  633. void
  634. i8250setmouseputc(char* which, int (*putc)(Queue*, int))
  635. {
  636. char *p;
  637. int port;
  638. port = strtol(which, &p, 0);
  639. if(p == which || port < 0 || port > 1)
  640. error(Ebadarg);
  641. uartsetmouseputc(&i8250uart[port], putc);
  642. }