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