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