Home | History | Annotate | Line # | Download | only in ic
cy.c revision 1.8
      1  1.8   thorpej /*	$NetBSD: cy.c,v 1.8 1998/01/12 09:23:20 thorpej Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * cy.c
      5  1.1  christos  *
      6  1.1  christos  * Driver for Cyclades Cyclom-8/16/32 multiport serial cards
      7  1.1  christos  * (currently not tested with Cyclom-32 cards)
      8  1.1  christos  *
      9  1.1  christos  * Timo Rossi, 1996
     10  1.1  christos  *
     11  1.1  christos  * Supports both ISA and PCI Cyclom cards
     12  1.1  christos  *
     13  1.1  christos  * Uses CD1400 automatic CTS flow control, and
     14  1.1  christos  * if CY_HW_RTS is defined, uses CD1400 automatic input flow control.
     15  1.1  christos  * This requires a special cable that exchanges the RTS and DTR lines.
     16  1.1  christos  *
     17  1.1  christos  * Lots of debug output can be enabled by defining CY_DEBUG
     18  1.1  christos  * Some debugging counters (number of receive/transmit interrupts etc.)
     19  1.1  christos  * can be enabled by defining CY_DEBUG1
     20  1.1  christos  */
     21  1.1  christos 
     22  1.1  christos #include <sys/types.h>
     23  1.1  christos #include <sys/param.h>
     24  1.1  christos #include <sys/ioctl.h>
     25  1.1  christos #include <sys/syslog.h>
     26  1.1  christos #include <sys/fcntl.h>
     27  1.1  christos #include <sys/tty.h>
     28  1.1  christos #include <sys/proc.h>
     29  1.1  christos #include <sys/conf.h>
     30  1.1  christos #include <sys/user.h>
     31  1.1  christos #include <sys/ioctl.h>
     32  1.1  christos #include <sys/select.h>
     33  1.1  christos #include <sys/device.h>
     34  1.1  christos #include <sys/malloc.h>
     35  1.1  christos #include <sys/systm.h>
     36  1.5   thorpej 
     37  1.1  christos #include <machine/bus.h>
     38  1.1  christos 
     39  1.1  christos #include <dev/ic/cd1400reg.h>
     40  1.1  christos #include <dev/ic/cyreg.h>
     41  1.1  christos #include <dev/ic/cyvar.h>
     42  1.1  christos 
     43  1.1  christos /* Macros to clear/set/test flags. */
     44  1.1  christos #define	SET(t, f)	(t) |= (f)
     45  1.1  christos #define	CLR(t, f)	(t) &= ~(f)
     46  1.1  christos #define	ISSET(t, f)	((t) & (f))
     47  1.1  christos 
     48  1.1  christos static int cyparam __P((struct tty *, struct termios *));
     49  1.1  christos static void cystart __P((struct tty *));
     50  1.1  christos static void cy_poll __P((void *));
     51  1.1  christos static int cy_modem_control __P((struct cy_softc *,
     52  1.1  christos     struct cy_port *, int, int));
     53  1.1  christos static void cy_enable_transmitter __P((struct cy_softc *, struct cy_port *));
     54  1.1  christos static void cd1400_channel_cmd __P((struct cy_softc *, struct cy_port *, int));
     55  1.1  christos static int cy_speed __P((speed_t, int *, int *));
     56  1.1  christos 
     57  1.8   thorpej extern struct cfdriver cy_cd;
     58  1.1  christos 
     59  1.1  christos static int      cy_open = 0;
     60  1.1  christos static int      cy_events = 0;
     61  1.1  christos 
     62  1.3  christos cdev_decl(cy);
     63  1.3  christos 
     64  1.1  christos /*
     65  1.1  christos  * Common probe routine
     66  1.1  christos  */
     67  1.1  christos int
     68  1.1  christos cy_find(sc)
     69  1.1  christos 	struct cy_softc *sc;
     70  1.1  christos {
     71  1.1  christos 	int cy_chip, chip;
     72  1.1  christos 	u_char firmware_ver;
     73  1.5   thorpej 	bus_space_tag_t tag = sc->sc_memt;
     74  1.5   thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
     75  1.1  christos 	int bustype = sc->sc_bustype;
     76  1.1  christos 
     77  1.1  christos 	/* Cyclom card hardware reset */
     78  1.5   thorpej 	bus_space_write_1(tag, bsh, CY16_RESET << bustype, 0);
     79  1.1  christos 	DELAY(500);		/* wait for reset to complete */
     80  1.5   thorpej 	bus_space_write_1(tag, bsh, CY_CLEAR_INTR << bustype, 0);
     81  1.1  christos 
     82  1.1  christos #ifdef CY_DEBUG
     83  1.4  christos 	printf("cy: card reset done\n");
     84  1.1  christos #endif
     85  1.1  christos 	sc->sc_nchips = 0;
     86  1.1  christos 
     87  1.1  christos 	for (cy_chip = 0, chip = 0; cy_chip < CY_MAX_CD1400s;
     88  1.1  christos 	    cy_chip++, chip += (CY_CD1400_MEMSPACING << bustype)) {
     89  1.1  christos 		int i;
     90  1.1  christos 
     91  1.1  christos 		/*
     92  1.1  christos 		 * the last 4 nchips are 'interleaved' with the first 4 on
     93  1.1  christos 		 * 32-port boards
     94  1.1  christos 		 */
     95  1.1  christos 		if (cy_chip == 4)
     96  1.1  christos 			chip -= (CY32_ADDR_FIX << bustype);
     97  1.1  christos 
     98  1.1  christos #ifdef CY_DEBUG
     99  1.7       cgd 		printf("%s probe chip %d offset 0x%x ... ",
    100  1.1  christos 		    sc->sc_dev.dv_xname, cy_chip, chip);
    101  1.1  christos #endif
    102  1.1  christos 
    103  1.1  christos 		/* wait until the chip is ready for command */
    104  1.1  christos 		DELAY(1000);
    105  1.5   thorpej 		if (bus_space_read_1(tag, bsh, chip +
    106  1.1  christos 		    ((CD1400_CCR << 1) << bustype)) != 0) {
    107  1.1  christos #ifdef CY_DEBUG
    108  1.4  christos 			printf("not ready for command\n");
    109  1.1  christos #endif
    110  1.1  christos 			break;
    111  1.1  christos 		}
    112  1.1  christos 		/* clear the firmware version reg. */
    113  1.5   thorpej 		bus_space_write_1(tag, bsh, chip +
    114  1.1  christos 		    ((CD1400_GFRCR << 1) << bustype), 0);
    115  1.1  christos 
    116  1.1  christos 		/*
    117  1.1  christos 	         * On Cyclom-16 references to non-existent chip 4
    118  1.1  christos 	         * actually access chip 0 (address line 9 not decoded).
    119  1.1  christos 	         * Here we check if the clearing of chip 4 GFRCR actually
    120  1.1  christos 	         * cleared chip 0 GFRCR. In that case we have a 16 port card.
    121  1.1  christos 	         */
    122  1.1  christos 		if (cy_chip == 4 &&
    123  1.7       cgd 		    bus_space_read_1(tag, bsh, /* off for chip 0 (0) + */
    124  1.1  christos 		       ((CD1400_GFRCR << 1) << bustype)) == 0)
    125  1.1  christos 			break;
    126  1.1  christos 
    127  1.1  christos 		/* reset the chip */
    128  1.5   thorpej 		bus_space_write_1(tag, bsh, chip +
    129  1.1  christos 		    ((CD1400_CCR << 1) << bustype),
    130  1.1  christos 		    CD1400_CCR_CMDRESET | CD1400_CCR_FULLRESET);
    131  1.1  christos 
    132  1.1  christos 		/* wait for the chip to initialize itself */
    133  1.1  christos 		for (i = 0; i < 200; i++) {
    134  1.1  christos 			DELAY(50);
    135  1.5   thorpej 			firmware_ver = bus_space_read_1(tag, bsh, chip +
    136  1.1  christos 			    ((CD1400_GFRCR << 1) << bustype));
    137  1.1  christos 			if ((firmware_ver & 0xf0) == 0x40) /* found a CD1400 */
    138  1.1  christos 				break;
    139  1.1  christos 		}
    140  1.1  christos #ifdef CY_DEBUG
    141  1.4  christos 		printf("firmware version 0x%x\n", firmware_ver);
    142  1.1  christos #endif
    143  1.1  christos 
    144  1.1  christos 		if ((firmware_ver & 0xf0) != 0x40)
    145  1.1  christos 			break;
    146  1.1  christos 
    147  1.1  christos 		/* firmware version OK, CD1400 found */
    148  1.1  christos 		sc->sc_nchips++;
    149  1.1  christos 	}
    150  1.1  christos 
    151  1.1  christos 	if (sc->sc_nchips == 0) {
    152  1.1  christos #ifdef CY_DEBUG
    153  1.4  christos 		printf("no CD1400s found\n");
    154  1.1  christos #endif
    155  1.1  christos 		return 0;
    156  1.1  christos 	}
    157  1.1  christos #ifdef CY_DEBUG
    158  1.4  christos 	printf("found %d CD1400s\n", sc->sc_nchips);
    159  1.1  christos #endif
    160  1.1  christos 
    161  1.1  christos 	return 1;
    162  1.1  christos }
    163  1.1  christos 
    164  1.1  christos void
    165  1.1  christos cy_attach(parent, self, aux)
    166  1.1  christos 	struct device  *parent, *self;
    167  1.1  christos 	void *aux;
    168  1.1  christos {
    169  1.3  christos 	int  port, cy_chip, num_chips, cdu, chip;
    170  1.1  christos 	struct cy_softc *sc = (void *) self;
    171  1.1  christos 
    172  1.1  christos 	num_chips = sc->sc_nchips;
    173  1.1  christos 	if (num_chips == 0)
    174  1.1  christos 		return;
    175  1.1  christos 
    176  1.1  christos 	bzero(sc->sc_ports, sizeof(sc->sc_ports));
    177  1.1  christos 
    178  1.1  christos 	port = 0;
    179  1.1  christos 	for (cy_chip = 0, chip = 0; cy_chip < num_chips; cy_chip++,
    180  1.1  christos 	    chip += (CY_CD1400_MEMSPACING << sc->sc_bustype)) {
    181  1.1  christos 
    182  1.1  christos 		if (cy_chip == 4)
    183  1.1  christos 			chip -= (CY32_ADDR_FIX << sc->sc_bustype);
    184  1.1  christos 
    185  1.1  christos #ifdef CY_DEBUG
    186  1.4  christos 		printf("attach CD1400 #%d offset 0x%x\n", cy_chip, chip);
    187  1.1  christos #endif
    188  1.1  christos 		sc->sc_cd1400_offs[cy_chip] = chip;
    189  1.1  christos 
    190  1.1  christos 		/*
    191  1.1  christos 		 * configure port 0 as serial port (should already be after
    192  1.1  christos 		 * reset)
    193  1.1  christos 		 */
    194  1.1  christos 		cd_write_reg(sc, cy_chip, CD1400_GCR, 0);
    195  1.1  christos 
    196  1.1  christos 		/* set up a receive timeout period (1ms) */
    197  1.1  christos 		cd_write_reg(sc, cy_chip, CD1400_PPR,
    198  1.1  christos 		    (CY_CLOCK / CD1400_PPR_PRESCALER / 1000) + 1);
    199  1.1  christos 
    200  1.1  christos 		for (cdu = 0; cdu < CD1400_NO_OF_CHANNELS; cdu++) {
    201  1.3  christos 			sc->sc_ports[port].cy_port_num = port;
    202  1.7       cgd 			sc->sc_ports[port].cy_chip = cy_chip;
    203  1.1  christos 
    204  1.1  christos 			/* should we initialize anything else here? */
    205  1.1  christos 			port++;
    206  1.1  christos 		} /* for(each port on one CD1400...) */
    207  1.1  christos 
    208  1.1  christos 	} /* for(each CD1400 on a card... ) */
    209  1.1  christos 
    210  1.6       cgd 	printf(": %d ports\n", port);
    211  1.1  christos 
    212  1.1  christos 	/* ensure an edge for the next interrupt */
    213  1.5   thorpej 	bus_space_write_1(sc->sc_memt, sc->sc_bsh,
    214  1.1  christos 	    CY_CLEAR_INTR << sc->sc_bustype, 0);
    215  1.1  christos }
    216  1.1  christos 
    217  1.1  christos /*
    218  1.1  christos  * open routine. returns zero if successfull, else error code
    219  1.1  christos  */
    220  1.1  christos int
    221  1.1  christos cyopen(dev, flag, mode, p)
    222  1.1  christos 	dev_t dev;
    223  1.1  christos 	int flag, mode;
    224  1.1  christos 	struct proc *p;
    225  1.1  christos {
    226  1.1  christos 	int card = CY_CARD(dev);
    227  1.1  christos 	int port = CY_PORT(dev);
    228  1.1  christos 	struct cy_softc *sc;
    229  1.1  christos 	struct cy_port *cy;
    230  1.1  christos 	struct tty *tp;
    231  1.1  christos 	int s, error;
    232  1.1  christos 
    233  1.1  christos #ifdef CY_DEBUG
    234  1.4  christos 	printf("cy%d open port %d flag 0x%x mode 0x%x\n",
    235  1.1  christos 	    card, port, flag, mode);
    236  1.1  christos #endif
    237  1.1  christos 
    238  1.1  christos 	if (card >= cy_cd.cd_ndevs || (sc = cy_cd.cd_devs[card]) == NULL)
    239  1.1  christos 		return ENXIO;
    240  1.1  christos 
    241  1.1  christos 	cy = &sc->sc_ports[port];
    242  1.1  christos 
    243  1.1  christos 	s = spltty();
    244  1.1  christos 	if (cy->cy_tty == NULL) {
    245  1.1  christos 		if ((cy->cy_tty = ttymalloc()) == NULL) {
    246  1.1  christos 			splx(s);
    247  1.4  christos 			printf("cy%d: port %d: can't allocate tty\n",
    248  1.1  christos 			    card, port);
    249  1.1  christos 			return ENOMEM;
    250  1.1  christos 		}
    251  1.3  christos 		tty_attach(cy->cy_tty);
    252  1.1  christos 	}
    253  1.1  christos 	splx(s);
    254  1.1  christos 
    255  1.1  christos 	tp = cy->cy_tty;
    256  1.1  christos 	tp->t_oproc = cystart;
    257  1.1  christos 	tp->t_param = cyparam;
    258  1.1  christos 	tp->t_dev = dev;
    259  1.1  christos 
    260  1.1  christos 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    261  1.1  christos 		SET(tp->t_state, TS_WOPEN);
    262  1.1  christos 		ttychars(tp);
    263  1.1  christos 		tp->t_iflag = TTYDEF_IFLAG;
    264  1.1  christos 		tp->t_oflag = TTYDEF_OFLAG;
    265  1.1  christos 		tp->t_cflag = TTYDEF_CFLAG;
    266  1.1  christos 		if (ISSET(cy->cy_openflags, TIOCFLAG_CLOCAL))
    267  1.1  christos 			SET(tp->t_cflag, CLOCAL);
    268  1.1  christos 		if (ISSET(cy->cy_openflags, TIOCFLAG_CRTSCTS))
    269  1.1  christos 			SET(tp->t_cflag, CRTSCTS);
    270  1.1  christos 		if (ISSET(cy->cy_openflags, TIOCFLAG_MDMBUF))
    271  1.1  christos 			SET(tp->t_cflag, MDMBUF);
    272  1.1  christos 		tp->t_lflag = TTYDEF_LFLAG;
    273  1.1  christos 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    274  1.1  christos 
    275  1.1  christos 		s = spltty();
    276  1.1  christos 
    277  1.1  christos 		/*
    278  1.1  christos 		 * Allocate input ring buffer if we don't already have one
    279  1.1  christos 		 */
    280  1.1  christos 		if (cy->cy_ibuf == NULL) {
    281  1.1  christos 			cy->cy_ibuf = malloc(CY_IBUF_SIZE, M_DEVBUF, M_NOWAIT);
    282  1.1  christos 			if (cy->cy_ibuf == NULL) {
    283  1.4  christos 				printf("%s: port %d: can't allocate input buffer\n",
    284  1.1  christos 				       sc->sc_dev.dv_xname, port);
    285  1.1  christos 				splx(s);
    286  1.1  christos 				return ENOMEM;
    287  1.1  christos 			}
    288  1.1  christos 			cy->cy_ibuf_end = cy->cy_ibuf + CY_IBUF_SIZE;
    289  1.1  christos 		}
    290  1.1  christos 		/* mark the ring buffer as empty */
    291  1.1  christos 		cy->cy_ibuf_rd_ptr = cy->cy_ibuf_wr_ptr = cy->cy_ibuf;
    292  1.1  christos 
    293  1.1  christos 		/* select CD1400 channel */
    294  1.1  christos 		cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
    295  1.1  christos 		    port & CD1400_CAR_CHAN);
    296  1.1  christos 		/* reset the channel */
    297  1.1  christos 		cd1400_channel_cmd(sc, cy, CD1400_CCR_CMDRESET);
    298  1.1  christos 		/* encode unit (port) number in LIVR */
    299  1.1  christos 		/* there is just enough space for 5 bits (32 ports) */
    300  1.1  christos 		cd_write_reg(sc, cy->cy_chip, CD1400_LIVR, port << 3);
    301  1.1  christos 
    302  1.1  christos 		cy->cy_channel_control = 0;
    303  1.1  christos 
    304  1.1  christos 		/* hmm... need spltty() here? */
    305  1.1  christos 		if (cy_open == 0) {
    306  1.1  christos 			cy_open = 1;
    307  1.1  christos 			timeout(cy_poll, NULL, 1);
    308  1.1  christos 		}
    309  1.1  christos 		/* this sets parameters and raises DTR */
    310  1.1  christos 		cyparam(tp, &tp->t_termios);
    311  1.1  christos 
    312  1.1  christos 		ttsetwater(tp);
    313  1.1  christos 
    314  1.1  christos 		/* raise RTS too */
    315  1.1  christos 		cy_modem_control(sc, cy, TIOCM_RTS, DMBIS);
    316  1.1  christos 
    317  1.1  christos 		cy->cy_carrier_stat =
    318  1.1  christos 			cd_read_reg(sc, cy->cy_chip, CD1400_MSVR2);
    319  1.1  christos 
    320  1.1  christos 		/* enable receiver and modem change interrupts */
    321  1.1  christos 		cd_write_reg(sc, cy->cy_chip, CD1400_SRER,
    322  1.1  christos 		    CD1400_SRER_MDMCH | CD1400_SRER_RXDATA);
    323  1.1  christos 
    324  1.1  christos 		if (CY_DIALOUT(dev) ||
    325  1.1  christos 		    ISSET(cy->cy_openflags, TIOCFLAG_SOFTCAR) ||
    326  1.1  christos 		    ISSET(tp->t_cflag, MDMBUF) ||
    327  1.1  christos 		    ISSET(cy->cy_carrier_stat, CD1400_MSVR2_CD))
    328  1.1  christos 			SET(tp->t_state, TS_CARR_ON);
    329  1.1  christos 		else
    330  1.1  christos 			CLR(tp->t_state, TS_CARR_ON);
    331  1.1  christos 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0) {
    332  1.1  christos 		return EBUSY;
    333  1.1  christos 	} else {
    334  1.1  christos 		s = spltty();
    335  1.1  christos 	}
    336  1.1  christos 
    337  1.1  christos 	/* wait for carrier if necessary */
    338  1.1  christos 	if (!ISSET(flag, O_NONBLOCK)) {
    339  1.1  christos 		while (!ISSET(tp->t_cflag, CLOCAL) &&
    340  1.1  christos 		    !ISSET(tp->t_state, TS_CARR_ON)) {
    341  1.1  christos 			SET(tp->t_state, TS_WOPEN);
    342  1.1  christos 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    343  1.1  christos 			    "cydcd", 0);
    344  1.1  christos 			if (error != 0) {
    345  1.1  christos 				splx(s);
    346  1.1  christos 				return error;
    347  1.1  christos 			}
    348  1.1  christos 		}
    349  1.1  christos 	}
    350  1.1  christos 	splx(s);
    351  1.1  christos 
    352  1.1  christos 	return (*linesw[tp->t_line].l_open) (dev, tp);
    353  1.1  christos }
    354  1.1  christos 
    355  1.1  christos /*
    356  1.1  christos  * close routine. returns zero if successfull, else error code
    357  1.1  christos  */
    358  1.1  christos int
    359  1.1  christos cyclose(dev, flag, mode, p)
    360  1.1  christos 	dev_t dev;
    361  1.1  christos 	int flag, mode;
    362  1.1  christos 	struct proc *p;
    363  1.1  christos {
    364  1.1  christos 	int card = CY_CARD(dev);
    365  1.1  christos 	int port = CY_PORT(dev);
    366  1.1  christos 	struct cy_softc *sc = cy_cd.cd_devs[card];
    367  1.1  christos 	struct cy_port *cy = &sc->sc_ports[port];
    368  1.1  christos 	struct tty *tp = cy->cy_tty;
    369  1.1  christos 	int s;
    370  1.1  christos 
    371  1.1  christos #ifdef CY_DEBUG
    372  1.4  christos 	printf("%s: close port %d, flag 0x%x, mode 0x%x\n",
    373  1.1  christos 	    sc->sc_dev.dv_xname, port, flag, mode);
    374  1.1  christos #endif
    375  1.1  christos 
    376  1.1  christos 	(*linesw[tp->t_line].l_close) (tp, flag);
    377  1.1  christos 	s = spltty();
    378  1.1  christos 
    379  1.1  christos 	if (ISSET(tp->t_cflag, HUPCL) &&
    380  1.1  christos 	    !ISSET(cy->cy_openflags, TIOCFLAG_SOFTCAR)) {
    381  1.1  christos 		/*
    382  1.1  christos 		 * drop DTR and RTS (should we wait for output buffer to
    383  1.1  christos 		 * become empty first?)
    384  1.1  christos 		 */
    385  1.1  christos 		cy_modem_control(sc, cy, 0, DMSET);
    386  1.1  christos 	}
    387  1.1  christos 	/*
    388  1.1  christos 	 * XXX should we disable modem change and
    389  1.1  christos 	 * receive interrupts here or somewhere ?
    390  1.1  christos 	 */
    391  1.1  christos 	CLR(tp->t_state, TS_BUSY | TS_FLUSH);
    392  1.1  christos 
    393  1.1  christos 	splx(s);
    394  1.1  christos 	ttyclose(tp);
    395  1.1  christos 
    396  1.1  christos 	return 0;
    397  1.1  christos }
    398  1.1  christos 
    399  1.1  christos /*
    400  1.1  christos  * Read routine
    401  1.1  christos  */
    402  1.1  christos int
    403  1.1  christos cyread(dev, uio, flag)
    404  1.1  christos 	dev_t dev;
    405  1.1  christos 	struct uio *uio;
    406  1.1  christos 	int flag;
    407  1.1  christos {
    408  1.1  christos 	int card = CY_CARD(dev);
    409  1.1  christos 	int port = CY_PORT(dev);
    410  1.1  christos 	struct cy_softc *sc = cy_cd.cd_devs[card];
    411  1.1  christos 	struct cy_port *cy = &sc->sc_ports[port];
    412  1.1  christos 	struct tty *tp = cy->cy_tty;
    413  1.1  christos 
    414  1.1  christos #ifdef CY_DEBUG
    415  1.4  christos 	printf("%s: read port %d uio 0x%x flag 0x%x\n",
    416  1.1  christos 	    sc->sc_dev.dv_xname, port, uio, flag);
    417  1.1  christos #endif
    418  1.1  christos 
    419  1.1  christos 	return ((*linesw[tp->t_line].l_read) (tp, uio, flag));
    420  1.1  christos }
    421  1.1  christos 
    422  1.1  christos /*
    423  1.1  christos  * Write routine
    424  1.1  christos  */
    425  1.1  christos int
    426  1.1  christos cywrite(dev, uio, flag)
    427  1.1  christos 	dev_t dev;
    428  1.1  christos 	struct uio *uio;
    429  1.1  christos 	int flag;
    430  1.1  christos {
    431  1.1  christos 	int card = CY_CARD(dev);
    432  1.1  christos 	int port = CY_PORT(dev);
    433  1.1  christos 	struct cy_softc *sc = cy_cd.cd_devs[card];
    434  1.1  christos 	struct cy_port *cy = &sc->sc_ports[port];
    435  1.1  christos 	struct tty *tp = cy->cy_tty;
    436  1.1  christos 
    437  1.1  christos #ifdef CY_DEBUG
    438  1.4  christos 	printf("%s: write port %d uio 0x%x flag 0x%x\n",
    439  1.1  christos 	    sc->sc_dev.dv_xname, port, uio, flag);
    440  1.1  christos #endif
    441  1.1  christos 
    442  1.1  christos 	return ((*linesw[tp->t_line].l_write) (tp, uio, flag));
    443  1.1  christos }
    444  1.1  christos 
    445  1.1  christos /*
    446  1.1  christos  * return tty pointer
    447  1.1  christos  */
    448  1.1  christos struct tty *
    449  1.1  christos cytty(dev)
    450  1.1  christos 	dev_t dev;
    451  1.1  christos {
    452  1.1  christos 	int card = CY_CARD(dev);
    453  1.1  christos 	int port = CY_PORT(dev);
    454  1.1  christos 	struct cy_softc *sc = cy_cd.cd_devs[card];
    455  1.1  christos 	struct cy_port *cy = &sc->sc_ports[port];
    456  1.1  christos 	struct tty *tp = cy->cy_tty;
    457  1.1  christos 
    458  1.1  christos #ifdef CY_DEBUG
    459  1.4  christos 	printf("%s: tty port %d tp 0x%x\n", sc->sc_dev.dv_xname, port, tp);
    460  1.1  christos #endif
    461  1.1  christos 	return tp;
    462  1.1  christos }
    463  1.1  christos 
    464  1.1  christos /*
    465  1.1  christos  * ioctl routine
    466  1.1  christos  */
    467  1.1  christos int
    468  1.1  christos cyioctl(dev, cmd, data, flag, p)
    469  1.1  christos 	dev_t dev;
    470  1.1  christos 	u_long cmd;
    471  1.1  christos 	caddr_t data;
    472  1.1  christos 	int flag;
    473  1.1  christos 	struct proc *p;
    474  1.1  christos {
    475  1.1  christos 	int card = CY_CARD(dev);
    476  1.1  christos 	int port = CY_PORT(dev);
    477  1.1  christos 	struct cy_softc *sc = cy_cd.cd_devs[card];
    478  1.1  christos 	struct cy_port *cy = &sc->sc_ports[port];
    479  1.1  christos 	struct tty *tp = cy->cy_tty;
    480  1.1  christos 	int error;
    481  1.1  christos 
    482  1.1  christos #ifdef CY_DEBUG
    483  1.4  christos 	printf("%s: port %d ioctl cmd 0x%x data 0x%x flag 0x%x\n",
    484  1.1  christos 	    sc->sc_dev.dv_xname, port, cmd, data, flag);
    485  1.1  christos #endif
    486  1.1  christos 
    487  1.1  christos 	error = (*linesw[tp->t_line].l_ioctl) (tp, cmd, data, flag, p);
    488  1.1  christos 	if (error >= 0)
    489  1.1  christos 		return error;
    490  1.1  christos 
    491  1.1  christos 	error = ttioctl(tp, cmd, data, flag, p);
    492  1.1  christos 	if (error >= 0)
    493  1.1  christos 		return error;
    494  1.1  christos 
    495  1.1  christos 	/* XXX should not allow dropping DTR when dialin? */
    496  1.1  christos 
    497  1.1  christos 	switch (cmd) {
    498  1.1  christos 	case TIOCSBRK:		/* start break */
    499  1.1  christos 		SET(cy->cy_flags, CY_F_START_BREAK);
    500  1.1  christos 		cy_enable_transmitter(sc, cy);
    501  1.1  christos 		break;
    502  1.1  christos 
    503  1.1  christos 	case TIOCCBRK:		/* stop break */
    504  1.1  christos 		SET(cy->cy_flags, CY_F_END_BREAK);
    505  1.1  christos 		cy_enable_transmitter(sc, cy);
    506  1.1  christos 		break;
    507  1.1  christos 
    508  1.1  christos 	case TIOCSDTR:		/* DTR on */
    509  1.1  christos 		cy_modem_control(sc, cy, TIOCM_DTR, DMBIS);
    510  1.1  christos 		break;
    511  1.1  christos 
    512  1.1  christos 	case TIOCCDTR:		/* DTR off */
    513  1.1  christos 		cy_modem_control(sc, cy, TIOCM_DTR, DMBIC);
    514  1.1  christos 		break;
    515  1.1  christos 
    516  1.1  christos 	case TIOCMSET:		/* set new modem control line values */
    517  1.1  christos 		cy_modem_control(sc, cy, *((int *) data), DMSET);
    518  1.1  christos 		break;
    519  1.1  christos 
    520  1.1  christos 	case TIOCMBIS:		/* turn modem control bits on */
    521  1.1  christos 		cy_modem_control(sc, cy, *((int *) data), DMBIS);
    522  1.1  christos 		break;
    523  1.1  christos 
    524  1.1  christos 	case TIOCMBIC:		/* turn modem control bits off */
    525  1.1  christos 		cy_modem_control(sc, cy, *((int *) data), DMBIC);
    526  1.1  christos 		break;
    527  1.1  christos 
    528  1.1  christos 	case TIOCMGET:		/* get modem control/status line state */
    529  1.1  christos 		*((int *) data) = cy_modem_control(sc, cy, 0, DMGET);
    530  1.1  christos 		break;
    531  1.1  christos 
    532  1.1  christos 	case TIOCGFLAGS:
    533  1.1  christos 		*((int *) data) = cy->cy_openflags |
    534  1.1  christos 			(CY_DIALOUT(dev) ? TIOCFLAG_SOFTCAR : 0);
    535  1.1  christos 		break;
    536  1.1  christos 
    537  1.1  christos 	case TIOCSFLAGS:
    538  1.1  christos 		error = suser(p->p_ucred, &p->p_acflag);
    539  1.1  christos 		if (error != 0)
    540  1.1  christos 			return EPERM;
    541  1.1  christos 
    542  1.1  christos 		cy->cy_openflags = *((int *) data) &
    543  1.1  christos 		    (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
    544  1.1  christos 		     TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
    545  1.1  christos 		break;
    546  1.1  christos 
    547  1.1  christos 	default:
    548  1.1  christos 		return ENOTTY;
    549  1.1  christos 	}
    550  1.1  christos 
    551  1.1  christos 	return 0;
    552  1.1  christos }
    553  1.1  christos 
    554  1.1  christos /*
    555  1.1  christos  * start output
    556  1.1  christos  */
    557  1.1  christos void
    558  1.1  christos cystart(tp)
    559  1.1  christos 	struct tty *tp;
    560  1.1  christos {
    561  1.1  christos 	int card = CY_CARD(tp->t_dev);
    562  1.1  christos 	int port = CY_PORT(tp->t_dev);
    563  1.1  christos 	struct cy_softc *sc = cy_cd.cd_devs[card];
    564  1.1  christos 	struct cy_port *cy = &sc->sc_ports[port];
    565  1.1  christos 	int s;
    566  1.1  christos 
    567  1.1  christos #ifdef CY_DEBUG
    568  1.4  christos 	printf("%s: port %d start, tty 0x%x\n", sc->sc_dev.dv_xname, port, tp);
    569  1.1  christos #endif
    570  1.1  christos 
    571  1.1  christos 
    572  1.1  christos 	s = spltty();
    573  1.1  christos 
    574  1.1  christos #ifdef CY_DEBUG1
    575  1.1  christos 	cy->cy_start_count++;
    576  1.1  christos #endif
    577  1.1  christos 
    578  1.1  christos 	if (!ISSET(tp->t_state, TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) {
    579  1.1  christos 		if (tp->t_outq.c_cc <= tp->t_lowat) {
    580  1.1  christos 			if (ISSET(tp->t_state, TS_ASLEEP)) {
    581  1.1  christos 				CLR(tp->t_state, TS_ASLEEP);
    582  1.1  christos 				wakeup(&tp->t_outq);
    583  1.1  christos 			}
    584  1.1  christos 			selwakeup(&tp->t_wsel);
    585  1.1  christos 
    586  1.1  christos 			if (tp->t_outq.c_cc == 0)
    587  1.1  christos 				goto out;
    588  1.1  christos 		}
    589  1.1  christos 		SET(tp->t_state, TS_BUSY);
    590  1.1  christos 		cy_enable_transmitter(sc, cy);
    591  1.1  christos 	}
    592  1.1  christos out:
    593  1.1  christos 
    594  1.1  christos 	splx(s);
    595  1.1  christos }
    596  1.1  christos 
    597  1.1  christos /*
    598  1.1  christos  * stop output
    599  1.1  christos  */
    600  1.1  christos void
    601  1.1  christos cystop(tp, flag)
    602  1.1  christos 	struct tty *tp;
    603  1.1  christos 	int flag;
    604  1.1  christos {
    605  1.1  christos 	int card = CY_CARD(tp->t_dev);
    606  1.1  christos 	int port = CY_PORT(tp->t_dev);
    607  1.1  christos 	struct cy_softc *sc = cy_cd.cd_devs[card];
    608  1.1  christos 	struct cy_port *cy = &sc->sc_ports[port];
    609  1.1  christos 	int s;
    610  1.1  christos 
    611  1.1  christos #ifdef CY_DEBUG
    612  1.4  christos 	printf("%s: port %d stop tty 0x%x flag 0x%x\n",
    613  1.1  christos 	    sc->sc_dev.dv_xname, port, tp, flag);
    614  1.1  christos #endif
    615  1.1  christos 
    616  1.1  christos 	s = spltty();
    617  1.1  christos 
    618  1.1  christos 	if (ISSET(tp->t_state, TS_BUSY)) {
    619  1.1  christos 		if (!ISSET(tp->t_state, TS_TTSTOP))
    620  1.1  christos 			SET(tp->t_state, TS_FLUSH);
    621  1.1  christos 
    622  1.1  christos 		/*
    623  1.1  christos 		 * the transmit interrupt routine will disable transmit when it
    624  1.1  christos 		 * notices that CY_F_STOP has been set.
    625  1.1  christos 		 */
    626  1.1  christos 		SET(cy->cy_flags, CY_F_STOP);
    627  1.1  christos 	}
    628  1.1  christos 	splx(s);
    629  1.1  christos }
    630  1.1  christos 
    631  1.1  christos /*
    632  1.1  christos  * parameter setting routine.
    633  1.1  christos  * returns 0 if successfull, else returns error code
    634  1.1  christos  */
    635  1.1  christos static int
    636  1.1  christos cyparam(tp, t)
    637  1.1  christos 	struct tty *tp;
    638  1.1  christos 	struct termios *t;
    639  1.1  christos {
    640  1.1  christos 	int card = CY_CARD(tp->t_dev);
    641  1.1  christos 	int port = CY_PORT(tp->t_dev);
    642  1.1  christos 	struct cy_softc *sc = cy_cd.cd_devs[card];
    643  1.1  christos 	struct cy_port *cy = &sc->sc_ports[port];
    644  1.1  christos 	int ibpr, obpr, i_clk_opt, o_clk_opt;
    645  1.1  christos 	int s, opt;
    646  1.1  christos 
    647  1.1  christos #ifdef CY_DEBUG
    648  1.4  christos 	printf("%s: port %d param tty 0x%x termios 0x%x\n",
    649  1.1  christos 	    sc->sc_dev.dv_xname, port, tp, t);
    650  1.4  christos 	printf("ispeed %d ospeed %d\n", t->c_ispeed, t->c_ospeed);
    651  1.1  christos #endif
    652  1.1  christos 
    653  1.1  christos 	if (t->c_ospeed != 0 && cy_speed(t->c_ospeed, &o_clk_opt, &obpr) < 0)
    654  1.1  christos 		return EINVAL;
    655  1.1  christos 
    656  1.1  christos 	if (t->c_ispeed != 0 && cy_speed(t->c_ispeed, &i_clk_opt, &ibpr) < 0)
    657  1.1  christos 		return EINVAL;
    658  1.1  christos 
    659  1.1  christos 	s = spltty();
    660  1.1  christos 
    661  1.1  christos 	/* hang up the line is ospeed is zero, else turn DTR on */
    662  1.1  christos 	cy_modem_control(sc, cy, TIOCM_DTR, (t->c_ospeed == 0 ? DMBIC : DMBIS));
    663  1.1  christos 
    664  1.1  christos 	/* channel was selected by the above call to cy_modem_control() */
    665  1.1  christos #if 0
    666  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_CAR, port & CD1400_CAR_CHAN);
    667  1.1  christos #endif
    668  1.1  christos 
    669  1.1  christos 	/* set transmit speed */
    670  1.1  christos 	if (t->c_ospeed != 0) {
    671  1.1  christos 		cd_write_reg(sc, cy->cy_chip, CD1400_TCOR, o_clk_opt);
    672  1.1  christos 		cd_write_reg(sc, cy->cy_chip, CD1400_TBPR, obpr);
    673  1.1  christos 	}
    674  1.1  christos 	/* set receive speed */
    675  1.1  christos 	if (t->c_ispeed != 0) {
    676  1.1  christos 		cd_write_reg(sc, cy->cy_chip, CD1400_RCOR, i_clk_opt);
    677  1.1  christos 		cd_write_reg(sc, cy->cy_chip, CD1400_RBPR, ibpr);
    678  1.1  christos 	}
    679  1.1  christos 	opt = CD1400_CCR_CMDCHANCTL | CD1400_CCR_XMTEN
    680  1.1  christos 		| (ISSET(t->c_cflag, CREAD) ? CD1400_CCR_RCVEN : CD1400_CCR_RCVDIS);
    681  1.1  christos 
    682  1.1  christos 	if (opt != cy->cy_channel_control) {
    683  1.1  christos 		cy->cy_channel_control = opt;
    684  1.1  christos 		cd1400_channel_cmd(sc, cy, opt);
    685  1.1  christos 	}
    686  1.1  christos 	/* compute COR1 contents */
    687  1.1  christos 	opt = 0;
    688  1.1  christos 	if (ISSET(t->c_cflag, PARENB)) {
    689  1.1  christos 		if (ISSET(t->c_cflag, PARODD))
    690  1.1  christos 			opt |= CD1400_COR1_PARODD;
    691  1.1  christos 		opt |= CD1400_COR1_PARNORMAL;
    692  1.1  christos 	}
    693  1.1  christos 	if (!ISSET(t->c_iflag, INPCK))
    694  1.1  christos 		opt |= CD1400_COR1_NOINPCK;	/* no parity checking */
    695  1.1  christos 
    696  1.1  christos 	if (ISSET(t->c_cflag, CSTOPB))
    697  1.1  christos 		opt |= CD1400_COR1_STOP2;
    698  1.1  christos 
    699  1.1  christos 	switch (t->c_cflag & CSIZE) {
    700  1.1  christos 	case CS5:
    701  1.1  christos 		opt |= CD1400_COR1_CS5;
    702  1.1  christos 		break;
    703  1.1  christos 
    704  1.1  christos 	case CS6:
    705  1.1  christos 		opt |= CD1400_COR1_CS6;
    706  1.1  christos 		break;
    707  1.1  christos 
    708  1.1  christos 	case CS7:
    709  1.1  christos 		opt |= CD1400_COR1_CS7;
    710  1.1  christos 		break;
    711  1.1  christos 
    712  1.1  christos 	default:
    713  1.1  christos 		opt |= CD1400_COR1_CS8;
    714  1.1  christos 		break;
    715  1.1  christos 	}
    716  1.1  christos 
    717  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_COR1, opt);
    718  1.1  christos 
    719  1.1  christos #ifdef CY_DEBUG
    720  1.4  christos 	printf("cor1 = 0x%x...", opt);
    721  1.1  christos #endif
    722  1.1  christos 
    723  1.1  christos 	/*
    724  1.1  christos 	 * use the CD1400 automatic CTS flow control if CRTSCTS is set
    725  1.1  christos 	 *
    726  1.1  christos 	 * CD1400_COR2_ETC is used because breaks are generated with
    727  1.1  christos 	 * embedded transmit commands
    728  1.1  christos 	 */
    729  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_COR2,
    730  1.1  christos 		     CD1400_COR2_ETC |
    731  1.1  christos 		 (ISSET(t->c_cflag, CRTSCTS) ? CD1400_COR2_CCTS_OFLOW : 0));
    732  1.1  christos 
    733  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_COR3, CY_RX_FIFO_THRESHOLD);
    734  1.1  christos 
    735  1.1  christos 	cd1400_channel_cmd(sc, cy, CD1400_CCR_CMDCORCHG |
    736  1.1  christos 	    CD1400_CCR_COR1 | CD1400_CCR_COR2 | CD1400_CCR_COR3);
    737  1.1  christos 
    738  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_COR4, CD1400_COR4_PFO_EXCEPTION);
    739  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_COR5, 0);
    740  1.1  christos 
    741  1.1  christos 	/*
    742  1.1  christos          * set modem change option registers to generate interrupts
    743  1.1  christos          * on carrier detect changes.
    744  1.1  christos          *
    745  1.1  christos          * if hardware RTS handshaking is used (CY_HW_RTS, DTR and RTS lines
    746  1.1  christos          * exchanged), also set the handshaking threshold.
    747  1.1  christos          */
    748  1.1  christos #ifdef CY_HW_RTS
    749  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_MCOR1, CD1400_MCOR1_CDzd |
    750  1.1  christos 		     (ISSET(t->c_cflag, CRTSCTS) ? RX_DTR_THRESHOLD : 0));
    751  1.1  christos #else
    752  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_MCOR1, CD1400_MCOR1_CDzd);
    753  1.1  christos #endif				/* CY_HW_RTS */
    754  1.1  christos 
    755  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_MCOR2, CD1400_MCOR2_CDod);
    756  1.1  christos 
    757  1.1  christos 	/*
    758  1.1  christos          * set receive timeout to approx. 2ms
    759  1.1  christos          * could use more complex logic here...
    760  1.1  christos          * (but is it actually needed or even useful?)
    761  1.1  christos          */
    762  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_RTPR, 2);
    763  1.1  christos 
    764  1.1  christos 	/*
    765  1.1  christos          * should do anything else here?
    766  1.1  christos          * XXX check MDMBUF handshaking like in com.c?
    767  1.1  christos          */
    768  1.1  christos 
    769  1.1  christos 	splx(s);
    770  1.1  christos 	return 0;
    771  1.1  christos }
    772  1.1  christos 
    773  1.1  christos /*
    774  1.1  christos  * set/get modem line status
    775  1.1  christos  *
    776  1.1  christos  * bits can be: TIOCM_DTR, TIOCM_RTS, TIOCM_CTS, TIOCM_CD, TIOCM_RI, TIOCM_DSR
    777  1.1  christos  *
    778  1.1  christos  * RTS and DTR are exchanged if CY_HW_RTS is set
    779  1.1  christos  *
    780  1.1  christos  */
    781  1.1  christos static int
    782  1.1  christos cy_modem_control(sc, cy, bits, howto)
    783  1.1  christos 	struct cy_softc *sc;
    784  1.1  christos 	struct cy_port *cy;
    785  1.1  christos 	int bits;
    786  1.1  christos 	int howto;
    787  1.1  christos {
    788  1.1  christos 	int s, msvr;
    789  1.1  christos 	struct tty *tp = cy->cy_tty;
    790  1.1  christos 
    791  1.1  christos 	s = spltty();
    792  1.1  christos 
    793  1.1  christos 	/* select channel */
    794  1.2   thorpej 	cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
    795  1.2   thorpej 	    cy->cy_port_num & CD1400_CAR_CHAN);
    796  1.1  christos 
    797  1.1  christos 	/* does not manipulate RTS if it is used for flow control */
    798  1.1  christos 	switch (howto) {
    799  1.1  christos 	case DMGET:
    800  1.1  christos 		splx(s);
    801  1.1  christos 		bits = 0;
    802  1.1  christos 		if (cy->cy_channel_control & CD1400_CCR_RCVEN)
    803  1.1  christos 			bits |= TIOCM_LE;
    804  1.1  christos 		msvr = cd_read_reg(sc, cy->cy_chip, CD1400_MSVR2);
    805  1.1  christos #ifdef CY_HW_RTS
    806  1.1  christos 		if (cd_read_reg(sc, cy->cy_chip, CD1400_MSVR1) &
    807  1.1  christos 		    CD1400_MSVR1_RTS)
    808  1.1  christos 			bits |= TIOCM_DTR;
    809  1.1  christos 		if (msvr & CD1400_MSVR2_DTR)
    810  1.1  christos 			bits |= TIOCM_RTS;
    811  1.1  christos #else
    812  1.1  christos 		if (cd_read_reg(sc, cy->cy_chip, CD1400_MSVR1) &
    813  1.1  christos 		    CD1400_MSVR1_RTS)
    814  1.1  christos 			bits |= TIOCM_RTS;
    815  1.1  christos 		if (msvr & CD1400_MSVR2_DTR)
    816  1.1  christos 			bits |= TIOCM_DTR;
    817  1.1  christos #endif				/* CY_HW_RTS */
    818  1.1  christos 		if (msvr & CD1400_MSVR2_CTS)
    819  1.1  christos 			bits |= TIOCM_CTS;
    820  1.1  christos 		if (msvr & CD1400_MSVR2_CD)
    821  1.1  christos 			bits |= TIOCM_CD;
    822  1.1  christos 		if (msvr & CD1400_MSVR2_DSR)	/* not connected on some
    823  1.1  christos 						 * Cyclom cards? */
    824  1.1  christos 			bits |= TIOCM_DSR;
    825  1.1  christos 		if (msvr & CD1400_MSVR2_RI)	/* not connected on Cyclom-8Y
    826  1.1  christos 						 * cards? */
    827  1.1  christos 			bits |= TIOCM_RI;
    828  1.1  christos 		splx(s);
    829  1.1  christos 		return bits;
    830  1.1  christos 
    831  1.1  christos 	case DMSET:		/* replace old values with new ones */
    832  1.1  christos #ifdef CY_HW_RTS
    833  1.1  christos 		if (!ISSET(tp->>t_cflag, CRTSCTS))
    834  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
    835  1.1  christos 			    ((bits & TIOCM_RTS) ? CD1400_MSVR2_DTR : 0));
    836  1.1  christos 		cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
    837  1.1  christos 		    ((bits & TIOCM_DTR) ? CD1400_MSVR1_RTS : 0));
    838  1.1  christos #else
    839  1.1  christos 		if (!ISSET(tp->t_cflag, CRTSCTS))
    840  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
    841  1.1  christos 			   ((bits & TIOCM_RTS) ? CD1400_MSVR1_RTS : 0));
    842  1.1  christos 		cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
    843  1.1  christos 		    ((bits & TIOCM_DTR) ? CD1400_MSVR2_DTR : 0));
    844  1.1  christos #endif /* CY_HW_RTS */
    845  1.1  christos 		break;
    846  1.1  christos 
    847  1.1  christos 	case DMBIS:		/* set bits */
    848  1.1  christos #ifdef CY_HW_RTS
    849  1.1  christos 		if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS) != 0)
    850  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
    851  1.1  christos 			    CD1400_MSVR2_DTR);
    852  1.1  christos 		if (bits & TIOCM_DTR)
    853  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
    854  1.1  christos 			    CD1400_MSVR1_RTS);
    855  1.1  christos #else
    856  1.1  christos 		if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS) != 0)
    857  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
    858  1.1  christos 			    CD1400_MSVR1_RTS);
    859  1.1  christos 		if (bits & TIOCM_DTR)
    860  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2,
    861  1.1  christos 			    CD1400_MSVR2_DTR);
    862  1.1  christos #endif /* CY_HW_RTS */
    863  1.1  christos 		break;
    864  1.1  christos 
    865  1.1  christos 	case DMBIC:		/* clear bits */
    866  1.1  christos #ifdef CY_HW_RTS
    867  1.1  christos 		if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS))
    868  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2, 0);
    869  1.1  christos 		if (bits & TIOCM_DTR)
    870  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1, 0);
    871  1.1  christos #else
    872  1.1  christos 		if (!ISSET(tp->t_cflag, CRTSCTS) && (bits & TIOCM_RTS))
    873  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1, 0);
    874  1.1  christos 		if (bits & TIOCM_DTR)
    875  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_MSVR2, 0);
    876  1.1  christos #endif /* CY_HW_RTS */
    877  1.1  christos 		break;
    878  1.1  christos 	}
    879  1.1  christos 	splx(s);
    880  1.1  christos 	return 0;
    881  1.1  christos }
    882  1.1  christos 
    883  1.1  christos /*
    884  1.1  christos  * Upper-level handler loop (called from timer interrupt?)
    885  1.1  christos  * This routine is common for multiple cards
    886  1.1  christos  */
    887  1.1  christos static void
    888  1.1  christos cy_poll(arg)
    889  1.1  christos 	void *arg;
    890  1.1  christos {
    891  1.1  christos 	int card, port;
    892  1.1  christos 	struct cy_softc *sc;
    893  1.1  christos 	struct cy_port *cy;
    894  1.1  christos 	struct tty *tp;
    895  1.1  christos 	static int counter = 0;
    896  1.1  christos #ifdef CY_DEBUG1
    897  1.1  christos 	int did_something;
    898  1.1  christos #endif
    899  1.1  christos 	int s = spltty();
    900  1.1  christos 
    901  1.1  christos 	if (cy_events == 0 && ++counter < 200) {
    902  1.1  christos 		splx(s);
    903  1.1  christos 		goto out;
    904  1.1  christos 	}
    905  1.1  christos 	cy_events = 0;
    906  1.1  christos 	splx(s);
    907  1.1  christos 
    908  1.1  christos 	for (card = 0; card < cy_cd.cd_ndevs; card++) {
    909  1.1  christos 		sc = cy_cd.cd_devs[card];
    910  1.1  christos 		if (sc == NULL)
    911  1.1  christos 			continue;
    912  1.1  christos 
    913  1.1  christos #ifdef CY_DEBUG1
    914  1.1  christos 		sc->sc_poll_count1++;
    915  1.1  christos 		did_something = 0;
    916  1.1  christos #endif
    917  1.1  christos 
    918  1.1  christos 		for (port = 0; port < sc->sc_nchips * CD1400_NO_OF_CHANNELS;
    919  1.1  christos 		    port++) {
    920  1.1  christos 			cy = &sc->sc_ports[port];
    921  1.1  christos 			if ((tp = cy->cy_tty) == NULL || cy->cy_ibuf == NULL ||
    922  1.1  christos 			    !ISSET(tp->t_state, TS_ISOPEN | TS_WOPEN))
    923  1.1  christos 				continue;
    924  1.1  christos 
    925  1.1  christos 			/*
    926  1.1  christos 		         * handle received data
    927  1.1  christos 		         */
    928  1.1  christos 			while (cy->cy_ibuf_rd_ptr != cy->cy_ibuf_wr_ptr) {
    929  1.1  christos 				u_char line_stat;
    930  1.1  christos 				int chr;
    931  1.1  christos 
    932  1.1  christos 				line_stat = cy->cy_ibuf_rd_ptr[0];
    933  1.1  christos 				chr = cy->cy_ibuf_rd_ptr[1];
    934  1.1  christos 
    935  1.1  christos 				if (line_stat &
    936  1.1  christos 				    (CD1400_RDSR_BREAK | CD1400_RDSR_FE))
    937  1.1  christos 					chr |= TTY_FE;
    938  1.1  christos 				if (line_stat & CD1400_RDSR_PE)
    939  1.1  christos 					chr |= TTY_PE;
    940  1.1  christos 
    941  1.1  christos 				/*
    942  1.1  christos 				 * on an overrun error the data is treated as
    943  1.1  christos 				 * good just as it should be.
    944  1.1  christos 				 */
    945  1.1  christos 
    946  1.1  christos #ifdef CY_DEBUG
    947  1.4  christos 				printf("%s: port %d ttyinput 0x%x\n",
    948  1.1  christos 				    sc->sc_dev.dv_xname, port, chr);
    949  1.1  christos #endif
    950  1.1  christos 
    951  1.1  christos 				(*linesw[tp->t_line].l_rint) (chr, tp);
    952  1.1  christos 
    953  1.1  christos 				s = spltty();	/* really necessary? */
    954  1.1  christos 				if ((cy->cy_ibuf_rd_ptr += 2) ==
    955  1.1  christos 				    cy->cy_ibuf_end)
    956  1.1  christos 					cy->cy_ibuf_rd_ptr = cy->cy_ibuf;
    957  1.1  christos 				splx(s);
    958  1.1  christos 
    959  1.1  christos #ifdef CY_DEBUG1
    960  1.1  christos 				did_something = 1;
    961  1.1  christos #endif
    962  1.1  christos 			}
    963  1.1  christos 
    964  1.1  christos #ifndef CY_HW_RTS
    965  1.1  christos 			/*
    966  1.1  christos 			 * If we don't have any received data in ibuf and
    967  1.1  christos 			 * CRTSCTS is on and RTS is turned off, it is time to
    968  1.1  christos 			 * turn RTS back on
    969  1.1  christos 			 */
    970  1.1  christos 			if (ISSET(tp->t_cflag, CRTSCTS)) {
    971  1.1  christos 				/*
    972  1.1  christos 				 * we can't use cy_modem_control() here as it
    973  1.1  christos 				 * doesn't change RTS if RTSCTS is on
    974  1.1  christos 				 */
    975  1.1  christos 				cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
    976  1.1  christos 				    port & CD1400_CAR_CHAN);
    977  1.1  christos 
    978  1.1  christos 				if ((cd_read_reg(sc, cy->cy_chip,
    979  1.1  christos 				    CD1400_MSVR1) & CD1400_MSVR1_RTS) == 0) {
    980  1.1  christos 					cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1,
    981  1.1  christos 					    CD1400_MSVR1_RTS);
    982  1.1  christos #ifdef CY_DEBUG1
    983  1.1  christos 					did_something = 1;
    984  1.1  christos #endif
    985  1.1  christos 				}
    986  1.1  christos 			}
    987  1.1  christos #endif /* CY_HW_RTS */
    988  1.1  christos 
    989  1.1  christos 			/*
    990  1.1  christos 		         * handle carrier changes
    991  1.1  christos 		         */
    992  1.1  christos 			s = spltty();
    993  1.1  christos 			if (ISSET(cy->cy_flags, CY_F_CARRIER_CHANGED)) {
    994  1.1  christos 				int             carrier;
    995  1.1  christos 
    996  1.1  christos 				CLR(cy->cy_flags, CY_F_CARRIER_CHANGED);
    997  1.1  christos 				splx(s);
    998  1.1  christos 
    999  1.1  christos 				carrier = ((cy->cy_carrier_stat &
   1000  1.1  christos 				    CD1400_MSVR2_CD) != 0);
   1001  1.1  christos 
   1002  1.1  christos #ifdef CY_DEBUG
   1003  1.4  christos 				printf("cy_poll: carrier change "
   1004  1.1  christos 				    "(card %d, port %d, carrier %d)\n",
   1005  1.1  christos 				    card, port, carrier);
   1006  1.1  christos #endif
   1007  1.1  christos 				if (CY_DIALIN(tp->t_dev) &&
   1008  1.1  christos 				    !(*linesw[tp->t_line].l_modem)(tp, carrier))
   1009  1.1  christos 					cy_modem_control(sc, cy,
   1010  1.1  christos 					    TIOCM_DTR, DMBIC);
   1011  1.1  christos 
   1012  1.1  christos #ifdef CY_DEBUG1
   1013  1.1  christos 				did_something = 1;
   1014  1.1  christos #endif
   1015  1.1  christos 			} else
   1016  1.1  christos 				splx(s);
   1017  1.1  christos 
   1018  1.1  christos 			s = spltty();
   1019  1.1  christos 			if (ISSET(cy->cy_flags, CY_F_START)) {
   1020  1.1  christos 				CLR(cy->cy_flags, CY_F_START);
   1021  1.1  christos 				splx(s);
   1022  1.1  christos 
   1023  1.1  christos 				(*linesw[tp->t_line].l_start) (tp);
   1024  1.1  christos 
   1025  1.1  christos #ifdef CY_DEBUG1
   1026  1.1  christos 				did_something = 1;
   1027  1.1  christos #endif
   1028  1.1  christos 			} else
   1029  1.1  christos 				splx(s);
   1030  1.1  christos 
   1031  1.1  christos 			/* could move this to even upper level... */
   1032  1.1  christos 			if (cy->cy_fifo_overruns) {
   1033  1.1  christos 				cy->cy_fifo_overruns = 0;
   1034  1.1  christos 				/*
   1035  1.1  christos 				 * doesn't report overrun count, but
   1036  1.1  christos 				 * shouldn't really matter
   1037  1.1  christos 				 */
   1038  1.1  christos 				log(LOG_WARNING, "%s: port %d fifo overrun\n",
   1039  1.1  christos 				    sc->sc_dev.dv_xname, port);
   1040  1.1  christos 			}
   1041  1.1  christos 			if (cy->cy_ibuf_overruns) {
   1042  1.1  christos 				cy->cy_ibuf_overruns = 0;
   1043  1.1  christos 				log(LOG_WARNING, "%s: port %d ibuf overrun\n",
   1044  1.1  christos 				    sc->sc_dev.dv_xname, port);
   1045  1.1  christos 			}
   1046  1.1  christos 		}		/* for(port...) */
   1047  1.1  christos #ifdef CY_DEBUG1
   1048  1.1  christos 		if (did_something && counter >= 200)
   1049  1.1  christos 			sc->sc_poll_count2++;
   1050  1.1  christos #endif
   1051  1.1  christos 	} /* for(card...) */
   1052  1.1  christos 
   1053  1.1  christos 	counter = 0;
   1054  1.1  christos 
   1055  1.1  christos out:
   1056  1.1  christos 	timeout(cy_poll, NULL, 1);
   1057  1.1  christos }
   1058  1.1  christos 
   1059  1.1  christos /*
   1060  1.1  christos  * hardware interrupt routine
   1061  1.1  christos  */
   1062  1.1  christos int
   1063  1.1  christos cy_intr(arg)
   1064  1.1  christos 	void *arg;
   1065  1.1  christos {
   1066  1.1  christos 	struct cy_softc *sc = arg;
   1067  1.1  christos 	struct cy_port *cy;
   1068  1.1  christos 	int cy_chip, stat;
   1069  1.1  christos 	int int_serviced = 0;
   1070  1.1  christos 
   1071  1.1  christos 	/*
   1072  1.1  christos 	 * Check interrupt status of each CD1400 chip on this card
   1073  1.1  christos 	 * (multiple cards cannot share the same interrupt)
   1074  1.1  christos 	 */
   1075  1.1  christos 	for (cy_chip = 0; cy_chip < sc->sc_nchips; cy_chip++) {
   1076  1.1  christos 
   1077  1.1  christos 		stat = cd_read_reg(sc, cy_chip, CD1400_SVRR);
   1078  1.1  christos 		if (stat == 0)
   1079  1.1  christos 			continue;
   1080  1.1  christos 
   1081  1.1  christos 		if (ISSET(stat, CD1400_SVRR_RXRDY)) {
   1082  1.1  christos 			u_char save_car, save_rir, serv_type;
   1083  1.1  christos 			u_char line_stat, recv_data, n_chars;
   1084  1.1  christos 			u_char *buf_p;
   1085  1.1  christos 
   1086  1.1  christos 			save_rir = cd_read_reg(sc, cy_chip, CD1400_RIR);
   1087  1.1  christos 			save_car = cd_read_reg(sc, cy_chip, CD1400_CAR);
   1088  1.1  christos 			/* enter rx service */
   1089  1.1  christos 			cd_write_reg(sc, cy_chip, CD1400_CAR, save_rir);
   1090  1.1  christos 
   1091  1.1  christos 			serv_type = cd_read_reg(sc, cy_chip, CD1400_RIVR);
   1092  1.1  christos 			cy = &sc->sc_ports[serv_type >> 3];
   1093  1.1  christos 
   1094  1.1  christos #ifdef CY_DEBUG1
   1095  1.1  christos 			cy->cy_rx_int_count++;
   1096  1.1  christos #endif
   1097  1.1  christos 
   1098  1.1  christos 			if (cy->cy_tty == NULL ||
   1099  1.1  christos 			    !ISSET(cy->cy_tty->t_state, TS_ISOPEN))
   1100  1.1  christos 				goto end_rx_serv;
   1101  1.1  christos 
   1102  1.1  christos 			buf_p = cy->cy_ibuf_wr_ptr;
   1103  1.1  christos 
   1104  1.1  christos 			if (ISSET(serv_type, CD1400_RIVR_EXCEPTION)) {
   1105  1.1  christos 				line_stat = cd_read_reg(sc, cy->cy_chip,
   1106  1.1  christos 				    CD1400_RDSR);
   1107  1.1  christos 				recv_data = cd_read_reg(sc, cy->cy_chip,
   1108  1.1  christos 				    CD1400_RDSR);
   1109  1.1  christos 
   1110  1.1  christos #ifdef CY_DEBUG
   1111  1.4  christos 				printf("cy%d port %d recv exception, line_stat 0x%x, char 0x%x\n",
   1112  1.2   thorpej 				card, cy->cy_port_num, line_stat, recv_data);
   1113  1.1  christos #endif
   1114  1.1  christos 				if (ISSET(line_stat, CD1400_RDSR_OE))
   1115  1.1  christos 					cy->cy_fifo_overruns++;
   1116  1.1  christos 
   1117  1.1  christos 				*buf_p++ = line_stat;
   1118  1.1  christos 				*buf_p++ = recv_data;
   1119  1.1  christos 				if (buf_p == cy->cy_ibuf_end)
   1120  1.1  christos 					buf_p = cy->cy_ibuf;
   1121  1.1  christos 
   1122  1.1  christos 				if (buf_p == cy->cy_ibuf_rd_ptr) {
   1123  1.1  christos 					if (buf_p == cy->cy_ibuf)
   1124  1.1  christos 						buf_p = cy->cy_ibuf_end;
   1125  1.1  christos 					buf_p -= 2;
   1126  1.1  christos 					cy->cy_ibuf_overruns++;
   1127  1.1  christos 				}
   1128  1.1  christos 				cy_events = 1;
   1129  1.1  christos 			} else {/* no exception, received data OK */
   1130  1.1  christos 				n_chars = cd_read_reg(sc, cy->cy_chip,
   1131  1.1  christos 				    CD1400_RDCR);
   1132  1.1  christos #ifdef CY_DEBUG
   1133  1.4  christos 				printf("cy%d port %d receive ok %d chars\n",
   1134  1.2   thorpej 				    card, cy->cy_port_num, n_chars);
   1135  1.1  christos #endif
   1136  1.1  christos 				while (n_chars--) {
   1137  1.1  christos 					*buf_p++ = 0;	/* status: OK */
   1138  1.1  christos 					/* data byte */
   1139  1.1  christos 					*buf_p++ = cd_read_reg(sc,
   1140  1.1  christos 					    cy->cy_chip, CD1400_RDSR);
   1141  1.1  christos 					if (buf_p == cy->cy_ibuf_end)
   1142  1.1  christos 						buf_p = cy->cy_ibuf;
   1143  1.1  christos 					if (buf_p == cy->cy_ibuf_rd_ptr) {
   1144  1.1  christos 						if (buf_p == cy->cy_ibuf)
   1145  1.1  christos 							buf_p = cy->cy_ibuf_end;
   1146  1.1  christos 						buf_p -= 2;
   1147  1.1  christos 						cy->cy_ibuf_overruns++;
   1148  1.1  christos 						break;
   1149  1.1  christos 					}
   1150  1.1  christos 				}
   1151  1.1  christos 				cy_events = 1;
   1152  1.1  christos 			}
   1153  1.1  christos 
   1154  1.1  christos 			cy->cy_ibuf_wr_ptr = buf_p;
   1155  1.1  christos 
   1156  1.1  christos #ifndef CY_HW_RTS
   1157  1.1  christos 			/* RTS handshaking for incoming data */
   1158  1.1  christos 			if (ISSET(cy->cy_tty->t_cflag, CRTSCTS)) {
   1159  1.1  christos 				int bf;
   1160  1.1  christos 
   1161  1.1  christos 				bf = buf_p - cy->cy_ibuf_rd_ptr;
   1162  1.1  christos 				if (bf < 0)
   1163  1.1  christos 					bf += CY_IBUF_SIZE;
   1164  1.1  christos 
   1165  1.1  christos 				if (bf > (CY_IBUF_SIZE / 2)) /* turn RTS off */
   1166  1.1  christos 					cd_write_reg(sc, cy->cy_chip, CD1400_MSVR1, 0);
   1167  1.1  christos 			}
   1168  1.1  christos #endif				/* CY_HW_RTS */
   1169  1.1  christos 
   1170  1.1  christos 	end_rx_serv:
   1171  1.1  christos 			/* terminate service context */
   1172  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_RIR, save_rir & 0x3f);
   1173  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_CAR, save_car);
   1174  1.1  christos 			int_serviced = 1;
   1175  1.1  christos 		} /* if(rx_service...) */
   1176  1.1  christos 		if (ISSET(stat, CD1400_SVRR_MDMCH)) {
   1177  1.1  christos 			u_char save_car, save_mir, serv_type, modem_stat;
   1178  1.1  christos 
   1179  1.1  christos 			save_mir = cd_read_reg(sc, cy_chip, CD1400_MIR);
   1180  1.1  christos 			save_car = cd_read_reg(sc, cy_chip, CD1400_CAR);
   1181  1.1  christos 			/* enter modem service */
   1182  1.1  christos 			cd_write_reg(sc, cy_chip, CD1400_CAR, save_mir);
   1183  1.1  christos 
   1184  1.1  christos 			serv_type = cd_read_reg(sc, cy_chip, CD1400_MIVR);
   1185  1.1  christos 			cy = &sc->sc_ports[serv_type >> 3];
   1186  1.1  christos 
   1187  1.1  christos #ifdef CY_DEBUG1
   1188  1.1  christos 			cy->cy_modem_int_count++;
   1189  1.1  christos #endif
   1190  1.1  christos 
   1191  1.1  christos 			modem_stat = cd_read_reg(sc, cy->cy_chip, CD1400_MSVR2);
   1192  1.1  christos 
   1193  1.1  christos #ifdef CY_DEBUG
   1194  1.4  christos 			printf("cy%d port %d modem line change, new stat 0x%x\n",
   1195  1.2   thorpej 			       card, cy->cy_port_num, modem_stat);
   1196  1.1  christos #endif
   1197  1.1  christos 			if (ISSET((cy->cy_carrier_stat ^ modem_stat), CD1400_MSVR2_CD)) {
   1198  1.1  christos 				SET(cy->cy_flags, CY_F_CARRIER_CHANGED);
   1199  1.1  christos 				cy_events = 1;
   1200  1.1  christos 			}
   1201  1.1  christos 			cy->cy_carrier_stat = modem_stat;
   1202  1.1  christos 
   1203  1.1  christos 			/* terminate service context */
   1204  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_MIR, save_mir & 0x3f);
   1205  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_CAR, save_car);
   1206  1.1  christos 			int_serviced = 1;
   1207  1.1  christos 		} /* if(modem_service...) */
   1208  1.1  christos 		if (ISSET(stat, CD1400_SVRR_TXRDY)) {
   1209  1.1  christos 			u_char          save_car, save_tir, serv_type,
   1210  1.1  christos 			                count, ch;
   1211  1.1  christos 			struct tty     *tp;
   1212  1.1  christos 
   1213  1.1  christos 			save_tir = cd_read_reg(sc, cy_chip, CD1400_TIR);
   1214  1.1  christos 			save_car = cd_read_reg(sc, cy_chip, CD1400_CAR);
   1215  1.1  christos 			/* enter tx service */
   1216  1.1  christos 			cd_write_reg(sc, cy_chip, CD1400_CAR, save_tir);
   1217  1.1  christos 
   1218  1.1  christos 			serv_type = cd_read_reg(sc, cy_chip, CD1400_TIVR);
   1219  1.1  christos 			cy = &sc->sc_ports[serv_type >> 3];
   1220  1.1  christos 
   1221  1.1  christos #ifdef CY_DEBUG1
   1222  1.1  christos 			cy->cy_tx_int_count++;
   1223  1.1  christos #endif
   1224  1.1  christos #ifdef CY_DEBUG
   1225  1.4  christos 			printf("cy%d port %d tx service\n", card,
   1226  1.2   thorpej 			    cy->cy_port_num);
   1227  1.1  christos #endif
   1228  1.1  christos 
   1229  1.1  christos 			/* stop transmitting if no tty or CY_F_STOP set */
   1230  1.1  christos 			tp = cy->cy_tty;
   1231  1.1  christos 			if (tp == NULL || ISSET(cy->cy_flags, CY_F_STOP))
   1232  1.1  christos 				goto txdone;
   1233  1.1  christos 
   1234  1.1  christos 			count = 0;
   1235  1.1  christos 			if (ISSET(cy->cy_flags, CY_F_SEND_NUL)) {
   1236  1.1  christos 				cd_write_reg(sc, cy->cy_chip, CD1400_TDR, 0);
   1237  1.1  christos 				cd_write_reg(sc, cy->cy_chip, CD1400_TDR, 0);
   1238  1.1  christos 				count += 2;
   1239  1.1  christos 				CLR(cy->cy_flags, CY_F_SEND_NUL);
   1240  1.1  christos 			}
   1241  1.1  christos 			if (tp->t_outq.c_cc > 0) {
   1242  1.1  christos 				SET(tp->t_state, TS_BUSY);
   1243  1.1  christos 				while (tp->t_outq.c_cc > 0 &&
   1244  1.1  christos 				    count < CD1400_TX_FIFO_SIZE) {
   1245  1.1  christos 					ch = getc(&tp->t_outq);
   1246  1.1  christos 					/*
   1247  1.1  christos 					 * remember to double NUL characters
   1248  1.1  christos 					 * because embedded transmit commands
   1249  1.1  christos 					 * are enabled
   1250  1.1  christos 					 */
   1251  1.1  christos 					if (ch == 0) {
   1252  1.1  christos 						if (count >= CD1400_TX_FIFO_SIZE - 2) {
   1253  1.1  christos 							SET(cy->cy_flags, CY_F_SEND_NUL);
   1254  1.1  christos 							break;
   1255  1.1  christos 						}
   1256  1.1  christos 						cd_write_reg(sc, cy->cy_chip,
   1257  1.1  christos 						    CD1400_TDR, ch);
   1258  1.1  christos 						count++;
   1259  1.1  christos 					}
   1260  1.1  christos 					cd_write_reg(sc, cy->cy_chip,
   1261  1.1  christos 					    CD1400_TDR, ch);
   1262  1.1  christos 					count++;
   1263  1.1  christos 				}
   1264  1.1  christos 			} else {
   1265  1.1  christos 				/*
   1266  1.1  christos 				 * no data to send -- check if we should
   1267  1.1  christos 				 * start/stop a break
   1268  1.1  christos 				 */
   1269  1.1  christos 				/*
   1270  1.1  christos 				 * XXX does this cause too much delay before
   1271  1.1  christos 				 * breaks?
   1272  1.1  christos 				 */
   1273  1.1  christos 				if (ISSET(cy->cy_flags, CY_F_START_BREAK)) {
   1274  1.1  christos 					cd_write_reg(sc, cy->cy_chip,
   1275  1.1  christos 					    CD1400_TDR, 0);
   1276  1.1  christos 					cd_write_reg(sc, cy->cy_chip,
   1277  1.1  christos 					    CD1400_TDR, 0x81);
   1278  1.1  christos 					CLR(cy->cy_flags, CY_F_START_BREAK);
   1279  1.1  christos 				}
   1280  1.1  christos 				if (ISSET(cy->cy_flags, CY_F_END_BREAK)) {
   1281  1.1  christos 					cd_write_reg(sc, cy->cy_chip,
   1282  1.1  christos 					    CD1400_TDR, 0);
   1283  1.1  christos 					cd_write_reg(sc, cy->cy_chip,
   1284  1.1  christos 					    CD1400_TDR, 0x83);
   1285  1.1  christos 					CLR(cy->cy_flags, CY_F_END_BREAK);
   1286  1.1  christos 				}
   1287  1.1  christos 			}
   1288  1.1  christos 
   1289  1.1  christos 			if (tp->t_outq.c_cc == 0) {
   1290  1.1  christos 		txdone:
   1291  1.1  christos 				/*
   1292  1.1  christos 				 * No data to send or requested to stop.
   1293  1.1  christos 				 * Disable transmit interrupt
   1294  1.1  christos 				 */
   1295  1.1  christos 				cd_write_reg(sc, cy->cy_chip, CD1400_SRER,
   1296  1.1  christos 				     cd_read_reg(sc, cy->cy_chip, CD1400_SRER)
   1297  1.1  christos 				     & ~CD1400_SRER_TXRDY);
   1298  1.1  christos 				CLR(cy->cy_flags, CY_F_STOP);
   1299  1.1  christos 				CLR(tp->t_state, TS_BUSY);
   1300  1.1  christos 			}
   1301  1.1  christos 			if (tp->t_outq.c_cc <= tp->t_lowat) {
   1302  1.1  christos 				SET(cy->cy_flags, CY_F_START);
   1303  1.1  christos 				cy_events = 1;
   1304  1.1  christos 			}
   1305  1.1  christos 			/* terminate service context */
   1306  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_TIR, save_tir & 0x3f);
   1307  1.1  christos 			cd_write_reg(sc, cy->cy_chip, CD1400_CAR, save_car);
   1308  1.1  christos 			int_serviced = 1;
   1309  1.1  christos 		}		/* if(tx_service...) */
   1310  1.1  christos 	}			/* for(...all CD1400s on a card) */
   1311  1.1  christos 
   1312  1.1  christos 	/* ensure an edge for next interrupt */
   1313  1.5   thorpej 	bus_space_write_1(sc->sc_memt, sc->sc_bsh,
   1314  1.1  christos 			CY_CLEAR_INTR << sc->sc_bustype, 0);
   1315  1.1  christos 	return int_serviced;
   1316  1.1  christos }
   1317  1.1  christos 
   1318  1.1  christos /*
   1319  1.1  christos  * subroutine to enable CD1400 transmitter
   1320  1.1  christos  */
   1321  1.1  christos static void
   1322  1.1  christos cy_enable_transmitter(sc, cy)
   1323  1.1  christos 	struct cy_softc *sc;
   1324  1.1  christos 	struct cy_port *cy;
   1325  1.1  christos {
   1326  1.1  christos 	int s = spltty();
   1327  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_CAR,
   1328  1.2   thorpej 	    cy->cy_port_num & CD1400_CAR_CHAN);
   1329  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_SRER,
   1330  1.1  christos 	    cd_read_reg(sc, cy->cy_chip, CD1400_SRER) | CD1400_SRER_TXRDY);
   1331  1.1  christos 	splx(s);
   1332  1.1  christos }
   1333  1.1  christos 
   1334  1.1  christos /*
   1335  1.1  christos  * Execute a CD1400 channel command
   1336  1.1  christos  */
   1337  1.1  christos static void
   1338  1.1  christos cd1400_channel_cmd(sc, cy, cmd)
   1339  1.1  christos 	struct cy_softc *sc;
   1340  1.1  christos 	struct cy_port *cy;
   1341  1.1  christos 	int cmd;
   1342  1.1  christos {
   1343  1.1  christos 	u_int waitcnt = 5 * 8 * 1024;	/* approx 5 ms */
   1344  1.1  christos 
   1345  1.1  christos #ifdef CY_DEBUG
   1346  1.4  christos 	printf("c1400_channel_cmd cy 0x%x command 0x%x\n", cy, cmd);
   1347  1.1  christos #endif
   1348  1.1  christos 
   1349  1.1  christos 	/* wait until cd1400 is ready to process a new command */
   1350  1.1  christos 	while (cd_read_reg(sc, cy->cy_chip, CD1400_CCR) != 0 && waitcnt-- > 0);
   1351  1.1  christos 
   1352  1.1  christos 	if (waitcnt == 0)
   1353  1.1  christos 		log(LOG_ERR, "%s: channel command timeout\n",
   1354  1.1  christos 		    sc->sc_dev.dv_xname);
   1355  1.1  christos 
   1356  1.1  christos 	cd_write_reg(sc, cy->cy_chip, CD1400_CCR, cmd);
   1357  1.1  christos }
   1358  1.1  christos 
   1359  1.1  christos /*
   1360  1.1  christos  * Compute clock option register and baud rate register values
   1361  1.1  christos  * for a given speed. Return 0 on success, -1 on failure.
   1362  1.1  christos  *
   1363  1.1  christos  * The error between requested and actual speed seems
   1364  1.1  christos  * to be well within allowed limits (less than 3%)
   1365  1.1  christos  * with every speed value between 50 and 150000 bps.
   1366  1.1  christos  */
   1367  1.1  christos static int
   1368  1.1  christos cy_speed(speed, cor, bpr)
   1369  1.1  christos     speed_t speed;
   1370  1.1  christos     int *cor, *bpr;
   1371  1.1  christos {
   1372  1.1  christos 	int c, co, br;
   1373  1.1  christos 
   1374  1.1  christos 	if (speed < 50 || speed > 150000)
   1375  1.1  christos 		return -1;
   1376  1.1  christos 
   1377  1.1  christos 	for (c = 0, co = 8; co <= 2048; co <<= 2, c++) {
   1378  1.1  christos 		br = (CY_CLOCK + (co * speed) / 2) / (co * speed);
   1379  1.1  christos 		if (br < 0x100) {
   1380  1.1  christos 			*bpr = br;
   1381  1.1  christos 			*cor = c;
   1382  1.1  christos 			return 0;
   1383  1.1  christos 		}
   1384  1.1  christos 	}
   1385  1.1  christos 
   1386  1.1  christos 	return -1;
   1387  1.1  christos }
   1388