Home | History | Annotate | Line # | Download | only in usb
ucom.c revision 1.82.2.3
      1 /*	$NetBSD: ucom.c,v 1.82.2.3 2010/11/06 08:08:37 uebayasi 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.82.2.3 2010/11/06 08:08:37 uebayasi 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 	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 	rndsource_element_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 = ttymalloc();
    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 		ttyfree(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 	if (sc == NULL)
    374 		return (ENXIO);
    375 
    376 	if (sc->sc_dying)
    377 		return (EIO);
    378 
    379 	if (!device_is_active(sc->sc_dev))
    380 		return (ENXIO);
    381 
    382 	tp = sc->sc_tty;
    383 
    384 	DPRINTF(("ucomopen: unit=%d, tp=%p\n", unit, tp));
    385 
    386 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    387 		return (EBUSY);
    388 
    389 	s = spltty();
    390 
    391 	/*
    392 	 * Do the following iff this is a first open.
    393 	 */
    394 	while (sc->sc_opening)
    395 		tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
    396 
    397 	if (sc->sc_dying) {
    398 		splx(s);
    399 		return (EIO);
    400 	}
    401 	sc->sc_opening = 1;
    402 
    403 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    404 		struct termios t;
    405 
    406 		tp->t_dev = dev;
    407 
    408 		if (sc->sc_methods->ucom_open != NULL) {
    409 			error = sc->sc_methods->ucom_open(sc->sc_parent,
    410 							  sc->sc_portno);
    411 			if (error) {
    412 				ucom_cleanup(sc);
    413 				sc->sc_opening = 0;
    414 				wakeup(&sc->sc_opening);
    415 				splx(s);
    416 				return (error);
    417 			}
    418 		}
    419 
    420 		ucom_status_change(sc);
    421 
    422 		/*
    423 		 * Initialize the termios status to the defaults.  Add in the
    424 		 * sticky bits from TIOCSFLAGS.
    425 		 */
    426 		t.c_ispeed = 0;
    427 		t.c_ospeed = TTYDEF_SPEED;
    428 		t.c_cflag = TTYDEF_CFLAG;
    429 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    430 			SET(t.c_cflag, CLOCAL);
    431 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    432 			SET(t.c_cflag, CRTSCTS);
    433 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    434 			SET(t.c_cflag, MDMBUF);
    435 		/* Make sure ucomparam() will do something. */
    436 		tp->t_ospeed = 0;
    437 		(void) ucomparam(tp, &t);
    438 		tp->t_iflag = TTYDEF_IFLAG;
    439 		tp->t_oflag = TTYDEF_OFLAG;
    440 		tp->t_lflag = TTYDEF_LFLAG;
    441 		ttychars(tp);
    442 		ttsetwater(tp);
    443 
    444 		/*
    445 		 * Turn on DTR.  We must always do this, even if carrier is not
    446 		 * present, because otherwise we'd have to use TIOCSDTR
    447 		 * immediately after setting CLOCAL, which applications do not
    448 		 * expect.  We always assert DTR while the device is open
    449 		 * unless explicitly requested to deassert it.  Ditto RTS.
    450 		 */
    451 		ucom_dtr(sc, 1);
    452 		ucom_rts(sc, 1);
    453 
    454 		DPRINTF(("ucomopen: open pipes in=%d out=%d\n",
    455 			 sc->sc_bulkin_no, sc->sc_bulkout_no));
    456 
    457 		/* Open the bulk pipes */
    458 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no,
    459 				     USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
    460 		if (err) {
    461 			DPRINTF(("%s: open bulk in error (addr %d), err=%s\n",
    462 				 device_xname(sc->sc_dev), sc->sc_bulkin_no,
    463 				 usbd_errstr(err)));
    464 			error = EIO;
    465 			goto fail_0;
    466 		}
    467 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
    468 				     USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
    469 		if (err) {
    470 			DPRINTF(("%s: open bulk out error (addr %d), err=%s\n",
    471 				 device_xname(sc->sc_dev), sc->sc_bulkout_no,
    472 				 usbd_errstr(err)));
    473 			error = EIO;
    474 			goto fail_1;
    475 		}
    476 
    477 		sc->sc_rx_unblock = 0;
    478 		sc->sc_rx_stopped = 0;
    479 		sc->sc_tx_stopped = 0;
    480 
    481 		memset(sc->sc_ibuff, 0, sizeof(sc->sc_ibuff));
    482 		memset(sc->sc_obuff, 0, sizeof(sc->sc_obuff));
    483 
    484 		SIMPLEQ_INIT(&sc->sc_ibuff_empty);
    485 		SIMPLEQ_INIT(&sc->sc_ibuff_full);
    486 		SIMPLEQ_INIT(&sc->sc_obuff_free);
    487 		SIMPLEQ_INIT(&sc->sc_obuff_full);
    488 
    489 		/* Allocate input buffers */
    490 		for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
    491 		    ub++) {
    492 			ub->ub_xfer = usbd_alloc_xfer(sc->sc_udev);
    493 			if (ub->ub_xfer == NULL) {
    494 				error = ENOMEM;
    495 				goto fail_2;
    496 			}
    497 			ub->ub_data = usbd_alloc_buffer(ub->ub_xfer,
    498 			    sc->sc_ibufsizepad);
    499 			if (ub->ub_data == NULL) {
    500 				error = ENOMEM;
    501 				goto fail_2;
    502 			}
    503 
    504 			if (ucomsubmitread(sc, ub) != USBD_NORMAL_COMPLETION) {
    505 				error = EIO;
    506 				goto fail_2;
    507 			}
    508 		}
    509 
    510 		for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
    511 		    ub++) {
    512 			ub->ub_xfer = usbd_alloc_xfer(sc->sc_udev);
    513 			if (ub->ub_xfer == NULL) {
    514 				error = ENOMEM;
    515 				goto fail_2;
    516 			}
    517 			ub->ub_data = usbd_alloc_buffer(ub->ub_xfer,
    518 			    sc->sc_obufsize);
    519 			if (ub->ub_data == NULL) {
    520 				error = ENOMEM;
    521 				goto fail_2;
    522 			}
    523 
    524 			SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
    525 		}
    526 
    527 	}
    528 	sc->sc_opening = 0;
    529 	wakeup(&sc->sc_opening);
    530 	splx(s);
    531 
    532 	error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    533 	if (error)
    534 		goto bad;
    535 
    536 	error = (*tp->t_linesw->l_open)(dev, tp);
    537 	if (error)
    538 		goto bad;
    539 
    540 	return (0);
    541 
    542 fail_2:
    543 	usbd_abort_pipe(sc->sc_bulkin_pipe);
    544 	for (i = 0; i < UCOM_IN_BUFFS; i++) {
    545 		if (sc->sc_ibuff[i].ub_xfer != NULL) {
    546 			usbd_free_xfer(sc->sc_ibuff[i].ub_xfer);
    547 			sc->sc_ibuff[i].ub_xfer = NULL;
    548 			sc->sc_ibuff[i].ub_data = NULL;
    549 		}
    550 	}
    551 	usbd_abort_pipe(sc->sc_bulkout_pipe);
    552 	for (i = 0; i < UCOM_OUT_BUFFS; i++) {
    553 		if (sc->sc_obuff[i].ub_xfer != NULL) {
    554 			usbd_free_xfer(sc->sc_obuff[i].ub_xfer);
    555 			sc->sc_obuff[i].ub_xfer = NULL;
    556 			sc->sc_obuff[i].ub_data = NULL;
    557 		}
    558 	}
    559 
    560 	usbd_close_pipe(sc->sc_bulkout_pipe);
    561 	sc->sc_bulkout_pipe = NULL;
    562 fail_1:
    563 	usbd_close_pipe(sc->sc_bulkin_pipe);
    564 	sc->sc_bulkin_pipe = NULL;
    565 fail_0:
    566 	sc->sc_opening = 0;
    567 	wakeup(&sc->sc_opening);
    568 	splx(s);
    569 	return (error);
    570 
    571 bad:
    572 	s = spltty();
    573 	CLR(tp->t_state, TS_BUSY);
    574 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    575 		/*
    576 		 * We failed to open the device, and nobody else had it opened.
    577 		 * Clean up the state as appropriate.
    578 		 */
    579 		ucom_cleanup(sc);
    580 	}
    581 	splx(s);
    582 
    583 	return (error);
    584 }
    585 
    586 int
    587 ucomclose(dev_t dev, int flag, int mode, struct lwp *l)
    588 {
    589 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    590 	struct tty *tp = sc->sc_tty;
    591 	int s;
    592 
    593 	DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev)));
    594 	if (!ISSET(tp->t_state, TS_ISOPEN))
    595 		return (0);
    596 
    597 	s = spltty();
    598 	sc->sc_refcnt++;
    599 	CLR(tp->t_state, TS_BUSY);
    600 
    601 	(*tp->t_linesw->l_close)(tp, flag);
    602 	ttyclose(tp);
    603 
    604 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    605 		/*
    606 		 * Although we got a last close, the device may still be in
    607 		 * use; e.g. if this was the dialout node, and there are still
    608 		 * processes waiting for carrier on the non-dialout node.
    609 		 */
    610 		ucom_cleanup(sc);
    611 	}
    612 
    613 	if (sc->sc_methods->ucom_close != NULL)
    614 		sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
    615 
    616 	if (--sc->sc_refcnt < 0)
    617 		usb_detach_wakeup(sc->sc_dev);
    618 	splx(s);
    619 
    620 	return (0);
    621 }
    622 
    623 int
    624 ucomread(dev_t dev, struct uio *uio, int flag)
    625 {
    626 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    627 	struct tty *tp = sc->sc_tty;
    628 	int error;
    629 
    630 	if (sc->sc_dying)
    631 		return (EIO);
    632 
    633 	sc->sc_refcnt++;
    634 	error = ((*tp->t_linesw->l_read)(tp, uio, flag));
    635 	if (--sc->sc_refcnt < 0)
    636 		usb_detach_wakeup(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 = sc->sc_tty;
    645 	int error;
    646 
    647 	if (sc->sc_dying)
    648 		return (EIO);
    649 
    650 	sc->sc_refcnt++;
    651 	error = ((*tp->t_linesw->l_write)(tp, uio, flag));
    652 	if (--sc->sc_refcnt < 0)
    653 		usb_detach_wakeup(sc->sc_dev);
    654 	return (error);
    655 }
    656 
    657 int
    658 ucompoll(dev_t dev, int events, struct lwp *l)
    659 {
    660 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    661 	struct tty *tp = sc->sc_tty;
    662 	int revents;
    663 
    664 	if (sc->sc_dying)
    665 		return (POLLHUP);
    666 
    667 	sc->sc_refcnt++;
    668 	revents = ((*tp->t_linesw->l_poll)(tp, events, l));
    669 	if (--sc->sc_refcnt < 0)
    670 		usb_detach_wakeup(sc->sc_dev);
    671 	return (revents);
    672 }
    673 
    674 struct tty *
    675 ucomtty(dev_t dev)
    676 {
    677 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    678 	struct tty *tp = sc->sc_tty;
    679 
    680 	return (tp);
    681 }
    682 
    683 int
    684 ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    685 {
    686 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    687 	int error;
    688 
    689 	sc->sc_refcnt++;
    690 	error = ucom_do_ioctl(sc, cmd, data, flag, l);
    691 	if (--sc->sc_refcnt < 0)
    692 		usb_detach_wakeup(sc->sc_dev);
    693 	return (error);
    694 }
    695 
    696 static int
    697 ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, void *data,
    698 	      int flag, struct lwp *l)
    699 {
    700 	struct tty *tp = sc->sc_tty;
    701 	int error;
    702 	int s;
    703 
    704 	if (sc->sc_dying)
    705 		return (EIO);
    706 
    707 	DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd));
    708 
    709 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    710 	if (error != EPASSTHROUGH)
    711 		return (error);
    712 
    713 	error = ttioctl(tp, cmd, data, flag, l);
    714 	if (error != EPASSTHROUGH)
    715 		return (error);
    716 
    717 	if (sc->sc_methods->ucom_ioctl != NULL) {
    718 		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
    719 			    sc->sc_portno, cmd, data, flag, l->l_proc);
    720 		if (error != EPASSTHROUGH)
    721 			return (error);
    722 	}
    723 
    724 	error = 0;
    725 
    726 	DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd));
    727 	s = spltty();
    728 
    729 	switch (cmd) {
    730 	case TIOCSBRK:
    731 		ucom_break(sc, 1);
    732 		break;
    733 
    734 	case TIOCCBRK:
    735 		ucom_break(sc, 0);
    736 		break;
    737 
    738 	case TIOCSDTR:
    739 		ucom_dtr(sc, 1);
    740 		break;
    741 
    742 	case TIOCCDTR:
    743 		ucom_dtr(sc, 0);
    744 		break;
    745 
    746 	case TIOCGFLAGS:
    747 		*(int *)data = sc->sc_swflags;
    748 		break;
    749 
    750 	case TIOCSFLAGS:
    751 		error = kauth_authorize_device_tty(l->l_cred,
    752 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    753 		if (error)
    754 			break;
    755 		sc->sc_swflags = *(int *)data;
    756 		break;
    757 
    758 	case TIOCMSET:
    759 	case TIOCMBIS:
    760 	case TIOCMBIC:
    761 		tiocm_to_ucom(sc, cmd, *(int *)data);
    762 		break;
    763 
    764 	case TIOCMGET:
    765 		*(int *)data = ucom_to_tiocm(sc);
    766 		break;
    767 
    768 	default:
    769 		error = EPASSTHROUGH;
    770 		break;
    771 	}
    772 
    773 	splx(s);
    774 
    775 	return (error);
    776 }
    777 
    778 static void
    779 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
    780 {
    781 	u_char combits;
    782 
    783 	combits = 0;
    784 	if (ISSET(ttybits, TIOCM_DTR))
    785 		SET(combits, UMCR_DTR);
    786 	if (ISSET(ttybits, TIOCM_RTS))
    787 		SET(combits, UMCR_RTS);
    788 
    789 	switch (how) {
    790 	case TIOCMBIC:
    791 		CLR(sc->sc_mcr, combits);
    792 		break;
    793 
    794 	case TIOCMBIS:
    795 		SET(sc->sc_mcr, combits);
    796 		break;
    797 
    798 	case TIOCMSET:
    799 		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
    800 		SET(sc->sc_mcr, combits);
    801 		break;
    802 	}
    803 
    804 	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
    805 		ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
    806 	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
    807 		ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
    808 }
    809 
    810 static int
    811 ucom_to_tiocm(struct ucom_softc *sc)
    812 {
    813 	u_char combits;
    814 	int ttybits = 0;
    815 
    816 	combits = sc->sc_mcr;
    817 	if (ISSET(combits, UMCR_DTR))
    818 		SET(ttybits, TIOCM_DTR);
    819 	if (ISSET(combits, UMCR_RTS))
    820 		SET(ttybits, TIOCM_RTS);
    821 
    822 	combits = sc->sc_msr;
    823 	if (ISSET(combits, UMSR_DCD))
    824 		SET(ttybits, TIOCM_CD);
    825 	if (ISSET(combits, UMSR_CTS))
    826 		SET(ttybits, TIOCM_CTS);
    827 	if (ISSET(combits, UMSR_DSR))
    828 		SET(ttybits, TIOCM_DSR);
    829 	if (ISSET(combits, UMSR_RI | UMSR_TERI))
    830 		SET(ttybits, TIOCM_RI);
    831 
    832 #if 0
    833 XXX;
    834 	if (sc->sc_ier != 0)
    835 		SET(ttybits, TIOCM_LE);
    836 #endif
    837 
    838 	return (ttybits);
    839 }
    840 
    841 static void
    842 ucom_break(struct ucom_softc *sc, int onoff)
    843 {
    844 	DPRINTF(("ucom_break: onoff=%d\n", onoff));
    845 
    846 	if (sc->sc_methods->ucom_set != NULL)
    847 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    848 		    UCOM_SET_BREAK, onoff);
    849 }
    850 
    851 static void
    852 ucom_dtr(struct ucom_softc *sc, int onoff)
    853 {
    854 	DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
    855 
    856 	if (sc->sc_methods->ucom_set != NULL)
    857 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    858 		    UCOM_SET_DTR, onoff);
    859 }
    860 
    861 static void
    862 ucom_rts(struct ucom_softc *sc, int onoff)
    863 {
    864 	DPRINTF(("ucom_rts: onoff=%d\n", onoff));
    865 
    866 	if (sc->sc_methods->ucom_set != NULL)
    867 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    868 		    UCOM_SET_RTS, onoff);
    869 }
    870 
    871 void
    872 ucom_status_change(struct ucom_softc *sc)
    873 {
    874 	struct tty *tp = sc->sc_tty;
    875 	u_char old_msr;
    876 
    877 	if (sc->sc_methods->ucom_get_status != NULL) {
    878 		old_msr = sc->sc_msr;
    879 		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
    880 		    &sc->sc_lsr, &sc->sc_msr);
    881 		if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD))
    882 			(*tp->t_linesw->l_modem)(tp,
    883 			    ISSET(sc->sc_msr, UMSR_DCD));
    884 	} else {
    885 		sc->sc_lsr = 0;
    886 		/* Assume DCD is present, if we have no chance to check it. */
    887 		sc->sc_msr = UMSR_DCD;
    888 	}
    889 }
    890 
    891 static int
    892 ucomparam(struct tty *tp, struct termios *t)
    893 {
    894 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
    895 	    UCOMUNIT(tp->t_dev));
    896 	int error;
    897 
    898 	if (sc->sc_dying)
    899 		return (EIO);
    900 
    901 	/* Check requested parameters. */
    902 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    903 		return (EINVAL);
    904 
    905 	/*
    906 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    907 	 * is always active.
    908 	 */
    909 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
    910 		SET(t->c_cflag, CLOCAL);
    911 		CLR(t->c_cflag, HUPCL);
    912 	}
    913 
    914 	/*
    915 	 * If there were no changes, don't do anything.  This avoids dropping
    916 	 * input and improves performance when all we did was frob things like
    917 	 * VMIN and VTIME.
    918 	 */
    919 	if (tp->t_ospeed == t->c_ospeed &&
    920 	    tp->t_cflag == t->c_cflag)
    921 		return (0);
    922 
    923 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
    924 
    925 	/* And copy to tty. */
    926 	tp->t_ispeed = 0;
    927 	tp->t_ospeed = t->c_ospeed;
    928 	tp->t_cflag = t->c_cflag;
    929 
    930 	if (sc->sc_methods->ucom_param != NULL) {
    931 		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
    932 			    t);
    933 		if (error)
    934 			return (error);
    935 	}
    936 
    937 	/* XXX worry about CHWFLOW */
    938 
    939 	/*
    940 	 * Update the tty layer's idea of the carrier bit, in case we changed
    941 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
    942 	 * explicit request.
    943 	 */
    944 	DPRINTF(("ucomparam: l_modem\n"));
    945 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
    946 
    947 #if 0
    948 XXX what if the hardware is not open
    949 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    950 		if (sc->sc_tx_stopped) {
    951 			sc->sc_tx_stopped = 0;
    952 			ucomstart(tp);
    953 		}
    954 	}
    955 #endif
    956 
    957 	return (0);
    958 }
    959 
    960 static int
    961 ucomhwiflow(struct tty *tp, int block)
    962 {
    963 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
    964 	    UCOMUNIT(tp->t_dev));
    965 	int old;
    966 
    967 	old = sc->sc_rx_stopped;
    968 	sc->sc_rx_stopped = (u_char)block;
    969 
    970 	if (old && !block) {
    971 		int s = splusb();
    972 		sc->sc_rx_unblock = 1;
    973 		softint_schedule(sc->sc_si);
    974 		splx(s);
    975 	}
    976 
    977 	return (1);
    978 }
    979 
    980 static void
    981 ucomstart(struct tty *tp)
    982 {
    983 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
    984 	    UCOMUNIT(tp->t_dev));
    985 	struct ucom_buffer *ub;
    986 	int s;
    987 	u_char *data;
    988 	int cnt;
    989 
    990 	if (sc->sc_dying)
    991 		return;
    992 
    993 	s = spltty();
    994 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    995 		goto out;
    996 	if (sc->sc_tx_stopped)
    997 		goto out;
    998 
    999 	if (!ttypull(tp))
   1000 		goto out;
   1001 
   1002 	/* Grab the first contiguous region of buffer space. */
   1003 	data = tp->t_outq.c_cf;
   1004 	cnt = ndqb(&tp->t_outq, 0);
   1005 
   1006 	if (cnt == 0)
   1007 		goto out;
   1008 
   1009 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_free);
   1010 	KASSERT(ub != NULL);
   1011 	SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_free, ub_link);
   1012 
   1013 	if (SIMPLEQ_FIRST(&sc->sc_obuff_free) == NULL)
   1014 		SET(tp->t_state, TS_BUSY);
   1015 
   1016 	if (cnt > sc->sc_obufsize)
   1017 		cnt = sc->sc_obufsize;
   1018 
   1019 	if (sc->sc_methods->ucom_write != NULL)
   1020 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
   1021 					   ub->ub_data, data, &cnt);
   1022 	else
   1023 		memcpy(ub->ub_data, data, cnt);
   1024 
   1025 	ub->ub_len = cnt;
   1026 	ub->ub_index = 0;
   1027 
   1028 	SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_full, ub, ub_link);
   1029 
   1030 	softint_schedule(sc->sc_si);
   1031 
   1032  out:
   1033 	splx(s);
   1034 }
   1035 
   1036 void
   1037 ucomstop(struct tty *tp, int flag)
   1038 {
   1039 #if 0
   1040 	/*struct ucom_softc *sc =
   1041 	    device_lookup_private(&ucom_cd, UCOMUNIT(dev));*/
   1042 	int s;
   1043 
   1044 	s = spltty();
   1045 	if (ISSET(tp->t_state, TS_BUSY)) {
   1046 		/* sc->sc_tx_stopped = 1; */
   1047 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1048 			SET(tp->t_state, TS_FLUSH);
   1049 	}
   1050 	splx(s);
   1051 #endif
   1052 }
   1053 
   1054 static void
   1055 ucom_write_status(struct ucom_softc *sc, struct ucom_buffer *ub,
   1056     usbd_status err)
   1057 {
   1058 	struct tty *tp = sc->sc_tty;
   1059 	uint32_t cc = ub->ub_len;
   1060 
   1061 	switch (err) {
   1062 	case USBD_IN_PROGRESS:
   1063 		ub->ub_index = ub->ub_len;
   1064 		break;
   1065 	case USBD_STALLED:
   1066 		ub->ub_index = 0;
   1067 		softint_schedule(sc->sc_si);
   1068 		break;
   1069 	case USBD_NORMAL_COMPLETION:
   1070 		usbd_get_xfer_status(ub->ub_xfer, NULL, NULL, &cc, NULL);
   1071 #if defined(__NetBSD__) && NRND > 0
   1072 		rnd_add_uint32(&sc->sc_rndsource, cc);
   1073 #endif
   1074 		/*FALLTHROUGH*/
   1075 	default:
   1076 		SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_full, ub_link);
   1077 		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
   1078 		cc -= sc->sc_opkthdrlen;
   1079 
   1080 		CLR(tp->t_state, TS_BUSY);
   1081 		if (ISSET(tp->t_state, TS_FLUSH))
   1082 			CLR(tp->t_state, TS_FLUSH);
   1083 		else
   1084 			ndflush(&tp->t_outq, cc);
   1085 
   1086 		if (err != USBD_CANCELLED && err != USBD_IOERROR &&
   1087 		    !sc->sc_dying) {
   1088 			if ((ub = SIMPLEQ_FIRST(&sc->sc_obuff_full)) != NULL)
   1089 				ucom_submit_write(sc, ub);
   1090 
   1091 			(*tp->t_linesw->l_start)(tp);
   1092 		}
   1093 		break;
   1094 	}
   1095 }
   1096 
   1097 /* Call at spltty() */
   1098 static void
   1099 ucom_submit_write(struct ucom_softc *sc, struct ucom_buffer *ub)
   1100 {
   1101 
   1102 	usbd_setup_xfer(ub->ub_xfer, sc->sc_bulkout_pipe,
   1103 	    (usbd_private_handle)sc, ub->ub_data, ub->ub_len,
   1104 	    USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
   1105 
   1106 	ucom_write_status(sc, ub, usbd_transfer(ub->ub_xfer));
   1107 }
   1108 
   1109 static void
   1110 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
   1111 {
   1112 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1113 	int s;
   1114 
   1115 	s = spltty();
   1116 
   1117 	ucom_write_status(sc, SIMPLEQ_FIRST(&sc->sc_obuff_full), status);
   1118 
   1119 	splx(s);
   1120 }
   1121 
   1122 static void
   1123 ucom_softintr(void *arg)
   1124 {
   1125 	struct ucom_softc *sc = arg;
   1126 	struct tty *tp = sc->sc_tty;
   1127 	struct ucom_buffer *ub;
   1128 	int s;
   1129 
   1130 	if (!ISSET(tp->t_state, TS_ISOPEN))
   1131 		return;
   1132 
   1133 	s = spltty();
   1134 
   1135 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_full);
   1136 
   1137 	if (ub != NULL && ub->ub_index == 0)
   1138 		ucom_submit_write(sc, ub);
   1139 
   1140 	if (sc->sc_rx_unblock)
   1141 		ucom_read_complete(sc);
   1142 
   1143 	splx(s);
   1144 }
   1145 
   1146 static void
   1147 ucom_read_complete(struct ucom_softc *sc)
   1148 {
   1149 	int (*rint)(int, struct tty *);
   1150 	struct ucom_buffer *ub;
   1151 	struct tty *tp;
   1152 	int s;
   1153 
   1154 	tp = sc->sc_tty;
   1155 	rint = tp->t_linesw->l_rint;
   1156 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1157 
   1158 	while (ub != NULL && !sc->sc_rx_stopped) {
   1159 
   1160 		s = spltty();
   1161 
   1162 		while (ub->ub_index < ub->ub_len && !sc->sc_rx_stopped) {
   1163 			/* Give characters to tty layer. */
   1164 			if ((*rint)(ub->ub_data[ub->ub_index], tp) == -1) {
   1165 				/* Overflow: drop remainder */
   1166 				ub->ub_index = ub->ub_len;
   1167 			} else
   1168 				ub->ub_index++;
   1169 		}
   1170 
   1171 		splx(s);
   1172 
   1173 		if (ub->ub_index == ub->ub_len) {
   1174 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
   1175 
   1176 			ucomsubmitread(sc, ub);
   1177 
   1178 			ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1179 		}
   1180 	}
   1181 
   1182 	sc->sc_rx_unblock = (ub != NULL);
   1183 }
   1184 
   1185 static usbd_status
   1186 ucomsubmitread(struct ucom_softc *sc, struct ucom_buffer *ub)
   1187 {
   1188 	usbd_status err;
   1189 
   1190 	usbd_setup_xfer(ub->ub_xfer, sc->sc_bulkin_pipe,
   1191 	    (usbd_private_handle)sc, ub->ub_data, sc->sc_ibufsize,
   1192 	    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, ucomreadcb);
   1193 
   1194 	if ((err = usbd_transfer(ub->ub_xfer)) != USBD_IN_PROGRESS) {
   1195 		/* XXX: Recover from this, please! */
   1196 		printf("ucomsubmitread: err=%s\n", usbd_errstr(err));
   1197 		return (err);
   1198 	}
   1199 
   1200 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_empty, ub, ub_link);
   1201 
   1202 	return (USBD_NORMAL_COMPLETION);
   1203 }
   1204 
   1205 static void
   1206 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
   1207 {
   1208 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1209 	struct tty *tp = sc->sc_tty;
   1210 	struct ucom_buffer *ub;
   1211 	u_int32_t cc;
   1212 	u_char *cp;
   1213 	int s;
   1214 
   1215 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty);
   1216 	SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_empty, ub_link);
   1217 
   1218 	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
   1219 	    sc->sc_dying) {
   1220 		DPRINTF(("ucomreadcb: dying\n"));
   1221 		ub->ub_index = ub->ub_len = 0;
   1222 		/* Send something to wake upper layer */
   1223 		s = spltty();
   1224 		if (status != USBD_CANCELLED) {
   1225 			(tp->t_linesw->l_rint)('\n', tp);
   1226 			mutex_spin_enter(&tty_lock);	/* XXX */
   1227 			ttwakeup(tp);
   1228 			mutex_spin_exit(&tty_lock);	/* XXX */
   1229 		}
   1230 		splx(s);
   1231 		return;
   1232 	}
   1233 
   1234 	if (status == USBD_STALLED) {
   1235 		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
   1236 		ucomsubmitread(sc, ub);
   1237 		return;
   1238 	}
   1239 
   1240 	if (status != USBD_NORMAL_COMPLETION) {
   1241 		printf("ucomreadcb: wonky status=%s\n", usbd_errstr(status));
   1242 		return;
   1243 	}
   1244 
   1245 	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
   1246 
   1247 	if (cc == 0) {
   1248 		aprint_normal_dev(sc->sc_dev,
   1249 		    "ucomreadcb: zero length xfer!\n");
   1250 	}
   1251 
   1252 	KDASSERT(cp == ub->ub_data);
   1253 
   1254 #if defined(__NetBSD__) && NRND > 0
   1255 	rnd_add_uint32(&sc->sc_rndsource, cc);
   1256 #endif
   1257 
   1258 	if (sc->sc_opening) {
   1259 		ucomsubmitread(sc, ub);
   1260 		return;
   1261 	}
   1262 
   1263 	if (sc->sc_methods->ucom_read != NULL) {
   1264 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
   1265 		    &cp, &cc);
   1266 		ub->ub_index = (u_int)(cp - ub->ub_data);
   1267 	} else
   1268 		ub->ub_index = 0;
   1269 
   1270 	ub->ub_len = cc;
   1271 
   1272 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_full, ub, ub_link);
   1273 
   1274 	ucom_read_complete(sc);
   1275 }
   1276 
   1277 static void
   1278 ucom_cleanup(struct ucom_softc *sc)
   1279 {
   1280 	struct ucom_buffer *ub;
   1281 
   1282 	DPRINTF(("ucom_cleanup: closing pipes\n"));
   1283 
   1284 	ucom_shutdown(sc);
   1285 	if (sc->sc_bulkin_pipe != NULL) {
   1286 		usbd_abort_pipe(sc->sc_bulkin_pipe);
   1287 		usbd_close_pipe(sc->sc_bulkin_pipe);
   1288 		sc->sc_bulkin_pipe = NULL;
   1289 	}
   1290 	if (sc->sc_bulkout_pipe != NULL) {
   1291 		usbd_abort_pipe(sc->sc_bulkout_pipe);
   1292 		usbd_close_pipe(sc->sc_bulkout_pipe);
   1293 		sc->sc_bulkout_pipe = NULL;
   1294 	}
   1295 	for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS]; ub++) {
   1296 		if (ub->ub_xfer != NULL) {
   1297 			usbd_free_xfer(ub->ub_xfer);
   1298 			ub->ub_xfer = NULL;
   1299 			ub->ub_data = NULL;
   1300 		}
   1301 	}
   1302 	for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS]; ub++){
   1303 		if (ub->ub_xfer != NULL) {
   1304 			usbd_free_xfer(ub->ub_xfer);
   1305 			ub->ub_xfer = NULL;
   1306 			ub->ub_data = NULL;
   1307 		}
   1308 	}
   1309 }
   1310 
   1311 #endif /* NUCOM > 0 */
   1312 
   1313 int
   1314 ucomprint(void *aux, const char *pnp)
   1315 {
   1316 	struct ucom_attach_args *uca = aux;
   1317 
   1318 	if (pnp)
   1319 		aprint_normal("ucom at %s", pnp);
   1320 	if (uca->portno != UCOM_UNK_PORTNO)
   1321 		aprint_normal(" portno %d", uca->portno);
   1322 	return (UNCONF);
   1323 }
   1324 
   1325 int
   1326 ucomsubmatch(device_t parent, cfdata_t cf,
   1327 	     const int *ldesc, void *aux)
   1328 {
   1329 	struct ucom_attach_args *uca = aux;
   1330 
   1331 	if (uca->portno != UCOM_UNK_PORTNO &&
   1332 	    cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
   1333 	    cf->cf_loc[UCOMBUSCF_PORTNO] != uca->portno)
   1334 		return (0);
   1335 	return (config_match(parent, cf, aux));
   1336 }
   1337