Home | History | Annotate | Line # | Download | only in usb
      1 /*	$NetBSD: ucom.c,v 1.153 2026/11/07 22:20:14 thorpej 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.153 2026/11/07 22:20:14 thorpej Exp $");
     38 
     39 #ifdef _KERNEL_OPT
     40 #include "opt_ddb.h"
     41 #include "opt_ntp.h"
     42 #include "opt_usb.h"
     43 #endif
     44 
     45 #include <sys/param.h>
     46 #include <sys/callout.h>
     47 #include <sys/conf.h>
     48 #include <sys/device.h>
     49 #include <sys/file.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/kauth.h>
     52 #include <sys/kernel.h>
     53 #include <sys/poll.h>
     54 #include <sys/proc.h>
     55 #include <sys/queue.h>
     56 #include <sys/rndsource.h>
     57 #include <sys/select.h>
     58 #include <sys/sysctl.h>
     59 #include <sys/systm.h>
     60 #include <sys/timepps.h>
     61 #include <sys/tty.h>
     62 #include <sys/vnode.h>
     63 
     64 #include <dev/cons.h>
     65 
     66 #include <dev/usb/usb.h>
     67 #include <dev/usb/usbdi.h>
     68 #include <dev/usb/usbdevs.h>
     69 #include <dev/usb/usbhist.h>
     70 
     71 #include <dev/usb/ucomvar.h>
     72 
     73 #include "ucom.h"
     74 #include "locators.h"
     75 #include "ioconf.h"
     76 
     77 #if NUCOM > 0
     78 
     79 #ifdef USB_DEBUG
     80 #ifndef UCOM_DEBUG
     81 #define ucomdebug 0
     82 #else
     83 int ucomdebug = 0;
     84 
     85 SYSCTL_SETUP(sysctl_hw_ucom_setup, "sysctl hw.ucom setup")
     86 {
     87 	int err;
     88 	const struct sysctlnode *rnode;
     89 	const struct sysctlnode *cnode;
     90 
     91 	err = sysctl_createv(clog, 0, NULL, &rnode,
     92 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ucom",
     93 	    SYSCTL_DESCR("ucom global controls"),
     94 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
     95 
     96 	if (err)
     97 		goto fail;
     98 
     99 	/* control debugging printfs */
    100 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    101 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    102 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    103 	    NULL, 0, &ucomdebug, sizeof(ucomdebug), CTL_CREATE, CTL_EOL);
    104 	if (err)
    105 		goto fail;
    106 
    107 	return;
    108 fail:
    109 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    110 }
    111 
    112 #endif /* UCOM_DEBUG */
    113 #endif /* USB_DEBUG */
    114 
    115 #define DPRINTF(FMT,A,B,C,D)	USBHIST_LOGN(ucomdebug,1,FMT,A,B,C,D)
    116 #define DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(ucomdebug,N,FMT,A,B,C,D)
    117 #define UCOMHIST_FUNC()		USBHIST_FUNC()
    118 #define UCOMHIST_CALLED(name)	USBHIST_CALLED(ucomdebug)
    119 
    120 #define	UCOMCALLUNIT_MASK	TTCALLUNIT_MASK
    121 #define	UCOMUNIT_MASK		TTUNIT_MASK
    122 #define	UCOMDIALOUT_MASK	TTDIALOUT_MASK
    123 
    124 #define	UCOMCALLUNIT(x)		TTCALLUNIT(x)
    125 #define	UCOMUNIT(x)		TTUNIT(x)
    126 #define	UCOMDIALOUT(x)		TTDIALOUT(x)
    127 #define	ucom_unit		tty_unit
    128 
    129 /*
    130  * XXX: We can submit multiple input/output buffers to the usb stack
    131  * to improve throughput, but the usb stack is too lame to deal with this
    132  * in a number of places.
    133  */
    134 #define	UCOM_IN_BUFFS	1
    135 #define	UCOM_OUT_BUFFS	1
    136 
    137 struct ucom_buffer {
    138 	SIMPLEQ_ENTRY(ucom_buffer) ub_link;
    139 	struct usbd_xfer *ub_xfer;
    140 	u_char *ub_data;
    141 	u_int ub_len;
    142 	u_int ub_index;
    143 };
    144 
    145 struct ucom_softc {
    146 	device_t		sc_dev;		/* base device */
    147 
    148 	struct usbd_device *	sc_udev;	/* USB device */
    149 	struct usbd_interface *	sc_iface;	/* data interface */
    150 
    151 	int			sc_bulkin_no;	/* bulk in endpoint address */
    152 	struct usbd_pipe *	sc_bulkin_pipe;/* bulk in pipe */
    153 	u_int			sc_ibufsize;	/* read buffer size */
    154 	u_int			sc_ibufsizepad;	/* read buffer size padded */
    155 	struct ucom_buffer	sc_ibuff[UCOM_IN_BUFFS];
    156 	SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_empty;
    157 	SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_full;
    158 
    159 	int			sc_bulkout_no;	/* bulk out endpoint address */
    160 	struct usbd_pipe *	sc_bulkout_pipe;/* bulk out pipe */
    161 	u_int			sc_obufsize;	/* write buffer size */
    162 	u_int			sc_opkthdrlen;	/* header length of */
    163 	struct ucom_buffer	sc_obuff[UCOM_OUT_BUFFS];
    164 	SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_free;
    165 	SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_full;
    166 
    167 	void			*sc_si;
    168 
    169 	const struct ucom_methods *sc_methods;
    170 	void			*sc_parent;
    171 	int			sc_portno;
    172 
    173 	struct tty		*sc_tty;	/* our tty */
    174 	u_char			sc_lsr;
    175 	u_char			sc_msr;
    176 	u_char			sc_mcr;
    177 	volatile u_char		sc_rx_stopped;
    178 	u_char			sc_rx_unblock;
    179 	u_char			sc_tx_stopped;
    180 	int			sc_swflags;
    181 
    182 	enum ucom_state {
    183 	    UCOM_DEAD,
    184 	    UCOM_ATTACHED,
    185 	    UCOM_OPENING,
    186 	    UCOM_OPEN
    187 	}			sc_state;
    188 	bool			sc_closing;	/* software is closing */
    189 	bool			sc_dying;	/* hardware is gone */
    190 	struct ucom_cnstate	*sc_cn;		/* console state */
    191 
    192 	struct pps_state	sc_pps_state;	/* pps state */
    193 
    194 	krndsource_t		sc_rndsource;	/* random source */
    195 
    196 	kmutex_t		sc_lock;
    197 	kcondvar_t		sc_statecv;
    198 
    199 	struct timeval		sc_hup_time;
    200 };
    201 
    202 dev_type_open(ucomopen);
    203 dev_type_cancel(ucomcancel);
    204 dev_type_close(ucomclose);
    205 dev_type_read(ucomread);
    206 dev_type_write(ucomwrite);
    207 dev_type_ioctl(ucomioctl);
    208 dev_type_stop(ucomstop);
    209 dev_type_tty(ucomtty);
    210 dev_type_poll(ucompoll);
    211 
    212 const struct cdevsw ucom_cdevsw = {
    213 	.d_open = ucomopen,
    214 	.d_cancel = ucomcancel,
    215 	.d_close = ucomclose,
    216 	.d_read = ucomread,
    217 	.d_write = ucomwrite,
    218 	.d_ioctl = ucomioctl,
    219 	.d_stop = ucomstop,
    220 	.d_tty = ucomtty,
    221 	.d_poll = ucompoll,
    222 	.d_mmap = nommap,
    223 	.d_kqfilter = ttykqfilter,
    224 	.d_discard = nodiscard,
    225 	.d_cfdriver = &ucom_cd,
    226 	.d_devtounit = ucom_unit,
    227 	.d_flag = D_TTY | D_MPSAFE
    228 };
    229 
    230 static void	ucom_cleanup(struct ucom_softc *, int);
    231 static int	ucomparam(struct tty *, struct termios *);
    232 static int	ucomhwiflow(struct tty *, int);
    233 static void	ucomstart(struct tty *);
    234 static void	ucom_shutdown(struct ucom_softc *);
    235 static void	ucom_dtr(struct ucom_softc *, int);
    236 static void	ucom_rts(struct ucom_softc *, int);
    237 static void	ucom_break(struct ucom_softc *, int);
    238 static void	tiocm_to_ucom(struct ucom_softc *, u_long, int);
    239 static int	ucom_to_tiocm(struct ucom_softc *);
    240 
    241 static void	ucomreadcb(struct usbd_xfer *, void *, usbd_status);
    242 static void	ucom_submit_write(struct ucom_softc *, struct ucom_buffer *);
    243 static void	ucom_write_status(struct ucom_softc *, struct ucom_buffer *,
    244 		    usbd_status);
    245 
    246 static void	ucomwritecb(struct usbd_xfer *, void *, usbd_status);
    247 static void	ucom_read_complete(struct ucom_softc *);
    248 static int	ucomsubmitread(struct ucom_softc *, struct ucom_buffer *);
    249 static void	ucom_softintr(void *);
    250 
    251 static bool	ucom_polling(struct ucom_softc *);
    252 static void	ucom_mutex_enter(struct ucom_softc *);
    253 static void	ucom_mutex_exit(struct ucom_softc *);
    254 static bool	ucom_xfer_done(struct usbd_xfer *);
    255 
    256 static void	ucom_cninit(struct ucom_softc *);
    257 static void	ucom_cnread_complete(struct ucom_softc *);
    258 static int	ucom_cnput_bytes(struct ucom_softc *);
    259 static void	ucom_cnput_complete(struct usbd_xfer *, void *, usbd_status);
    260 static void	ucom_cnput_callout(void *);
    261 static void	ucom_cnput_softintr(void *);
    262 static int	ucom_cngetc(dev_t);
    263 static void	ucom_cnputc(dev_t, int);
    264 static void	ucom_cnpollc(dev_t, int);
    265 static void	ucom_cnfree(struct ucom_softc *);
    266 static void	ucom_cnhalt(dev_t);
    267 #ifdef DDB
    268 static void	ucom_cncheckmagic(struct ucom_softc *, struct ucom_buffer *);
    269 #endif
    270 
    271 /*
    272  * Following are all routines needed for COM to act as console
    273  */
    274 static struct consdev ucom_cnops = {
    275 	.cn_getc = ucom_cngetc,
    276 	.cn_putc = ucom_cnputc,
    277 	.cn_pollc = ucom_cnpollc,
    278 	.cn_halt = ucom_cnhalt,
    279 	.cn_dev = NODEV,
    280 	.cn_pri = CN_NORMAL
    281 };
    282 static int ucom_cnspeed = B115200;
    283 
    284 struct ucom_cnstate {
    285 	dev_t			cn_dev;
    286 	u_char			*cn_buf_in;
    287 	u_int			cn_prod_in;
    288 	u_int			cn_cons_in;
    289 
    290 	uint32_t		cn_sending;
    291 	struct usbd_xfer	*cn_xfer_out;
    292 	u_char			*cn_buf_out;
    293 	size_t			cn_buf_out_size;
    294 	u_char			*cn_linear_buf_out;
    295 	u_int			cn_prod_out;
    296 	u_int			cn_cons_out;
    297 	void			*cn_si;
    298 	callout_t		cn_callout;
    299 	struct cnm_state	cn_magic_state;
    300 	bool			cn_magic_req;
    301 	bool			cn_polling;
    302 };
    303 
    304 int ucom_match(device_t, cfdata_t, void *);
    305 void ucom_attach(device_t, device_t, void *);
    306 int ucom_detach(device_t, int);
    307 
    308 CFATTACH_DECL_NEW(ucom, sizeof(struct ucom_softc), ucom_match, ucom_attach,
    309     ucom_detach, NULL);
    310 
    311 int
    312 ucom_match(device_t parent, cfdata_t match, void *aux)
    313 {
    314 	return 1;
    315 }
    316 
    317 /*
    318  * Output a chunk of data.
    319  *
    320  * This is the consumer side of the output ring buffer.
    321  */
    322 static void
    323 ucom_cnput_complete(struct usbd_xfer *xfer, void *priv, usbd_status status)
    324 {
    325 	struct ucom_softc * const sc = priv;
    326 	struct ucom_cnstate * const cn = sc->sc_cn;
    327 	bool polling = ucom_polling(sc);
    328 	usbd_status err;
    329 	uint32_t cnt;
    330 
    331 	KASSERT(ucom_xfer_done(cn->cn_xfer_out));
    332 
    333 	usbd_get_xfer_status(cn->cn_xfer_out, NULL, NULL, &cnt, &err);
    334 
    335 	u_int cnconsout = atomic_load_relaxed(&cn->cn_cons_out);
    336 
    337 	switch (err) {
    338 	case USBD_STALLED:
    339 		if (!polling)
    340 			usbd_clear_endpoint_stall_async(sc->sc_bulkout_pipe);
    341 
    342 		/*
    343 		 * The device can't keep up so give it a rest and retry
    344 		 * again in 10 ticks.
    345 		 * Do not move cn->cn_cons_out so that this output is
    346 		 * retried later.
    347 		 * XXXNH Maybe a more scientific retry timer calculation.
    348 		 * When DDB is active, ucom_cnputc() loops on
    349 		 * ucom_cnput_bytes(), this will perform the retry.
    350 		 */
    351 		if (!db_active)
    352 			callout_schedule(&cn->cn_callout, 10);
    353 		return;
    354 
    355 	case USBD_NORMAL_COMPLETION:
    356 		cnconsout += cnt;
    357 		atomic_store_release(&cn->cn_cons_out, cnconsout);
    358 		break;
    359 
    360 	case USBD_CANCELLED:
    361 		/*
    362 		 * Device close causes pipe abort, and all in-progress
    363 		 * transfers get USBD_CANCELLED. We will retry the
    364 		 * output on next ucom_cnput_bytes() call.
    365 		 */
    366 		break;
    367 
    368 	default:
    369 		/*
    370 		 * Catch unusual errors when DIAGNOSTIC, otherwise
    371 		 * retry on next com_cnput_bytes() call.
    372 		 */
    373 #ifdef DIAGNOSTIC
    374 		printf("%s: error %d", __func__, err);
    375 #endif
    376 		break;
    377 	}
    378 
    379 	KASSERT(db_active || atomic_load_relaxed(&cn->cn_sending) == 1);
    380 
    381 	/*
    382 	 * Under DDB rule, just return, ucom_cnput_bytes() should
    383 	 * poll for completion.
    384 	 */
    385 	if (db_active)
    386 		return;
    387 
    388 	(void)ucom_cnput_bytes(sc);
    389 }
    390 
    391 static int
    392 ucom_cnput_bytes(struct ucom_softc *sc)
    393 {
    394 	struct ucom_cnstate * const cn = sc->sc_cn;
    395 	u_char *ub_out = usbd_get_buffer(cn->cn_xfer_out);
    396 	u_char *out = NULL;
    397 	usbd_status err;
    398 	u_int prodout = atomic_load_acquire(&cn->cn_prod_out);
    399 	u_int consout = atomic_load_relaxed(&cn->cn_cons_out);
    400 	u_int consout_idx = consout % cn->cn_buf_out_size;
    401 	u_int cnt = prodout - consout;
    402 
    403 	if (cnt > sc->sc_obufsize)
    404 		cnt = sc->sc_obufsize;
    405 
    406 	KASSERT(db_active || atomic_load_relaxed(&cn->cn_sending) == 1);
    407 
    408 	/* Nothing to do so success. */
    409 	if (cnt == 0) {
    410 		if (!db_active)
    411 			atomic_store_relaxed(&cn->cn_sending, 0);
    412 		return 0;
    413 	}
    414 
    415 	u_int cntidx = (consout_idx + cnt) % cn->cn_buf_out_size;
    416 	if (cntidx > consout_idx || cntidx == 0) {
    417 		out = cn->cn_buf_out + consout_idx;
    418 	} else {
    419 		/*
    420 		 * Producer wrapped around the ring buffer,
    421 		 * data to send is split in two chenks that
    422 		 * we copy to the linear buffer.
    423 		 * - from consout_idx to end of ring buffer
    424 		 * - from start of ring buffer up to cnt total.
    425 		 */
    426 		u_int top_size = cn->cn_buf_out_size - consout_idx;
    427 
    428 		memcpy(cn->cn_linear_buf_out,
    429 		    cn->cn_buf_out + consout_idx, top_size);
    430 
    431 		memcpy(cn->cn_linear_buf_out + top_size,
    432 		    cn->cn_buf_out, cntidx);
    433 
    434 		out = cn->cn_linear_buf_out;
    435 	}
    436 
    437 	if (sc->sc_methods->ucom_write != NULL)
    438 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
    439 		    ub_out, out, &cnt);
    440 	else
    441 		memcpy(ub_out, out, cnt);
    442 
    443 	KASSERT(ucom_xfer_done(cn->cn_xfer_out));
    444 
    445 	/*
    446 	 * cn->cn_cons_out is increased after output succeeds,
    447 	 * in callback ucom_cnput_complete().
    448 	 */
    449 	usbd_setup_xfer(cn->cn_xfer_out, sc, ub_out, cnt,
    450 	    0, USBD_NO_TIMEOUT, ucom_cnput_complete);
    451 	err = usbd_transfer(cn->cn_xfer_out);
    452 
    453 	switch (err) {
    454 	case USBD_NOT_STARTED:
    455 	case USBD_IN_PROGRESS:
    456 		/*
    457 		 * Poll for completion if DDB is active.
    458 		 * Otherwise rely on cn->cn_sending to serialize transfers
    459 		 */
    460 		while (db_active && ucom_polling(sc) &&
    461 		    !ucom_xfer_done(cn->cn_xfer_out))
    462 			usbd_dopoll(sc->sc_iface);
    463 		break;
    464 	case USBD_CANCELLED:
    465 	case USBD_NORMAL_COMPLETION:
    466 		break;
    467 	default:
    468 #ifdef DIAGNOSTIC
    469 		printf("%s: error %d", __func__, err);
    470 #endif
    471 		break;
    472 	}
    473 
    474 	return cnt;
    475 }
    476 
    477 static void
    478 ucom_cnput_softintr(void *priv)
    479 {
    480 	struct ucom_softc * const sc = priv;
    481 	struct ucom_cnstate * const cn = sc->sc_cn;
    482 
    483 	/*
    484 	 * atomic_cas_32() returns the original value,
    485 	 * hence we only proceed if cn->cn_sending was 0.
    486 	 * This makes sure ucom_cnput_bytes() does not
    487 	 * reuse cn->cn_xfer_out before it completes the
    488 	 * transfer.
    489 	 */
    490 	if (atomic_cas_32(&cn->cn_sending, 0, 1) == 0)
    491 		(void)ucom_cnput_bytes(sc);
    492 }
    493 
    494 static void
    495 ucom_cnput_callout(void *priv)
    496 {
    497 	struct ucom_softc * const sc = priv;
    498 	struct ucom_cnstate * const cn = sc->sc_cn;
    499 
    500 	kpreempt_disable();
    501 	softint_schedule(cn->cn_si);
    502 	kpreempt_enable();
    503 }
    504 
    505 static bool
    506 ucom_polling(struct ucom_softc *sc)
    507 {
    508 	struct ucom_cnstate * const cn = sc->sc_cn;
    509 
    510 	if (cn == NULL)
    511 		return false;
    512 
    513 	return cn->cn_polling;
    514 }
    515 
    516 static void
    517 ucom_mutex_enter(struct ucom_softc *sc)
    518 {
    519 	if (!ucom_polling(sc))
    520 		mutex_enter(&sc->sc_lock);
    521 }
    522 
    523 static void
    524 ucom_mutex_exit(struct ucom_softc *sc)
    525 {
    526 	if (!ucom_polling(sc))
    527 		mutex_exit(&sc->sc_lock);
    528 }
    529 
    530 
    531 static void
    532 ucom_cninit(struct ucom_softc *sc)
    533 {
    534 	int major = cdevsw_lookup_major(&ucom_cdevsw);
    535 	int unit = device_unit(sc->sc_dev);
    536 	struct tty *tp = sc->sc_tty;
    537 	struct termios t;
    538 	struct ucom_buffer *ub;
    539 	int error = -1;
    540 
    541 	if (makedev(major, unit) != ucom_cnops.cn_dev)
    542 		return;
    543 
    544 	/*
    545 	 * TIOCFLAG_SOFTCAR sets CLOCAL in ucomopen().
    546 	 */
    547 	SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
    548 
    549 	memset(&t, 0, sizeof(t));
    550 	t.c_ispeed = ucom_cnspeed;
    551 	t.c_ospeed = ucom_cnspeed;
    552 	error = ucomparam(tp, &t);
    553 	if (error)
    554 		aprint_error_dev(sc->sc_dev,
    555 		    "ucomparam fail, error = %d", error);
    556 
    557 	sc->sc_cn = kmem_zalloc(sizeof(*sc->sc_cn), KM_SLEEP);
    558 	struct ucom_cnstate * const cn = sc->sc_cn;
    559 
    560 	cn->cn_dev = ucom_cnops.cn_dev;
    561 
    562 	cn->cn_si = softint_establish(SOFTINT_USB, ucom_cnput_softintr, sc);
    563 
    564 	callout_init(&cn->cn_callout, CALLOUT_MPSAFE);
    565 	callout_setfunc(&cn->cn_callout, ucom_cnput_callout, sc);
    566 
    567 	cn->cn_buf_in = kmem_alloc(sc->sc_ibufsize, KM_SLEEP);
    568 	cn->cn_prod_in = 0;
    569 	cn->cn_cons_in = 0;
    570 
    571 	/*
    572 	 * Kernel can easily write to the output buffer faster
    573 	 * that the serial interface can consume. We need a
    574 	 * buffer large enough to avoid overflown abd
    575 	 * sc->sc_obufsize, may be too small for that.
    576 	 */
    577 	cn->cn_buf_out_size = MAX(sc->sc_obufsize, PAGE_SIZE);
    578 	cn->cn_buf_out = kmem_alloc(cn->cn_buf_out_size, KM_SLEEP);
    579 	cn->cn_prod_out = 0;
    580 	cn->cn_cons_out = 0;
    581 	if (usbd_create_xfer(sc->sc_bulkout_pipe,
    582 	    sc->sc_obufsize, 0, 0, &cn->cn_xfer_out) != 0) {
    583 		aprint_error_dev(sc->sc_dev, "fail to create cn_xfer_out");
    584 		goto ucom_cnfail;
    585 	}
    586 
    587 	cn->cn_linear_buf_out = kmem_alloc(sc->sc_obufsize, KM_SLEEP);
    588 
    589 	cn_tab = &ucom_cnops;
    590 	cn_init_magic(&cn->cn_magic_state);
    591 	cn_set_magic("\047\001"); /* default magic is BREAK */
    592 
    593 	aprint_normal_dev(sc->sc_dev, "console\n");
    594 
    595 	mutex_enter(&sc->sc_lock);
    596 	for (size_t i = 0; i < UCOM_IN_BUFFS; i++) {
    597 		ub = &sc->sc_ibuff[i];
    598 		error = ucomsubmitread(sc, ub);
    599 		if (error) {
    600 			mutex_exit(&sc->sc_lock);
    601 			goto ucom_cnfail;
    602 		}
    603 	}
    604 	mutex_exit(&sc->sc_lock);
    605 
    606 	return;
    607 
    608 ucom_cnfail:
    609 	if (error)
    610 		ucom_cnfree(sc);
    611 }
    612 
    613 void
    614 ucom_attach(device_t parent, device_t self, void *aux)
    615 {
    616 	struct ucom_softc *sc = device_private(self);
    617 	struct ucom_attach_args *ucaa = aux;
    618 
    619 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    620 
    621 	if (ucaa->ucaa_info != NULL)
    622 		aprint_normal(": %s", ucaa->ucaa_info);
    623 	aprint_normal("\n");
    624 
    625 	prop_dictionary_set_int32(device_properties(self), "port",
    626 	    ucaa->ucaa_portno);
    627 
    628 	sc->sc_dev = self;
    629 	sc->sc_udev = ucaa->ucaa_device;
    630 	sc->sc_iface = ucaa->ucaa_iface;
    631 	sc->sc_bulkout_no = ucaa->ucaa_bulkout;
    632 	sc->sc_bulkin_no = ucaa->ucaa_bulkin;
    633 	sc->sc_ibufsize = ucaa->ucaa_ibufsize;
    634 	sc->sc_ibufsizepad = ucaa->ucaa_ibufsizepad;
    635 	sc->sc_obufsize = ucaa->ucaa_obufsize;
    636 	sc->sc_opkthdrlen = ucaa->ucaa_opkthdrlen;
    637 	sc->sc_methods = ucaa->ucaa_methods;
    638 	sc->sc_parent = ucaa->ucaa_arg;
    639 	sc->sc_portno = ucaa->ucaa_portno;
    640 
    641 	sc->sc_lsr = 0;
    642 	sc->sc_msr = 0;
    643 	sc->sc_mcr = 0;
    644 	sc->sc_tx_stopped = 0;
    645 	sc->sc_swflags = 0;
    646 	sc->sc_closing = false;
    647 	sc->sc_dying = false;
    648 	sc->sc_cn = NULL;
    649 	sc->sc_state = UCOM_DEAD;
    650 
    651 	sc->sc_si = softint_establish(SOFTINT_USB, ucom_softintr, sc);
    652 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    653 	cv_init(&sc->sc_statecv, "ucomstate");
    654 
    655 	rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev),
    656 	    RND_TYPE_TTY, RND_FLAG_DEFAULT);
    657 
    658 	SIMPLEQ_INIT(&sc->sc_ibuff_empty);
    659 	SIMPLEQ_INIT(&sc->sc_ibuff_full);
    660 	SIMPLEQ_INIT(&sc->sc_obuff_free);
    661 	SIMPLEQ_INIT(&sc->sc_obuff_full);
    662 
    663 	memset(sc->sc_ibuff, 0, sizeof(sc->sc_ibuff));
    664 	memset(sc->sc_obuff, 0, sizeof(sc->sc_obuff));
    665 
    666 	DPRINTF("open pipes in=%jd out=%jd",
    667 	    sc->sc_bulkin_no, sc->sc_bulkout_no, 0, 0);
    668 
    669 	struct ucom_buffer *ub;
    670 	usbd_status err;
    671 	int error;
    672 
    673 	/* Open the bulk pipes */
    674 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no,
    675 	    USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
    676 	if (err) {
    677 		DPRINTF("open bulk in error (addr %jd), err=%jd",
    678 		    sc->sc_bulkin_no, err, 0, 0);
    679 		error = EIO;
    680 		goto fail_0;
    681 	}
    682 	/* Allocate input buffers */
    683 	for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
    684 	    ub++) {
    685 		error = usbd_create_xfer(sc->sc_bulkin_pipe,
    686 		    sc->sc_ibufsizepad, 0, 0,
    687 		    &ub->ub_xfer);
    688 		if (error)
    689 			goto fail_1;
    690 		ub->ub_data = usbd_get_buffer(ub->ub_xfer);
    691 	}
    692 
    693 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
    694 	    USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
    695 	if (err) {
    696 		DPRINTF("open bulk out error (addr %jd), err=%jd",
    697 		    sc->sc_bulkout_no, err, 0, 0);
    698 		error = EIO;
    699 		goto fail_1;
    700 	}
    701 	for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
    702 	    ub++) {
    703 		error = usbd_create_xfer(sc->sc_bulkout_pipe,
    704 		    sc->sc_obufsize, 0, 0, &ub->ub_xfer);
    705 		if (error)
    706 			goto fail_2;
    707 		ub->ub_data = usbd_get_buffer(ub->ub_xfer);
    708 		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
    709 	}
    710 
    711 	struct tty *tp = tty_alloc();
    712 	tp->t_oproc = ucomstart;
    713 	tp->t_param = ucomparam;
    714 	tp->t_hwiflow = ucomhwiflow;
    715 	tp->t_softc = sc;
    716 
    717 	sc->sc_tty = tp;
    718 
    719 	DPRINTF("tty_attach %#jx", (uintptr_t)tp, 0, 0, 0);
    720 	tty_attach(tp);
    721 
    722 	if (!pmf_device_register(self, NULL, NULL))
    723 		aprint_error_dev(self, "couldn't establish power handler\n");
    724 
    725 	sc->sc_state = UCOM_ATTACHED;
    726 
    727 	/*
    728 	 * Attach ucom console
    729 	 */
    730 	ucom_cninit(sc);
    731 
    732 	return;
    733 
    734 fail_2:
    735 	for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
    736 	    ub++) {
    737 		if (ub->ub_xfer)
    738 			usbd_destroy_xfer(ub->ub_xfer);
    739 	}
    740 	usbd_close_pipe(sc->sc_bulkout_pipe);
    741 	sc->sc_bulkout_pipe = NULL;
    742 
    743 fail_1:
    744 	for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
    745 	    ub++) {
    746 		if (ub->ub_xfer)
    747 			usbd_destroy_xfer(ub->ub_xfer);
    748 	}
    749 	usbd_close_pipe(sc->sc_bulkin_pipe);
    750 	sc->sc_bulkin_pipe = NULL;
    751 
    752 fail_0:
    753 	aprint_error_dev(self, "attach failed, error=%d\n", error);
    754 }
    755 
    756 int
    757 ucom_cnattach(int unit, int speed)
    758 {
    759 	int major = cdevsw_lookup_major(&ucom_cdevsw);
    760 
    761 	/*
    762 	 * This is called from consinit(), but at that time the
    763 	 * usb devices are not yet attached, hence we must defer
    764 	 * ucom console attachement until ucom_attach(). We set
    765 	 * ucomcn.cn_dev to signal ucom_attach() which ucom
    766 	 * unit should attach the console.
    767 	 */
    768 	ucom_cnops.cn_dev = makedev(major, unit);
    769 	ucom_cnspeed = speed;
    770 
    771 	return 0;
    772 }
    773 
    774 
    775 int
    776 ucom_detach(device_t self, int flags)
    777 {
    778 	struct ucom_softc *sc = device_private(self);
    779 	struct tty *tp = sc->sc_tty;
    780 	int maj, mn;
    781 	int i;
    782 
    783 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    784 
    785 	DPRINTF("sc=%#jx flags=%jd tp=%#jx", (uintptr_t)sc, flags,
    786 	    (uintptr_t)tp, 0);
    787 	DPRINTF("... pipe=%jd,%jd", sc->sc_bulkin_no, sc->sc_bulkout_no, 0, 0);
    788 
    789 	/* Prevent new opens from hanging.  */
    790 	mutex_enter(&sc->sc_lock);
    791 	sc->sc_dying = true;
    792 	mutex_exit(&sc->sc_lock);
    793 
    794 	/*
    795 	 * If console is attached, we still have input pipe used
    796 	 * after device is closed, so that we can catch cnmagic
    797 	 * and run DDB. It is time to abort the pipe now.
    798 	 */
    799 	if (sc->sc_cn)
    800 		usbd_abort_pipe(sc->sc_bulkin_pipe);
    801 
    802 	pmf_device_deregister(self);
    803 
    804 	/* tty is now off.  */
    805 	if (tp != NULL) {
    806 		ttylock(tp);
    807 		CLR(tp->t_state, TS_CARR_ON);
    808 		CLR(tp->t_cflag, CLOCAL | MDMBUF);
    809 		ttyunlock(tp);
    810 	}
    811 
    812 	/* locate the major number */
    813 	maj = cdevsw_lookup_major(&ucom_cdevsw);
    814 
    815 	/* Nuke the vnodes for any open instances. */
    816 	mn = device_unit(self);
    817 	DPRINTF("maj=%jd mn=%jd", maj, mn, 0, 0);
    818 	vdevgone(maj, mn, mn, VCHR);
    819 	vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR);
    820 	vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR);
    821 
    822 	softint_disestablish(sc->sc_si);
    823 
    824 	/* Detach and free the tty. */
    825 	if (tp != NULL) {
    826 		tty_detach(tp);
    827 		tty_free(tp);
    828 		sc->sc_tty = NULL;
    829 	}
    830 
    831 	for (i = 0; i < UCOM_IN_BUFFS; i++) {
    832 		if (sc->sc_ibuff[i].ub_xfer != NULL)
    833 			usbd_destroy_xfer(sc->sc_ibuff[i].ub_xfer);
    834 	}
    835 
    836 	for (i = 0; i < UCOM_OUT_BUFFS; i++) {
    837 		if (sc->sc_obuff[i].ub_xfer != NULL)
    838 			usbd_destroy_xfer(sc->sc_obuff[i].ub_xfer);
    839 	}
    840 
    841 	if (sc->sc_bulkin_pipe != NULL) {
    842 		usbd_close_pipe(sc->sc_bulkin_pipe);
    843 		sc->sc_bulkin_pipe = NULL;
    844 	}
    845 
    846 	if (sc->sc_bulkout_pipe != NULL) {
    847 		usbd_close_pipe(sc->sc_bulkout_pipe);
    848 		sc->sc_bulkout_pipe = NULL;
    849 	}
    850 
    851 	/*
    852 	 * Detach console after pipes are closed, so that
    853 	 * cn->cn_xfer_out is properly aborted.
    854 	 */
    855 	if (sc->sc_cn) {
    856 		ucom_cnhalt(sc->sc_cn->cn_dev);
    857 		sc->sc_cn = NULL;
    858 	}
    859 
    860 	/* Detach the random source */
    861 	rnd_detach_source(&sc->sc_rndsource);
    862 
    863 	mutex_destroy(&sc->sc_lock);
    864 	cv_destroy(&sc->sc_statecv);
    865 
    866 	return 0;
    867 }
    868 
    869 void
    870 ucom_shutdown(struct ucom_softc *sc)
    871 {
    872 	struct tty *tp = sc->sc_tty;
    873 
    874 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    875 
    876 	/*
    877 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    878 	 * notice even if we immediately open the port again.
    879 	 */
    880 	if (ISSET(tp->t_cflag, HUPCL)) {
    881 		ucom_dtr(sc, 0);
    882 		mutex_enter(&sc->sc_lock);
    883 		microuptime(&sc->sc_hup_time);
    884 		sc->sc_hup_time.tv_sec++;
    885 		mutex_exit(&sc->sc_lock);
    886 	}
    887 }
    888 
    889 /*
    890  * ucomopen(dev, flag, mode, l)
    891  *
    892  *	Called when anyone tries to open /dev/ttyU? for an existing
    893  *	ucom? instance that has completed attach.  The attach may have
    894  *	failed, though, or there may be concurrent detach or close in
    895  *	progress, so fail if attach failed (no sc_tty) or detach has
    896  *	begun (sc_dying), or wait if there's a concurrent close in
    897  *	progress before reopening.
    898  */
    899 int
    900 ucomopen(dev_t dev, int flag, int mode, struct lwp *l)
    901 {
    902 	const int unit = UCOMUNIT(dev);
    903 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    904 	int error = 0;
    905 
    906 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    907 
    908 	mutex_enter(&sc->sc_lock);
    909 	if (sc->sc_dying) {
    910 		DPRINTF("... dying", 0, 0, 0, 0);
    911 		mutex_exit(&sc->sc_lock);
    912 		return ENXIO;
    913 	}
    914 
    915 	if (!device_is_active(sc->sc_dev)) {
    916 		mutex_exit(&sc->sc_lock);
    917 		return ENXIO;
    918 	}
    919 
    920 	struct tty *tp = sc->sc_tty;
    921 	if (tp == NULL) {
    922 		DPRINTF("... not attached", 0, 0, 0, 0);
    923 		mutex_exit(&sc->sc_lock);
    924 		return ENXIO;
    925 	}
    926 
    927 	DPRINTF("unit=%jd, tp=%#jx", unit, (uintptr_t)tp, 0, 0);
    928 
    929 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) {
    930 		mutex_exit(&sc->sc_lock);
    931 		return EBUSY;
    932 	}
    933 
    934 	/*
    935 	 * If the previous use had set DTR on close, wait up to one
    936 	 * second for the other side to notice we hung up.  After
    937 	 * sleeping, the tty may have been revoked, so restart the
    938 	 * whole operation.
    939 	 *
    940 	 * XXX The wchan is not ttclose but maybe should be.
    941 	 */
    942 	if (timerisset(&sc->sc_hup_time)) {
    943 		struct timeval now, delta;
    944 		int ms, ticks;
    945 
    946 		microuptime(&now);
    947 		if (timercmp(&now, &sc->sc_hup_time, <)) {
    948 			timersub(&sc->sc_hup_time, &now, &delta);
    949 			ms = MIN(INT_MAX - 1000, delta.tv_sec*1000);
    950 			ms += howmany(delta.tv_usec, 1000);
    951 			ticks = MAX(1, MIN(INT_MAX, mstohz(ms)));
    952 			(void)cv_timedwait(&sc->sc_statecv, &sc->sc_lock,
    953 			    ticks);
    954 			mutex_exit(&sc->sc_lock);
    955 			return ERESTART;
    956 		}
    957 		timerclear(&sc->sc_hup_time);
    958 	}
    959 
    960 	/*
    961 	 * Wait while the device is initialized by the
    962 	 * first opener or cleaned up by the last closer.
    963 	 */
    964 	enum ucom_state state = sc->sc_state;
    965 	if (state == UCOM_OPENING || sc->sc_closing) {
    966 		if (flag & FNONBLOCK)
    967 			error = EWOULDBLOCK;
    968 		else
    969 			error = cv_wait_sig(&sc->sc_statecv, &sc->sc_lock);
    970 		mutex_exit(&sc->sc_lock);
    971 		return error ? error : ERESTART;
    972 	}
    973 	KASSERTMSG(state == UCOM_OPEN || state == UCOM_ATTACHED,
    974 	    "state is %d", state);
    975 
    976 	/*
    977 	 * If this is the first open, then make sure the pipes are
    978 	 * running and perform any initialization needed.
    979 	 */
    980 	bool firstopen = (state == UCOM_ATTACHED);
    981 	if (firstopen) {
    982 		KASSERT(!ISSET(tp->t_state, TS_ISOPEN));
    983 		KASSERT(tp->t_wopen == 0);
    984 
    985 		tp->t_dev = dev;
    986 		sc->sc_state = UCOM_OPENING;
    987 		mutex_exit(&sc->sc_lock);
    988 
    989 		if (sc->sc_methods->ucom_open != NULL) {
    990 			error = sc->sc_methods->ucom_open(sc->sc_parent,
    991 			    sc->sc_portno);
    992 			if (error)
    993 				goto bad;
    994 		}
    995 
    996 		ucom_status_change(sc);
    997 
    998 		/* Clear PPS capture state on first open. */
    999 		mutex_spin_enter(&timecounter_lock);
   1000 		memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state));
   1001 		sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
   1002 		pps_init(&sc->sc_pps_state);
   1003 		mutex_spin_exit(&timecounter_lock);
   1004 
   1005 		/*
   1006 		 * Initialize the termios status to the defaults.  Add in the
   1007 		 * sticky bits from TIOCSFLAGS.
   1008 		 */
   1009 		struct termios t;
   1010 
   1011 		if (cn_isconsole(dev)) {
   1012 			t.c_ispeed = ucom_cnspeed;
   1013 			t.c_ospeed = ucom_cnspeed;
   1014 		} else {
   1015 			t.c_ispeed = 0;
   1016 			t.c_ospeed = TTYDEF_SPEED;
   1017 		}
   1018 		t.c_cflag = TTYDEF_CFLAG;
   1019 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
   1020 			SET(t.c_cflag, CLOCAL);
   1021 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
   1022 			SET(t.c_cflag, CRTSCTS);
   1023 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
   1024 			SET(t.c_cflag, MDMBUF);
   1025 		/* Make sure ucomparam() will do something. */
   1026 		tp->t_ospeed = 0;
   1027 		(void) ucomparam(tp, &t);
   1028 		tp->t_iflag = TTYDEF_IFLAG;
   1029 		tp->t_oflag = TTYDEF_OFLAG;
   1030 		tp->t_lflag = TTYDEF_LFLAG;
   1031 		ttychars(tp);
   1032 		ttsetwater(tp);
   1033 
   1034 		/*
   1035 		 * Turn on DTR.  We must always do this, even if carrier is not
   1036 		 * present, because otherwise we'd have to use TIOCSDTR
   1037 		 * immediately after setting CLOCAL, which applications do not
   1038 		 * expect.  We always assert DTR while the device is open
   1039 		 * unless explicitly requested to deassert it.  Ditto RTS.
   1040 		 */
   1041 		ucom_dtr(sc, 1);
   1042 		ucom_rts(sc, 1);
   1043 
   1044 		mutex_enter(&sc->sc_lock);
   1045 		sc->sc_rx_unblock = 0;
   1046 		sc->sc_rx_stopped = 0;
   1047 		sc->sc_tx_stopped = 0;
   1048 
   1049 		/*
   1050 		 * If device is console, input xfers are scheduled
   1051 		 * from attach to detach time. Otherwise from open
   1052 		 * here to close.
   1053 		 */
   1054 		if (!cn_isconsole(dev)) {
   1055 			/*
   1056 			 * Either after ucom was attached, or after
   1057 			 * ucomclose(), this list must be empty,
   1058 			 * otherwise ucomsubmitread() will corrupt
   1059 			 * it by queueing an ucom_buffer that is already
   1060 			 *  queued.
   1061 			 */
   1062 			KASSERT(SIMPLEQ_EMPTY(&sc->sc_ibuff_empty));
   1063 
   1064 			for (size_t i = 0; i < UCOM_IN_BUFFS; i++) {
   1065 				struct ucom_buffer *ub = &sc->sc_ibuff[i];
   1066 				error = ucomsubmitread(sc, ub);
   1067 				if (error) {
   1068 					mutex_exit(&sc->sc_lock);
   1069 					goto bad;
   1070 				}
   1071 			}
   1072 		}
   1073 	}
   1074 	mutex_exit(&sc->sc_lock);
   1075 
   1076 	DPRINTF("unit=%jd, tp=%#jx dialout %jd nonblock %jd", unit,
   1077 	    (uintptr_t)tp, !!UCOMDIALOUT(dev), !!ISSET(flag, O_NONBLOCK));
   1078 	error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
   1079 	if (error)
   1080 		goto bad;
   1081 
   1082 	error = (*tp->t_linesw->l_open)(dev, tp);
   1083 	if (error)
   1084 		goto bad;
   1085 
   1086 	/*
   1087 	 * Success!  If this was the first open, notify waiters that
   1088 	 * the tty is open for business.
   1089 	 */
   1090 	if (firstopen) {
   1091 		mutex_enter(&sc->sc_lock);
   1092 		KASSERT(sc->sc_state == UCOM_OPENING);
   1093 		sc->sc_state = UCOM_OPEN;
   1094 		cv_broadcast(&sc->sc_statecv);
   1095 		mutex_exit(&sc->sc_lock);
   1096 	}
   1097 	return 0;
   1098 
   1099 bad:
   1100 	/*
   1101 	 * Failure!  If this was the first open, hang up, abort pipes,
   1102 	 * and notify waiters that we're not opening after all.
   1103 	 */
   1104 	if (firstopen) {
   1105 		ucom_cleanup(sc, flag);
   1106 
   1107 		mutex_enter(&sc->sc_lock);
   1108 		KASSERT(sc->sc_state == UCOM_OPENING);
   1109 		sc->sc_state = UCOM_ATTACHED;
   1110 		cv_broadcast(&sc->sc_statecv);
   1111 		mutex_exit(&sc->sc_lock);
   1112 	}
   1113 	return error;
   1114 }
   1115 
   1116 /*
   1117  * ucomcancel(dev, flag, mode, l)
   1118  *
   1119  *	Called on revoke or last close.  Must interrupt any pending I/O
   1120  *	operations and make them fail promptly; once they have all
   1121  *	finished (except possibly new opens), ucomclose will be called.
   1122  *	We set sc_closing to block new opens until ucomclose runs.
   1123  */
   1124 int
   1125 ucomcancel(dev_t dev, int flag, int mode, struct lwp *l)
   1126 {
   1127 	const int unit = UCOMUNIT(dev);
   1128 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit);
   1129 
   1130 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1131 
   1132 	DPRINTF("unit=%jd", UCOMUNIT(dev), 0, 0, 0);
   1133 
   1134 	/*
   1135 	 * This can run at any time before ucomclose on any device
   1136 	 * node, even if never attached or if attach failed, so we may
   1137 	 * not have a softc or a tty.
   1138 	 */
   1139 	if (sc == NULL)
   1140 		return 0;
   1141 	struct tty *tp = sc->sc_tty;
   1142 	if (tp == NULL)
   1143 		return 0;
   1144 
   1145 	/*
   1146 	 * Mark the device closing so opens block until we're done
   1147 	 * closing.  Wake them up so they start over at the top -- if
   1148 	 * we're closing because we're detaching, they need to wake up
   1149 	 * and notice it's time to fail.
   1150 	 */
   1151 	mutex_enter(&sc->sc_lock);
   1152 	sc->sc_closing = true;
   1153 	cv_broadcast(&sc->sc_statecv);
   1154 	mutex_exit(&sc->sc_lock);
   1155 
   1156 	/*
   1157 	 * Cancel any pending tty I/O operations, causing them to wake
   1158 	 * up and fail promptly, and preventing any new ones from
   1159 	 * starting to wait until we have finished closing.
   1160 	 */
   1161 	ttycancel(tp);
   1162 
   1163 	return 0;
   1164 }
   1165 
   1166 /*
   1167  * ucomclose(dev, flag, mode, l)
   1168  *
   1169  *	Called after ucomcancel, when all prior operations on the /dev
   1170  *	node have completed.  Only new opens may be in progress at this
   1171  *	point, but they will block until sc_closing is set to false.
   1172  */
   1173 int
   1174 ucomclose(dev_t dev, int flag, int mode, struct lwp *l)
   1175 {
   1176 	const int unit = UCOMUNIT(dev);
   1177 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit);
   1178 	int error = 0;
   1179 
   1180 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1181 
   1182 	DPRINTF("unit=%jd", UCOMUNIT(dev), 0, 0, 0);
   1183 
   1184 	/*
   1185 	 * This can run at any time after ucomcancel on any device
   1186 	 * node, even if never attached or if attach failed, so we may
   1187 	 * not have a softc or a tty.
   1188 	 */
   1189 	if (sc == NULL)
   1190 		return 0;
   1191 	struct tty *tp = sc->sc_tty;
   1192 	if (tp == NULL)
   1193 		return 0;
   1194 
   1195 	/*
   1196 	 * Close the tty, causing anyone waiting for it to wake, and
   1197 	 * hang up the phone.
   1198 	 */
   1199 	ucom_cleanup(sc, flag);
   1200 
   1201 	/*
   1202 	 * ttyclose should have cleared TS_ISOPEN and interrupted all
   1203 	 * pending opens, which should have completed by now.
   1204 	 */
   1205 	ttylock(tp);
   1206 	KASSERT(!ISSET(tp->t_state, TS_ISOPEN));
   1207 	KASSERT(tp->t_wopen == 0);
   1208 	ttyunlock(tp);
   1209 
   1210 	/*
   1211 	 * Close any device-specific state.
   1212 	 */
   1213 	if (sc->sc_methods->ucom_close != NULL) {
   1214 		sc->sc_methods->ucom_close(sc->sc_parent,
   1215 		    sc->sc_portno);
   1216 	}
   1217 
   1218 	/*
   1219 	 * We're now closed.  Can reopen after this point, so resume
   1220 	 * transfers, mark us no longer closing, and notify anyone
   1221 	 * waiting in open.  The state may be OPEN or ATTACHED at this
   1222 	 * point -- OPEN if the device was already open when we closed
   1223 	 * it, ATTACHED if we interrupted it in the process of opening.
   1224 	 */
   1225 	mutex_enter(&sc->sc_lock);
   1226 	KASSERTMSG(sc->sc_state == UCOM_ATTACHED || sc->sc_state == UCOM_OPEN,
   1227 	    "%s sc=%p state=%d", device_xname(sc->sc_dev), sc, sc->sc_state);
   1228 	KASSERT(sc->sc_closing);
   1229 	sc->sc_state = UCOM_ATTACHED;
   1230 	sc->sc_closing = false;
   1231 	cv_broadcast(&sc->sc_statecv);
   1232 	mutex_exit(&sc->sc_lock);
   1233 
   1234 	return error;
   1235 }
   1236 
   1237 int
   1238 ucomread(dev_t dev, struct uio *uio, int flag)
   1239 {
   1240 	const int unit = UCOMUNIT(dev);
   1241 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1242 	struct tty *tp = sc->sc_tty;
   1243 
   1244 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1245 
   1246 	return (*tp->t_linesw->l_read)(tp, uio, flag);
   1247 }
   1248 
   1249 int
   1250 ucomwrite(dev_t dev, struct uio *uio, int flag)
   1251 {
   1252 	const int unit = UCOMUNIT(dev);
   1253 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1254 	struct tty *tp = sc->sc_tty;
   1255 
   1256 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1257 
   1258 	return (*tp->t_linesw->l_write)(tp, uio, flag);
   1259 }
   1260 
   1261 int
   1262 ucompoll(dev_t dev, int events, struct lwp *l)
   1263 {
   1264 	const int unit = UCOMUNIT(dev);
   1265 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1266 	struct tty *tp = sc->sc_tty;
   1267 
   1268 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1269 
   1270 	return (*tp->t_linesw->l_poll)(tp, events, l);
   1271 }
   1272 
   1273 struct tty *
   1274 ucomtty(dev_t dev)
   1275 {
   1276 	const int unit = UCOMUNIT(dev);
   1277 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1278 
   1279 	return sc->sc_tty;
   1280 }
   1281 
   1282 int
   1283 ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1284 {
   1285 	const int unit = UCOMUNIT(dev);
   1286 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1287 	struct tty *tp = sc->sc_tty;
   1288 	int error;
   1289 
   1290 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1291 
   1292 	DPRINTF("cmd=0x%08jx", cmd, 0, 0, 0);
   1293 
   1294 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
   1295 	if (error != EPASSTHROUGH)
   1296 		return error;
   1297 
   1298 	error = ttioctl(tp, cmd, data, flag, l);
   1299 	if (error != EPASSTHROUGH)
   1300 		return error;
   1301 
   1302 	if (sc->sc_methods->ucom_ioctl != NULL) {
   1303 		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
   1304 		    sc->sc_portno, cmd, data, flag, l->l_proc);
   1305 		if (error != EPASSTHROUGH)
   1306 			return error;
   1307 	}
   1308 
   1309 	error = 0;
   1310 
   1311 	DPRINTF("our cmd=0x%08jx", cmd, 0, 0, 0);
   1312 
   1313 	switch (cmd) {
   1314 	case TIOCSBRK:
   1315 		ucom_break(sc, 1);
   1316 		break;
   1317 
   1318 	case TIOCCBRK:
   1319 		ucom_break(sc, 0);
   1320 		break;
   1321 
   1322 	case TIOCSDTR:
   1323 		ucom_dtr(sc, 1);
   1324 		break;
   1325 
   1326 	case TIOCCDTR:
   1327 		ucom_dtr(sc, 0);
   1328 		break;
   1329 
   1330 	case TIOCGFLAGS:
   1331 		mutex_enter(&sc->sc_lock);
   1332 		*(int *)data = sc->sc_swflags;
   1333 		mutex_exit(&sc->sc_lock);
   1334 		break;
   1335 
   1336 	case TIOCSFLAGS:
   1337 		error = kauth_authorize_device_tty(l->l_cred,
   1338 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
   1339 		if (error)
   1340 			break;
   1341 
   1342 		int swflags = *(int *)data;
   1343 		/* Force local if device is console */
   1344 		if (cn_isconsole(dev))
   1345 			swflags |= TIOCFLAG_SOFTCAR;
   1346 
   1347 		mutex_enter(&sc->sc_lock);
   1348 		sc->sc_swflags = swflags;
   1349 		mutex_exit(&sc->sc_lock);
   1350 		break;
   1351 
   1352 	case TIOCMSET:
   1353 	case TIOCMBIS:
   1354 	case TIOCMBIC:
   1355 		tiocm_to_ucom(sc, cmd, *(int *)data);
   1356 		break;
   1357 
   1358 	case TIOCMGET:
   1359 		*(int *)data = ucom_to_tiocm(sc);
   1360 		break;
   1361 
   1362 	case PPS_IOC_CREATE:
   1363 	case PPS_IOC_DESTROY:
   1364 	case PPS_IOC_GETPARAMS:
   1365 	case PPS_IOC_SETPARAMS:
   1366 	case PPS_IOC_GETCAP:
   1367 	case PPS_IOC_FETCH:
   1368 #ifdef PPS_SYNC
   1369 	case PPS_IOC_KCBIND:
   1370 #endif
   1371 		mutex_spin_enter(&timecounter_lock);
   1372 		error = pps_ioctl(cmd, data, &sc->sc_pps_state);
   1373 		mutex_spin_exit(&timecounter_lock);
   1374 		break;
   1375 
   1376 	default:
   1377 		error = EPASSTHROUGH;
   1378 		break;
   1379 	}
   1380 
   1381 	return error;
   1382 }
   1383 
   1384 static void
   1385 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
   1386 {
   1387 	u_char combits;
   1388 
   1389 	combits = 0;
   1390 	if (ISSET(ttybits, TIOCM_DTR))
   1391 		SET(combits, UMCR_DTR);
   1392 	if (ISSET(ttybits, TIOCM_RTS))
   1393 		SET(combits, UMCR_RTS);
   1394 
   1395 	mutex_enter(&sc->sc_lock);
   1396 	switch (how) {
   1397 	case TIOCMBIC:
   1398 		CLR(sc->sc_mcr, combits);
   1399 		break;
   1400 
   1401 	case TIOCMBIS:
   1402 		SET(sc->sc_mcr, combits);
   1403 		break;
   1404 
   1405 	case TIOCMSET:
   1406 		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
   1407 		SET(sc->sc_mcr, combits);
   1408 		break;
   1409 	}
   1410 	u_char mcr = sc->sc_mcr;
   1411 	mutex_exit(&sc->sc_lock);
   1412 
   1413 	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
   1414 		ucom_dtr(sc, (mcr & UMCR_DTR) != 0);
   1415 	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
   1416 		ucom_rts(sc, (mcr & UMCR_RTS) != 0);
   1417 }
   1418 
   1419 static int
   1420 ucom_to_tiocm(struct ucom_softc *sc)
   1421 {
   1422 	u_char combits;
   1423 	int ttybits = 0;
   1424 
   1425 	mutex_enter(&sc->sc_lock);
   1426 	combits = sc->sc_mcr;
   1427 	if (ISSET(combits, UMCR_DTR))
   1428 		SET(ttybits, TIOCM_DTR);
   1429 	if (ISSET(combits, UMCR_RTS))
   1430 		SET(ttybits, TIOCM_RTS);
   1431 
   1432 	combits = sc->sc_msr;
   1433 	if (ISSET(combits, UMSR_DCD))
   1434 		SET(ttybits, TIOCM_CD);
   1435 	if (ISSET(combits, UMSR_CTS))
   1436 		SET(ttybits, TIOCM_CTS);
   1437 	if (ISSET(combits, UMSR_DSR))
   1438 		SET(ttybits, TIOCM_DSR);
   1439 	if (ISSET(combits, UMSR_RI | UMSR_TERI))
   1440 		SET(ttybits, TIOCM_RI);
   1441 
   1442 #if 0
   1443 XXX;
   1444 	if (sc->sc_ier != 0)
   1445 		SET(ttybits, TIOCM_LE);
   1446 #endif
   1447 	mutex_exit(&sc->sc_lock);
   1448 
   1449 	return ttybits;
   1450 }
   1451 
   1452 static void
   1453 ucom_break(struct ucom_softc *sc, int onoff)
   1454 {
   1455 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1456 
   1457 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
   1458 
   1459 	if (sc->sc_methods->ucom_set != NULL)
   1460 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1461 		    UCOM_SET_BREAK, onoff);
   1462 }
   1463 
   1464 static void
   1465 ucom_dtr(struct ucom_softc *sc, int onoff)
   1466 {
   1467 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1468 
   1469 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
   1470 
   1471 	if (sc->sc_methods->ucom_set != NULL)
   1472 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1473 		    UCOM_SET_DTR, onoff);
   1474 }
   1475 
   1476 static void
   1477 ucom_rts(struct ucom_softc *sc, int onoff)
   1478 {
   1479 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1480 
   1481 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
   1482 
   1483 	if (sc->sc_methods->ucom_set != NULL)
   1484 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1485 		    UCOM_SET_RTS, onoff);
   1486 }
   1487 
   1488 void
   1489 ucom_status_change(struct ucom_softc *sc)
   1490 {
   1491 	/* We get here from sc->sc_methods->ucom_read */
   1492 	if (ucom_polling(sc))
   1493 		return;
   1494 
   1495 	if (sc->sc_methods->ucom_get_status != NULL) {
   1496 		struct tty *tp = sc->sc_tty;
   1497 		u_char msr, lsr;
   1498 
   1499 		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
   1500 		    &lsr, &msr);
   1501 		mutex_enter(&sc->sc_lock);
   1502 		u_char old_msr = sc->sc_msr;
   1503 
   1504 		sc->sc_lsr = lsr;
   1505 		sc->sc_msr = msr;
   1506 		mutex_exit(&sc->sc_lock);
   1507 
   1508 		if (ISSET((msr ^ old_msr), UMSR_DCD)) {
   1509 			mutex_spin_enter(&timecounter_lock);
   1510 			pps_capture(&sc->sc_pps_state);
   1511 			pps_event(&sc->sc_pps_state,
   1512 			    (sc->sc_msr & UMSR_DCD) ?
   1513 			    PPS_CAPTUREASSERT :
   1514 			    PPS_CAPTURECLEAR);
   1515 			mutex_spin_exit(&timecounter_lock);
   1516 
   1517 			(*tp->t_linesw->l_modem)(tp, ISSET(msr, UMSR_DCD));
   1518 		}
   1519 	} else {
   1520 		mutex_enter(&sc->sc_lock);
   1521 		sc->sc_lsr = 0;
   1522 		/* Assume DCD is present, if we have no chance to check it. */
   1523 		sc->sc_msr = UMSR_DCD;
   1524 		mutex_exit(&sc->sc_lock);
   1525 	}
   1526 }
   1527 
   1528 static int
   1529 ucomparam(struct tty *tp, struct termios *t)
   1530 {
   1531 	struct ucom_softc * const sc = tp->t_softc;
   1532 	int error = 0;
   1533 
   1534 	/* XXX should take tty lock around touching tp */
   1535 
   1536 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1537 
   1538 	/* Check requested parameters. */
   1539 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
   1540 		error = EINVAL;
   1541 		goto out;
   1542 	}
   1543 
   1544 	/*
   1545 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1546 	 * is always active.
   1547 	 */
   1548 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
   1549 		SET(t->c_cflag, CLOCAL);
   1550 		CLR(t->c_cflag, HUPCL);
   1551 	}
   1552 
   1553 	/*
   1554 	 * If there were no changes, don't do anything.  This avoids dropping
   1555 	 * input and improves performance when all we did was frob things like
   1556 	 * VMIN and VTIME.
   1557 	 */
   1558 	if (tp->t_ospeed == t->c_ospeed &&
   1559 	    tp->t_cflag == t->c_cflag) {
   1560 		goto out;
   1561 	}
   1562 
   1563 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
   1564 
   1565 	/* And copy to tty. */
   1566 	tp->t_ispeed = 0;
   1567 	tp->t_ospeed = t->c_ospeed;
   1568 	tp->t_cflag = t->c_cflag;
   1569 
   1570 	if (sc->sc_methods->ucom_param != NULL) {
   1571 		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
   1572 		    t);
   1573 		if (error)
   1574 			goto out;
   1575 	}
   1576 
   1577 	/* XXX worry about CHWFLOW */
   1578 
   1579 	/*
   1580 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1581 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1582 	 * explicit request.
   1583 	 */
   1584 	DPRINTF("l_modem", 0, 0, 0, 0);
   1585 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
   1586 
   1587 #if 0
   1588 XXX what if the hardware is not open
   1589 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1590 		if (sc->sc_tx_stopped) {
   1591 			sc->sc_tx_stopped = 0;
   1592 			ucomstart(tp);
   1593 		}
   1594 	}
   1595 #endif
   1596 out:
   1597 	return error;
   1598 }
   1599 
   1600 static int
   1601 ucomhwiflow(struct tty *tp, int block)
   1602 {
   1603 	struct ucom_softc * const sc = tp->t_softc;
   1604 	int old;
   1605 
   1606 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1607 
   1608 	KASSERT(&sc->sc_lock);
   1609 	KASSERT(ttylocked(tp));
   1610 
   1611 	old = sc->sc_rx_stopped;
   1612 	sc->sc_rx_stopped = (u_char)block;
   1613 
   1614 	if (old && !block) {
   1615 		sc->sc_rx_unblock = 1;
   1616 		kpreempt_disable();
   1617 		softint_schedule(sc->sc_si);
   1618 		kpreempt_enable();
   1619 	}
   1620 
   1621 	return 1;
   1622 }
   1623 
   1624 static void
   1625 ucomstart(struct tty *tp)
   1626 {
   1627 	struct ucom_softc * const sc = tp->t_softc;
   1628 	struct ucom_buffer *ub;
   1629 	u_char *data;
   1630 	int cnt;
   1631 
   1632 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1633 
   1634 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1635 		goto out;
   1636 	if (sc->sc_tx_stopped)
   1637 		goto out;
   1638 
   1639 	if (!ttypull(tp))
   1640 		goto out;
   1641 
   1642 	/* Grab the first contiguous region of buffer space. */
   1643 	data = tp->t_outq.c_cf;
   1644 	cnt = ndqb(&tp->t_outq, 0);
   1645 
   1646 	if (cnt == 0)
   1647 		goto out;
   1648 
   1649 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_free);
   1650 	if (ub == NULL) {
   1651 		SET(tp->t_state, TS_BUSY);
   1652 		goto out;
   1653 	}
   1654 
   1655 	SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_free, ub_link);
   1656 
   1657 	if (SIMPLEQ_FIRST(&sc->sc_obuff_free) == NULL)
   1658 		SET(tp->t_state, TS_BUSY);
   1659 
   1660 	if (cnt > sc->sc_obufsize)
   1661 		cnt = sc->sc_obufsize;
   1662 
   1663 	if (sc->sc_methods->ucom_write != NULL)
   1664 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
   1665 		    ub->ub_data, data, &cnt);
   1666 	else
   1667 		memcpy(ub->ub_data, data, cnt);
   1668 
   1669 	ub->ub_len = cnt;
   1670 	ub->ub_index = 0;
   1671 
   1672 	SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_full, ub, ub_link);
   1673 
   1674 	kpreempt_disable();
   1675 	softint_schedule(sc->sc_si);
   1676 	kpreempt_enable();
   1677 
   1678  out:
   1679 	DPRINTF("... done", 0, 0, 0, 0);
   1680 }
   1681 
   1682 void
   1683 ucomstop(struct tty *tp, int flag)
   1684 {
   1685 #if 0
   1686 	struct ucom_softc * const sc = tp->t_softc;
   1687 
   1688 	mutex_enter(&sc->sc_lock);
   1689 	ttylock(tp);
   1690 	if (ISSET(tp->t_state, TS_BUSY)) {
   1691 		/* obuff_full -> obuff_free? */
   1692 		/* sc->sc_tx_stopped = 1; */
   1693 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1694 			SET(tp->t_state, TS_FLUSH);
   1695 	}
   1696 	ttyunlock(tp);
   1697 	mutex_exit(&sc->sc_lock);
   1698 #endif
   1699 }
   1700 
   1701 static void
   1702 ucom_write_status(struct ucom_softc *sc, struct ucom_buffer *ub,
   1703     usbd_status err)
   1704 {
   1705 	struct tty *tp = sc->sc_tty;
   1706 	uint32_t cc = ub->ub_len;
   1707 	bool polling = ucom_polling(sc);
   1708 
   1709 	KASSERT(polling || mutex_owned(&sc->sc_lock));
   1710 
   1711 	switch (err) {
   1712 	case USBD_IN_PROGRESS:
   1713 		ub->ub_index = ub->ub_len;
   1714 		break;
   1715 	case USBD_STALLED:
   1716 		ub->ub_index = 0;
   1717 		kpreempt_disable();
   1718 		softint_schedule(sc->sc_si);
   1719 		kpreempt_enable();
   1720 		break;
   1721 	case USBD_NORMAL_COMPLETION:
   1722 		usbd_get_xfer_status(ub->ub_xfer, NULL, NULL, &cc, NULL);
   1723 		rnd_add_uint32(&sc->sc_rndsource, cc);
   1724 		/*FALLTHROUGH*/
   1725 	default:
   1726 		SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_full, ub_link);
   1727 		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
   1728 		cc -= sc->sc_opkthdrlen;
   1729 
   1730 		ttylock(tp);
   1731 		CLR(tp->t_state, TS_BUSY);
   1732 		if (ISSET(tp->t_state, TS_FLUSH))
   1733 			CLR(tp->t_state, TS_FLUSH);
   1734 		else
   1735 			ndflush(&tp->t_outq, cc);
   1736 		ttyunlock(tp);
   1737 
   1738 		if (err != USBD_CANCELLED && err != USBD_IOERROR &&
   1739 		    !sc->sc_closing) {
   1740 			if ((ub = SIMPLEQ_FIRST(&sc->sc_obuff_full)) != NULL)
   1741 				ucom_submit_write(sc, ub);
   1742 
   1743 			ttylock(tp);
   1744 			(*tp->t_linesw->l_start)(tp);
   1745 			ttyunlock(tp);
   1746 		}
   1747 		break;
   1748 	}
   1749 }
   1750 
   1751 static void
   1752 ucom_submit_write(struct ucom_softc *sc, struct ucom_buffer *ub)
   1753 {
   1754 
   1755 	KASSERT(mutex_owned(&sc->sc_lock));
   1756 
   1757 	usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, ub->ub_len,
   1758 	    0, USBD_NO_TIMEOUT, ucomwritecb);
   1759 
   1760 	ucom_write_status(sc, ub, usbd_transfer(ub->ub_xfer));
   1761 }
   1762 
   1763 static void
   1764 ucomwritecb(struct usbd_xfer *xfer, void *p, usbd_status status)
   1765 {
   1766 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1767 
   1768 	ucom_mutex_enter(sc);
   1769 	ucom_write_status(sc, SIMPLEQ_FIRST(&sc->sc_obuff_full), status);
   1770 	ucom_mutex_exit(sc);
   1771 
   1772 }
   1773 
   1774 static void
   1775 ucom_softintr(void *arg)
   1776 {
   1777 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1778 
   1779 	struct ucom_softc *sc = arg;
   1780 	struct tty *tp = sc->sc_tty;
   1781 	struct ucom_buffer *ub;
   1782 
   1783 	if (ucom_polling(sc))
   1784 		return;
   1785 
   1786 	mutex_enter(&sc->sc_lock);
   1787 
   1788 #ifdef DDB
   1789 	struct ucom_cnstate *cn = sc->sc_cn;
   1790 
   1791 	if (cn && cn->cn_magic_req) {
   1792 		if (!SIMPLEQ_EMPTY(&sc->sc_ibuff_empty)) {
   1793 			cn->cn_magic_req = false;
   1794 			cn_trap();
   1795 		} else {
   1796 			/*
   1797 			 * Very unlikely case where ucom_softintr()
   1798 			 * raced with ucomreadcb(). Just retry later.
   1799 			 */
   1800 			kpreempt_disable();
   1801 			softint_schedule(sc->sc_si);
   1802 			kpreempt_enable();
   1803 		}
   1804 	}
   1805 #endif
   1806 
   1807 	ttylock(tp);
   1808 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
   1809 		ttyunlock(tp);
   1810 		mutex_exit(&sc->sc_lock);
   1811 		return;
   1812 	}
   1813 	ttyunlock(tp);
   1814 
   1815 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_full);
   1816 
   1817 	if (ub != NULL && ub->ub_index == 0)
   1818 		ucom_submit_write(sc, ub);
   1819 
   1820 	if (sc->sc_rx_unblock)
   1821 		ucom_read_complete(sc);
   1822 
   1823 	mutex_exit(&sc->sc_lock);
   1824 }
   1825 
   1826 static void
   1827 ucom_read_complete(struct ucom_softc *sc)
   1828 {
   1829 	int (*rint)(int, struct tty *);
   1830 	struct ucom_buffer *ub;
   1831 	struct tty *tp;
   1832 
   1833 	KASSERT(mutex_owned(&sc->sc_lock));
   1834 
   1835 	tp = sc->sc_tty;
   1836 	rint = tp->t_linesw->l_rint;
   1837 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1838 
   1839 	while (ub != NULL && !sc->sc_rx_stopped) {
   1840 
   1841 		/* XXX ttyinput takes ttylock */
   1842 		while (ub->ub_index < ub->ub_len && !sc->sc_rx_stopped) {
   1843 			/* Give characters to tty layer. */
   1844 			if ((*rint)(ub->ub_data[ub->ub_index], tp) == -1) {
   1845 				/* Overflow: drop remainder */
   1846 				ub->ub_index = ub->ub_len;
   1847 			} else
   1848 				ub->ub_index++;
   1849 		}
   1850 
   1851 		if (ub->ub_index == ub->ub_len) {
   1852 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
   1853 			ucomsubmitread(sc, ub);
   1854 			ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1855 		}
   1856 	}
   1857 
   1858 	sc->sc_rx_unblock = (ub != NULL);
   1859 }
   1860 
   1861 static int
   1862 ucomsubmitread(struct ucom_softc *sc, struct ucom_buffer *ub)
   1863 {
   1864 	usbd_status err;
   1865 
   1866 	KASSERT(ucom_polling(sc) || mutex_owned(&sc->sc_lock));
   1867 
   1868 	usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, sc->sc_ibufsize,
   1869 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ucomreadcb);
   1870 
   1871 	if ((err = usbd_transfer(ub->ub_xfer)) != USBD_IN_PROGRESS) {
   1872 		/* XXX: Recover from this, please! */
   1873 		device_printf(sc->sc_dev, "%s: err=%s\n",
   1874 		    __func__, usbd_errstr(err));
   1875 		return EIO;
   1876 	}
   1877 
   1878 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_empty, ub, ub_link);
   1879 
   1880 	return 0;
   1881 }
   1882 
   1883 static void
   1884 ucom_cnread_complete(struct ucom_softc *sc)
   1885 {
   1886 	struct ucom_buffer *ub;
   1887 	struct ucom_cnstate * const cn = sc->sc_cn;
   1888 	u_int prodin = cn->cn_prod_in;
   1889 	u_int consin = cn->cn_cons_in;
   1890 	u_int prodin_idx = prodin % sc->sc_ibufsize;
   1891 	u_int consin_idx = consin % sc->sc_ibufsize;
   1892 	uint32_t cc, cnt;
   1893 	u_char *cp;
   1894 
   1895 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1896 
   1897 	while (ub != NULL) {
   1898 		KASSERT(ucom_xfer_done(ub->ub_xfer));
   1899 
   1900 		cp = ub->ub_data + ub->ub_index;
   1901 		cc = ub->ub_len - ub->ub_index;
   1902 
   1903 		if (cc == 0)
   1904 			goto next;
   1905 
   1906 		if (consin_idx > prodin_idx) {
   1907 			if (cc > consin_idx - prodin_idx)
   1908 				cnt = consin_idx - prodin_idx;
   1909 			else
   1910 				cnt = cc;
   1911 
   1912 			KASSERT(prodin_idx + cnt <= sc->sc_ibufsize);
   1913 
   1914 			memcpy(cn->cn_buf_in + prodin_idx, cp, cnt);
   1915 			cn->cn_prod_in += cnt;
   1916 			cp += cnt;
   1917 			cc -= cnt;
   1918 		} else { /* consin_idx <= prodin_idx */
   1919 			/*
   1920 			 * First chunk, from prodin_idx to end of buffer
   1921 			 */
   1922 			if (cc > sc->sc_ibufsize - prodin_idx)
   1923 				cnt = sc->sc_ibufsize - prodin_idx;
   1924 			else
   1925 				cnt = cc;
   1926 
   1927 			KASSERT(prodin_idx + cnt <= sc->sc_ibufsize);
   1928 
   1929 			memcpy(cn->cn_buf_in + prodin_idx, cp, cnt);
   1930 			cn->cn_prod_in += cnt;
   1931 			cp += cnt;
   1932 			cc -= cnt;
   1933 
   1934 			if (cc == 0)
   1935 				goto next;
   1936 
   1937 			/*
   1938 			 * Second chunk, from start of buffer to cosin_idx
   1939 			 */
   1940 			if (cc > consin_idx)
   1941 				cnt = consin_idx;
   1942 			else
   1943 				cnt = cc;
   1944 
   1945 			KASSERT(cn->cn_prod_in + cnt <= sc->sc_ibufsize);
   1946 
   1947 			memcpy(cn->cn_buf_in + prodin_idx, cp, cnt);
   1948 			cn->cn_prod_in += cnt;
   1949 			cp += cnt;
   1950 			cc -= cnt;
   1951 		}
   1952 next:
   1953 		/* If cc > 0 we discard some input */
   1954 
   1955 		SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
   1956 		ucomsubmitread(sc, ub);
   1957 		ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1958 	}
   1959 }
   1960 
   1961 static void
   1962 ucomreadcb(struct usbd_xfer *xfer, void *p, usbd_status status)
   1963 {
   1964 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1965 	struct ucom_buffer *ub;
   1966 	uint32_t cc;
   1967 	u_char *cp;
   1968 
   1969 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1970 
   1971 	ucom_mutex_enter(sc);
   1972 
   1973 	/*
   1974 	 * Always remove the ucom_buffer from the sc_ibuff_empty list
   1975 	 * regardless of transfer status. Leaving it on the list on
   1976 	 * USBD_CANCELLED or USBD_IOERROR would cause corruption
   1977 	 * next time the device is opened.
   1978 	 *
   1979 	 * USBD_CANCELLED is a reported when the device is closed
   1980 	 * via ucomclose() -> usbd_abort_pipe() -> usbd_xfer_abort()
   1981 	 */
   1982 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty);
   1983 	SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_empty, ub_link);
   1984 	KASSERT(xfer == ub->ub_xfer);
   1985 
   1986 	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
   1987 	    sc->sc_closing) {
   1988 		DPRINTF("... done (status %jd closing %jd)",
   1989 		    status, sc->sc_closing, 0, 0);
   1990 
   1991 		/*
   1992 		 * If console is attached, keep reading after
   1993 		 * close so that we can catch cnmagic.
   1994 		 */
   1995 		if (sc->sc_cn)
   1996 			ucomsubmitread(sc, ub);
   1997 
   1998 		ucom_mutex_exit(sc);
   1999 
   2000 		return;
   2001 	}
   2002 
   2003 	if (status != USBD_NORMAL_COMPLETION) {
   2004 		if (status == USBD_STALLED) {
   2005 			usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
   2006 		} else {
   2007 			device_printf(sc->sc_dev,
   2008 			    "ucomreadcb: wonky status=%s\n",
   2009 			    usbd_errstr(status));
   2010 		}
   2011 
   2012 		DPRINTF("... done (status %jd)", status, 0, 0, 0);
   2013 		/* re-adds ub to sc_ibuff_empty */
   2014 		ucomsubmitread(sc, ub);
   2015 		ucom_mutex_exit(sc);
   2016 		return;
   2017 	}
   2018 
   2019 	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
   2020 
   2021 #ifdef UCOM_DEBUG
   2022 	/* This is triggered by uslsa(4) occasionally. */
   2023 	if ((ucomdebug > 0) && (cc == 0)) {
   2024 		device_printf(sc->sc_dev, "ucomreadcb: zero length xfer!\n");
   2025 	}
   2026 #endif
   2027 
   2028 	KDASSERT(cp == ub->ub_data);
   2029 
   2030 	ucom_mutex_exit(sc);
   2031 	if (sc->sc_methods->ucom_read != NULL) {
   2032 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
   2033 		    &cp, &cc);
   2034 		ub->ub_index = (u_int)(cp - ub->ub_data);
   2035 	} else
   2036 		ub->ub_index = 0;
   2037 
   2038 	ub->ub_len = cc;
   2039 
   2040 	rnd_add_uint32(&sc->sc_rndsource, cc);
   2041 
   2042 #ifdef DDB
   2043 	ucom_cncheckmagic(sc, ub);
   2044 #endif
   2045 	ucom_mutex_enter(sc);
   2046 	/*
   2047 	 * Quickly discard input if device is not open,
   2048 	 * except when console is polling.
   2049 	 */
   2050 	if (sc->sc_state != UCOM_OPEN && !ucom_polling(sc)) {
   2051 		/* Go around again - we're not quite ready */
   2052 		/* re-adds ub to sc_ibuff_empty */
   2053 		ucomsubmitread(sc, ub);
   2054 		ucom_mutex_exit(sc);
   2055 		DPRINTF("... done (not open)", 0, 0, 0, 0);
   2056 		return;
   2057 	}
   2058 
   2059 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_full, ub, ub_link);
   2060 
   2061 	if (ucom_polling(sc))
   2062 		ucom_cnread_complete(sc);
   2063 	else
   2064 		ucom_read_complete(sc);
   2065 
   2066 	ucom_mutex_exit(sc);
   2067 
   2068 	DPRINTF("... done", 0, 0, 0, 0);
   2069 }
   2070 
   2071 static void
   2072 ucom_cleanup(struct ucom_softc *sc, int flag)
   2073 {
   2074 	struct tty *tp = sc->sc_tty;
   2075 
   2076 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   2077 
   2078 	DPRINTF("closing pipes", 0, 0, 0, 0);
   2079 
   2080 	/*
   2081 	 * Close the tty and interrupt any pending opens waiting for
   2082 	 * carrier so they restart or give up.  This may flush data.
   2083 	 */
   2084 	(*tp->t_linesw->l_close)(tp, flag);
   2085 	ttyclose(tp);
   2086 
   2087 	/*
   2088 	 * Interrupt any pending xfers and cause them to fail promptly.
   2089 	 * New xfers will only be submitted under the lock after
   2090 	 * sc_closing is cleared.
   2091 	 * If console is attached, we keep the input pipe running
   2092 	 * so that we can catch cnmagic and run DDB.
   2093 	 */
   2094 	if (sc->sc_cn == NULL)
   2095 		usbd_abort_pipe(sc->sc_bulkin_pipe);
   2096 	usbd_abort_pipe(sc->sc_bulkout_pipe);
   2097 
   2098 	/*
   2099 	 * Hang up the phone and start the timer before we can make a
   2100 	 * call again, if necessary.
   2101 	 */
   2102 	ucom_shutdown(sc);
   2103 }
   2104 
   2105 #endif /* NUCOM > 0 */
   2106 
   2107 static bool
   2108 ucom_xfer_done(struct usbd_xfer *xfer)
   2109 {
   2110 	usbd_status err;
   2111 
   2112 	usbd_get_xfer_status(xfer, NULL, NULL, NULL, &err);
   2113 
   2114 	return (err != USBD_NOT_STARTED && err != USBD_IN_PROGRESS);
   2115 }
   2116 
   2117 /*
   2118  * Kernel console input, used when no user process is involved:
   2119  * used by userconf prompt (boot -c), root device prompt (boot -a)
   2120  * or DDB. This code path is not used in single user mode or by getty.
   2121  *
   2122  * ucom console specificities: userconf prompt (boot -c) cannot work
   2123  * as USB devices are not yet attached.
   2124  */
   2125 static int
   2126 ucom_cngetc(dev_t dev)
   2127 {
   2128 	const int unit = UCOMUNIT(dev);
   2129 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   2130 	struct ucom_cnstate * const cn = sc->sc_cn;
   2131 	int c = -1; /* If -1 we are called again in a loop at splhigh */
   2132 
   2133 	if (cn->cn_cons_in == cn->cn_prod_in) {
   2134 		struct ucom_buffer *ub;
   2135 
   2136 		if ((ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty)) != NULL) {
   2137 			if (!ucom_xfer_done(ub->ub_xfer))
   2138 				usbd_dopoll(sc->sc_iface);
   2139 		} else if ((ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full)) != NULL) {
   2140 			/*
   2141 			 * This may happen if we entered DDB while
   2142 			 * executing ucomreadcb(). We can start an
   2143 			 * input transfer for DDB, but if we get there
   2144 			 * because of a breakpoint, we are not likely
   2145 			 * survive exitting DDB.
   2146 			 */
   2147 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
   2148 			ucomsubmitread(sc, ub);
   2149 		} else {
   2150 			panic("no ibuff anywhere");
   2151 		}
   2152 	}
   2153 
   2154 	if (cn->cn_cons_in != cn->cn_prod_in) {
   2155 		c = cn->cn_buf_in[cn->cn_cons_in % sc->sc_ibufsize];
   2156 		cn->cn_cons_in++;
   2157 	}
   2158 
   2159 	return c;
   2160 }
   2161 
   2162 /*
   2163  * Console kernel output, used when no userland process is
   2164  * involved. This is the "green" output on wscons default setup.
   2165  * This code path is not used in single user mode, during
   2166  * userland boot sequence, or by getty.
   2167  *
   2168  * When ucom_cnputc() is called from kprintf(), kprintf_lock is held. Take
   2169  * advantage of this to serialiase the "producer" side of the ucom console
   2170  * ring buffer.
   2171  *
   2172  * Add to the ring buffer if there is space and trigger a soft interrupt
   2173  * handler at IPL_SOFTUSB to initiate USB transfers with the device.
   2174  */
   2175 static void
   2176 ucom_cnputc(dev_t dev, int out)
   2177 {
   2178 	const int unit = UCOMUNIT(dev);
   2179 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   2180 	struct ucom_cnstate * const cn = sc->sc_cn;
   2181 
   2182 	u_int cnprodout = cn->cn_prod_out;
   2183 	u_int cnconsout = atomic_load_relaxed(&cn->cn_cons_out);
   2184 	size_t bufsz = cn->cn_buf_out_size;
   2185 
   2186 	/*
   2187 	 * Check for cn->cn_buf_out rini buffer overflow.
   2188 	 * New character is discared in that case.
   2189 	 */
   2190 	if (cnprodout - cnconsout < bufsz) {
   2191 		cn->cn_buf_out[cnprodout % bufsz] = (u_char)(out & 0xff);
   2192 
   2193 		/*
   2194 		 * ensure the output character write completes before
   2195 		 * incrementing cn->cn_prod_out
   2196 		 */
   2197 		atomic_store_release(&cn->cn_prod_out, cnprodout + 1);
   2198 	}
   2199 
   2200 	if (db_active) {
   2201 		int res;
   2202 
   2203 		/*
   2204 		 * When DDB takes over, output is done synchronously
   2205 		 * by ucom_cnput_bytes().
   2206 		 */
   2207 		do {
   2208 			res = ucom_cnput_bytes(sc);
   2209 		} while (res != 0);
   2210 	} else {
   2211 		/*
   2212 		 * ucom_cnput_bytes() call is deferred to soft
   2213 		 * interrupt context in case current context does not
   2214 		 * allow USB transfer to proceed.
   2215 		 *
   2216 		 * This must be done even if we detected overflow, because
   2217 		 * in that case we still need the output to be flushed.
   2218 		 */
   2219 		kpreempt_disable();
   2220 		softint_schedule(cn->cn_si);
   2221 		kpreempt_enable();
   2222 	}
   2223 }
   2224 
   2225 /*
   2226  * Toggle USB polling, where USB interrupt handler (hard and soft)
   2227  * are called from ucom_cngetc() through usbd_dopoll().
   2228  * This is called by console driver before calling ucom_cngetc().
   2229  */
   2230 static void
   2231 ucom_cnpollc(dev_t dev, int on)
   2232 {
   2233 	const int unit = UCOMUNIT(dev);
   2234 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   2235 	struct ucom_cnstate * const cn = sc->sc_cn;
   2236 	struct usbd_device *udev = sc->sc_udev;
   2237 
   2238 	if (on)
   2239 		cn->cn_polling = true;
   2240 
   2241 	usbd_set_polling(udev, on);
   2242 
   2243 	if (on) {
   2244 		/*
   2245 		 * Poll for regular output completion
   2246 		 */
   2247 		while(!db_active && atomic_load_relaxed(&cn->cn_sending) == 1)
   2248 			usbd_dopoll(sc->sc_iface);
   2249 	} else {
   2250 		cn->cn_polling = false;
   2251 	}
   2252 }
   2253 
   2254 static void
   2255 ucom_cnfree(struct ucom_softc *sc)
   2256 {
   2257 	struct ucom_cnstate * const cn = sc->sc_cn;
   2258 
   2259 	usbd_destroy_xfer(cn->cn_xfer_out);
   2260 
   2261 	callout_halt(&cn->cn_callout, NULL);
   2262 	callout_destroy(&cn->cn_callout);
   2263 
   2264 	softint_disestablish(cn->cn_si);
   2265 
   2266 	if (cn->cn_buf_in)
   2267 		kmem_free(cn->cn_buf_in, sc->sc_ibufsize);
   2268 
   2269 	if (cn->cn_buf_out)
   2270 		kmem_free(cn->cn_buf_out, cn->cn_buf_out_size);
   2271 
   2272 	if (cn->cn_linear_buf_out)
   2273 		kmem_free(cn->cn_linear_buf_out, sc->sc_obufsize);
   2274 
   2275 	kmem_free(cn, sizeof(*cn));
   2276 
   2277 	sc->sc_cn = NULL;
   2278 }
   2279 
   2280 static void
   2281 ucom_cnhalt(dev_t dev)
   2282 {
   2283 	const int unit = UCOMUNIT(dev);
   2284 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   2285 	struct ucom_cnstate * const cn = sc->sc_cn;
   2286 
   2287 	if (cn_tab != &ucom_cnops)
   2288 		return;
   2289 
   2290 	/*
   2291 	 * Make sure we do not get any new output, and wait for
   2292 	 * pending ones to complete
   2293 	 */
   2294 	cn_tab = NULL;
   2295 	while (atomic_load_relaxed(&cn->cn_sending) == 1)
   2296 		kpause("ucomhalt", true, mstohz(1), NULL);
   2297 
   2298 	cn_destroy_magic(&cn->cn_magic_state);
   2299 
   2300 	ucom_cnfree(sc);
   2301 }
   2302 
   2303 #ifdef DDB
   2304 /*
   2305  * This is almost cn_check_magic() but the call to cn_trap() is
   2306  * deferred so that it happens in ucom_softintr().
   2307  */
   2308 static void
   2309 ucom_cncheckmagic(struct ucom_softc *sc, struct ucom_buffer *ub)
   2310 {
   2311 	struct ucom_cnstate *cn = sc->sc_cn;
   2312 	struct cnm_state *s = &cn->cn_magic_state;
   2313 
   2314 	if (cn == NULL || db_active)
   2315 		return;
   2316 
   2317 	for (size_t i = ub->ub_index; i < ub->ub_len; i++) {
   2318 		int _v = s->cnm_magic[s->cnm_state];
   2319 		if (ub->ub_data[i] == CNS_MAGIC_VAL(_v)) {
   2320 			s->cnm_state = CNS_MAGIC_NEXT(_v);
   2321 			if (s->cnm_state == CNS_TERM) {
   2322 				cn->cn_magic_req = true;
   2323 				s->cnm_state = 0;
   2324 
   2325 				kpreempt_disable();
   2326 				softint_schedule(sc->sc_si);
   2327 				kpreempt_enable();
   2328 				break;
   2329 			}
   2330 		} else {
   2331 			s->cnm_state = 0;
   2332 		}
   2333 	}
   2334 }
   2335 #endif /* DDB */
   2336 
   2337 int
   2338 ucomprint(void *aux, const char *pnp)
   2339 {
   2340 	struct ucom_attach_args *ucaa = aux;
   2341 
   2342 	if (pnp)
   2343 		aprint_normal("ucom at %s", pnp);
   2344 	if (ucaa->ucaa_portno != UCOM_UNK_PORTNO)
   2345 		aprint_normal(" portno %d", ucaa->ucaa_portno);
   2346 	return UNCONF;
   2347 }
   2348 
   2349 int
   2350 ucomsubmatch(device_t parent, cfdata_t cf,
   2351 	     const int *ldesc, void *aux)
   2352 {
   2353 	struct ucom_attach_args *ucaa = aux;
   2354 
   2355 	if (ucaa->ucaa_portno != UCOM_UNK_PORTNO &&
   2356 	    cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
   2357 	    cf->cf_loc[UCOMBUSCF_PORTNO] != ucaa->ucaa_portno)
   2358 		return 0;
   2359 	return config_match(parent, cf, aux);
   2360 }
   2361