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