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