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