Home | History | Annotate | Line # | Download | only in usb
ucom.c revision 1.130
      1 /*	$NetBSD: ucom.c,v 1.130 2022/03/28 12:42:37 riastradh 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.130 2022/03/28 12:42:37 riastradh Exp $");
     38 
     39 #ifdef _KERNEL_OPT
     40 #include "opt_usb.h"
     41 #endif
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/conf.h>
     48 #include <sys/tty.h>
     49 #include <sys/file.h>
     50 #include <sys/select.h>
     51 #include <sys/proc.h>
     52 #include <sys/vnode.h>
     53 #include <sys/device.h>
     54 #include <sys/poll.h>
     55 #include <sys/queue.h>
     56 #include <sys/kauth.h>
     57 #include <sys/sysctl.h>
     58 #include <sys/timepps.h>
     59 #include <sys/rndsource.h>
     60 
     61 #include <dev/usb/usb.h>
     62 
     63 #include <dev/usb/usbdi.h>
     64 #include <dev/usb/usbdi_util.h>
     65 #include <dev/usb/usbdevs.h>
     66 #include <dev/usb/usb_quirks.h>
     67 #include <dev/usb/usbhist.h>
     68 
     69 #include <dev/usb/ucomvar.h>
     70 
     71 #include "ucom.h"
     72 #include "locators.h"
     73 #include "ioconf.h"
     74 
     75 #if NUCOM > 0
     76 
     77 #ifdef USB_DEBUG
     78 #ifndef UCOM_DEBUG
     79 #define ucomdebug 0
     80 #else
     81 int ucomdebug = 0;
     82 
     83 SYSCTL_SETUP(sysctl_hw_ucom_setup, "sysctl hw.ucom setup")
     84 {
     85 	int err;
     86 	const struct sysctlnode *rnode;
     87 	const struct sysctlnode *cnode;
     88 
     89 	err = sysctl_createv(clog, 0, NULL, &rnode,
     90 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ucom",
     91 	    SYSCTL_DESCR("ucom global controls"),
     92 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
     93 
     94 	if (err)
     95 		goto fail;
     96 
     97 	/* control debugging printfs */
     98 	err = sysctl_createv(clog, 0, &rnode, &cnode,
     99 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    100 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    101 	    NULL, 0, &ucomdebug, sizeof(ucomdebug), CTL_CREATE, CTL_EOL);
    102 	if (err)
    103 		goto fail;
    104 
    105 	return;
    106 fail:
    107 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    108 }
    109 
    110 #endif /* UCOM_DEBUG */
    111 #endif /* USB_DEBUG */
    112 
    113 #define DPRINTF(FMT,A,B,C,D)    USBHIST_LOGN(ucomdebug,1,FMT,A,B,C,D)
    114 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(ucomdebug,N,FMT,A,B,C,D)
    115 #define UCOMHIST_FUNC()         USBHIST_FUNC()
    116 #define UCOMHIST_CALLED(name)   USBHIST_CALLED(ucomdebug)
    117 
    118 #define	UCOMCALLUNIT_MASK	TTCALLUNIT_MASK
    119 #define	UCOMUNIT_MASK		TTUNIT_MASK
    120 #define	UCOMDIALOUT_MASK	TTDIALOUT_MASK
    121 
    122 #define	UCOMCALLUNIT(x)		TTCALLUNIT(x)
    123 #define	UCOMUNIT(x)		TTUNIT(x)
    124 #define	UCOMDIALOUT(x)		TTDIALOUT(x)
    125 
    126 /*
    127  * XXX: We can submit multiple input/output buffers to the usb stack
    128  * to improve throughput, but the usb stack is too lame to deal with this
    129  * in a number of places.
    130  */
    131 #define	UCOM_IN_BUFFS	1
    132 #define	UCOM_OUT_BUFFS	1
    133 
    134 struct ucom_buffer {
    135 	SIMPLEQ_ENTRY(ucom_buffer) ub_link;
    136 	struct usbd_xfer *ub_xfer;
    137 	u_char *ub_data;
    138 	u_int ub_len;
    139 	u_int ub_index;
    140 };
    141 
    142 struct ucom_softc {
    143 	device_t		sc_dev;		/* base device */
    144 
    145 	struct usbd_device *	sc_udev;	/* USB device */
    146 	struct usbd_interface *	sc_iface;	/* data interface */
    147 
    148 	int			sc_bulkin_no;	/* bulk in endpoint address */
    149 	struct usbd_pipe *	sc_bulkin_pipe;/* bulk in pipe */
    150 	u_int			sc_ibufsize;	/* read buffer size */
    151 	u_int			sc_ibufsizepad;	/* read buffer size padded */
    152 	struct ucom_buffer	sc_ibuff[UCOM_IN_BUFFS];
    153 	SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_empty;
    154 	SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_full;
    155 
    156 	int			sc_bulkout_no;	/* bulk out endpoint address */
    157 	struct usbd_pipe *	sc_bulkout_pipe;/* bulk out pipe */
    158 	u_int			sc_obufsize;	/* write buffer size */
    159 	u_int			sc_opkthdrlen;	/* header length of */
    160 	struct ucom_buffer	sc_obuff[UCOM_OUT_BUFFS];
    161 	SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_free;
    162 	SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_full;
    163 
    164 	void			*sc_si;
    165 
    166 	const struct ucom_methods *sc_methods;
    167 	void                    *sc_parent;
    168 	int			sc_portno;
    169 
    170 	struct tty		*sc_tty;	/* our tty */
    171 	u_char			sc_lsr;
    172 	u_char			sc_msr;
    173 	u_char			sc_mcr;
    174 	volatile u_char		sc_rx_stopped;
    175 	u_char			sc_rx_unblock;
    176 	u_char			sc_tx_stopped;
    177 	int			sc_swflags;
    178 
    179 	enum ucom_state {
    180 	    UCOM_DEAD,
    181 	    UCOM_ATTACHED,
    182 	    UCOM_OPENING,
    183 	    UCOM_OPEN
    184 	}			sc_state;
    185 	bool			sc_closing;	/* software is closing */
    186 	bool			sc_dying;	/* hardware is gone */
    187 
    188 	struct pps_state	sc_pps_state;	/* pps state */
    189 
    190 	krndsource_t		sc_rndsource;	/* random source */
    191 
    192 	kmutex_t		sc_lock;
    193 	kcondvar_t		sc_statecv;
    194 
    195 	struct timeval		sc_hup_time;
    196 };
    197 
    198 dev_type_open(ucomopen);
    199 dev_type_cancel(ucomcancel);
    200 dev_type_close(ucomclose);
    201 dev_type_read(ucomread);
    202 dev_type_write(ucomwrite);
    203 dev_type_ioctl(ucomioctl);
    204 dev_type_stop(ucomstop);
    205 dev_type_tty(ucomtty);
    206 dev_type_poll(ucompoll);
    207 
    208 const struct cdevsw ucom_cdevsw = {
    209 	.d_open = ucomopen,
    210 	.d_cancel = ucomcancel,
    211 	.d_close = ucomclose,
    212 	.d_read = ucomread,
    213 	.d_write = ucomwrite,
    214 	.d_ioctl = ucomioctl,
    215 	.d_stop = ucomstop,
    216 	.d_tty = ucomtty,
    217 	.d_poll = ucompoll,
    218 	.d_mmap = nommap,
    219 	.d_kqfilter = ttykqfilter,
    220 	.d_discard = nodiscard,
    221 	.d_cfdriver = &ucom_cd,
    222 	.d_devtounit = dev_minor_unit,
    223 	.d_flag = D_TTY | D_MPSAFE
    224 };
    225 
    226 static void	ucom_cleanup(struct ucom_softc *, int);
    227 static int	ucomparam(struct tty *, struct termios *);
    228 static int	ucomhwiflow(struct tty *, int);
    229 static void	ucomstart(struct tty *);
    230 static void	ucom_shutdown(struct ucom_softc *);
    231 static void	ucom_dtr(struct ucom_softc *, int);
    232 static void	ucom_rts(struct ucom_softc *, int);
    233 static void	ucom_break(struct ucom_softc *, int);
    234 static void	tiocm_to_ucom(struct ucom_softc *, u_long, int);
    235 static int	ucom_to_tiocm(struct ucom_softc *);
    236 
    237 static void	ucomreadcb(struct usbd_xfer *, void *, usbd_status);
    238 static void	ucom_submit_write(struct ucom_softc *, struct ucom_buffer *);
    239 static void	ucom_write_status(struct ucom_softc *, struct ucom_buffer *,
    240 		    usbd_status);
    241 
    242 static void	ucomwritecb(struct usbd_xfer *, void *, usbd_status);
    243 static void	ucom_read_complete(struct ucom_softc *);
    244 static int	ucomsubmitread(struct ucom_softc *, struct ucom_buffer *);
    245 static void	ucom_softintr(void *);
    246 
    247 int ucom_match(device_t, cfdata_t, void *);
    248 void ucom_attach(device_t, device_t, void *);
    249 int ucom_detach(device_t, int);
    250 
    251 CFATTACH_DECL_NEW(ucom, sizeof(struct ucom_softc), ucom_match, ucom_attach,
    252     ucom_detach, NULL);
    253 
    254 int
    255 ucom_match(device_t parent, cfdata_t match, void *aux)
    256 {
    257 	return 1;
    258 }
    259 
    260 void
    261 ucom_attach(device_t parent, device_t self, void *aux)
    262 {
    263 	struct ucom_softc *sc = device_private(self);
    264 	struct ucom_attach_args *ucaa = aux;
    265 
    266 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    267 
    268 	if (ucaa->ucaa_info != NULL)
    269 		aprint_normal(": %s", ucaa->ucaa_info);
    270 	aprint_normal("\n");
    271 
    272 	prop_dictionary_set_int32(device_properties(self), "port",
    273 	    ucaa->ucaa_portno);
    274 
    275 	sc->sc_dev = self;
    276 	sc->sc_udev = ucaa->ucaa_device;
    277 	sc->sc_iface = ucaa->ucaa_iface;
    278 	sc->sc_bulkout_no = ucaa->ucaa_bulkout;
    279 	sc->sc_bulkin_no = ucaa->ucaa_bulkin;
    280 	sc->sc_ibufsize = ucaa->ucaa_ibufsize;
    281 	sc->sc_ibufsizepad = ucaa->ucaa_ibufsizepad;
    282 	sc->sc_obufsize = ucaa->ucaa_obufsize;
    283 	sc->sc_opkthdrlen = ucaa->ucaa_opkthdrlen;
    284 	sc->sc_methods = ucaa->ucaa_methods;
    285 	sc->sc_parent = ucaa->ucaa_arg;
    286 	sc->sc_portno = ucaa->ucaa_portno;
    287 
    288 	sc->sc_lsr = 0;
    289 	sc->sc_msr = 0;
    290 	sc->sc_mcr = 0;
    291 	sc->sc_tx_stopped = 0;
    292 	sc->sc_swflags = 0;
    293 	sc->sc_closing = false;
    294 	sc->sc_dying = false;
    295 	sc->sc_state = UCOM_DEAD;
    296 
    297 	sc->sc_si = softint_establish(SOFTINT_USB, ucom_softintr, sc);
    298 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    299 	cv_init(&sc->sc_statecv, "ucomstate");
    300 
    301 	SIMPLEQ_INIT(&sc->sc_ibuff_empty);
    302 	SIMPLEQ_INIT(&sc->sc_ibuff_full);
    303 	SIMPLEQ_INIT(&sc->sc_obuff_free);
    304 	SIMPLEQ_INIT(&sc->sc_obuff_full);
    305 
    306 	memset(sc->sc_ibuff, 0, sizeof(sc->sc_ibuff));
    307 	memset(sc->sc_obuff, 0, sizeof(sc->sc_obuff));
    308 
    309 	DPRINTF("open pipes in=%jd out=%jd",
    310 	    sc->sc_bulkin_no, sc->sc_bulkout_no, 0, 0);
    311 
    312 	struct ucom_buffer *ub;
    313 	usbd_status err;
    314 	int error;
    315 
    316 	/* Open the bulk pipes */
    317 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no,
    318 	    USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
    319 	if (err) {
    320 		DPRINTF("open bulk in error (addr %jd), err=%jd",
    321 		    sc->sc_bulkin_no, err, 0, 0);
    322 		error = EIO;
    323 		goto fail_0;
    324 	}
    325 	/* Allocate input buffers */
    326 	for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
    327 	    ub++) {
    328 		error = usbd_create_xfer(sc->sc_bulkin_pipe,
    329 		    sc->sc_ibufsizepad, 0, 0,
    330 		    &ub->ub_xfer);
    331 		if (error)
    332 			goto fail_1;
    333 		ub->ub_data = usbd_get_buffer(ub->ub_xfer);
    334 	}
    335 
    336 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
    337 	    USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
    338 	if (err) {
    339 		DPRINTF("open bulk out error (addr %jd), err=%jd",
    340 		    sc->sc_bulkout_no, err, 0, 0);
    341 		error = EIO;
    342 		goto fail_1;
    343 	}
    344 	for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
    345 	    ub++) {
    346 		error = usbd_create_xfer(sc->sc_bulkout_pipe,
    347 		    sc->sc_obufsize, 0, 0, &ub->ub_xfer);
    348 		if (error)
    349 			goto fail_2;
    350 		ub->ub_data = usbd_get_buffer(ub->ub_xfer);
    351 		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
    352 	}
    353 
    354 	struct tty *tp = tty_alloc();
    355 	tp->t_oproc = ucomstart;
    356 	tp->t_param = ucomparam;
    357 	tp->t_hwiflow = ucomhwiflow;
    358 	sc->sc_tty = tp;
    359 
    360 	DPRINTF("tty_attach %#jx", (uintptr_t)tp, 0, 0, 0);
    361 	tty_attach(tp);
    362 
    363 	rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev),
    364 			  RND_TYPE_TTY, RND_FLAG_DEFAULT);
    365 
    366 	if (!pmf_device_register(self, NULL, NULL))
    367 		aprint_error_dev(self, "couldn't establish power handler\n");
    368 
    369 	sc->sc_state = UCOM_ATTACHED;
    370 	return;
    371 
    372 fail_2:
    373 	for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
    374 	    ub++) {
    375 		if (ub->ub_xfer)
    376 			usbd_destroy_xfer(ub->ub_xfer);
    377 	}
    378 	usbd_close_pipe(sc->sc_bulkout_pipe);
    379 	sc->sc_bulkout_pipe = NULL;
    380 
    381 fail_1:
    382 	for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
    383 	    ub++) {
    384 		if (ub->ub_xfer)
    385 			usbd_destroy_xfer(ub->ub_xfer);
    386 	}
    387 	usbd_close_pipe(sc->sc_bulkin_pipe);
    388 	sc->sc_bulkin_pipe = NULL;
    389 
    390 fail_0:
    391 	aprint_error_dev(self, "attach failed, error=%d\n", error);
    392 }
    393 
    394 int
    395 ucom_detach(device_t self, int flags)
    396 {
    397 	struct ucom_softc *sc = device_private(self);
    398 	struct tty *tp = sc->sc_tty;
    399 	int maj, mn;
    400 	int i;
    401 
    402 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    403 
    404 	DPRINTF("sc=%#jx flags=%jd tp=%#jx", (uintptr_t)sc, flags,
    405 	    (uintptr_t)tp, 0);
    406 	DPRINTF("... pipe=%jd,%jd", sc->sc_bulkin_no, sc->sc_bulkout_no, 0, 0);
    407 
    408 	/* Prevent new opens from hanging.  */
    409 	mutex_enter(&sc->sc_lock);
    410 	sc->sc_dying = true;
    411 	mutex_exit(&sc->sc_lock);
    412 
    413 	pmf_device_deregister(self);
    414 
    415 	/* tty is now off.  */
    416 	if (tp != NULL) {
    417 		mutex_spin_enter(&tty_lock);
    418 		CLR(tp->t_state, TS_CARR_ON);
    419 		CLR(tp->t_cflag, CLOCAL | MDMBUF);
    420 		mutex_spin_exit(&tty_lock);
    421 	}
    422 
    423 	/* locate the major number */
    424 	maj = cdevsw_lookup_major(&ucom_cdevsw);
    425 
    426 	/* Nuke the vnodes for any open instances. */
    427 	mn = device_unit(self);
    428 	DPRINTF("maj=%jd mn=%jd", maj, mn, 0, 0);
    429 	vdevgone(maj, mn, mn, VCHR);
    430 	vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR);
    431 	vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR);
    432 
    433 	softint_disestablish(sc->sc_si);
    434 
    435 	/* Detach and free the tty. */
    436 	if (tp != NULL) {
    437 		tty_detach(tp);
    438 		tty_free(tp);
    439 		sc->sc_tty = NULL;
    440 	}
    441 
    442 	for (i = 0; i < UCOM_IN_BUFFS; i++) {
    443 		if (sc->sc_ibuff[i].ub_xfer != NULL)
    444 			usbd_destroy_xfer(sc->sc_ibuff[i].ub_xfer);
    445 	}
    446 
    447 	for (i = 0; i < UCOM_OUT_BUFFS; i++) {
    448 		if (sc->sc_obuff[i].ub_xfer != NULL)
    449 			usbd_destroy_xfer(sc->sc_obuff[i].ub_xfer);
    450 	}
    451 
    452 	if (sc->sc_bulkin_pipe != NULL) {
    453 		usbd_close_pipe(sc->sc_bulkin_pipe);
    454 		sc->sc_bulkin_pipe = NULL;
    455 	}
    456 
    457 	if (sc->sc_bulkout_pipe != NULL) {
    458 		usbd_close_pipe(sc->sc_bulkout_pipe);
    459 		sc->sc_bulkout_pipe = NULL;
    460 	}
    461 
    462 	/* Detach the random source */
    463 	rnd_detach_source(&sc->sc_rndsource);
    464 
    465 	mutex_destroy(&sc->sc_lock);
    466 	cv_destroy(&sc->sc_statecv);
    467 
    468 	return 0;
    469 }
    470 
    471 void
    472 ucom_shutdown(struct ucom_softc *sc)
    473 {
    474 	struct tty *tp = sc->sc_tty;
    475 
    476 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    477 
    478 	/*
    479 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    480 	 * notice even if we immediately open the port again.
    481 	 */
    482 	if (ISSET(tp->t_cflag, HUPCL)) {
    483 		ucom_dtr(sc, 0);
    484 		mutex_enter(&sc->sc_lock);
    485 		microuptime(&sc->sc_hup_time);
    486 		sc->sc_hup_time.tv_sec++;
    487 		mutex_exit(&sc->sc_lock);
    488 	}
    489 }
    490 
    491 /*
    492  * ucomopen(dev, flag, mode, l)
    493  *
    494  *	Called when anyone tries to open /dev/ttyU? for an existing
    495  *	ucom? instance that has completed attach.  The attach may have
    496  *	failed, though, or there may be concurrent detach or close in
    497  *	progress, so fail if attach failed (no sc_tty) or detach has
    498  *	begun (sc_dying), or wait if there's a concurrent close in
    499  *	progress before reopening.
    500  */
    501 int
    502 ucomopen(dev_t dev, int flag, int mode, struct lwp *l)
    503 {
    504 	const int unit = UCOMUNIT(dev);
    505 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    506 	int error = 0;
    507 
    508 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    509 
    510 	mutex_enter(&sc->sc_lock);
    511 	if (sc->sc_dying) {
    512 		DPRINTF("... dying", 0, 0, 0, 0);
    513 		mutex_exit(&sc->sc_lock);
    514 		return ENXIO;
    515 	}
    516 
    517 	if (!device_is_active(sc->sc_dev)) {
    518 		mutex_exit(&sc->sc_lock);
    519 		return ENXIO;
    520 	}
    521 
    522 	struct tty *tp = sc->sc_tty;
    523 	if (tp == NULL) {
    524 		DPRINTF("... not attached", 0, 0, 0, 0);
    525 		mutex_exit(&sc->sc_lock);
    526 		return ENXIO;
    527 	}
    528 
    529 	DPRINTF("unit=%jd, tp=%#jx", unit, (uintptr_t)tp, 0, 0);
    530 
    531 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) {
    532 		mutex_exit(&sc->sc_lock);
    533 		return EBUSY;
    534 	}
    535 
    536 	/*
    537 	 * If the previous use had set DTR on close, wait up to one
    538 	 * second for the other side to notice we hung up.  After
    539 	 * sleeping, the tty may have been revoked, so restart the
    540 	 * whole operation.
    541 	 *
    542 	 * XXX The wchan is not ttclose but maybe should be.
    543 	 */
    544 	if (timerisset(&sc->sc_hup_time)) {
    545 		struct timeval now, delta;
    546 		int ms, ticks;
    547 
    548 		microuptime(&now);
    549 		if (timercmp(&now, &sc->sc_hup_time, <)) {
    550 			timersub(&sc->sc_hup_time, &now, &delta);
    551 			ms = MIN(INT_MAX - 1000, delta.tv_sec*1000);
    552 			ms += howmany(delta.tv_usec, 1000);
    553 			ticks = MAX(1, MIN(INT_MAX, mstohz(ms)));
    554 			error = cv_timedwait_sig(&sc->sc_statecv, &sc->sc_lock,
    555 			    ticks);
    556 			mutex_exit(&sc->sc_lock);
    557 			return error ? error : ERESTART;
    558 		}
    559 	}
    560 
    561 	/*
    562 	 * Wait while the device is initialized by the
    563 	 * first opener or cleaned up by the last closer.
    564 	 */
    565 	enum ucom_state state = sc->sc_state;
    566 	if (state == UCOM_OPENING || sc->sc_closing) {
    567 		if (flag & FNONBLOCK)
    568 			error = EWOULDBLOCK;
    569 		else
    570 			error = cv_wait_sig(&sc->sc_statecv, &sc->sc_lock);
    571 		mutex_exit(&sc->sc_lock);
    572 		return error ? error : ERESTART;
    573 	}
    574 	KASSERTMSG(state == UCOM_OPEN || state == UCOM_ATTACHED,
    575 	    "state is %d", state);
    576 
    577 	/*
    578 	 * If this is the first open, then make sure the pipes are
    579 	 * running and perform any initialization needed.
    580 	 */
    581 	bool firstopen = (state == UCOM_ATTACHED);
    582 	if (firstopen) {
    583 		KASSERT(!ISSET(tp->t_state, TS_ISOPEN));
    584 		KASSERT(tp->t_wopen == 0);
    585 
    586 		tp->t_dev = dev;
    587 		sc->sc_state = UCOM_OPENING;
    588 		mutex_exit(&sc->sc_lock);
    589 
    590 		if (sc->sc_methods->ucom_open != NULL) {
    591 			error = sc->sc_methods->ucom_open(sc->sc_parent,
    592 			    sc->sc_portno);
    593 			if (error)
    594 				goto bad;
    595 		}
    596 
    597 		ucom_status_change(sc);
    598 
    599 		/* Clear PPS capture state on first open. */
    600 		mutex_spin_enter(&timecounter_lock);
    601 		memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state));
    602 		sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
    603 		pps_init(&sc->sc_pps_state);
    604 		mutex_spin_exit(&timecounter_lock);
    605 
    606 		/*
    607 		 * Initialize the termios status to the defaults.  Add in the
    608 		 * sticky bits from TIOCSFLAGS.
    609 		 */
    610 		struct termios t;
    611 
    612 		t.c_ispeed = 0;
    613 		t.c_ospeed = TTYDEF_SPEED;
    614 		t.c_cflag = TTYDEF_CFLAG;
    615 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    616 			SET(t.c_cflag, CLOCAL);
    617 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    618 			SET(t.c_cflag, CRTSCTS);
    619 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    620 			SET(t.c_cflag, MDMBUF);
    621 		/* Make sure ucomparam() will do something. */
    622 		tp->t_ospeed = 0;
    623 		(void) ucomparam(tp, &t);
    624 		tp->t_iflag = TTYDEF_IFLAG;
    625 		tp->t_oflag = TTYDEF_OFLAG;
    626 		tp->t_lflag = TTYDEF_LFLAG;
    627 		ttychars(tp);
    628 		ttsetwater(tp);
    629 
    630 		/*
    631 		 * Turn on DTR.  We must always do this, even if carrier is not
    632 		 * present, because otherwise we'd have to use TIOCSDTR
    633 		 * immediately after setting CLOCAL, which applications do not
    634 		 * expect.  We always assert DTR while the device is open
    635 		 * unless explicitly requested to deassert it.  Ditto RTS.
    636 		 */
    637 		ucom_dtr(sc, 1);
    638 		ucom_rts(sc, 1);
    639 
    640 		mutex_enter(&sc->sc_lock);
    641 		sc->sc_rx_unblock = 0;
    642 		sc->sc_rx_stopped = 0;
    643 		sc->sc_tx_stopped = 0;
    644 
    645 		for (size_t i = 0; i < UCOM_IN_BUFFS; i++) {
    646 			struct ucom_buffer *ub = &sc->sc_ibuff[i];
    647 			error = ucomsubmitread(sc, ub);
    648 			if (error) {
    649 		    		mutex_exit(&sc->sc_lock);
    650 				goto bad;
    651 			}
    652 		}
    653 	}
    654 	mutex_exit(&sc->sc_lock);
    655 
    656 	DPRINTF("unit=%jd, tp=%#jx dialout %jd nonblock %jd", unit,
    657 	    (uintptr_t)tp, !!UCOMDIALOUT(dev), !!ISSET(flag, O_NONBLOCK));
    658 	error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    659 	if (error)
    660 		goto bad;
    661 
    662 	error = (*tp->t_linesw->l_open)(dev, tp);
    663 	if (error)
    664 		goto bad;
    665 
    666 	/*
    667 	 * Success!  If this was the first open, notify waiters that
    668 	 * the tty is open for business.
    669 	 */
    670 	if (firstopen) {
    671 		mutex_enter(&sc->sc_lock);
    672 		KASSERT(sc->sc_state == UCOM_OPENING);
    673 		sc->sc_state = UCOM_OPEN;
    674 		cv_broadcast(&sc->sc_statecv);
    675 		mutex_exit(&sc->sc_lock);
    676 	}
    677 	return 0;
    678 
    679 bad:
    680 	/*
    681 	 * Failure!  If this was the first open, hang up, abort pipes,
    682 	 * and notify waiters that we're not opening after all.
    683 	 */
    684 	if (firstopen) {
    685 		ucom_cleanup(sc, flag);
    686 
    687 		mutex_enter(&sc->sc_lock);
    688 		KASSERT(sc->sc_state == UCOM_OPENING);
    689 		sc->sc_state = UCOM_ATTACHED;
    690 		cv_broadcast(&sc->sc_statecv);
    691 		mutex_exit(&sc->sc_lock);
    692 	}
    693 	return error;
    694 }
    695 
    696 /*
    697  * ucomcancel(dev, flag, mode, l)
    698  *
    699  *	Called on revoke or last close.  Must interrupt any pending I/O
    700  *	operations and make them fail promptly; once they have all
    701  *	finished (except possibly new opens), ucomclose will be called.
    702  *	We set sc_closing to block new opens until ucomclose runs.
    703  */
    704 int
    705 ucomcancel(dev_t dev, int flag, int mode, struct lwp *l)
    706 {
    707 	const int unit = UCOMUNIT(dev);
    708 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit);
    709 
    710 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    711 
    712 	DPRINTF("unit=%jd", UCOMUNIT(dev), 0, 0, 0);
    713 
    714 	/*
    715 	 * This can run at any time before ucomclose on any device
    716 	 * node, even if never attached or if attach failed, so we may
    717 	 * not have a softc or a tty.
    718 	 */
    719 	if (sc == NULL)
    720 		return 0;
    721 	struct tty *tp = sc->sc_tty;
    722 	if (tp == NULL)
    723 		return 0;
    724 
    725 	/*
    726 	 * Mark the device closing so opens block until we're done
    727 	 * closing.  Wake them up so they start over at the top -- if
    728 	 * we're closing because we're detaching, they need to wake up
    729 	 * and notice it's time to fail.
    730 	 */
    731 	mutex_enter(&sc->sc_lock);
    732 	sc->sc_closing = true;
    733 	cv_broadcast(&sc->sc_statecv);
    734 	mutex_exit(&sc->sc_lock);
    735 
    736 	/*
    737 	 * Cancel any pending tty I/O operations, causing them to wake
    738 	 * up and fail promptly, and preventing any new ones from
    739 	 * starting to wait until we have finished closing.
    740 	 */
    741 	ttycancel(tp);
    742 
    743 	return 0;
    744 }
    745 
    746 /*
    747  * ucomclose(dev, flag, mode, l)
    748  *
    749  *	Called after ucomcancel, when all prior operations on the /dev
    750  *	node have completed.  Only new opens may be in progress at this
    751  *	point, but they will block until sc_closing is set to false.
    752  */
    753 int
    754 ucomclose(dev_t dev, int flag, int mode, struct lwp *l)
    755 {
    756 	const int unit = UCOMUNIT(dev);
    757 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit);
    758 	int error = 0;
    759 
    760 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    761 
    762 	DPRINTF("unit=%jd", UCOMUNIT(dev), 0, 0, 0);
    763 
    764 	/*
    765 	 * This can run at any time after ucomcancel on any device
    766 	 * node, even if never attached or if attach failed, so we may
    767 	 * not have a softc or a tty.
    768 	 */
    769 	if (sc == NULL)
    770 		return 0;
    771 	struct tty *tp = sc->sc_tty;
    772 	if (tp == NULL)
    773 		return 0;
    774 
    775 	/*
    776 	 * Close the tty, causing anyone waiting for it to wake, and
    777 	 * hang up the phone.
    778 	 */
    779 	ucom_cleanup(sc, flag);
    780 
    781 	/*
    782 	 * ttyclose should have cleared TS_ISOPEN and interrupted all
    783 	 * pending opens, which should have completed by now.
    784 	 */
    785 	mutex_spin_enter(&tty_lock);
    786 	KASSERT(!ISSET(tp->t_state, TS_ISOPEN));
    787 	KASSERT(tp->t_wopen == 0);
    788 	mutex_spin_exit(&tty_lock);
    789 
    790 	/*
    791 	 * Close any device-specific state.
    792 	 */
    793 	if (sc->sc_methods->ucom_close != NULL) {
    794 		sc->sc_methods->ucom_close(sc->sc_parent,
    795 		    sc->sc_portno);
    796 	}
    797 
    798 	/*
    799 	 * We're now closed.  Can reopen after this point, so resume
    800 	 * transfers, mark us no longer closing, and notify anyone
    801 	 * waiting in open.  The state may be OPEN or ATTACHED at this
    802 	 * point -- OPEN if the device was already open when we closed
    803 	 * it, ATTACHED if we interrupted it in the process of opening.
    804 	 */
    805 	mutex_enter(&sc->sc_lock);
    806 	KASSERTMSG(sc->sc_state == UCOM_ATTACHED || sc->sc_state == UCOM_OPEN,
    807 	    "%s sc=%p state=%d", device_xname(sc->sc_dev), sc, sc->sc_state);
    808 	KASSERT(sc->sc_closing);
    809 	sc->sc_state = UCOM_ATTACHED;
    810 	sc->sc_closing = false;
    811 	cv_broadcast(&sc->sc_statecv);
    812 	mutex_exit(&sc->sc_lock);
    813 
    814 	return error;
    815 }
    816 
    817 int
    818 ucomread(dev_t dev, struct uio *uio, int flag)
    819 {
    820 	const int unit = UCOMUNIT(dev);
    821 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    822 	struct tty *tp = sc->sc_tty;
    823 
    824 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    825 
    826 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    827 }
    828 
    829 int
    830 ucomwrite(dev_t dev, struct uio *uio, int flag)
    831 {
    832 	const int unit = UCOMUNIT(dev);
    833 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    834 	struct tty *tp = sc->sc_tty;
    835 
    836 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    837 
    838 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    839 }
    840 
    841 int
    842 ucompoll(dev_t dev, int events, struct lwp *l)
    843 {
    844 	const int unit = UCOMUNIT(dev);
    845 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    846 	struct tty *tp = sc->sc_tty;
    847 
    848 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    849 
    850 	return (*tp->t_linesw->l_poll)(tp, events, l);
    851 }
    852 
    853 struct tty *
    854 ucomtty(dev_t dev)
    855 {
    856 	const int unit = UCOMUNIT(dev);
    857 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    858 
    859 	return sc->sc_tty;
    860 }
    861 
    862 int
    863 ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    864 {
    865 	const int unit = UCOMUNIT(dev);
    866 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    867 	struct tty *tp = sc->sc_tty;
    868 	int error;
    869 
    870 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    871 
    872 	DPRINTF("cmd=0x%08jx", cmd, 0, 0, 0);
    873 
    874 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    875 	if (error != EPASSTHROUGH)
    876 		return error;
    877 
    878 	error = ttioctl(tp, cmd, data, flag, l);
    879 	if (error != EPASSTHROUGH)
    880 		return error;
    881 
    882 	if (sc->sc_methods->ucom_ioctl != NULL) {
    883 		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
    884 		    sc->sc_portno, cmd, data, flag, l->l_proc);
    885 		if (error != EPASSTHROUGH)
    886 			return error;
    887 	}
    888 
    889 	error = 0;
    890 
    891 	DPRINTF("our cmd=0x%08jx", cmd, 0, 0, 0);
    892 
    893 	switch (cmd) {
    894 	case TIOCSBRK:
    895 		ucom_break(sc, 1);
    896 		break;
    897 
    898 	case TIOCCBRK:
    899 		ucom_break(sc, 0);
    900 		break;
    901 
    902 	case TIOCSDTR:
    903 		ucom_dtr(sc, 1);
    904 		break;
    905 
    906 	case TIOCCDTR:
    907 		ucom_dtr(sc, 0);
    908 		break;
    909 
    910 	case TIOCGFLAGS:
    911 		mutex_enter(&sc->sc_lock);
    912 		*(int *)data = sc->sc_swflags;
    913 		mutex_exit(&sc->sc_lock);
    914 		break;
    915 
    916 	case TIOCSFLAGS:
    917 		error = kauth_authorize_device_tty(l->l_cred,
    918 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    919 		if (error)
    920 			break;
    921 		mutex_enter(&sc->sc_lock);
    922 		sc->sc_swflags = *(int *)data;
    923 		mutex_exit(&sc->sc_lock);
    924 		break;
    925 
    926 	case TIOCMSET:
    927 	case TIOCMBIS:
    928 	case TIOCMBIC:
    929 		tiocm_to_ucom(sc, cmd, *(int *)data);
    930 		break;
    931 
    932 	case TIOCMGET:
    933 		*(int *)data = ucom_to_tiocm(sc);
    934 		break;
    935 
    936 	case PPS_IOC_CREATE:
    937 	case PPS_IOC_DESTROY:
    938 	case PPS_IOC_GETPARAMS:
    939 	case PPS_IOC_SETPARAMS:
    940 	case PPS_IOC_GETCAP:
    941 	case PPS_IOC_FETCH:
    942 #ifdef PPS_SYNC
    943 	case PPS_IOC_KCBIND:
    944 #endif
    945 		mutex_spin_enter(&timecounter_lock);
    946 		error = pps_ioctl(cmd, data, &sc->sc_pps_state);
    947 		mutex_spin_exit(&timecounter_lock);
    948 		break;
    949 
    950 	default:
    951 		error = EPASSTHROUGH;
    952 		break;
    953 	}
    954 
    955 	return error;
    956 }
    957 
    958 static void
    959 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
    960 {
    961 	u_char combits;
    962 
    963 	combits = 0;
    964 	if (ISSET(ttybits, TIOCM_DTR))
    965 		SET(combits, UMCR_DTR);
    966 	if (ISSET(ttybits, TIOCM_RTS))
    967 		SET(combits, UMCR_RTS);
    968 
    969 	mutex_enter(&sc->sc_lock);
    970 	switch (how) {
    971 	case TIOCMBIC:
    972 		CLR(sc->sc_mcr, combits);
    973 		break;
    974 
    975 	case TIOCMBIS:
    976 		SET(sc->sc_mcr, combits);
    977 		break;
    978 
    979 	case TIOCMSET:
    980 		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
    981 		SET(sc->sc_mcr, combits);
    982 		break;
    983 	}
    984 	u_char mcr = sc->sc_mcr;
    985 	mutex_exit(&sc->sc_lock);
    986 
    987 	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
    988 		ucom_dtr(sc, (mcr & UMCR_DTR) != 0);
    989 	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
    990 		ucom_rts(sc, (mcr & UMCR_RTS) != 0);
    991 }
    992 
    993 static int
    994 ucom_to_tiocm(struct ucom_softc *sc)
    995 {
    996 	u_char combits;
    997 	int ttybits = 0;
    998 
    999 	mutex_enter(&sc->sc_lock);
   1000 	combits = sc->sc_mcr;
   1001 	if (ISSET(combits, UMCR_DTR))
   1002 		SET(ttybits, TIOCM_DTR);
   1003 	if (ISSET(combits, UMCR_RTS))
   1004 		SET(ttybits, TIOCM_RTS);
   1005 
   1006 	combits = sc->sc_msr;
   1007 	if (ISSET(combits, UMSR_DCD))
   1008 		SET(ttybits, TIOCM_CD);
   1009 	if (ISSET(combits, UMSR_CTS))
   1010 		SET(ttybits, TIOCM_CTS);
   1011 	if (ISSET(combits, UMSR_DSR))
   1012 		SET(ttybits, TIOCM_DSR);
   1013 	if (ISSET(combits, UMSR_RI | UMSR_TERI))
   1014 		SET(ttybits, TIOCM_RI);
   1015 
   1016 #if 0
   1017 XXX;
   1018 	if (sc->sc_ier != 0)
   1019 		SET(ttybits, TIOCM_LE);
   1020 #endif
   1021 	mutex_exit(&sc->sc_lock);
   1022 
   1023 	return ttybits;
   1024 }
   1025 
   1026 static void
   1027 ucom_break(struct ucom_softc *sc, int onoff)
   1028 {
   1029 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1030 
   1031 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
   1032 
   1033 	if (sc->sc_methods->ucom_set != NULL)
   1034 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1035 		    UCOM_SET_BREAK, onoff);
   1036 }
   1037 
   1038 static void
   1039 ucom_dtr(struct ucom_softc *sc, int onoff)
   1040 {
   1041 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1042 
   1043 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
   1044 
   1045 	if (sc->sc_methods->ucom_set != NULL)
   1046 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1047 		    UCOM_SET_DTR, onoff);
   1048 }
   1049 
   1050 static void
   1051 ucom_rts(struct ucom_softc *sc, int onoff)
   1052 {
   1053 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1054 
   1055 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
   1056 
   1057 	if (sc->sc_methods->ucom_set != NULL)
   1058 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1059 		    UCOM_SET_RTS, onoff);
   1060 }
   1061 
   1062 void
   1063 ucom_status_change(struct ucom_softc *sc)
   1064 {
   1065 	struct tty *tp = sc->sc_tty;
   1066 
   1067 	if (sc->sc_methods->ucom_get_status != NULL) {
   1068 		u_char msr, lsr;
   1069 
   1070 		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
   1071 		    &lsr, &msr);
   1072 		mutex_enter(&sc->sc_lock);
   1073 		u_char old_msr = sc->sc_msr;
   1074 
   1075 		sc->sc_lsr = lsr;
   1076 		sc->sc_msr = msr;
   1077 		mutex_exit(&sc->sc_lock);
   1078 
   1079 		if (ISSET((msr ^ old_msr), UMSR_DCD)) {
   1080 			mutex_spin_enter(&timecounter_lock);
   1081 			pps_capture(&sc->sc_pps_state);
   1082 			pps_event(&sc->sc_pps_state,
   1083 			    (sc->sc_msr & UMSR_DCD) ?
   1084 			    PPS_CAPTUREASSERT :
   1085 			    PPS_CAPTURECLEAR);
   1086 			mutex_spin_exit(&timecounter_lock);
   1087 
   1088 			(*tp->t_linesw->l_modem)(tp, ISSET(msr, UMSR_DCD));
   1089 		}
   1090 	} else {
   1091 		mutex_enter(&sc->sc_lock);
   1092 		sc->sc_lsr = 0;
   1093 		/* Assume DCD is present, if we have no chance to check it. */
   1094 		sc->sc_msr = UMSR_DCD;
   1095 		mutex_exit(&sc->sc_lock);
   1096 	}
   1097 }
   1098 
   1099 static int
   1100 ucomparam(struct tty *tp, struct termios *t)
   1101 {
   1102 	const int unit = UCOMUNIT(tp->t_dev);
   1103 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1104 	int error = 0;
   1105 
   1106 	/* XXX should take tty lock around touching tp */
   1107 
   1108 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1109 
   1110 	/* Check requested parameters. */
   1111 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
   1112 		error = EINVAL;
   1113 		goto out;
   1114 	}
   1115 
   1116 	/*
   1117 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1118 	 * is always active.
   1119 	 */
   1120 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
   1121 		SET(t->c_cflag, CLOCAL);
   1122 		CLR(t->c_cflag, HUPCL);
   1123 	}
   1124 
   1125 	/*
   1126 	 * If there were no changes, don't do anything.  This avoids dropping
   1127 	 * input and improves performance when all we did was frob things like
   1128 	 * VMIN and VTIME.
   1129 	 */
   1130 	if (tp->t_ospeed == t->c_ospeed &&
   1131 	    tp->t_cflag == t->c_cflag) {
   1132 		goto out;
   1133 	}
   1134 
   1135 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
   1136 
   1137 	/* And copy to tty. */
   1138 	tp->t_ispeed = 0;
   1139 	tp->t_ospeed = t->c_ospeed;
   1140 	tp->t_cflag = t->c_cflag;
   1141 
   1142 	if (sc->sc_methods->ucom_param != NULL) {
   1143 		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
   1144 		    t);
   1145 		if (error)
   1146 			goto out;
   1147 	}
   1148 
   1149 	/* XXX worry about CHWFLOW */
   1150 
   1151 	/*
   1152 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1153 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1154 	 * explicit request.
   1155 	 */
   1156 	DPRINTF("l_modem", 0, 0, 0, 0);
   1157 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
   1158 
   1159 #if 0
   1160 XXX what if the hardware is not open
   1161 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1162 		if (sc->sc_tx_stopped) {
   1163 			sc->sc_tx_stopped = 0;
   1164 			ucomstart(tp);
   1165 		}
   1166 	}
   1167 #endif
   1168 out:
   1169 	return error;
   1170 }
   1171 
   1172 static int
   1173 ucomhwiflow(struct tty *tp, int block)
   1174 {
   1175 	const int unit = UCOMUNIT(tp->t_dev);
   1176 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1177 	int old;
   1178 
   1179 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1180 
   1181 	KASSERT(&sc->sc_lock);
   1182 	KASSERT(mutex_owned(&tty_lock));
   1183 
   1184 	old = sc->sc_rx_stopped;
   1185 	sc->sc_rx_stopped = (u_char)block;
   1186 
   1187 	if (old && !block) {
   1188 		sc->sc_rx_unblock = 1;
   1189 		kpreempt_disable();
   1190 		softint_schedule(sc->sc_si);
   1191 		kpreempt_enable();
   1192 	}
   1193 
   1194 	return 1;
   1195 }
   1196 
   1197 static void
   1198 ucomstart(struct tty *tp)
   1199 {
   1200 	const int unit = UCOMUNIT(tp->t_dev);
   1201 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1202 	struct ucom_buffer *ub;
   1203 	u_char *data;
   1204 	int cnt;
   1205 
   1206 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1207 
   1208 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1209 		goto out;
   1210 	if (sc->sc_tx_stopped)
   1211 		goto out;
   1212 
   1213 	if (!ttypull(tp))
   1214 		goto out;
   1215 
   1216 	/* Grab the first contiguous region of buffer space. */
   1217 	data = tp->t_outq.c_cf;
   1218 	cnt = ndqb(&tp->t_outq, 0);
   1219 
   1220 	if (cnt == 0)
   1221 		goto out;
   1222 
   1223 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_free);
   1224 	if (ub == NULL) {
   1225 		SET(tp->t_state, TS_BUSY);
   1226 		goto out;
   1227 	}
   1228 
   1229 	SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_free, ub_link);
   1230 
   1231 	if (SIMPLEQ_FIRST(&sc->sc_obuff_free) == NULL)
   1232 		SET(tp->t_state, TS_BUSY);
   1233 
   1234 	if (cnt > sc->sc_obufsize)
   1235 		cnt = sc->sc_obufsize;
   1236 
   1237 	if (sc->sc_methods->ucom_write != NULL)
   1238 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
   1239 		    ub->ub_data, data, &cnt);
   1240 	else
   1241 		memcpy(ub->ub_data, data, cnt);
   1242 
   1243 	ub->ub_len = cnt;
   1244 	ub->ub_index = 0;
   1245 
   1246 	SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_full, ub, ub_link);
   1247 
   1248 	kpreempt_disable();
   1249 	softint_schedule(sc->sc_si);
   1250 	kpreempt_enable();
   1251 
   1252  out:
   1253 	DPRINTF("... done", 0, 0, 0, 0);
   1254 	return;
   1255 }
   1256 
   1257 void
   1258 ucomstop(struct tty *tp, int flag)
   1259 {
   1260 #if 0
   1261 	const int unit = UCOMUNIT(tp->t_dev);
   1262 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1263 
   1264 	mutex_enter(&sc->sc_lock);
   1265 	mutex_spin_enter(&tty_lock);
   1266 	if (ISSET(tp->t_state, TS_BUSY)) {
   1267 		/* obuff_full -> obuff_free? */
   1268 		/* sc->sc_tx_stopped = 1; */
   1269 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1270 			SET(tp->t_state, TS_FLUSH);
   1271 	}
   1272 	mutex_spin_exit(&tty_lock);
   1273 	mutex_exit(&sc->sc_lock);
   1274 #endif
   1275 }
   1276 
   1277 static void
   1278 ucom_write_status(struct ucom_softc *sc, struct ucom_buffer *ub,
   1279     usbd_status err)
   1280 {
   1281 	struct tty *tp = sc->sc_tty;
   1282 	uint32_t cc = ub->ub_len;
   1283 
   1284 	KASSERT(mutex_owned(&sc->sc_lock));
   1285 
   1286 	switch (err) {
   1287 	case USBD_IN_PROGRESS:
   1288 		ub->ub_index = ub->ub_len;
   1289 		break;
   1290 	case USBD_STALLED:
   1291 		ub->ub_index = 0;
   1292 		kpreempt_disable();
   1293 		softint_schedule(sc->sc_si);
   1294 		kpreempt_enable();
   1295 		break;
   1296 	case USBD_NORMAL_COMPLETION:
   1297 		usbd_get_xfer_status(ub->ub_xfer, NULL, NULL, &cc, NULL);
   1298 		rnd_add_uint32(&sc->sc_rndsource, cc);
   1299 		/*FALLTHROUGH*/
   1300 	default:
   1301 		SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_full, ub_link);
   1302 		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
   1303 		cc -= sc->sc_opkthdrlen;
   1304 
   1305 		mutex_spin_enter(&tty_lock);
   1306 		CLR(tp->t_state, TS_BUSY);
   1307 		if (ISSET(tp->t_state, TS_FLUSH))
   1308 			CLR(tp->t_state, TS_FLUSH);
   1309 		else
   1310 			ndflush(&tp->t_outq, cc);
   1311 		mutex_spin_exit(&tty_lock);
   1312 
   1313 		if (err != USBD_CANCELLED && err != USBD_IOERROR &&
   1314 		    !sc->sc_closing) {
   1315 			if ((ub = SIMPLEQ_FIRST(&sc->sc_obuff_full)) != NULL)
   1316 				ucom_submit_write(sc, ub);
   1317 
   1318 			mutex_spin_enter(&tty_lock);
   1319 			(*tp->t_linesw->l_start)(tp);
   1320 			mutex_spin_exit(&tty_lock);
   1321 		}
   1322 		break;
   1323 	}
   1324 }
   1325 
   1326 static void
   1327 ucom_submit_write(struct ucom_softc *sc, struct ucom_buffer *ub)
   1328 {
   1329 
   1330 	KASSERT(mutex_owned(&sc->sc_lock));
   1331 
   1332 	usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, ub->ub_len,
   1333 	    0, USBD_NO_TIMEOUT, ucomwritecb);
   1334 
   1335 	ucom_write_status(sc, ub, usbd_transfer(ub->ub_xfer));
   1336 }
   1337 
   1338 static void
   1339 ucomwritecb(struct usbd_xfer *xfer, void *p, usbd_status status)
   1340 {
   1341 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1342 
   1343 	mutex_enter(&sc->sc_lock);
   1344 	ucom_write_status(sc, SIMPLEQ_FIRST(&sc->sc_obuff_full), status);
   1345 	mutex_exit(&sc->sc_lock);
   1346 
   1347 }
   1348 
   1349 static void
   1350 ucom_softintr(void *arg)
   1351 {
   1352 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1353 
   1354 	struct ucom_softc *sc = arg;
   1355 	struct tty *tp = sc->sc_tty;
   1356 
   1357 	mutex_enter(&sc->sc_lock);
   1358 	mutex_enter(&tty_lock);
   1359 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
   1360 		mutex_exit(&tty_lock);
   1361 		mutex_exit(&sc->sc_lock);
   1362 		return;
   1363 	}
   1364 	mutex_exit(&tty_lock);
   1365 
   1366 	struct ucom_buffer *ub = SIMPLEQ_FIRST(&sc->sc_obuff_full);
   1367 
   1368 	if (ub != NULL && ub->ub_index == 0)
   1369 		ucom_submit_write(sc, ub);
   1370 
   1371 	if (sc->sc_rx_unblock)
   1372 		ucom_read_complete(sc);
   1373 
   1374 	mutex_exit(&sc->sc_lock);
   1375 }
   1376 
   1377 static void
   1378 ucom_read_complete(struct ucom_softc *sc)
   1379 {
   1380 	int (*rint)(int, struct tty *);
   1381 	struct ucom_buffer *ub;
   1382 	struct tty *tp;
   1383 
   1384 	KASSERT(mutex_owned(&sc->sc_lock));
   1385 
   1386 	tp = sc->sc_tty;
   1387 	rint = tp->t_linesw->l_rint;
   1388 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1389 
   1390 	while (ub != NULL && !sc->sc_rx_stopped) {
   1391 
   1392 		/* XXX ttyinput takes tty_lock */
   1393 		while (ub->ub_index < ub->ub_len && !sc->sc_rx_stopped) {
   1394 			/* Give characters to tty layer. */
   1395 			if ((*rint)(ub->ub_data[ub->ub_index], tp) == -1) {
   1396 				/* Overflow: drop remainder */
   1397 				ub->ub_index = ub->ub_len;
   1398 			} else
   1399 				ub->ub_index++;
   1400 		}
   1401 
   1402 		if (ub->ub_index == ub->ub_len) {
   1403 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
   1404 			ucomsubmitread(sc, ub);
   1405 			ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1406 		}
   1407 	}
   1408 
   1409 	sc->sc_rx_unblock = (ub != NULL);
   1410 }
   1411 
   1412 static int
   1413 ucomsubmitread(struct ucom_softc *sc, struct ucom_buffer *ub)
   1414 {
   1415 	usbd_status err;
   1416 
   1417 	KASSERT(mutex_owned(&sc->sc_lock));
   1418 
   1419 	usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, sc->sc_ibufsize,
   1420 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ucomreadcb);
   1421 
   1422 	if ((err = usbd_transfer(ub->ub_xfer)) != USBD_IN_PROGRESS) {
   1423 		/* XXX: Recover from this, please! */
   1424 		printf("%s: err=%s\n", __func__, usbd_errstr(err));
   1425 		return EIO;
   1426 	}
   1427 
   1428 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_empty, ub, ub_link);
   1429 
   1430 	return 0;
   1431 }
   1432 
   1433 static void
   1434 ucomreadcb(struct usbd_xfer *xfer, void *p, usbd_status status)
   1435 {
   1436 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1437 	struct tty *tp = sc->sc_tty;
   1438 	struct ucom_buffer *ub;
   1439 	uint32_t cc;
   1440 	u_char *cp;
   1441 
   1442 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1443 
   1444 	mutex_enter(&sc->sc_lock);
   1445 
   1446 	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
   1447 	    sc->sc_closing) {
   1448 
   1449 		DPRINTF("... done (status %jd closing %jd)",
   1450 		    status, sc->sc_closing, 0, 0);
   1451 
   1452 		if (status == USBD_IOERROR || sc->sc_closing) {
   1453 			/* Send something to wake upper layer */
   1454 			(tp->t_linesw->l_rint)('\n', tp);
   1455 			mutex_spin_enter(&tty_lock);	/* XXX */
   1456 			ttwakeup(tp);
   1457 			mutex_spin_exit(&tty_lock);	/* XXX */
   1458 		}
   1459 
   1460 		mutex_exit(&sc->sc_lock);
   1461 		return;
   1462 	}
   1463 
   1464 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty);
   1465 	SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_empty, ub_link);
   1466 
   1467 	if (status != USBD_NORMAL_COMPLETION) {
   1468 		if (status == USBD_STALLED) {
   1469 			usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
   1470 		} else {
   1471 			printf("ucomreadcb: wonky status=%s\n",
   1472 			    usbd_errstr(status));
   1473 		}
   1474 
   1475 		DPRINTF("... done (status %jd)", status, 0, 0, 0);
   1476 		/* re-adds ub to sc_ibuff_empty */
   1477 		ucomsubmitread(sc, ub);
   1478 		mutex_exit(&sc->sc_lock);
   1479 		return;
   1480 	}
   1481 
   1482 	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
   1483 
   1484 #ifdef UCOM_DEBUG
   1485 	/* This is triggered by uslsa(4) occasionally. */
   1486 	if ((ucomdebug > 0) && (cc == 0)) {
   1487 		device_printf(sc->sc_dev, "ucomreadcb: zero length xfer!\n");
   1488 	}
   1489 #endif
   1490 	KDASSERT(cp == ub->ub_data);
   1491 
   1492 	rnd_add_uint32(&sc->sc_rndsource, cc);
   1493 
   1494 	if (sc->sc_state != UCOM_OPEN) {
   1495 		/* Go around again - we're not quite ready */
   1496 		/* re-adds ub to sc_ibuff_empty */
   1497 		ucomsubmitread(sc, ub);
   1498 		mutex_exit(&sc->sc_lock);
   1499 		DPRINTF("... done (not open)", 0, 0, 0, 0);
   1500 		return;
   1501 	}
   1502 
   1503 	mutex_exit(&sc->sc_lock);
   1504 	if (sc->sc_methods->ucom_read != NULL) {
   1505 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
   1506 		    &cp, &cc);
   1507 		ub->ub_index = (u_int)(cp - ub->ub_data);
   1508 	} else
   1509 		ub->ub_index = 0;
   1510 
   1511 	ub->ub_len = cc;
   1512 
   1513 	mutex_enter(&sc->sc_lock);
   1514 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_full, ub, ub_link);
   1515 	ucom_read_complete(sc);
   1516 	mutex_exit(&sc->sc_lock);
   1517 
   1518 	DPRINTF("... done", 0, 0, 0, 0);
   1519 }
   1520 
   1521 static void
   1522 ucom_cleanup(struct ucom_softc *sc, int flag)
   1523 {
   1524 	struct tty *tp = sc->sc_tty;
   1525 
   1526 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1527 
   1528 	DPRINTF("closing pipes", 0, 0, 0, 0);
   1529 
   1530 	/*
   1531 	 * Close the tty and interrupt any pending opens waiting for
   1532 	 * carrier so they restart or give up.  This may flush data.
   1533 	 */
   1534 	(*tp->t_linesw->l_close)(tp, flag);
   1535 	ttyclose(tp);
   1536 
   1537 	/*
   1538 	 * Interrupt any pending xfers and cause them to fail promptly.
   1539 	 * New xfers will only be submitted under the lock after
   1540 	 * sc_closing is cleared.
   1541 	 */
   1542 	usbd_abort_pipe(sc->sc_bulkin_pipe);
   1543 	usbd_abort_pipe(sc->sc_bulkout_pipe);
   1544 
   1545 	/*
   1546 	 * Hang up the phone and start the timer before we can make a
   1547 	 * call again, if necessary.
   1548 	 */
   1549 	ucom_shutdown(sc);
   1550 }
   1551 
   1552 #endif /* NUCOM > 0 */
   1553 
   1554 int
   1555 ucomprint(void *aux, const char *pnp)
   1556 {
   1557 	struct ucom_attach_args *ucaa = aux;
   1558 
   1559 	if (pnp)
   1560 		aprint_normal("ucom at %s", pnp);
   1561 	if (ucaa->ucaa_portno != UCOM_UNK_PORTNO)
   1562 		aprint_normal(" portno %d", ucaa->ucaa_portno);
   1563 	return UNCONF;
   1564 }
   1565 
   1566 int
   1567 ucomsubmatch(device_t parent, cfdata_t cf,
   1568 	     const int *ldesc, void *aux)
   1569 {
   1570 	struct ucom_attach_args *ucaa = aux;
   1571 
   1572 	if (ucaa->ucaa_portno != UCOM_UNK_PORTNO &&
   1573 	    cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
   1574 	    cf->cf_loc[UCOMBUSCF_PORTNO] != ucaa->ucaa_portno)
   1575 		return 0;
   1576 	return config_match(parent, cf, aux);
   1577 }
   1578