com.c revision 1.57 1 /* $NetBSD: com.c,v 1.57 1995/06/04 20:39:22 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
5 * Copyright (c) 1991 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)com.c 7.5 (Berkeley) 5/16/91
37 */
38
39 /*
40 * COM driver, based on HP dca driver
41 * uses National Semiconductor NS16450/NS16550AF UART
42 */
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/ioctl.h>
46 #include <sys/select.h>
47 #include <sys/tty.h>
48 #include <sys/proc.h>
49 #include <sys/user.h>
50 #include <sys/conf.h>
51 #include <sys/file.h>
52 #include <sys/uio.h>
53 #include <sys/kernel.h>
54 #include <sys/syslog.h>
55 #include <sys/types.h>
56 #include <sys/device.h>
57
58 #include <machine/cpu.h>
59 #include <machine/pio.h>
60
61 #include <dev/isa/isavar.h>
62 #include <dev/isa/comreg.h>
63 #include <dev/ic/ns16550.h>
64
65 #define COM_IBUFSIZE (2 * 256)
66 #define COM_IHIGHWATER ((3 * COM_IBUFSIZE) / 4)
67
68 struct com_softc {
69 struct device sc_dev;
70 void *sc_ih;
71 struct tty *sc_tty;
72
73 int sc_overflows;
74 int sc_floods;
75 int sc_errors;
76
77 int sc_iobase;
78 u_char sc_hwflags;
79 #define COM_HW_NOIEN 0x01
80 #define COM_HW_FIFO 0x02
81 #define COM_HW_CONSOLE 0x40
82 u_char sc_swflags;
83 #define COM_SW_SOFTCAR 0x01
84 #define COM_SW_CLOCAL 0x02
85 #define COM_SW_CRTSCTS 0x04
86 #define COM_SW_MDMBUF 0x08
87 u_char sc_msr, sc_mcr;
88 u_char sc_dtr;
89
90 u_char *sc_ibuf, *sc_ibufp, *sc_ibufhigh, *sc_ibufend;
91 u_char sc_ibufs[2][COM_IBUFSIZE];
92 };
93
94 int comprobe __P((struct device *, void *, void *));
95 void comattach __P((struct device *, struct device *, void *));
96 int comopen __P((dev_t, int, int, struct proc *));
97 int comclose __P((dev_t, int, int, struct proc *));
98 void comdiag __P((void *));
99 int comintr __P((void *));
100 void compoll __P((void *));
101 int comparam __P((struct tty *, struct termios *));
102 void comstart __P((struct tty *));
103
104 struct cfdriver comcd = {
105 NULL, "com", comprobe, comattach, DV_TTY, sizeof(struct com_softc)
106 };
107
108 int comdefaultrate = TTYDEF_SPEED;
109 #ifdef COMCONSOLE
110 int comconsole = COMCONSOLE;
111 #else
112 int comconsole = -1;
113 #endif
114 int comconsinit;
115 int commajor;
116 int comsopen = 0;
117 int comevents = 0;
118
119 #ifdef KGDB
120 #include <machine/remote-sl.h>
121 extern int kgdb_dev;
122 extern int kgdb_rate;
123 extern int kgdb_debug_init;
124 #endif
125
126 #define COMUNIT(x) (minor(x))
127
128 #define bis(c, b) do { const register int com_ad = (c); \
129 outb(com_ad, inb(com_ad) | (b)); } while(0)
130 #define bic(c, b) do { const register int com_ad = (c); \
131 outb(com_ad, inb(com_ad) & ~(b)); } while(0)
132
133 int
134 comspeed(speed)
135 long speed;
136 {
137 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
138
139 int x, err;
140
141 if (speed == 0)
142 return 0;
143 if (speed < 0)
144 return -1;
145 x = divrnd((COM_FREQ / 16), speed);
146 if (x <= 0)
147 return -1;
148 err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
149 if (err < 0)
150 err = -err;
151 if (err > COM_TOLERANCE)
152 return -1;
153 return x;
154
155 #undef divrnd(n, q)
156 }
157
158 int
159 comprobe1(iobase)
160 int iobase;
161 {
162
163 /* force access to id reg */
164 outb(iobase + com_cfcr, 0);
165 outb(iobase + com_iir, 0);
166 if (inb(iobase + com_iir) & 0x38)
167 return 0;
168
169 return 1;
170 }
171
172 int
173 comprobe(parent, match, aux)
174 struct device *parent;
175 void *match, *aux;
176 {
177 struct isa_attach_args *ia = aux;
178 int iobase = ia->ia_iobase;
179
180 if (!comprobe1(iobase))
181 return 0;
182
183 ia->ia_iosize = COM_NPORTS;
184 ia->ia_msize = 0;
185 return 1;
186 }
187
188 void
189 comattach(parent, self, aux)
190 struct device *parent, *self;
191 void *aux;
192 {
193 struct com_softc *sc = (void *)self;
194 struct isa_attach_args *ia = aux;
195 struct cfdata *cf = sc->sc_dev.dv_cfdata;
196 int iobase = ia->ia_iobase;
197 struct tty *tp;
198
199 sc->sc_iobase = iobase;
200 sc->sc_hwflags = cf->cf_flags & COM_HW_NOIEN;
201 sc->sc_swflags = 0;
202
203 if (sc->sc_dev.dv_unit == comconsole)
204 delay(1000);
205
206 /* look for a NS 16550AF UART with FIFOs */
207 outb(iobase + com_fifo,
208 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
209 delay(100);
210 if ((inb(iobase + com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK)
211 if ((inb(iobase + com_fifo) & FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
212 sc->sc_hwflags |= COM_HW_FIFO;
213 printf(": ns16550a, working fifo\n");
214 } else
215 printf(": ns16550, broken fifo\n");
216 else
217 printf(": ns8250 or ns16450, no fifo\n");
218 outb(iobase + com_fifo, 0);
219
220 /* disable interrupts */
221 outb(iobase + com_ier, 0);
222 outb(iobase + com_mcr, 0);
223
224 if (ia->ia_irq != IRQUNK)
225 sc->sc_ih = isa_intr_establish(ia->ia_irq, ISA_IST_EDGE,
226 ISA_IPL_TTY, comintr, sc);
227
228 #ifdef KGDB
229 if (kgdb_dev == makedev(commajor, unit)) {
230 if (comconsole == unit)
231 kgdb_dev = -1; /* can't debug over console port */
232 else {
233 (void) cominit(unit, kgdb_rate);
234 if (kgdb_debug_init) {
235 /*
236 * Print prefix of device name,
237 * let kgdb_connect print the rest.
238 */
239 printf("%s: ", sc->sc_dev.dv_xname);
240 kgdb_connect(1);
241 } else
242 printf("%s: kgdb enabled\n",
243 sc->sc_dev.dv_xname);
244 }
245 }
246 #endif
247
248 if (sc->sc_dev.dv_unit == comconsole) {
249 /*
250 * Need to reset baud rate, etc. of next print so reset
251 * comconsinit. Also make sure console is always "hardwired".
252 */
253 comconsinit = 0;
254 sc->sc_hwflags |= COM_HW_CONSOLE;
255 sc->sc_swflags |= COM_SW_SOFTCAR;
256 }
257 }
258
259 int
260 comopen(dev, flag, mode, p)
261 dev_t dev;
262 int flag, mode;
263 struct proc *p;
264 {
265 int unit = COMUNIT(dev);
266 struct com_softc *sc;
267 int iobase;
268 struct tty *tp;
269 int s;
270 int error = 0;
271
272 if (unit >= comcd.cd_ndevs)
273 return ENXIO;
274 sc = comcd.cd_devs[unit];
275 if (!sc)
276 return ENXIO;
277
278 if (!sc->sc_tty)
279 tp = sc->sc_tty = ttymalloc();
280 else
281 tp = sc->sc_tty;
282
283 tp->t_oproc = comstart;
284 tp->t_param = comparam;
285 tp->t_dev = dev;
286 if ((tp->t_state & TS_ISOPEN) == 0) {
287 tp->t_state |= TS_WOPEN;
288 ttychars(tp);
289 tp->t_iflag = TTYDEF_IFLAG;
290 tp->t_oflag = TTYDEF_OFLAG;
291 tp->t_cflag = TTYDEF_CFLAG;
292 if (sc->sc_swflags & COM_SW_CLOCAL)
293 tp->t_cflag |= CLOCAL;
294 if (sc->sc_swflags & COM_SW_CRTSCTS)
295 tp->t_cflag |= CRTSCTS;
296 if (sc->sc_swflags & COM_SW_MDMBUF)
297 tp->t_cflag |= MDMBUF;
298 tp->t_lflag = TTYDEF_LFLAG;
299 tp->t_ispeed = tp->t_ospeed = comdefaultrate;
300
301 s = spltty();
302
303 comparam(tp, &tp->t_termios);
304 ttsetwater(tp);
305
306 if (comsopen++ == 0)
307 timeout(compoll, NULL, 1);
308
309 sc->sc_ibufp = sc->sc_ibuf = sc->sc_ibufs[0];
310 sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
311 sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
312
313 iobase = sc->sc_iobase;
314 /* Set the FIFO threshold based on the receive speed. */
315 if (sc->sc_hwflags & COM_HW_FIFO)
316 outb(iobase + com_fifo,
317 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
318 (tp->t_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
319 /* flush any pending I/O */
320 while (inb(iobase + com_lsr) & LSR_RXRDY)
321 (void) inb(iobase + com_data);
322 /* you turn me on, baby */
323 sc->sc_mcr = MCR_DTR | MCR_RTS;
324 if (!(sc->sc_hwflags & COM_HW_NOIEN))
325 sc->sc_mcr |= MCR_IENABLE;
326 outb(iobase + com_mcr, sc->sc_mcr);
327 outb(iobase + com_ier,
328 IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC);
329
330 sc->sc_msr = inb(iobase + com_msr);
331 if (sc->sc_swflags & COM_SW_SOFTCAR || sc->sc_msr & MSR_DCD ||
332 tp->t_cflag & MDMBUF)
333 tp->t_state |= TS_CARR_ON;
334 else
335 tp->t_state &= ~TS_CARR_ON;
336 } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) {
337 return EBUSY;
338 } else
339 s = spltty();
340
341 /* wait for carrier if necessary */
342 if ((flag & O_NONBLOCK) == 0)
343 while ((tp->t_cflag & CLOCAL) == 0 &&
344 (tp->t_state & TS_CARR_ON) == 0) {
345 tp->t_state |= TS_WOPEN;
346 error = ttysleep(tp, (caddr_t)&tp->t_rawq,
347 TTIPRI | PCATCH, ttopen, 0);
348 if (error) {
349 /* XXX should turn off chip if we're the
350 only waiter */
351 splx(s);
352 return error;
353 }
354 }
355 splx(s);
356
357 return (*linesw[tp->t_line].l_open)(dev, tp);
358 }
359
360 int
361 comclose(dev, flag, mode, p)
362 dev_t dev;
363 int flag, mode;
364 struct proc *p;
365 {
366 int unit = COMUNIT(dev);
367 struct com_softc *sc = comcd.cd_devs[unit];
368 struct tty *tp = sc->sc_tty;
369 int iobase = sc->sc_iobase;
370 int s;
371
372 /* XXX This is for cons.c. */
373 if ((tp->t_state & TS_ISOPEN) == 0)
374 return 0;
375
376 (*linesw[tp->t_line].l_close)(tp, flag);
377 s = spltty();
378 bic(iobase + com_cfcr, CFCR_SBREAK);
379 outb(iobase + com_ier, 0);
380 if (tp->t_cflag & HUPCL &&
381 (sc->sc_swflags & COM_SW_SOFTCAR) == 0) {
382 /* XXX perhaps only clear DTR */
383 outb(iobase + com_mcr, 0);
384 }
385 tp->t_state &= ~(TS_BUSY | TS_FLUSH);
386 if (--comsopen == 0)
387 untimeout(compoll, NULL);
388 splx(s);
389 ttyclose(tp);
390 #ifdef notyet /* XXXX */
391 if (unit != comconsole) {
392 ttyfree(tp);
393 sc->sc_tty = 0;
394 }
395 #endif
396 return 0;
397 }
398
399 int
400 comread(dev, uio, flag)
401 dev_t dev;
402 struct uio *uio;
403 int flag;
404 {
405 struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
406 struct tty *tp = sc->sc_tty;
407
408 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
409 }
410
411 int
412 comwrite(dev, uio, flag)
413 dev_t dev;
414 struct uio *uio;
415 int flag;
416 {
417 struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
418 struct tty *tp = sc->sc_tty;
419
420 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
421 }
422
423 struct tty *
424 comtty(dev)
425 dev_t dev;
426 {
427 struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
428 struct tty *tp = sc->sc_tty;
429
430 return (tp);
431 }
432
433 static u_char
434 tiocm_xxx2mcr(data)
435 int data;
436 {
437 u_char m = 0;
438
439 if (data & TIOCM_DTR)
440 m |= MCR_DTR;
441 if (data & TIOCM_RTS)
442 m |= MCR_RTS;
443 return m;
444 }
445
446 int
447 comioctl(dev, cmd, data, flag, p)
448 dev_t dev;
449 u_long cmd;
450 caddr_t data;
451 int flag;
452 struct proc *p;
453 {
454 int unit = COMUNIT(dev);
455 struct com_softc *sc = comcd.cd_devs[unit];
456 struct tty *tp = sc->sc_tty;
457 int iobase = sc->sc_iobase;
458 int error;
459
460 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
461 if (error >= 0)
462 return error;
463 error = ttioctl(tp, cmd, data, flag, p);
464 if (error >= 0)
465 return error;
466
467 switch (cmd) {
468 case TIOCSBRK:
469 bis(iobase + com_cfcr, CFCR_SBREAK);
470 break;
471 case TIOCCBRK:
472 bic(iobase + com_cfcr, CFCR_SBREAK);
473 break;
474 case TIOCSDTR:
475 outb(iobase + com_mcr, sc->sc_mcr |= sc->sc_dtr);
476 break;
477 case TIOCCDTR:
478 outb(iobase + com_mcr, sc->sc_mcr &= ~sc->sc_dtr);
479 break;
480 case TIOCMSET:
481 sc->sc_mcr &= ~(MCR_DTR | MCR_RTS);
482 case TIOCMBIS:
483 outb(iobase + com_mcr,
484 sc->sc_mcr |= tiocm_xxx2mcr(*(int *)data));
485 break;
486 case TIOCMBIC:
487 outb(iobase + com_mcr,
488 sc->sc_mcr &= ~tiocm_xxx2mcr(*(int *)data));
489 break;
490 case TIOCMGET: {
491 u_char m;
492 int bits = 0;
493
494 m = sc->sc_mcr;
495 if (m & MCR_DTR)
496 bits |= TIOCM_DTR;
497 if (m & MCR_RTS)
498 bits |= TIOCM_RTS;
499 m = sc->sc_msr;
500 if (m & MSR_DCD)
501 bits |= TIOCM_CD;
502 if (m & MSR_CTS)
503 bits |= TIOCM_CTS;
504 if (m & MSR_DSR)
505 bits |= TIOCM_DSR;
506 if (m & (MSR_RI | MSR_TERI))
507 bits |= TIOCM_RI;
508 if (inb(iobase + com_ier))
509 bits |= TIOCM_LE;
510 *(int *)data = bits;
511 break;
512 }
513 case TIOCGFLAGS: {
514 int bits = 0;
515
516 if (sc->sc_swflags & COM_SW_SOFTCAR)
517 bits |= TIOCFLAG_SOFTCAR;
518 if (sc->sc_swflags & COM_SW_CLOCAL)
519 bits |= TIOCFLAG_CLOCAL;
520 if (sc->sc_swflags & COM_SW_CRTSCTS)
521 bits |= TIOCFLAG_CRTSCTS;
522 if (sc->sc_swflags & COM_SW_MDMBUF)
523 bits |= TIOCFLAG_MDMBUF;
524
525 *(int *)data = bits;
526 break;
527 }
528 case TIOCSFLAGS: {
529 int userbits, driverbits = 0;
530
531 error = suser(p->p_ucred, &p->p_acflag);
532 if (error != 0)
533 return(EPERM);
534
535 userbits = *(int *)data;
536 if ((userbits & TIOCFLAG_SOFTCAR) ||
537 (sc->sc_hwflags & COM_HW_CONSOLE))
538 driverbits |= COM_SW_SOFTCAR;
539 if (userbits & TIOCFLAG_CLOCAL)
540 driverbits |= COM_SW_CLOCAL;
541 if (userbits & TIOCFLAG_CRTSCTS)
542 driverbits |= COM_SW_CRTSCTS;
543 if (userbits & TIOCFLAG_MDMBUF)
544 driverbits |= COM_SW_MDMBUF;
545
546 sc->sc_swflags = driverbits;
547 break;
548 }
549 default:
550 return ENOTTY;
551 }
552
553 return 0;
554 }
555
556 int
557 comparam(tp, t)
558 struct tty *tp;
559 struct termios *t;
560 {
561 struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
562 int iobase = sc->sc_iobase;
563 int ospeed = comspeed(t->c_ospeed);
564 u_char cfcr;
565 tcflag_t oldcflag;
566 int s;
567
568 /* check requested parameters */
569 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
570 return EINVAL;
571
572 switch (t->c_cflag & CSIZE) {
573 case CS5:
574 cfcr = CFCR_5BITS;
575 break;
576 case CS6:
577 cfcr = CFCR_6BITS;
578 break;
579 case CS7:
580 cfcr = CFCR_7BITS;
581 break;
582 case CS8:
583 cfcr = CFCR_8BITS;
584 break;
585 }
586 if (t->c_cflag & PARENB) {
587 cfcr |= CFCR_PENAB;
588 if ((t->c_cflag & PARODD) == 0)
589 cfcr |= CFCR_PEVEN;
590 }
591 if (t->c_cflag & CSTOPB)
592 cfcr |= CFCR_STOPB;
593
594 s = spltty();
595
596 if (ospeed == 0)
597 outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_DTR);
598
599 /*
600 * Set the FIFO threshold based on the receive speed, if we are
601 * changing it.
602 */
603 if (tp->t_ispeed != t->c_ispeed) {
604 if (sc->sc_hwflags & COM_HW_FIFO)
605 outb(iobase + com_fifo,
606 FIFO_ENABLE |
607 (t->c_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
608 }
609
610 if (ospeed != 0) {
611 outb(iobase + com_cfcr, cfcr | CFCR_DLAB);
612 outb(iobase + com_dlbl, ospeed);
613 outb(iobase + com_dlbh, ospeed >> 8);
614 outb(iobase + com_cfcr, cfcr);
615 outb(iobase + com_mcr, sc->sc_mcr |= MCR_DTR);
616 } else
617 outb(iobase + com_cfcr, cfcr);
618
619 /* When not using CRTSCTS, RTS follows DTR. */
620 if ((t->c_cflag & CRTSCTS) == 0) {
621 if (sc->sc_mcr & MCR_DTR) {
622 if ((sc->sc_mcr & MCR_RTS) == 0)
623 outb(iobase + com_mcr, sc->sc_mcr |= MCR_RTS);
624 } else {
625 if (sc->sc_mcr & MCR_RTS)
626 outb(iobase + com_mcr, sc->sc_mcr &= ~MCR_RTS);
627 }
628 sc->sc_dtr = MCR_DTR | MCR_RTS;
629 } else
630 sc->sc_dtr = MCR_DTR;
631
632 /* and copy to tty */
633 tp->t_ispeed = t->c_ispeed;
634 tp->t_ospeed = t->c_ospeed;
635 oldcflag = tp->t_cflag;
636 tp->t_cflag = t->c_cflag;
637
638 /*
639 * If DCD is off and MDMBUF is changed, ask the tty layer if we should
640 * stop the device.
641 */
642 if ((sc->sc_msr & MSR_DCD) == 0 &&
643 (sc->sc_swflags & COM_SW_SOFTCAR) == 0 &&
644 (oldcflag & MDMBUF) != (tp->t_cflag & MDMBUF) &&
645 (*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
646 outb(iobase + com_mcr, sc->sc_mcr &= ~sc->sc_dtr);
647 }
648
649 splx(s);
650 return 0;
651 }
652
653 void
654 comstart(tp)
655 struct tty *tp;
656 {
657 struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
658 int iobase = sc->sc_iobase;
659 int s;
660
661 s = spltty();
662 if (tp->t_state & (TS_TTSTOP | TS_BUSY))
663 goto out;
664 if (tp->t_cflag & CRTSCTS && (sc->sc_mcr & MSR_CTS) == 0)
665 goto out;
666 if (tp->t_outq.c_cc <= tp->t_lowat) {
667 if (tp->t_state & TS_ASLEEP) {
668 tp->t_state &= ~TS_ASLEEP;
669 wakeup((caddr_t)&tp->t_outq);
670 }
671 selwakeup(&tp->t_wsel);
672 }
673 if (tp->t_outq.c_cc == 0)
674 goto out;
675 tp->t_state |= TS_BUSY;
676 if (sc->sc_hwflags & COM_HW_FIFO) {
677 u_char buffer[16], *cp = buffer;
678 int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
679 do {
680 outb(iobase + com_data, *cp++);
681 } while (--n);
682 } else
683 outb(iobase + com_data, getc(&tp->t_outq));
684 out:
685 splx(s);
686 }
687
688 /*
689 * Stop output on a line.
690 */
691 void
692 comstop(tp, flag)
693 struct tty *tp;
694 {
695 int s;
696
697 s = spltty();
698 if (tp->t_state & TS_BUSY)
699 if ((tp->t_state & TS_TTSTOP) == 0)
700 tp->t_state |= TS_FLUSH;
701 splx(s);
702 }
703
704 void
705 comdiag(arg)
706 void *arg;
707 {
708 struct com_softc *sc = arg;
709 int overflows, floods;
710 int s;
711
712 s = spltty();
713 sc->sc_errors = 0;
714 overflows = sc->sc_overflows;
715 sc->sc_overflows = 0;
716 floods = sc->sc_floods;
717 sc->sc_floods = 0;
718 splx(s);
719
720 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf overflow%s\n",
721 sc->sc_dev.dv_xname,
722 overflows, overflows == 1 ? "" : "s",
723 floods, floods == 1 ? "" : "s");
724 }
725
726 void
727 compoll(arg)
728 void *arg;
729 {
730 int unit;
731 struct com_softc *sc;
732 struct tty *tp;
733 register u_char *ibufp;
734 u_char *ibufend;
735 register int c;
736 int s;
737 static int lsrmap[8] = {
738 0, TTY_PE,
739 TTY_FE, TTY_PE|TTY_FE,
740 TTY_FE, TTY_PE|TTY_FE,
741 TTY_FE, TTY_PE|TTY_FE
742 };
743
744 s = spltty();
745 if (comevents == 0) {
746 splx(s);
747 goto out;
748 }
749 comevents = 0;
750 splx(s);
751
752 for (unit = 0; unit < comcd.cd_ndevs; unit++) {
753 sc = comcd.cd_devs[unit];
754 if (sc == 0 || sc->sc_ibufp == sc->sc_ibuf)
755 continue;
756
757 tp = sc->sc_tty;
758
759 s = spltty();
760
761 ibufp = sc->sc_ibuf;
762 ibufend = sc->sc_ibufp;
763
764 sc->sc_ibufp = sc->sc_ibuf = (ibufp == sc->sc_ibufs[0]) ?
765 sc->sc_ibufs[1] : sc->sc_ibufs[0];
766 sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
767 sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
768
769 if (tp == 0 || (tp->t_state & TS_ISOPEN) == 0) {
770 splx(s);
771 continue;
772 }
773
774 if ((tp->t_cflag & CRTSCTS) != 0 &&
775 (sc->sc_mcr & MCR_RTS) == 0)
776 outb(sc->sc_iobase + com_mcr, sc->sc_mcr |= MCR_RTS);
777
778 splx(s);
779
780 while (ibufp < ibufend) {
781 c = *ibufp++;
782 if (*ibufp & LSR_OE) {
783 sc->sc_overflows++;
784 if (sc->sc_errors++ == 0)
785 timeout(comdiag, sc, 60 * hz);
786 }
787 /* This is ugly, but fast. */
788 c |= lsrmap[(*ibufp++ & (LSR_BI|LSR_FE|LSR_PE)) >> 2];
789 (*linesw[tp->t_line].l_rint)(c, tp);
790 }
791 }
792
793 out:
794 timeout(compoll, NULL, 1);
795 }
796
797 int
798 comintr(arg)
799 void *arg;
800 {
801 struct com_softc *sc = arg;
802 int iobase = sc->sc_iobase;
803 struct tty *tp;
804 u_char lsr, data, msr, delta;
805
806 if (inb(iobase + com_iir) & IIR_NOPEND)
807 return (0);
808
809 tp = sc->sc_tty;
810
811 for (;;) {
812 lsr = inb(iobase + com_lsr);
813
814 if (lsr & LSR_RCV_MASK) {
815 register u_char *p = sc->sc_ibufp;
816
817 comevents = 1;
818 do {
819 #ifdef DDB
820 if ((lsr & LSR_BI) != 0 &&
821 sc->sc_dev.dv_unit == comconsole) {
822 Debugger();
823 goto next;
824 }
825 #endif
826 data = inb(iobase + com_data);
827 if (p >= sc->sc_ibufend) {
828 sc->sc_floods++;
829 if (sc->sc_errors++ == 0)
830 timeout(comdiag, sc, 60 * hz);
831 } else {
832 *p++ = data;
833 *p++ = lsr;
834 if (p == sc->sc_ibufhigh &&
835 (tp->t_cflag & CRTSCTS) != 0)
836 outb(iobase + com_mcr,
837 sc->sc_mcr &= ~MCR_RTS);
838 }
839 next:
840 lsr = inb(iobase + com_lsr);
841 } while (lsr & LSR_RCV_MASK);
842
843 sc->sc_ibufp = p;
844 }
845
846 if (lsr & LSR_TXRDY && (tp->t_state & TS_BUSY) != 0) {
847 tp->t_state &= ~TS_BUSY;
848 if (tp->t_state & TS_FLUSH)
849 tp->t_state &= ~TS_FLUSH;
850 else
851 if (tp->t_line)
852 (*linesw[tp->t_line].l_start)(tp);
853 else
854 comstart(tp);
855 }
856
857 msr = inb(iobase + com_msr);
858
859 if (msr != sc->sc_msr) {
860 delta = msr ^ sc->sc_msr;
861 sc->sc_msr = msr;
862 if ((delta & MSR_DCD) != 0 &&
863 (sc->sc_swflags & COM_SW_SOFTCAR) == 0 &&
864 (*linesw[tp->t_line].l_modem)(tp, (msr & MSR_DCD) != 0) == 0) {
865 outb(iobase + com_mcr, sc->sc_mcr &= ~sc->sc_dtr);
866 }
867 if ((delta & msr & MSR_CTS) != 0 &&
868 (tp->t_cflag & CRTSCTS) != 0) {
869 /* the line is up and we want to do rts/cts flow control */
870 (*linesw[tp->t_line].l_start)(tp);
871 }
872 }
873
874 if (inb(iobase + com_iir) & IIR_NOPEND)
875 return (1);
876 }
877 }
878
879 /*
880 * Following are all routines needed for COM to act as console
881 */
882 #include <dev/cons.h>
883
884 void
885 comcnprobe(cp)
886 struct consdev *cp;
887 {
888
889 if (!comprobe1(CONADDR)) {
890 cp->cn_pri = CN_DEAD;
891 return;
892 }
893
894 /* locate the major number */
895 for (commajor = 0; commajor < nchrdev; commajor++)
896 if (cdevsw[commajor].d_open == comopen)
897 break;
898
899 /* initialize required fields */
900 cp->cn_dev = makedev(commajor, CONUNIT);
901 #ifdef COMCONSOLE
902 cp->cn_pri = CN_REMOTE; /* Force a serial port console */
903 #else
904 cp->cn_pri = CN_NORMAL;
905 #endif
906 }
907
908 void
909 comcninit(cp)
910 struct consdev *cp;
911 {
912
913 cominit(CONUNIT, comdefaultrate);
914 comconsole = CONUNIT;
915 comconsinit = 0;
916 }
917
918 cominit(unit, rate)
919 int unit, rate;
920 {
921 int s = splhigh();
922 int iobase = CONADDR;
923 u_char stat;
924
925 outb(iobase + com_cfcr, CFCR_DLAB);
926 rate = comspeed(comdefaultrate);
927 outb(iobase + com_dlbl, rate);
928 outb(iobase + com_dlbh, rate >> 8);
929 outb(iobase + com_cfcr, CFCR_8BITS);
930 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
931 outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
932 stat = inb(iobase + com_iir);
933 splx(s);
934 }
935
936 comcngetc(dev)
937 dev_t dev;
938 {
939 int s = splhigh();
940 int iobase = CONADDR;
941 u_char stat, c;
942
943 while (((stat = inb(iobase + com_lsr)) & LSR_RXRDY) == 0)
944 ;
945 c = inb(iobase + com_data);
946 stat = inb(iobase + com_iir);
947 splx(s);
948 return c;
949 }
950
951 /*
952 * Console kernel output character routine.
953 */
954 void
955 comcnputc(dev, c)
956 dev_t dev;
957 int c;
958 {
959 int s = splhigh();
960 int iobase = CONADDR;
961 u_char stat;
962 register int timo;
963
964 #ifdef KGDB
965 if (dev != kgdb_dev)
966 #endif
967 if (comconsinit == 0) {
968 (void) cominit(COMUNIT(dev), comdefaultrate);
969 comconsinit = 1;
970 }
971 /* wait for any pending transmission to finish */
972 timo = 50000;
973 while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
974 ;
975 outb(iobase + com_data, c);
976 /* wait for this transmission to complete */
977 timo = 1500000;
978 while (((stat = inb(iobase + com_lsr)) & LSR_TXRDY) == 0 && --timo)
979 ;
980 /* clear any interrupts generated by this transmission */
981 stat = inb(iobase + com_iir);
982 splx(s);
983 }
984
985 void
986 comcnpollc(dev, on)
987 dev_t dev;
988 int on;
989 {
990
991 }
992