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