com.c revision 1.74 1 /* $NetBSD: com.c,v 1.74 1996/03/09 01:02:08 cgd Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Charles M. Hannum. All rights reserved.
6 * Copyright (c) 1991 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)com.c 7.5 (Berkeley) 5/16/91
38 */
39
40 /*
41 * COM driver, based on HP dca driver
42 * uses National Semiconductor NS16450/NS16550AF UART
43 */
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/ioctl.h>
47 #include <sys/select.h>
48 #include <sys/tty.h>
49 #include <sys/proc.h>
50 #include <sys/user.h>
51 #include <sys/conf.h>
52 #include <sys/file.h>
53 #include <sys/uio.h>
54 #include <sys/kernel.h>
55 #include <sys/syslog.h>
56 #include <sys/types.h>
57 #include <sys/device.h>
58
59 #include <machine/cpu.h>
60 #include <machine/pio.h>
61
62 #include <dev/isa/isavar.h>
63 #include <dev/isa/comreg.h>
64 #include <dev/isa/comvar.h>
65 #include <dev/ic/ns16550reg.h>
66 #ifdef COM_HAYESP
67 #include <dev/ic/hayespreg.h>
68 #endif
69 #define com_lcr com_cfcr
70
71 #define COM_IBUFSIZE (2 * 512)
72 #define COM_IHIGHWATER ((3 * COM_IBUFSIZE) / 4)
73
74 struct com_softc {
75 struct device sc_dev;
76 void *sc_ih;
77 struct tty *sc_tty;
78
79 int sc_overflows;
80 int sc_floods;
81 int sc_errors;
82
83 int sc_halt;
84
85 int sc_iobase;
86 #ifdef COM_HAYESP
87 int sc_hayespbase;
88 #endif
89 u_char sc_hwflags;
90 #define COM_HW_NOIEN 0x01
91 #define COM_HW_FIFO 0x02
92 #define COM_HW_HAYESP 0x04
93 #define COM_HW_CONSOLE 0x40
94 u_char sc_swflags;
95 #define COM_SW_SOFTCAR 0x01
96 #define COM_SW_CLOCAL 0x02
97 #define COM_SW_CRTSCTS 0x04
98 #define COM_SW_MDMBUF 0x08
99 u_char sc_msr, sc_mcr, sc_lcr, sc_ier;
100 u_char sc_dtr;
101
102 u_char *sc_ibuf, *sc_ibufp, *sc_ibufhigh, *sc_ibufend;
103 u_char sc_ibufs[2][COM_IBUFSIZE];
104 };
105
106 int comprobe __P((struct device *, void *, void *));
107 void comattach __P((struct device *, struct device *, void *));
108 int comopen __P((dev_t, int, int, struct proc *));
109 int comclose __P((dev_t, int, int, struct proc *));
110 void comdiag __P((void *));
111 int comintr __P((void *));
112 void compoll __P((void *));
113 int comparam __P((struct tty *, struct termios *));
114 void comstart __P((struct tty *));
115
116 struct cfdriver comcd = {
117 NULL, "com", comprobe, comattach, DV_TTY, sizeof(struct com_softc)
118 };
119
120 #ifdef COMCONSOLE
121 int comdefaultrate = CONSPEED;
122 int comconsole = COMCONSOLE;
123 #else
124 int comdefaultrate = TTYDEF_SPEED;
125 int comconsole = -1;
126 #endif
127 int comconsinit;
128 int commajor;
129 int comsopen = 0;
130 int comevents = 0;
131
132 #ifdef KGDB
133 #include <machine/remote-sl.h>
134 extern int kgdb_dev;
135 extern int kgdb_rate;
136 extern int kgdb_debug_init;
137 #endif
138
139 #define COMUNIT(x) (minor(x))
140
141 /* Macros to clear/set/test flags. */
142 #define SET(t, f) (t) |= (f)
143 #define CLR(t, f) (t) &= ~(f)
144 #define ISSET(t, f) ((t) & (f))
145
146 int
147 comspeed(speed)
148 long speed;
149 {
150 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
151
152 int x, err;
153
154 if (speed == 0)
155 return 0;
156 if (speed < 0)
157 return -1;
158 x = divrnd((COM_FREQ / 16), speed);
159 if (x <= 0)
160 return -1;
161 err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
162 if (err < 0)
163 err = -err;
164 if (err > COM_TOLERANCE)
165 return -1;
166 return x;
167
168 #undef divrnd(n, q)
169 }
170
171 int
172 comprobe1(iobase)
173 int iobase;
174 {
175
176 /* force access to id reg */
177 outb(iobase + com_lcr, 0);
178 outb(iobase + com_iir, 0);
179 if (inb(iobase + com_iir) & 0x38)
180 return 0;
181
182 return 1;
183 }
184
185 #ifdef COM_HAYESP
186 int
187 comprobeHAYESP(iobase, sc)
188 int iobase;
189 struct com_softc *sc;
190 {
191 char val, dips;
192 int combaselist[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
193
194 /*
195 * Hayes ESP cards have two iobases. One is for compatibility with
196 * 16550 serial chips, and at the same ISA PC base addresses. The
197 * other is for ESP-specific enhanced features, and lies at a
198 * different addressing range entirely (0x140, 0x180, 0x280, or 0x300).
199 */
200
201 /* Test for ESP signature */
202 if ((inb(iobase) & 0xf3) == 0)
203 return 0;
204
205 /*
206 * ESP is present at ESP enhanced base address; unknown com port
207 */
208
209 /* Get the dip-switch configurations */
210 outb(iobase + HAYESP_CMD1, HAYESP_GETDIPS);
211 dips = inb(iobase + HAYESP_STATUS1);
212
213 /* Determine which com port this ESP card services: bits 0,1 of */
214 /* dips is the port # (0-3); combaselist[val] is the com_iobase */
215 if (sc->sc_iobase != combaselist[dips & 0x03])
216 return 0;
217
218 printf(": ESP");
219
220 /* Check ESP Self Test bits. */
221 /* Check for ESP version 2.0: bits 4,5,6 == 010 */
222 outb(iobase + HAYESP_CMD1, HAYESP_GETTEST);
223 val = inb(iobase + HAYESP_STATUS1); /* Clear reg 1 */
224 val = inb(iobase + HAYESP_STATUS2);
225 if ((val & 0x70) < 0x20) {
226 printf("-old (%o)", val & 0x70);
227 /* we do not support the necessary features */
228 return 0;
229 }
230
231 /* Check for ability to emulate 16550: bit 8 == 1 */
232 if ((dips & 0x80) == 0) {
233 printf(" slave");
234 /* XXX Does slave really mean no 16550 support?? */
235 return 0;
236 }
237
238 /*
239 * If we made it this far, we are a full-featured ESP v2.0 (or
240 * better), at the correct com port address.
241 */
242
243 SET(sc->sc_hwflags, COM_HW_HAYESP);
244 printf(", 1024 byte fifo\n");
245 return 1;
246 }
247 #endif
248
249 int
250 comprobe(parent, match, aux)
251 struct device *parent;
252 void *match, *aux;
253 {
254 struct isa_attach_args *ia = aux;
255 int iobase = ia->ia_iobase;
256
257 if (!comprobe1(iobase))
258 return 0;
259
260 ia->ia_iosize = COM_NPORTS;
261 ia->ia_msize = 0;
262 return 1;
263 }
264
265 void
266 comattach(parent, self, aux)
267 struct device *parent, *self;
268 void *aux;
269 {
270 struct com_softc *sc = (void *)self;
271 struct isa_attach_args *ia = aux;
272 struct cfdata *cf = sc->sc_dev.dv_cfdata;
273 int iobase = ia->ia_iobase;
274 struct tty *tp;
275 #ifdef COM_HAYESP
276 int hayesp_ports[] = { 0x140, 0x180, 0x280, 0x300, 0 };
277 int *hayespp;
278 #endif
279
280 sc->sc_iobase = iobase;
281 sc->sc_hwflags = ISSET(cf->cf_flags, COM_HW_NOIEN);
282 sc->sc_swflags = 0;
283
284 if (sc->sc_dev.dv_unit == comconsole)
285 delay(1000);
286
287 #ifdef COM_HAYESP
288 /* Look for a Hayes ESP board. */
289 for (hayespp = hayesp_ports; *hayespp != 0; hayespp++)
290 if (comprobeHAYESP(*hayespp, sc)) {
291 sc->sc_hayespbase = *hayespp;
292 break;
293 }
294 /* No ESP; look for other things. */
295 if (*hayespp == 0) {
296 #endif
297
298 /* look for a NS 16550AF UART with FIFOs */
299 outb(iobase + com_fifo,
300 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_14);
301 delay(100);
302 if (ISSET(inb(iobase + com_iir), IIR_FIFO_MASK) == IIR_FIFO_MASK)
303 if (ISSET(inb(iobase + com_fifo), FIFO_TRIGGER_14) == FIFO_TRIGGER_14) {
304 SET(sc->sc_hwflags, COM_HW_FIFO);
305 printf(": ns16550a, working fifo\n");
306 } else
307 printf(": ns16550, broken fifo\n");
308 else
309 printf(": ns8250 or ns16450, no fifo\n");
310 outb(iobase + com_fifo, 0);
311 #ifdef COM_HAYESP
312 }
313 #endif
314
315 /* disable interrupts */
316 outb(iobase + com_ier, 0);
317 outb(iobase + com_mcr, 0);
318
319 if (ia->ia_irq != IRQUNK)
320 sc->sc_ih = isa_intr_establish(ia->ia_irq, IST_EDGE, IPL_TTY,
321 comintr, sc);
322
323 #ifdef KGDB
324 if (kgdb_dev == makedev(commajor, unit)) {
325 if (comconsole == unit)
326 kgdb_dev = -1; /* can't debug over console port */
327 else {
328 (void) cominit(unit, kgdb_rate);
329 if (kgdb_debug_init) {
330 /*
331 * Print prefix of device name,
332 * let kgdb_connect print the rest.
333 */
334 printf("%s: ", sc->sc_dev.dv_xname);
335 kgdb_connect(1);
336 } else
337 printf("%s: kgdb enabled\n",
338 sc->sc_dev.dv_xname);
339 }
340 }
341 #endif
342
343 if (sc->sc_dev.dv_unit == comconsole) {
344 /*
345 * Need to reset baud rate, etc. of next print so reset
346 * comconsinit. Also make sure console is always "hardwired".
347 */
348 comconsinit = 0;
349 SET(sc->sc_hwflags, COM_HW_CONSOLE);
350 SET(sc->sc_swflags, COM_SW_SOFTCAR);
351 }
352 }
353
354 int
355 comopen(dev, flag, mode, p)
356 dev_t dev;
357 int flag, mode;
358 struct proc *p;
359 {
360 int unit = COMUNIT(dev);
361 struct com_softc *sc;
362 int iobase;
363 struct tty *tp;
364 int s;
365 int error = 0;
366
367 if (unit >= comcd.cd_ndevs)
368 return ENXIO;
369 sc = comcd.cd_devs[unit];
370 if (!sc)
371 return ENXIO;
372
373 if (!sc->sc_tty)
374 tp = sc->sc_tty = ttymalloc();
375 else
376 tp = sc->sc_tty;
377
378 tp->t_oproc = comstart;
379 tp->t_param = comparam;
380 tp->t_dev = dev;
381 if (!ISSET(tp->t_state, TS_ISOPEN)) {
382 SET(tp->t_state, TS_WOPEN);
383 ttychars(tp);
384 tp->t_iflag = TTYDEF_IFLAG;
385 tp->t_oflag = TTYDEF_OFLAG;
386 tp->t_cflag = TTYDEF_CFLAG;
387 if (ISSET(sc->sc_swflags, COM_SW_CLOCAL))
388 SET(tp->t_cflag, CLOCAL);
389 if (ISSET(sc->sc_swflags, COM_SW_CRTSCTS))
390 SET(tp->t_cflag, CRTSCTS);
391 if (ISSET(sc->sc_swflags, COM_SW_MDMBUF))
392 SET(tp->t_cflag, MDMBUF);
393 tp->t_lflag = TTYDEF_LFLAG;
394 tp->t_ispeed = tp->t_ospeed = comdefaultrate;
395
396 s = spltty();
397
398 comparam(tp, &tp->t_termios);
399 ttsetwater(tp);
400
401 if (comsopen++ == 0)
402 timeout(compoll, NULL, 1);
403
404 sc->sc_ibufp = sc->sc_ibuf = sc->sc_ibufs[0];
405 sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
406 sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
407
408 iobase = sc->sc_iobase;
409 #ifdef COM_HAYESP
410 /* Setup the ESP board */
411 if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
412 int hayespbase = sc->sc_hayespbase;
413
414 outb(iobase + com_fifo,
415 FIFO_DMA_MODE|FIFO_ENABLE|
416 FIFO_RCV_RST|FIFO_XMT_RST|FIFO_TRIGGER_8);
417
418 /* Set 16550 compatibility mode */
419 outb(hayespbase + HAYESP_CMD1, HAYESP_SETMODE);
420 outb(hayespbase + HAYESP_CMD2,
421 HAYESP_MODE_FIFO|HAYESP_MODE_RTS|
422 HAYESP_MODE_SCALE);
423
424 /* Set RTS/CTS flow control */
425 outb(hayespbase + HAYESP_CMD1, HAYESP_SETFLOWTYPE);
426 outb(hayespbase + HAYESP_CMD2, HAYESP_FLOW_RTS);
427 outb(hayespbase + HAYESP_CMD2, HAYESP_FLOW_CTS);
428
429 /* Set flow control levels */
430 outb(hayespbase + HAYESP_CMD1, HAYESP_SETRXFLOW);
431 outb(hayespbase + HAYESP_CMD2,
432 HAYESP_HIBYTE(HAYESP_RXHIWMARK));
433 outb(hayespbase + HAYESP_CMD2,
434 HAYESP_LOBYTE(HAYESP_RXHIWMARK));
435 outb(hayespbase + HAYESP_CMD2,
436 HAYESP_HIBYTE(HAYESP_RXLOWMARK));
437 outb(hayespbase + HAYESP_CMD2,
438 HAYESP_LOBYTE(HAYESP_RXLOWMARK));
439 } else
440 #endif
441 if (ISSET(sc->sc_hwflags, COM_HW_FIFO))
442 /* Set the FIFO threshold based on the receive speed. */
443 outb(iobase + com_fifo,
444 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
445 (tp->t_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
446 /* flush any pending I/O */
447 while (ISSET(inb(iobase + com_lsr), LSR_RXRDY))
448 (void) inb(iobase + com_data);
449 /* you turn me on, baby */
450 sc->sc_mcr = MCR_DTR | MCR_RTS;
451 if (!ISSET(sc->sc_hwflags, COM_HW_NOIEN))
452 SET(sc->sc_mcr, MCR_IENABLE);
453 outb(iobase + com_mcr, sc->sc_mcr);
454 sc->sc_ier = IER_ERXRDY | IER_ERLS | IER_EMSC;
455 outb(iobase + com_ier, sc->sc_ier);
456
457 sc->sc_msr = inb(iobase + com_msr);
458 if (ISSET(sc->sc_swflags, COM_SW_SOFTCAR) ||
459 ISSET(sc->sc_msr, MSR_DCD) || ISSET(tp->t_cflag, MDMBUF))
460 SET(tp->t_state, TS_CARR_ON);
461 else
462 CLR(tp->t_state, TS_CARR_ON);
463 } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
464 return EBUSY;
465 else
466 s = spltty();
467
468 /* wait for carrier if necessary */
469 if (!ISSET(flag, O_NONBLOCK))
470 while (!ISSET(tp->t_cflag, CLOCAL) &&
471 !ISSET(tp->t_state, TS_CARR_ON)) {
472 SET(tp->t_state, TS_WOPEN);
473 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
474 ttopen, 0);
475 if (error) {
476 /* XXX should turn off chip if we're the
477 only waiter */
478 splx(s);
479 return error;
480 }
481 }
482 splx(s);
483
484 return (*linesw[tp->t_line].l_open)(dev, tp);
485 }
486
487 int
488 comclose(dev, flag, mode, p)
489 dev_t dev;
490 int flag, mode;
491 struct proc *p;
492 {
493 int unit = COMUNIT(dev);
494 struct com_softc *sc = comcd.cd_devs[unit];
495 struct tty *tp = sc->sc_tty;
496 int iobase = sc->sc_iobase;
497 int s;
498
499 /* XXX This is for cons.c. */
500 if (!ISSET(tp->t_state, TS_ISOPEN))
501 return 0;
502
503 (*linesw[tp->t_line].l_close)(tp, flag);
504 s = spltty();
505 CLR(sc->sc_lcr, LCR_SBREAK);
506 outb(iobase + com_lcr, sc->sc_lcr);
507 outb(iobase + com_ier, 0);
508 if (ISSET(tp->t_cflag, HUPCL) &&
509 !ISSET(sc->sc_swflags, COM_SW_SOFTCAR)) {
510 /* XXX perhaps only clear DTR */
511 outb(iobase + com_mcr, 0);
512 }
513 CLR(tp->t_state, TS_BUSY | TS_FLUSH);
514 if (--comsopen == 0)
515 untimeout(compoll, NULL);
516 splx(s);
517 ttyclose(tp);
518 #ifdef notyet /* XXXX */
519 if (unit != comconsole) {
520 ttyfree(tp);
521 sc->sc_tty = 0;
522 }
523 #endif
524 return 0;
525 }
526
527 int
528 comread(dev, uio, flag)
529 dev_t dev;
530 struct uio *uio;
531 int flag;
532 {
533 struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
534 struct tty *tp = sc->sc_tty;
535
536 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
537 }
538
539 int
540 comwrite(dev, uio, flag)
541 dev_t dev;
542 struct uio *uio;
543 int flag;
544 {
545 struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
546 struct tty *tp = sc->sc_tty;
547
548 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
549 }
550
551 struct tty *
552 comtty(dev)
553 dev_t dev;
554 {
555 struct com_softc *sc = comcd.cd_devs[COMUNIT(dev)];
556 struct tty *tp = sc->sc_tty;
557
558 return (tp);
559 }
560
561 static u_char
562 tiocm_xxx2mcr(data)
563 int data;
564 {
565 u_char m = 0;
566
567 if (ISSET(data, TIOCM_DTR))
568 SET(m, MCR_DTR);
569 if (ISSET(data, TIOCM_RTS))
570 SET(m, MCR_RTS);
571 return m;
572 }
573
574 int
575 comioctl(dev, cmd, data, flag, p)
576 dev_t dev;
577 u_long cmd;
578 caddr_t data;
579 int flag;
580 struct proc *p;
581 {
582 int unit = COMUNIT(dev);
583 struct com_softc *sc = comcd.cd_devs[unit];
584 struct tty *tp = sc->sc_tty;
585 int iobase = sc->sc_iobase;
586 int error;
587
588 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
589 if (error >= 0)
590 return error;
591 error = ttioctl(tp, cmd, data, flag, p);
592 if (error >= 0)
593 return error;
594
595 switch (cmd) {
596 case TIOCSBRK:
597 SET(sc->sc_lcr, LCR_SBREAK);
598 outb(iobase + com_lcr, sc->sc_lcr);
599 break;
600 case TIOCCBRK:
601 CLR(sc->sc_lcr, LCR_SBREAK);
602 outb(iobase + com_lcr, sc->sc_lcr);
603 break;
604 case TIOCSDTR:
605 SET(sc->sc_mcr, sc->sc_dtr);
606 outb(iobase + com_mcr, sc->sc_mcr);
607 break;
608 case TIOCCDTR:
609 CLR(sc->sc_mcr, sc->sc_dtr);
610 outb(iobase + com_mcr, sc->sc_mcr);
611 break;
612 case TIOCMSET:
613 CLR(sc->sc_mcr, MCR_DTR | MCR_RTS);
614 case TIOCMBIS:
615 SET(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
616 outb(iobase + com_mcr, sc->sc_mcr);
617 break;
618 case TIOCMBIC:
619 CLR(sc->sc_mcr, tiocm_xxx2mcr(*(int *)data));
620 outb(iobase + com_mcr, sc->sc_mcr);
621 break;
622 case TIOCMGET: {
623 u_char m;
624 int bits = 0;
625
626 m = sc->sc_mcr;
627 if (ISSET(m, MCR_DTR))
628 SET(bits, TIOCM_DTR);
629 if (ISSET(m, MCR_RTS))
630 SET(bits, TIOCM_RTS);
631 m = sc->sc_msr;
632 if (ISSET(m, MSR_DCD))
633 SET(bits, TIOCM_CD);
634 if (ISSET(m, MSR_CTS))
635 SET(bits, TIOCM_CTS);
636 if (ISSET(m, MSR_DSR))
637 SET(bits, TIOCM_DSR);
638 if (ISSET(m, MSR_RI | MSR_TERI))
639 SET(bits, TIOCM_RI);
640 if (inb(iobase + com_ier))
641 SET(bits, TIOCM_LE);
642 *(int *)data = bits;
643 break;
644 }
645 case TIOCGFLAGS: {
646 int driverbits, userbits = 0;
647
648 driverbits = sc->sc_swflags;
649 if (ISSET(driverbits, COM_SW_SOFTCAR))
650 SET(userbits, TIOCFLAG_SOFTCAR);
651 if (ISSET(driverbits, COM_SW_CLOCAL))
652 SET(userbits, TIOCFLAG_CLOCAL);
653 if (ISSET(driverbits, COM_SW_CRTSCTS))
654 SET(userbits, TIOCFLAG_CRTSCTS);
655 if (ISSET(driverbits, COM_SW_MDMBUF))
656 SET(userbits, TIOCFLAG_MDMBUF);
657
658 *(int *)data = userbits;
659 break;
660 }
661 case TIOCSFLAGS: {
662 int userbits, driverbits = 0;
663
664 error = suser(p->p_ucred, &p->p_acflag);
665 if (error != 0)
666 return(EPERM);
667
668 userbits = *(int *)data;
669 if (ISSET(userbits, TIOCFLAG_SOFTCAR) ||
670 ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
671 SET(driverbits, COM_SW_SOFTCAR);
672 if (ISSET(userbits, TIOCFLAG_CLOCAL))
673 SET(driverbits, COM_SW_CLOCAL);
674 if (ISSET(userbits, TIOCFLAG_CRTSCTS))
675 SET(driverbits, COM_SW_CRTSCTS);
676 if (ISSET(userbits, TIOCFLAG_MDMBUF))
677 SET(driverbits, COM_SW_MDMBUF);
678
679 sc->sc_swflags = driverbits;
680 break;
681 }
682 default:
683 return ENOTTY;
684 }
685
686 return 0;
687 }
688
689 int
690 comparam(tp, t)
691 struct tty *tp;
692 struct termios *t;
693 {
694 struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
695 int iobase = sc->sc_iobase;
696 int ospeed = comspeed(t->c_ospeed);
697 u_char lcr;
698 tcflag_t oldcflag;
699 int s;
700
701 /* check requested parameters */
702 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
703 return EINVAL;
704
705 lcr = ISSET(sc->sc_lcr, LCR_SBREAK);
706
707 switch (ISSET(t->c_cflag, CSIZE)) {
708 case CS5:
709 SET(lcr, LCR_5BITS);
710 break;
711 case CS6:
712 SET(lcr, LCR_6BITS);
713 break;
714 case CS7:
715 SET(lcr, LCR_7BITS);
716 break;
717 case CS8:
718 SET(lcr, LCR_8BITS);
719 break;
720 }
721 if (ISSET(t->c_cflag, PARENB)) {
722 SET(lcr, LCR_PENAB);
723 if (!ISSET(t->c_cflag, PARODD))
724 SET(lcr, LCR_PEVEN);
725 }
726 if (ISSET(t->c_cflag, CSTOPB))
727 SET(lcr, LCR_STOPB);
728
729 sc->sc_lcr = lcr;
730
731 s = spltty();
732
733 if (ospeed == 0) {
734 CLR(sc->sc_mcr, MCR_DTR);
735 outb(iobase + com_mcr, sc->sc_mcr);
736 }
737
738 /*
739 * Set the FIFO threshold based on the receive speed, if we are
740 * changing it.
741 */
742 #if 1
743 if (tp->t_ispeed != t->c_ispeed) {
744 #else
745 if (1) {
746 #endif
747 if (ospeed != 0) {
748 /*
749 * Make sure the transmit FIFO is empty before
750 * proceeding. If we don't do this, some revisions
751 * of the UART will hang. Interestingly enough,
752 * even if we do this will the last character is
753 * still being pushed out, they don't hang. This
754 * seems good enough.
755 */
756 while (ISSET(tp->t_state, TS_BUSY)) {
757 int error;
758
759 ++sc->sc_halt;
760 error = ttysleep(tp, &tp->t_outq,
761 TTOPRI | PCATCH, "comprm", 0);
762 --sc->sc_halt;
763 if (error) {
764 splx(s);
765 comstart(tp);
766 return (error);
767 }
768 }
769
770 outb(iobase + com_lcr, lcr | LCR_DLAB);
771 outb(iobase + com_dlbl, ospeed);
772 outb(iobase + com_dlbh, ospeed >> 8);
773 outb(iobase + com_lcr, lcr);
774 SET(sc->sc_mcr, MCR_DTR);
775 outb(iobase + com_mcr, sc->sc_mcr);
776 } else
777 outb(iobase + com_lcr, lcr);
778
779 if (!ISSET(sc->sc_hwflags, COM_HW_HAYESP) &&
780 ISSET(sc->sc_hwflags, COM_HW_FIFO))
781 outb(iobase + com_fifo,
782 FIFO_ENABLE |
783 (t->c_ispeed <= 1200 ? FIFO_TRIGGER_1 : FIFO_TRIGGER_8));
784 } else
785 outb(iobase + com_lcr, lcr);
786
787 /* When not using CRTSCTS, RTS follows DTR. */
788 if (!ISSET(t->c_cflag, CRTSCTS)) {
789 if (ISSET(sc->sc_mcr, MCR_DTR)) {
790 if (!ISSET(sc->sc_mcr, MCR_RTS)) {
791 SET(sc->sc_mcr, MCR_RTS);
792 outb(iobase + com_mcr, sc->sc_mcr);
793 }
794 } else {
795 if (ISSET(sc->sc_mcr, MCR_RTS)) {
796 CLR(sc->sc_mcr, MCR_RTS);
797 outb(iobase + com_mcr, sc->sc_mcr);
798 }
799 }
800 sc->sc_dtr = MCR_DTR | MCR_RTS;
801 } else
802 sc->sc_dtr = MCR_DTR;
803
804 /* and copy to tty */
805 tp->t_ispeed = t->c_ispeed;
806 tp->t_ospeed = t->c_ospeed;
807 oldcflag = tp->t_cflag;
808 tp->t_cflag = t->c_cflag;
809
810 /*
811 * If DCD is off and MDMBUF is changed, ask the tty layer if we should
812 * stop the device.
813 */
814 if (!ISSET(sc->sc_msr, MSR_DCD) &&
815 !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
816 ISSET(oldcflag, MDMBUF) != ISSET(tp->t_cflag, MDMBUF) &&
817 (*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
818 CLR(sc->sc_mcr, sc->sc_dtr);
819 outb(iobase + com_mcr, sc->sc_mcr);
820 }
821
822 /* Just to be sure... */
823 splx(s);
824 comstart(tp);
825 return 0;
826 }
827
828 void
829 comstart(tp)
830 struct tty *tp;
831 {
832 struct com_softc *sc = comcd.cd_devs[COMUNIT(tp->t_dev)];
833 int iobase = sc->sc_iobase;
834 int s;
835
836 s = spltty();
837 if (ISSET(tp->t_state, TS_BUSY))
838 goto out;
839 if (ISSET(tp->t_state, TS_TIMEOUT | TS_TTSTOP) ||
840 sc->sc_halt > 0)
841 goto stopped;
842 if (ISSET(tp->t_cflag, CRTSCTS) && !ISSET(sc->sc_msr, MSR_CTS))
843 goto stopped;
844 if (tp->t_outq.c_cc <= tp->t_lowat) {
845 if (ISSET(tp->t_state, TS_ASLEEP)) {
846 CLR(tp->t_state, TS_ASLEEP);
847 wakeup(&tp->t_outq);
848 }
849 if (tp->t_outq.c_cc == 0)
850 goto stopped;
851 selwakeup(&tp->t_wsel);
852 }
853 SET(tp->t_state, TS_BUSY);
854
855 if (!ISSET(sc->sc_ier, IER_ETXRDY)) {
856 SET(sc->sc_ier, IER_ETXRDY);
857 outb(iobase + com_ier, sc->sc_ier);
858 }
859 #ifdef COM_HAYESP
860 if (ISSET(sc->sc_hwflags, COM_HW_HAYESP)) {
861 u_char buffer[1024], *cp = buffer;
862 int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
863 do
864 outb(iobase + com_data, *cp++);
865 while (--n);
866 }
867 else
868 #endif
869 if (ISSET(sc->sc_hwflags, COM_HW_FIFO)) {
870 u_char buffer[16], *cp = buffer;
871 int n = q_to_b(&tp->t_outq, cp, sizeof buffer);
872 do {
873 outb(iobase + com_data, *cp++);
874 } while (--n);
875 } else
876 outb(iobase + com_data, getc(&tp->t_outq));
877 out:
878 splx(s);
879 return;
880 stopped:
881 if (ISSET(sc->sc_ier, IER_ETXRDY)) {
882 CLR(sc->sc_ier, IER_ETXRDY);
883 outb(iobase + com_ier, sc->sc_ier);
884 }
885 splx(s);
886 }
887
888 /*
889 * Stop output on a line.
890 */
891 void
892 comstop(tp, flag)
893 struct tty *tp;
894 {
895 int s;
896
897 s = spltty();
898 if (ISSET(tp->t_state, TS_BUSY))
899 if (!ISSET(tp->t_state, TS_TTSTOP))
900 SET(tp->t_state, TS_FLUSH);
901 splx(s);
902 }
903
904 void
905 comdiag(arg)
906 void *arg;
907 {
908 struct com_softc *sc = arg;
909 int overflows, floods;
910 int s;
911
912 s = spltty();
913 sc->sc_errors = 0;
914 overflows = sc->sc_overflows;
915 sc->sc_overflows = 0;
916 floods = sc->sc_floods;
917 sc->sc_floods = 0;
918 splx(s);
919
920 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf overflow%s\n",
921 sc->sc_dev.dv_xname,
922 overflows, overflows == 1 ? "" : "s",
923 floods, floods == 1 ? "" : "s");
924 }
925
926 void
927 compoll(arg)
928 void *arg;
929 {
930 int unit;
931 struct com_softc *sc;
932 struct tty *tp;
933 register u_char *ibufp;
934 u_char *ibufend;
935 register int c;
936 int s;
937 static int lsrmap[8] = {
938 0, TTY_PE,
939 TTY_FE, TTY_PE|TTY_FE,
940 TTY_FE, TTY_PE|TTY_FE,
941 TTY_FE, TTY_PE|TTY_FE
942 };
943
944 s = spltty();
945 if (comevents == 0) {
946 splx(s);
947 goto out;
948 }
949 comevents = 0;
950 splx(s);
951
952 for (unit = 0; unit < comcd.cd_ndevs; unit++) {
953 sc = comcd.cd_devs[unit];
954 if (sc == 0 || sc->sc_ibufp == sc->sc_ibuf)
955 continue;
956
957 tp = sc->sc_tty;
958
959 s = spltty();
960
961 ibufp = sc->sc_ibuf;
962 ibufend = sc->sc_ibufp;
963
964 if (ibufp == ibufend) {
965 splx(s);
966 continue;
967 }
968
969 sc->sc_ibufp = sc->sc_ibuf = (ibufp == sc->sc_ibufs[0]) ?
970 sc->sc_ibufs[1] : sc->sc_ibufs[0];
971 sc->sc_ibufhigh = sc->sc_ibuf + COM_IHIGHWATER;
972 sc->sc_ibufend = sc->sc_ibuf + COM_IBUFSIZE;
973
974 if (tp == 0 || !ISSET(tp->t_state, TS_ISOPEN)) {
975 splx(s);
976 continue;
977 }
978
979 if (ISSET(tp->t_cflag, CRTSCTS) &&
980 !ISSET(sc->sc_mcr, MCR_RTS)) {
981 /* XXX */
982 SET(sc->sc_mcr, MCR_RTS);
983 outb(sc->sc_iobase + com_mcr, sc->sc_mcr);
984 }
985
986 splx(s);
987
988 while (ibufp < ibufend) {
989 c = *ibufp++;
990 if (*ibufp & LSR_OE) {
991 sc->sc_overflows++;
992 if (sc->sc_errors++ == 0)
993 timeout(comdiag, sc, 60 * hz);
994 }
995 /* This is ugly, but fast. */
996 c |= lsrmap[(*ibufp++ & (LSR_BI|LSR_FE|LSR_PE)) >> 2];
997 (*linesw[tp->t_line].l_rint)(c, tp);
998 }
999 }
1000
1001 out:
1002 timeout(compoll, NULL, 1);
1003 }
1004
1005 int
1006 comintr(arg)
1007 void *arg;
1008 {
1009 struct com_softc *sc = arg;
1010 int iobase = sc->sc_iobase;
1011 struct tty *tp;
1012 u_char lsr, data, msr, delta;
1013 #ifdef COM_DEBUG
1014 int n;
1015 struct {
1016 u_char iir, lsr, msr;
1017 } iter[32];
1018 #endif
1019
1020 #ifdef COM_DEBUG
1021 n = 0;
1022 if (ISSET(iter[n].iir = inb(iobase + com_iir), IIR_NOPEND))
1023 return (0);
1024 #else
1025 if (ISSET(inb(iobase + com_iir), IIR_NOPEND))
1026 return (0);
1027 #endif
1028
1029 tp = sc->sc_tty;
1030
1031 for (;;) {
1032 #ifdef COM_DEBUG
1033 iter[n].lsr =
1034 #endif
1035 lsr = inb(iobase + com_lsr);
1036
1037 if (ISSET(lsr, LSR_RXRDY)) {
1038 register u_char *p = sc->sc_ibufp;
1039
1040 comevents = 1;
1041 do {
1042 data = inb(iobase + com_data);
1043 if (ISSET(lsr, LSR_BI)) {
1044 #ifdef notdef
1045 printf("break %02x %02x %02x %02x\n",
1046 sc->sc_msr, sc->sc_mcr, sc->sc_lcr,
1047 sc->sc_dtr);
1048 #endif
1049 #ifdef DDB
1050 if (sc->sc_dev.dv_unit == comconsole) {
1051 Debugger();
1052 goto next;
1053 }
1054 #endif
1055 }
1056 if (p >= sc->sc_ibufend) {
1057 sc->sc_floods++;
1058 if (sc->sc_errors++ == 0)
1059 timeout(comdiag, sc, 60 * hz);
1060 } else {
1061 *p++ = data;
1062 *p++ = lsr;
1063 if (p == sc->sc_ibufhigh &&
1064 ISSET(tp->t_cflag, CRTSCTS)) {
1065 /* XXX */
1066 CLR(sc->sc_mcr, MCR_RTS);
1067 outb(iobase + com_mcr,
1068 sc->sc_mcr);
1069 }
1070 }
1071 next:
1072 #ifdef COM_DEBUG
1073 if (++n >= 32)
1074 goto ohfudge;
1075 iter[n].lsr =
1076 #endif
1077 lsr = inb(iobase + com_lsr);
1078 } while (ISSET(lsr, LSR_RXRDY));
1079
1080 sc->sc_ibufp = p;
1081 }
1082 #ifdef COM_DEBUG
1083 else if (ISSET(lsr, LSR_BI|LSR_FE|LSR_PE|LSR_OE))
1084 printf("weird lsr %02x\n", lsr);
1085 #endif
1086
1087 #ifdef COM_DEBUG
1088 iter[n].msr =
1089 #endif
1090 msr = inb(iobase + com_msr);
1091
1092 if (msr != sc->sc_msr) {
1093 delta = msr ^ sc->sc_msr;
1094 sc->sc_msr = msr;
1095 if (ISSET(delta, MSR_DCD) &&
1096 !ISSET(sc->sc_swflags, COM_SW_SOFTCAR) &&
1097 (*linesw[tp->t_line].l_modem)(tp, ISSET(msr, MSR_DCD)) == 0) {
1098 CLR(sc->sc_mcr, sc->sc_dtr);
1099 outb(iobase + com_mcr, sc->sc_mcr);
1100 }
1101 if (ISSET(delta & msr, MSR_CTS) &&
1102 ISSET(tp->t_cflag, CRTSCTS)) {
1103 /* the line is up and we want to do rts/cts flow control */
1104 (*linesw[tp->t_line].l_start)(tp);
1105 }
1106 }
1107
1108 if (ISSET(lsr, LSR_TXRDY) && ISSET(tp->t_state, TS_BUSY)) {
1109 CLR(tp->t_state, TS_BUSY | TS_FLUSH);
1110 if (sc->sc_halt > 0)
1111 wakeup(&tp->t_outq);
1112 (*linesw[tp->t_line].l_start)(tp);
1113 }
1114
1115 #ifdef COM_DEBUG
1116 if (++n >= 32)
1117 goto ohfudge;
1118 if (ISSET(iter[n].iir = inb(iobase + com_iir), IIR_NOPEND))
1119 return (1);
1120 #else
1121 if (ISSET(inb(iobase + com_iir), IIR_NOPEND))
1122 return (1);
1123 #endif
1124 }
1125 #ifdef COM_DEBUG
1126 ohfudge:
1127 printf("comintr: too many iterations");
1128 for (n = 0; n < 32; n++) {
1129 if ((n % 4) == 0)
1130 printf("\ncomintr: iter[%02d]", n);
1131 printf(" %02x %02x %02x", iter[n].iir, iter[n].lsr, iter[n].msr);
1132 }
1133 printf("\n");
1134 printf("comintr: msr %02x mcr %02x lcr %02x ier %02x\n",
1135 sc->sc_msr, sc->sc_mcr, sc->sc_lcr, sc->sc_ier);
1136 printf("comintr: state %08x cc %d\n", sc->sc_tty->t_state,
1137 sc->sc_tty->t_outq.c_cc);
1138 #endif
1139 }
1140
1141 /*
1142 * Following are all routines needed for COM to act as console
1143 */
1144 #include <dev/cons.h>
1145
1146 void
1147 comcnprobe(cp)
1148 struct consdev *cp;
1149 {
1150
1151 if (!comprobe1(CONADDR)) {
1152 cp->cn_pri = CN_DEAD;
1153 return;
1154 }
1155
1156 /* locate the major number */
1157 for (commajor = 0; commajor < nchrdev; commajor++)
1158 if (cdevsw[commajor].d_open == comopen)
1159 break;
1160
1161 /* initialize required fields */
1162 cp->cn_dev = makedev(commajor, CONUNIT);
1163 #ifdef COMCONSOLE
1164 cp->cn_pri = CN_REMOTE; /* Force a serial port console */
1165 #else
1166 cp->cn_pri = CN_NORMAL;
1167 #endif
1168 }
1169
1170 void
1171 comcninit(cp)
1172 struct consdev *cp;
1173 {
1174
1175 cominit(CONUNIT, comdefaultrate);
1176 comconsole = CONUNIT;
1177 comconsinit = 0;
1178 }
1179
1180 cominit(unit, rate)
1181 int unit, rate;
1182 {
1183 int s = splhigh();
1184 int iobase = CONADDR;
1185 u_char stat;
1186
1187 outb(iobase + com_lcr, LCR_DLAB);
1188 rate = comspeed(comdefaultrate);
1189 outb(iobase + com_dlbl, rate);
1190 outb(iobase + com_dlbh, rate >> 8);
1191 outb(iobase + com_lcr, LCR_8BITS);
1192 outb(iobase + com_ier, IER_ERXRDY | IER_ETXRDY);
1193 outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_4);
1194 stat = inb(iobase + com_iir);
1195 splx(s);
1196 }
1197
1198 comcngetc(dev)
1199 dev_t dev;
1200 {
1201 int s = splhigh();
1202 int iobase = CONADDR;
1203 u_char stat, c;
1204
1205 while (!ISSET(stat = inb(iobase + com_lsr), LSR_RXRDY))
1206 ;
1207 c = inb(iobase + com_data);
1208 stat = inb(iobase + com_iir);
1209 splx(s);
1210 return c;
1211 }
1212
1213 /*
1214 * Console kernel output character routine.
1215 */
1216 void
1217 comcnputc(dev, c)
1218 dev_t dev;
1219 int c;
1220 {
1221 int s = splhigh();
1222 int iobase = CONADDR;
1223 u_char stat;
1224 register int timo;
1225
1226 #ifdef KGDB
1227 if (dev != kgdb_dev)
1228 #endif
1229 if (comconsinit == 0) {
1230 (void) cominit(COMUNIT(dev), comdefaultrate);
1231 comconsinit = 1;
1232 }
1233 /* wait for any pending transmission to finish */
1234 timo = 50000;
1235 while (!ISSET(stat = inb(iobase + com_lsr), LSR_TXRDY) && --timo)
1236 ;
1237 outb(iobase + com_data, c);
1238 /* wait for this transmission to complete */
1239 timo = 1500000;
1240 while (!ISSET(stat = inb(iobase + com_lsr), LSR_TXRDY) && --timo)
1241 ;
1242 /* clear any interrupts generated by this transmission */
1243 stat = inb(iobase + com_iir);
1244 splx(s);
1245 }
1246
1247 void
1248 comcnpollc(dev, on)
1249 dev_t dev;
1250 int on;
1251 {
1252
1253 }
1254