Home | History | Annotate | Line # | Download | only in usb
ucom.c revision 1.87.4.2
      1 /*	$NetBSD: ucom.c,v 1.87.4.2 2012/10/30 17:22:08 yamt 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.87.4.2 2012/10/30 17:22:08 yamt 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 = sc->sc_tty;
    584 	int s;
    585 
    586 	DPRINTF(("ucomclose: unit=%d\n", UCOMUNIT(dev)));
    587 	if (!ISSET(tp->t_state, TS_ISOPEN))
    588 		return (0);
    589 
    590 	s = spltty();
    591 	sc->sc_refcnt++;
    592 
    593 	(*tp->t_linesw->l_close)(tp, flag);
    594 	ttyclose(tp);
    595 
    596 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    597 		/*
    598 		 * Although we got a last close, the device may still be in
    599 		 * use; e.g. if this was the dialout node, and there are still
    600 		 * processes waiting for carrier on the non-dialout node.
    601 		 */
    602 		ucom_cleanup(sc);
    603 	}
    604 
    605 	if (sc->sc_methods->ucom_close != NULL)
    606 		sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
    607 
    608 	if (--sc->sc_refcnt < 0)
    609 		usb_detach_wakeupold(sc->sc_dev);
    610 	splx(s);
    611 
    612 	return (0);
    613 }
    614 
    615 int
    616 ucomread(dev_t dev, struct uio *uio, int flag)
    617 {
    618 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    619 	struct tty *tp = sc->sc_tty;
    620 	int error;
    621 
    622 	if (sc->sc_dying)
    623 		return (EIO);
    624 
    625 	sc->sc_refcnt++;
    626 	error = ((*tp->t_linesw->l_read)(tp, uio, flag));
    627 	if (--sc->sc_refcnt < 0)
    628 		usb_detach_wakeupold(sc->sc_dev);
    629 	return (error);
    630 }
    631 
    632 int
    633 ucomwrite(dev_t dev, struct uio *uio, int flag)
    634 {
    635 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    636 	struct tty *tp = sc->sc_tty;
    637 	int error;
    638 
    639 	if (sc->sc_dying)
    640 		return (EIO);
    641 
    642 	sc->sc_refcnt++;
    643 	error = ((*tp->t_linesw->l_write)(tp, uio, flag));
    644 	if (--sc->sc_refcnt < 0)
    645 		usb_detach_wakeupold(sc->sc_dev);
    646 	return (error);
    647 }
    648 
    649 int
    650 ucompoll(dev_t dev, int events, struct lwp *l)
    651 {
    652 	struct ucom_softc *sc;
    653 	struct tty *tp;
    654 	int revents;
    655 
    656 	sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    657 	if (sc == NULL || sc->sc_dying)
    658 		return (POLLHUP);
    659 
    660 	tp = sc->sc_tty;
    661 
    662 	sc->sc_refcnt++;
    663 	revents = ((*tp->t_linesw->l_poll)(tp, events, l));
    664 	if (--sc->sc_refcnt < 0)
    665 		usb_detach_wakeupold(sc->sc_dev);
    666 	return (revents);
    667 }
    668 
    669 struct tty *
    670 ucomtty(dev_t dev)
    671 {
    672 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    673 	struct tty *tp = sc->sc_tty;
    674 
    675 	return (tp);
    676 }
    677 
    678 int
    679 ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    680 {
    681 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    682 	int error;
    683 
    684 	sc->sc_refcnt++;
    685 	error = ucom_do_ioctl(sc, cmd, data, flag, l);
    686 	if (--sc->sc_refcnt < 0)
    687 		usb_detach_wakeupold(sc->sc_dev);
    688 	return (error);
    689 }
    690 
    691 static int
    692 ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, void *data,
    693 	      int flag, struct lwp *l)
    694 {
    695 	struct tty *tp = sc->sc_tty;
    696 	int error;
    697 	int s;
    698 
    699 	if (sc->sc_dying)
    700 		return (EIO);
    701 
    702 	DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd));
    703 
    704 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    705 	if (error != EPASSTHROUGH)
    706 		return (error);
    707 
    708 	error = ttioctl(tp, cmd, data, flag, l);
    709 	if (error != EPASSTHROUGH)
    710 		return (error);
    711 
    712 	if (sc->sc_methods->ucom_ioctl != NULL) {
    713 		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
    714 			    sc->sc_portno, cmd, data, flag, l->l_proc);
    715 		if (error != EPASSTHROUGH)
    716 			return (error);
    717 	}
    718 
    719 	error = 0;
    720 
    721 	DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd));
    722 	s = spltty();
    723 
    724 	switch (cmd) {
    725 	case TIOCSBRK:
    726 		ucom_break(sc, 1);
    727 		break;
    728 
    729 	case TIOCCBRK:
    730 		ucom_break(sc, 0);
    731 		break;
    732 
    733 	case TIOCSDTR:
    734 		ucom_dtr(sc, 1);
    735 		break;
    736 
    737 	case TIOCCDTR:
    738 		ucom_dtr(sc, 0);
    739 		break;
    740 
    741 	case TIOCGFLAGS:
    742 		*(int *)data = sc->sc_swflags;
    743 		break;
    744 
    745 	case TIOCSFLAGS:
    746 		error = kauth_authorize_device_tty(l->l_cred,
    747 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    748 		if (error)
    749 			break;
    750 		sc->sc_swflags = *(int *)data;
    751 		break;
    752 
    753 	case TIOCMSET:
    754 	case TIOCMBIS:
    755 	case TIOCMBIC:
    756 		tiocm_to_ucom(sc, cmd, *(int *)data);
    757 		break;
    758 
    759 	case TIOCMGET:
    760 		*(int *)data = ucom_to_tiocm(sc);
    761 		break;
    762 
    763 	default:
    764 		error = EPASSTHROUGH;
    765 		break;
    766 	}
    767 
    768 	splx(s);
    769 
    770 	return (error);
    771 }
    772 
    773 static void
    774 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
    775 {
    776 	u_char combits;
    777 
    778 	combits = 0;
    779 	if (ISSET(ttybits, TIOCM_DTR))
    780 		SET(combits, UMCR_DTR);
    781 	if (ISSET(ttybits, TIOCM_RTS))
    782 		SET(combits, UMCR_RTS);
    783 
    784 	switch (how) {
    785 	case TIOCMBIC:
    786 		CLR(sc->sc_mcr, combits);
    787 		break;
    788 
    789 	case TIOCMBIS:
    790 		SET(sc->sc_mcr, combits);
    791 		break;
    792 
    793 	case TIOCMSET:
    794 		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
    795 		SET(sc->sc_mcr, combits);
    796 		break;
    797 	}
    798 
    799 	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
    800 		ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
    801 	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
    802 		ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
    803 }
    804 
    805 static int
    806 ucom_to_tiocm(struct ucom_softc *sc)
    807 {
    808 	u_char combits;
    809 	int ttybits = 0;
    810 
    811 	combits = sc->sc_mcr;
    812 	if (ISSET(combits, UMCR_DTR))
    813 		SET(ttybits, TIOCM_DTR);
    814 	if (ISSET(combits, UMCR_RTS))
    815 		SET(ttybits, TIOCM_RTS);
    816 
    817 	combits = sc->sc_msr;
    818 	if (ISSET(combits, UMSR_DCD))
    819 		SET(ttybits, TIOCM_CD);
    820 	if (ISSET(combits, UMSR_CTS))
    821 		SET(ttybits, TIOCM_CTS);
    822 	if (ISSET(combits, UMSR_DSR))
    823 		SET(ttybits, TIOCM_DSR);
    824 	if (ISSET(combits, UMSR_RI | UMSR_TERI))
    825 		SET(ttybits, TIOCM_RI);
    826 
    827 #if 0
    828 XXX;
    829 	if (sc->sc_ier != 0)
    830 		SET(ttybits, TIOCM_LE);
    831 #endif
    832 
    833 	return (ttybits);
    834 }
    835 
    836 static void
    837 ucom_break(struct ucom_softc *sc, int onoff)
    838 {
    839 	DPRINTF(("ucom_break: onoff=%d\n", onoff));
    840 
    841 	if (sc->sc_methods->ucom_set != NULL)
    842 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    843 		    UCOM_SET_BREAK, onoff);
    844 }
    845 
    846 static void
    847 ucom_dtr(struct ucom_softc *sc, int onoff)
    848 {
    849 	DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
    850 
    851 	if (sc->sc_methods->ucom_set != NULL)
    852 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    853 		    UCOM_SET_DTR, onoff);
    854 }
    855 
    856 static void
    857 ucom_rts(struct ucom_softc *sc, int onoff)
    858 {
    859 	DPRINTF(("ucom_rts: onoff=%d\n", onoff));
    860 
    861 	if (sc->sc_methods->ucom_set != NULL)
    862 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    863 		    UCOM_SET_RTS, onoff);
    864 }
    865 
    866 void
    867 ucom_status_change(struct ucom_softc *sc)
    868 {
    869 	struct tty *tp = sc->sc_tty;
    870 	u_char old_msr;
    871 
    872 	if (sc->sc_methods->ucom_get_status != NULL) {
    873 		old_msr = sc->sc_msr;
    874 		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
    875 		    &sc->sc_lsr, &sc->sc_msr);
    876 		if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD))
    877 			(*tp->t_linesw->l_modem)(tp,
    878 			    ISSET(sc->sc_msr, UMSR_DCD));
    879 	} else {
    880 		sc->sc_lsr = 0;
    881 		/* Assume DCD is present, if we have no chance to check it. */
    882 		sc->sc_msr = UMSR_DCD;
    883 	}
    884 }
    885 
    886 static int
    887 ucomparam(struct tty *tp, struct termios *t)
    888 {
    889 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
    890 	    UCOMUNIT(tp->t_dev));
    891 	int error;
    892 
    893 	if (sc->sc_dying)
    894 		return (EIO);
    895 
    896 	/* Check requested parameters. */
    897 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    898 		return (EINVAL);
    899 
    900 	/*
    901 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    902 	 * is always active.
    903 	 */
    904 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
    905 		SET(t->c_cflag, CLOCAL);
    906 		CLR(t->c_cflag, HUPCL);
    907 	}
    908 
    909 	/*
    910 	 * If there were no changes, don't do anything.  This avoids dropping
    911 	 * input and improves performance when all we did was frob things like
    912 	 * VMIN and VTIME.
    913 	 */
    914 	if (tp->t_ospeed == t->c_ospeed &&
    915 	    tp->t_cflag == t->c_cflag)
    916 		return (0);
    917 
    918 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
    919 
    920 	/* And copy to tty. */
    921 	tp->t_ispeed = 0;
    922 	tp->t_ospeed = t->c_ospeed;
    923 	tp->t_cflag = t->c_cflag;
    924 
    925 	if (sc->sc_methods->ucom_param != NULL) {
    926 		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
    927 			    t);
    928 		if (error)
    929 			return (error);
    930 	}
    931 
    932 	/* XXX worry about CHWFLOW */
    933 
    934 	/*
    935 	 * Update the tty layer's idea of the carrier bit, in case we changed
    936 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
    937 	 * explicit request.
    938 	 */
    939 	DPRINTF(("ucomparam: l_modem\n"));
    940 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
    941 
    942 #if 0
    943 XXX what if the hardware is not open
    944 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    945 		if (sc->sc_tx_stopped) {
    946 			sc->sc_tx_stopped = 0;
    947 			ucomstart(tp);
    948 		}
    949 	}
    950 #endif
    951 
    952 	return (0);
    953 }
    954 
    955 static int
    956 ucomhwiflow(struct tty *tp, int block)
    957 {
    958 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
    959 	    UCOMUNIT(tp->t_dev));
    960 	int old;
    961 
    962 	old = sc->sc_rx_stopped;
    963 	sc->sc_rx_stopped = (u_char)block;
    964 
    965 	if (old && !block) {
    966 		int s = splusb();
    967 		sc->sc_rx_unblock = 1;
    968 		softint_schedule(sc->sc_si);
    969 		splx(s);
    970 	}
    971 
    972 	return (1);
    973 }
    974 
    975 static void
    976 ucomstart(struct tty *tp)
    977 {
    978 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
    979 	    UCOMUNIT(tp->t_dev));
    980 	struct ucom_buffer *ub;
    981 	int s;
    982 	u_char *data;
    983 	int cnt;
    984 
    985 	if (sc->sc_dying)
    986 		return;
    987 
    988 	s = spltty();
    989 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
    990 		goto out;
    991 	if (sc->sc_tx_stopped)
    992 		goto out;
    993 
    994 	if (!ttypull(tp))
    995 		goto out;
    996 
    997 	/* Grab the first contiguous region of buffer space. */
    998 	data = tp->t_outq.c_cf;
    999 	cnt = ndqb(&tp->t_outq, 0);
   1000 
   1001 	if (cnt == 0)
   1002 		goto out;
   1003 
   1004 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_free);
   1005 	if (ub == NULL) {
   1006 		SET(tp->t_state, TS_BUSY);
   1007 		goto out;
   1008 	}
   1009 	SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_free, ub_link);
   1010 
   1011 	if (SIMPLEQ_FIRST(&sc->sc_obuff_free) == NULL)
   1012 		SET(tp->t_state, TS_BUSY);
   1013 
   1014 	if (cnt > sc->sc_obufsize)
   1015 		cnt = sc->sc_obufsize;
   1016 
   1017 	if (sc->sc_methods->ucom_write != NULL)
   1018 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
   1019 					   ub->ub_data, data, &cnt);
   1020 	else
   1021 		memcpy(ub->ub_data, data, cnt);
   1022 
   1023 	ub->ub_len = cnt;
   1024 	ub->ub_index = 0;
   1025 
   1026 	SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_full, ub, ub_link);
   1027 
   1028 	softint_schedule(sc->sc_si);
   1029 
   1030  out:
   1031 	splx(s);
   1032 }
   1033 
   1034 void
   1035 ucomstop(struct tty *tp, int flag)
   1036 {
   1037 #if 0
   1038 	/*struct ucom_softc *sc =
   1039 	    device_lookup_private(&ucom_cd, UCOMUNIT(tp->t_dev));*/
   1040 	int s;
   1041 
   1042 	s = spltty();
   1043 	if (ISSET(tp->t_state, TS_BUSY)) {
   1044 		/* sc->sc_tx_stopped = 1; */
   1045 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1046 			SET(tp->t_state, TS_FLUSH);
   1047 	}
   1048 	splx(s);
   1049 #endif
   1050 }
   1051 
   1052 static void
   1053 ucom_write_status(struct ucom_softc *sc, struct ucom_buffer *ub,
   1054     usbd_status err)
   1055 {
   1056 	struct tty *tp = sc->sc_tty;
   1057 	uint32_t cc = ub->ub_len;
   1058 
   1059 	switch (err) {
   1060 	case USBD_IN_PROGRESS:
   1061 		ub->ub_index = ub->ub_len;
   1062 		break;
   1063 	case USBD_STALLED:
   1064 		ub->ub_index = 0;
   1065 		softint_schedule(sc->sc_si);
   1066 		break;
   1067 	case USBD_NORMAL_COMPLETION:
   1068 		usbd_get_xfer_status(ub->ub_xfer, NULL, NULL, &cc, NULL);
   1069 		rnd_add_uint32(&sc->sc_rndsource, cc);
   1070 		/*FALLTHROUGH*/
   1071 	default:
   1072 		SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_full, ub_link);
   1073 		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
   1074 		cc -= sc->sc_opkthdrlen;
   1075 
   1076 		mutex_spin_enter(&tty_lock);
   1077 		CLR(tp->t_state, TS_BUSY);
   1078 		if (ISSET(tp->t_state, TS_FLUSH))
   1079 			CLR(tp->t_state, TS_FLUSH);
   1080 		else
   1081 			ndflush(&tp->t_outq, cc);
   1082 		mutex_spin_exit(&tty_lock);
   1083 
   1084 		if (err != USBD_CANCELLED && err != USBD_IOERROR &&
   1085 		    !sc->sc_dying) {
   1086 			if ((ub = SIMPLEQ_FIRST(&sc->sc_obuff_full)) != NULL)
   1087 				ucom_submit_write(sc, ub);
   1088 
   1089 			(*tp->t_linesw->l_start)(tp);
   1090 		}
   1091 		break;
   1092 	}
   1093 }
   1094 
   1095 /* Call at spltty() */
   1096 static void
   1097 ucom_submit_write(struct ucom_softc *sc, struct ucom_buffer *ub)
   1098 {
   1099 
   1100 	usbd_setup_xfer(ub->ub_xfer, sc->sc_bulkout_pipe,
   1101 	    (usbd_private_handle)sc, ub->ub_data, ub->ub_len,
   1102 	    USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
   1103 
   1104 	ucom_write_status(sc, ub, usbd_transfer(ub->ub_xfer));
   1105 }
   1106 
   1107 static void
   1108 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
   1109 {
   1110 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1111 	int s;
   1112 
   1113 	s = spltty();
   1114 
   1115 	ucom_write_status(sc, SIMPLEQ_FIRST(&sc->sc_obuff_full), status);
   1116 
   1117 	splx(s);
   1118 }
   1119 
   1120 static void
   1121 ucom_softintr(void *arg)
   1122 {
   1123 	struct ucom_softc *sc = arg;
   1124 	struct tty *tp = sc->sc_tty;
   1125 	struct ucom_buffer *ub;
   1126 	int s;
   1127 
   1128 	if (!ISSET(tp->t_state, TS_ISOPEN))
   1129 		return;
   1130 
   1131 	s = spltty();
   1132 
   1133 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_full);
   1134 
   1135 	if (ub != NULL && ub->ub_index == 0)
   1136 		ucom_submit_write(sc, ub);
   1137 
   1138 	if (sc->sc_rx_unblock)
   1139 		ucom_read_complete(sc);
   1140 
   1141 	splx(s);
   1142 }
   1143 
   1144 static void
   1145 ucom_read_complete(struct ucom_softc *sc)
   1146 {
   1147 	int (*rint)(int, struct tty *);
   1148 	struct ucom_buffer *ub;
   1149 	struct tty *tp;
   1150 	int s;
   1151 
   1152 	tp = sc->sc_tty;
   1153 	rint = tp->t_linesw->l_rint;
   1154 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1155 
   1156 	while (ub != NULL && !sc->sc_rx_stopped) {
   1157 
   1158 		s = spltty();
   1159 
   1160 		while (ub->ub_index < ub->ub_len && !sc->sc_rx_stopped) {
   1161 			/* Give characters to tty layer. */
   1162 			if ((*rint)(ub->ub_data[ub->ub_index], tp) == -1) {
   1163 				/* Overflow: drop remainder */
   1164 				ub->ub_index = ub->ub_len;
   1165 			} else
   1166 				ub->ub_index++;
   1167 		}
   1168 
   1169 		splx(s);
   1170 
   1171 		if (ub->ub_index == ub->ub_len) {
   1172 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
   1173 
   1174 			ucomsubmitread(sc, ub);
   1175 
   1176 			ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1177 		}
   1178 	}
   1179 
   1180 	sc->sc_rx_unblock = (ub != NULL);
   1181 }
   1182 
   1183 static usbd_status
   1184 ucomsubmitread(struct ucom_softc *sc, struct ucom_buffer *ub)
   1185 {
   1186 	usbd_status err;
   1187 
   1188 	usbd_setup_xfer(ub->ub_xfer, sc->sc_bulkin_pipe,
   1189 	    (usbd_private_handle)sc, ub->ub_data, sc->sc_ibufsize,
   1190 	    USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, ucomreadcb);
   1191 
   1192 	if ((err = usbd_transfer(ub->ub_xfer)) != USBD_IN_PROGRESS) {
   1193 		/* XXX: Recover from this, please! */
   1194 		printf("ucomsubmitread: err=%s\n", usbd_errstr(err));
   1195 		return (err);
   1196 	}
   1197 
   1198 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_empty, ub, ub_link);
   1199 
   1200 	return (USBD_NORMAL_COMPLETION);
   1201 }
   1202 
   1203 static void
   1204 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
   1205 {
   1206 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1207 	struct tty *tp = sc->sc_tty;
   1208 	struct ucom_buffer *ub;
   1209 	u_int32_t cc;
   1210 	u_char *cp;
   1211 	int s;
   1212 
   1213 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty);
   1214 	SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_empty, ub_link);
   1215 
   1216 	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
   1217 	    sc->sc_dying) {
   1218 		DPRINTF(("ucomreadcb: dying\n"));
   1219 		ub->ub_index = ub->ub_len = 0;
   1220 		/* Send something to wake upper layer */
   1221 		s = spltty();
   1222 		if (status != USBD_CANCELLED) {
   1223 			(tp->t_linesw->l_rint)('\n', tp);
   1224 			mutex_spin_enter(&tty_lock);	/* XXX */
   1225 			ttwakeup(tp);
   1226 			mutex_spin_exit(&tty_lock);	/* XXX */
   1227 		}
   1228 		splx(s);
   1229 		return;
   1230 	}
   1231 
   1232 	if (status == USBD_STALLED) {
   1233 		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
   1234 		ucomsubmitread(sc, ub);
   1235 		return;
   1236 	}
   1237 
   1238 	if (status != USBD_NORMAL_COMPLETION) {
   1239 		printf("ucomreadcb: wonky status=%s\n", usbd_errstr(status));
   1240 		return;
   1241 	}
   1242 
   1243 	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
   1244 
   1245 #ifdef UCOM_DEBUG
   1246 	/* This is triggered by uslsa(4) occasionally. */
   1247 	if ((ucomdebug > 0) && (cc == 0)) {
   1248 		device_printf(sc->sc_dev, "ucomreadcb: zero length xfer!\n");
   1249 	}
   1250 #endif
   1251 
   1252 	KDASSERT(cp == ub->ub_data);
   1253 
   1254 	rnd_add_uint32(&sc->sc_rndsource, cc);
   1255 
   1256 	if (sc->sc_opening) {
   1257 		ucomsubmitread(sc, ub);
   1258 		return;
   1259 	}
   1260 
   1261 	if (sc->sc_methods->ucom_read != NULL) {
   1262 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
   1263 		    &cp, &cc);
   1264 		ub->ub_index = (u_int)(cp - ub->ub_data);
   1265 	} else
   1266 		ub->ub_index = 0;
   1267 
   1268 	ub->ub_len = cc;
   1269 
   1270 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_full, ub, ub_link);
   1271 
   1272 	ucom_read_complete(sc);
   1273 }
   1274 
   1275 static void
   1276 ucom_cleanup(struct ucom_softc *sc)
   1277 {
   1278 	struct ucom_buffer *ub;
   1279 
   1280 	DPRINTF(("ucom_cleanup: closing pipes\n"));
   1281 
   1282 	ucom_shutdown(sc);
   1283 	if (sc->sc_bulkin_pipe != NULL) {
   1284 		usbd_abort_pipe(sc->sc_bulkin_pipe);
   1285 		usbd_close_pipe(sc->sc_bulkin_pipe);
   1286 		sc->sc_bulkin_pipe = NULL;
   1287 	}
   1288 	if (sc->sc_bulkout_pipe != NULL) {
   1289 		usbd_abort_pipe(sc->sc_bulkout_pipe);
   1290 		usbd_close_pipe(sc->sc_bulkout_pipe);
   1291 		sc->sc_bulkout_pipe = NULL;
   1292 	}
   1293 	for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS]; ub++) {
   1294 		if (ub->ub_xfer != NULL) {
   1295 			usbd_free_xfer(ub->ub_xfer);
   1296 			ub->ub_xfer = NULL;
   1297 			ub->ub_data = NULL;
   1298 		}
   1299 	}
   1300 	for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS]; ub++){
   1301 		if (ub->ub_xfer != NULL) {
   1302 			usbd_free_xfer(ub->ub_xfer);
   1303 			ub->ub_xfer = NULL;
   1304 			ub->ub_data = NULL;
   1305 		}
   1306 	}
   1307 }
   1308 
   1309 #endif /* NUCOM > 0 */
   1310 
   1311 int
   1312 ucomprint(void *aux, const char *pnp)
   1313 {
   1314 	struct ucom_attach_args *uca = aux;
   1315 
   1316 	if (pnp)
   1317 		aprint_normal("ucom at %s", pnp);
   1318 	if (uca->portno != UCOM_UNK_PORTNO)
   1319 		aprint_normal(" portno %d", uca->portno);
   1320 	return (UNCONF);
   1321 }
   1322 
   1323 int
   1324 ucomsubmatch(device_t parent, cfdata_t cf,
   1325 	     const int *ldesc, void *aux)
   1326 {
   1327 	struct ucom_attach_args *uca = aux;
   1328 
   1329 	if (uca->portno != UCOM_UNK_PORTNO &&
   1330 	    cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
   1331 	    cf->cf_loc[UCOMBUSCF_PORTNO] != uca->portno)
   1332 		return (0);
   1333 	return (config_match(parent, cf, aux));
   1334 }
   1335