Home | History | Annotate | Line # | Download | only in usb
ucom.c revision 1.102
      1 /*	$NetBSD: ucom.c,v 1.102 2012/12/15 04:10:05 jakllsch Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 /*
     33  * This code is very heavily based on the 16550 driver, com.c.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: ucom.c,v 1.102 2012/12/15 04:10:05 jakllsch Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/conf.h>
     44 #include <sys/tty.h>
     45 #include <sys/file.h>
     46 #include <sys/select.h>
     47 #include <sys/proc.h>
     48 #include <sys/vnode.h>
     49 #include <sys/device.h>
     50 #include <sys/poll.h>
     51 #include <sys/queue.h>
     52 #include <sys/kauth.h>
     53 #include <sys/rnd.h>
     54 
     55 #include <dev/usb/usb.h>
     56 
     57 #include <dev/usb/usbdi.h>
     58 #include <dev/usb/usbdi_util.h>
     59 #include <dev/usb/usbdevs.h>
     60 #include <dev/usb/usb_quirks.h>
     61 
     62 #include <dev/usb/ucomvar.h>
     63 
     64 #include "ucom.h"
     65 
     66 #include "locators.h"
     67 
     68 #if NUCOM > 0
     69 
     70 #ifdef UCOM_DEBUG
     71 #define DPRINTFN(n, x)	if (ucomdebug > (n)) printf x
     72 int ucomdebug = 0;
     73 #else
     74 #define DPRINTFN(n, x)
     75 #endif
     76 #define DPRINTF(x) DPRINTFN(0, x)
     77 
     78 #define	UCOMUNIT_MASK		0x3ffff
     79 #define	UCOMDIALOUT_MASK	0x80000
     80 #define	UCOMCALLUNIT_MASK	0x40000
     81 
     82 #define	UCOMUNIT(x)		(minor(x) & UCOMUNIT_MASK)
     83 #define	UCOMDIALOUT(x)		(minor(x) & UCOMDIALOUT_MASK)
     84 #define	UCOMCALLUNIT(x)		(minor(x) & UCOMCALLUNIT_MASK)
     85 
     86 /*
     87  * XXX: We can submit multiple input/output buffers to the usb stack
     88  * to improve throughput, but the usb stack is too lame to deal with this
     89  * in a number of places.
     90  */
     91 #define	UCOM_IN_BUFFS	1
     92 #define	UCOM_OUT_BUFFS	1
     93 
     94 struct ucom_buffer {
     95 	SIMPLEQ_ENTRY(ucom_buffer) ub_link;
     96 	usbd_xfer_handle ub_xfer;
     97 	u_char *ub_data;
     98 	u_int ub_len;
     99 	u_int ub_index;
    100 };
    101 
    102 struct ucom_softc {
    103 	device_t		sc_dev;		/* base device */
    104 
    105 	usbd_device_handle	sc_udev;	/* USB device */
    106 
    107 	usbd_interface_handle	sc_iface;	/* data interface */
    108 
    109 	int			sc_bulkin_no;	/* bulk in endpoint address */
    110 	usbd_pipe_handle	sc_bulkin_pipe;	/* bulk in pipe */
    111 	u_int			sc_ibufsize;	/* read buffer size */
    112 	u_int			sc_ibufsizepad;	/* read buffer size padded */
    113 	struct ucom_buffer	sc_ibuff[UCOM_IN_BUFFS];
    114 	SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_empty;
    115 	SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_full;
    116 
    117 	int			sc_bulkout_no;	/* bulk out endpoint address */
    118 	usbd_pipe_handle	sc_bulkout_pipe;/* bulk out pipe */
    119 	u_int			sc_obufsize;	/* write buffer size */
    120 	u_int			sc_opkthdrlen;	/* header length of */
    121 	struct ucom_buffer	sc_obuff[UCOM_OUT_BUFFS];
    122 	SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_free;
    123 	SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_full;
    124 
    125 	void			*sc_si;
    126 
    127 	const struct ucom_methods *sc_methods;
    128 	void                    *sc_parent;
    129 	int			sc_portno;
    130 
    131 	struct tty		*sc_tty;	/* our tty */
    132 	u_char			sc_lsr;
    133 	u_char			sc_msr;
    134 	u_char			sc_mcr;
    135 	volatile u_char		sc_rx_stopped;
    136 	u_char			sc_rx_unblock;
    137 	u_char			sc_tx_stopped;
    138 	int			sc_swflags;
    139 
    140 	u_char			sc_opening;	/* lock during open */
    141 	int			sc_refcnt;
    142 	u_char			sc_dying;	/* disconnecting */
    143 
    144 	krndsource_t	sc_rndsource;	/* random source */
    145 };
    146 
    147 dev_type_open(ucomopen);
    148 dev_type_close(ucomclose);
    149 dev_type_read(ucomread);
    150 dev_type_write(ucomwrite);
    151 dev_type_ioctl(ucomioctl);
    152 dev_type_stop(ucomstop);
    153 dev_type_tty(ucomtty);
    154 dev_type_poll(ucompoll);
    155 
    156 const struct cdevsw ucom_cdevsw = {
    157 	ucomopen, ucomclose, ucomread, ucomwrite, ucomioctl,
    158 	ucomstop, ucomtty, ucompoll, nommap, ttykqfilter, D_TTY
    159 };
    160 
    161 static void	ucom_cleanup(struct ucom_softc *);
    162 static int	ucomparam(struct tty *, struct termios *);
    163 static int	ucomhwiflow(struct tty *, int);
    164 static void	ucomstart(struct tty *);
    165 static void	ucom_shutdown(struct ucom_softc *);
    166 static int	ucom_do_ioctl(struct ucom_softc *, u_long, void *,
    167 			      int, struct lwp *);
    168 static void	ucom_dtr(struct ucom_softc *, int);
    169 static void	ucom_rts(struct ucom_softc *, int);
    170 static void	ucom_break(struct ucom_softc *, int);
    171 static void	tiocm_to_ucom(struct ucom_softc *, u_long, int);
    172 static int	ucom_to_tiocm(struct ucom_softc *);
    173 
    174 static void	ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
    175 static void	ucom_submit_write(struct ucom_softc *, struct ucom_buffer *);
    176 static void	ucom_write_status(struct ucom_softc *, struct ucom_buffer *,
    177 			usbd_status);
    178 
    179 static void	ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
    180 static void	ucom_read_complete(struct ucom_softc *);
    181 static usbd_status ucomsubmitread(struct ucom_softc *, struct ucom_buffer *);
    182 static void	ucom_softintr(void *);
    183 
    184 int ucom_match(device_t, cfdata_t, void *);
    185 void ucom_attach(device_t, device_t, void *);
    186 int ucom_detach(device_t, int);
    187 int ucom_activate(device_t, enum devact);
    188 extern struct cfdriver ucom_cd;
    189 CFATTACH_DECL_NEW(ucom, sizeof(struct ucom_softc), ucom_match, ucom_attach,
    190     ucom_detach, ucom_activate);
    191 
    192 int
    193 ucom_match(device_t parent, cfdata_t match, void *aux)
    194 {
    195 	return (1);
    196 }
    197 
    198 void
    199 ucom_attach(device_t parent, device_t self, void *aux)
    200 {
    201 	struct ucom_softc *sc = device_private(self);
    202 	struct ucom_attach_args *uca = aux;
    203 	struct tty *tp;
    204 
    205 	if (uca->info != NULL)
    206 		aprint_normal(": %s", uca->info);
    207 	aprint_normal("\n");
    208 
    209 	sc->sc_dev = self;
    210 	sc->sc_udev = uca->device;
    211 	sc->sc_iface = uca->iface;
    212 	sc->sc_bulkout_no = uca->bulkout;
    213 	sc->sc_bulkin_no = uca->bulkin;
    214 	sc->sc_ibufsize = uca->ibufsize;
    215 	sc->sc_ibufsizepad = uca->ibufsizepad;
    216 	sc->sc_obufsize = uca->obufsize;
    217 	sc->sc_opkthdrlen = uca->opkthdrlen;
    218 	sc->sc_methods = uca->methods;
    219 	sc->sc_parent = uca->arg;
    220 	sc->sc_portno = uca->portno;
    221 
    222 	sc->sc_lsr = 0;
    223 	sc->sc_msr = 0;
    224 	sc->sc_mcr = 0;
    225 	sc->sc_tx_stopped = 0;
    226 	sc->sc_swflags = 0;
    227 	sc->sc_opening = 0;
    228 	sc->sc_refcnt = 0;
    229 	sc->sc_dying = 0;
    230 
    231 	sc->sc_si = softint_establish(SOFTINT_NET, ucom_softintr, sc);
    232 
    233 	tp = tty_alloc();
    234 	tp->t_oproc = ucomstart;
    235 	tp->t_param = ucomparam;
    236 	tp->t_hwiflow = ucomhwiflow;
    237 	sc->sc_tty = tp;
    238 
    239 	DPRINTF(("ucom_attach: tty_attach %p\n", tp));
    240 	tty_attach(tp);
    241 
    242 	rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev),
    243 			  RND_TYPE_TTY, 0);
    244 
    245 	if (!pmf_device_register(self, NULL, NULL))
    246 		aprint_error_dev(self, "couldn't establish power handler\n");
    247 	return;
    248 }
    249 
    250 int
    251 ucom_detach(device_t self, int flags)
    252 {
    253 	struct ucom_softc *sc = device_private(self);
    254 	struct tty *tp = sc->sc_tty;
    255 	int maj, mn;
    256 	int s, i;
    257 
    258 	DPRINTF(("ucom_detach: sc=%p flags=%d tp=%p, pipe=%d,%d\n",
    259 		 sc, flags, tp, sc->sc_bulkin_no, sc->sc_bulkout_no));
    260 
    261 	sc->sc_dying = 1;
    262 	pmf_device_deregister(self);
    263 
    264 	if (sc->sc_bulkin_pipe != NULL)
    265 		usbd_abort_pipe(sc->sc_bulkin_pipe);
    266 	if (sc->sc_bulkout_pipe != NULL)
    267 		usbd_abort_pipe(sc->sc_bulkout_pipe);
    268 
    269 	s = splusb();
    270 	if (--sc->sc_refcnt >= 0) {
    271 		/* Wake up anyone waiting */
    272 		if (tp != NULL) {
    273 			mutex_spin_enter(&tty_lock);
    274 			CLR(tp->t_state, TS_CARR_ON);
    275 			CLR(tp->t_cflag, CLOCAL | MDMBUF);
    276 			ttyflush(tp, FREAD|FWRITE);
    277 			mutex_spin_exit(&tty_lock);
    278 		}
    279 		/* Wait for processes to go away. */
    280 		usb_detach_waitold(sc->sc_dev);
    281 	}
    282 
    283 	softint_disestablish(sc->sc_si);
    284 	splx(s);
    285 
    286 	/* locate the major number */
    287 	maj = cdevsw_lookup_major(&ucom_cdevsw);
    288 
    289 	/* Nuke the vnodes for any open instances. */
    290 	mn = device_unit(self);
    291 	DPRINTF(("ucom_detach: maj=%d mn=%d\n", maj, mn));
    292 	vdevgone(maj, mn, mn, VCHR);
    293 	vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR);
    294 	vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR);
    295 
    296 	/* Detach and free the tty. */
    297 	if (tp != NULL) {
    298 		tty_detach(tp);
    299 		tty_free(tp);
    300 		sc->sc_tty = NULL;
    301 	}
    302 
    303 	for (i = 0; i < UCOM_IN_BUFFS; i++) {
    304 		if (sc->sc_ibuff[i].ub_xfer != NULL)
    305 			usbd_free_xfer(sc->sc_ibuff[i].ub_xfer);
    306 	}
    307 
    308 	for (i = 0; i < UCOM_OUT_BUFFS; i++) {
    309 		if (sc->sc_obuff[i].ub_xfer != NULL)
    310 			usbd_free_xfer(sc->sc_obuff[i].ub_xfer);
    311 	}
    312 
    313 	/* Detach the random source */
    314 	rnd_detach_source(&sc->sc_rndsource);
    315 
    316 	return (0);
    317 }
    318 
    319 int
    320 ucom_activate(device_t self, enum devact act)
    321 {
    322 	struct ucom_softc *sc = device_private(self);
    323 
    324 	DPRINTFN(5,("ucom_activate: %d\n", act));
    325 
    326 	switch (act) {
    327 	case DVACT_DEACTIVATE:
    328 		sc->sc_dying = 1;
    329 		return 0;
    330 	default:
    331 		return EOPNOTSUPP;
    332 	}
    333 }
    334 
    335 void
    336 ucom_shutdown(struct ucom_softc *sc)
    337 {
    338 	struct tty *tp = sc->sc_tty;
    339 
    340 	DPRINTF(("ucom_shutdown\n"));
    341 	/*
    342 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    343 	 * notice even if we immediately open the port again.
    344 	 */
    345 	if (ISSET(tp->t_cflag, HUPCL)) {
    346 		ucom_dtr(sc, 0);
    347 		(void)tsleep(sc, TTIPRI, ttclos, hz);
    348 	}
    349 }
    350 
    351 int
    352 ucomopen(dev_t dev, int flag, int mode, struct lwp *l)
    353 {
    354 	int unit = UCOMUNIT(dev);
    355 	usbd_status err;
    356 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit);
    357 	struct ucom_buffer *ub;
    358 	struct tty *tp;
    359 	int s, i;
    360 	int error;
    361 
    362 	/* XXX This is a hopefully temporary stopgap for kern/42848. */
    363 	if ((flag & (FREAD|FWRITE)) != (FREAD|FWRITE))
    364 		return (EINVAL);
    365 
    366 	if (sc == NULL)
    367 		return (ENXIO);
    368 
    369 	if (sc->sc_dying)
    370 		return (EIO);
    371 
    372 	if (!device_is_active(sc->sc_dev))
    373 		return (ENXIO);
    374 
    375 	tp = sc->sc_tty;
    376 
    377 	DPRINTF(("ucomopen: unit=%d, tp=%p\n", unit, tp));
    378 
    379 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    380 		return (EBUSY);
    381 
    382 	s = spltty();
    383 
    384 	/*
    385 	 * Do the following iff this is a first open.
    386 	 */
    387 	while (sc->sc_opening)
    388 		tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
    389 
    390 	if (sc->sc_dying) {
    391 		splx(s);
    392 		return (EIO);
    393 	}
    394 	sc->sc_opening = 1;
    395 
    396 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    397 		struct termios t;
    398 
    399 		tp->t_dev = dev;
    400 
    401 		if (sc->sc_methods->ucom_open != NULL) {
    402 			error = sc->sc_methods->ucom_open(sc->sc_parent,
    403 							  sc->sc_portno);
    404 			if (error) {
    405 				ucom_cleanup(sc);
    406 				sc->sc_opening = 0;
    407 				wakeup(&sc->sc_opening);
    408 				splx(s);
    409 				return (error);
    410 			}
    411 		}
    412 
    413 		ucom_status_change(sc);
    414 
    415 		/*
    416 		 * Initialize the termios status to the defaults.  Add in the
    417 		 * sticky bits from TIOCSFLAGS.
    418 		 */
    419 		t.c_ispeed = 0;
    420 		t.c_ospeed = TTYDEF_SPEED;
    421 		t.c_cflag = TTYDEF_CFLAG;
    422 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    423 			SET(t.c_cflag, CLOCAL);
    424 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    425 			SET(t.c_cflag, CRTSCTS);
    426 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    427 			SET(t.c_cflag, MDMBUF);
    428 		/* Make sure ucomparam() will do something. */
    429 		tp->t_ospeed = 0;
    430 		(void) ucomparam(tp, &t);
    431 		tp->t_iflag = TTYDEF_IFLAG;
    432 		tp->t_oflag = TTYDEF_OFLAG;
    433 		tp->t_lflag = TTYDEF_LFLAG;
    434 		ttychars(tp);
    435 		ttsetwater(tp);
    436 
    437 		/*
    438 		 * Turn on DTR.  We must always do this, even if carrier is not
    439 		 * present, because otherwise we'd have to use TIOCSDTR
    440 		 * immediately after setting CLOCAL, which applications do not
    441 		 * expect.  We always assert DTR while the device is open
    442 		 * unless explicitly requested to deassert it.  Ditto RTS.
    443 		 */
    444 		ucom_dtr(sc, 1);
    445 		ucom_rts(sc, 1);
    446 
    447 		DPRINTF(("ucomopen: open pipes in=%d out=%d\n",
    448 			 sc->sc_bulkin_no, sc->sc_bulkout_no));
    449 
    450 		/* Open the bulk pipes */
    451 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no,
    452 				     USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
    453 		if (err) {
    454 			DPRINTF(("%s: open bulk in error (addr %d), err=%s\n",
    455 				 device_xname(sc->sc_dev), sc->sc_bulkin_no,
    456 				 usbd_errstr(err)));
    457 			error = EIO;
    458 			goto fail_0;
    459 		}
    460 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
    461 				     USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
    462 		if (err) {
    463 			DPRINTF(("%s: open bulk out error (addr %d), err=%s\n",
    464 				 device_xname(sc->sc_dev), sc->sc_bulkout_no,
    465 				 usbd_errstr(err)));
    466 			error = EIO;
    467 			goto fail_1;
    468 		}
    469 
    470 		sc->sc_rx_unblock = 0;
    471 		sc->sc_rx_stopped = 0;
    472 		sc->sc_tx_stopped = 0;
    473 
    474 		memset(sc->sc_ibuff, 0, sizeof(sc->sc_ibuff));
    475 		memset(sc->sc_obuff, 0, sizeof(sc->sc_obuff));
    476 
    477 		SIMPLEQ_INIT(&sc->sc_ibuff_empty);
    478 		SIMPLEQ_INIT(&sc->sc_ibuff_full);
    479 		SIMPLEQ_INIT(&sc->sc_obuff_free);
    480 		SIMPLEQ_INIT(&sc->sc_obuff_full);
    481 
    482 		/* Allocate input buffers */
    483 		for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
    484 		    ub++) {
    485 			ub->ub_xfer = usbd_alloc_xfer(sc->sc_udev);
    486 			if (ub->ub_xfer == NULL) {
    487 				error = ENOMEM;
    488 				goto fail_2;
    489 			}
    490 			ub->ub_data = usbd_alloc_buffer(ub->ub_xfer,
    491 			    sc->sc_ibufsizepad);
    492 			if (ub->ub_data == NULL) {
    493 				error = ENOMEM;
    494 				goto fail_2;
    495 			}
    496 
    497 			if (ucomsubmitread(sc, ub) != USBD_NORMAL_COMPLETION) {
    498 				error = EIO;
    499 				goto fail_2;
    500 			}
    501 		}
    502 
    503 		for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
    504 		    ub++) {
    505 			ub->ub_xfer = usbd_alloc_xfer(sc->sc_udev);
    506 			if (ub->ub_xfer == NULL) {
    507 				error = ENOMEM;
    508 				goto fail_2;
    509 			}
    510 			ub->ub_data = usbd_alloc_buffer(ub->ub_xfer,
    511 			    sc->sc_obufsize);
    512 			if (ub->ub_data == NULL) {
    513 				error = ENOMEM;
    514 				goto fail_2;
    515 			}
    516 
    517 			SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
    518 		}
    519 
    520 	}
    521 	sc->sc_opening = 0;
    522 	wakeup(&sc->sc_opening);
    523 	splx(s);
    524 
    525 	error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    526 	if (error)
    527 		goto bad;
    528 
    529 	error = (*tp->t_linesw->l_open)(dev, tp);
    530 	if (error)
    531 		goto bad;
    532 
    533 	return (0);
    534 
    535 fail_2:
    536 	usbd_abort_pipe(sc->sc_bulkin_pipe);
    537 	for (i = 0; i < UCOM_IN_BUFFS; i++) {
    538 		if (sc->sc_ibuff[i].ub_xfer != NULL) {
    539 			usbd_free_xfer(sc->sc_ibuff[i].ub_xfer);
    540 			sc->sc_ibuff[i].ub_xfer = NULL;
    541 			sc->sc_ibuff[i].ub_data = NULL;
    542 		}
    543 	}
    544 	usbd_abort_pipe(sc->sc_bulkout_pipe);
    545 	for (i = 0; i < UCOM_OUT_BUFFS; i++) {
    546 		if (sc->sc_obuff[i].ub_xfer != NULL) {
    547 			usbd_free_xfer(sc->sc_obuff[i].ub_xfer);
    548 			sc->sc_obuff[i].ub_xfer = NULL;
    549 			sc->sc_obuff[i].ub_data = NULL;
    550 		}
    551 	}
    552 
    553 	usbd_close_pipe(sc->sc_bulkout_pipe);
    554 	sc->sc_bulkout_pipe = NULL;
    555 fail_1:
    556 	usbd_close_pipe(sc->sc_bulkin_pipe);
    557 	sc->sc_bulkin_pipe = NULL;
    558 fail_0:
    559 	sc->sc_opening = 0;
    560 	wakeup(&sc->sc_opening);
    561 	splx(s);
    562 	return (error);
    563 
    564 bad:
    565 	s = spltty();
    566 	CLR(tp->t_state, TS_BUSY);
    567 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    568 		/*
    569 		 * We failed to open the device, and nobody else had it opened.
    570 		 * Clean up the state as appropriate.
    571 		 */
    572 		ucom_cleanup(sc);
    573 	}
    574 	splx(s);
    575 
    576 	return (error);
    577 }
    578 
    579 int
    580 ucomclose(dev_t dev, int flag, int mode, struct lwp *l)
    581 {
    582 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    583 	struct tty *tp;
    584 	int s;
    585 
    586 	DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev)));
    587 
    588 	if (sc == NULL)
    589 		return 0;
    590 
    591 	tp = sc->sc_tty;
    592 
    593 	if (!ISSET(tp->t_state, TS_ISOPEN))
    594 		return (0);
    595 
    596 	s = spltty();
    597 	sc->sc_refcnt++;
    598 
    599 	(*tp->t_linesw->l_close)(tp, flag);
    600 	ttyclose(tp);
    601 
    602 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    603 		/*
    604 		 * Although we got a last close, the device may still be in
    605 		 * use; e.g. if this was the dialout node, and there are still
    606 		 * processes waiting for carrier on the non-dialout node.
    607 		 */
    608 		ucom_cleanup(sc);
    609 	}
    610 
    611 	if (sc->sc_methods->ucom_close != NULL)
    612 		sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
    613 
    614 	if (--sc->sc_refcnt < 0)
    615 		usb_detach_wakeupold(sc->sc_dev);
    616 	splx(s);
    617 
    618 	return (0);
    619 }
    620 
    621 int
    622 ucomread(dev_t dev, struct uio *uio, int flag)
    623 {
    624 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    625 	struct tty *tp;
    626 	int error;
    627 
    628 	if (sc == NULL || sc->sc_dying)
    629 		return (EIO);
    630 
    631 	tp = sc->sc_tty;
    632 
    633 	sc->sc_refcnt++;
    634 	error = ((*tp->t_linesw->l_read)(tp, uio, flag));
    635 	if (--sc->sc_refcnt < 0)
    636 		usb_detach_wakeupold(sc->sc_dev);
    637 	return (error);
    638 }
    639 
    640 int
    641 ucomwrite(dev_t dev, struct uio *uio, int flag)
    642 {
    643 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    644 	struct tty *tp;
    645 	int error;
    646 
    647 	if (sc == NULL || sc->sc_dying)
    648 		return (EIO);
    649 
    650 	tp = sc->sc_tty;
    651 
    652 	sc->sc_refcnt++;
    653 	error = ((*tp->t_linesw->l_write)(tp, uio, flag));
    654 	if (--sc->sc_refcnt < 0)
    655 		usb_detach_wakeupold(sc->sc_dev);
    656 	return (error);
    657 }
    658 
    659 int
    660 ucompoll(dev_t dev, int events, struct lwp *l)
    661 {
    662 	struct ucom_softc *sc;
    663 	struct tty *tp;
    664 	int revents;
    665 
    666 	sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    667 	if (sc == NULL || sc->sc_dying)
    668 		return (POLLHUP);
    669 
    670 	tp = sc->sc_tty;
    671 
    672 	sc->sc_refcnt++;
    673 	revents = ((*tp->t_linesw->l_poll)(tp, events, l));
    674 	if (--sc->sc_refcnt < 0)
    675 		usb_detach_wakeupold(sc->sc_dev);
    676 	return (revents);
    677 }
    678 
    679 struct tty *
    680 ucomtty(dev_t dev)
    681 {
    682 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    683 
    684 	return ((sc != NULL) ? sc->sc_tty : NULL);
    685 }
    686 
    687 int
    688 ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    689 {
    690 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    691 	int error;
    692 
    693 	if (sc == NULL || sc->sc_dying)
    694 		return (EIO);
    695 
    696 	sc->sc_refcnt++;
    697 	error = ucom_do_ioctl(sc, cmd, data, flag, l);
    698 	if (--sc->sc_refcnt < 0)
    699 		usb_detach_wakeupold(sc->sc_dev);
    700 	return (error);
    701 }
    702 
    703 static int
    704 ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, void *data,
    705 	      int flag, struct lwp *l)
    706 {
    707 	struct tty *tp = sc->sc_tty;
    708 	int error;
    709 	int s;
    710 
    711 	DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd));
    712 
    713 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    714 	if (error != EPASSTHROUGH)
    715 		return (error);
    716 
    717 	error = ttioctl(tp, cmd, data, flag, l);
    718 	if (error != EPASSTHROUGH)
    719 		return (error);
    720 
    721 	if (sc->sc_methods->ucom_ioctl != NULL) {
    722 		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
    723 			    sc->sc_portno, cmd, data, flag, l->l_proc);
    724 		if (error != EPASSTHROUGH)
    725 			return (error);
    726 	}
    727 
    728 	error = 0;
    729 
    730 	DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd));
    731 	s = spltty();
    732 
    733 	switch (cmd) {
    734 	case TIOCSBRK:
    735 		ucom_break(sc, 1);
    736 		break;
    737 
    738 	case TIOCCBRK:
    739 		ucom_break(sc, 0);
    740 		break;
    741 
    742 	case TIOCSDTR:
    743 		ucom_dtr(sc, 1);
    744 		break;
    745 
    746 	case TIOCCDTR:
    747 		ucom_dtr(sc, 0);
    748 		break;
    749 
    750 	case TIOCGFLAGS:
    751 		*(int *)data = sc->sc_swflags;
    752 		break;
    753 
    754 	case TIOCSFLAGS:
    755 		error = kauth_authorize_device_tty(l->l_cred,
    756 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    757 		if (error)
    758 			break;
    759 		sc->sc_swflags = *(int *)data;
    760 		break;
    761 
    762 	case TIOCMSET:
    763 	case TIOCMBIS:
    764 	case TIOCMBIC:
    765 		tiocm_to_ucom(sc, cmd, *(int *)data);
    766 		break;
    767 
    768 	case TIOCMGET:
    769 		*(int *)data = ucom_to_tiocm(sc);
    770 		break;
    771 
    772 	default:
    773 		error = EPASSTHROUGH;
    774 		break;
    775 	}
    776 
    777 	splx(s);
    778 
    779 	return (error);
    780 }
    781 
    782 static void
    783 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
    784 {
    785 	u_char combits;
    786 
    787 	combits = 0;
    788 	if (ISSET(ttybits, TIOCM_DTR))
    789 		SET(combits, UMCR_DTR);
    790 	if (ISSET(ttybits, TIOCM_RTS))
    791 		SET(combits, UMCR_RTS);
    792 
    793 	switch (how) {
    794 	case TIOCMBIC:
    795 		CLR(sc->sc_mcr, combits);
    796 		break;
    797 
    798 	case TIOCMBIS:
    799 		SET(sc->sc_mcr, combits);
    800 		break;
    801 
    802 	case TIOCMSET:
    803 		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
    804 		SET(sc->sc_mcr, combits);
    805 		break;
    806 	}
    807 
    808 	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
    809 		ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
    810 	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
    811 		ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
    812 }
    813 
    814 static int
    815 ucom_to_tiocm(struct ucom_softc *sc)
    816 {
    817 	u_char combits;
    818 	int ttybits = 0;
    819 
    820 	combits = sc->sc_mcr;
    821 	if (ISSET(combits, UMCR_DTR))
    822 		SET(ttybits, TIOCM_DTR);
    823 	if (ISSET(combits, UMCR_RTS))
    824 		SET(ttybits, TIOCM_RTS);
    825 
    826 	combits = sc->sc_msr;
    827 	if (ISSET(combits, UMSR_DCD))
    828 		SET(ttybits, TIOCM_CD);
    829 	if (ISSET(combits, UMSR_CTS))
    830 		SET(ttybits, TIOCM_CTS);
    831 	if (ISSET(combits, UMSR_DSR))
    832 		SET(ttybits, TIOCM_DSR);
    833 	if (ISSET(combits, UMSR_RI | UMSR_TERI))
    834 		SET(ttybits, TIOCM_RI);
    835 
    836 #if 0
    837 XXX;
    838 	if (sc->sc_ier != 0)
    839 		SET(ttybits, TIOCM_LE);
    840 #endif
    841 
    842 	return (ttybits);
    843 }
    844 
    845 static void
    846 ucom_break(struct ucom_softc *sc, int onoff)
    847 {
    848 	DPRINTF(("ucom_break: onoff=%d\n", onoff));
    849 
    850 	if (sc->sc_methods->ucom_set != NULL)
    851 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    852 		    UCOM_SET_BREAK, onoff);
    853 }
    854 
    855 static void
    856 ucom_dtr(struct ucom_softc *sc, int onoff)
    857 {
    858 	DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
    859 
    860 	if (sc->sc_methods->ucom_set != NULL)
    861 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    862 		    UCOM_SET_DTR, onoff);
    863 }
    864 
    865 static void
    866 ucom_rts(struct ucom_softc *sc, int onoff)
    867 {
    868 	DPRINTF(("ucom_rts: onoff=%d\n", onoff));
    869 
    870 	if (sc->sc_methods->ucom_set != NULL)
    871 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    872 		    UCOM_SET_RTS, onoff);
    873 }
    874 
    875 void
    876 ucom_status_change(struct ucom_softc *sc)
    877 {
    878 	struct tty *tp = sc->sc_tty;
    879 	u_char old_msr;
    880 
    881 	if (sc->sc_methods->ucom_get_status != NULL) {
    882 		old_msr = sc->sc_msr;
    883 		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
    884 		    &sc->sc_lsr, &sc->sc_msr);
    885 		if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD))
    886 			(*tp->t_linesw->l_modem)(tp,
    887 			    ISSET(sc->sc_msr, UMSR_DCD));
    888 	} else {
    889 		sc->sc_lsr = 0;
    890 		/* Assume DCD is present, if we have no chance to check it. */
    891 		sc->sc_msr = UMSR_DCD;
    892 	}
    893 }
    894 
    895 static int
    896 ucomparam(struct tty *tp, struct termios *t)
    897 {
    898 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
    899 	    UCOMUNIT(tp->t_dev));
    900 	int error;
    901 
    902 	if (sc == NULL || sc->sc_dying)
    903 		return (EIO);
    904 
    905 	/* Check requested parameters. */
    906 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    907 		return (EINVAL);
    908 
    909 	/*
    910 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    911 	 * is always active.
    912 	 */
    913 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
    914 		SET(t->c_cflag, CLOCAL);
    915 		CLR(t->c_cflag, HUPCL);
    916 	}
    917 
    918 	/*
    919 	 * If there were no changes, don't do anything.  This avoids dropping
    920 	 * input and improves performance when all we did was frob things like
    921 	 * VMIN and VTIME.
    922 	 */
    923 	if (tp->t_ospeed == t->c_ospeed &&
    924 	    tp->t_cflag == t->c_cflag)
    925 		return (0);
    926 
    927 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
    928 
    929 	/* And copy to tty. */
    930 	tp->t_ispeed = 0;
    931 	tp->t_ospeed = t->c_ospeed;
    932 	tp->t_cflag = t->c_cflag;
    933 
    934 	if (sc->sc_methods->ucom_param != NULL) {
    935 		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
    936 			    t);
    937 		if (error)
    938 			return (error);
    939 	}
    940 
    941 	/* XXX worry about CHWFLOW */
    942 
    943 	/*
    944 	 * Update the tty layer's idea of the carrier bit, in case we changed
    945 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
    946 	 * explicit request.
    947 	 */
    948 	DPRINTF(("ucomparam: l_modem\n"));
    949 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
    950 
    951 #if 0
    952 XXX what if the hardware is not open
    953 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    954 		if (sc->sc_tx_stopped) {
    955 			sc->sc_tx_stopped = 0;
    956 			ucomstart(tp);
    957 		}
    958 	}
    959 #endif
    960 
    961 	return (0);
    962 }
    963 
    964 static int
    965 ucomhwiflow(struct tty *tp, int block)
    966 {
    967 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
    968 	    UCOMUNIT(tp->t_dev));
    969 	int old;
    970 
    971 	if (sc == NULL)
    972 		return (0);
    973 
    974 	old = sc->sc_rx_stopped;
    975 	sc->sc_rx_stopped = (u_char)block;
    976 
    977 	if (old && !block) {
    978 		int s = splusb();
    979 		sc->sc_rx_unblock = 1;
    980 		softint_schedule(sc->sc_si);
    981 		splx(s);
    982 	}
    983 
    984 	return (1);
    985 }
    986 
    987 static void
    988 ucomstart(struct tty *tp)
    989 {
    990 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
    991 	    UCOMUNIT(tp->t_dev));
    992 	struct ucom_buffer *ub;
    993 	int s;
    994 	u_char *data;
    995 	int cnt;
    996 
    997 	if (sc == NULL || sc->sc_dying)
    998 		return;
    999 
   1000 	s = spltty();
   1001 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1002 		goto out;
   1003 	if (sc->sc_tx_stopped)
   1004 		goto out;
   1005 
   1006 	if (!ttypull(tp))
   1007 		goto out;
   1008 
   1009 	/* Grab the first contiguous region of buffer space. */
   1010 	data = tp->t_outq.c_cf;
   1011 	cnt = ndqb(&tp->t_outq, 0);
   1012 
   1013 	if (cnt == 0)
   1014 		goto out;
   1015 
   1016 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_free);
   1017 	if (ub == NULL) {
   1018 		SET(tp->t_state, TS_BUSY);
   1019 		goto out;
   1020 	}
   1021 	SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_free, ub_link);
   1022 
   1023 	if (SIMPLEQ_FIRST(&sc->sc_obuff_free) == NULL)
   1024 		SET(tp->t_state, TS_BUSY);
   1025 
   1026 	if (cnt > sc->sc_obufsize)
   1027 		cnt = sc->sc_obufsize;
   1028 
   1029 	if (sc->sc_methods->ucom_write != NULL)
   1030 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
   1031 					   ub->ub_data, data, &cnt);
   1032 	else
   1033 		memcpy(ub->ub_data, data, cnt);
   1034 
   1035 	ub->ub_len = cnt;
   1036 	ub->ub_index = 0;
   1037 
   1038 	SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_full, ub, ub_link);
   1039 
   1040 	softint_schedule(sc->sc_si);
   1041 
   1042  out:
   1043 	splx(s);
   1044 }
   1045 
   1046 void
   1047 ucomstop(struct tty *tp, int flag)
   1048 {
   1049 #if 0
   1050 	/*struct ucom_softc *sc =
   1051 	    device_lookup_private(&ucom_cd, UCOMUNIT(tp->t_dev));*/
   1052 	int s;
   1053 
   1054 	s = spltty();
   1055 	if (ISSET(tp->t_state, TS_BUSY)) {
   1056 		/* sc->sc_tx_stopped = 1; */
   1057 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1058 			SET(tp->t_state, TS_FLUSH);
   1059 	}
   1060 	splx(s);
   1061 #endif
   1062 }
   1063 
   1064 static void
   1065 ucom_write_status(struct ucom_softc *sc, struct ucom_buffer *ub,
   1066     usbd_status err)
   1067 {
   1068 	struct tty *tp = sc->sc_tty;
   1069 	uint32_t cc = ub->ub_len;
   1070 
   1071 	switch (err) {
   1072 	case USBD_IN_PROGRESS:
   1073 		ub->ub_index = ub->ub_len;
   1074 		break;
   1075 	case USBD_STALLED:
   1076 		ub->ub_index = 0;
   1077 		softint_schedule(sc->sc_si);
   1078 		break;
   1079 	case USBD_NORMAL_COMPLETION:
   1080 		usbd_get_xfer_status(ub->ub_xfer, NULL, NULL, &cc, NULL);
   1081 		rnd_add_uint32(&sc->sc_rndsource, cc);
   1082 		/*FALLTHROUGH*/
   1083 	default:
   1084 		SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_full, ub_link);
   1085 		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
   1086 		cc -= sc->sc_opkthdrlen;
   1087 
   1088 		mutex_spin_enter(&tty_lock);
   1089 		CLR(tp->t_state, TS_BUSY);
   1090 		if (ISSET(tp->t_state, TS_FLUSH))
   1091 			CLR(tp->t_state, TS_FLUSH);
   1092 		else
   1093 			ndflush(&tp->t_outq, cc);
   1094 		mutex_spin_exit(&tty_lock);
   1095 
   1096 		if (err != USBD_CANCELLED && err != USBD_IOERROR &&
   1097 		    !sc->sc_dying) {
   1098 			if ((ub = SIMPLEQ_FIRST(&sc->sc_obuff_full)) != NULL)
   1099 				ucom_submit_write(sc, ub);
   1100 
   1101 			(*tp->t_linesw->l_start)(tp);
   1102 		}
   1103 		break;
   1104 	}
   1105 }
   1106 
   1107 /* Call at spltty() */
   1108 static void
   1109 ucom_submit_write(struct ucom_softc *sc, struct ucom_buffer *ub)
   1110 {
   1111 
   1112 	usbd_setup_xfer(ub->ub_xfer, sc->sc_bulkout_pipe,
   1113 	    (usbd_private_handle)sc, ub->ub_data, ub->ub_len,
   1114 	    USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
   1115 
   1116 	ucom_write_status(sc, ub, usbd_transfer(ub->ub_xfer));
   1117 }
   1118 
   1119 static void
   1120 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
   1121 {
   1122 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1123 	int s;
   1124 
   1125 	s = spltty();
   1126 
   1127 	ucom_write_status(sc, SIMPLEQ_FIRST(&sc->sc_obuff_full), status);
   1128 
   1129 	splx(s);
   1130 }
   1131 
   1132 static void
   1133 ucom_softintr(void *arg)
   1134 {
   1135 	struct ucom_softc *sc = arg;
   1136 	struct tty *tp = sc->sc_tty;
   1137 	struct ucom_buffer *ub;
   1138 	int s;
   1139 
   1140 	if (!ISSET(tp->t_state, TS_ISOPEN))
   1141 		return;
   1142 
   1143 	s = spltty();
   1144 
   1145 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_full);
   1146 
   1147 	if (ub != NULL && ub->ub_index == 0)
   1148 		ucom_submit_write(sc, ub);
   1149 
   1150 	if (sc->sc_rx_unblock)
   1151 		ucom_read_complete(sc);
   1152 
   1153 	splx(s);
   1154 }
   1155 
   1156 static void
   1157 ucom_read_complete(struct ucom_softc *sc)
   1158 {
   1159 	int (*rint)(int, struct tty *);
   1160 	struct ucom_buffer *ub;
   1161 	struct tty *tp;
   1162 	int s;
   1163 
   1164 	tp = sc->sc_tty;
   1165 	rint = tp->t_linesw->l_rint;
   1166 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1167 
   1168 	while (ub != NULL && !sc->sc_rx_stopped) {
   1169 
   1170 		s = spltty();
   1171 
   1172 		while (ub->ub_index < ub->ub_len && !sc->sc_rx_stopped) {
   1173 			/* Give characters to tty layer. */
   1174 			if ((*rint)(ub->ub_data[ub->ub_index], tp) == -1) {
   1175 				/* Overflow: drop remainder */
   1176 				ub->ub_index = ub->ub_len;
   1177 			} else
   1178 				ub->ub_index++;
   1179 		}
   1180 
   1181 		splx(s);
   1182 
   1183 		if (ub->ub_index == ub->ub_len) {
   1184 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
   1185 
   1186 			ucomsubmitread(sc, ub);
   1187 
   1188 			ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1189 		}
   1190 	}
   1191 
   1192 	sc->sc_rx_unblock = (ub != NULL);
   1193 }
   1194 
   1195 static usbd_status
   1196 ucomsubmitread(struct ucom_softc *sc, struct ucom_buffer *ub)
   1197 {
   1198 	usbd_status err;
   1199 
   1200 	usbd_setup_xfer(ub->ub_xfer, sc->sc_bulkin_pipe,
   1201 	    (usbd_private_handle)sc, ub->ub_data, sc->sc_ibufsize,
   1202 	    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, ucomreadcb);
   1203 
   1204 	if ((err = usbd_transfer(ub->ub_xfer)) != USBD_IN_PROGRESS) {
   1205 		/* XXX: Recover from this, please! */
   1206 		printf("ucomsubmitread: err=%s\n", usbd_errstr(err));
   1207 		return (err);
   1208 	}
   1209 
   1210 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_empty, ub, ub_link);
   1211 
   1212 	return (USBD_NORMAL_COMPLETION);
   1213 }
   1214 
   1215 static void
   1216 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
   1217 {
   1218 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1219 	struct tty *tp = sc->sc_tty;
   1220 	struct ucom_buffer *ub;
   1221 	u_int32_t cc;
   1222 	u_char *cp;
   1223 	int s;
   1224 
   1225 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty);
   1226 	SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_empty, ub_link);
   1227 
   1228 	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
   1229 	    sc->sc_dying) {
   1230 		DPRINTF(("ucomreadcb: dying\n"));
   1231 		ub->ub_index = ub->ub_len = 0;
   1232 		/* Send something to wake upper layer */
   1233 		s = spltty();
   1234 		if (status != USBD_CANCELLED) {
   1235 			(tp->t_linesw->l_rint)('\n', tp);
   1236 			mutex_spin_enter(&tty_lock);	/* XXX */
   1237 			ttwakeup(tp);
   1238 			mutex_spin_exit(&tty_lock);	/* XXX */
   1239 		}
   1240 		splx(s);
   1241 		return;
   1242 	}
   1243 
   1244 	if (status == USBD_STALLED) {
   1245 		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
   1246 		ucomsubmitread(sc, ub);
   1247 		return;
   1248 	}
   1249 
   1250 	if (status != USBD_NORMAL_COMPLETION) {
   1251 		printf("ucomreadcb: wonky status=%s\n", usbd_errstr(status));
   1252 		return;
   1253 	}
   1254 
   1255 	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
   1256 
   1257 #ifdef UCOM_DEBUG
   1258 	/* This is triggered by uslsa(4) occasionally. */
   1259 	if ((ucomdebug > 0) && (cc == 0)) {
   1260 		device_printf(sc->sc_dev, "ucomreadcb: zero length xfer!\n");
   1261 	}
   1262 #endif
   1263 
   1264 	KDASSERT(cp == ub->ub_data);
   1265 
   1266 	rnd_add_uint32(&sc->sc_rndsource, cc);
   1267 
   1268 	if (sc->sc_opening) {
   1269 		ucomsubmitread(sc, ub);
   1270 		return;
   1271 	}
   1272 
   1273 	if (sc->sc_methods->ucom_read != NULL) {
   1274 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
   1275 		    &cp, &cc);
   1276 		ub->ub_index = (u_int)(cp - ub->ub_data);
   1277 	} else
   1278 		ub->ub_index = 0;
   1279 
   1280 	ub->ub_len = cc;
   1281 
   1282 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_full, ub, ub_link);
   1283 
   1284 	ucom_read_complete(sc);
   1285 }
   1286 
   1287 static void
   1288 ucom_cleanup(struct ucom_softc *sc)
   1289 {
   1290 	struct ucom_buffer *ub;
   1291 
   1292 	DPRINTF(("ucom_cleanup: closing pipes\n"));
   1293 
   1294 	ucom_shutdown(sc);
   1295 	if (sc->sc_bulkin_pipe != NULL) {
   1296 		usbd_abort_pipe(sc->sc_bulkin_pipe);
   1297 		usbd_close_pipe(sc->sc_bulkin_pipe);
   1298 		sc->sc_bulkin_pipe = NULL;
   1299 	}
   1300 	if (sc->sc_bulkout_pipe != NULL) {
   1301 		usbd_abort_pipe(sc->sc_bulkout_pipe);
   1302 		usbd_close_pipe(sc->sc_bulkout_pipe);
   1303 		sc->sc_bulkout_pipe = NULL;
   1304 	}
   1305 	for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS]; ub++) {
   1306 		if (ub->ub_xfer != NULL) {
   1307 			usbd_free_xfer(ub->ub_xfer);
   1308 			ub->ub_xfer = NULL;
   1309 			ub->ub_data = NULL;
   1310 		}
   1311 	}
   1312 	for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS]; ub++){
   1313 		if (ub->ub_xfer != NULL) {
   1314 			usbd_free_xfer(ub->ub_xfer);
   1315 			ub->ub_xfer = NULL;
   1316 			ub->ub_data = NULL;
   1317 		}
   1318 	}
   1319 }
   1320 
   1321 #endif /* NUCOM > 0 */
   1322 
   1323 int
   1324 ucomprint(void *aux, const char *pnp)
   1325 {
   1326 	struct ucom_attach_args *uca = aux;
   1327 
   1328 	if (pnp)
   1329 		aprint_normal("ucom at %s", pnp);
   1330 	if (uca->portno != UCOM_UNK_PORTNO)
   1331 		aprint_normal(" portno %d", uca->portno);
   1332 	return (UNCONF);
   1333 }
   1334 
   1335 int
   1336 ucomsubmatch(device_t parent, cfdata_t cf,
   1337 	     const int *ldesc, void *aux)
   1338 {
   1339 	struct ucom_attach_args *uca = aux;
   1340 
   1341 	if (uca->portno != UCOM_UNK_PORTNO &&
   1342 	    cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
   1343 	    cf->cf_loc[UCOMBUSCF_PORTNO] != uca->portno)
   1344 		return (0);
   1345 	return (config_match(parent, cf, aux));
   1346 }
   1347