Home | History | Annotate | Line # | Download | only in usb
ucycom.c revision 1.9
      1  1.9   thorpej /*	$NetBSD: ucycom.c,v 1.9 2006/03/28 17:38:35 thorpej Exp $	*/
      2  1.1     skrll 
      3  1.1     skrll /*
      4  1.1     skrll  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  1.1     skrll  * All rights reserved.
      6  1.1     skrll  *
      7  1.1     skrll  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1     skrll  * by Nick Hudson
      9  1.1     skrll  *
     10  1.1     skrll  * Redistribution and use in source and binary forms, with or without
     11  1.1     skrll  * modification, are permitted provided that the following conditions
     12  1.1     skrll  * are met:
     13  1.1     skrll  * 1. Redistributions of source code must retain the above copyright
     14  1.1     skrll  *    notice, this list of conditions and the following disclaimer.
     15  1.1     skrll  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     skrll  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     skrll  *    documentation and/or other materials provided with the distribution.
     18  1.1     skrll  * 3. All advertising materials mentioning features or use of this software
     19  1.1     skrll  *    must display the following acknowledgement:
     20  1.1     skrll  *        This product includes software developed by the NetBSD
     21  1.1     skrll  *        Foundation, Inc. and its contributors.
     22  1.1     skrll  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1     skrll  *    contributors may be used to endorse or promote products derived
     24  1.1     skrll  *    from this software without specific prior written permission.
     25  1.1     skrll  *
     26  1.1     skrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1     skrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1     skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1     skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1     skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1     skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1     skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1     skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1     skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1     skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1     skrll  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1     skrll  */
     38  1.1     skrll /*
     39  1.1     skrll  * This code is based on the ucom driver.
     40  1.1     skrll  */
     41  1.1     skrll 
     42  1.1     skrll /*
     43  1.1     skrll  * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
     44  1.1     skrll  * RS232 bridges.
     45  1.1     skrll  */
     46  1.1     skrll 
     47  1.1     skrll #include <sys/cdefs.h>
     48  1.9   thorpej __RCSID("$NetBSD: ucycom.c,v 1.9 2006/03/28 17:38:35 thorpej Exp $");
     49  1.1     skrll 
     50  1.1     skrll #include <sys/param.h>
     51  1.1     skrll #include <sys/systm.h>
     52  1.1     skrll #include <sys/conf.h>
     53  1.1     skrll #include <sys/kernel.h>
     54  1.1     skrll #include <sys/malloc.h>
     55  1.3     skrll #include <sys/device.h>
     56  1.1     skrll #include <sys/sysctl.h>
     57  1.1     skrll #include <sys/tty.h>
     58  1.1     skrll #include <sys/file.h>
     59  1.1     skrll #include <sys/vnode.h>
     60  1.1     skrll 
     61  1.1     skrll #include <dev/usb/usb.h>
     62  1.1     skrll #include <dev/usb/usbhid.h>
     63  1.1     skrll 
     64  1.1     skrll #include <dev/usb/usbdi.h>
     65  1.1     skrll #include <dev/usb/usbdi_util.h>
     66  1.1     skrll #include <dev/usb/usbdevs.h>
     67  1.1     skrll #include <dev/usb/uhidev.h>
     68  1.1     skrll #include <dev/usb/hid.h>
     69  1.1     skrll 
     70  1.1     skrll #include "ioconf.h"
     71  1.1     skrll 
     72  1.1     skrll #ifdef UCYCOM_DEBUG
     73  1.1     skrll #define DPRINTF(x)	if (ucycomdebug) logprintf x
     74  1.1     skrll #define DPRINTFN(n, x)	if (ucycomdebug > (n)) logprintf x
     75  1.1     skrll int	ucycomdebug = 0;
     76  1.1     skrll #else
     77  1.1     skrll #define DPRINTF(x)
     78  1.1     skrll #define DPRINTFN(n,x)
     79  1.1     skrll #endif
     80  1.1     skrll 
     81  1.1     skrll 
     82  1.1     skrll #define UCYCOMUNIT_MASK		0x3ffff
     83  1.1     skrll #define UCYCOMDIALOUT_MASK	0x80000
     84  1.1     skrll #define UCYCOMCALLUNIT_MASK	0x40000
     85  1.1     skrll 
     86  1.1     skrll #define UCYCOMUNIT(x)		(minor(x) & UCYCOMUNIT_MASK)
     87  1.1     skrll #define UCYCOMDIALOUT(x)	(minor(x) & UCYCOMDIALOUT_MASK)
     88  1.1     skrll #define UCYCOMCALLUNIT(x)	(minor(x) & UCYCOMCALLUNIT_MASK)
     89  1.1     skrll 
     90  1.1     skrll /* Configuration Byte */
     91  1.1     skrll #define UCYCOM_RESET		0x80
     92  1.1     skrll #define UCYCOM_PARITY_TYPE_MASK	0x20
     93  1.1     skrll #define  UCYCOM_PARITY_ODD	 0x20
     94  1.1     skrll #define  UCYCOM_PARITY_EVEN	 0x00
     95  1.1     skrll #define UCYCOM_PARITY_MASK	0x10
     96  1.1     skrll #define  UCYCOM_PARITY_ON	 0x10
     97  1.1     skrll #define  UCYCOM_PARITY_OFF	 0x00
     98  1.1     skrll #define UCYCOM_STOP_MASK	0x08
     99  1.1     skrll #define  UCYCOM_STOP_BITS_2	 0x08
    100  1.1     skrll #define  UCYCOM_STOP_BITS_1	 0x00
    101  1.1     skrll #define UCYCOM_DATA_MASK	0x03
    102  1.1     skrll #define  UCYCOM_DATA_BITS_8	 0x03
    103  1.1     skrll #define  UCYCOM_DATA_BITS_7	 0x02
    104  1.1     skrll #define  UCYCOM_DATA_BITS_6	 0x01
    105  1.1     skrll #define  UCYCOM_DATA_BITS_5	 0x00
    106  1.1     skrll 
    107  1.1     skrll /* Modem (Input) status byte */
    108  1.1     skrll #define UCYCOM_RI	0x80
    109  1.1     skrll #define UCYCOM_DCD	0x40
    110  1.1     skrll #define UCYCOM_DSR	0x20
    111  1.1     skrll #define UCYCOM_CTS	0x10
    112  1.1     skrll #define UCYCOM_ERROR	0x08
    113  1.1     skrll #define UCYCOM_LMASK	0x07
    114  1.1     skrll 
    115  1.1     skrll /* Modem (Output) control byte */
    116  1.1     skrll #define UCYCOM_DTR	0x20
    117  1.1     skrll #define UCYCOM_RTS	0x10
    118  1.1     skrll #define UCYCOM_ORESET	0x08
    119  1.1     skrll 
    120  1.1     skrll struct ucycom_softc {
    121  1.1     skrll 	struct uhidev		sc_hdev;
    122  1.1     skrll 
    123  1.1     skrll 	struct tty		*sc_tty;
    124  1.1     skrll 
    125  1.1     skrll 	/* uhidev parameters */
    126  1.1     skrll 	size_t			sc_flen; /* feature report length */
    127  1.1     skrll 	size_t			sc_ilen; /* input report length */
    128  1.1     skrll 	size_t			sc_olen; /* output report length */
    129  1.1     skrll 
    130  1.1     skrll 	uint8_t			*sc_obuf;
    131  1.1     skrll 
    132  1.1     skrll 	/* settings */
    133  1.1     skrll 	uint32_t		sc_baud;
    134  1.1     skrll 	uint8_t			sc_cfg;	/* Data format */
    135  1.1     skrll 	uint8_t			sc_mcr;	/* Modem control */
    136  1.1     skrll 	uint8_t			sc_msr;	/* Modem status */
    137  1.1     skrll 	int			sc_swflags;
    138  1.1     skrll 
    139  1.1     skrll 	/* flags */
    140  1.1     skrll 	char			sc_dying;
    141  1.1     skrll };
    142  1.1     skrll 
    143  1.1     skrll dev_type_open(ucycomopen);
    144  1.1     skrll dev_type_close(ucycomclose);
    145  1.1     skrll dev_type_read(ucycomread);
    146  1.1     skrll dev_type_write(ucycomwrite);
    147  1.1     skrll dev_type_ioctl(ucycomioctl);
    148  1.1     skrll dev_type_stop(ucycomstop);
    149  1.1     skrll dev_type_tty(ucycomtty);
    150  1.1     skrll dev_type_poll(ucycompoll);
    151  1.1     skrll 
    152  1.1     skrll const struct cdevsw ucycom_cdevsw = {
    153  1.1     skrll 	ucycomopen, ucycomclose, ucycomread, ucycomwrite, ucycomioctl,
    154  1.1     skrll 	ucycomstop, ucycomtty, ucycompoll, nommap, ttykqfilter, D_TTY
    155  1.1     skrll };
    156  1.1     skrll 
    157  1.1     skrll Static int ucycomparam(struct tty *, struct termios *);
    158  1.1     skrll Static void ucycomstart(struct tty *);
    159  1.1     skrll Static void ucycom_intr(struct uhidev *, void *, u_int);
    160  1.1     skrll Static int ucycom_configure(struct ucycom_softc *, uint32_t, uint8_t);
    161  1.1     skrll Static void tiocm_to_ucycom(struct ucycom_softc *, u_long, int);
    162  1.1     skrll Static int ucycom_to_tiocm(struct ucycom_softc *);
    163  1.1     skrll Static void ucycom_set_status(struct ucycom_softc *);
    164  1.1     skrll Static void ucycom_dtr(struct ucycom_softc *, int);
    165  1.1     skrll #if 0
    166  1.1     skrll Static void ucycom_rts(struct ucycom_softc *, int);
    167  1.1     skrll #endif
    168  1.1     skrll Static void ucycom_cleanup(struct ucycom_softc *sc);
    169  1.1     skrll 
    170  1.1     skrll #ifdef UCYCOM_DEBUG
    171  1.1     skrll Static void ucycom_get_cfg(struct ucycom_softc *);
    172  1.1     skrll #endif
    173  1.1     skrll 
    174  1.1     skrll Static const struct usb_devno ucycom_devs[] = {
    175  1.1     skrll 	{ USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 },
    176  1.1     skrll 	{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE },
    177  1.1     skrll };
    178  1.1     skrll #define ucycom_lookup(v, p) usb_lookup(ucycom_devs, v, p)
    179  1.1     skrll 
    180  1.1     skrll USB_DECLARE_DRIVER(ucycom);
    181  1.1     skrll 
    182  1.1     skrll int
    183  1.1     skrll ucycom_match(struct device *parent, struct cfdata *match, void *aux)
    184  1.1     skrll {
    185  1.1     skrll 	struct uhidev_attach_arg *uha = aux;
    186  1.1     skrll 
    187  1.1     skrll 	return (ucycom_lookup(uha->uaa->vendor, uha->uaa->product) != NULL ?
    188  1.1     skrll 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
    189  1.1     skrll }
    190  1.1     skrll 
    191  1.1     skrll void
    192  1.1     skrll ucycom_attach(struct device *parent, struct device *self, void *aux)
    193  1.1     skrll {
    194  1.1     skrll 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
    195  1.1     skrll 	struct uhidev_attach_arg *uha = aux;
    196  1.1     skrll 	int size, repid;
    197  1.1     skrll 	void *desc;
    198  1.1     skrll 
    199  1.1     skrll 	sc->sc_hdev.sc_intr = ucycom_intr;
    200  1.1     skrll 	sc->sc_hdev.sc_parent = uha->parent;
    201  1.1     skrll 	sc->sc_hdev.sc_report_id = uha->reportid;
    202  1.1     skrll 
    203  1.1     skrll 	uhidev_get_report_desc(uha->parent, &desc, &size);
    204  1.1     skrll 	repid = uha->reportid;
    205  1.1     skrll 	sc->sc_ilen = hid_report_size(desc, size, hid_input, repid);
    206  1.1     skrll 	sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
    207  1.1     skrll 	sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);
    208  1.1     skrll 
    209  1.1     skrll 	sc->sc_msr = sc->sc_mcr = 0;
    210  1.1     skrll 
    211  1.1     skrll 	/* set up tty */
    212  1.1     skrll 	sc->sc_tty = ttymalloc();
    213  1.1     skrll 	sc->sc_tty->t_sc = sc;
    214  1.1     skrll 	sc->sc_tty->t_oproc = ucycomstart;
    215  1.1     skrll 	sc->sc_tty->t_param = ucycomparam;
    216  1.1     skrll 
    217  1.1     skrll 	tty_attach(sc->sc_tty);
    218  1.1     skrll 
    219  1.4     skrll 	/* Nothing interesting to report */
    220  1.4     skrll 	printf("\n");
    221  1.4     skrll 
    222  1.1     skrll }
    223  1.1     skrll 
    224  1.1     skrll 
    225  1.1     skrll int
    226  1.1     skrll ucycom_detach(struct device *self, int flags)
    227  1.1     skrll {
    228  1.1     skrll 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
    229  1.1     skrll 	struct tty *tp = sc->sc_tty;
    230  1.1     skrll 	int maj, mn;
    231  1.1     skrll 	int s;
    232  1.1     skrll 
    233  1.1     skrll 	DPRINTF(("ucycom_detach: sc=%p flags=%d tp=%p\n", sc, flags, tp));
    234  1.1     skrll 
    235  1.1     skrll 	sc->sc_dying = 1;
    236  1.1     skrll 
    237  1.1     skrll 	s = splusb();
    238  1.1     skrll 	if (tp != NULL) {
    239  1.1     skrll 		CLR(tp->t_state, TS_CARR_ON);
    240  1.1     skrll 		CLR(tp->t_cflag, CLOCAL | MDMBUF);
    241  1.1     skrll 		ttyflush(tp, FREAD|FWRITE);
    242  1.1     skrll 	}
    243  1.1     skrll 	/* Wait for processes to go away. */
    244  1.1     skrll 	usb_detach_wait(USBDEV(sc->sc_hdev.sc_dev));
    245  1.1     skrll 	splx(s);
    246  1.1     skrll 
    247  1.1     skrll 	/* locate the major number */
    248  1.1     skrll 	maj = cdevsw_lookup_major(&ucycom_cdevsw);
    249  1.1     skrll 
    250  1.1     skrll 	/* Nuke the vnodes for any open instances. */
    251  1.9   thorpej 	mn = device_unit(self);
    252  1.1     skrll 
    253  1.1     skrll 	DPRINTFN(2, ("ucycom_detach: maj=%d mn=%d\n", maj, mn));
    254  1.1     skrll 	vdevgone(maj, mn, mn, VCHR);
    255  1.1     skrll 	vdevgone(maj, mn | UCYCOMDIALOUT_MASK, mn | UCYCOMDIALOUT_MASK, VCHR);
    256  1.1     skrll 	vdevgone(maj, mn | UCYCOMCALLUNIT_MASK, mn | UCYCOMCALLUNIT_MASK, VCHR);
    257  1.1     skrll 
    258  1.1     skrll 	/* Detach and free the tty. */
    259  1.1     skrll 	if (tp != NULL) {
    260  1.1     skrll 		DPRINTF(("ucycom_detach: tty_detach %p\n", tp));
    261  1.1     skrll 		tty_detach(tp);
    262  1.1     skrll 		ttyfree(tp);
    263  1.1     skrll 		sc->sc_tty = NULL;
    264  1.1     skrll 	}
    265  1.1     skrll 
    266  1.1     skrll 	return 0;
    267  1.1     skrll }
    268  1.1     skrll 
    269  1.1     skrll int
    270  1.1     skrll ucycom_activate(device_ptr_t self, enum devact act)
    271  1.1     skrll {
    272  1.1     skrll 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
    273  1.1     skrll 
    274  1.1     skrll 	DPRINTFN(5,("ucycom_activate: %d\n", act));
    275  1.1     skrll 
    276  1.1     skrll 	switch (act) {
    277  1.1     skrll 	case DVACT_ACTIVATE:
    278  1.1     skrll 		return (EOPNOTSUPP);
    279  1.1     skrll 
    280  1.1     skrll 	case DVACT_DEACTIVATE:
    281  1.1     skrll 		sc->sc_dying = 1;
    282  1.1     skrll 		break;
    283  1.1     skrll 	}
    284  1.1     skrll 	return (0);
    285  1.1     skrll }
    286  1.1     skrll 
    287  1.1     skrll #if 0
    288  1.1     skrll void
    289  1.1     skrll ucycom_shutdown(struct ucycom_softc *sc)
    290  1.1     skrll {
    291  1.1     skrll 	struct tty *tp = sc->sc_tty;
    292  1.1     skrll 
    293  1.1     skrll 	DPRINTF(("ucycom_shutdown\n"));
    294  1.1     skrll 	/*
    295  1.1     skrll 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    296  1.1     skrll 	 * notice even if we immediately open the port again.
    297  1.1     skrll 	 */
    298  1.1     skrll 	if (ISSET(tp->t_cflag, HUPCL)) {
    299  1.1     skrll 		ucycom_dtr(sc, 0);
    300  1.1     skrll 		(void)tsleep(sc, TTIPRI, ttclos, hz);
    301  1.1     skrll 	}
    302  1.1     skrll }
    303  1.1     skrll #endif
    304  1.1     skrll 
    305  1.1     skrll int
    306  1.6  christos ucycomopen(dev_t dev, int flag, int mode, struct lwp *l)
    307  1.1     skrll {
    308  1.1     skrll 	int unit = UCYCOMUNIT(dev);
    309  1.1     skrll 	struct ucycom_softc *sc;
    310  1.1     skrll 	struct tty *tp;
    311  1.1     skrll 	int s, err;
    312  1.1     skrll 
    313  1.1     skrll 	DPRINTF(("ucycomopen: unit=%d\n", unit));
    314  1.1     skrll 
    315  1.1     skrll 	if (unit >= ucycom_cd.cd_ndevs)
    316  1.1     skrll 		return (ENXIO);
    317  1.1     skrll 	sc = ucycom_cd.cd_devs[unit];
    318  1.1     skrll 
    319  1.1     skrll 	DPRINTF(("ucycomopen: sc=%p\n", sc));
    320  1.1     skrll 
    321  1.1     skrll 	if (sc == NULL)
    322  1.1     skrll 		return (ENXIO);
    323  1.1     skrll 
    324  1.1     skrll 	if (sc->sc_dying)
    325  1.1     skrll 		return (EIO);
    326  1.1     skrll 
    327  1.7   thorpej 	if (!device_is_active(&sc->sc_hdev.sc_dev))
    328  1.1     skrll 		return (ENXIO);
    329  1.1     skrll 
    330  1.1     skrll 	tp = sc->sc_tty;
    331  1.1     skrll 
    332  1.1     skrll 	DPRINTF(("ucycomopen: tp=%p\n", tp));
    333  1.1     skrll 
    334  1.1     skrll 	if (ISSET(tp->t_state, TS_ISOPEN) &&
    335  1.1     skrll 	    ISSET(tp->t_state, TS_XCLUDE) &&
    336  1.6  christos 	    suser(l->l_proc->p_ucred, &l->l_proc->p_acflag) != 0)
    337  1.1     skrll 		return (EBUSY);
    338  1.1     skrll 
    339  1.1     skrll 	s = spltty();
    340  1.1     skrll 
    341  1.1     skrll 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    342  1.1     skrll 		struct termios t;
    343  1.1     skrll 
    344  1.1     skrll 		tp->t_dev = dev;
    345  1.1     skrll 
    346  1.1     skrll 		err = uhidev_open(&sc->sc_hdev);
    347  1.1     skrll 		if (err) {
    348  1.1     skrll 			/* Any cleanup? */
    349  1.1     skrll 			splx(s);
    350  1.1     skrll 			return (err);
    351  1.1     skrll 		}
    352  1.1     skrll 
    353  1.1     skrll 		/*
    354  1.1     skrll 		 * Initialize the termios status to the defaults.  Add in the
    355  1.1     skrll 		 * sticky bits from TIOCSFLAGS.
    356  1.1     skrll 		 */
    357  1.1     skrll 		t.c_ispeed = 0;
    358  1.1     skrll 		t.c_ospeed = TTYDEF_SPEED;
    359  1.1     skrll 		t.c_cflag = TTYDEF_CFLAG;
    360  1.1     skrll 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    361  1.1     skrll 			SET(t.c_cflag, CLOCAL);
    362  1.1     skrll 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    363  1.1     skrll 			SET(t.c_cflag, CRTSCTS);
    364  1.1     skrll 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    365  1.1     skrll 			SET(t.c_cflag, MDMBUF);
    366  1.1     skrll 
    367  1.1     skrll 		tp->t_ospeed = 0;
    368  1.1     skrll 		(void) ucycomparam(tp, &t);
    369  1.1     skrll 		tp->t_iflag = TTYDEF_IFLAG;
    370  1.1     skrll 		tp->t_oflag = TTYDEF_OFLAG;
    371  1.1     skrll 		tp->t_lflag = TTYDEF_LFLAG;
    372  1.1     skrll 		ttychars(tp);
    373  1.1     skrll 		ttsetwater(tp);
    374  1.1     skrll 
    375  1.1     skrll 		/* Allocate an output report buffer */
    376  1.1     skrll 		sc->sc_obuf = malloc(sc->sc_olen, M_USBDEV, M_WAITOK);
    377  1.1     skrll 
    378  1.1     skrll 		DPRINTF(("ucycomopen: sc->sc_obuf=%p\n", sc->sc_obuf));
    379  1.1     skrll 
    380  1.1     skrll #if 0
    381  1.1     skrll 		/* XXX Don't do this as for some reason trying to do an interrupt
    382  1.1     skrll 		 * XXX out transfer at this point means everything gets stuck!?!
    383  1.1     skrll 		 */
    384  1.1     skrll 		/*
    385  1.1     skrll 		 * Turn on DTR.  We must always do this, even if carrier is not
    386  1.1     skrll 		 * present, because otherwise we'd have to use TIOCSDTR
    387  1.1     skrll 		 * immediately after setting CLOCAL, which applications do not
    388  1.1     skrll 		 * expect.  We always assert DTR while the device is open
    389  1.1     skrll 		 * unless explicitly requested to deassert it.
    390  1.1     skrll 		 */
    391  1.1     skrll 		ucycom_dtr(sc, 1);
    392  1.1     skrll #endif
    393  1.1     skrll 
    394  1.1     skrll #if 0
    395  1.1     skrll 		/* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/
    396  1.1     skrll 		ucycom_hwiflow(sc);
    397  1.1     skrll #endif
    398  1.1     skrll 
    399  1.1     skrll 	}
    400  1.1     skrll 	splx(s);
    401  1.1     skrll 
    402  1.1     skrll 	err = ttyopen(tp, UCYCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    403  1.1     skrll 	if (err)
    404  1.1     skrll 		goto bad;
    405  1.1     skrll 
    406  1.1     skrll 	err = (*tp->t_linesw->l_open)(dev, tp);
    407  1.1     skrll 	if (err)
    408  1.1     skrll 		goto bad;
    409  1.1     skrll 
    410  1.1     skrll 	return (0);
    411  1.1     skrll 
    412  1.1     skrll bad:
    413  1.1     skrll 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    414  1.1     skrll 		/*
    415  1.1     skrll 		 * We failed to open the device, and nobody else had it opened.
    416  1.1     skrll 		 * Clean up the state as appropriate.
    417  1.1     skrll 		 */
    418  1.1     skrll 		ucycom_cleanup(sc);
    419  1.1     skrll 	}
    420  1.1     skrll 
    421  1.1     skrll 	return (err);
    422  1.1     skrll 
    423  1.1     skrll }
    424  1.1     skrll 
    425  1.1     skrll 
    426  1.1     skrll int
    427  1.6  christos ucycomclose(dev_t dev, int flag, int mode, struct lwp *l)
    428  1.1     skrll {
    429  1.1     skrll 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
    430  1.1     skrll 	struct tty *tp = sc->sc_tty;
    431  1.1     skrll 
    432  1.1     skrll 	DPRINTF(("ucycomclose: unit=%d\n", UCYCOMUNIT(dev)));
    433  1.1     skrll 	if (!ISSET(tp->t_state, TS_ISOPEN))
    434  1.1     skrll 		return (0);
    435  1.1     skrll 
    436  1.1     skrll 	(*tp->t_linesw->l_close)(tp, flag);
    437  1.1     skrll 	ttyclose(tp);
    438  1.1     skrll 
    439  1.1     skrll 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    440  1.1     skrll 		/*
    441  1.1     skrll 		 * Although we got a last close, the device may still be in
    442  1.1     skrll 		 * use; e.g. if this was the dialout node, and there are still
    443  1.1     skrll 		 * processes waiting for carrier on the non-dialout node.
    444  1.1     skrll 		 */
    445  1.1     skrll 		ucycom_cleanup(sc);
    446  1.1     skrll 	}
    447  1.1     skrll 
    448  1.1     skrll 	return (0);
    449  1.1     skrll }
    450  1.1     skrll 
    451  1.1     skrll Static void
    452  1.1     skrll ucycomstart(struct tty *tp)
    453  1.1     skrll {
    454  1.1     skrll 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(tp->t_dev)];
    455  1.1     skrll 	u_char *data;
    456  1.1     skrll 	int cnt, len, err, s;
    457  1.1     skrll 
    458  1.1     skrll 	if (sc->sc_dying)
    459  1.1     skrll 		return;
    460  1.1     skrll 
    461  1.1     skrll 	s = spltty();
    462  1.1     skrll 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
    463  1.1     skrll 		DPRINTFN(4,("ucycomstart: no go, state=0x%x\n", tp->t_state));
    464  1.1     skrll 		goto out;
    465  1.1     skrll 	}
    466  1.1     skrll 
    467  1.1     skrll #if 0
    468  1.1     skrll 	/* HW FLOW CTL */
    469  1.1     skrll 	if (sc->sc_tx_stopped)
    470  1.1     skrll 		goto out;
    471  1.1     skrll #endif
    472  1.1     skrll 
    473  1.1     skrll 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    474  1.1     skrll 		if (ISSET(tp->t_state, TS_ASLEEP)) {
    475  1.1     skrll 			CLR(tp->t_state, TS_ASLEEP);
    476  1.1     skrll 			wakeup(&tp->t_outq);
    477  1.1     skrll 		}
    478  1.1     skrll 		selwakeup(&tp->t_wsel);
    479  1.1     skrll 		if (tp->t_outq.c_cc == 0)
    480  1.1     skrll 			goto out;
    481  1.1     skrll 	}
    482  1.1     skrll 
    483  1.1     skrll 	/* Grab the first contiguous region of buffer space. */
    484  1.1     skrll 	data = tp->t_outq.c_cf;
    485  1.1     skrll 	cnt = ndqb(&tp->t_outq, 0);
    486  1.1     skrll 
    487  1.1     skrll 	if (cnt == 0) {
    488  1.1     skrll 		DPRINTF(("ucycomstart: cnt == 0\n"));
    489  1.1     skrll 		goto out;
    490  1.1     skrll 	}
    491  1.1     skrll 
    492  1.1     skrll 	SET(tp->t_state, TS_BUSY);
    493  1.1     skrll 
    494  1.1     skrll 	/*
    495  1.1     skrll 	 * The 8 byte output report uses byte 0 for control and byte
    496  1.1     skrll 	 * count.
    497  1.1     skrll 	 *
    498  1.1     skrll 	 * The 32 byte output report uses byte 0 for control. Byte 1
    499  1.1     skrll 	 * is used for byte count.
    500  1.1     skrll 	 */
    501  1.1     skrll 	memset(sc->sc_obuf, 0, sc->sc_olen);
    502  1.1     skrll 	len = cnt;
    503  1.1     skrll 	switch (sc->sc_olen) {
    504  1.1     skrll 	case 8:
    505  1.1     skrll 		if (cnt > sc->sc_olen - 1) {
    506  1.1     skrll 			DPRINTF(("ucycomstart(8): big buffer %d chars\n", len));
    507  1.1     skrll 			len = sc->sc_olen - 1;
    508  1.1     skrll 		}
    509  1.1     skrll 
    510  1.1     skrll 		memcpy(sc->sc_obuf + 1, data, len);
    511  1.1     skrll 		sc->sc_obuf[0] = len | sc->sc_mcr;
    512  1.1     skrll 
    513  1.1     skrll 		DPRINTF(("ucycomstart(8): sc->sc_obuf[0] = %d | %d = %d\n", len, sc->sc_mcr, sc->sc_obuf[0]));
    514  1.2     skrll #ifdef UCYCOM_DEBUG
    515  1.1     skrll 		if (ucycomdebug > 10) {
    516  1.1     skrll 			u_int32_t i;
    517  1.1     skrll 			u_int8_t *d = data;
    518  1.1     skrll 
    519  1.1     skrll 			DPRINTF(("ucycomstart(8): data ="));
    520  1.1     skrll 			for (i = 0; i < len; i++)
    521  1.1     skrll 				DPRINTF((" %02x", d[i]));
    522  1.1     skrll 			DPRINTF(("\n"));
    523  1.1     skrll 		}
    524  1.2     skrll #endif
    525  1.1     skrll 		break;
    526  1.1     skrll 
    527  1.1     skrll 	case 32:
    528  1.1     skrll 		if (cnt > sc->sc_olen - 2) {
    529  1.1     skrll 			DPRINTF(("ucycomstart(32): big buffer %d chars\n", len));
    530  1.1     skrll 			len = sc->sc_olen - 2;
    531  1.1     skrll 		}
    532  1.1     skrll 
    533  1.1     skrll 		memcpy(sc->sc_obuf + 2, data, len);
    534  1.1     skrll 		sc->sc_obuf[0] = sc->sc_mcr;
    535  1.1     skrll 		sc->sc_obuf[1] = len;
    536  1.1     skrll 		DPRINTF(("ucycomstart(32): sc->sc_obuf[0] = %d\nsc->sc_obuf[1] = %d\n", sc->sc_obuf[0], sc->sc_obuf[1]));
    537  1.2     skrll #ifdef UCYCOM_DEBUG
    538  1.1     skrll 		if (ucycomdebug > 10) {
    539  1.1     skrll 			u_int32_t i;
    540  1.1     skrll 			u_int8_t *d = data;
    541  1.1     skrll 
    542  1.1     skrll 			DPRINTF(("ucycomstart(32): data ="));
    543  1.1     skrll 			for (i = 0; i < len; i++)
    544  1.1     skrll 				DPRINTF((" %02x", d[i]));
    545  1.1     skrll 			DPRINTF(("\n"));
    546  1.1     skrll 		}
    547  1.2     skrll #endif
    548  1.1     skrll 		break;
    549  1.1     skrll 
    550  1.1     skrll 	default:
    551  1.2     skrll         	DPRINTFN(2,("ucycomstart: unknown output report size (%zd)\n",
    552  1.1     skrll 		    sc->sc_olen));
    553  1.1     skrll 		goto out;
    554  1.1     skrll 	}
    555  1.1     skrll 	splx(s);
    556  1.1     skrll 
    557  1.1     skrll #ifdef UCYCOM_DEBUG
    558  1.1     skrll 	if (ucycomdebug > 5) {
    559  1.1     skrll 		int i;
    560  1.1     skrll 
    561  1.1     skrll 		if (len != 0) {
    562  1.2     skrll 			DPRINTF(("ucycomstart: sc->sc_obuf[0..%zd) =", sc->sc_olen));
    563  1.1     skrll 			for (i = 0; i < sc->sc_olen; i++)
    564  1.1     skrll 				DPRINTF((" %02x", sc->sc_obuf[i]));
    565  1.1     skrll 			DPRINTF(("\n"));
    566  1.1     skrll 		}
    567  1.1     skrll 	}
    568  1.1     skrll #endif
    569  1.1     skrll 	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
    570  1.1     skrll 
    571  1.1     skrll 	if (err)
    572  1.1     skrll 		DPRINTF(("ucycomstart: error doing uhidev_write = %d\n", err));
    573  1.1     skrll 
    574  1.1     skrll #ifdef UCYCOM_DEBUG
    575  1.1     skrll 	ucycom_get_cfg(sc);
    576  1.1     skrll #endif
    577  1.1     skrll 	DPRINTFN(4,("ucycomstart: req %d chars did %d chars\n", cnt, len));
    578  1.1     skrll 
    579  1.1     skrll  	s = spltty();
    580  1.1     skrll 	CLR(tp->t_state, TS_BUSY);
    581  1.1     skrll 	if (ISSET(tp->t_state, TS_FLUSH))
    582  1.1     skrll 		CLR(tp->t_state, TS_FLUSH);
    583  1.1     skrll 	else
    584  1.1     skrll 		ndflush(&tp->t_outq, len);
    585  1.1     skrll 	(*tp->t_linesw->l_start)(tp);
    586  1.1     skrll 
    587  1.1     skrll out:
    588  1.1     skrll 	splx(s);
    589  1.1     skrll }
    590  1.1     skrll 
    591  1.1     skrll Static int
    592  1.1     skrll ucycomparam(struct tty *tp, struct termios *t)
    593  1.1     skrll {
    594  1.1     skrll 	struct ucycom_softc *sc = tp->t_sc;
    595  1.1     skrll 	uint32_t baud;
    596  1.1     skrll 	uint8_t cfg;
    597  1.1     skrll 	int err;
    598  1.1     skrll 
    599  1.1     skrll 	if (t->c_ospeed < 0) {
    600  1.1     skrll 		DPRINTF(("ucycomparam: c_ospeed < 0\n"));
    601  1.1     skrll 		return (EINVAL);
    602  1.1     skrll 	}
    603  1.1     skrll 
    604  1.1     skrll 	/* Check requested parameters. */
    605  1.1     skrll 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    606  1.1     skrll 		return (EINVAL);
    607  1.1     skrll 
    608  1.1     skrll 	/*
    609  1.1     skrll 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    610  1.1     skrll 	 * is always active.
    611  1.1     skrll 	 */
    612  1.1     skrll 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
    613  1.1     skrll 		SET(t->c_cflag, CLOCAL);
    614  1.1     skrll 		CLR(t->c_cflag, HUPCL);
    615  1.1     skrll 	}
    616  1.1     skrll 
    617  1.1     skrll 	/*
    618  1.1     skrll 	 * If there were no changes, don't do anything.  This avoids dropping
    619  1.1     skrll 	 * input and improves performance when all we did was frob things like
    620  1.1     skrll 	 * VMIN and VTIME.
    621  1.1     skrll 	 */
    622  1.1     skrll 	if (tp->t_ospeed == t->c_ospeed &&
    623  1.1     skrll 	    tp->t_cflag == t->c_cflag)
    624  1.1     skrll 		return (0);
    625  1.1     skrll 
    626  1.1     skrll 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
    627  1.1     skrll 
    628  1.1     skrll 	/* And copy to tty. */
    629  1.1     skrll 	tp->t_ispeed = 0;
    630  1.1     skrll 	tp->t_ospeed = t->c_ospeed;
    631  1.1     skrll 	tp->t_cflag = t->c_cflag;
    632  1.1     skrll 
    633  1.1     skrll 	baud = t->c_ispeed;
    634  1.1     skrll 	DPRINTF(("ucycomparam: baud=%d\n", baud));
    635  1.1     skrll 
    636  1.1     skrll 	if (t->c_cflag & CIGNORE) {
    637  1.1     skrll 		cfg = sc->sc_cfg;
    638  1.1     skrll 	} else {
    639  1.1     skrll 		cfg = 0;
    640  1.1     skrll 		switch (t->c_cflag & CSIZE) {
    641  1.1     skrll 		case CS8:
    642  1.1     skrll 			cfg |= UCYCOM_DATA_BITS_8;
    643  1.1     skrll 			break;
    644  1.1     skrll 		case CS7:
    645  1.1     skrll 			cfg |= UCYCOM_DATA_BITS_7;
    646  1.1     skrll 			break;
    647  1.1     skrll 		case CS6:
    648  1.1     skrll 			cfg |= UCYCOM_DATA_BITS_6;
    649  1.1     skrll 			break;
    650  1.1     skrll 		case CS5:
    651  1.1     skrll 			cfg |= UCYCOM_DATA_BITS_5;
    652  1.1     skrll 			break;
    653  1.1     skrll 		default:
    654  1.1     skrll 			return (EINVAL);
    655  1.1     skrll 		}
    656  1.1     skrll 		cfg |= ISSET(t->c_cflag, CSTOPB) ?
    657  1.1     skrll 		    UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1;
    658  1.1     skrll 		cfg |= ISSET(t->c_cflag, PARENB) ?
    659  1.1     skrll 		    UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF;
    660  1.1     skrll 		cfg |= ISSET(t->c_cflag, PARODD) ?
    661  1.1     skrll 		    UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN;
    662  1.1     skrll 	}
    663  1.1     skrll 
    664  1.1     skrll 	/*
    665  1.1     skrll 	 * Update the tty layer's idea of the carrier bit, in case we changed
    666  1.1     skrll 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
    667  1.1     skrll 	 * explicit request.
    668  1.1     skrll 	 */
    669  1.1     skrll 	DPRINTF(("ucycomparam: l_modem\n"));
    670  1.1     skrll 	(void) (*tp->t_linesw->l_modem)(tp, 1 /* XXX carrier */ );
    671  1.1     skrll 
    672  1.1     skrll 	err = ucycom_configure(sc, baud, cfg);
    673  1.1     skrll 	return (err);
    674  1.1     skrll }
    675  1.1     skrll 
    676  1.1     skrll void
    677  1.1     skrll ucycomstop(struct tty *tp, int flag)
    678  1.1     skrll {
    679  1.1     skrll 	DPRINTF(("ucycomstop: flag=%d\n", flag));
    680  1.1     skrll }
    681  1.1     skrll 
    682  1.1     skrll int
    683  1.1     skrll ucycomread(dev_t dev, struct uio *uio, int flag)
    684  1.1     skrll {
    685  1.1     skrll 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
    686  1.1     skrll 	struct tty *tp = sc->sc_tty;
    687  1.1     skrll 	int err;
    688  1.1     skrll 
    689  1.1     skrll 	DPRINTF(("ucycomread: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio, flag));
    690  1.1     skrll 	if (sc->sc_dying)
    691  1.1     skrll 		return (EIO);
    692  1.1     skrll 
    693  1.1     skrll 	err = ((*tp->t_linesw->l_read)(tp, uio, flag));
    694  1.1     skrll 	return (err);
    695  1.1     skrll }
    696  1.1     skrll 
    697  1.1     skrll 
    698  1.1     skrll int
    699  1.1     skrll ucycomwrite(dev_t dev, struct uio *uio, int flag)
    700  1.1     skrll {
    701  1.1     skrll 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
    702  1.1     skrll 	struct tty *tp = sc->sc_tty;
    703  1.1     skrll 	int err;
    704  1.1     skrll 
    705  1.1     skrll 	DPRINTF(("ucycomwrite: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio, flag));
    706  1.1     skrll 	if (sc->sc_dying)
    707  1.1     skrll 		return (EIO);
    708  1.1     skrll 
    709  1.1     skrll 	err = ((*tp->t_linesw->l_write)(tp, uio, flag));
    710  1.1     skrll 	return (err);
    711  1.1     skrll }
    712  1.1     skrll 
    713  1.1     skrll struct tty *
    714  1.1     skrll ucycomtty(dev_t dev)
    715  1.1     skrll {
    716  1.1     skrll 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
    717  1.1     skrll 	struct tty *tp = sc->sc_tty;
    718  1.1     skrll 
    719  1.1     skrll 	DPRINTF(("ucycomtty: sc=%p, tp=%p\n", sc, tp));
    720  1.1     skrll 
    721  1.1     skrll 	return (tp);
    722  1.1     skrll }
    723  1.1     skrll 
    724  1.1     skrll int
    725  1.6  christos ucycomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    726  1.1     skrll {
    727  1.1     skrll 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
    728  1.1     skrll 	struct tty *tp = sc->sc_tty;
    729  1.1     skrll 	int err;
    730  1.1     skrll 	int s;
    731  1.1     skrll 
    732  1.1     skrll 	if (sc->sc_dying)
    733  1.1     skrll 		return (EIO);
    734  1.1     skrll 
    735  1.1     skrll 	DPRINTF(("ucycomioctl: sc=%p, tp=%p, data=%p\n", sc, tp, data));
    736  1.1     skrll 
    737  1.6  christos 	err = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    738  1.1     skrll 	if (err != EPASSTHROUGH)
    739  1.1     skrll 		return (err);
    740  1.1     skrll 
    741  1.6  christos 	err = ttioctl(tp, cmd, data, flag, l);
    742  1.1     skrll 	if (err != EPASSTHROUGH)
    743  1.1     skrll 		return (err);
    744  1.1     skrll 
    745  1.1     skrll 	err = 0;
    746  1.1     skrll 
    747  1.1     skrll 	DPRINTF(("ucycomioctl: our cmd=0x%08lx\n", cmd));
    748  1.1     skrll 	s = spltty();
    749  1.1     skrll 
    750  1.1     skrll 	switch (cmd) {
    751  1.1     skrll /*	case TIOCSBRK:
    752  1.1     skrll 		ucycom_break(sc, 1);
    753  1.1     skrll 		break;
    754  1.1     skrll 
    755  1.1     skrll 	case TIOCCBRK:
    756  1.1     skrll 		ucycom_break(sc, 0);
    757  1.1     skrll 		break;
    758  1.1     skrll */
    759  1.1     skrll 	case TIOCSDTR:
    760  1.1     skrll 		ucycom_dtr(sc, 1);
    761  1.1     skrll 		break;
    762  1.1     skrll 
    763  1.1     skrll 	case TIOCCDTR:
    764  1.1     skrll 		ucycom_dtr(sc, 0);
    765  1.1     skrll 		break;
    766  1.1     skrll 
    767  1.1     skrll 	case TIOCGFLAGS:
    768  1.1     skrll 		*(int *)data = sc->sc_swflags;
    769  1.1     skrll 		break;
    770  1.1     skrll 
    771  1.1     skrll 	case TIOCSFLAGS:
    772  1.6  christos 		err = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
    773  1.1     skrll 		if (err)
    774  1.1     skrll 			break;
    775  1.1     skrll 		sc->sc_swflags = *(int *)data;
    776  1.1     skrll 		break;
    777  1.1     skrll 
    778  1.1     skrll 	case TIOCMSET:
    779  1.1     skrll 	case TIOCMBIS:
    780  1.1     skrll 	case TIOCMBIC:
    781  1.1     skrll 		tiocm_to_ucycom(sc, cmd, *(int *)data);
    782  1.1     skrll 		break;
    783  1.1     skrll 
    784  1.1     skrll 	case TIOCMGET:
    785  1.1     skrll 		*(int *)data = ucycom_to_tiocm(sc);
    786  1.1     skrll 		break;
    787  1.1     skrll 
    788  1.1     skrll 	default:
    789  1.1     skrll 		err = EPASSTHROUGH;
    790  1.1     skrll 		break;
    791  1.1     skrll 	}
    792  1.1     skrll 
    793  1.1     skrll 	splx(s);
    794  1.1     skrll 
    795  1.1     skrll 	return (err);
    796  1.1     skrll }
    797  1.1     skrll 
    798  1.1     skrll int
    799  1.6  christos ucycompoll(dev_t dev, int events, struct lwp *l)
    800  1.1     skrll {
    801  1.1     skrll 	struct ucycom_softc *sc = ucycom_cd.cd_devs[UCYCOMUNIT(dev)];
    802  1.1     skrll 	struct tty *tp = sc->sc_tty;
    803  1.1     skrll 	int err;
    804  1.1     skrll 
    805  1.6  christos 	DPRINTF(("ucycompoll: sc=%p, tp=%p, events=%d, lwp=%p\n", sc, tp, events, l));
    806  1.1     skrll 
    807  1.1     skrll 	if (sc->sc_dying)
    808  1.1     skrll 		return (EIO);
    809  1.1     skrll 
    810  1.6  christos 	err = ((*tp->t_linesw->l_poll)(tp, events, l));
    811  1.1     skrll 	return (err);
    812  1.1     skrll }
    813  1.1     skrll 
    814  1.1     skrll Static int
    815  1.1     skrll ucycom_configure(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
    816  1.1     skrll {
    817  1.1     skrll 	uint8_t report[5];
    818  1.1     skrll 	int err;
    819  1.1     skrll 
    820  1.1     skrll 	switch (baud) {
    821  1.1     skrll 	case 600:
    822  1.1     skrll 	case 1200:
    823  1.1     skrll 	case 2400:
    824  1.1     skrll 	case 4800:
    825  1.1     skrll 	case 9600:
    826  1.1     skrll 	case 19200:
    827  1.1     skrll 	case 38400:
    828  1.1     skrll 	case 57600:
    829  1.1     skrll #if 0
    830  1.1     skrll 	/*
    831  1.1     skrll 	 * Stock chips only support standard baud rates in the 600 - 57600
    832  1.1     skrll 	 * range, but higher rates can be achieved using custom firmware.
    833  1.1     skrll 	 */
    834  1.1     skrll 	case 115200:
    835  1.1     skrll 	case 153600:
    836  1.1     skrll 	case 192000:
    837  1.1     skrll #endif
    838  1.1     skrll 		break;
    839  1.1     skrll 	default:
    840  1.1     skrll 		return (EINVAL);
    841  1.1     skrll 	}
    842  1.1     skrll 
    843  1.1     skrll 	DPRINTF(("ucycom_configure: setting %d baud, %d-%c-%d (%d)\n", baud,
    844  1.1     skrll 	    5 + (cfg & UCYCOM_DATA_MASK),
    845  1.1     skrll 	    (cfg & UCYCOM_PARITY_MASK) ?
    846  1.1     skrll 		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
    847  1.1     skrll 	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
    848  1.1     skrll 
    849  1.1     skrll 	report[0] = baud & 0xff;
    850  1.1     skrll 	report[1] = (baud >> 8) & 0xff;
    851  1.1     skrll 	report[2] = (baud >> 16) & 0xff;
    852  1.1     skrll 	report[3] = (baud >> 24) & 0xff;
    853  1.1     skrll 	report[4] = cfg;
    854  1.1     skrll 	err = uhidev_set_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
    855  1.1     skrll 	    report, sc->sc_flen);
    856  1.1     skrll 	if (err != 0) {
    857  1.1     skrll 		DPRINTF(("%s\n", usbd_errstr(err)));
    858  1.1     skrll 		return EIO;
    859  1.1     skrll 	}
    860  1.1     skrll 	sc->sc_baud = baud;
    861  1.1     skrll 	sc->sc_cfg = cfg;
    862  1.1     skrll 
    863  1.1     skrll 	return 0;
    864  1.1     skrll }
    865  1.1     skrll 
    866  1.1     skrll Static void
    867  1.1     skrll ucycom_intr(struct uhidev *addr, void *ibuf, u_int len)
    868  1.1     skrll {
    869  1.1     skrll 	struct ucycom_softc *sc = (struct ucycom_softc *)addr;
    870  1.1     skrll 	struct tty *tp = sc->sc_tty;
    871  1.1     skrll 	int (*rint)(int , struct tty *) = tp->t_linesw->l_rint;
    872  1.1     skrll 	uint8_t *cp = ibuf;
    873  1.1     skrll 	int s, n, st, chg;
    874  1.1     skrll 
    875  1.1     skrll 	/* We understand 8 byte and 32 byte input records */
    876  1.1     skrll 	switch (len) {
    877  1.1     skrll 	case 8:
    878  1.1     skrll 		n = cp[0] & UCYCOM_LMASK;
    879  1.1     skrll 		st = cp[0] & ~UCYCOM_LMASK;
    880  1.1     skrll 		cp++;
    881  1.1     skrll 		break;
    882  1.1     skrll 
    883  1.1     skrll 	case 32:
    884  1.1     skrll 		st = cp[0];
    885  1.1     skrll 		n = cp[1];
    886  1.1     skrll 		cp += 2;
    887  1.1     skrll 		break;
    888  1.1     skrll 
    889  1.1     skrll 	default:
    890  1.1     skrll 		DPRINTFN(3,("ucycom_intr: Unknown input report length\n"));
    891  1.1     skrll 		return;
    892  1.1     skrll 	}
    893  1.1     skrll 
    894  1.1     skrll #ifdef UCYCOM_DEBUG
    895  1.1     skrll 	if (ucycomdebug > 5) {
    896  1.1     skrll 		u_int32_t i;
    897  1.1     skrll 
    898  1.1     skrll 		if (n != 0) {
    899  1.1     skrll 			DPRINTF(("ucycom_intr: ibuf[0..%d) =", n));
    900  1.1     skrll 			for (i = 0; i < n; i++)
    901  1.1     skrll 				DPRINTF((" %02x", cp[i]));
    902  1.1     skrll 			DPRINTF(("\n"));
    903  1.1     skrll 		}
    904  1.1     skrll 	}
    905  1.1     skrll #endif
    906  1.1     skrll 	s = spltty();
    907  1.1     skrll 
    908  1.1     skrll 	/* Give characters to tty layer. */
    909  1.1     skrll 	while (n-- > 0) {
    910  1.1     skrll 		DPRINTFN(7,("ucycom_intr: char=0x%02x\n", *cp));
    911  1.1     skrll 		if ((*rint)(*cp++, tp) == -1) {
    912  1.1     skrll 			/* XXX what should we do? */
    913  1.1     skrll 			printf("%s: lost a character\n", USBDEVNAME(sc->sc_hdev.sc_dev));
    914  1.1     skrll 			break;
    915  1.1     skrll 		}
    916  1.1     skrll 	}
    917  1.1     skrll 	splx(s);
    918  1.1     skrll 	chg = st ^ sc->sc_msr;
    919  1.1     skrll 	sc->sc_msr = st;
    920  1.1     skrll 	if (ISSET(chg, UCYCOM_DCD))
    921  1.1     skrll 		(*tp->t_linesw->l_modem)(tp,
    922  1.1     skrll 		    ISSET(sc->sc_msr, UCYCOM_DCD));
    923  1.1     skrll 
    924  1.1     skrll }
    925  1.1     skrll 
    926  1.1     skrll Static void
    927  1.1     skrll tiocm_to_ucycom(struct ucycom_softc *sc, u_long how, int ttybits)
    928  1.1     skrll {
    929  1.1     skrll 	u_char combits;
    930  1.1     skrll 	u_char before = sc->sc_mcr;
    931  1.1     skrll 
    932  1.1     skrll 	combits = 0;
    933  1.1     skrll 	if (ISSET(ttybits, TIOCM_DTR))
    934  1.1     skrll 		SET(combits, UCYCOM_DTR);
    935  1.1     skrll 	if (ISSET(ttybits, TIOCM_RTS))
    936  1.1     skrll 		SET(combits, UCYCOM_RTS);
    937  1.1     skrll 
    938  1.1     skrll 	switch (how) {
    939  1.1     skrll 	case TIOCMBIC:
    940  1.1     skrll 		CLR(sc->sc_mcr, combits);
    941  1.1     skrll 		break;
    942  1.1     skrll 
    943  1.1     skrll 	case TIOCMBIS:
    944  1.1     skrll 		SET(sc->sc_mcr, combits);
    945  1.1     skrll 		break;
    946  1.1     skrll 
    947  1.1     skrll 	case TIOCMSET:
    948  1.1     skrll 		CLR(sc->sc_mcr, UCYCOM_DTR | UCYCOM_RTS);
    949  1.1     skrll 		SET(sc->sc_mcr, combits);
    950  1.1     skrll 		break;
    951  1.1     skrll 	}
    952  1.1     skrll 	if (before ^ sc->sc_mcr) {
    953  1.1     skrll 		DPRINTF(("tiocm_to_ucycom: something has changed\n"));
    954  1.1     skrll 		ucycom_set_status(sc);
    955  1.1     skrll 	}
    956  1.1     skrll }
    957  1.1     skrll 
    958  1.1     skrll Static int
    959  1.1     skrll ucycom_to_tiocm(struct ucycom_softc *sc)
    960  1.1     skrll {
    961  1.1     skrll 	u_char combits;
    962  1.1     skrll 	int ttybits = 0;
    963  1.1     skrll 
    964  1.1     skrll 	combits = sc->sc_mcr;
    965  1.1     skrll 	if (ISSET(combits, UCYCOM_DTR))
    966  1.1     skrll 		SET(ttybits, TIOCM_DTR);
    967  1.1     skrll 	if (ISSET(combits, UCYCOM_RTS))
    968  1.1     skrll 		SET(ttybits, TIOCM_RTS);
    969  1.1     skrll 
    970  1.1     skrll 	combits = sc->sc_msr;
    971  1.1     skrll 	if (ISSET(combits, UCYCOM_DCD))
    972  1.1     skrll 		SET(ttybits, TIOCM_CD);
    973  1.1     skrll 	if (ISSET(combits, UCYCOM_CTS))
    974  1.1     skrll 		SET(ttybits, TIOCM_CTS);
    975  1.1     skrll 	if (ISSET(combits, UCYCOM_DSR))
    976  1.1     skrll 		SET(ttybits, TIOCM_DSR);
    977  1.1     skrll 	if (ISSET(combits, UCYCOM_RI))
    978  1.1     skrll 		SET(ttybits, TIOCM_RI);
    979  1.1     skrll 
    980  1.1     skrll 	return (ttybits);
    981  1.1     skrll }
    982  1.1     skrll 
    983  1.1     skrll Static void
    984  1.1     skrll ucycom_dtr(struct ucycom_softc *sc, int set)
    985  1.1     skrll {
    986  1.1     skrll 	uint8_t old;
    987  1.1     skrll 
    988  1.1     skrll 	old = sc->sc_mcr;
    989  1.1     skrll 	if (set)
    990  1.1     skrll 		SET(sc->sc_mcr, UCYCOM_DTR);
    991  1.1     skrll 	else
    992  1.1     skrll 		CLR(sc->sc_mcr, UCYCOM_DTR);
    993  1.1     skrll 
    994  1.1     skrll 	if (old ^ sc->sc_mcr)
    995  1.1     skrll 		ucycom_set_status(sc);
    996  1.1     skrll }
    997  1.1     skrll 
    998  1.1     skrll #if 0
    999  1.1     skrll Static void
   1000  1.1     skrll ucycom_rts(struct ucycom_softc *sc, int set)
   1001  1.1     skrll {
   1002  1.1     skrll 	uint8_t old;
   1003  1.1     skrll 
   1004  1.1     skrll 	old = sc->sc_msr;
   1005  1.1     skrll 	if (set)
   1006  1.1     skrll 		SET(sc->sc_mcr, UCYCOM_RTS);
   1007  1.1     skrll 	else
   1008  1.1     skrll 		CLR(sc->sc_mcr, UCYCOM_RTS);
   1009  1.1     skrll 
   1010  1.1     skrll 	if (old ^ sc->sc_mcr)
   1011  1.1     skrll 		ucycom_set_status(sc);
   1012  1.1     skrll }
   1013  1.1     skrll #endif
   1014  1.1     skrll 
   1015  1.1     skrll Static void
   1016  1.1     skrll ucycom_set_status(struct ucycom_softc *sc)
   1017  1.1     skrll {
   1018  1.1     skrll 	int err;
   1019  1.1     skrll 
   1020  1.1     skrll 	if (sc->sc_olen != 8 && sc->sc_olen != 32) {
   1021  1.2     skrll 		DPRINTFN(2,("ucycom_set_status: unknown output report size (%zd)\n",
   1022  1.1     skrll 		    sc->sc_olen));
   1023  1.1     skrll 		return;
   1024  1.1     skrll 	}
   1025  1.1     skrll 
   1026  1.1     skrll 	DPRINTF(("ucycom_set_status: %d\n", sc->sc_mcr));
   1027  1.1     skrll 
   1028  1.1     skrll 	memset(sc->sc_obuf, 0, sc->sc_olen);
   1029  1.1     skrll 	sc->sc_obuf[0] = sc->sc_mcr;
   1030  1.1     skrll 
   1031  1.1     skrll 	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
   1032  1.1     skrll 	if (err)
   1033  1.1     skrll 		DPRINTF(("ucycom_set_status: err=%d\n", err));
   1034  1.1     skrll }
   1035  1.1     skrll 
   1036  1.1     skrll #ifdef UCYCOM_DEBUG
   1037  1.1     skrll Static void
   1038  1.1     skrll ucycom_get_cfg(struct ucycom_softc *sc)
   1039  1.1     skrll {
   1040  1.1     skrll 	int err, cfg, baud;
   1041  1.1     skrll 	uint8_t report[5];
   1042  1.1     skrll 
   1043  1.1     skrll 	err = uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
   1044  1.1     skrll 	    report, sc->sc_flen);
   1045  1.1     skrll 	cfg = report[4];
   1046  1.1     skrll 	baud = (report[3] << 24) + (report[2] << 16) + (report[1] << 8) + report[0];
   1047  1.1     skrll 	DPRINTF(("ucycom_configure: device reports %d baud, %d-%c-%d (%d)\n", baud,
   1048  1.1     skrll 	    5 + (cfg & UCYCOM_DATA_MASK),
   1049  1.1     skrll 	    (cfg & UCYCOM_PARITY_MASK) ?
   1050  1.1     skrll 		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
   1051  1.1     skrll 	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
   1052  1.1     skrll }
   1053  1.1     skrll #endif
   1054  1.1     skrll 
   1055  1.1     skrll Static void
   1056  1.1     skrll ucycom_cleanup(struct ucycom_softc *sc)
   1057  1.1     skrll {
   1058  1.1     skrll 	DPRINTF(("ucycom_cleanup: closing uhidev\n"));
   1059  1.1     skrll 
   1060  1.1     skrll 	if (sc->sc_obuf !=NULL)
   1061  1.1     skrll 		free (sc->sc_obuf, M_USBDEV);
   1062  1.1     skrll 	uhidev_close(&sc->sc_hdev);
   1063  1.1     skrll }
   1064