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