com.c revision 1.108 1 /* $NetBSD: com.c,v 1.108 1997/08/27 18:00:13 is Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995, 1996, 1997
5 * Charles M. Hannum. All rights reserved.
6 *
7 * Interrupt processing and hardware flow control partly based on code from
8 * Onno van der Linden and Gordon Ross.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Charles M. Hannum.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*
37 * Copyright (c) 1991 The Regents of the University of California.
38 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)com.c 7.5 (Berkeley) 5/16/91
69 */
70
71 /*
72 * COM driver, uses National Semiconductor NS16450/NS16550AF UART
73 */
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/ioctl.h>
77 #include <sys/select.h>
78 #include <sys/tty.h>
79 #include <sys/proc.h>
80 #include <sys/user.h>
81 #include <sys/conf.h>
82 #include <sys/file.h>
83 #include <sys/uio.h>
84 #include <sys/kernel.h>
85 #include <sys/syslog.h>
86 #include <sys/types.h>
87 #include <sys/device.h>
88
89 #include <machine/intr.h>
90 #include <machine/bus.h>
91
92 #include <dev/isa/comreg.h>
93 #include <dev/isa/comvar.h>
94 #include <dev/ic/ns16550reg.h>
95 #ifdef COM_HAYESP
96 #include <dev/ic/hayespreg.h>
97 #endif
98 #define com_lcr com_cfcr
99 #include <dev/cons.h>
100
101 #include "com.h"
102
103 #ifdef COM_HAYESP
104 int comprobeHAYESP __P((bus_space_handle_t hayespioh, struct com_softc *sc));
105 #endif
106
107 #if defined(DDB) || defined(KGDB)
108 static void com_enable_debugport __P((struct com_softc *));
109 #endif
110 void com_attach_subr __P((struct com_softc *sc));
111 void comdiag __P((void *));
112 int comspeed __P((long));
113 static u_char cflag2lcr __P((tcflag_t));
114 int comparam __P((struct tty *, struct termios *));
115 void comstart __P((struct tty *));
116 void comstop __P((struct tty *, int));
117 #ifdef __GENERIC_SOFT_INTERRUPTS
118 void comsoft __P((void *));
119 #else
120 #ifndef alpha
121 void comsoft __P((void));
122 #else
123 void comsoft __P((void *));
124 #endif
125 #endif
126 int comhwiflow __P((struct tty *, int));
127
128 void com_loadchannelregs __P((struct com_softc *));
129 void com_hwiflow __P((struct com_softc *));
130 void com_break __P((struct com_softc *, int));
131 void com_modem __P((struct com_softc *, int));
132 void com_iflush __P((struct com_softc *));
133
134 int com_common_getc __P((bus_space_tag_t, bus_space_handle_t));
135 void com_common_putc __P((bus_space_tag_t, bus_space_handle_t, int));
136
137 /* XXX: These belong elsewhere */
138 cdev_decl(com);
139 bdev_decl(com);
140
141 int comcngetc __P((dev_t));
142 void comcnputc __P((dev_t, int));
143 void comcnpollc __P((dev_t, int));
144
145 #define integrate static inline
146 integrate void comrxint __P((struct com_softc *, struct tty *));
147 integrate void comtxint __P((struct com_softc *, struct tty *));
148 integrate void commsrint __P((struct com_softc *, struct tty *));
149 integrate void com_schedrx __P((struct com_softc *));
150
151 struct cfdriver com_cd = {
152 NULL, "com", DV_TTY
153 };
154
155 static int comconsaddr;
156 static bus_space_tag_t comconstag;
157 static bus_space_handle_t comconsioh;
158 static int comconsattached;
159 static int comconsrate;
160 static tcflag_t comconscflag;
161
162 #ifndef __GENERIC_SOFT_INTERRUPTS
163 #ifdef alpha
164 volatile int com_softintr_scheduled;
165 #endif
166 #endif
167
168 #ifdef KGDB
169 #include <sys/kgdb.h>
170
171 static int com_kgdb_addr;
172 static bus_space_tag_t com_kgdb_iot;
173 static bus_space_handle_t com_kgdb_ioh;
174 static int com_kgdb_attached;
175
176 int com_kgdb_getc __P((void *));
177 void com_kgdb_putc __P((void *, int));
178 #endif /* KGDB */
179
180 #define COMUNIT(x) (minor(x))
181
182 int
183 comspeed(speed)
184 long speed;
185 {
186 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
187
188 int x, err;
189
190 #if 0
191 if (speed == 0)
192 return (0);
193 #endif
194 if (speed <= 0)
195 return (-1);
196 x = divrnd((COM_FREQ / 16), speed);
197 if (x <= 0)
198 return (-1);
199 err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
200 if (err < 0)
201 err = -err;
202 if (err > COM_TOLERANCE)
203 return (-1);
204 return (x);
205
206 #undef divrnd(n, q)
207 }
208
209 #ifdef COM_DEBUG
210 int com_debug = 0;
211
212 void comstatus __P((struct com_softc *, char *));
213 void
214 comstatus(sc, str)
215 struct com_softc *sc;
216 char *str;
217 {
218 struct tty *tp = sc->sc_tty;
219
220 printf("%s: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n",
221 sc->sc_dev.dv_xname, str,
222 ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
223 ISSET(sc->sc_msr, MSR_DCD) ? "+" : "-",
224 ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
225 ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-",
226 sc->sc_tx_stopped ? "+" : "-");
227
228 printf("%s: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n",
229 sc->sc_dev.dv_xname, str,
230 ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
231 ISSET(sc->sc_msr, MSR_CTS) ? "+" : "-",
232 ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
233 ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-",
234 sc->sc_rx_flags);
235 }
236 #endif
237
238 int
239 comprobe1(iot, ioh, iobase)
240 bus_space_tag_t iot;
241 bus_space_handle_t ioh;
242 int iobase;
243 {
244
245 /* force access to id reg */
246 bus_space_write_1(iot, ioh, com_lcr, 0);
247 bus_space_write_1(iot, ioh, com_iir, 0);
248 if (bus_space_read_1(iot, ioh, com_iir) & 0x38)
249 return (0);
250
251 return (1);
252 }
253
254 #ifdef COM_HAYESP
255 int
256 comprobeHAYESP(hayespioh, sc)
257 bus_space_handle_t hayespioh;
258 struct com_softc *sc;
259 {
260 char val, dips;
261 int combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
262 bus_space_tag_t iot = sc->sc_iot;
263
264 /*
265 * Hayes ESP cards have two iobases. One is for compatibility with
266 * 16550 serial chips, and at the same ISA PC base addresses. The
267 * other is for ESP-specific enhanced features, and lies at a
268 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
269 */
270
271 /* Test for ESP signature */
272 if ((bus_space_read_1(iot, hayespioh, 0) & 0xf3) == 0)
273 return (0);
274
275 /*
276 * ESP is present at ESP enhanced base address; unknown com port
277 */
278
279 /* Get the dip-switch configurations */
280 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETDIPS);
281 dips = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1);
282
283 /* Determine which com port this ESP card services: bits 0,1 of */
284 /* dips is the port # (0-3); combaselist[val] is the com_iobase */
285 if (sc->sc_iobase != combaselist[dips & 0x03])
286 return (0);
287
288 printf(": ESP");
289
290 /* Check ESP Self Test bits. */
291 /* Check for ESP version 2.0: bits 4,5,6 == 010 */
292 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETTEST);
293 val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1); /* Clear reg1 */
294 val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS2);
295 if ((val & 0x70) < 0x20) {
296 printf("-old (%o)", val & 0x70);
297 /* we do not support the necessary features */
298 return (0);
299 }
300
301 /* Check for ability to emulate 16550: bit 8 == 1 */
302 if ((dips & 0x80) == 0) {
303 printf(" slave");
304 /* XXX Does slave really mean no 16550 support?? */
305 return (0);
306 }
307
308 /*
309 * If we made it this far, we are a full-featured ESP v2.0 (or
310 * better), at the correct com port address.
311 */
312
313 SET(sc->sc_hwflags, COM_HW_HAYESP);
314 printf(", 1024 byte fifo\n");
315 return (1);
316 }
317 #endif
318
319 #if defined(DDB) || defined(KGDB)
320 static void
321 com_enable_debugport(sc)
322 struct com_softc *sc;
323 {
324 int s;
325
326 /* Turn on line break interrupt, set carrier. */
327 s = splserial();
328 sc->sc_ier = IER_ERXRDY;
329 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
330 SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
331 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
332 splx(s);
333 }
334 #endif
335
336 void
337 com_attach_subr(sc)
338 struct com_softc *sc;
339 {
340 int iobase = sc->sc_iobase;
341 bus_space_tag_t iot = sc->sc_iot;
342 bus_space_handle_t ioh = sc->sc_ioh;
343 #ifdef COM_HAYESP
344 int hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
345 int *hayespp;
346 #endif
347
348 if (iot == comconstag && iobase == comconsaddr) {
349 comconsattached = 1;
350
351 /* Make sure the console is always "hardwired". */
352 delay(1000); /* wait for output to finish */
353 SET(sc->sc_hwflags, COM_HW_CONSOLE);
354 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
355 }
356
357 #ifdef COM_HAYESP
358 /* Look for a Hayes ESP board. */
359 for (hayespp = hayesp_ports; *hayespp != 0; hayespp++) {
360 bus_space_handle_t hayespioh;
361
362 #define HAYESP_NPORTS 8 /* XXX XXX XXX ??? ??? ??? */
363 if (bus_space_map(iot, *hayespp, HAYESP_NPORTS, 0, &hayespioh))
364 continue;
365 if (comprobeHAYESP(hayespioh, sc)) {
366 sc->sc_hayespioh = hayespioh;
367 sc->sc_fifolen = 1024;
368
369 /* Set 16550 compatibility mode */
370 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_SETMODE);
371 bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
372 HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
373 HAYESP_MODE_SCALE);
374
375 /* Set RTS/CTS flow control */
376 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_SETFLOWTYPE);
377 bus_space_write_1(iot, hayespioh, HAYESP_CMD2, HAYESP_FLOW_RTS);
378 bus_space_write_1(iot, hayespioh, HAYESP_CMD2, HAYESP_FLOW_CTS);
379
380 /* Set flow control levels */
381 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_SETRXFLOW);
382 bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
383 HAYESP_HIBYTE(HAYESP_RXHIWMARK));
384 bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
385 HAYESP_LOBYTE(HAYESP_RXHIWMARK));
386 bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
387 HAYESP_HIBYTE(HAYESP_RXLOWMARK));
388 bus_space_write_1(iot, hayespioh, HAYESP_CMD2,
389 HAYESP_LOBYTE(HAYESP_RXLOWMARK));
390
391 break;
392 }
393 bus_space_unmap(iot, hayespioh, HAYESP_NPORTS);
394 }
395 /* No ESP; look for other things. */
396 if (*hayespp == 0) {
397 #endif
398
399 sc->sc_fifolen = 1;
400 /* look for a NS 16550AF UART with FIFOs */
401 bus_space_write_1(iot, ioh, com_fifo,
402 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
403 delay(100);
404 if (ISSET(bus_space_read_1(iot, ioh, com_iir), IIR_FIFO_MASK)
405 == IIR_FIFO_MASK)
406 if (ISSET(bus_space_read_1(iot, ioh, com_fifo), FIFO_TRIGGER_14)
407 == FIFO_TRIGGER_14) {
408 SET(sc->sc_hwflags, COM_HW_FIFO);
409 printf(": ns16550a, working fifo\n");
410 sc->sc_fifolen = 16;
411 } else
412 printf(": ns16550, broken fifo\n");
413 else
414 printf(": ns8250 or ns16450, no fifo\n");
415 bus_space_write_1(iot, ioh, com_fifo, 0);
416 #ifdef COM_HAYESP
417 }
418 #endif
419
420 if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
421 SET(sc->sc_mcr, MCR_IENABLE);
422
423 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
424 int maj;
425
426 /* locate the major number */
427 for (maj = 0; maj < nchrdev; maj++)
428 if (cdevsw[maj].d_open == comopen)
429 break;
430
431 cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
432 #ifdef DDB
433 com_enable_debugport(sc);
434 #endif
435 printf("%s: console\n", sc->sc_dev.dv_xname);
436 }
437
438 #ifdef KGDB
439 /*
440 * Allow kgdb to "take over" this port. If this is
441 * the kgdb device, it has exclusive use.
442 */
443 if (iot == com_kgdb_iot && iobase == com_kgdb_addr) {
444 com_kgdb_attached = 1;
445
446 SET(sc->sc_hwflags, COM_HW_KGDB);
447 com_enable_debugport(sc);
448 printf("%s: kgdb\n", sc->sc_dev.dv_xname);
449 }
450 #endif
451
452 #ifdef __GENERIC_SOFT_INTERRUPTS
453 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, comsoft, sc);
454 #endif
455 }
456
457 int
458 comopen(dev, flag, mode, p)
459 dev_t dev;
460 int flag, mode;
461 struct proc *p;
462 {
463 int unit = COMUNIT(dev);
464 struct com_softc *sc;
465 struct tty *tp;
466 int s, s2;
467 int error = 0;
468
469 if (unit >= com_cd.cd_ndevs)
470 return (ENXIO);
471 sc = com_cd.cd_devs[unit];
472 if (!sc)
473 return (ENXIO);
474
475 #ifdef KGDB
476 /*
477 * If this is the kgdb port, no other use is permitted.
478 */
479 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
480 return (EBUSY);
481 #endif
482
483 if (!sc->sc_tty) {
484 tp = sc->sc_tty = ttymalloc();
485 tty_attach(tp);
486 } else
487 tp = sc->sc_tty;
488
489 if (ISSET(tp->t_state, TS_ISOPEN) &&
490 ISSET(tp->t_state, TS_XCLUDE) &&
491 p->p_ucred->cr_uid != 0)
492 return (EBUSY);
493
494 s = spltty();
495
496 /* We need to set this early for the benefit of comsoft(). */
497 SET(tp->t_state, TS_WOPEN);
498
499 /*
500 * Do the following iff this is a first open.
501 */
502 if (!ISSET(tp->t_state, TS_ISOPEN)) {
503 struct termios t;
504
505 /* Turn on interrupts. */
506 sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
507 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
508
509 /* Fetch the current modem control status, needed later. */
510 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
511
512 /* Add some entry points needed by the tty layer. */
513 tp->t_oproc = comstart;
514 tp->t_param = comparam;
515 tp->t_hwiflow = comhwiflow;
516 tp->t_dev = dev;
517
518 /*
519 * Initialize the termios status to the defaults. Add in the
520 * sticky bits from TIOCSFLAGS.
521 */
522 t.c_ispeed = 0;
523 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
524 t.c_ospeed = comconsrate;
525 t.c_cflag = comconscflag;
526 } else {
527 t.c_ospeed = TTYDEF_SPEED;
528 t.c_cflag = TTYDEF_CFLAG;
529 }
530 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
531 SET(t.c_cflag, CLOCAL);
532 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
533 SET(t.c_cflag, CRTSCTS);
534 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
535 SET(t.c_cflag, MDMBUF);
536 tp->t_iflag = TTYDEF_IFLAG;
537 tp->t_oflag = TTYDEF_OFLAG;
538 tp->t_lflag = TTYDEF_LFLAG;
539 ttychars(tp);
540 (void) comparam(tp, &t);
541 ttsetwater(tp);
542
543 s2 = splserial();
544
545 /*
546 * Turn on DTR. We must always do this, even if carrier is not
547 * present, because otherwise we'd have to use TIOCSDTR
548 * immediately after setting CLOCAL. We will drop DTR only on
549 * the next high-low transition of DCD, or by explicit request.
550 */
551 com_modem(sc, 1);
552
553 /* Clear the input ring, and unblock. */
554 sc->sc_rbput = sc->sc_rbget = 0;
555 sc->sc_rbavail = RXBUFSIZE;
556 com_iflush(sc);
557 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
558 com_hwiflow(sc);
559
560 #ifdef COM_DEBUG
561 if (com_debug)
562 comstatus(sc, "comopen ");
563 #endif
564
565 splx(s2);
566 }
567 error = 0;
568
569 /* If we're doing a blocking open... */
570 if (!ISSET(flag, O_NONBLOCK))
571 /* ...then wait for carrier. */
572 while (!ISSET(tp->t_state, TS_CARR_ON) &&
573 !ISSET(tp->t_cflag, CLOCAL | MDMBUF)) {
574 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
575 ttopen, 0);
576 if (error) {
577 /*
578 * If the open was interrupted and nobody
579 * else has the device open, then hang up.
580 */
581 if (!ISSET(tp->t_state, TS_ISOPEN)) {
582 com_modem(sc, 0);
583 CLR(tp->t_state, TS_WOPEN);
584 ttwakeup(tp);
585 }
586 break;
587 }
588 SET(tp->t_state, TS_WOPEN);
589 }
590
591 splx(s);
592 if (error == 0)
593 error = (*linesw[tp->t_line].l_open)(dev, tp);
594 return (error);
595 }
596
597 int
598 comclose(dev, flag, mode, p)
599 dev_t dev;
600 int flag, mode;
601 struct proc *p;
602 {
603 int unit = COMUNIT(dev);
604 struct com_softc *sc = com_cd.cd_devs[unit];
605 struct tty *tp = sc->sc_tty;
606 int s;
607
608 /* XXX This is for cons.c. */
609 if (!ISSET(tp->t_state, TS_ISOPEN))
610 return (0);
611
612 (*linesw[tp->t_line].l_close)(tp, flag);
613 ttyclose(tp);
614
615 /* If we were asserting flow control, then deassert it. */
616 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
617 com_hwiflow(sc);
618 /* Clear any break condition set with TIOCSBRK. */
619 com_break(sc, 0);
620 /*
621 * Hang up if necessary. Wait a bit, so the other side has time to
622 * notice even if we immediately open the port again.
623 */
624 if (ISSET(tp->t_cflag, HUPCL)) {
625 com_modem(sc, 0);
626 (void) tsleep(sc, TTIPRI, ttclos, hz);
627 }
628
629 s = splserial();
630 /* Turn off interrupts. */
631 #ifdef DDB
632 if(ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
633 sc->sc_ier = IER_ERXRDY; /* interrupt on break */
634 else
635 #else
636 sc->sc_ier = 0;
637 #endif
638 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
639 splx(s);
640
641 return (0);
642 }
643
644 int
645 comread(dev, uio, flag)
646 dev_t dev;
647 struct uio *uio;
648 int flag;
649 {
650 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
651 struct tty *tp = sc->sc_tty;
652
653 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
654 }
655
656 int
657 comwrite(dev, uio, flag)
658 dev_t dev;
659 struct uio *uio;
660 int flag;
661 {
662 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
663 struct tty *tp = sc->sc_tty;
664
665 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
666 }
667
668 struct tty *
669 comtty(dev)
670 dev_t dev;
671 {
672 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
673 struct tty *tp = sc->sc_tty;
674
675 return (tp);
676 }
677
678 int
679 comioctl(dev, cmd, data, flag, p)
680 dev_t dev;
681 u_long cmd;
682 caddr_t data;
683 int flag;
684 struct proc *p;
685 {
686 int unit = COMUNIT(dev);
687 struct com_softc *sc = com_cd.cd_devs[unit];
688 struct tty *tp = sc->sc_tty;
689 int error;
690
691 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
692 if (error >= 0)
693 return (error);
694
695 error = ttioctl(tp, cmd, data, flag, p);
696 if (error >= 0)
697 return (error);
698
699 switch (cmd) {
700 case TIOCSBRK:
701 com_break(sc, 1);
702 break;
703
704 case TIOCCBRK:
705 com_break(sc, 0);
706 break;
707
708 case TIOCSDTR:
709 com_modem(sc, 1);
710 break;
711
712 case TIOCCDTR:
713 com_modem(sc, 0);
714 break;
715
716 case TIOCGFLAGS:
717 *(int *)data = sc->sc_swflags;
718 break;
719
720 case TIOCSFLAGS:
721 error = suser(p->p_ucred, &p->p_acflag);
722 if (error)
723 return (error);
724 sc->sc_swflags = *(int *)data;
725 break;
726
727 case TIOCMSET:
728 case TIOCMBIS:
729 case TIOCMBIC:
730 case TIOCMGET:
731 default:
732 return (ENOTTY);
733 }
734
735 #ifdef COM_DEBUG
736 if (com_debug)
737 comstatus(sc, "comioctl ");
738 #endif
739
740 return (0);
741 }
742
743 integrate void
744 com_schedrx(sc)
745 struct com_softc *sc;
746 {
747
748 sc->sc_rx_ready = 1;
749
750 /* Wake up the poller. */
751 #ifdef __GENERIC_SOFT_INTERRUPTS
752 softintr_schedule(sc->sc_si);
753 #else
754 #ifndef alpha
755 setsoftserial();
756 #else
757 if (!com_softintr_scheduled) {
758 com_softintr_scheduled = 1;
759 timeout(comsoft, NULL, 1);
760 }
761 #endif
762 #endif
763 }
764
765 void
766 com_break(sc, onoff)
767 struct com_softc *sc;
768 int onoff;
769 {
770 int s;
771
772 s = splserial();
773 if (onoff)
774 SET(sc->sc_lcr, LCR_SBREAK);
775 else
776 CLR(sc->sc_lcr, LCR_SBREAK);
777
778 if (!sc->sc_heldchange) {
779 if (sc->sc_tx_busy) {
780 sc->sc_heldtbc = sc->sc_tbc;
781 sc->sc_tbc = 0;
782 sc->sc_heldchange = 1;
783 } else
784 com_loadchannelregs(sc);
785 }
786 splx(s);
787 }
788
789 void
790 com_modem(sc, onoff)
791 struct com_softc *sc;
792 int onoff;
793 {
794 int s;
795
796 s = splserial();
797 if (onoff)
798 SET(sc->sc_mcr, sc->sc_mcr_dtr);
799 else
800 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
801
802 if (!sc->sc_heldchange) {
803 if (sc->sc_tx_busy) {
804 sc->sc_heldtbc = sc->sc_tbc;
805 sc->sc_tbc = 0;
806 sc->sc_heldchange = 1;
807 } else
808 com_loadchannelregs(sc);
809 }
810 splx(s);
811 }
812
813 static u_char
814 cflag2lcr(cflag)
815 tcflag_t cflag;
816 {
817 u_char lcr = 0;
818
819 switch (ISSET(cflag, CSIZE)) {
820 case CS5:
821 SET(lcr, LCR_5BITS);
822 break;
823 case CS6:
824 SET(lcr, LCR_6BITS);
825 break;
826 case CS7:
827 SET(lcr, LCR_7BITS);
828 break;
829 case CS8:
830 SET(lcr, LCR_8BITS);
831 break;
832 }
833 if (ISSET(cflag, PARENB)) {
834 SET(lcr, LCR_PENAB);
835 if (!ISSET(cflag, PARODD))
836 SET(lcr, LCR_PEVEN);
837 }
838 if (ISSET(cflag, CSTOPB))
839 SET(lcr, LCR_STOPB);
840
841 return(lcr);
842 }
843
844 int
845 comparam(tp, t)
846 struct tty *tp;
847 struct termios *t;
848 {
849 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
850 int ospeed = comspeed(t->c_ospeed);
851 u_char lcr;
852 int s;
853
854 /* check requested parameters */
855 if (ospeed < 0)
856 return (EINVAL);
857 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
858 return (EINVAL);
859
860 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
861
862 s = splserial();
863
864 sc->sc_lcr = lcr;
865
866 /*
867 * For the console, always force CLOCAL and !HUPCL, so that the port
868 * is always active.
869 */
870 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
871 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
872 SET(t->c_cflag, CLOCAL);
873 CLR(t->c_cflag, HUPCL);
874 }
875
876 /*
877 * If we're not in a mode that assumes a connection is present, then
878 * ignore carrier changes.
879 */
880 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
881 sc->sc_msr_dcd = 0;
882 else
883 sc->sc_msr_dcd = MSR_DCD;
884 /*
885 * Set the flow control pins depending on the current flow control
886 * mode.
887 */
888 if (ISSET(t->c_cflag, CRTSCTS)) {
889 sc->sc_mcr_dtr = MCR_DTR;
890 sc->sc_mcr_rts = MCR_RTS;
891 sc->sc_msr_cts = MSR_CTS;
892 sc->sc_r_hiwat = RXHIWAT;
893 sc->sc_r_lowat = RXLOWAT;
894 } else if (ISSET(t->c_cflag, MDMBUF)) {
895 /*
896 * For DTR/DCD flow control, make sure we don't toggle DTR for
897 * carrier detection.
898 */
899 sc->sc_mcr_dtr = 0;
900 sc->sc_mcr_rts = MCR_DTR;
901 sc->sc_msr_cts = MSR_DCD;
902 sc->sc_r_hiwat = RXHIWAT;
903 sc->sc_r_lowat = RXLOWAT;
904 } else {
905 /*
906 * If no flow control, then always set RTS. This will make
907 * the other side happy if it mistakenly thinks we're doing
908 * RTS/CTS flow control.
909 */
910 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
911 sc->sc_mcr_rts = 0;
912 sc->sc_msr_cts = 0;
913 sc->sc_r_hiwat = 0;
914 sc->sc_r_lowat = 0;
915 if (ISSET(sc->sc_mcr, MCR_DTR))
916 SET(sc->sc_mcr, MCR_RTS);
917 else
918 CLR(sc->sc_mcr, MCR_RTS);
919 }
920 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
921
922 #if 0
923 if (ospeed == 0)
924 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
925 else
926 SET(sc->sc_mcr, sc->sc_mcr_dtr);
927 #endif
928
929 sc->sc_dlbl = ospeed;
930 sc->sc_dlbh = ospeed >> 8;
931
932 /*
933 * Set the FIFO threshold based on the receive speed.
934 *
935 * * If it's a low speed, it's probably a mouse or some other
936 * interactive device, so set the threshold low.
937 * * If it's a high speed, trim the trigger level down to prevent
938 * overflows.
939 * * Otherwise set it a bit higher.
940 */
941 if (ISSET(sc->sc_hwflags, COM_HW_HAYESP))
942 sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
943 else if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
944 sc->sc_fifo = FIFO_ENABLE |
945 (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
946 t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
947 else
948 sc->sc_fifo = 0;
949
950 /* and copy to tty */
951 tp->t_ispeed = 0;
952 tp->t_ospeed = t->c_ospeed;
953 tp->t_cflag = t->c_cflag;
954
955 if (!sc->sc_heldchange) {
956 if (sc->sc_tx_busy) {
957 sc->sc_heldtbc = sc->sc_tbc;
958 sc->sc_tbc = 0;
959 sc->sc_heldchange = 1;
960 } else
961 com_loadchannelregs(sc);
962 }
963
964 splx(s);
965
966 /*
967 * Update the tty layer's idea of the carrier bit, in case we changed
968 * CLOCAL or MDMBUF. We don't hang up here; we only do that if we
969 * lose carrier while carrier detection is on.
970 */
971 (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
972
973 #ifdef COM_DEBUG
974 if (com_debug)
975 comstatus(sc, "comparam ");
976 #endif
977
978 /* Block or unblock as needed. */
979 if (!ISSET(t->c_cflag, CHWFLOW)) {
980 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
981 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
982 com_schedrx(sc);
983 }
984 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
985 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
986 com_hwiflow(sc);
987 }
988 if (sc->sc_tx_stopped) {
989 sc->sc_tx_stopped = 0;
990 comstart(tp);
991 }
992 } else {
993 /* XXXXX FIX ME */
994 #if 0
995 commsrint(sc, tp);
996 #endif
997 }
998
999 return (0);
1000 }
1001
1002 void
1003 com_iflush(sc)
1004 struct com_softc *sc;
1005 {
1006 bus_space_tag_t iot = sc->sc_iot;
1007 bus_space_handle_t ioh = sc->sc_ioh;
1008
1009 /* flush any pending I/O */
1010 while (ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
1011 (void) bus_space_read_1(iot, ioh, com_data);
1012 }
1013
1014 void
1015 com_loadchannelregs(sc)
1016 struct com_softc *sc;
1017 {
1018 bus_space_tag_t iot = sc->sc_iot;
1019 bus_space_handle_t ioh = sc->sc_ioh;
1020
1021 /* XXXXX necessary? */
1022 com_iflush(sc);
1023
1024 bus_space_write_1(iot, ioh, com_ier, 0);
1025
1026 bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr | LCR_DLAB);
1027 bus_space_write_1(iot, ioh, com_dlbl, sc->sc_dlbl);
1028 bus_space_write_1(iot, ioh, com_dlbh, sc->sc_dlbh);
1029 bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
1030 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active = sc->sc_mcr);
1031 bus_space_write_1(iot, ioh, com_fifo, sc->sc_fifo);
1032
1033 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1034 }
1035
1036 int
1037 comhwiflow(tp, block)
1038 struct tty *tp;
1039 int block;
1040 {
1041 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1042 int s;
1043
1044 if (sc->sc_mcr_rts == 0)
1045 return (0);
1046
1047 s = splserial();
1048 if (block) {
1049 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1050 SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1051 com_hwiflow(sc);
1052 }
1053 } else {
1054 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1055 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1056 com_schedrx(sc);
1057 }
1058 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1059 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1060 com_hwiflow(sc);
1061 }
1062 }
1063 splx(s);
1064 return (1);
1065 }
1066
1067 /*
1068 * (un)block input via hw flowcontrol
1069 */
1070 void
1071 com_hwiflow(sc)
1072 struct com_softc *sc;
1073 {
1074 bus_space_tag_t iot = sc->sc_iot;
1075 bus_space_handle_t ioh = sc->sc_ioh;
1076
1077 if (sc->sc_mcr_rts == 0)
1078 return;
1079
1080 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1081 CLR(sc->sc_mcr, sc->sc_mcr_rts);
1082 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
1083 } else {
1084 SET(sc->sc_mcr, sc->sc_mcr_rts);
1085 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
1086 }
1087 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
1088 }
1089
1090
1091 void
1092 comstart(tp)
1093 struct tty *tp;
1094 {
1095 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1096 bus_space_tag_t iot = sc->sc_iot;
1097 bus_space_handle_t ioh = sc->sc_ioh;
1098 int s;
1099
1100 s = spltty();
1101 if (ISSET(tp->t_state, TS_BUSY))
1102 goto out;
1103 if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP))
1104 goto stopped;
1105
1106 if (sc->sc_tx_stopped)
1107 goto stopped;
1108
1109 if (tp->t_outq.c_cc <= tp->t_lowat) {
1110 if (ISSET(tp->t_state, TS_ASLEEP)) {
1111 CLR(tp->t_state, TS_ASLEEP);
1112 wakeup(&tp->t_outq);
1113 }
1114 selwakeup(&tp->t_wsel);
1115 if (tp->t_outq.c_cc == 0)
1116 goto stopped;
1117 }
1118
1119 /* Grab the first contiguous region of buffer space. */
1120 {
1121 u_char *tba;
1122 int tbc;
1123
1124 tba = tp->t_outq.c_cf;
1125 tbc = ndqb(&tp->t_outq, 0);
1126
1127 (void)splserial();
1128
1129 sc->sc_tba = tba;
1130 sc->sc_tbc = tbc;
1131 }
1132
1133 SET(tp->t_state, TS_BUSY);
1134 sc->sc_tx_busy = 1;
1135
1136 /* Enable transmit completion interrupts if necessary. */
1137 if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
1138 SET(sc->sc_ier, IER_ETXRDY);
1139 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1140 }
1141
1142 /* Output the first chunk of the contiguous buffer. */
1143 {
1144 int n;
1145
1146 n = sc->sc_fifolen;
1147 if (n > sc->sc_tbc)
1148 n = sc->sc_tbc;
1149 bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
1150 sc->sc_tbc -= n;
1151 sc->sc_tba += n;
1152 }
1153 splx(s);
1154 return;
1155
1156 stopped:
1157 /* Disable transmit completion interrupts if necessary. */
1158 if (ISSET(sc->sc_ier, IER_ETXRDY)) {
1159 CLR(sc->sc_ier, IER_ETXRDY);
1160 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1161 }
1162 out:
1163 splx(s);
1164 return;
1165 }
1166
1167 /*
1168 * Stop output on a line.
1169 */
1170 void
1171 comstop(tp, flag)
1172 struct tty *tp;
1173 int flag;
1174 {
1175 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1176 int s;
1177
1178 s = splserial();
1179 if (ISSET(tp->t_state, TS_BUSY)) {
1180 /* Stop transmitting at the next chunk. */
1181 sc->sc_tbc = 0;
1182 sc->sc_heldtbc = 0;
1183 if (!ISSET(tp->t_state, TS_TTSTOP))
1184 SET(tp->t_state, TS_FLUSH);
1185 }
1186 splx(s);
1187 }
1188
1189 void
1190 comdiag(arg)
1191 void *arg;
1192 {
1193 struct com_softc *sc = arg;
1194 int overflows, floods;
1195 int s;
1196
1197 s = splserial();
1198 overflows = sc->sc_overflows;
1199 sc->sc_overflows = 0;
1200 floods = sc->sc_floods;
1201 sc->sc_floods = 0;
1202 sc->sc_errors = 0;
1203 splx(s);
1204
1205 log(LOG_WARNING,
1206 "%s: %d silo overflow%s, %d ibuf flood%s\n",
1207 sc->sc_dev.dv_xname,
1208 overflows, overflows == 1 ? "" : "s",
1209 floods, floods == 1 ? "" : "s");
1210 }
1211
1212 integrate void
1213 comrxint(sc, tp)
1214 struct com_softc *sc;
1215 struct tty *tp;
1216 {
1217 u_int get, cc, scc;
1218 int code;
1219 u_char lsr;
1220 int s;
1221 static int lsrmap[8] = {
1222 0, TTY_PE,
1223 TTY_FE, TTY_PE|TTY_FE,
1224 TTY_FE, TTY_PE|TTY_FE,
1225 TTY_FE, TTY_PE|TTY_FE
1226 };
1227
1228 get = sc->sc_rbget;
1229 scc = cc = RXBUFSIZE - sc->sc_rbavail;
1230
1231 if (cc == RXBUFSIZE) {
1232 sc->sc_floods++;
1233 if (sc->sc_errors++ == 0)
1234 timeout(comdiag, sc, 60 * hz);
1235 }
1236
1237 while (cc) {
1238 lsr = sc->sc_lbuf[get];
1239 if (ISSET(lsr, LSR_OE)) {
1240 sc->sc_overflows++;
1241 if (sc->sc_errors++ == 0)
1242 timeout(comdiag, sc, 60 * hz);
1243 }
1244 code = sc->sc_rbuf[get] |
1245 lsrmap[(lsr & (LSR_BI|LSR_FE|LSR_PE)) >> 2];
1246 if ((*linesw[tp->t_line].l_rint)(code, tp) == -1) {
1247 /*
1248 * The line discipline's buffer is out of space.
1249 */
1250 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1251 /*
1252 * We're either not using flow control, or the
1253 * line discipline didn't tell us to block for
1254 * some reason. Either way, we have no way to
1255 * know when there's more space available, so
1256 * just drop the rest of the data.
1257 */
1258 get = (get + cc) & RXBUFMASK;
1259 cc = 0;
1260 } else {
1261 /*
1262 * Don't schedule any more receive processing
1263 * until the line discipline tells us there's
1264 * space available (through comhwiflow()).
1265 * Leave the rest of the data in the input
1266 * buffer.
1267 */
1268 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1269 }
1270 break;
1271 }
1272 get = (get + 1) & RXBUFMASK;
1273 cc--;
1274 }
1275
1276 if (cc != scc) {
1277 sc->sc_rbget = get;
1278 s = splserial();
1279 cc = sc->sc_rbavail += scc - cc;
1280 /* Buffers should be ok again, release possible block. */
1281 if (cc >= sc->sc_r_lowat) {
1282 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1283 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1284 SET(sc->sc_ier, IER_ERXRDY);
1285 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
1286 }
1287 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1288 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1289 com_hwiflow(sc);
1290 }
1291 }
1292 splx(s);
1293 }
1294 }
1295
1296 integrate void
1297 comtxint(sc, tp)
1298 struct com_softc *sc;
1299 struct tty *tp;
1300 {
1301
1302 CLR(tp->t_state, TS_BUSY);
1303 if (ISSET(tp->t_state, TS_FLUSH))
1304 CLR(tp->t_state, TS_FLUSH);
1305 else
1306 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1307 (*linesw[tp->t_line].l_start)(tp);
1308 }
1309
1310 integrate void
1311 commsrint(sc, tp)
1312 struct com_softc *sc;
1313 struct tty *tp;
1314 {
1315 u_char msr, delta;
1316 int s;
1317
1318 s = splserial();
1319 msr = sc->sc_msr;
1320 delta = sc->sc_msr_delta;
1321 sc->sc_msr_delta = 0;
1322 splx(s);
1323
1324 if (ISSET(delta, sc->sc_msr_dcd)) {
1325 /*
1326 * Inform the tty layer that carrier detect changed.
1327 */
1328 (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD));
1329 }
1330
1331 if (ISSET(delta, sc->sc_msr_cts)) {
1332 /* Block or unblock output according to flow control. */
1333 if (ISSET(msr, sc->sc_msr_cts)) {
1334 sc->sc_tx_stopped = 0;
1335 (*linesw[tp->t_line].l_start)(tp);
1336 } else {
1337 sc->sc_tx_stopped = 1;
1338 }
1339 }
1340
1341 #ifdef COM_DEBUG
1342 if (com_debug)
1343 comstatus(sc, "commsrint");
1344 #endif
1345 }
1346
1347 #ifdef __GENERIC_SOFT_INTERRUPTS
1348 void
1349 comsoft(arg)
1350 void *arg;
1351 {
1352 struct com_softc *sc = arg;
1353 struct tty *tp;
1354
1355 {
1356 #else
1357 void
1358 #ifndef alpha
1359 comsoft()
1360 #else
1361 comsoft(arg)
1362 void *arg;
1363 #endif
1364 {
1365 struct com_softc *sc;
1366 struct tty *tp;
1367 int unit;
1368 #ifdef alpha
1369 int s;
1370
1371 s = splsoftserial();
1372 com_softintr_scheduled = 0;
1373 #endif
1374
1375 for (unit = 0; unit < com_cd.cd_ndevs; unit++) {
1376 sc = com_cd.cd_devs[unit];
1377 if (sc == NULL)
1378 continue;
1379
1380 tp = sc->sc_tty;
1381 if (tp == NULL || !ISSET(tp->t_state, TS_ISOPEN | TS_WOPEN))
1382 continue;
1383 #endif
1384 tp = sc->sc_tty;
1385
1386 if (sc->sc_rx_ready) {
1387 sc->sc_rx_ready = 0;
1388 comrxint(sc, tp);
1389 }
1390
1391 if (sc->sc_st_check) {
1392 sc->sc_st_check = 0;
1393 commsrint(sc, tp);
1394 }
1395
1396 if (sc->sc_tx_done) {
1397 sc->sc_tx_done = 0;
1398 comtxint(sc, tp);
1399 }
1400 }
1401
1402 #ifndef __GENERIC_SOFT_INTERRUPTS
1403 #ifdef alpha
1404 splx(s);
1405 #endif
1406 #endif
1407 }
1408
1409 int
1410 comintr(arg)
1411 void *arg;
1412 {
1413 struct com_softc *sc = arg;
1414 bus_space_tag_t iot = sc->sc_iot;
1415 bus_space_handle_t ioh = sc->sc_ioh;
1416 u_char lsr, iir;
1417 u_int put, cc;
1418
1419 iir = bus_space_read_1(iot, ioh, com_iir);
1420 if (ISSET(iir, IIR_NOPEND))
1421 return (0);
1422
1423 put = sc->sc_rbput;
1424 cc = sc->sc_rbavail;
1425
1426 do {
1427 u_char msr, delta;
1428
1429 lsr = bus_space_read_1(iot, ioh, com_lsr);
1430 #if defined(DDB) || defined(KGDB)
1431 if (ISSET(lsr, LSR_BI)) {
1432 #ifdef DDB
1433 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1434 Debugger();
1435 continue;
1436 }
1437 #endif
1438 #ifdef KGDB
1439 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
1440 kgdb_connect(1);
1441 continue;
1442 }
1443 #endif
1444 }
1445 #endif /* DDB || KGDB */
1446
1447 if (ISSET(lsr, LSR_RCV_MASK) &&
1448 !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1449 for (; ISSET(lsr, LSR_RCV_MASK) && cc > 0; cc--) {
1450 sc->sc_rbuf[put] =
1451 bus_space_read_1(iot, ioh, com_data);
1452 sc->sc_lbuf[put] = lsr;
1453 put = (put + 1) & RXBUFMASK;
1454 lsr = bus_space_read_1(iot, ioh, com_lsr);
1455 }
1456 /*
1457 * Current string of incoming characters ended because
1458 * no more data was available. Schedule a receive event
1459 * if any data was received. Drop any characters that
1460 * we couldn't handle.
1461 */
1462 sc->sc_rbput = put;
1463 sc->sc_rbavail = cc;
1464 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1465 sc->sc_rx_ready = 1;
1466 /*
1467 * See if we are in danger of overflowing a buffer. If
1468 * so, use hardware flow control to ease the pressure.
1469 */
1470 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1471 cc < sc->sc_r_hiwat) {
1472 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1473 com_hwiflow(sc);
1474 }
1475 /*
1476 * If we're out of space, disable receive interrupts
1477 * until the queue has drained a bit.
1478 */
1479 if (!cc) {
1480 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1481 CLR(sc->sc_ier, IER_ERXRDY);
1482 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1483 }
1484 } else {
1485 if ((iir & IIR_IMASK) == IIR_RXRDY) {
1486 bus_space_write_1(iot, ioh, com_ier, 0);
1487 delay(10);
1488 bus_space_write_1(iot, ioh, com_ier,sc->sc_ier);
1489 iir = IIR_NOPEND;
1490 continue;
1491 }
1492 }
1493
1494 msr = bus_space_read_1(iot, ioh, com_msr);
1495 delta = msr ^ sc->sc_msr;
1496 sc->sc_msr = msr;
1497 if (ISSET(delta, sc->sc_msr_mask)) {
1498 sc->sc_msr_delta |= delta;
1499
1500 /*
1501 * Stop output immediately if we lose the output
1502 * flow control signal or carrier detect.
1503 */
1504 if (ISSET(~msr, sc->sc_msr_mask)) {
1505 sc->sc_tbc = 0;
1506 sc->sc_heldtbc = 0;
1507 #ifdef COM_DEBUG
1508 if (com_debug)
1509 comstatus(sc, "comintr ");
1510 #endif
1511 }
1512
1513 sc->sc_st_check = 1;
1514 }
1515 } while (!ISSET((iir = bus_space_read_1(iot, ioh, com_iir)), IIR_NOPEND));
1516
1517 /*
1518 * Done handling any receive interrupts. See if data can be
1519 * transmitted as well. Schedule tx done event if no data left
1520 * and tty was marked busy.
1521 */
1522 if (ISSET(lsr, LSR_TXRDY)) {
1523 /*
1524 * If we've delayed a parameter change, do it now, and restart
1525 * output.
1526 */
1527 if (sc->sc_heldchange) {
1528 com_loadchannelregs(sc);
1529 sc->sc_heldchange = 0;
1530 sc->sc_tbc = sc->sc_heldtbc;
1531 sc->sc_heldtbc = 0;
1532 }
1533 /* Output the next chunk of the contiguous buffer, if any. */
1534 if (sc->sc_tbc > 0) {
1535 int n;
1536
1537 n = sc->sc_fifolen;
1538 if (n > sc->sc_tbc)
1539 n = sc->sc_tbc;
1540 bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
1541 sc->sc_tbc -= n;
1542 sc->sc_tba += n;
1543 } else if (sc->sc_tx_busy) {
1544 sc->sc_tx_busy = 0;
1545 sc->sc_tx_done = 1;
1546 }
1547 }
1548
1549 /* Wake up the poller. */
1550 #ifdef __GENERIC_SOFT_INTERRUPTS
1551 softintr_schedule(sc->sc_si);
1552 #else
1553 #ifndef alpha
1554 setsoftserial();
1555 #else
1556 if (!com_softintr_scheduled) {
1557 com_softintr_scheduled = 1;
1558 timeout(comsoft, NULL, 1);
1559 }
1560 #endif
1561 #endif
1562 return (1);
1563 }
1564
1565 /*
1566 * The following functions are polled getc and putc routines, shared
1567 * by the console and kgdb glue.
1568 */
1569
1570 int
1571 com_common_getc(iot, ioh)
1572 bus_space_tag_t iot;
1573 bus_space_handle_t ioh;
1574 {
1575 int s = splserial();
1576 u_char stat, c;
1577
1578 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
1579 ;
1580 c = bus_space_read_1(iot, ioh, com_data);
1581 stat = bus_space_read_1(iot, ioh, com_iir);
1582 splx(s);
1583 return (c);
1584 }
1585
1586 void
1587 com_common_putc(iot, ioh, c)
1588 bus_space_tag_t iot;
1589 bus_space_handle_t ioh;
1590 int c;
1591 {
1592 int s = splserial();
1593 u_char stat;
1594 register int timo;
1595
1596 /* wait for any pending transmission to finish */
1597 timo = 50000;
1598 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY)
1599 && --timo)
1600 ;
1601 bus_space_write_1(iot, ioh, com_data, c);
1602 /* wait for this transmission to complete */
1603 timo = 1500000;
1604 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY)
1605 && --timo)
1606 ;
1607 /* clear any interrupts generated by this transmission */
1608 stat = bus_space_read_1(iot, ioh, com_iir);
1609 splx(s);
1610 }
1611
1612 /*
1613 * Initialize UART to known state.
1614 */
1615 int
1616 cominit(iot, iobase, rate, cflag, iohp)
1617 bus_space_tag_t iot;
1618 int iobase;
1619 int rate;
1620 tcflag_t cflag;
1621 bus_space_handle_t *iohp;
1622 {
1623 bus_space_handle_t ioh;
1624
1625 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
1626 return(ENOMEM); /* ??? */
1627
1628 bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
1629 rate = comspeed(rate);
1630 bus_space_write_1(iot, ioh, com_dlbl, rate);
1631 bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
1632 bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
1633 bus_space_write_1(iot, ioh, com_mcr, 0);
1634 bus_space_write_1(iot, ioh, com_fifo,
1635 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
1636 bus_space_write_1(iot, ioh, com_ier, 0);
1637
1638 *iohp = ioh;
1639 return(0);
1640 }
1641
1642 /*
1643 * Following are all routines needed for COM to act as console
1644 */
1645
1646 int
1647 comcnattach(iot, iobase, rate, cflag)
1648 bus_space_tag_t iot;
1649 int iobase;
1650 int rate;
1651 tcflag_t cflag;
1652 {
1653 int res;
1654 static struct consdev comcons = { NULL, NULL,
1655 comcngetc, comcnputc, comcnpollc, NODEV, CN_NORMAL};
1656
1657 res = cominit(iot, iobase, rate, cflag, &comconsioh);
1658 if(res) return(res);
1659
1660 cn_tab = &comcons;
1661
1662 comconstag = iot;
1663 comconsaddr = iobase;
1664 comconsrate = rate;
1665 comconscflag = cflag;
1666
1667 return(0);
1668 }
1669
1670 int
1671 comcngetc(dev)
1672 dev_t dev;
1673 {
1674
1675 return (com_common_getc(comconstag, comconsioh));
1676 }
1677
1678 /*
1679 * Console kernel output character routine.
1680 */
1681 void
1682 comcnputc(dev, c)
1683 dev_t dev;
1684 int c;
1685 {
1686
1687 com_common_putc(comconstag, comconsioh, c);
1688 }
1689
1690 void
1691 comcnpollc(dev, on)
1692 dev_t dev;
1693 int on;
1694 {
1695
1696 }
1697
1698 #ifdef KGDB
1699 int
1700 com_kgdb_attach(iot, iobase, rate, cflag)
1701 bus_space_tag_t iot;
1702 int iobase;
1703 int rate;
1704 tcflag_t cflag;
1705 {
1706 int res;
1707
1708 if (iot == comconstag && iobase == comconsaddr)
1709 return(EBUSY); /* cannot share with console */
1710
1711 res = cominit(iot, iobase, rate, cflag, &com_kgdb_ioh);
1712 if(res) return(res);
1713
1714 kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
1715 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
1716
1717 com_kgdb_iot = iot;
1718 com_kgdb_addr = iobase;
1719
1720 return(0);
1721 }
1722
1723 /* ARGSUSED */
1724 int
1725 com_kgdb_getc(arg)
1726 void *arg;
1727 {
1728
1729 return (com_common_getc(com_kgdb_iot, com_kgdb_ioh));
1730 }
1731
1732 /* ARGSUSED */
1733 void
1734 com_kgdb_putc(arg, c)
1735 void *arg;
1736 int c;
1737 {
1738
1739 return (com_common_putc(com_kgdb_iot, com_kgdb_ioh, c));
1740 }
1741 #endif /* KGDB */
1742
1743 /* helper function to identify the com ports used by
1744 console or KGDB (and not yet autoconf attached) */
1745 int
1746 com_is_console(iot, iobase, ioh)
1747 bus_space_tag_t iot;
1748 int iobase;
1749 bus_space_handle_t *ioh;
1750 {
1751 bus_space_handle_t help;
1752
1753 if (!comconsattached
1754 && iot == comconstag && iobase == comconsaddr)
1755 help = comconsioh;
1756 #ifdef KGDB
1757 else if (!com_kgdb_attached
1758 && iot == com_kgdb_iot && iobase == com_kgdb_addr)
1759 help = com_kgdb_ioh;
1760 #endif
1761 else
1762 return(0);
1763
1764 if(ioh) *ioh = help;
1765 return(1);
1766 }
1767