cd18xx.c revision 1.16 1 /* $NetBSD: cd18xx.c,v 1.16 2006/07/21 16:48:48 ad Exp $ */
2
3 /* XXXad does this even compile? */
4
5 /*
6 * Copyright (c) 1998, 2001 Matthew R. Green
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. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*-
34 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
35 * All rights reserved.
36 *
37 * This code is derived from software contributed to The NetBSD Foundation
38 * by Charles M. Hannum.
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 NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * Copyright (c) 1991 The Regents of the University of California.
71 * All rights reserved.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. Neither the name of the University nor the names of its contributors
82 * may be used to endorse or promote products derived from this software
83 * without specific prior written permission.
84 *
85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95 * SUCH DAMAGE.
96 *
97 * @(#)com.c 7.5 (Berkeley) 5/16/91
98 */
99
100 /*
101 * cirrus logic CL-CD180/CD1864/CD1865 driver, based in (large) parts on
102 * the com and z8530 drivers. thanks charles.
103 */
104
105 #include <sys/cdefs.h>
106 __KERNEL_RCSID(0, "$NetBSD: cd18xx.c,v 1.16 2006/07/21 16:48:48 ad Exp $");
107
108 #include <sys/param.h>
109 #include <sys/conf.h>
110 #include <sys/device.h>
111 #include <sys/systm.h>
112 #include <sys/malloc.h>
113 #include <sys/proc.h>
114 #include <sys/kernel.h>
115 #include <sys/tty.h>
116 #include <sys/fcntl.h>
117 #include <sys/kauth.h>
118
119 #include <machine/bus.h>
120
121 #include <dev/ic/cd18xxvar.h>
122 #include <dev/ic/cd18xxreg.h>
123
124 #include "ioconf.h"
125
126 /*
127 * some helpers
128 */
129
130 /* macros to clear/set/test flags. */
131 #define SET(t, f) (t) |= (f)
132 #define CLR(t, f) (t) &= ~(f)
133 #define ISSET(t, f) ((t) & (f))
134
135 static void cdtty_attach(struct cd18xx_softc *, int);
136
137 static inline void cd18xx_rint(struct cd18xx_softc *, int *);
138 static inline void cd18xx_tint(struct cd18xx_softc *, int *);
139 static inline void cd18xx_mint(struct cd18xx_softc *, int *);
140
141 void cdtty_rxsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
142 void cdtty_txsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
143 void cdtty_stsoft(struct cd18xx_softc *, struct cdtty_port *, struct tty *);
144 void cd18xx_softintr(void *);
145
146 dev_type_open(cdttyopen);
147 dev_type_close(cdttyclose);
148 dev_type_read(cdttyread);
149 dev_type_write(cdttywrite);
150 dev_type_ioctl(cdttyioctl);
151 dev_type_stop(cdttystop);
152 dev_type_tty(cdttytty);
153 dev_type_poll(cdttypoll);
154
155 const struct cdevsw cdtty_cdevsw = {
156 cdttyopen, cdttyclose, cdttyread, cdttywrite, cdttyioctl,
157 cdttystop, cdttytty, cdttypoll, nommap, ttykqfilter, D_TTY
158 };
159
160 static void cdtty_shutdown(struct cd18xx_softc *, struct cdtty_port *);
161 static void cdttystart(struct tty *);
162 static int cdttyparam(struct tty *, struct termios *);
163 static void cdtty_break(struct cd18xx_softc *, struct cdtty_port *, int);
164 static void cdtty_modem(struct cd18xx_softc *, struct cdtty_port *, int);
165 static int cdttyhwiflow(struct tty *, int);
166 static void cdtty_hwiflow(struct cd18xx_softc *, struct cdtty_port *);
167
168 static void cdtty_loadchannelregs(struct cd18xx_softc *,
169 struct cdtty_port *);
170
171 /* default read buffer size */
172 u_int cdtty_rbuf_size = CDTTY_RING_SIZE;
173
174 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
175 u_int cdtty_rbuf_hiwat = (CDTTY_RING_SIZE * 1) / 4;
176 u_int cdtty_rbuf_lowat = (CDTTY_RING_SIZE * 3) / 4;
177
178 #define CD18XXDEBUG
179 #ifdef CD18XXDEBUG
180 #define CDD_INFO 0x0001
181 #define CDD_INTR 0x0002
182 int cd18xx_debug = CDD_INTR|CDD_INFO;
183 # define DPRINTF(l, x) if (cd18xx_debug & l) printf x
184 #else
185 # define DPRINTF(l, x) /* nothing */
186 #endif
187
188 /* Known supported revisions. */
189 struct cd18xx_revs {
190 u_char revision;
191 u_char onehundred_pin;
192 char *name;
193 } cd18xx_revs[] = {
194 { CD180_GFRCR_REV_B, 0, "CL-CD180 rev. B" },
195 { CD180_GFRCR_REV_C, 0, "CL-CD180 rev. C" },
196 { CD1864_GFRCR_REVISION_A, 1, "CL-CD1864 rev. A" },
197 { CD1865_GFRCR_REVISION_A, 1, "CL-CD1865 rev. A" },
198 { CD1865_GFRCR_REVISION_B, 1, "CL-CD1865 rev. B" },
199 { CD1865_GFRCR_REVISION_C, 1, "CL-CD1865 rev. C" },
200 { 0, 0, 0 }
201 };
202
203 /* wait for the CCR to go to zero */
204 static inline int cd18xx_wait_ccr(struct cd18xx_softc *);
205 static inline int
206 cd18xx_wait_ccr(sc)
207 struct cd18xx_softc *sc;
208 {
209 int i = 100000;
210
211 while (--i &&
212 bus_space_read_1(sc->sc_tag, sc->sc_handle, CD18xx_CCR) != 0)
213 ;
214 return (i == 0);
215 }
216
217 /*
218 * device attach routine, high-end portion
219 */
220 void
221 cd18xx_attach(sc)
222 struct cd18xx_softc *sc;
223 {
224 static int chip_id_next = 1;
225 int onehundred_pin, revision, i, port;
226
227 /* read and print the revision */
228 revision = cd18xx_read(sc, CD18xx_GFRCR);
229 onehundred_pin = ISSET(cd18xx_read(sc, CD18xx_SRCR),CD18xx_SRCR_PKGTYP);
230 for (i = 0; cd18xx_revs[i].name; i++)
231 if (revision == cd18xx_revs[i].revision ||
232 onehundred_pin == cd18xx_revs[i].onehundred_pin) {
233 printf(": %s", cd18xx_revs[i].name);
234 break;
235 }
236
237 if (cd18xx_revs[i].name == NULL) {
238 printf("%s: unknown revision, bailing.\n", sc->sc_dev.dv_xname);
239 return;
240 }
241
242 /* prepare for reset */
243 cd18xx_set_car(sc, 0);
244 cd18xx_write(sc, CD18xx_GSVR, CD18xx_GSVR_CLEAR);
245
246 /* wait for CCR to go to zero */
247 if (cd18xx_wait_ccr(sc)) {
248 printf("cd18xx_attach: reset change command timed out\n");
249 return;
250 }
251
252 /* full reset of all channels */
253 cd18xx_write(sc, CD18xx_CCR,
254 CD18xx_CCR_RESET|CD18xx_CCR_RESET_HARD);
255
256 /* loop until the GSVR is ready */
257 i = 100000;
258 while (--i && cd18xx_read(sc, CD18xx_GSVR) == CD18xx_GSVR_READY)
259 ;
260 if (i == 0) {
261 printf("\n%s: did not reset!\n", sc->sc_dev.dv_xname);
262 return;
263 }
264
265 /* write the chip_id */
266 sc->sc_chip_id = chip_id_next++;
267 #ifdef DIAGNOSTIC
268 if (sc->sc_chip_id > 31)
269 panic("more than 31 cd18xx's? help.");
270 #endif
271 cd18xx_write(sc, CD18xx_GSVR, CD18xx_GSVR_SETID(sc));
272
273 /* rx/tx/modem service match vectors, initalised by higher level */
274 cd18xx_write(sc, CD18xx_MSMR, sc->sc_msmr | 0x80);
275 cd18xx_write(sc, CD18xx_TSMR, sc->sc_tsmr | 0x80);
276 cd18xx_write(sc, CD18xx_RSMR, sc->sc_rsmr | 0x80);
277
278 printf(", gsvr %x msmr %x tsmr %x rsmr %x",
279 cd18xx_read(sc, CD18xx_GSVR),
280 cd18xx_read(sc, CD18xx_MSMR),
281 cd18xx_read(sc, CD18xx_TSMR),
282 cd18xx_read(sc, CD18xx_RSMR));
283
284 /* prescale registers */
285 sc->sc_pprh = 0xf0;
286 sc->sc_pprl = 0;
287 cd18xx_write(sc, CD18xx_PPRH, sc->sc_pprh);
288 cd18xx_write(sc, CD18xx_PPRL, sc->sc_pprl);
289
290 /* establish our soft interrupt. */
291 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, cd18xx_softintr, sc);
292
293 printf(", 8 ports ready (chip id %d)\n", sc->sc_chip_id);
294
295 /*
296 * finally, we loop over all 8 channels initialising them
297 */
298 for (port = 0; port < 8; port++)
299 cdtty_attach(sc, port);
300 }
301
302 /* tty portion of the code */
303
304 /*
305 * tty portion attach routine
306 */
307 void
308 cdtty_attach(sc, port)
309 struct cd18xx_softc *sc;
310 int port;
311 {
312 struct cdtty_port *p = &sc->sc_ports[port];
313 int i;
314
315 /* load CAR with channel number */
316 cd18xx_set_car(sc, port);
317
318 /* wait for CCR to go to zero */
319 if (cd18xx_wait_ccr(sc)) {
320 printf("cd18xx_attach: change command timed out setting "
321 "CAR for port %d\n", i);
322 return;
323 }
324
325 /* set the RPTR to (arbitrary) 8 */
326 cd18xx_write(sc, CD18xx_RTPR, 8);
327
328 /* reset the modem signal value register */
329 sc->sc_ports[port].p_msvr = CD18xx_MSVR_RESET;
330
331 /* zero the service request enable register */
332 cd18xx_write(sc, CD18xx_SRER, 0);
333
334 /* enable the transmitter & receiver */
335 SET(p->p_chanctl, CD18xx_CCR_CHANCTL |
336 CD18xx_CCR_CHANCTL_TxEN |
337 CD18xx_CCR_CHANCTL_RxEN);
338
339 /* XXX no console or kgdb support yet! */
340
341 /* get a tty structure */
342 p->p_tty = ttymalloc();
343 p->p_tty->t_oproc = cdttystart;
344 p->p_tty->t_param = cdttyparam;
345 p->p_tty->t_hwiflow = cdttyhwiflow;
346
347 p->p_rbuf = malloc(cdtty_rbuf_size << 1, M_DEVBUF, M_WAITOK);
348 p->p_rbput = p->p_rbget = p->p_rbuf;
349 p->p_rbavail = cdtty_rbuf_size;
350 if (p->p_rbuf == NULL) {
351 printf("%s: unable to allocate ring buffer for tty %d\n",
352 sc->sc_dev.dv_xname, port);
353 return;
354 }
355 p->p_ebuf = p->p_rbuf + (cdtty_rbuf_size << 1);
356
357 tty_attach(p->p_tty);
358 }
359
360 /*
361 * cdtty_shutdown: called when the device is last closed.
362 */
363 void
364 cdtty_shutdown(sc, p)
365 struct cd18xx_softc *sc;
366 struct cdtty_port *p;
367 {
368 struct tty *tp = p->p_tty;
369 int s;
370
371 s = splserial();
372
373 /* If we were asserting flow control, then deassert it. */
374 SET(p->p_rx_flags, RX_IBUF_BLOCKED);
375 cdtty_hwiflow(sc, p);
376
377 /* Clear any break condition set with TIOCSBRK. */
378 cdtty_break(sc, p, 0);
379
380 /*
381 * Hang up if necessary. Wait a bit, so the other side has time to
382 * notice even if we immediately open the port again.
383 * Avoid tsleeping above splhigh().
384 */
385 if (ISSET(tp->t_cflag, HUPCL)) {
386 cdtty_modem(sc, p, 0);
387 splx(s);
388 /* XXX tsleep will only timeout */
389 (void) tsleep(sc, TTIPRI, ttclos, hz);
390 s = splserial();
391 }
392
393 /* Turn off interrupts. */
394 p->p_srer = 0;
395 cd18xx_write(sc, CD18xx_SRER, p->p_srer);
396
397 splx(s);
398 }
399
400 /*
401 * cdttyopen: open syscall for cdtty terminals..
402 */
403 int
404 cdttyopen(dev, flag, mode, p)
405 dev_t dev;
406 int flag;
407 int mode;
408 struct proc *p;
409 {
410 struct tty *tp;
411 struct cd18xx_softc *sc;
412 struct cdtty_port *port;
413 int channel, instance, s, error;
414
415 channel = CD18XX_CHANNEL(dev);
416 instance = CD18XX_INSTANCE(dev);
417
418 /* ensure instance is valid */
419 if (instance >= clcd_cd.cd_ndevs)
420 return (ENXIO);
421
422 /* get softc and port */
423 sc = clcd_cd.cd_devs[instance];
424 if (sc == NULL)
425 return (ENXIO);
426 port = &sc->sc_ports[channel];
427 if (port == NULL || port->p_rbuf == NULL)
428 return (ENXIO);
429
430 /* kgdb support? maybe later... */
431
432 tp = port->p_tty;
433
434 /* enforce exclude */
435 if (tp == NULL ||
436 (ISSET(tp->t_state, TS_ISOPEN) &&
437 ISSET(tp->t_state, TS_XCLUDE) &&
438 kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
439 &l->l_acflag) != 0))
440 return (EBUSY);
441
442 s = spltty();
443
444 /*
445 * Do the following iff this is a first open.
446 */
447 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
448 struct termios t;
449
450 /* set up things in tp as necessary */
451 tp->t_dev = dev;
452
453 /*
454 * Initialize the termios status to the defaults. Add in the
455 * sticky bits from TIOCSFLAGS.
456 */
457 t.c_ispeed = 0;
458 t.c_ospeed = TTYDEF_SPEED;
459 t.c_cflag = TTYDEF_CFLAG;
460
461 if (ISSET(port->p_swflags, TIOCFLAG_CLOCAL))
462 SET(t.c_cflag, CLOCAL);
463 if (ISSET(port->p_swflags, TIOCFLAG_CRTSCTS))
464 SET(t.c_cflag, CRTSCTS);
465 if (ISSET(port->p_swflags, TIOCFLAG_CDTRCTS))
466 SET(t.c_cflag, CDTRCTS);
467 if (ISSET(port->p_swflags, TIOCFLAG_MDMBUF))
468 SET(t.c_cflag, MDMBUF);
469
470 /* Make sure param will see changes. */
471 tp->t_ospeed = 0; /* XXX set above ignored? */
472 (void)cdttyparam(tp, &t);
473
474 tp->t_iflag = TTYDEF_IFLAG;
475 tp->t_oflag = TTYDEF_OFLAG;
476 tp->t_lflag = TTYDEF_LFLAG;
477 ttychars(tp);
478 ttsetwater(tp);
479
480 (void)splserial();
481
482 /* turn on rx and modem interrupts */
483 cd18xx_set_car(sc, CD18XX_CHANNEL(dev));
484 SET(port->p_srer, CD18xx_SRER_Rx |
485 CD18xx_SRER_RxSC |
486 CD18xx_SRER_CD);
487 cd18xx_write(sc, CD18xx_SRER, port->p_srer);
488
489 /* always turn on DTR when open */
490 cdtty_modem(sc, port, 1);
491
492 /* initialise ring buffer */
493 port->p_rbget = port->p_rbput = port->p_rbuf;
494 port->p_rbavail = cdtty_rbuf_size;
495 CLR(port->p_rx_flags, RX_ANY_BLOCK);
496 cdtty_hwiflow(sc, port);
497 }
498
499 /* drop spl back before going into the line open */
500 splx(s);
501
502 error = ttyopen(tp, CD18XX_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
503 if (error == 0)
504 error = (*tp->t_linesw->l_open)(dev, tp);
505
506 return (error);
507 }
508
509 /*
510 * cdttyclose: close syscall for cdtty terminals..
511 */
512 int
513 cdttyclose(dev, flag, mode, p)
514 dev_t dev;
515 int flag;
516 int mode;
517 struct proc *p;
518 {
519 struct cd18xx_softc *sc;
520 struct cdtty_port *port;
521 struct tty *tp;
522 int channel, instance;
523
524 channel = CD18XX_CHANNEL(dev);
525 instance = CD18XX_INSTANCE(dev);
526
527 /* ensure instance is valid */
528 if (instance >= clcd_cd.cd_ndevs)
529 return (ENXIO);
530
531 /* get softc and port */
532 sc = clcd_cd.cd_devs[instance];
533 if (sc == NULL)
534 return (ENXIO);
535 port = &sc->sc_ports[channel];
536
537 tp = port->p_tty;
538
539 (*tp->t_linesw->l_close)(tp, flag);
540 ttyclose(tp);
541
542 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
543 /*
544 * Although we got a last close, the device may still be in
545 * use; e.g. if this was the dialout node, and there are still
546 * processes waiting for carrier on the non-dialout node.
547 */
548 cdtty_shutdown(sc, port);
549 }
550
551 return (0);
552 }
553
554 /*
555 * cdttyread: read syscall for cdtty terminals..
556 */
557 int
558 cdttyread(dev, uio, flag)
559 dev_t dev;
560 struct uio *uio;
561 int flag;
562 {
563 struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
564 struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
565 struct tty *tp = port->p_tty;
566
567 return ((*tp->t_linesw->l_read)(tp, uio, flag));
568 }
569
570 /*
571 * cdttywrite: write syscall for cdtty terminals..
572 */
573 int
574 cdttywrite(dev, uio, flag)
575 dev_t dev;
576 struct uio *uio;
577 int flag;
578 {
579 struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
580 struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
581 struct tty *tp = port->p_tty;
582
583 return ((*tp->t_linesw->l_write)(tp, uio, flag));
584 }
585
586 int
587 cdttypoll(dev, events, p)
588 dev_t dev;
589 int events;
590 struct proc *p;
591 {
592 struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
593 struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
594 struct tty *tp = port->p_tty;
595
596 return ((*tp->t_linesw->l_poll)(tp, events, p));
597 }
598
599 /*
600 * cdttytty: return a pointer to our (cdtty) tp.
601 */
602 struct tty *
603 cdttytty(dev)
604 dev_t dev;
605 {
606 struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
607 struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
608
609 return (port->p_tty);
610 }
611
612 /*
613 * cdttyioctl: ioctl syscall for cdtty terminals..
614 */
615 int
616 cdttyioctl(dev, cmd, data, flag, p)
617 dev_t dev;
618 u_long cmd;
619 caddr_t data;
620 int flag;
621 struct proc *p;
622 {
623 struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(dev)];
624 struct cdtty_port *port = &sc->sc_ports[CD18XX_CHANNEL(dev)];
625 struct tty *tp = port->p_tty;
626 int error, s;
627
628 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
629 if (error != EPASSTHROUGH)
630 return (error);
631
632 error = ttioctl(tp, cmd, data, flag, p);
633 if (error != EPASSTHROUGH)
634 return (error);
635
636 s = splserial();
637
638 switch (cmd) {
639 case TIOCSBRK:
640 cdtty_break(sc, port, 1);
641 break;
642
643 case TIOCCBRK:
644 cdtty_break(sc, port, 0);
645 break;
646
647 case TIOCSDTR:
648 cdtty_modem(sc, port, 1);
649 break;
650
651 case TIOCCDTR:
652 cdtty_modem(sc, port, 0);
653 break;
654
655 case TIOCGFLAGS:
656 *(int *)data = port->p_swflags;
657 break;
658
659 case TIOCSFLAGS:
660 error = kauth_authorize_generic(l->l_cred,
661 KAUTH_GENERIC_ISSUSER, &l->l_acflag);
662 if (error)
663 return (error);
664 port->p_swflags = *(int *)data;
665 break;
666
667 case TIOCMSET:
668 case TIOCMBIS:
669 case TIOCMBIC:
670 case TIOCMGET:
671 default:
672 return (EPASSTHROUGH);
673 }
674
675 splx(s);
676 return (0);
677 }
678
679 /*
680 * Start or restart transmission.
681 */
682 static void
683 cdttystart(tp)
684 struct tty *tp;
685 {
686 struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
687 struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
688 int s;
689
690 s = spltty();
691 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
692 goto out;
693 if (p->p_tx_stopped)
694 goto out;
695
696 if (tp->t_outq.c_cc <= tp->t_lowat) {
697 if (ISSET(tp->t_state, TS_ASLEEP)) {
698 CLR(tp->t_state, TS_ASLEEP);
699 wakeup((caddr_t)&tp->t_outq);
700 }
701 selwakeup(&tp->t_wsel);
702 if (tp->t_outq.c_cc == 0)
703 goto out;
704 }
705
706 /* Grab the first contiguous region of buffer space. */
707 {
708 u_char *tba;
709 int tbc;
710
711 tba = tp->t_outq.c_cf;
712 tbc = ndqb(&tp->t_outq, 0);
713
714 (void)splserial();
715
716 p->p_tba = tba;
717 p->p_tbc = tbc;
718 }
719
720 SET(tp->t_state, TS_BUSY);
721 p->p_tx_busy = 1;
722
723 /* turn on tx interrupts */
724 if ((p->p_srer & CD18xx_SRER_Tx) == 0) {
725 cd18xx_set_car(sc, CD18XX_CHANNEL(tp->t_dev));
726 SET(p->p_srer, CD18xx_SRER_Tx);
727 cd18xx_write(sc, CD18xx_SRER, p->p_srer);
728 }
729
730 /*
731 * Now bail; we can't actually transmit bytes until we're in a
732 * transmit interrupt service routine.
733 */
734 out:
735 splx(s);
736 return;
737 }
738
739 /*
740 * cdttystop: handing ^S or other stop signals, for a cdtty
741 */
742 void
743 cdttystop(tp, flag)
744 struct tty *tp;
745 int flag;
746 {
747 struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
748 struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
749 int s;
750
751 s = splserial();
752 if (ISSET(tp->t_state, TS_BUSY)) {
753 /* Stop transmitting at the next chunk. */
754 p->p_tbc = 0;
755 p->p_heldtbc = 0;
756 if (!ISSET(tp->t_state, TS_TTSTOP))
757 SET(tp->t_state, TS_FLUSH);
758 }
759 splx(s);
760 }
761
762 /*
763 * load a channel's registers.
764 */
765 void
766 cdtty_loadchannelregs(sc, p)
767 struct cd18xx_softc *sc;
768 struct cdtty_port *p;
769 {
770
771 cd18xx_set_car(sc, CD18XX_CHANNEL(p->p_tty->t_dev));
772 cd18xx_write(sc, CD18xx_SRER, p->p_srer);
773 cd18xx_write(sc, CD18xx_MSVR, p->p_msvr_active = p->p_msvr);
774 cd18xx_write(sc, CD18xx_COR1, p->p_cor1);
775 cd18xx_write(sc, CD18xx_COR2, p->p_cor2);
776 cd18xx_write(sc, CD18xx_COR3, p->p_cor3);
777 /*
778 * COR2 and COR3 change commands are not required here for
779 * the CL-CD1865 but we do them anyway for simplicity.
780 */
781 cd18xx_write(sc, CD18xx_CCR, CD18xx_CCR_CORCHG |
782 CD18xx_CCR_CORCHG_COR1 |
783 CD18xx_CCR_CORCHG_COR2 |
784 CD18xx_CCR_CORCHG_COR3);
785 cd18xx_write(sc, CD18xx_RBPRH, p->p_rbprh);
786 cd18xx_write(sc, CD18xx_RBPRL, p->p_rbprl);
787 cd18xx_write(sc, CD18xx_TBPRH, p->p_tbprh);
788 cd18xx_write(sc, CD18xx_TBPRL, p->p_tbprl);
789 if (cd18xx_wait_ccr(sc)) {
790 DPRINTF(CDD_INFO,
791 ("%s: cdtty_loadchannelregs ccr wait timed out\n",
792 sc->sc_dev.dv_xname));
793 }
794 cd18xx_write(sc, CD18xx_CCR, p->p_chanctl);
795 }
796
797 /*
798 * Set tty parameters from termios.
799 * XXX - Should just copy the whole termios after
800 * making sure all the changes could be done.
801 */
802 static int
803 cdttyparam(tp, t)
804 struct tty *tp;
805 struct termios *t;
806 {
807 struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
808 struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
809 int s;
810
811 /* Check requested parameters. */
812 if (t->c_ospeed < 0)
813 return (EINVAL);
814 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
815 return (EINVAL);
816
817 /*
818 * For the console, always force CLOCAL and !HUPCL, so that the port
819 * is always active.
820 */
821 if (ISSET(p->p_swflags, TIOCFLAG_SOFTCAR)) {
822 SET(t->c_cflag, CLOCAL);
823 CLR(t->c_cflag, HUPCL);
824 }
825
826 /*
827 * If there were no changes, don't do anything. This avoids dropping
828 * input and improves performance when all we did was frob things like
829 * VMIN and VTIME.
830 */
831 if (tp->t_ospeed == t->c_ospeed &&
832 tp->t_cflag == t->c_cflag)
833 return (0);
834
835 /*
836 * Block interrupts so that state will not
837 * be altered until we are done setting it up.
838 */
839 s = splserial();
840
841 /*
842 * Copy across the size, parity and stop bit info.
843 */
844 switch (t->c_cflag & CSIZE) {
845 case CS5:
846 p->p_cor1 = CD18xx_COR1_CS5;
847 break;
848 case CS6:
849 p->p_cor1 = CD18xx_COR1_CS6;
850 break;
851 case CS7:
852 p->p_cor1 = CD18xx_COR1_CS7;
853 break;
854 default:
855 p->p_cor1 = CD18xx_COR1_CS8;
856 break;
857 }
858 if (ISSET(t->c_cflag, PARENB)) {
859 SET(p->p_cor1, CD18xx_COR1_PARITY_NORMAL);
860 if (ISSET(t->c_cflag, PARODD))
861 SET(p->p_cor1, CD18xx_COR1_PARITY_ODD);
862 }
863 if (!ISSET(t->c_iflag, INPCK))
864 SET(p->p_cor1, CD18xx_COR1_IGNORE);
865 if (ISSET(t->c_cflag, CSTOPB))
866 SET(p->p_cor1, CD18xx_COR1_STOPBIT_2);
867
868 /*
869 * If we're not in a mode that assumes a connection is present, then
870 * ignore carrier changes.
871 */
872 if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
873 p->p_msvr_dcd = 0;
874 else
875 p->p_msvr_dcd = CD18xx_MSVR_CD;
876
877 /*
878 * Set the flow control pins depending on the current flow control
879 * mode.
880 */
881 if (ISSET(t->c_cflag, CRTSCTS)) {
882 p->p_mcor1_dtr = CD18xx_MCOR1_DTR;
883 p->p_msvr_rts = CD18xx_MSVR_RTS;
884 p->p_msvr_cts = CD18xx_MSVR_CTS;
885 p->p_cor2 = CD18xx_COR2_RTSAOE|CD18xx_COR2_CTSAE;
886 } else if (ISSET(t->c_cflag, MDMBUF)) {
887 /*
888 * For DTR/DCD flow control, make sure we don't toggle DTR for
889 * carrier detection.
890 */
891 p->p_mcor1_dtr = 0;
892 p->p_msvr_rts = CD18xx_MSVR_DTR;
893 p->p_msvr_cts = CD18xx_MSVR_CD;
894 p->p_cor2 = 0;
895 } else {
896 /*
897 * If no flow control, then always set RTS. This will make
898 * the other side happy if it mistakenly thinks we're doing
899 * RTS/CTS flow control.
900 */
901 p->p_mcor1_dtr = CD18xx_MSVR_DTR;
902 p->p_msvr_rts = 0;
903 p->p_msvr_cts = 0;
904 p->p_cor2 = 0;
905 }
906 p->p_msvr_mask = p->p_msvr_cts | p->p_msvr_dcd;
907
908 /*
909 * Set the FIFO threshold based on the receive speed.
910 *
911 * * If it's a low speed, it's probably a mouse or some other
912 * interactive device, so set the threshold low.
913 * * If it's a high speed, trim the trigger level down to prevent
914 * overflows.
915 * * Otherwise set it a bit higher.
916 */
917 p->p_cor3 = (t->c_ospeed <= 1200 ? 1 : t->c_ospeed <= 38400 ? 8 : 4);
918
919 #define PORT_RATE(o, s) \
920 (((((o) + (s)/2) / (s)) + CD18xx_xBRPR_TPC/2) / CD18xx_xBRPR_TPC)
921 /* Compute BPS for the requested speeds */
922 if (t->c_ospeed) {
923 u_int32_t tbpr = PORT_RATE(sc->sc_osc, t->c_ospeed);
924
925 if (tbpr == 0 || tbpr > 0xffff)
926 return (EINVAL);
927
928 p->p_tbprh = tbpr >> 8;
929 p->p_tbprl = tbpr & 0xff;
930 }
931
932 if (t->c_ispeed) {
933 u_int32_t rbpr = PORT_RATE(sc->sc_osc, t->c_ispeed);
934
935 if (rbpr == 0 || rbpr > 0xffff)
936 return (EINVAL);
937
938 p->p_rbprh = rbpr >> 8;
939 p->p_rbprl = rbpr & 0xff;
940 }
941
942 /* And copy to tty. */
943 tp->t_ispeed = 0;
944 tp->t_ospeed = t->c_ospeed;
945 tp->t_cflag = t->c_cflag;
946
947 if (!p->p_heldchange) {
948 if (p->p_tx_busy) {
949 p->p_heldtbc = p->p_tbc;
950 p->p_tbc = 0;
951 p->p_heldchange = 1;
952 } else
953 cdtty_loadchannelregs(sc, p);
954 }
955
956 if (!ISSET(t->c_cflag, CHWFLOW)) {
957 /* Disable the high water mark. */
958 p->p_r_hiwat = 0;
959 p->p_r_lowat = 0;
960 if (ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
961 CLR(p->p_rx_flags, RX_TTY_OVERFLOWED);
962 softintr_schedule(sc->sc_si);
963 }
964 if (ISSET(p->p_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
965 CLR(p->p_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
966 cdtty_hwiflow(sc, p);
967 }
968 } else {
969 p->p_r_hiwat = cdtty_rbuf_hiwat;
970 p->p_r_lowat = cdtty_rbuf_lowat;
971 }
972
973 splx(s);
974
975 /*
976 * Update the tty layer's idea of the carrier bit, in case we changed
977 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
978 * explicit request.
979 */
980 (void) (*tp->t_linesw->l_modem)(tp, ISSET(p->p_msvr, CD18xx_MSVR_CD));
981
982 if (!ISSET(t->c_cflag, CHWFLOW)) {
983 if (p->p_tx_stopped) {
984 p->p_tx_stopped = 0;
985 cdttystart(tp);
986 }
987 }
988
989 return (0);
990 }
991
992 static void
993 cdtty_break(sc, p, onoff)
994 struct cd18xx_softc *sc;
995 struct cdtty_port *p;
996 int onoff;
997 {
998
999 /* tell tx intr handler we need a break */
1000 p->p_needbreak = !!onoff;
1001
1002 /* turn on tx interrupts if break has changed */
1003 if (p->p_needbreak != p->p_break)
1004 SET(p->p_srer, CD18xx_SRER_Tx);
1005
1006 if (!p->p_heldchange) {
1007 if (p->p_tx_busy) {
1008 p->p_heldtbc = p->p_tbc;
1009 p->p_tbc = 0;
1010 p->p_heldchange = 1;
1011 } else
1012 cdtty_loadchannelregs(sc, p);
1013 }
1014 }
1015
1016 /*
1017 * Raise or lower modem control (DTR/RTS) signals. If a character is
1018 * in transmission, the change is deferred.
1019 */
1020 static void
1021 cdtty_modem(sc, p, onoff)
1022 struct cd18xx_softc *sc;
1023 struct cdtty_port *p;
1024 int onoff;
1025 {
1026
1027 if (p->p_mcor1_dtr == 0)
1028 return;
1029
1030 if (onoff)
1031 CLR(p->p_mcor1, p->p_mcor1_dtr);
1032 else
1033 SET(p->p_mcor1, p->p_mcor1_dtr);
1034
1035 if (!p->p_heldchange) {
1036 if (p->p_tx_busy) {
1037 p->p_heldtbc = p->p_tbc;
1038 p->p_tbc = 0;
1039 p->p_heldchange = 1;
1040 } else
1041 cdtty_loadchannelregs(sc, p);
1042 }
1043 }
1044
1045 /*
1046 * Try to block or unblock input using hardware flow-control.
1047 * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
1048 * if this function returns non-zero, the TS_TBLOCK flag will
1049 * be set or cleared according to the "block" arg passed.
1050 */
1051 int
1052 cdttyhwiflow(tp, block)
1053 struct tty *tp;
1054 int block;
1055 {
1056 struct cd18xx_softc *sc = clcd_cd.cd_devs[CD18XX_INSTANCE(tp->t_dev)];
1057 struct cdtty_port *p = &sc->sc_ports[CD18XX_CHANNEL(tp->t_dev)];
1058 int s;
1059
1060 if (p->p_msvr_rts == 0)
1061 return (0);
1062
1063 s = splserial();
1064 if (block) {
1065 if (!ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
1066 SET(p->p_rx_flags, RX_TTY_BLOCKED);
1067 cdtty_hwiflow(sc, p);
1068 }
1069 } else {
1070 if (ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
1071 CLR(p->p_rx_flags, RX_TTY_OVERFLOWED);
1072 softintr_schedule(sc->sc_si);
1073 }
1074 if (ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
1075 CLR(p->p_rx_flags, RX_TTY_BLOCKED);
1076 cdtty_hwiflow(sc, p);
1077 }
1078 }
1079 splx(s);
1080 return (1);
1081 }
1082
1083 /*
1084 * Internal version of cdttyhwiflow, called at cdtty's priority.
1085 */
1086 static void
1087 cdtty_hwiflow(sc, p)
1088 struct cd18xx_softc *sc;
1089 struct cdtty_port *p;
1090 {
1091
1092 if (p->p_msvr_rts == 0)
1093 return;
1094
1095 if (ISSET(p->p_rx_flags, RX_ANY_BLOCK)) {
1096 CLR(p->p_msvr, p->p_msvr_rts);
1097 CLR(p->p_msvr_active, p->p_msvr_rts);
1098 } else {
1099 SET(p->p_msvr, p->p_msvr_rts);
1100 SET(p->p_msvr_active, p->p_msvr_rts);
1101 }
1102 cd18xx_set_car(sc, CD18XX_CHANNEL(p->p_tty->t_dev));
1103 cd18xx_write(sc, CD18xx_MSVR, p->p_msvr_active);
1104 }
1105
1106 /*
1107 * indiviual interrupt routines.
1108 */
1109
1110 /*
1111 * this is the number of interrupts allowed, total. set it to 0
1112 * to allow unlimited interrpts
1113 */
1114 #define INTR_MAX_ALLOWED 0
1115
1116 #if INTR_MAX_ALLOWED == 0
1117 #define GOTINTR(sc, p) /* nothing */
1118 #else
1119 int intrcount;
1120 #define GOTINTR(sc, p) \
1121 do { \
1122 if (intrcount++ == INTR_MAX_ALLOWED) { \
1123 CLR(p->p_srer, CD18xx_SRER_Tx); \
1124 cd18xx_write(sc, CD18xx_SRER, p->p_srer); \
1125 } \
1126 DPRINTF(CDD_INTR, (", intrcount %d srer %x", intrcount, p->p_srer)); \
1127 } while (0)
1128 #endif
1129
1130 /* receiver interrupt */
1131 static inline void
1132 cd18xx_rint(sc, ns)
1133 struct cd18xx_softc *sc;
1134 int *ns;
1135 {
1136 struct cdtty_port *p;
1137 u_int channel, count;
1138 u_char *put, *end;
1139 u_int cc;
1140
1141 /* work out the channel and softc */
1142 channel = cd18xx_get_gscr1_channel(sc);
1143 p = &sc->sc_ports[channel];
1144 DPRINTF(CDD_INTR, ("%s: rint: channel %d", sc->sc_dev.dv_xname, channel));
1145 GOTINTR(sc, p);
1146
1147 end = p->p_ebuf;
1148 put = p->p_rbput;
1149 cc = p->p_rbavail;
1150
1151 /* read as many bytes as necessary */
1152 count = cd18xx_read(sc, CD18xx_RDCR);
1153 DPRINTF(CDD_INTR, (", %d bytes available: ", count));
1154
1155 while (cc > 0 && count > 0) {
1156 u_char rcsr = cd18xx_read(sc, CD18xx_RCSR);
1157
1158 put[0] = cd18xx_read(sc, CD18xx_RDR);
1159 put[1] = rcsr;
1160
1161 if (rcsr)
1162 *ns = 1;
1163
1164 put += 2;
1165 if (put >= end)
1166 put = p->p_rbuf;
1167
1168 DPRINTF(CDD_INTR, ("."));
1169 cc--;
1170 count--;
1171 }
1172
1173 DPRINTF(CDD_INTR, (" finished reading"));
1174
1175 /*
1176 * Current string of incoming characters ended because
1177 * no more data was available or we ran out of space.
1178 * If we're out of space, turn off receive interrupts.
1179 */
1180 p->p_rbput = put;
1181 p->p_rbavail = cc;
1182 if (!ISSET(p->p_rx_flags, RX_TTY_OVERFLOWED)) {
1183 p->p_rx_ready = 1;
1184 }
1185
1186 /*
1187 * If we're out of space, disable receive interrupts
1188 * until the queue has drained a bit.
1189 */
1190 if (!cc) {
1191 SET(p->p_rx_flags, RX_IBUF_OVERFLOWED);
1192 CLR(p->p_srer, CD18xx_SRER_Rx |
1193 CD18xx_SRER_RxSC |
1194 CD18xx_SRER_CD);
1195 cd18xx_write(sc, CD18xx_SRER, p->p_srer);
1196 }
1197
1198 /* finish the interrupt transaction with the IC */
1199 cd18xx_write(sc, CD18xx_EOSRR, 0);
1200 DPRINTF(CDD_INTR, (", done\n"));
1201 }
1202
1203 /*
1204 * transmitter interrupt
1205 *
1206 * note this relys on the fact that we allow the transmitter FIFO to
1207 * drain completely
1208 */
1209 static inline void
1210 cd18xx_tint(sc, ns)
1211 struct cd18xx_softc *sc;
1212 int *ns;
1213 {
1214 struct cdtty_port *p;
1215 u_int channel;
1216
1217 /* work out the channel and softc */
1218 channel = cd18xx_get_gscr1_channel(sc);
1219 p = &sc->sc_ports[channel];
1220 DPRINTF(CDD_INTR, ("%s: tint: channel %d", sc->sc_dev.dv_xname,
1221 channel));
1222 GOTINTR(sc, p);
1223
1224 /* if the current break condition is wrong, fix it */
1225 if (p->p_break != p->p_needbreak) {
1226 u_char buf[2];
1227
1228 DPRINTF(CDD_INTR, (", changing break to %d", p->p_needbreak));
1229
1230 /* turn on ETC processing */
1231 cd18xx_write(sc, CD18xx_COR2, p->p_cor2 | CD18xx_COR2_ETC);
1232
1233 buf[0] = CD18xx_TDR_ETC_BYTE;
1234 buf[1] = p->p_needbreak ? CD18xx_TDR_BREAK_BYTE :
1235 CD18xx_TDR_NOBREAK_BYTE;
1236 cd18xx_write_multi(sc, CD18xx_TDR, buf, 2);
1237
1238 p->p_break = p->p_needbreak;
1239
1240 /* turn off ETC processing */
1241 cd18xx_write(sc, CD18xx_COR2, p->p_cor2);
1242 }
1243
1244 /*
1245 * If we've delayed a parameter change, do it now, and restart
1246 * output.
1247 */
1248 if (p->p_heldchange) {
1249 cdtty_loadchannelregs(sc, p);
1250 p->p_heldchange = 0;
1251 p->p_tbc = p->p_heldtbc;
1252 p->p_heldtbc = 0;
1253 }
1254
1255 /* Output the next chunk of the contiguous buffer, if any. */
1256 if (p->p_tbc > 0) {
1257 int n;
1258
1259 n = p->p_tbc;
1260 if (n > 8) /* write up to 8 entries */
1261 n = 8;
1262 DPRINTF(CDD_INTR, (", writing %d bytes to TDR", n));
1263 cd18xx_write_multi(sc, CD18xx_TDR, p->p_tba, n);
1264 p->p_tbc -= n;
1265 p->p_tba += n;
1266 }
1267
1268 /* Disable transmit completion interrupts if we ran out of bytes. */
1269 if (p->p_tbc == 0) {
1270 /* Note that Tx interrupts should already be enabled */
1271 if (ISSET(p->p_srer, CD18xx_SRER_Tx)) {
1272 DPRINTF(CDD_INTR, (", disabling tx interrupts"));
1273 CLR(p->p_srer, CD18xx_SRER_Tx);
1274 cd18xx_write(sc, CD18xx_SRER, p->p_srer);
1275 }
1276 if (p->p_tx_busy) {
1277 p->p_tx_busy = 0;
1278 p->p_tx_done = 1;
1279 }
1280 }
1281 *ns = 1;
1282
1283 /* finish the interrupt transaction with the IC */
1284 cd18xx_write(sc, CD18xx_EOSRR, 0);
1285 DPRINTF(CDD_INTR, (", done\n"));
1286 }
1287
1288 /* modem signal change interrupt */
1289 static inline void
1290 cd18xx_mint(sc, ns)
1291 struct cd18xx_softc *sc;
1292 int *ns;
1293 {
1294 struct cdtty_port *p;
1295 u_int channel;
1296 u_char msvr, delta;
1297
1298 /* work out the channel and softc */
1299 channel = cd18xx_get_gscr1_channel(sc);
1300 p = &sc->sc_ports[channel];
1301 DPRINTF(CDD_INTR, ("%s: mint: channel %d", sc->sc_dev.dv_xname, channel));
1302 GOTINTR(sc, p);
1303
1304 /*
1305 * We ignore the MCR register, and handle detecting deltas
1306 * via software, like many other serial drivers.
1307 */
1308 msvr = cd18xx_read(sc, CD18xx_MSVR);
1309 delta = msvr ^ p->p_msvr;
1310 DPRINTF(CDD_INTR, (", msvr %d", msvr));
1311
1312 /*
1313 * Process normal status changes
1314 */
1315 if (ISSET(delta, p->p_msvr_mask)) {
1316 SET(p->p_msvr_delta, delta);
1317
1318 DPRINTF(CDD_INTR, (", status changed delta %d", delta));
1319 /*
1320 * Stop output immediately if we lose the output
1321 * flow control signal or carrier detect.
1322 */
1323 if (ISSET(~msvr, p->p_msvr_mask)) {
1324 p->p_tbc = 0;
1325 p->p_heldtbc = 0;
1326 /* Stop modem interrupt processing */
1327 }
1328 p->p_st_check = 1;
1329 *ns = 1;
1330 }
1331
1332 /* reset the modem signal register */
1333 cd18xx_write(sc, CD18xx_MCR, 0);
1334
1335 /* finish the interrupt transaction with the IC */
1336 cd18xx_write(sc, CD18xx_EOSRR, 0);
1337 DPRINTF(CDD_INTR, (", done\n"));
1338 }
1339
1340 /*
1341 * hardware interrupt routine. call the relevant interrupt routines until
1342 * no interrupts are pending.
1343 *
1344 * note: we do receive interrupts before all others (as we'd rather lose
1345 * a chance to transmit, than lose a character). and we do transmit
1346 * interrupts before modem interrupts.
1347 *
1348 * we have to traverse all of the cd18xx's attached, unfortunately.
1349 */
1350 int
1351 cd18xx_hardintr(v)
1352 void *v;
1353 {
1354 int i, rv = 0;
1355 u_char ack;
1356
1357 DPRINTF(CDD_INTR, ("cd18xx_hardintr (ndevs %d):\n", clcd_cd.cd_ndevs));
1358 for (i = 0; i < clcd_cd.cd_ndevs; i++)
1359 {
1360 struct cd18xx_softc *sc = clcd_cd.cd_devs[i];
1361 int status, ns = 0;
1362 int count = 1; /* process only 1 interrupts at a time for now */
1363
1364 if (sc == NULL)
1365 continue;
1366
1367 DPRINTF(CDD_INTR, ("%s:", sc->sc_dev.dv_xname));
1368 while (count-- &&
1369 (status = (cd18xx_read(sc, CD18xx_SRSR) &
1370 CD18xx_SRSR_PENDING))) {
1371 rv = 1;
1372
1373 DPRINTF(CDD_INTR, (" status %x:", status));
1374 if (ISSET(status, CD18xx_SRSR_RxPEND)) {
1375 ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
1376 CD18xx_INTRACK_RxINT);
1377 DPRINTF(CDD_INTR, (" rx: ack1 %x\n", ack));
1378 cd18xx_rint(sc, &ns);
1379 }
1380 if (ISSET(status, CD18xx_SRSR_TxPEND)) {
1381 ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
1382 CD18xx_INTRACK_TxINT);
1383 DPRINTF(CDD_INTR, (" tx: ack1 %x\n", ack));
1384 cd18xx_tint(sc, &ns);
1385
1386 }
1387 if (ISSET(status, CD18xx_SRSR_MxPEND)) {
1388 ack = (*sc->sc_ackfunc)(sc->sc_ackfunc_arg,
1389 CD18xx_INTRACK_MxINT);
1390 DPRINTF(CDD_INTR, (" mx: ack1 %x\n", ack));
1391 cd18xx_mint(sc, &ns);
1392 }
1393 }
1394 if (ns)
1395 softintr_schedule(sc->sc_si);
1396 }
1397
1398 return (rv);
1399 }
1400
1401 /*
1402 * software interrupt
1403 */
1404
1405 void
1406 cdtty_rxsoft(sc, p, tp)
1407 struct cd18xx_softc *sc;
1408 struct cdtty_port *p;
1409 struct tty *tp;
1410 {
1411 u_char *get, *end;
1412 u_int cc, scc;
1413 u_char rcsr;
1414 int code;
1415 int s;
1416
1417 end = p->p_ebuf;
1418 get = p->p_rbget;
1419 scc = cc = cdtty_rbuf_size - p->p_rbavail;
1420
1421 if (cc == cdtty_rbuf_size) {
1422 p->p_floods++;
1423 #if 0
1424 if (p->p_errors++ == 0)
1425 callout_reset(&p->p_diag_callout, 60 * hz,
1426 cdttydiag, p);
1427 #endif
1428 }
1429
1430 while (cc) {
1431 code = get[0];
1432 rcsr = get[1];
1433 if (ISSET(rcsr, CD18xx_RCSR_OVERRUNERR | CD18xx_RCSR_BREAK |
1434 CD18xx_RCSR_FRAMERR | CD18xx_RCSR_PARITYERR)) {
1435 if (ISSET(rcsr, CD18xx_RCSR_OVERRUNERR)) {
1436 p->p_overflows++;
1437 #if 0
1438 if (p->p_errors++ == 0)
1439 callout_reset(&p->p_diag_callout,
1440 60 * hz, cdttydiag, p);
1441 #endif
1442 }
1443 if (ISSET(rcsr, CD18xx_RCSR_BREAK|CD18xx_RCSR_FRAMERR))
1444 SET(code, TTY_FE);
1445 if (ISSET(rcsr, CD18xx_RCSR_PARITYERR))
1446 SET(code, TTY_PE);
1447 }
1448 if ((*tp->t_linesw->l_rint)(code, tp) == -1) {
1449 /*
1450 * The line discipline's buffer is out of space.
1451 */
1452 if (!ISSET(p->p_rx_flags, RX_TTY_BLOCKED)) {
1453 /*
1454 * We're either not using flow control, or the
1455 * line discipline didn't tell us to block for
1456 * some reason. Either way, we have no way to
1457 * know when there's more space available, so
1458 * just drop the rest of the data.
1459 */
1460 get += cc << 1;
1461 if (get >= end)
1462 get -= cdtty_rbuf_size << 1;
1463 cc = 0;
1464 } else {
1465 /*
1466 * Don't schedule any more receive processing
1467 * until the line discipline tells us there's
1468 * space available (through cdttyhwiflow()).
1469 * Leave the rest of the data in the input
1470 * buffer.
1471 */
1472 SET(p->p_rx_flags, RX_TTY_OVERFLOWED);
1473 }
1474 break;
1475 }
1476 get += 2;
1477 if (get >= end)
1478 get = p->p_rbuf;
1479 cc--;
1480 }
1481
1482 if (cc != scc) {
1483 p->p_rbget = get;
1484 s = splserial();
1485
1486 cc = p->p_rbavail += scc - cc;
1487 /* Buffers should be ok again, release possible block. */
1488 if (cc >= p->p_r_lowat) {
1489 if (ISSET(p->p_rx_flags, RX_IBUF_OVERFLOWED)) {
1490 CLR(p->p_rx_flags, RX_IBUF_OVERFLOWED);
1491 cd18xx_set_car(sc, CD18XX_CHANNEL(tp->t_dev));
1492 SET(p->p_srer, CD18xx_SRER_Rx |
1493 CD18xx_SRER_RxSC |
1494 CD18xx_SRER_CD);
1495 cd18xx_write(sc, CD18xx_SRER, p->p_srer);
1496 }
1497 if (ISSET(p->p_rx_flags, RX_IBUF_BLOCKED)) {
1498 CLR(p->p_rx_flags, RX_IBUF_BLOCKED);
1499 cdtty_hwiflow(sc, p);
1500 }
1501 }
1502 splx(s);
1503 }
1504 }
1505
1506 void
1507 cdtty_txsoft(sc, p, tp)
1508 struct cd18xx_softc *sc;
1509 struct cdtty_port *p;
1510 struct tty *tp;
1511 {
1512
1513 CLR(tp->t_state, TS_BUSY);
1514 if (ISSET(tp->t_state, TS_FLUSH))
1515 CLR(tp->t_state, TS_FLUSH);
1516 else
1517 ndflush(&tp->t_outq, (int)(p->p_tba - tp->t_outq.c_cf));
1518 (*tp->t_linesw->l_start)(tp);
1519 }
1520
1521 void
1522 cdtty_stsoft(sc, p, tp)
1523 struct cd18xx_softc *sc;
1524 struct cdtty_port *p;
1525 struct tty *tp;
1526 {
1527 u_char msvr, delta;
1528 int s;
1529
1530 s = splserial();
1531 msvr = p->p_msvr;
1532 delta = p->p_msvr_delta;
1533 p->p_msvr_delta = 0;
1534 splx(s);
1535
1536 if (ISSET(delta, p->p_msvr_dcd)) {
1537 /*
1538 * Inform the tty layer that carrier detect changed.
1539 */
1540 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msvr, CD18xx_MSVR_CD));
1541 }
1542
1543 if (ISSET(delta, p->p_msvr_cts)) {
1544 /* Block or unblock output according to flow control. */
1545 if (ISSET(msvr, p->p_msvr_cts)) {
1546 p->p_tx_stopped = 0;
1547 (*tp->t_linesw->l_start)(tp);
1548 } else {
1549 p->p_tx_stopped = 1;
1550 }
1551 }
1552 }
1553
1554 void
1555 cd18xx_softintr(v)
1556 void *v;
1557 {
1558 struct cd18xx_softc *sc = v;
1559 struct cdtty_port *p;
1560 struct tty *tp;
1561 int i;
1562
1563 for (i = 0; i < 8; i++) {
1564 p = &sc->sc_ports[i];
1565
1566 tp = p->p_tty;
1567 if (tp == NULL)
1568 continue;
1569 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
1570 continue;
1571
1572 if (p->p_rx_ready) {
1573 p->p_rx_ready = 0;
1574 cdtty_rxsoft(sc, p, tp);
1575 }
1576
1577 if (p->p_st_check) {
1578 p->p_st_check = 0;
1579 cdtty_stsoft(sc, p, tp);
1580 }
1581
1582 if (p->p_tx_done) {
1583 p->p_tx_done = 0;
1584 cdtty_txsoft(sc, p, tp);
1585 }
1586 }
1587 }
1588