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