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