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