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