Media table reorganization.
[unix-history] / sys / i386 / isa / com.c
CommitLineData
15637ed4
RG
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
d7136515 33 * from: @(#)com.c 7.5 (Berkeley) 5/16/91
4c45483e 34 * $Id: com.c,v 1.5 1993/10/16 13:45:46 rgrimes Exp $
15637ed4 35 */
15637ed4
RG
36
37#include "com.h"
38#if NCOM > 0
39/*
40 * COM driver, based on HP dca driver
41 * uses National Semiconductor NS16450/NS16550AF UART
42 */
43#include "param.h"
44#include "systm.h"
45#include "ioctl.h"
46#include "tty.h"
47#include "proc.h"
48#include "user.h"
49#include "conf.h"
50#include "file.h"
51#include "uio.h"
52#include "kernel.h"
53#include "syslog.h"
54
23d13371 55#include "i386/isa/isa.h"
15637ed4
RG
56#include "i386/isa/isa_device.h"
57#include "i386/isa/comreg.h"
58#include "i386/isa/ic/ns16550.h"
59#define cominor(d)
60
4c45483e
GW
61static int comprobe();
62static int comattach();
63void comintr(int);
64static void comstart(struct tty *);
65static int comparam(struct tty *, struct termios *);
66
67static void comeint(int, int, int);
68static void commint(int, int);
69static void cominit(int, int);
15637ed4
RG
70
71struct isa_driver comdriver = {
72 comprobe, comattach, "com"
73};
74
75int comsoftCAR;
76int com_active;
77int com_hasfifo;
78int ncom = NCOM;
79#ifdef COMCONSOLE
80int comconsole = COMCONSOLE;
81#else
82int comconsole = -1;
83#endif
84int comconsinit;
85int comdefaultrate = TTYDEF_SPEED;
86int commajor;
87short com_addr[NCOM];
88struct tty com_tty[NCOM];
89
90struct speedtab comspeedtab[] = {
91 0, 0,
92 50, COMBRD(50),
93 75, COMBRD(75),
94 110, COMBRD(110),
95 134, COMBRD(134),
96 150, COMBRD(150),
97 200, COMBRD(200),
98 300, COMBRD(300),
99 600, COMBRD(600),
100 1200, COMBRD(1200),
101 1800, COMBRD(1800),
102 2400, COMBRD(2400),
103 4800, COMBRD(4800),
104 9600, COMBRD(9600),
105 19200, COMBRD(19200),
106 38400, COMBRD(38400),
107 57600, COMBRD(57600),
108 -1, -1
109};
110
111extern struct tty *constty;
112#ifdef KGDB
113#include "machine/remote-sl.h"
114
115extern int kgdb_dev;
116extern int kgdb_rate;
117extern int kgdb_debug_init;
118#endif
119
120#define UNIT(x) (minor(x))
121
4c45483e 122int
15637ed4
RG
123comprobe(dev)
124struct isa_device *dev;
125{
126 /* force access to id reg */
127 outb(dev->id_iobase+com_cfcr, 0);
128 outb(dev->id_iobase+com_iir, 0);
129 DELAY(100);
130 if ((inb(dev->id_iobase+com_iir) & 0x38) == 0)
23d13371 131 return(IO_COMSIZE);
15637ed4
RG
132 return(0);
133}
134
135
136int
137comattach(isdp)
138struct isa_device *isdp;
139{
140 struct tty *tp;
141 u_char unit;
142 int port = isdp->id_iobase;
143
144 unit = isdp->id_unit;
145 if (unit == comconsole)
146 DELAY(1000);
147 com_addr[unit] = port;
148 com_active |= 1 << unit;
149 comsoftCAR |= 1 << unit; /* XXX */
150
151 /* look for a NS 16550AF UART with FIFOs */
152 outb(port+com_fifo, FIFO_ENABLE|FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_4);
153 DELAY(100);
154 if ((inb(port+com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK) {
155 com_hasfifo |= 1 << unit;
ba8a6aec 156 printf("com%d: fifo\n", unit);
15637ed4
RG
157 }
158
159 outb(port+com_ier, 0);
160 outb(port+com_mcr, 0 | MCR_IENABLE);
161#ifdef KGDB
162 if (kgdb_dev == makedev(commajor, unit)) {
163 if (comconsole == unit)
164 kgdb_dev = -1; /* can't debug over console port */
165 else {
166 (void) cominit(unit, kgdb_rate);
167 if (kgdb_debug_init) {
168 /*
169 * Print prefix of device name,
170 * let kgdb_connect print the rest.
171 */
172 printf("com%d: ", unit);
173 kgdb_connect(1);
174 } else
175 printf("com%d: kgdb enabled\n", unit);
176 }
177 }
178#endif
179 /*
180 * Need to reset baud rate, etc. of next print so reset comconsinit.
181 * Also make sure console is always "hardwired"
182 */
183 if (unit == comconsole) {
184 comconsinit = 0;
185 comsoftCAR |= (1 << unit);
186 }
187 return (1);
188}
189
190/* ARGSUSED */
4c45483e
GW
191int
192comopen(int /*dev_t*/ dev, int flag, int mode, struct proc *p)
15637ed4
RG
193{
194 register struct tty *tp;
195 register int unit;
196 int error = 0;
197
198 unit = UNIT(dev);
199 if (unit >= NCOM || (com_active & (1 << unit)) == 0)
200 return (ENXIO);
201 tp = &com_tty[unit];
202 tp->t_oproc = comstart;
203 tp->t_param = comparam;
204 tp->t_dev = dev;
205 if ((tp->t_state & TS_ISOPEN) == 0) {
206 tp->t_state |= TS_WOPEN;
207 ttychars(tp);
208 if (tp->t_ispeed == 0) {
209 tp->t_iflag = TTYDEF_IFLAG;
210 tp->t_oflag = TTYDEF_OFLAG;
211 tp->t_cflag = TTYDEF_CFLAG;
212 tp->t_lflag = TTYDEF_LFLAG;
213 tp->t_ispeed = tp->t_ospeed = comdefaultrate;
214 }
215 comparam(tp, &tp->t_termios);
216 ttsetwater(tp);
217 } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
218 return (EBUSY);
219 (void) spltty();
220 (void) commctl(dev, MCR_DTR | MCR_RTS, DMSET);
221 if ((comsoftCAR & (1 << unit)) || (commctl(dev, 0, DMGET) & MSR_DCD))
222 tp->t_state |= TS_CARR_ON;
223 while ((flag&O_NONBLOCK) == 0 && (tp->t_cflag&CLOCAL) == 0 &&
224 (tp->t_state & TS_CARR_ON) == 0) {
225 tp->t_state |= TS_WOPEN;
226 if (error = ttysleep(tp, (caddr_t)&tp->t_raw, TTIPRI | PCATCH,
227 ttopen, 0))
228 break;
229 }
230 (void) spl0();
231 if (error == 0)
4c45483e 232 error = (*linesw[tp->t_line].l_open)(dev, tp, 0);
15637ed4
RG
233 return (error);
234}
235
236/*ARGSUSED*/
4c45483e 237int
15637ed4
RG
238comclose(dev, flag, mode, p)
239 dev_t dev;
240 int flag, mode;
241 struct proc *p;
242{
243 register struct tty *tp;
244 register com;
245 register int unit;
246
247 unit = UNIT(dev);
248 com = com_addr[unit];
249 tp = &com_tty[unit];
250 (*linesw[tp->t_line].l_close)(tp, flag);
251 outb(com+com_cfcr, inb(com+com_cfcr) & ~CFCR_SBREAK);
252#ifdef KGDB
253 /* do not disable interrupts if debugging */
254 if (kgdb_dev != makedev(commajor, unit))
255#endif
256 outb(com+com_ier, 0);
257 if (tp->t_cflag&HUPCL || tp->t_state&TS_WOPEN ||
258 (tp->t_state&TS_ISOPEN) == 0)
259 (void) commctl(dev, 0, DMSET);
260 ttyclose(tp);
261 return(0);
262}
263
4c45483e 264int
15637ed4
RG
265comread(dev, uio, flag)
266 dev_t dev;
267 struct uio *uio;
4c45483e 268 int flag;
15637ed4
RG
269{
270 register struct tty *tp = &com_tty[UNIT(dev)];
271
272 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
273}
274
4c45483e 275int
15637ed4
RG
276comwrite(dev, uio, flag)
277 dev_t dev;
278 struct uio *uio;
4c45483e 279 int flag;
15637ed4
RG
280{
281 int unit = UNIT(dev);
282 register struct tty *tp = &com_tty[unit];
283
284 /*
285 * (XXX) We disallow virtual consoles if the physical console is
286 * a serial port. This is in case there is a display attached that
287 * is not the console. In that situation we don't need/want the X
288 * server taking over the console.
289 */
290 if (constty && unit == comconsole)
291 constty = NULL;
292 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
293}
294
4c45483e 295void
15637ed4
RG
296comintr(unit)
297 register int unit;
298{
299 register com;
300 register u_char code;
301 register struct tty *tp;
302
15637ed4
RG
303 com = com_addr[unit];
304 while (1) {
305 code = inb(com+com_iir);
306 switch (code & IIR_IMASK) {
307 case IIR_NOPEND:
4c45483e 308 return;
15637ed4
RG
309 case IIR_RXTOUT:
310 case IIR_RXRDY:
311 tp = &com_tty[unit];
312/*
313 * Process received bytes. Inline for speed...
314 */
315#ifdef KGDB
316#define RCVBYTE() \
317 code = inb(com+com_data); \
318 if ((tp->t_state & TS_ISOPEN) == 0) { \
319 if (kgdb_dev == makedev(commajor, unit) && \
320 code == FRAME_END) \
321 kgdb_connect(0); /* trap into kgdb */ \
322 } else \
323 (*linesw[tp->t_line].l_rint)(code, tp)
324#else
325#define RCVBYTE() \
326 code = inb(com+com_data); \
327 if (tp->t_state & TS_ISOPEN) \
328 (*linesw[tp->t_line].l_rint)(code, tp)
329#endif
330
331 RCVBYTE();
332
333 if (com_hasfifo & (1 << unit))
334 while ((code = inb(com+com_lsr)) & LSR_RCV_MASK) {
335 if (code == LSR_RXRDY) {
336 RCVBYTE();
337 } else
338 comeint(unit, code, com);
339 }
340 break;
341 case IIR_TXRDY:
342 tp = &com_tty[unit];
343 tp->t_state &=~ (TS_BUSY|TS_FLUSH);
344 if (tp->t_line)
345 (*linesw[tp->t_line].l_start)(tp);
346 else
347 comstart(tp);
348 break;
349 case IIR_RLS:
350 comeint(unit, inb(com+com_lsr), com);
351 break;
352 default:
353 if (code & IIR_NOPEND)
4c45483e 354 return;
15637ed4
RG
355 log(LOG_WARNING, "com%d: weird interrupt: 0x%x\n",
356 unit, code);
357 /* fall through */
358 case IIR_MLSC:
359 commint(unit, com);
360 break;
361 }
362 }
363}
364
4c45483e 365static void
15637ed4
RG
366comeint(unit, stat, com)
367 register int unit, stat;
4c45483e 368 register int com;
15637ed4
RG
369{
370 register struct tty *tp;
371 register int c;
372
373 tp = &com_tty[unit];
374 c = inb(com+com_data);
375 if ((tp->t_state & TS_ISOPEN) == 0) {
376#ifdef KGDB
377 /* we don't care about parity errors */
378 if (((stat & (LSR_BI|LSR_FE|LSR_PE)) == LSR_PE) &&
379 kgdb_dev == makedev(commajor, unit) && c == FRAME_END)
380 kgdb_connect(0); /* trap into kgdb */
381#endif
382 return;
383 }
384 if (stat & (LSR_BI | LSR_FE))
385 c |= TTY_FE;
386 else if (stat & LSR_PE)
387 c |= TTY_PE;
388 else if (stat & LSR_OE) { /* 30 Aug 92*/
389 c |= TTY_PE; /* Ought to have it's own define... */
390 log(LOG_WARNING, "com%d: silo overflow\n", unit);
391 }
392 (*linesw[tp->t_line].l_rint)(c, tp);
393}
394
4c45483e 395static void
15637ed4
RG
396commint(unit, com)
397 register int unit;
4c45483e 398 register int com;
15637ed4
RG
399{
400 register struct tty *tp;
401 register int stat;
402
403 tp = &com_tty[unit];
404 stat = inb(com+com_msr);
405 if ((stat & MSR_DDCD) && (comsoftCAR & (1 << unit)) == 0) {
406 if (stat & MSR_DCD)
407 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
408 else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0)
409 outb(com+com_mcr,
410 inb(com+com_mcr) & ~(MCR_DTR | MCR_RTS) | MCR_IENABLE);
411 } else if ((stat & MSR_DCTS) && (tp->t_state & TS_ISOPEN) &&
55e49450 412 (tp->t_cflag & CRTSCTS)) {
15637ed4
RG
413 /* the line is up and we want to do rts/cts flow control */
414 if (stat & MSR_CTS) {
415 tp->t_state &=~ TS_TTSTOP;
416 ttstart(tp);
417 } else
418 tp->t_state |= TS_TTSTOP;
419 }
420}
421
4c45483e 422int
15637ed4
RG
423comioctl(dev, cmd, data, flag)
424 dev_t dev;
4c45483e 425 int cmd;
15637ed4 426 caddr_t data;
4c45483e 427 int flag;
15637ed4
RG
428{
429 register struct tty *tp;
430 register int unit = UNIT(dev);
431 register com;
432 register int error;
433
434 tp = &com_tty[unit];
435 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
436 if (error >= 0)
437 return (error);
438 error = ttioctl(tp, cmd, data, flag);
439 if (error >= 0)
440 return (error);
441
442 com = com_addr[unit];
443 switch (cmd) {
444
445 case TIOCSBRK:
446 outb(com+com_cfcr, inb(com+com_cfcr) | CFCR_SBREAK);
447 break;
448
449 case TIOCCBRK:
450 outb(com+com_cfcr, inb(com+com_cfcr) & ~CFCR_SBREAK);
451 break;
452
453 case TIOCSDTR:
454 (void) commctl(dev, MCR_DTR | MCR_RTS, DMBIS);
455 break;
456
457 case TIOCCDTR:
458 (void) commctl(dev, MCR_DTR | MCR_RTS, DMBIC);
459 break;
460
461 case TIOCMSET:
462 (void) commctl(dev, *(int *)data, DMSET);
463 break;
464
465 case TIOCMBIS:
466 (void) commctl(dev, *(int *)data, DMBIS);
467 break;
468
469 case TIOCMBIC:
470 (void) commctl(dev, *(int *)data, DMBIC);
471 break;
472
473 case TIOCMGET:
474 *(int *)data = commctl(dev, 0, DMGET);
475 break;
476
477 default:
478 return (ENOTTY);
479 }
480 return (0);
481}
482
4c45483e 483static int
15637ed4
RG
484comparam(tp, t)
485 register struct tty *tp;
486 register struct termios *t;
487{
4c45483e
GW
488 register int com;
489 register int cfcr = 0, cflag = t->c_cflag;
15637ed4
RG
490 int unit = UNIT(tp->t_dev);
491 int ospeed = ttspeedtab(t->c_ospeed, comspeedtab);
492
493 /* check requested parameters */
494 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
495 return(EINVAL);
496 /* and copy to tty */
497 tp->t_ispeed = t->c_ispeed;
498 tp->t_ospeed = t->c_ospeed;
499 tp->t_cflag = cflag;
500
501 com = com_addr[unit];
502 outb(com+com_ier, IER_ERXRDY | IER_ETXRDY | IER_ERLS /*| IER_EMSC*/);
503 if (ospeed == 0) {
504 (void) commctl(unit, 0, DMSET); /* hang up line */
505 return(0);
506 }
507 outb(com+com_cfcr, inb(com+com_cfcr) | CFCR_DLAB);
508 outb(com+com_data, ospeed & 0xFF);
509 outb(com+com_ier, ospeed >> 8);
510 switch (cflag&CSIZE) {
511 case CS5:
512 cfcr = CFCR_5BITS; break;
513 case CS6:
514 cfcr = CFCR_6BITS; break;
515 case CS7:
516 cfcr = CFCR_7BITS; break;
517 case CS8:
518 cfcr = CFCR_8BITS; break;
519 }
520 if (cflag&PARENB) {
521 cfcr |= CFCR_PENAB;
522 if ((cflag&PARODD) == 0)
523 cfcr |= CFCR_PEVEN;
524 }
525 if (cflag&CSTOPB)
526 cfcr |= CFCR_STOPB;
527 outb(com+com_cfcr, cfcr);
528
529 if (com_hasfifo & (1 << unit))
530 outb(com+com_fifo, FIFO_ENABLE | FIFO_TRIGGER_4);
531
532 return(0);
533}
534
4c45483e 535void
15637ed4
RG
536comstart(tp)
537 register struct tty *tp;
538{
539 register com;
540 int s, unit, c;
541
542 unit = UNIT(tp->t_dev);
543 com = com_addr[unit];
544 s = spltty();
545 if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP))
546 goto out;
547 if (RB_LEN(&tp->t_out) <= tp->t_lowat) {
548 if (tp->t_state&TS_ASLEEP) {
549 tp->t_state &= ~TS_ASLEEP;
550 wakeup((caddr_t)&tp->t_out);
551 }
552 if (tp->t_wsel) {
553 selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
554 tp->t_wsel = 0;
555 tp->t_state &= ~TS_WCOLL;
556 }
557 }
558 if (RB_LEN(&tp->t_out) == 0)
559 goto out;
560 if (inb(com+com_lsr) & LSR_TXRDY) {
561 c = getc(&tp->t_out);
562 tp->t_state |= TS_BUSY;
563 outb(com+com_data, c);
564 if (com_hasfifo & (1 << unit))
565 for (c = 1; c < 16 && RB_LEN(&tp->t_out); ++c)
566 outb(com+com_data, getc(&tp->t_out));
567 }
568out:
569 splx(s);
570}
571
572/*
573 * Stop output on a line.
574 */
575/*ARGSUSED*/
4c45483e 576void
15637ed4
RG
577comstop(tp, flag)
578 register struct tty *tp;
4c45483e 579 int flag;
15637ed4
RG
580{
581 register int s;
582
583 s = spltty();
584 if (tp->t_state & TS_BUSY) {
585 if ((tp->t_state&TS_TTSTOP)==0)
586 tp->t_state |= TS_FLUSH;
587 }
588 splx(s);
589}
590
4c45483e 591int
15637ed4
RG
592commctl(dev, bits, how)
593 dev_t dev;
594 int bits, how;
595{
596 register com;
597 register int unit;
598 int s;
599
600 unit = UNIT(dev);
601 com = com_addr[unit];
602 s = spltty();
603 switch (how) {
604
605 case DMSET:
606 outb(com+com_mcr, bits | MCR_IENABLE);
607 break;
608
609 case DMBIS:
610 outb(com+com_mcr, inb(com+com_mcr) | bits | MCR_IENABLE);
611 break;
612
613 case DMBIC:
614 outb(com+com_mcr, inb(com+com_mcr) & ~bits | MCR_IENABLE);
615 break;
616
617 case DMGET:
618 bits = inb(com+com_msr);
619 break;
620 }
621 (void) splx(s);
622 return(bits);
623}
624
625/*
626 * Following are all routines needed for COM to act as console
627 */
628#include "i386/i386/cons.h"
629
4c45483e 630void
15637ed4
RG
631comcnprobe(cp)
632 struct consdev *cp;
633{
634 int unit;
635
636 /* locate the major number */
637 for (commajor = 0; commajor < nchrdev; commajor++)
638 if (cdevsw[commajor].d_open == comopen)
639 break;
640
641 /* XXX: ick */
642 unit = CONUNIT;
643 com_addr[CONUNIT] = CONADDR;
644
645 /* make sure hardware exists? XXX */
646
647 /* initialize required fields */
648 cp->cn_dev = makedev(commajor, unit);
649 cp->cn_tp = &com_tty[unit];
650#ifdef COMCONSOLE
651 cp->cn_pri = CN_REMOTE; /* Force a serial port console */
652#else
653 cp->cn_pri = CN_NORMAL;
654#endif
655}
656
4c45483e 657void
15637ed4
RG
658comcninit(cp)
659 struct consdev *cp;
660{
661 int unit = UNIT(cp->cn_dev);
662
663 cominit(unit, comdefaultrate);
664 comconsole = unit;
665 comconsinit = 1;
666}
667
4c45483e 668static void
15637ed4
RG
669cominit(unit, rate)
670 int unit, rate;
671{
672 register int com;
673 int s;
674 short stat;
675
676#ifdef lint
677 stat = unit; if (stat) return;
678#endif
679 com = com_addr[unit];
680 s = splhigh();
681 outb(com+com_cfcr, CFCR_DLAB);
682 rate = ttspeedtab(comdefaultrate, comspeedtab);
683 outb(com+com_data, rate & 0xFF);
684 outb(com+com_ier, rate >> 8);
685 outb(com+com_cfcr, CFCR_8BITS);
686 outb(com+com_ier, IER_ERXRDY | IER_ETXRDY);
687 outb(com+com_fifo, FIFO_ENABLE|FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_4);
688 stat = inb(com+com_iir);
689 splx(s);
690}
691
4c45483e 692int
15637ed4 693comcngetc(dev)
4c45483e 694 dev_t dev;
15637ed4
RG
695{
696 register com = com_addr[UNIT(dev)];
697 short stat;
698 int c, s;
699
700#ifdef lint
701 stat = dev; if (stat) return(0);
702#endif
703 s = splhigh();
704 while (((stat = inb(com+com_lsr)) & LSR_RXRDY) == 0)
705 ;
706 c = inb(com+com_data);
707 stat = inb(com+com_iir);
708 splx(s);
709 return(c);
710}
711
712/*
713 * Console kernel output character routine.
714 */
4c45483e 715void
15637ed4
RG
716comcnputc(dev, c)
717 dev_t dev;
718 register int c;
719{
720 register com = com_addr[UNIT(dev)];
721 register int timo;
722 short stat;
723 int s = splhigh();
724
725#ifdef lint
726 stat = dev; if (stat) return;
727#endif
728#ifdef KGDB
729 if (dev != kgdb_dev)
730#endif
731 if (comconsinit == 0) {
732 (void) cominit(UNIT(dev), comdefaultrate);
733 comconsinit = 1;
734 }
735 /* wait for any pending transmission to finish */
736 timo = 50000;
737 while (((stat = inb(com+com_lsr)) & LSR_TXRDY) == 0 && --timo)
738 ;
739 outb(com+com_data, c);
740 /* wait for this transmission to complete */
741 timo = 1500000;
742 while (((stat = inb(com+com_lsr)) & LSR_TXRDY) == 0 && --timo)
743 ;
744 /* clear any interrupts generated by this transmission */
745 stat = inb(com+com_iir);
746 splx(s);
747}
748#endif
749
750int
751comselect(dev, rw, p)
752 dev_t dev;
753 int rw;
754 struct proc *p;
755{
756 register struct tty *tp = &com_tty[UNIT(dev)];
757 int nread;
758 int s = spltty();
759 struct proc *selp;
760
761 switch (rw) {
762
763 case FREAD:
764 nread = ttnread(tp);
765 if (nread > 0 ||
766 ((tp->t_cflag&CLOCAL) == 0 && (tp->t_state&TS_CARR_ON) == 0))
767 goto win;
768 if (tp->t_rsel && (selp = pfind(tp->t_rsel)) && selp->p_wchan == (caddr_t)&selwait)
769 tp->t_state |= TS_RCOLL;
770 else
771 tp->t_rsel = p->p_pid;
772 break;
773
774 case FWRITE:
775 if (RB_LEN(&tp->t_out) <= tp->t_lowat)
776 goto win;
777 if (tp->t_wsel && (selp = pfind(tp->t_wsel)) && selp->p_wchan == (caddr_t)&selwait)
778 tp->t_state |= TS_WCOLL;
779 else
780 tp->t_wsel = p->p_pid;
781 break;
782 }
783 splx(s);
784 return (0);
785 win:
786 splx(s);
787 return (1);
788}