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