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