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