com.c revision 1.132 1 /* $NetBSD: com.c,v 1.132 1998/02/02 22:54:55 cgd 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 alpha
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 alpha
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 printf("%s: device enable failed\n",
631 sc->sc_dev.dv_xname);
632 return (EIO);
633 }
634 sc->enabled = 1;
635 com_config(sc);
636 }
637
638 /* Turn on interrupts. */
639 sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
640 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
641
642 /* Fetch the current modem control status, needed later. */
643 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
644
645 splx(s2);
646
647 /*
648 * Initialize the termios status to the defaults. Add in the
649 * sticky bits from TIOCSFLAGS.
650 */
651 t.c_ispeed = 0;
652 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
653 t.c_ospeed = comconsrate;
654 t.c_cflag = comconscflag;
655 } else {
656 t.c_ospeed = TTYDEF_SPEED;
657 t.c_cflag = TTYDEF_CFLAG;
658 }
659 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
660 SET(t.c_cflag, CLOCAL);
661 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
662 SET(t.c_cflag, CRTSCTS);
663 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
664 SET(t.c_cflag, MDMBUF);
665 /* Make sure comparam() will do something. */
666 tp->t_ospeed = 0;
667 (void) comparam(tp, &t);
668 tp->t_iflag = TTYDEF_IFLAG;
669 tp->t_oflag = TTYDEF_OFLAG;
670 tp->t_lflag = TTYDEF_LFLAG;
671 ttychars(tp);
672 ttsetwater(tp);
673
674 /*
675 * Turn on DTR. We must always do this, even if carrier is not
676 * present, because otherwise we'd have to use TIOCSDTR
677 * immediately after setting CLOCAL, which applications do not
678 * expect. We always assert DTR while the device is open
679 * unless explicitly requested to deassert it.
680 */
681 com_modem(sc, 1);
682
683 s2 = splserial();
684
685 /* Clear the input ring, and unblock. */
686 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
687 sc->sc_rbavail = com_rbuf_size;
688 com_iflush(sc);
689 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
690 com_hwiflow(sc);
691
692 #ifdef COM_DEBUG
693 if (com_debug)
694 comstatus(sc, "comopen ");
695 #endif
696
697 splx(s2);
698 }
699 error = 0;
700
701 /* If we're doing a blocking open... */
702 if (!ISSET(flag, O_NONBLOCK))
703 /* ...then wait for carrier. */
704 while (!ISSET(tp->t_state, TS_CARR_ON) &&
705 !ISSET(tp->t_cflag, CLOCAL | MDMBUF)) {
706 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
707 ttopen, 0);
708 if (error) {
709 /*
710 * If the open was interrupted and nobody
711 * else has the device open, then hang up.
712 */
713 if (!ISSET(tp->t_state, TS_ISOPEN)) {
714 com_modem(sc, 0);
715 CLR(tp->t_state, TS_WOPEN);
716 ttwakeup(tp);
717 }
718 break;
719 }
720 SET(tp->t_state, TS_WOPEN);
721 }
722
723 splx(s);
724 if (error == 0)
725 error = (*linesw[tp->t_line].l_open)(dev, tp);
726 return (error);
727 }
728
729 int
730 comclose(dev, flag, mode, p)
731 dev_t dev;
732 int flag, mode;
733 struct proc *p;
734 {
735 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
736 struct tty *tp = sc->sc_tty;
737 int s;
738
739 /* XXX This is for cons.c. */
740 if (!ISSET(tp->t_state, TS_ISOPEN))
741 return (0);
742
743 (*linesw[tp->t_line].l_close)(tp, flag);
744 ttyclose(tp);
745
746 s = splserial();
747
748 /* If we were asserting flow control, then deassert it. */
749 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
750 com_hwiflow(sc);
751
752 splx(s);
753
754 /* Clear any break condition set with TIOCSBRK. */
755 com_break(sc, 0);
756
757 /*
758 * Hang up if necessary. Wait a bit, so the other side has time to
759 * notice even if we immediately open the port again.
760 */
761 if (ISSET(tp->t_cflag, HUPCL)) {
762 com_modem(sc, 0);
763 (void) tsleep(sc, TTIPRI, ttclos, hz);
764 }
765
766 s = splserial();
767
768 /* Turn off interrupts. */
769 #ifdef DDB
770 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
771 sc->sc_ier = IER_ERXRDY; /* interrupt on break */
772 else
773 #endif
774 sc->sc_ier = 0;
775 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
776
777 if (sc->disable) {
778 if (sc->enabled) {
779 (*sc->disable)(sc);
780 sc->enabled = 0;
781 }
782 }
783
784 splx(s);
785
786 return (0);
787 }
788
789 int
790 comread(dev, uio, flag)
791 dev_t dev;
792 struct uio *uio;
793 int flag;
794 {
795 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
796 struct tty *tp = sc->sc_tty;
797
798 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
799 }
800
801 int
802 comwrite(dev, uio, flag)
803 dev_t dev;
804 struct uio *uio;
805 int flag;
806 {
807 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
808 struct tty *tp = sc->sc_tty;
809
810 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
811 }
812
813 struct tty *
814 comtty(dev)
815 dev_t dev;
816 {
817 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
818 struct tty *tp = sc->sc_tty;
819
820 return (tp);
821 }
822
823 static u_char
824 tiocm_xxx2mcr(data)
825 int data;
826 {
827 u_char m = 0;
828
829 if (ISSET(data, TIOCM_DTR))
830 SET(m, MCR_DTR);
831 if (ISSET(data, TIOCM_RTS))
832 SET(m, MCR_RTS);
833 return m;
834 }
835
836 int
837 comioctl(dev, cmd, data, flag, p)
838 dev_t dev;
839 u_long cmd;
840 caddr_t data;
841 int flag;
842 struct proc *p;
843 {
844 struct com_softc *sc = com_cd.cd_devs[COMUNIT(dev)];
845 struct tty *tp = sc->sc_tty;
846 int error;
847
848 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
849 if (error >= 0)
850 return (error);
851
852 error = ttioctl(tp, cmd, data, flag, p);
853 if (error >= 0)
854 return (error);
855
856 switch (cmd) {
857 case TIOCSBRK:
858 com_break(sc, 1);
859 break;
860
861 case TIOCCBRK:
862 com_break(sc, 0);
863 break;
864
865 case TIOCSDTR:
866 com_modem(sc, 1);
867 break;
868
869 case TIOCCDTR:
870 com_modem(sc, 0);
871 break;
872
873 case TIOCGFLAGS:
874 *(int *)data = sc->sc_swflags;
875 break;
876
877 case TIOCSFLAGS:
878 error = suser(p->p_ucred, &p->p_acflag);
879 if (error)
880 return (error);
881 sc->sc_swflags = *(int *)data;
882 break;
883
884 case TIOCMSET:
885 CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
886 /*FALLTHROUGH*/
887
888 case TIOCMBIS:
889 SET(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
890 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
891 break;
892
893 case TIOCMBIC:
894 CLR(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
895 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_mcr, sc->sc_mcr);
896 break;
897
898 case TIOCMGET: {
899 u_char m;
900 int bits = 0;
901
902 m = sc->sc_mcr;
903 if (ISSET(m, MCR_DTR))
904 SET(bits, TIOCM_DTR);
905 if (ISSET(m, MCR_RTS))
906 SET(bits, TIOCM_RTS);
907 m = sc->sc_msr;
908 if (ISSET(m, MSR_DCD))
909 SET(bits, TIOCM_CD);
910 if (ISSET(m, MSR_CTS))
911 SET(bits, TIOCM_CTS);
912 if (ISSET(m, MSR_DSR))
913 SET(bits, TIOCM_DSR);
914 if (ISSET(m, MSR_RI | MSR_TERI))
915 SET(bits, TIOCM_RI);
916 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_ier))
917 SET(bits, TIOCM_LE);
918 *(int *)data = bits;
919 break;
920 }
921 default:
922 return (ENOTTY);
923 }
924
925 #ifdef COM_DEBUG
926 if (com_debug)
927 comstatus(sc, "comioctl ");
928 #endif
929
930 return (0);
931 }
932
933 integrate void
934 com_schedrx(sc)
935 struct com_softc *sc;
936 {
937
938 sc->sc_rx_ready = 1;
939
940 /* Wake up the poller. */
941 #ifdef __GENERIC_SOFT_INTERRUPTS
942 softintr_schedule(sc->sc_si);
943 #else
944 #ifndef alpha
945 setsoftserial();
946 #else
947 if (!com_softintr_scheduled) {
948 com_softintr_scheduled = 1;
949 timeout(comsoft, NULL, 1);
950 }
951 #endif
952 #endif
953 }
954
955 void
956 com_break(sc, onoff)
957 struct com_softc *sc;
958 int onoff;
959 {
960 int s;
961
962 s = splserial();
963 if (onoff)
964 SET(sc->sc_lcr, LCR_SBREAK);
965 else
966 CLR(sc->sc_lcr, LCR_SBREAK);
967
968 if (!sc->sc_heldchange) {
969 if (sc->sc_tx_busy) {
970 sc->sc_heldtbc = sc->sc_tbc;
971 sc->sc_tbc = 0;
972 sc->sc_heldchange = 1;
973 } else
974 com_loadchannelregs(sc);
975 }
976 splx(s);
977 }
978
979 void
980 com_modem(sc, onoff)
981 struct com_softc *sc;
982 int onoff;
983 {
984 int s;
985
986 s = splserial();
987 if (onoff)
988 SET(sc->sc_mcr, sc->sc_mcr_dtr);
989 else
990 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
991
992 if (!sc->sc_heldchange) {
993 if (sc->sc_tx_busy) {
994 sc->sc_heldtbc = sc->sc_tbc;
995 sc->sc_tbc = 0;
996 sc->sc_heldchange = 1;
997 } else
998 com_loadchannelregs(sc);
999 }
1000 splx(s);
1001 }
1002
1003 static u_char
1004 cflag2lcr(cflag)
1005 tcflag_t cflag;
1006 {
1007 u_char lcr = 0;
1008
1009 switch (ISSET(cflag, CSIZE)) {
1010 case CS5:
1011 SET(lcr, LCR_5BITS);
1012 break;
1013 case CS6:
1014 SET(lcr, LCR_6BITS);
1015 break;
1016 case CS7:
1017 SET(lcr, LCR_7BITS);
1018 break;
1019 case CS8:
1020 SET(lcr, LCR_8BITS);
1021 break;
1022 }
1023 if (ISSET(cflag, PARENB)) {
1024 SET(lcr, LCR_PENAB);
1025 if (!ISSET(cflag, PARODD))
1026 SET(lcr, LCR_PEVEN);
1027 }
1028 if (ISSET(cflag, CSTOPB))
1029 SET(lcr, LCR_STOPB);
1030
1031 return (lcr);
1032 }
1033
1034 int
1035 comparam(tp, t)
1036 struct tty *tp;
1037 struct termios *t;
1038 {
1039 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1040 int ospeed = comspeed(t->c_ospeed, sc->sc_frequency);
1041 u_char lcr;
1042 int s;
1043
1044 /* Check requested parameters. */
1045 if (ospeed < 0)
1046 return (EINVAL);
1047 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
1048 return (EINVAL);
1049
1050 /*
1051 * For the console, always force CLOCAL and !HUPCL, so that the port
1052 * is always active.
1053 */
1054 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
1055 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1056 SET(t->c_cflag, CLOCAL);
1057 CLR(t->c_cflag, HUPCL);
1058 }
1059
1060 /*
1061 * If there were no changes, don't do anything. This avoids dropping
1062 * input and improves performance when all we did was frob things like
1063 * VMIN and VTIME.
1064 */
1065 if (tp->t_ospeed == t->c_ospeed &&
1066 tp->t_cflag == t->c_cflag)
1067 return (0);
1068
1069 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
1070
1071 s = splserial();
1072
1073 sc->sc_lcr = lcr;
1074
1075 /*
1076 * If we're not in a mode that assumes a connection is present, then
1077 * ignore carrier changes.
1078 */
1079 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1080 sc->sc_msr_dcd = 0;
1081 else
1082 sc->sc_msr_dcd = MSR_DCD;
1083 /*
1084 * Set the flow control pins depending on the current flow control
1085 * mode.
1086 */
1087 if (ISSET(t->c_cflag, CRTSCTS)) {
1088 sc->sc_mcr_dtr = MCR_DTR;
1089 sc->sc_mcr_rts = MCR_RTS;
1090 sc->sc_msr_cts = MSR_CTS;
1091 sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS;
1092 } else if (ISSET(t->c_cflag, MDMBUF)) {
1093 /*
1094 * For DTR/DCD flow control, make sure we don't toggle DTR for
1095 * carrier detection.
1096 */
1097 sc->sc_mcr_dtr = 0;
1098 sc->sc_mcr_rts = MCR_DTR;
1099 sc->sc_msr_cts = MSR_DCD;
1100 sc->sc_efr = 0;
1101 } else {
1102 /*
1103 * If no flow control, then always set RTS. This will make
1104 * the other side happy if it mistakenly thinks we're doing
1105 * RTS/CTS flow control.
1106 */
1107 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS;
1108 sc->sc_mcr_rts = 0;
1109 sc->sc_msr_cts = 0;
1110 sc->sc_efr = 0;
1111 if (ISSET(sc->sc_mcr, MCR_DTR))
1112 SET(sc->sc_mcr, MCR_RTS);
1113 else
1114 CLR(sc->sc_mcr, MCR_RTS);
1115 }
1116 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
1117
1118 #if 0
1119 if (ospeed == 0)
1120 CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1121 else
1122 SET(sc->sc_mcr, sc->sc_mcr_dtr);
1123 #endif
1124
1125 sc->sc_dlbl = ospeed;
1126 sc->sc_dlbh = ospeed >> 8;
1127
1128 /*
1129 * Set the FIFO threshold based on the receive speed.
1130 *
1131 * * If it's a low speed, it's probably a mouse or some other
1132 * interactive device, so set the threshold low.
1133 * * If it's a high speed, trim the trigger level down to prevent
1134 * overflows.
1135 * * Otherwise set it a bit higher.
1136 */
1137 if (ISSET(sc->sc_hwflags, COM_HW_HAYESP))
1138 sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
1139 else if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
1140 sc->sc_fifo = FIFO_ENABLE |
1141 (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
1142 t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
1143 else
1144 sc->sc_fifo = 0;
1145
1146 /* And copy to tty. */
1147 tp->t_ispeed = 0;
1148 tp->t_ospeed = t->c_ospeed;
1149 tp->t_cflag = t->c_cflag;
1150
1151 if (!sc->sc_heldchange) {
1152 if (sc->sc_tx_busy) {
1153 sc->sc_heldtbc = sc->sc_tbc;
1154 sc->sc_tbc = 0;
1155 sc->sc_heldchange = 1;
1156 } else
1157 com_loadchannelregs(sc);
1158 }
1159
1160 if (!ISSET(t->c_cflag, CHWFLOW)) {
1161 /* Disable the high water mark. */
1162 sc->sc_r_hiwat = 0;
1163 sc->sc_r_lowat = 0;
1164 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1165 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1166 com_schedrx(sc);
1167 }
1168 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1169 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1170 com_hwiflow(sc);
1171 }
1172 } else {
1173 sc->sc_r_hiwat = com_rbuf_hiwat;
1174 sc->sc_r_lowat = com_rbuf_lowat;
1175 }
1176
1177 splx(s);
1178
1179 /*
1180 * Update the tty layer's idea of the carrier bit, in case we changed
1181 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1182 * explicit request.
1183 */
1184 (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD));
1185
1186 #ifdef COM_DEBUG
1187 if (com_debug)
1188 comstatus(sc, "comparam ");
1189 #endif
1190
1191 if (!ISSET(t->c_cflag, CHWFLOW)) {
1192 if (sc->sc_tx_stopped) {
1193 sc->sc_tx_stopped = 0;
1194 comstart(tp);
1195 }
1196 }
1197
1198 return (0);
1199 }
1200
1201 void
1202 com_iflush(sc)
1203 struct com_softc *sc;
1204 {
1205 bus_space_tag_t iot = sc->sc_iot;
1206 bus_space_handle_t ioh = sc->sc_ioh;
1207 #ifdef DIAGNOSTIC
1208 int reg;
1209 #endif
1210 int timo;
1211
1212 #ifdef DIAGNOSTIC
1213 reg = 0xffff;
1214 #endif
1215 timo = 50000;
1216 /* flush any pending I/O */
1217 while (ISSET(bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY)
1218 && --timo)
1219 #ifdef DIAGNOSTIC
1220 reg =
1221 #else
1222 (void)
1223 #endif
1224 bus_space_read_1(iot, ioh, com_data);
1225 #ifdef DIAGNOSTIC
1226 if (!timo)
1227 printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
1228 reg);
1229 #endif
1230 }
1231
1232 void
1233 com_loadchannelregs(sc)
1234 struct com_softc *sc;
1235 {
1236 bus_space_tag_t iot = sc->sc_iot;
1237 bus_space_handle_t ioh = sc->sc_ioh;
1238
1239 /* XXXXX necessary? */
1240 com_iflush(sc);
1241
1242 bus_space_write_1(iot, ioh, com_ier, 0);
1243
1244 if (ISSET(sc->sc_hwflags, COM_HW_FLOW)) {
1245 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
1246 bus_space_write_1(iot, ioh, com_efr, sc->sc_efr);
1247 }
1248 bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr | LCR_DLAB);
1249 bus_space_write_1(iot, ioh, com_dlbl, sc->sc_dlbl);
1250 bus_space_write_1(iot, ioh, com_dlbh, sc->sc_dlbh);
1251 bus_space_write_1(iot, ioh, com_lcr, sc->sc_lcr);
1252 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active = sc->sc_mcr);
1253 bus_space_write_1(iot, ioh, com_fifo, sc->sc_fifo);
1254
1255 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1256 }
1257
1258 int
1259 comhwiflow(tp, block)
1260 struct tty *tp;
1261 int block;
1262 {
1263 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1264 int s;
1265
1266 if (sc->sc_mcr_rts == 0)
1267 return (0);
1268
1269 s = splserial();
1270 if (block) {
1271 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1272 SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1273 com_hwiflow(sc);
1274 }
1275 } else {
1276 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1277 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1278 com_schedrx(sc);
1279 }
1280 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1281 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1282 com_hwiflow(sc);
1283 }
1284 }
1285 splx(s);
1286 return (1);
1287 }
1288
1289 /*
1290 * (un)block input via hw flowcontrol
1291 */
1292 void
1293 com_hwiflow(sc)
1294 struct com_softc *sc;
1295 {
1296 bus_space_tag_t iot = sc->sc_iot;
1297 bus_space_handle_t ioh = sc->sc_ioh;
1298
1299 if (sc->sc_mcr_rts == 0)
1300 return;
1301
1302 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1303 CLR(sc->sc_mcr, sc->sc_mcr_rts);
1304 CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
1305 } else {
1306 SET(sc->sc_mcr, sc->sc_mcr_rts);
1307 SET(sc->sc_mcr_active, sc->sc_mcr_rts);
1308 }
1309 bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
1310 }
1311
1312
1313 void
1314 comstart(tp)
1315 struct tty *tp;
1316 {
1317 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1318 bus_space_tag_t iot = sc->sc_iot;
1319 bus_space_handle_t ioh = sc->sc_ioh;
1320 int s;
1321
1322 s = spltty();
1323 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1324 goto out;
1325 if (sc->sc_tx_stopped)
1326 goto out;
1327
1328 if (tp->t_outq.c_cc <= tp->t_lowat) {
1329 if (ISSET(tp->t_state, TS_ASLEEP)) {
1330 CLR(tp->t_state, TS_ASLEEP);
1331 wakeup(&tp->t_outq);
1332 }
1333 selwakeup(&tp->t_wsel);
1334 if (tp->t_outq.c_cc == 0)
1335 goto out;
1336 }
1337
1338 /* Grab the first contiguous region of buffer space. */
1339 {
1340 u_char *tba;
1341 int tbc;
1342
1343 tba = tp->t_outq.c_cf;
1344 tbc = ndqb(&tp->t_outq, 0);
1345
1346 (void)splserial();
1347
1348 sc->sc_tba = tba;
1349 sc->sc_tbc = tbc;
1350 }
1351
1352 SET(tp->t_state, TS_BUSY);
1353 sc->sc_tx_busy = 1;
1354
1355 /* Enable transmit completion interrupts if necessary. */
1356 if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
1357 SET(sc->sc_ier, IER_ETXRDY);
1358 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1359 }
1360
1361 /* Output the first chunk of the contiguous buffer. */
1362 {
1363 int n;
1364
1365 n = sc->sc_tbc;
1366 if (n > sc->sc_fifolen)
1367 n = sc->sc_fifolen;
1368 bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
1369 sc->sc_tbc -= n;
1370 sc->sc_tba += n;
1371 }
1372 out:
1373 splx(s);
1374 return;
1375 }
1376
1377 /*
1378 * Stop output on a line.
1379 */
1380 void
1381 comstop(tp, flag)
1382 struct tty *tp;
1383 int flag;
1384 {
1385 struct com_softc *sc = com_cd.cd_devs[COMUNIT(tp->t_dev)];
1386 int s;
1387
1388 s = splserial();
1389 if (ISSET(tp->t_state, TS_BUSY)) {
1390 /* Stop transmitting at the next chunk. */
1391 sc->sc_tbc = 0;
1392 sc->sc_heldtbc = 0;
1393 if (!ISSET(tp->t_state, TS_TTSTOP))
1394 SET(tp->t_state, TS_FLUSH);
1395 }
1396 splx(s);
1397 }
1398
1399 void
1400 comdiag(arg)
1401 void *arg;
1402 {
1403 struct com_softc *sc = arg;
1404 int overflows, floods;
1405 int s;
1406
1407 s = splserial();
1408 overflows = sc->sc_overflows;
1409 sc->sc_overflows = 0;
1410 floods = sc->sc_floods;
1411 sc->sc_floods = 0;
1412 sc->sc_errors = 0;
1413 splx(s);
1414
1415 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1416 sc->sc_dev.dv_xname,
1417 overflows, overflows == 1 ? "" : "s",
1418 floods, floods == 1 ? "" : "s");
1419 }
1420
1421 integrate void
1422 com_rxsoft(sc, tp)
1423 struct com_softc *sc;
1424 struct tty *tp;
1425 {
1426 int (*rint) __P((int c, struct tty *tp)) = linesw[tp->t_line].l_rint;
1427 u_char *get, *end;
1428 u_int cc, scc;
1429 u_char lsr;
1430 int code;
1431 int s;
1432
1433 end = sc->sc_ebuf;
1434 get = sc->sc_rbget;
1435 scc = cc = com_rbuf_size - sc->sc_rbavail;
1436
1437 if (cc == com_rbuf_size) {
1438 sc->sc_floods++;
1439 if (sc->sc_errors++ == 0)
1440 timeout(comdiag, sc, 60 * hz);
1441 }
1442
1443 while (cc) {
1444 code = get[0];
1445 lsr = get[1];
1446 if (ISSET(lsr, LSR_OE | LSR_BI | LSR_FE | LSR_PE)) {
1447 if (ISSET(lsr, LSR_OE)) {
1448 sc->sc_overflows++;
1449 if (sc->sc_errors++ == 0)
1450 timeout(comdiag, sc, 60 * hz);
1451 }
1452 if (ISSET(lsr, LSR_BI | LSR_FE))
1453 SET(code, TTY_FE);
1454 if (ISSET(lsr, LSR_PE))
1455 SET(code, TTY_PE);
1456 }
1457 if ((*rint)(code, tp) == -1) {
1458 /*
1459 * The line discipline's buffer is out of space.
1460 */
1461 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1462 /*
1463 * We're either not using flow control, or the
1464 * line discipline didn't tell us to block for
1465 * some reason. Either way, we have no way to
1466 * know when there's more space available, so
1467 * just drop the rest of the data.
1468 */
1469 get += cc << 1;
1470 if (get >= end)
1471 get -= com_rbuf_size << 1;
1472 cc = 0;
1473 } else {
1474 /*
1475 * Don't schedule any more receive processing
1476 * until the line discipline tells us there's
1477 * space available (through comhwiflow()).
1478 * Leave the rest of the data in the input
1479 * buffer.
1480 */
1481 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1482 }
1483 break;
1484 }
1485 get += 2;
1486 if (get >= end)
1487 get = sc->sc_rbuf;
1488 cc--;
1489 }
1490
1491 if (cc != scc) {
1492 sc->sc_rbget = get;
1493 s = splserial();
1494 cc = sc->sc_rbavail += scc - cc;
1495 /* Buffers should be ok again, release possible block. */
1496 if (cc >= sc->sc_r_lowat) {
1497 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1498 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1499 SET(sc->sc_ier, IER_ERXRDY);
1500 bus_space_write_1(sc->sc_iot, sc->sc_ioh, com_ier, sc->sc_ier);
1501 }
1502 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1503 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1504 com_hwiflow(sc);
1505 }
1506 }
1507 splx(s);
1508 }
1509 }
1510
1511 integrate void
1512 com_txsoft(sc, tp)
1513 struct com_softc *sc;
1514 struct tty *tp;
1515 {
1516
1517 CLR(tp->t_state, TS_BUSY);
1518 if (ISSET(tp->t_state, TS_FLUSH))
1519 CLR(tp->t_state, TS_FLUSH);
1520 else
1521 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1522 (*linesw[tp->t_line].l_start)(tp);
1523 }
1524
1525 integrate void
1526 com_stsoft(sc, tp)
1527 struct com_softc *sc;
1528 struct tty *tp;
1529 {
1530 u_char msr, delta;
1531 int s;
1532
1533 s = splserial();
1534 msr = sc->sc_msr;
1535 delta = sc->sc_msr_delta;
1536 sc->sc_msr_delta = 0;
1537 splx(s);
1538
1539 if (ISSET(delta, sc->sc_msr_dcd)) {
1540 /*
1541 * Inform the tty layer that carrier detect changed.
1542 */
1543 (void) (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD));
1544 }
1545
1546 if (ISSET(delta, sc->sc_msr_cts)) {
1547 /* Block or unblock output according to flow control. */
1548 if (ISSET(msr, sc->sc_msr_cts)) {
1549 sc->sc_tx_stopped = 0;
1550 (*linesw[tp->t_line].l_start)(tp);
1551 } else {
1552 sc->sc_tx_stopped = 1;
1553 }
1554 }
1555
1556 #ifdef COM_DEBUG
1557 if (com_debug)
1558 comstatus(sc, "com_stsoft");
1559 #endif
1560 }
1561
1562 #ifdef __GENERIC_SOFT_INTERRUPTS
1563 void
1564 comsoft(arg)
1565 void *arg;
1566 {
1567 struct com_softc *sc = arg;
1568 struct tty *tp;
1569
1570 if (!sc->enabled)
1571 return;
1572
1573 {
1574 #else
1575 void
1576 #ifndef alpha
1577 comsoft()
1578 #else
1579 comsoft(arg)
1580 void *arg;
1581 #endif
1582 {
1583 struct com_softc *sc;
1584 struct tty *tp;
1585 int unit;
1586 #ifdef alpha
1587 int s;
1588
1589 s = splsoftserial();
1590 com_softintr_scheduled = 0;
1591 #endif
1592
1593 for (unit = 0; unit < com_cd.cd_ndevs; unit++) {
1594 sc = com_cd.cd_devs[unit];
1595 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK))
1596 continue;
1597
1598 if (!sc->enabled)
1599 continue;
1600
1601 tp = sc->sc_tty;
1602 if (tp == NULL || !ISSET(tp->t_state, TS_ISOPEN | TS_WOPEN))
1603 continue;
1604 #endif
1605 tp = sc->sc_tty;
1606
1607 if (sc->sc_rx_ready) {
1608 sc->sc_rx_ready = 0;
1609 com_rxsoft(sc, tp);
1610 }
1611
1612 if (sc->sc_st_check) {
1613 sc->sc_st_check = 0;
1614 com_stsoft(sc, tp);
1615 }
1616
1617 if (sc->sc_tx_done) {
1618 sc->sc_tx_done = 0;
1619 com_txsoft(sc, tp);
1620 }
1621 }
1622
1623 #ifndef __GENERIC_SOFT_INTERRUPTS
1624 #ifdef alpha
1625 splx(s);
1626 #endif
1627 #endif
1628 }
1629
1630 int
1631 comintr(arg)
1632 void *arg;
1633 {
1634 struct com_softc *sc = arg;
1635 bus_space_tag_t iot = sc->sc_iot;
1636 bus_space_handle_t ioh = sc->sc_ioh;
1637 u_char *put, *end;
1638 u_int cc;
1639 u_char lsr, iir;
1640
1641 if (!sc->enabled)
1642 return (0);
1643
1644 iir = bus_space_read_1(iot, ioh, com_iir);
1645 if (ISSET(iir, IIR_NOPEND))
1646 return (0);
1647
1648 end = sc->sc_ebuf;
1649 put = sc->sc_rbput;
1650 cc = sc->sc_rbavail;
1651
1652 do {
1653 u_char msr, delta;
1654
1655 lsr = bus_space_read_1(iot, ioh, com_lsr);
1656 #if defined(DDB) || defined(KGDB)
1657 if (ISSET(lsr, LSR_BI)) {
1658 #ifdef DDB
1659 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1660 Debugger();
1661 continue;
1662 }
1663 #endif
1664 #ifdef KGDB
1665 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) {
1666 kgdb_connect(1);
1667 continue;
1668 }
1669 #endif
1670 }
1671 #endif /* DDB || KGDB */
1672
1673 if (ISSET(lsr, LSR_RCV_MASK) &&
1674 !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1675 while (cc > 0) {
1676 put[0] = bus_space_read_1(iot, ioh, com_data);
1677 put[1] = lsr;
1678 put += 2;
1679 if (put >= end)
1680 put = sc->sc_rbuf;
1681 cc--;
1682
1683 lsr = bus_space_read_1(iot, ioh, com_lsr);
1684 if (!ISSET(lsr, LSR_RCV_MASK))
1685 break;
1686 }
1687
1688 /*
1689 * Current string of incoming characters ended because
1690 * no more data was available or we ran out of space.
1691 * Schedule a receive event if any data was received.
1692 * If we're out of space, turn off receive interrupts.
1693 */
1694 sc->sc_rbput = put;
1695 sc->sc_rbavail = cc;
1696 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1697 sc->sc_rx_ready = 1;
1698
1699 /*
1700 * See if we are in danger of overflowing a buffer. If
1701 * so, use hardware flow control to ease the pressure.
1702 */
1703 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1704 cc < sc->sc_r_hiwat) {
1705 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1706 com_hwiflow(sc);
1707 }
1708
1709 /*
1710 * If we're out of space, disable receive interrupts
1711 * until the queue has drained a bit.
1712 */
1713 if (!cc) {
1714 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1715 CLR(sc->sc_ier, IER_ERXRDY);
1716 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1717 }
1718 } else {
1719 if ((iir & IIR_IMASK) == IIR_RXRDY) {
1720 bus_space_write_1(iot, ioh, com_ier, 0);
1721 delay(10);
1722 bus_space_write_1(iot, ioh, com_ier,sc->sc_ier);
1723 iir = IIR_NOPEND;
1724 continue;
1725 }
1726 }
1727
1728 msr = bus_space_read_1(iot, ioh, com_msr);
1729 delta = msr ^ sc->sc_msr;
1730 sc->sc_msr = msr;
1731 if (ISSET(delta, sc->sc_msr_mask)) {
1732 SET(sc->sc_msr_delta, delta);
1733
1734 /*
1735 * Stop output immediately if we lose the output
1736 * flow control signal or carrier detect.
1737 */
1738 if (ISSET(~msr, sc->sc_msr_mask)) {
1739 sc->sc_tbc = 0;
1740 sc->sc_heldtbc = 0;
1741 #ifdef COM_DEBUG
1742 if (com_debug)
1743 comstatus(sc, "comintr ");
1744 #endif
1745 }
1746
1747 sc->sc_st_check = 1;
1748 }
1749 } while (!ISSET((iir = bus_space_read_1(iot, ioh, com_iir)), IIR_NOPEND));
1750
1751 /*
1752 * Done handling any receive interrupts. See if data can be
1753 * transmitted as well. Schedule tx done event if no data left
1754 * and tty was marked busy.
1755 */
1756 if (ISSET(lsr, LSR_TXRDY)) {
1757 /*
1758 * If we've delayed a parameter change, do it now, and restart
1759 * output.
1760 */
1761 if (sc->sc_heldchange) {
1762 com_loadchannelregs(sc);
1763 sc->sc_heldchange = 0;
1764 sc->sc_tbc = sc->sc_heldtbc;
1765 sc->sc_heldtbc = 0;
1766 }
1767
1768 /* Output the next chunk of the contiguous buffer, if any. */
1769 if (sc->sc_tbc > 0) {
1770 int n;
1771
1772 n = sc->sc_tbc;
1773 if (n > sc->sc_fifolen)
1774 n = sc->sc_fifolen;
1775 bus_space_write_multi_1(iot, ioh, com_data, sc->sc_tba, n);
1776 sc->sc_tbc -= n;
1777 sc->sc_tba += n;
1778 } else {
1779 /* Disable transmit completion interrupts if necessary. */
1780 if (ISSET(sc->sc_ier, IER_ETXRDY)) {
1781 CLR(sc->sc_ier, IER_ETXRDY);
1782 bus_space_write_1(iot, ioh, com_ier, sc->sc_ier);
1783 }
1784 if (sc->sc_tx_busy) {
1785 sc->sc_tx_busy = 0;
1786 sc->sc_tx_done = 1;
1787 }
1788 }
1789 }
1790
1791 /* Wake up the poller. */
1792 #ifdef __GENERIC_SOFT_INTERRUPTS
1793 softintr_schedule(sc->sc_si);
1794 #else
1795 #ifndef alpha
1796 setsoftserial();
1797 #else
1798 if (!com_softintr_scheduled) {
1799 com_softintr_scheduled = 1;
1800 timeout(comsoft, NULL, 1);
1801 }
1802 #endif
1803 #endif
1804
1805 #if NRND > 0 && defined(RND_COM)
1806 rnd_add_uint32(&sc->rnd_source, iir | lsr);
1807 #endif
1808
1809 return (1);
1810 }
1811
1812 /*
1813 * The following functions are polled getc and putc routines, shared
1814 * by the console and kgdb glue.
1815 */
1816
1817 int
1818 com_common_getc(iot, ioh)
1819 bus_space_tag_t iot;
1820 bus_space_handle_t ioh;
1821 {
1822 int s = splserial();
1823 u_char stat, c;
1824 int timo;
1825
1826 timo = 50000;
1827 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_RXRDY)
1828 && --timo)
1829 ;
1830 #ifdef DIAGNOSTIC
1831 if (!timo)
1832 printf("com_common_getc timeout\n");
1833 #endif
1834 c = bus_space_read_1(iot, ioh, com_data);
1835 stat = bus_space_read_1(iot, ioh, com_iir);
1836 splx(s);
1837 return (c);
1838 }
1839
1840 void
1841 com_common_putc(iot, ioh, c)
1842 bus_space_tag_t iot;
1843 bus_space_handle_t ioh;
1844 int c;
1845 {
1846 int s = splserial();
1847 u_char stat;
1848 int timo;
1849
1850 /* wait for any pending transmission to finish */
1851 timo = 50000;
1852 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY)
1853 && --timo)
1854 ;
1855 #ifdef DIAGNOSTIC
1856 if (!timo)
1857 printf("com_common_putc flush timeout\n");
1858 #endif
1859 bus_space_write_1(iot, ioh, com_data, c);
1860 /* wait for this transmission to complete */
1861 timo = 1500000;
1862 while (!ISSET(stat = bus_space_read_1(iot, ioh, com_lsr), LSR_TXRDY)
1863 && --timo)
1864 ;
1865 #ifdef DIAGNOSTIC
1866 if (!timo)
1867 printf("com_common_putc wait timeout\n");
1868 #endif
1869 /* clear any interrupts generated by this transmission */
1870 stat = bus_space_read_1(iot, ioh, com_iir);
1871 splx(s);
1872 }
1873
1874 /*
1875 * Initialize UART to known state.
1876 */
1877 int
1878 cominit(iot, iobase, rate, frequency, cflag, iohp)
1879 bus_space_tag_t iot;
1880 int iobase;
1881 int rate, frequency;
1882 tcflag_t cflag;
1883 bus_space_handle_t *iohp;
1884 {
1885 bus_space_handle_t ioh;
1886
1887 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
1888 return (ENOMEM); /* ??? */
1889
1890 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
1891 bus_space_write_1(iot, ioh, com_efr, 0);
1892 bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
1893 rate = comspeed(rate, frequency);
1894 bus_space_write_1(iot, ioh, com_dlbl, rate);
1895 bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
1896 bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
1897 bus_space_write_1(iot, ioh, com_mcr, 0);
1898 bus_space_write_1(iot, ioh, com_fifo,
1899 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
1900 bus_space_write_1(iot, ioh, com_ier, 0);
1901
1902 *iohp = ioh;
1903 return (0);
1904 }
1905
1906 /*
1907 * Following are all routines needed for COM to act as console
1908 */
1909
1910 int
1911 comcnattach(iot, iobase, rate, frequency, cflag)
1912 bus_space_tag_t iot;
1913 int iobase;
1914 int rate, frequency;
1915 tcflag_t cflag;
1916 {
1917 int res;
1918 static struct consdev comcons = {
1919 NULL, NULL, comcngetc, comcnputc, comcnpollc, NODEV, CN_NORMAL
1920 };
1921
1922 res = cominit(iot, iobase, rate, frequency, cflag, &comconsioh);
1923 if (res)
1924 return (res);
1925
1926 cn_tab = &comcons;
1927
1928 comconstag = iot;
1929 comconsaddr = iobase;
1930 comconsrate = rate;
1931 comconscflag = cflag;
1932
1933 return (0);
1934 }
1935
1936 int
1937 comcngetc(dev)
1938 dev_t dev;
1939 {
1940
1941 return (com_common_getc(comconstag, comconsioh));
1942 }
1943
1944 /*
1945 * Console kernel output character routine.
1946 */
1947 void
1948 comcnputc(dev, c)
1949 dev_t dev;
1950 int c;
1951 {
1952
1953 com_common_putc(comconstag, comconsioh, c);
1954 }
1955
1956 void
1957 comcnpollc(dev, on)
1958 dev_t dev;
1959 int on;
1960 {
1961
1962 }
1963
1964 #ifdef KGDB
1965 int
1966 com_kgdb_attach(iot, iobase, rate, frequency, cflag)
1967 bus_space_tag_t iot;
1968 int iobase;
1969 int rate, frequency;
1970 tcflag_t cflag;
1971 {
1972 int res;
1973
1974 if (iot == comconstag && iobase == comconsaddr)
1975 return (EBUSY); /* cannot share with console */
1976
1977 res = cominit(iot, iobase, rate, frequency, cflag, &com_kgdb_ioh);
1978 if (res)
1979 return (res);
1980
1981 kgdb_attach(com_kgdb_getc, com_kgdb_putc, NULL);
1982 kgdb_dev = 123; /* unneeded, only to satisfy some tests */
1983
1984 com_kgdb_iot = iot;
1985 com_kgdb_addr = iobase;
1986
1987 return (0);
1988 }
1989
1990 /* ARGSUSED */
1991 int
1992 com_kgdb_getc(arg)
1993 void *arg;
1994 {
1995
1996 return (com_common_getc(com_kgdb_iot, com_kgdb_ioh));
1997 }
1998
1999 /* ARGSUSED */
2000 void
2001 com_kgdb_putc(arg, c)
2002 void *arg;
2003 int c;
2004 {
2005
2006 return (com_common_putc(com_kgdb_iot, com_kgdb_ioh, c));
2007 }
2008 #endif /* KGDB */
2009
2010 /* helper function to identify the com ports used by
2011 console or KGDB (and not yet autoconf attached) */
2012 int
2013 com_is_console(iot, iobase, ioh)
2014 bus_space_tag_t iot;
2015 int iobase;
2016 bus_space_handle_t *ioh;
2017 {
2018 bus_space_handle_t help;
2019
2020 if (!comconsattached &&
2021 iot == comconstag && iobase == comconsaddr)
2022 help = comconsioh;
2023 #ifdef KGDB
2024 else if (!com_kgdb_attached &&
2025 iot == com_kgdb_iot && iobase == com_kgdb_addr)
2026 help = com_kgdb_ioh;
2027 #endif
2028 else
2029 return (0);
2030
2031 if (ioh)
2032 *ioh = help;
2033 return (1);
2034 }
2035