com.c revision 1.174 1 /* $NetBSD: com.c,v 1.174 2000/08/03 00:30:47 jeffs Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
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 the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1991 The Regents of the University of California.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)com.c 7.5 (Berkeley) 5/16/91
72 */
73
74 /*
75 * COM driver, uses National Semiconductor NS16450/NS16550AF UART
76 * Supports automatic hardware flow control on StarTech ST16C650A UART
77 */
78
79 #include "opt_ddb.h"
80 #include "opt_ddbparam.h"
81 #include "opt_com.h"
82
83 #include "rnd.h"
84 #if NRND > 0 && defined(RND_COM)
85 #include <sys/rnd.h>
86 #endif
87
88 #include <sys/param.h>
89 #include <sys/systm.h>
90 #include <sys/ioctl.h>
91 #include <sys/select.h>
92 #include <sys/tty.h>
93 #include <sys/proc.h>
94 #include <sys/user.h>
95 #include <sys/conf.h>
96 #include <sys/file.h>
97 #include <sys/uio.h>
98 #include <sys/kernel.h>
99 #include <sys/syslog.h>
100 #include <sys/types.h>
101 #include <sys/device.h>
102 #include <sys/malloc.h>
103 #include <sys/timepps.h>
104 #include <sys/vnode.h>
105
106 #include <machine/intr.h>
107 #include <machine/bus.h>
108
109 #include <dev/ic/comreg.h>
110 #include <dev/ic/comvar.h>
111 #include <dev/ic/ns16550reg.h>
112 #include <dev/ic/st16650reg.h>
113 #ifdef COM_HAYESP
114 #include <dev/ic/hayespreg.h>
115 #endif
116 #define com_lcr com_cfcr
117 #include <dev/cons.h>
118
119 #include "com.h"
120
121 #ifdef COM_HAYESP
122 int comprobeHAYESP __P((bus_space_handle_t hayespioh, struct com_softc *sc));
123 #endif
124
125 #if defined(DDB) || defined(KGDB)
126 static void com_enable_debugport __P((struct com_softc *));
127 #endif
128 void com_config __P((struct com_softc *));
129 void com_shutdown __P((struct com_softc *));
130 int comspeed __P((long, long));
131 static u_char cflag2lcr __P((tcflag_t));
132 int comparam __P((struct tty *, struct termios *));
133 void comstart __P((struct tty *));
134 void comstop __P((struct tty *, int));
135 int comhwiflow __P((struct tty *, int));
136
137 void com_loadchannelregs __P((struct com_softc *));
138 void com_hwiflow __P((struct com_softc *));
139 void com_break __P((struct com_softc *, int));
140 void com_modem __P((struct com_softc *, int));
141 void tiocm_to_com __P((struct com_softc *, int, int));
142 int com_to_tiocm __P((struct com_softc *));
143 void com_iflush __P((struct com_softc *));
144
145 int com_common_getc __P((bus_space_tag_t, bus_space_handle_t));
146 void com_common_putc __P((bus_space_tag_t, bus_space_handle_t, int));
147
148 /* XXX: These belong elsewhere */
149 cdev_decl(com);
150 bdev_decl(com);
151
152 int comcngetc __P((dev_t));
153 void comcnputc __P((dev_t, int));
154 void comcnpollc __P((dev_t, int));
155
156 #define integrate static inline
157 #ifdef __GENERIC_SOFT_INTERRUPTS
158 void comsoft __P((void *));
159 #else
160 #ifndef __NO_SOFT_SERIAL_INTERRUPT
161 void comsoft __P((void));
162 #else
163 void comsoft __P((void *));
164 struct callout comsoft_callout = CALLOUT_INITIALIZER;
165 #endif
166 #endif
167 integrate void com_rxsoft __P((struct com_softc *, struct tty *));
168 integrate void com_txsoft __P((struct com_softc *, struct tty *));
169 integrate void com_stsoft __P((struct com_softc *, struct tty *));
170 integrate void com_schedrx __P((struct com_softc *));
171 void comdiag __P((void *));
172
173 extern struct cfdriver com_cd;
174
175 /*
176 * Make this an option variable one can patch.
177 * But be warned: this must be a power of 2!
178 */
179 u_int com_rbuf_size = COM_RING_SIZE;
180
181 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
182 u_int com_rbuf_hiwat = (COM_RING_SIZE * 1) / 4;
183 u_int com_rbuf_lowat = (COM_RING_SIZE * 3) / 4;
184
185 static int comconsaddr;
186 static bus_space_tag_t comconstag;
187 static bus_space_handle_t comconsioh;
188 static int comconsattached;
189 static int comconsrate;
190 static tcflag_t comconscflag;
191
192 static int ppscap =
193 PPS_TSFMT_TSPEC |
194 PPS_CAPTUREASSERT |
195 PPS_CAPTURECLEAR |
196 #ifdef PPS_SYNC
197 PPS_HARDPPSONASSERT | PPS_HARDPPSONCLEAR |
198 #endif /* PPS_SYNC */
199 PPS_OFFSETASSERT | PPS_OFFSETCLEAR;
200
201 #ifndef __GENERIC_SOFT_INTERRUPTS
202 #ifdef __NO_SOFT_SERIAL_INTERRUPT
203 volatile int com_softintr_scheduled;
204 #endif
205 #endif
206
207 #ifdef KGDB
208 #include <sys/kgdb.h>
209
210 static int com_kgdb_addr;
211 static bus_space_tag_t com_kgdb_iot;
212 static bus_space_handle_t com_kgdb_ioh;
213 static int com_kgdb_attached;
214
215 int com_kgdb_getc __P((void *));
216 void com_kgdb_putc __P((void *, int));
217 #endif /* KGDB */
218
219 #define COMUNIT_MASK 0x7ffff
220 #define COMDIALOUT_MASK 0x80000
221
222 #define COMUNIT(x) (minor(x) & COMUNIT_MASK)
223 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK)
224
225 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && \
226 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
227
228 #define BR BUS_SPACE_BARRIER_READ
229 #define BW BUS_SPACE_BARRIER_WRITE
230 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
231
232 int
233 comspeed(speed, frequency)
234 long speed, frequency;
235 {
236 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
237
238 int x, err;
239
240 #if 0
241 if (speed == 0)
242 return (0);
243 #endif
244 if (speed <= 0)
245 return (-1);
246 x = divrnd(frequency / 16, speed);
247 if (x <= 0)
248 return (-1);
249 err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
250 if (err < 0)
251 err = -err;
252 if (err > COM_TOLERANCE)
253 return (-1);
254 return (x);
255
256 #undef divrnd
257 }
258
259 #ifdef COM_DEBUG
260 int com_debug = 0;
261
262 void comstatus __P((struct com_softc *, char *));
263 void
264 comstatus(sc, str)
265 struct com_softc *sc;
266 char *str;
267 {
268 struct tty *tp = sc->sc_tty;
269
270 printf("%s: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n",
271 sc->sc_dev.dv_xname, str,
272 ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
273 ISSET(sc->sc_msr, MSR_DCD) ? "+" : "-",
274 ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
275 ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-",
276 sc->sc_tx_stopped ? "+" : "-");
277
278 printf("%s: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n",
279 sc->sc_dev.dv_xname, str,
280 ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
281 ISSET(sc->sc_msr, MSR_CTS) ? "+" : "-",
282 ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
283 ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-",
284 sc->sc_rx_flags);
285 }
286 #endif
287
288 int
289 comprobe1(iot, ioh)
290 bus_space_tag_t iot;
291 bus_space_handle_t ioh;
292 {
293
294 /* force access to id reg */
295 bus_space_write_1(iot, ioh, com_lcr, LCR_8BITS);
296 bus_space_write_1(iot, ioh, com_iir, 0);
297 if ((bus_space_read_1(iot, ioh, com_lcr) != LCR_8BITS) ||
298 (bus_space_read_1(iot, ioh, com_iir) & 0x38))
299 return (0);
300
301 return (1);
302 }
303
304 #ifdef COM_HAYESP
305 int
306 comprobeHAYESP(hayespioh, sc)
307 bus_space_handle_t hayespioh;
308 struct com_softc *sc;
309 {
310 char val, dips;
311 int combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
312 bus_space_tag_t iot = sc->sc_iot;
313
314 /*
315 * Hayes ESP cards have two iobases. One is for compatibility with
316 * 16550 serial chips, and at the same ISA PC base addresses. The
317 * other is for ESP-specific enhanced features, and lies at a
318 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
319 */
320
321 /* Test for ESP signature */
322 if ((bus_space_read_1(iot, hayespioh, 0) & 0xf3) == 0)
323 return (0);
324
325 /*
326 * ESP is present at ESP enhanced base address; unknown com port
327 */
328
329 /* Get the dip-switch configurations */
330 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETDIPS);
331 dips = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1);
332
333 /* Determine which com port this ESP card services: bits 0,1 of */
334 /* dips is the port # (0-3); combaselist[val] is the com_iobase */
335 if (sc->sc_iobase != combaselist[dips & 0x03])
336 return (0);
337
338 printf(": ESP");
339
340 /* Check ESP Self Test bits. */
341 /* Check for ESP version 2.0: bits 4,5,6 == 010 */
342 bus_space_write_1(iot, hayespioh, HAYESP_CMD1, HAYESP_GETTEST);
343 val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS1); /* Clear reg1 */
344 val = bus_space_read_1(iot, hayespioh, HAYESP_STATUS2);
345 if ((val & 0x70) < 0x20) {
346 printf("-old (%o)", val & 0x70);
347 /* we do not support the necessary features */
348 return (0);
349 }
350
351 /* Check for ability to emulate 16550: bit 8 == 1 */
352 if ((dips & 0x80) == 0) {
353 printf(" slave");
354 /* XXX Does slave really mean no 16550 support?? */
355 return (0);
356 }
357
358 /*
359 * If we made it this far, we are a full-featured ESP v2.0 (or
360 * better), at the correct com port address.
361 */
362
363 SET(sc->sc_hwflags, COM_HW_HAYESP);
364 printf(", 1024 byte fifo\n");
365 return (1);
366 }
367 #endif
368
369 #if defined(DDB) || defined(KGDB)
370 static void
371 com_enable_debugport(sc)
372 struct com_softc *sc;
373 {
374 int s;
375
376 /* Turn on line break interrupt, set carrier. */
377 s = splserial();
378 sc->sc_ier = IER_ERXRDY;
379 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
380 SET(sc->sc_mcr, MCR_DTR | MCR_RTS);
381 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
382 splx(s);
383 }
384 #endif
385
386 void
387 com_attach_subr(sc)
388 struct com_softc *sc;
389 {
390 int iobase = sc->sc_iobase;
391 bus_space_tag_t iot = sc->sc_iot;
392 bus_space_handle_t ioh = sc->sc_ioh;
393 struct tty *tp;
394 #ifdef COM16650
395 u_int8_t lcr;
396 #endif
397 #ifdef COM_HAYESP
398 int hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
399 int *hayespp;
400 #endif
401
402 callout_init(&sc->sc_diag_callout);
403
404 /* Disable interrupts before configuring the device. */
405 sc->sc_ier = 0;
406 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
407
408 if (iot == comconstag && iobase == comconsaddr) {
409 comconsattached = 1;
410
411 /* Make sure the console is always "hardwired". */
412 delay(1000); /* wait for output to finish */
413 SET(sc->sc_hwflags, COM_HW_CONSOLE);
414 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
415 }
416
417 #ifdef COM_HAYESP
418 /* Look for a Hayes ESP board. */
419 for (hayespp = hayesp_ports; *hayespp != 0; hayespp++) {
420 bus_space_handle_t hayespioh;
421
422 #define HAYESP_NPORTS 8 /* XXX XXX XXX ??? ??? ??? */
423 if (bus_space_map(iot, *hayespp, HAYESP_NPORTS, 0, &hayespioh))
424 continue;
425 if (comprobeHAYESP(hayespioh, sc)) {
426 sc->sc_hayespioh = hayespioh;
427 sc->sc_fifolen = 1024;
428
429 break;
430 }
431 bus_space_unmap(iot, hayespioh, HAYESP_NPORTS);
432 }
433 /* No ESP; look for other things. */
434 if (!ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
435 #endif
436 sc->sc_fifolen = 1;
437 /* look for a NS 16550AF UART with FIFOs */
438 bus_space_write_1(iot, ioh, com_fifo,
439 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
440 delay(100);
441 if (ISSET(bus_space_read_1(iot, ioh, com_iir), IIR_FIFO_MASK)
442 == IIR_FIFO_MASK)
443 if (ISSET(bus_space_read_1(iot, ioh, com_fifo), FIFO_TRIGGER_14)
444 == FIFO_TRIGGER_14) {
445 SET(sc->sc_hwflags, COM_HW_FIFO);
446
447 #ifdef COM16650
448 /*
449 * IIR changes into the EFR if LCR is set to LCR_EERS
450 * on 16650s. We also know IIR != 0 at this point.
451 * Write 0 into the EFR, and read it. If the result
452 * is 0, we have a 16650.
453 *
454 * Older 16650s were broken; the test to detect them
455 * is taken from the Linux driver. Apparently
456 * setting DLAB enable gives access to the EFR on
457 * these chips.
458 */
459 lcr = bus_space_read_1(iot, ioh, com_lcr);
460 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
461 bus_space_write_1(iot, ioh, com_efr, 0);
462 if (bus_space_read_1(iot, ioh, com_efr) == 0) {
463 bus_space_write_1(iot, ioh, com_lcr,
464 lcr | LCR_DLAB);
465 if (bus_space_read_1(iot, ioh, com_efr) == 0) {
466 CLR(sc->sc_hwflags, COM_HW_FIFO);
467 sc->sc_fifolen = 0;
468 } else {
469 SET(sc->sc_hwflags, COM_HW_FLOW);
470 sc->sc_fifolen = 32;
471 }
472 } else
473 #endif
474 sc->sc_fifolen = 16;
475
476 #ifdef COM16650
477 bus_space_write_1(iot, ioh, com_lcr, lcr);
478 if (sc->sc_fifolen == 0)
479 printf(": st16650, broken fifo\n");
480 else if (sc->sc_fifolen == 32)
481 printf(": st16650a, working fifo\n");
482 else
483 #endif
484 printf(": ns16550a, working fifo\n");
485 } else
486 printf(": ns16550, broken fifo\n");
487 else
488 printf(": ns8250 or ns16450, no fifo\n");
489 bus_space_write_1(iot, ioh, com_fifo, 0);
490 if (ISSET(sc->sc_hwflags, COM_HW_TXFIFO_DISABLE)) {
491 sc->sc_fifolen = 1;
492 printf("%s: txfifo disabled\n", sc->sc_dev.dv_xname);
493 }
494 #ifdef COM_HAYESP
495 }
496 #endif
497
498 tp = ttymalloc();
499 tp->t_oproc = comstart;
500 tp->t_param = comparam;
501 tp->t_hwiflow = comhwiflow;
502
503 sc->sc_tty = tp;
504 sc->sc_rbuf = malloc(com_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
505 if (sc->sc_rbuf == NULL) {
506 printf("%s: unable to allocate ring buffer\n",
507 sc->sc_dev.dv_xname);
508 return;
509 }
510 sc->sc_ebuf = sc->sc_rbuf + (com_rbuf_size << 1);
511
512 tty_attach(tp);
513
514 if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
515 SET(sc->sc_mcr, MCR_IENABLE);
516
517 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
518 int maj;
519
520 /* locate the major number */
521 for (maj = 0; maj < nchrdev; maj++)
522 if (cdevsw[maj].d_open == comopen)
523 break;
524
525 cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
526
527 printf("%s: console\n", sc->sc_dev.dv_xname);
528 }
529
530 #ifdef KGDB
531 /*
532 * Allow kgdb to "take over" this port. If this is
533 * the kgdb device, it has exclusive use.
534 */
535 if (iot == com_kgdb_iot && iobase == com_kgdb_addr) {
536 com_kgdb_attached = 1;
537
538 SET(sc->sc_hwflags, COM_HW_KGDB);
539 printf("%s: kgdb\n", sc->sc_dev.dv_xname);
540 }
541 #endif
542
543 #ifdef __GENERIC_SOFT_INTERRUPTS
544 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, comsoft, sc);
545 #endif
546
547 #if NRND > 0 && defined(RND_COM)
548 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
549 RND_TYPE_TTY, 0);
550 #endif
551
552 /* if there are no enable/disable functions, assume the device
553 is always enabled */
554 if (!sc->enable)
555 sc->enabled = 1;
556
557 com_config(sc);
558
559 SET(sc->sc_hwflags, COM_HW_DEV_OK);
560 }
561
562 void
563 com_config(sc)
564 struct com_softc *sc;
565 {
566 bus_space_tag_t iot = sc->sc_iot;
567 bus_space_handle_t ioh = sc->sc_ioh;
568
569 /* Disable interrupts before configuring the device. */
570 sc->sc_ier = 0;
571 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
572
573 #ifdef COM_HAYESP
574 /* Look for a Hayes ESP board. */
575 if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
576 sc->sc_fifolen = 1024;
577
578 /* Set 16550 compatibility mode */
579 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
580 HAYESP_SETMODE);
581 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
582 HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
583 HAYESP_MODE_SCALE);
584
585 /* Set RTS/CTS flow control */
586 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
587 HAYESP_SETFLOWTYPE);
588 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
589 HAYESP_FLOW_RTS);
590 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
591 HAYESP_FLOW_CTS);
592
593 /* Set flow control levels */
594 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD1,
595 HAYESP_SETRXFLOW);
596 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
597 HAYESP_HIBYTE(HAYESP_RXHIWMARK));
598 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
599 HAYESP_LOBYTE(HAYESP_RXHIWMARK));
600 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
601 HAYESP_HIBYTE(HAYESP_RXLOWMARK));
602 bus_space_write_1(iot, sc->sc_hayespioh, HAYESP_CMD2,
603 HAYESP_LOBYTE(HAYESP_RXLOWMARK));
604 }
605 #endif
606
607 #ifdef DDB
608 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
609 com_enable_debugport(sc);
610 #endif
611
612 #ifdef KGDB
613 /*
614 * Allow kgdb to "take over" this port. If this is
615 * the kgdb device, it has exclusive use.
616 */
617 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
618 com_enable_debugport(sc);
619 #endif
620 }
621
622 int
623 com_detach(self, flags)
624 struct device *self;
625 int flags;
626 {
627 struct com_softc *sc = (struct com_softc *)self;
628 int maj, mn;
629
630 /* locate the major number */
631 for (maj = 0; maj < nchrdev; maj++)
632 if (cdevsw[maj].d_open == comopen)
633 break;
634
635 /* Nuke the vnodes for any open instances. */
636 mn = self->dv_unit;
637 vdevgone(maj, mn, mn, VCHR);
638
639 mn |= COMDIALOUT_MASK;
640 vdevgone(maj, mn, mn, VCHR);
641
642 /* Free the receive buffer. */
643 free(sc->sc_rbuf, M_DEVBUF);
644
645 /* Detach and free the tty. */
646 tty_detach(sc->sc_tty);
647 ttyfree(sc->sc_tty);
648
649 #ifdef __GENERIC_SOFT_INTERRUPTS
650 /* Unhook the soft interrupt handler. */
651 softintr_disestablish(sc->sc_si);
652 #endif
653
654 #if NRND > 0 && defined(RND_COM)
655 /* Unhook the entropy source. */
656 rnd_detach_source(&sc->rnd_source);
657 #endif
658
659 return (0);
660 }
661
662 int
663 com_activate(self, act)
664 struct device *self;
665 enum devact act;
666 {
667 struct com_softc *sc = (struct com_softc *)self;
668 int s, rv = 0;
669
670 s = splserial();
671 switch (act) {
672 case DVACT_ACTIVATE:
673 rv = EOPNOTSUPP;
674 break;
675
676 case DVACT_DEACTIVATE:
677 if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) {
678 rv = EBUSY;
679 break;
680 }
681
682 if (sc->disable != NULL && sc->enabled != 0) {
683 (*sc->disable)(sc);
684 sc->enabled = 0;
685 }
686 break;
687 }
688 splx(s);
689 return (rv);
690 }
691
692 void
693 com_shutdown(sc)
694 struct com_softc *sc;
695 {
696 struct tty *tp = sc->sc_tty;
697 int s;
698
699 s = splserial();
700
701 /* If we were asserting flow control, then deassert it. */
702 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
703 com_hwiflow(sc);
704
705 /* Clear any break condition set with TIOCSBRK. */
706 com_break(sc, 0);
707
708 /* Turn off PPS capture on last close. */
709 sc->sc_ppsmask = 0;
710 sc->ppsparam.mode = 0;
711
712 /*
713 * Hang up if necessary. Wait a bit, so the other side has time to
714 * notice even if we immediately open the port again.
715 */
716 if (ISSET(tp->t_cflag, HUPCL)) {
717 com_modem(sc, 0);
718 (void) tsleep(sc, TTIPRI, ttclos, hz);
719 }
720
721 /* Turn off interrupts. */
722 #ifdef DDB
723 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
724 sc->sc_ier = IER_ERXRDY; /* interrupt on break */
725 else
726 #endif
727 sc->sc_ier = 0;
728 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
729
730 if (sc->disable) {
731 #ifdef DIAGNOSTIC
732 if (!sc->enabled)
733 panic("com_shutdown: not enabled?");
734 #endif
735 (*sc->disable)(sc);
736 sc->enabled = 0;
737 }
738
739 splx(s);
740 }
741
742 int
743 comopen(dev, flag, mode, p)
744 dev_t dev;
745 int flag, mode;
746 struct proc *p;
747 {
748 struct com_softc *sc;
749 struct tty *tp;
750 int s, s2;
751 int error;
752
753 sc = device_lookup(&com_cd, COMUNIT(dev));
754 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
755 sc->sc_rbuf == NULL)
756 return (ENXIO);
757
758 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
759 return (ENXIO);
760
761 #ifdef KGDB
762 /*
763 * If this is the kgdb port, no other use is permitted.
764 */
765 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
766 return (EBUSY);
767 #endif
768
769 tp = sc->sc_tty;
770
771 if (ISSET(tp->t_state, TS_ISOPEN) &&
772 ISSET(tp->t_state, TS_XCLUDE) &&
773 p->p_ucred->cr_uid != 0)
774 return (EBUSY);
775
776 s = spltty();
777
778 /*
779 * Do the following iff this is a first open.
780 */
781 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
782 struct termios t;
783
784 tp->t_dev = dev;
785
786 s2 = splserial();
787
788 if (sc->enable) {
789 if ((*sc->enable)(sc)) {
790 splx(s2);
791 splx(s);
792 printf("%s: device enable failed\n",
793 sc->sc_dev.dv_xname);
794 return (EIO);
795 }
796 sc->enabled = 1;
797 com_config(sc);
798 }
799
800 /* Turn on interrupts. */
801 sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
802 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
803
804 /* Fetch the current modem control status, needed later. */
805 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
806
807 /* Clear PPS capture state on first open. */
808 sc->sc_ppsmask = 0;
809 sc->ppsparam.mode = 0;
810
811 splx(s2);
812
813 /*
814 * Initialize the termios status to the defaults. Add in the
815 * sticky bits from TIOCSFLAGS.
816 */
817 t.c_ispeed = 0;
818 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
819 t.c_ospeed = comconsrate;
820 t.c_cflag = comconscflag;
821 } else {
822 t.c_ospeed = TTYDEF_SPEED;
823 t.c_cflag = TTYDEF_CFLAG;
824 }
825 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
826 SET(t.c_cflag, CLOCAL);
827 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
828 SET(t.c_cflag, CRTSCTS);
829 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
830 SET(t.c_cflag, MDMBUF);
831 /* Make sure comparam() will do something. */
832 tp->t_ospeed = 0;
833 (void) comparam(tp, &t);
834 tp->t_iflag = TTYDEF_IFLAG;
835 tp->t_oflag = TTYDEF_OFLAG;
836 tp->t_lflag = TTYDEF_LFLAG;
837 ttychars(tp);
838 ttsetwater(tp);
839
840 s2 = splserial();
841
842 /*
843 * Turn on DTR. We must always do this, even if carrier is not
844 * present, because otherwise we'd have to use TIOCSDTR
845 * immediately after setting CLOCAL, which applications do not
846 * expect. We always assert DTR while the device is open
847 * unless explicitly requested to deassert it.
848 */
849 com_modem(sc, 1);
850
851 /* Clear the input ring, and unblock. */
852 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
853 sc->sc_rbavail = com_rbuf_size;
854 com_iflush(sc);
855 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
856 com_hwiflow(sc);
857
858 #ifdef COM_DEBUG
859 if (com_debug)
860 comstatus(sc, "comopen ");
861 #endif
862
863 splx(s2);
864 }
865
866 splx(s);
867
868 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
869 if (error)
870 goto bad;
871
872 error = (*linesw[tp->t_line].l_open)(dev, tp);
873 if (error)
874 goto bad;
875
876 return (0);
877
878 bad:
879 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
880 /*
881 * We failed to open the device, and nobody else had it opened.
882 * Clean up the state as appropriate.
883 */
884 com_shutdown(sc);
885 }
886
887 return (error);
888 }
889
890 int
891 comclose(dev, flag, mode, p)
892 dev_t dev;
893 int flag, mode;
894 struct proc *p;
895 {
896 struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
897 struct tty *tp = sc->sc_tty;
898
899 /* XXX This is for cons.c. */
900 if (!ISSET(tp->t_state, TS_ISOPEN))
901 return (0);
902
903 (*linesw[tp->t_line].l_close)(tp, flag);
904 ttyclose(tp);
905
906 if (COM_ISALIVE(sc) == 0)
907 return (0);
908
909 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
910 /*
911 * Although we got a last close, the device may still be in
912 * use; e.g. if this was the dialout node, and there are still
913 * processes waiting for carrier on the non-dialout node.
914 */
915 com_shutdown(sc);
916 }
917
918 return (0);
919 }
920
921 int
922 comread(dev, uio, flag)
923 dev_t dev;
924 struct uio *uio;
925 int flag;
926 {
927 struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
928 struct tty *tp = sc->sc_tty;
929
930 if (COM_ISALIVE(sc) == 0)
931 return (EIO);
932
933 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
934 }
935
936 int
937 comwrite(dev, uio, flag)
938 dev_t dev;
939 struct uio *uio;
940 int flag;
941 {
942 struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
943 struct tty *tp = sc->sc_tty;
944
945 if (COM_ISALIVE(sc) == 0)
946 return (EIO);
947
948 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
949 }
950
951 struct tty *
952 comtty(dev)
953 dev_t dev;
954 {
955 struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
956 struct tty *tp = sc->sc_tty;
957
958 return (tp);
959 }
960
961 int
962 comioctl(dev, cmd, data, flag, p)
963 dev_t dev;
964 u_long cmd;
965 caddr_t data;
966 int flag;
967 struct proc *p;
968 {
969 struct com_softc *sc = device_lookup(&com_cd, COMUNIT(dev));
970 struct tty *tp = sc->sc_tty;
971 int error;
972 int s;
973
974 if (COM_ISALIVE(sc) == 0)
975 return (EIO);
976
977 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
978 if (error >= 0)
979 return (error);
980
981 error = ttioctl(tp, cmd, data, flag, p);
982 if (error >= 0)
983 return (error);
984
985 error = 0;
986
987 s = splserial();
988
989 switch (cmd) {
990 case TIOCSBRK:
991 com_break(sc, 1);
992 break;
993
994 case TIOCCBRK:
995 com_break(sc, 0);
996 break;
997
998 case TIOCSDTR:
999 com_modem(sc, 1);
1000 break;
1001
1002 case TIOCCDTR:
1003 com_modem(sc, 0);
1004 break;
1005
1006 case TIOCGFLAGS:
1007 *(int *)data = sc->sc_swflags;
1008 break;
1009
1010 case TIOCSFLAGS:
1011 error = suser(p->p_ucred, &p->p_acflag);
1012 if (error)
1013 break;
1014 sc->sc_swflags = *(int *)data;
1015 break;
1016
1017 case TIOCMSET:
1018 case TIOCMBIS:
1019 case TIOCMBIC:
1020 tiocm_to_com(sc, cmd, *(int *)data);
1021 break;
1022
1023 case TIOCMGET:
1024 *(int *)data = com_to_tiocm(sc);
1025 break;
1026
1027 case PPS_IOC_CREATE:
1028 break;
1029
1030 case PPS_IOC_DESTROY:
1031 break;
1032
1033 case PPS_IOC_GETPARAMS: {
1034 pps_params_t *pp;
1035 pp = (pps_params_t *)data;
1036 *pp = sc->ppsparam;
1037 break;
1038 }
1039
1040 case PPS_IOC_SETPARAMS: {
1041 pps_params_t *pp;
1042 int mode;
1043 pp = (pps_params_t *)data;
1044 if (pp->mode & ~ppscap) {
1045 error = EINVAL;
1046 break;
1047 }
1048 sc->ppsparam = *pp;
1049 /*
1050 * Compute msr masks from user-specified timestamp state.
1051 */
1052 mode = sc->ppsparam.mode;
1053 #ifdef PPS_SYNC
1054 if (mode & PPS_HARDPPSONASSERT) {
1055 mode |= PPS_CAPTUREASSERT;
1056 /* XXX revoke any previous HARDPPS source */
1057 }
1058 if (mode & PPS_HARDPPSONCLEAR) {
1059 mode |= PPS_CAPTURECLEAR;
1060 /* XXX revoke any previous HARDPPS source */
1061 }
1062 #endif /* PPS_SYNC */
1063 switch (mode & PPS_CAPTUREBOTH) {
1064 case 0:
1065 sc->sc_ppsmask = 0;
1066 break;
1067
1068 case PPS_CAPTUREASSERT:
1069 sc->sc_ppsmask = MSR_DCD;
1070 sc->sc_ppsassert = MSR_DCD;
1071 sc->sc_ppsclear = -1;
1072 break;
1073
1074 case PPS_CAPTURECLEAR:
1075 sc->sc_ppsmask = MSR_DCD;
1076 sc->sc_ppsassert = -1;
1077 sc->sc_ppsclear = 0;
1078 break;
1079
1080 case PPS_CAPTUREBOTH:
1081 sc->sc_ppsmask = MSR_DCD;
1082 sc->sc_ppsassert = MSR_DCD;
1083 sc->sc_ppsclear = 0;
1084 break;
1085
1086 default:
1087 error = EINVAL;
1088 break;
1089 }
1090 break;
1091 }
1092
1093 case PPS_IOC_GETCAP:
1094 *(int*)data = ppscap;
1095 break;
1096
1097 case PPS_IOC_FETCH: {
1098 pps_info_t *pi;
1099 pi = (pps_info_t *)data;
1100 *pi = sc->ppsinfo;
1101 break;
1102 }
1103
1104 case TIOCDCDTIMESTAMP: /* XXX old, overloaded API used by xntpd v3 */
1105 /*
1106 * Some GPS clocks models use the falling rather than
1107 * rising edge as the on-the-second signal.
1108 * The old API has no way to specify PPS polarity.
1109 */
1110 sc->sc_ppsmask = MSR_DCD;
1111 #ifndef PPS_TRAILING_EDGE
1112 sc->sc_ppsassert = MSR_DCD;
1113 sc->sc_ppsclear = -1;
1114 TIMESPEC_TO_TIMEVAL((struct timeval *)data,
1115 &sc->ppsinfo.assert_timestamp);
1116 #else
1117 sc->sc_ppsassert = -1
1118 sc->sc_ppsclear = 0;
1119 TIMESPEC_TO_TIMEVAL((struct timeval *)data,
1120 &sc->ppsinfo.clear_timestamp);
1121 #endif
1122 break;
1123
1124 default:
1125 error = ENOTTY;
1126 break;
1127 }
1128
1129 splx(s);
1130
1131 #ifdef COM_DEBUG
1132 if (com_debug)
1133 comstatus(sc, "comioctl ");
1134 #endif
1135
1136 return (error);
1137 }
1138
1139 integrate void
1140 com_schedrx(sc)
1141 struct com_softc *sc;
1142 {
1143
1144 sc->sc_rx_ready = 1;
1145
1146 /* Wake up the poller. */
1147 #ifdef __GENERIC_SOFT_INTERRUPTS
1148 softintr_schedule(sc->sc_si);
1149 #else
1150 #ifndef __NO_SOFT_SERIAL_INTERRUPT
1151 setsoftserial();
1152 #else
1153 if (!com_softintr_scheduled) {
1154 com_softintr_scheduled = 1;
1155 callout_reset(&comsoft_callout, 1, comsoft, NULL);
1156 }
1157 #endif
1158 #endif
1159 }
1160
1161 void
1162 com_break(sc, onoff)
1163 struct com_softc *sc;
1164 int onoff;
1165 {
1166
1167 if (onoff)
1168 SET(sc->sc_lcr, LCR_SBREAK);
1169 else
1170 CLR(sc->sc_lcr, LCR_SBREAK);
1171
1172 if (!sc->sc_heldchange) {
1173 if (sc->sc_tx_busy) {
1174 sc->sc_heldtbc = sc->sc_tbc;
1175 sc->sc_tbc = 0;
1176 sc->sc_heldchange = 1;
1177 } else
1178 com_loadchannelregs(sc);
1179 }
1180 }
1181
1182 void
1183 com_modem(sc, onoff)
1184 struct com_softc *sc;
1185 int onoff;
1186 {
1187
1188 if (sc->sc_mcr_dtr == 0)
1189 return;
1190
1191 if (onoff)
1192 SET(sc->sc_mcr, sc->sc_mcr_dtr);
1193 else
1194 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1195
1196 if (!sc->sc_heldchange) {
1197 if (sc->sc_tx_busy) {
1198 sc->sc_heldtbc = sc->sc_tbc;
1199 sc->sc_tbc = 0;
1200 sc->sc_heldchange = 1;
1201 } else
1202 com_loadchannelregs(sc);
1203 }
1204 }
1205
1206 void
1207 tiocm_to_com(sc, how, ttybits)
1208 struct com_softc *sc;
1209 int how, ttybits;
1210 {
1211 u_char combits;
1212
1213 combits = 0;
1214 if (ISSET(ttybits, TIOCM_DTR))
1215 SET(combits, MCR_DTR);
1216 if (ISSET(ttybits, TIOCM_RTS))
1217 SET(combits, MCR_RTS);
1218
1219 switch (how) {
1220 case TIOCMBIC:
1221 CLR(sc->sc_mcr, combits);
1222 break;
1223
1224 case TIOCMBIS:
1225 SET(sc->sc_mcr, combits);
1226 break;
1227
1228 case TIOCMSET:
1229 CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
1230 SET(sc->sc_mcr, combits);
1231 break;
1232 }
1233
1234 if (!sc->sc_heldchange) {
1235 if (sc->sc_tx_busy) {
1236 sc->sc_heldtbc = sc->sc_tbc;
1237 sc->sc_tbc = 0;
1238 sc->sc_heldchange = 1;
1239 } else
1240 com_loadchannelregs(sc);
1241 }
1242 }
1243
1244 int
1245 com_to_tiocm(sc)
1246 struct com_softc *sc;
1247 {
1248 u_char combits;
1249 int ttybits = 0;
1250
1251 combits = sc->sc_mcr;
1252 if (ISSET(combits, MCR_DTR))
1253 SET(ttybits, TIOCM_DTR);
1254 if (ISSET(combits, MCR_RTS))
1255 SET(ttybits, TIOCM_RTS);
1256
1257 combits = sc->sc_msr;
1258 if (ISSET(combits, MSR_DCD))
1259 SET(ttybits, TIOCM_CD);
1260 if (ISSET(combits, MSR_CTS))
1261 SET(ttybits, TIOCM_CTS);
1262 if (ISSET(combits, MSR_DSR))
1263 SET(ttybits, TIOCM_DSR);
1264 if (ISSET(combits, MSR_RI | MSR_TERI))
1265 SET(ttybits, TIOCM_RI);
1266
1267 if (sc->sc_ier != 0)
1268 SET(ttybits, TIOCM_LE);
1269
1270 return (ttybits);
1271 }
1272
1273 static u_char
1274 cflag2lcr(cflag)
1275 tcflag_t cflag;
1276 {
1277 u_char lcr = 0;
1278
1279 switch (ISSET(cflag, CSIZE)) {
1280 case CS5:
1281 SET(lcr, LCR_5BITS);
1282 break;
1283 case CS6:
1284 SET(lcr, LCR_6BITS);
1285 break;
1286 case CS7:
1287 SET(lcr, LCR_7BITS);
1288 break;
1289 case CS8:
1290 SET(lcr, LCR_8BITS);
1291 break;
1292 }
1293 if (ISSET(cflag, PARENB)) {
1294 SET(lcr, LCR_PENAB);
1295 if (!ISSET(cflag, PARODD))
1296 SET(lcr, LCR_PEVEN);
1297 }
1298 if (ISSET(cflag, CSTOPB))
1299 SET(lcr, LCR_STOPB);
1300
1301 return (lcr);
1302 }
1303
1304 int
1305 comparam(tp, t)
1306 struct tty *tp;
1307 struct termios *t;
1308 {
1309 struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
1310 int ospeed = comspeed(t->c_ospeed, sc->sc_frequency);
1311 u_char lcr;
1312 int s;
1313
1314 if (COM_ISALIVE(sc) == 0)
1315 return (EIO);
1316
1317 /* Check requested parameters. */
1318 if (ospeed < 0)
1319 return (EINVAL);
1320 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
1321 return (EINVAL);
1322
1323 /*
1324 * For the console, always force CLOCAL and !HUPCL, so that the port
1325 * is always active.
1326 */
1327 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
1328 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1329 SET(t->c_cflag, CLOCAL);
1330 CLR(t->c_cflag, HUPCL);
1331 }
1332
1333 /*
1334 * If there were no changes, don't do anything. This avoids dropping
1335 * input and improves performance when all we did was frob things like
1336 * VMIN and VTIME.
1337 */
1338 if (tp->t_ospeed == t->c_ospeed &&
1339 tp->t_cflag == t->c_cflag)
1340 return (0);
1341
1342 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
1343
1344 s = splserial();
1345
1346 sc->sc_lcr = lcr;
1347
1348 /*
1349 * If we're not in a mode that assumes a connection is present, then
1350 * ignore carrier changes.
1351 */
1352 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1353 sc->sc_msr_dcd = 0;
1354 else
1355 sc->sc_msr_dcd = MSR_DCD;
1356 /*
1357 * Set the flow control pins depending on the current flow control
1358 * mode.
1359 */
1360 if (ISSET(t->c_cflag, CRTSCTS)) {
1361 sc->sc_mcr_dtr = MCR_DTR;
1362 sc->sc_mcr_rts = MCR_RTS;
1363 sc->sc_msr_cts = MSR_CTS;
1364 sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
1365 } else if (ISSET(t->c_cflag, MDMBUF)) {
1366 /*
1367 * For DTR/DCD flow control, make sure we don't toggle DTR for
1368 * carrier detection.
1369 */
1370 sc->sc_mcr_dtr = 0;
1371 sc->sc_mcr_rts = MCR_DTR;
1372 sc->sc_msr_cts = MSR_DCD;
1373 sc->sc_efr = 0;
1374 } else {
1375 /*
1376 * If no flow control, then always set RTS. This will make
1377 * the other side happy if it mistakenly thinks we're doing
1378 * RTS/CTS flow control.
1379 */
1380 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
1381 sc->sc_mcr_rts = 0;
1382 sc->sc_msr_cts = 0;
1383 sc->sc_efr = 0;
1384 if (ISSET(sc->sc_mcr, MCR_DTR))
1385 SET(sc->sc_mcr, MCR_RTS);
1386 else
1387 CLR(sc->sc_mcr, MCR_RTS);
1388 }
1389 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
1390
1391 #if 0
1392 if (ospeed == 0)
1393 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1394 else
1395 SET(sc->sc_mcr, sc->sc_mcr_dtr);
1396 #endif
1397
1398 sc->sc_dlbl = ospeed;
1399 sc->sc_dlbh = ospeed >> 8;
1400
1401 /*
1402 * Set the FIFO threshold based on the receive speed.
1403 *
1404 * * If it's a low speed, it's probably a mouse or some other
1405 * interactive device, so set the threshold low.
1406 * * If it's a high speed, trim the trigger level down to prevent
1407 * overflows.
1408 * * Otherwise set it a bit higher.
1409 */
1410 if (ISSET(sc->sc_hwflags, COM_HW_HAYESP))
1411 sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
1412 else if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
1413 sc->sc_fifo = FIFO_ENABLE |
1414 (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
1415 t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
1416 else
1417 sc->sc_fifo = 0;
1418
1419 /* And copy to tty. */
1420 tp->t_ispeed = 0;
1421 tp->t_ospeed = t->c_ospeed;
1422 tp->t_cflag = t->c_cflag;
1423
1424 if (!sc->sc_heldchange) {
1425 if (sc->sc_tx_busy) {
1426 sc->sc_heldtbc = sc->sc_tbc;
1427 sc->sc_tbc = 0;
1428 sc->sc_heldchange = 1;
1429 } else
1430 com_loadchannelregs(sc);
1431 }
1432
1433 if (!ISSET(t->c_cflag, CHWFLOW)) {
1434 /* Disable the high water mark. */
1435 sc->sc_r_hiwat = 0;
1436 sc->sc_r_lowat = 0;
1437 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1438 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1439 com_schedrx(sc);
1440 }
1441 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1442 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1443 com_hwiflow(sc);
1444 }
1445 } else {
1446 sc->sc_r_hiwat = com_rbuf_hiwat;
1447 sc->sc_r_lowat = com_rbuf_lowat;
1448 }
1449
1450 splx(s);
1451
1452 /*
1453 * Update the tty layer's idea of the carrier bit, in case we changed
1454 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1455 * explicit request.
1456 */
1457 (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
1458
1459 #ifdef COM_DEBUG
1460 if (com_debug)
1461 comstatus(sc, "comparam ");
1462 #endif
1463
1464 if (!ISSET(t->c_cflag, CHWFLOW)) {
1465 if (sc->sc_tx_stopped) {
1466 sc->sc_tx_stopped = 0;
1467 comstart(tp);
1468 }
1469 }
1470
1471 return (0);
1472 }
1473
1474 void
1475 com_iflush(sc)
1476 struct com_softc *sc;
1477 {
1478 bus_space_tag_t iot = sc->sc_iot;
1479 bus_space_handle_t ioh = sc->sc_ioh;
1480 #ifdef DIAGNOSTIC
1481 int reg;
1482 #endif
1483 int timo;
1484
1485 #ifdef DIAGNOSTIC
1486 reg = 0xffff;
1487 #endif
1488 timo = 50000;
1489 /* flush any pending I/O */
1490 while (ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY)
1491 && --timo)
1492 #ifdef DIAGNOSTIC
1493 reg =
1494 #else
1495 (void)
1496 #endif
1497 bus_space_read_1(iot, ioh, com_data);
1498 #ifdef DIAGNOSTIC
1499 if (!timo)
1500 printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
1501 reg);
1502 #endif
1503 }
1504
1505 void
1506 com_loadchannelregs(sc)
1507 struct com_softc *sc;
1508 {
1509 bus_space_tag_t iot = sc->sc_iot;
1510 bus_space_handle_t ioh = sc->sc_ioh;
1511
1512 /* XXXXX necessary? */
1513 com_iflush(sc);
1514
1515 bus_space_write_1(iot, ioh, com_ier, 0);
1516
1517 if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
1518 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
1519 bus_space_write_1(iot, ioh, com_efr, sc->sc_efr);
1520 }
1521 bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr | LCR_DLAB);
1522 bus_space_write_1(iot, ioh, com_dlbl, sc->sc_dlbl);
1523 bus_space_write_1(iot, ioh, com_dlbh, sc->sc_dlbh);
1524 bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
1525 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active = sc->sc_mcr);
1526 bus_space_write_1(iot, ioh, com_fifo, sc->sc_fifo);
1527
1528 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1529 }
1530
1531 int
1532 comhwiflow(tp, block)
1533 struct tty *tp;
1534 int block;
1535 {
1536 struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
1537 int s;
1538
1539 if (COM_ISALIVE(sc) == 0)
1540 return (0);
1541
1542 if (sc->sc_mcr_rts == 0)
1543 return (0);
1544
1545 s = splserial();
1546 if (block) {
1547 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1548 SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1549 com_hwiflow(sc);
1550 }
1551 } else {
1552 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1553 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1554 com_schedrx(sc);
1555 }
1556 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1557 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1558 com_hwiflow(sc);
1559 }
1560 }
1561 splx(s);
1562 return (1);
1563 }
1564
1565 /*
1566 * (un)block input via hw flowcontrol
1567 */
1568 void
1569 com_hwiflow(sc)
1570 struct com_softc *sc;
1571 {
1572 bus_space_tag_t iot = sc->sc_iot;
1573 bus_space_handle_t ioh = sc->sc_ioh;
1574
1575 if (sc->sc_mcr_rts == 0)
1576 return;
1577
1578 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1579 CLR(sc->sc_mcr, sc->sc_mcr_rts);
1580 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
1581 } else {
1582 SET(sc->sc_mcr, sc->sc_mcr_rts);
1583 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
1584 }
1585 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
1586 }
1587
1588
1589 void
1590 comstart(tp)
1591 struct tty *tp;
1592 {
1593 struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
1594 bus_space_tag_t iot = sc->sc_iot;
1595 bus_space_handle_t ioh = sc->sc_ioh;
1596 int s;
1597
1598 if (COM_ISALIVE(sc) == 0)
1599 return;
1600
1601 s = spltty();
1602 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1603 goto out;
1604 if (sc->sc_tx_stopped)
1605 goto out;
1606
1607 if (tp->t_outq.c_cc <= tp->t_lowat) {
1608 if (ISSET(tp->t_state, TS_ASLEEP)) {
1609 CLR(tp->t_state, TS_ASLEEP);
1610 wakeup(&tp->t_outq);
1611 }
1612 selwakeup(&tp->t_wsel);
1613 if (tp->t_outq.c_cc == 0)
1614 goto out;
1615 }
1616
1617 /* Grab the first contiguous region of buffer space. */
1618 {
1619 u_char *tba;
1620 int tbc;
1621
1622 tba = tp->t_outq.c_cf;
1623 tbc = ndqb(&tp->t_outq, 0);
1624
1625 (void)splserial();
1626
1627 sc->sc_tba = tba;
1628 sc->sc_tbc = tbc;
1629 }
1630
1631 SET(tp->t_state, TS_BUSY);
1632 sc->sc_tx_busy = 1;
1633
1634 /* Enable transmit completion interrupts if necessary. */
1635 if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
1636 SET(sc->sc_ier, IER_ETXRDY);
1637 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1638 }
1639
1640 /* Output the first chunk of the contiguous buffer. */
1641 {
1642 int n;
1643
1644 n = sc->sc_tbc;
1645 if (n > sc->sc_fifolen)
1646 n = sc->sc_fifolen;
1647 bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
1648 sc->sc_tbc -= n;
1649 sc->sc_tba += n;
1650 }
1651 out:
1652 splx(s);
1653 return;
1654 }
1655
1656 /*
1657 * Stop output on a line.
1658 */
1659 void
1660 comstop(tp, flag)
1661 struct tty *tp;
1662 int flag;
1663 {
1664 struct com_softc *sc = device_lookup(&com_cd, COMUNIT(tp->t_dev));
1665 int s;
1666
1667 s = splserial();
1668 if (ISSET(tp->t_state, TS_BUSY)) {
1669 /* Stop transmitting at the next chunk. */
1670 sc->sc_tbc = 0;
1671 sc->sc_heldtbc = 0;
1672 if (!ISSET(tp->t_state, TS_TTSTOP))
1673 SET(tp->t_state, TS_FLUSH);
1674 }
1675 splx(s);
1676 }
1677
1678 void
1679 comdiag(arg)
1680 void *arg;
1681 {
1682 struct com_softc *sc = arg;
1683 int overflows, floods;
1684 int s;
1685
1686 s = splserial();
1687 overflows = sc->sc_overflows;
1688 sc->sc_overflows = 0;
1689 floods = sc->sc_floods;
1690 sc->sc_floods = 0;
1691 sc->sc_errors = 0;
1692 splx(s);
1693
1694 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1695 sc->sc_dev.dv_xname,
1696 overflows, overflows == 1 ? "" : "s",
1697 floods, floods == 1 ? "" : "s");
1698 }
1699
1700 integrate void
1701 com_rxsoft(sc, tp)
1702 struct com_softc *sc;
1703 struct tty *tp;
1704 {
1705 int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
1706 u_char *get, *end;
1707 u_int cc, scc;
1708 u_char lsr;
1709 int code;
1710 int s;
1711
1712 end = sc->sc_ebuf;
1713 get = sc->sc_rbget;
1714 scc = cc = com_rbuf_size - sc->sc_rbavail;
1715
1716 if (cc == com_rbuf_size) {
1717 sc->sc_floods++;
1718 if (sc->sc_errors++ == 0)
1719 callout_reset(&sc->sc_diag_callout, 60 * hz,
1720 comdiag, sc);
1721 }
1722
1723 while (cc) {
1724 code = get[0];
1725 lsr = get[1];
1726 if (ISSET(lsr, LSR_OE | LSR_BI | LSR_FE | LSR_PE)) {
1727 if (ISSET(lsr, LSR_OE)) {
1728 sc->sc_overflows++;
1729 if (sc->sc_errors++ == 0)
1730 callout_reset(&sc->sc_diag_callout,
1731 60 * hz, comdiag, sc);
1732 }
1733 if (ISSET(lsr, LSR_BI | LSR_FE))
1734 SET(code, TTY_FE);
1735 if (ISSET(lsr, LSR_PE))
1736 SET(code, TTY_PE);
1737 }
1738 if ((*rint)(code, tp) == -1) {
1739 /*
1740 * The line discipline's buffer is out of space.
1741 */
1742 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1743 /*
1744 * We're either not using flow control, or the
1745 * line discipline didn't tell us to block for
1746 * some reason. Either way, we have no way to
1747 * know when there's more space available, so
1748 * just drop the rest of the data.
1749 */
1750 get += cc << 1;
1751 if (get >= end)
1752 get -= com_rbuf_size << 1;
1753 cc = 0;
1754 } else {
1755 /*
1756 * Don't schedule any more receive processing
1757 * until the line discipline tells us there's
1758 * space available (through comhwiflow()).
1759 * Leave the rest of the data in the input
1760 * buffer.
1761 */
1762 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1763 }
1764 break;
1765 }
1766 get += 2;
1767 if (get >= end)
1768 get = sc->sc_rbuf;
1769 cc--;
1770 }
1771
1772 if (cc != scc) {
1773 sc->sc_rbget = get;
1774 s = splserial();
1775 cc = sc->sc_rbavail += scc - cc;
1776 /* Buffers should be ok again, release possible block. */
1777 if (cc >= sc->sc_r_lowat) {
1778 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1779 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1780 SET(sc->sc_ier, IER_ERXRDY);
1781 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
1782 }
1783 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1784 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1785 com_hwiflow(sc);
1786 }
1787 }
1788 splx(s);
1789 }
1790 }
1791
1792 integrate void
1793 com_txsoft(sc, tp)
1794 struct com_softc *sc;
1795 struct tty *tp;
1796 {
1797
1798 CLR(tp->t_state, TS_BUSY);
1799 if (ISSET(tp->t_state, TS_FLUSH))
1800 CLR(tp->t_state, TS_FLUSH);
1801 else
1802 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1803 (*linesw[tp->t_line].l_start)(tp);
1804 }
1805
1806 integrate void
1807 com_stsoft(sc, tp)
1808 struct com_softc *sc;
1809 struct tty *tp;
1810 {
1811 u_char msr, delta;
1812 int s;
1813
1814 s = splserial();
1815 msr = sc->sc_msr;
1816 delta = sc->sc_msr_delta;
1817 sc->sc_msr_delta = 0;
1818 splx(s);
1819
1820 if (ISSET(delta, sc->sc_msr_dcd)) {
1821 /*
1822 * Inform the tty layer that carrier detect changed.
1823 */
1824 (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD));
1825 }
1826
1827 if (ISSET(delta, sc->sc_msr_cts)) {
1828 /* Block or unblock output according to flow control. */
1829 if (ISSET(msr, sc->sc_msr_cts)) {
1830 sc->sc_tx_stopped = 0;
1831 (*linesw[tp->t_line].l_start)(tp);
1832 } else {
1833 sc->sc_tx_stopped = 1;
1834 }
1835 }
1836
1837 #ifdef COM_DEBUG
1838 if (com_debug)
1839 comstatus(sc, "com_stsoft");
1840 #endif
1841 }
1842
1843 #ifdef __GENERIC_SOFT_INTERRUPTS
1844 void
1845 comsoft(arg)
1846 void *arg;
1847 {
1848 struct com_softc *sc = arg;
1849 struct tty *tp;
1850
1851 if (COM_ISALIVE(sc) == 0)
1852 return;
1853
1854 {
1855 #else
1856 void
1857 #ifndef __NO_SOFT_SERIAL_INTERRUPT
1858 comsoft()
1859 #else
1860 comsoft(arg)
1861 void *arg;
1862 #endif
1863 {
1864 struct com_softc *sc;
1865 struct tty *tp;
1866 int unit;
1867 #ifdef __NO_SOFT_SERIAL_INTERRUPT
1868 int s;
1869
1870 s = splsoftserial();
1871 com_softintr_scheduled = 0;
1872 #endif
1873
1874 for (unit = 0; unit < com_cd.cd_ndevs; unit++) {
1875 sc = device_lookup(&com_cd, unit);
1876 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
1877 continue;
1878
1879 if (COM_ISALIVE(sc) == 0)
1880 continue;
1881
1882 tp = sc->sc_tty;
1883 if (tp == NULL)
1884 continue;
1885 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
1886 continue;
1887 #endif
1888 tp = sc->sc_tty;
1889
1890 if (sc->sc_rx_ready) {
1891 sc->sc_rx_ready = 0;
1892 com_rxsoft(sc, tp);
1893 }
1894
1895 if (sc->sc_st_check) {
1896 sc->sc_st_check = 0;
1897 com_stsoft(sc, tp);
1898 }
1899
1900 if (sc->sc_tx_done) {
1901 sc->sc_tx_done = 0;
1902 com_txsoft(sc, tp);
1903 }
1904 }
1905
1906 #ifndef __GENERIC_SOFT_INTERRUPTS
1907 #ifdef __NO_SOFT_SERIAL_INTERRUPT
1908 splx(s);
1909 #endif
1910 #endif
1911 }
1912
1913 #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS
1914 /* there has got to be a better way to do comsoft() */
1915 }}
1916 #endif
1917
1918 int
1919 comintr(arg)
1920 void *arg;
1921 {
1922 struct com_softc *sc = arg;
1923 bus_space_tag_t iot = sc->sc_iot;
1924 bus_space_handle_t ioh = sc->sc_ioh;
1925 u_char *put, *end;
1926 u_int cc;
1927 u_char lsr, iir;
1928
1929 if (COM_ISALIVE(sc) == 0)
1930 return (0);
1931
1932 iir = bus_space_read_1(iot, ioh, com_iir);
1933 if (ISSET(iir, IIR_NOPEND))
1934 return (0);
1935
1936 end = sc->sc_ebuf;
1937 put = sc->sc_rbput;
1938 cc = sc->sc_rbavail;
1939
1940 do {
1941 u_char msr, delta;
1942
1943 lsr = bus_space_read_1(iot, ioh, com_lsr);
1944 #if defined(DDB) || defined(KGDB)
1945 if (ISSET(lsr, LSR_BI)) {
1946 #ifndef DDB_BREAK_CHAR
1947 #ifdef DDB
1948 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1949 console_debugger();
1950 continue;
1951 }
1952 #endif
1953 #ifdef KGDB
1954 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
1955 kgdb_connect(1);
1956 continue;
1957 }
1958 #endif
1959 #endif
1960 }
1961 #endif /* DDB || KGDB */
1962
1963 if (ISSET(lsr, LSR_RCV_MASK) &&
1964 !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1965 while (cc > 0) {
1966 put[0] = bus_space_read_1(iot, ioh, com_data);
1967 put[1] = lsr;
1968 #if defined(DDB) && defined(DDB_BREAKCHAR)
1969 if (put[0] == DDB_BREAK_CHAR &&
1970 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1971 console_debugger();
1972
1973 lsr = bus_space_read_1(iot, ioh, com_lsr);
1974 if (!ISSET(lsr, LSR_RCV_MASK))
1975 break;
1976
1977 continue;
1978 }
1979 #endif
1980 put += 2;
1981 if (put >= end)
1982 put = sc->sc_rbuf;
1983 cc--;
1984
1985 lsr = bus_space_read_1(iot, ioh, com_lsr);
1986 if (!ISSET(lsr, LSR_RCV_MASK))
1987 break;
1988 }
1989
1990 /*
1991 * Current string of incoming characters ended because
1992 * no more data was available or we ran out of space.
1993 * Schedule a receive event if any data was received.
1994 * If we're out of space, turn off receive interrupts.
1995 */
1996 sc->sc_rbput = put;
1997 sc->sc_rbavail = cc;
1998 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1999 sc->sc_rx_ready = 1;
2000
2001 /*
2002 * See if we are in danger of overflowing a buffer. If
2003 * so, use hardware flow control to ease the pressure.
2004 */
2005 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
2006 cc < sc->sc_r_hiwat) {
2007 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
2008 com_hwiflow(sc);
2009 }
2010
2011 /*
2012 * If we're out of space, disable receive interrupts
2013 * until the queue has drained a bit.
2014 */
2015 if (!cc) {
2016 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
2017 CLR(sc->sc_ier, IER_ERXRDY);
2018 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
2019 }
2020 } else {
2021 if ((iir & IIR_IMASK) == IIR_RXRDY) {
2022 bus_space_write_1(iot, ioh, com_ier, 0);
2023 delay(10);
2024 bus_space_write_1(iot, ioh, com_ier,sc->sc_ier);
2025 iir = IIR_NOPEND;
2026 continue;
2027 }
2028 }
2029
2030 msr = bus_space_read_1(iot, ioh, com_msr);
2031 delta = msr ^ sc->sc_msr;
2032 sc->sc_msr = msr;
2033 /*
2034 * Pulse-per-second (PSS) signals on edge of DCD?
2035 * Process these even if line discipline is ignoring DCD.
2036 */
2037 if (delta & sc->sc_ppsmask) {
2038 struct timeval tv;
2039 if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) {
2040 /* XXX nanotime() */
2041 microtime(&tv);
2042 TIMEVAL_TO_TIMESPEC(&tv,
2043 &sc->ppsinfo.assert_timestamp);
2044 if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
2045 timespecadd(&sc->ppsinfo.assert_timestamp,
2046 &sc->ppsparam.assert_offset,
2047 &sc->ppsinfo.assert_timestamp);
2048 }
2049
2050 #ifdef PPS_SYNC
2051 if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
2052 hardpps(&tv, tv.tv_usec);
2053 #endif
2054 sc->ppsinfo.assert_sequence++;
2055 sc->ppsinfo.current_mode = sc->ppsparam.mode;
2056
2057 } else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) {
2058 /* XXX nanotime() */
2059 microtime(&tv);
2060 TIMEVAL_TO_TIMESPEC(&tv,
2061 &sc->ppsinfo.clear_timestamp);
2062 if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
2063 timespecadd(&sc->ppsinfo.clear_timestamp,
2064 &sc->ppsparam.clear_offset,
2065 &sc->ppsinfo.clear_timestamp);
2066 }
2067
2068 #ifdef PPS_SYNC
2069 if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
2070 hardpps(&tv, tv.tv_usec);
2071 #endif
2072 sc->ppsinfo.clear_sequence++;
2073 sc->ppsinfo.current_mode = sc->ppsparam.mode;
2074 }
2075 }
2076
2077 /*
2078 * Process normal status changes
2079 */
2080 if (ISSET(delta, sc->sc_msr_mask)) {
2081 SET(sc->sc_msr_delta, delta);
2082
2083 /*
2084 * Stop output immediately if we lose the output
2085 * flow control signal or carrier detect.
2086 */
2087 if (ISSET(~msr, sc->sc_msr_mask)) {
2088 sc->sc_tbc = 0;
2089 sc->sc_heldtbc = 0;
2090 #ifdef COM_DEBUG
2091 if (com_debug)
2092 comstatus(sc, "comintr ");
2093 #endif
2094 }
2095
2096 sc->sc_st_check = 1;
2097 }
2098 } while (!ISSET((iir = bus_space_read_1(iot, ioh, com_iir)), IIR_NOPEND));
2099
2100 /*
2101 * Done handling any receive interrupts. See if data can be
2102 * transmitted as well. Schedule tx done event if no data left
2103 * and tty was marked busy.
2104 */
2105 if (ISSET(lsr, LSR_TXRDY)) {
2106 /*
2107 * If we've delayed a parameter change, do it now, and restart
2108 * output.
2109 */
2110 if (sc->sc_heldchange) {
2111 com_loadchannelregs(sc);
2112 sc->sc_heldchange = 0;
2113 sc->sc_tbc = sc->sc_heldtbc;
2114 sc->sc_heldtbc = 0;
2115 }
2116
2117 /* Output the next chunk of the contiguous buffer, if any. */
2118 if (sc->sc_tbc > 0) {
2119 int n;
2120
2121 n = sc->sc_tbc;
2122 if (n > sc->sc_fifolen)
2123 n = sc->sc_fifolen;
2124 bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
2125 sc->sc_tbc -= n;
2126 sc->sc_tba += n;
2127 } else {
2128 /* Disable transmit completion interrupts if necessary. */
2129 if (ISSET(sc->sc_ier, IER_ETXRDY)) {
2130 CLR(sc->sc_ier, IER_ETXRDY);
2131 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
2132 }
2133 if (sc->sc_tx_busy) {
2134 sc->sc_tx_busy = 0;
2135 sc->sc_tx_done = 1;
2136 }
2137 }
2138 }
2139
2140 /* Wake up the poller. */
2141 #ifdef __GENERIC_SOFT_INTERRUPTS
2142 softintr_schedule(sc->sc_si);
2143 #else
2144 #ifndef __NO_SOFT_SERIAL_INTERRUPT
2145 setsoftserial();
2146 #else
2147 if (!com_softintr_scheduled) {
2148 com_softintr_scheduled = 1;
2149 callout_reset(&comsoft_callout, 1, comsoft, NULL);
2150 }
2151 #endif
2152 #endif
2153
2154 #if NRND > 0 && defined(RND_COM)
2155 rnd_add_uint32(&sc->rnd_source, iir | lsr);
2156 #endif
2157
2158 return (1);
2159 }
2160
2161 /*
2162 * The following functions are polled getc and putc routines, shared
2163 * by the console and kgdb glue.
2164 */
2165
2166 #if defined(DDB) && defined(DDB_BREAK_CHAR)
2167 #define MAX_UNGETC 20
2168 static int com_ungetc[MAX_UNGETC];
2169 static int com_ungetccount = 0;
2170 #endif
2171
2172 int
2173 com_common_getc(iot, ioh)
2174 bus_space_tag_t iot;
2175 bus_space_handle_t ioh;
2176 {
2177 int s = splserial();
2178 u_char stat, c;
2179
2180 #if defined(DDB) && defined(DDB_BREAK_CHAR)
2181 /* got a character from reading things earlier */
2182 if (com_ungetccount > 0) {
2183 int i;
2184
2185 c = com_ungetc[0];
2186 for (i = 1; i < com_ungetccount; i++) {
2187 com_ungetc[i -1] = com_ungetc[i];
2188 }
2189 com_ungetccount--;
2190 splx(s);
2191 return (c);
2192 }
2193 #endif
2194
2195 /* block until a character becomes available */
2196 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY))
2197 ;
2198
2199 c = bus_space_read_1(iot, ioh, com_data);
2200 stat = bus_space_read_1(iot, ioh, com_iir);
2201 #if defined(DDB) && defined(DDB_BREAK_CHAR)
2202 if (c == DDB_BREAK_CHAR) {
2203 extern int db_active;
2204
2205 if (db_active == 0) {
2206 console_debugger();
2207 }
2208 }
2209 #endif
2210 splx(s);
2211 return (c);
2212 }
2213
2214 void
2215 com_common_putc(iot, ioh, c)
2216 bus_space_tag_t iot;
2217 bus_space_handle_t ioh;
2218 int c;
2219 {
2220 int s = splserial();
2221 int timo;
2222
2223 #if defined(DDB) && defined(DDB_BREAK_CHAR)
2224 int cin, stat;
2225 if (com_ungetccount < MAX_UNGETC
2226 && ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY)) {
2227 cin = bus_space_read_1(iot, ioh, com_data);
2228 stat = bus_space_read_1(iot, ioh, com_iir);
2229 if (cin == DDB_BREAK_CHAR) {
2230 console_debugger();
2231 }
2232 com_ungetc[com_ungetccount++] = cin;
2233 }
2234 #endif
2235
2236 /* wait for any pending transmission to finish */
2237 timo = 150000;
2238 while (!ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY) && --timo)
2239 continue;
2240
2241 bus_space_write_1(iot, ioh, com_data, c);
2242 COM_BARRIER(iot, ioh, BR | BW);
2243
2244 /* wait for this transmission to complete */
2245 timo = 1500000;
2246 while (!ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY) && --timo)
2247 continue;
2248
2249 splx(s);
2250 }
2251
2252 /*
2253 * Initialize UART for use as console or KGDB line.
2254 */
2255 int
2256 cominit(iot, iobase, rate, frequency, cflag, iohp)
2257 bus_space_tag_t iot;
2258 int iobase;
2259 int rate, frequency;
2260 tcflag_t cflag;
2261 bus_space_handle_t *iohp;
2262 {
2263 bus_space_handle_t ioh;
2264
2265 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
2266 return (ENOMEM); /* ??? */
2267
2268 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
2269 bus_space_write_1(iot, ioh, com_efr, 0);
2270 bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
2271 rate = comspeed(rate, frequency);
2272 bus_space_write_1(iot, ioh, com_dlbl, rate);
2273 bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
2274 bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
2275 bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS);
2276 bus_space_write_1(iot, ioh, com_fifo,
2277 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
2278 bus_space_write_1(iot, ioh, com_ier, 0);
2279
2280 *iohp = ioh;
2281 return (0);
2282 }
2283
2284 /*
2285 * Following are all routines needed for COM to act as console
2286 */
2287
2288 int
2289 comcnattach(iot, iobase, rate, frequency, cflag)
2290 bus_space_tag_t iot;
2291 int iobase;
2292 int rate, frequency;
2293 tcflag_t cflag;
2294 {
2295 int res;
2296 static struct consdev comcons = {
2297 NULL, NULL, comcngetc, comcnputc, comcnpollc, NULL,
2298 NODEV, CN_NORMAL
2299 };
2300
2301 res = cominit(iot, iobase, rate, frequency, cflag, &comconsioh);
2302 if (res)
2303 return (res);
2304
2305 cn_tab = &comcons;
2306
2307 comconstag = iot;
2308 comconsaddr = iobase;
2309 comconsrate = rate;
2310 comconscflag = cflag;
2311
2312 return (0);
2313 }
2314
2315 int
2316 comcngetc(dev)
2317 dev_t dev;
2318 {
2319
2320 return (com_common_getc(comconstag, comconsioh));
2321 }
2322
2323 /*
2324 * Console kernel output character routine.
2325 */
2326 void
2327 comcnputc(dev, c)
2328 dev_t dev;
2329 int c;
2330 {
2331
2332 com_common_putc(comconstag, comconsioh, c);
2333 }
2334
2335 void
2336 comcnpollc(dev, on)
2337 dev_t dev;
2338 int on;
2339 {
2340
2341 }
2342
2343 #ifdef KGDB
2344 int
2345 com_kgdb_attach(iot, iobase, rate, frequency, cflag)
2346 bus_space_tag_t iot;
2347 int iobase;
2348 int rate, frequency;
2349 tcflag_t cflag;
2350 {
2351 int res;
2352
2353 if (iot == comconstag && iobase == comconsaddr)
2354 return (EBUSY); /* cannot share with console */
2355
2356 res = cominit(iot, iobase, rate, frequency, cflag, &com_kgdb_ioh);
2357 if (res)
2358 return (res);
2359
2360 kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
2361 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
2362
2363 com_kgdb_iot = iot;
2364 com_kgdb_addr = iobase;
2365
2366 return (0);
2367 }
2368
2369 /* ARGSUSED */
2370 int
2371 com_kgdb_getc(arg)
2372 void *arg;
2373 {
2374
2375 return (com_common_getc(com_kgdb_iot, com_kgdb_ioh));
2376 }
2377
2378 /* ARGSUSED */
2379 void
2380 com_kgdb_putc(arg, c)
2381 void *arg;
2382 int c;
2383 {
2384
2385 return (com_common_putc(com_kgdb_iot, com_kgdb_ioh, c));
2386 }
2387 #endif /* KGDB */
2388
2389 /* helper function to identify the com ports used by
2390 console or KGDB (and not yet autoconf attached) */
2391 int
2392 com_is_console(iot, iobase, ioh)
2393 bus_space_tag_t iot;
2394 int iobase;
2395 bus_space_handle_t *ioh;
2396 {
2397 bus_space_handle_t help;
2398
2399 if (!comconsattached &&
2400 iot == comconstag && iobase == comconsaddr)
2401 help = comconsioh;
2402 #ifdef KGDB
2403 else if (!com_kgdb_attached &&
2404 iot == com_kgdb_iot && iobase == com_kgdb_addr)
2405 help = com_kgdb_ioh;
2406 #endif
2407 else
2408 return (0);
2409
2410 if (ioh)
2411 *ioh = help;
2412 return (1);
2413 }
2414