Home | History | Annotate | Line # | Download | only in usb
ucom.c revision 1.108.2.18
      1 /*	$NetBSD: ucom.c,v 1.108.2.18 2016/10/05 20:55:57 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.18 2016/10/05 20:55:57 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 	mutex_exit(&sc->sc_lock);
    869 	error = ucom_do_ioctl(sc, cmd, data, flag, l);
    870 	mutex_enter(&sc->sc_lock);
    871 	if (--sc->sc_refcnt < 0)
    872 		usb_detach_broadcast(sc->sc_dev, &sc->sc_detachcv);
    873 	mutex_exit(&sc->sc_lock);
    874 	return error;
    875 }
    876 
    877 static int
    878 ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, void *data,
    879 	      int flag, struct lwp *l)
    880 {
    881 	struct tty *tp = sc->sc_tty;
    882 	int error;
    883 
    884 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    885 
    886 	DPRINTF("cmd=0x%08lx", cmd, 0, 0, 0);
    887 
    888 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    889 	if (error != EPASSTHROUGH)
    890 		return error;
    891 
    892 	error = ttioctl(tp, cmd, data, flag, l);
    893 	if (error != EPASSTHROUGH)
    894 		return error;
    895 
    896 	if (sc->sc_methods->ucom_ioctl != NULL) {
    897 		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
    898 			    sc->sc_portno, cmd, data, flag, l->l_proc);
    899 		if (error != EPASSTHROUGH)
    900 			return error;
    901 	}
    902 
    903 	error = 0;
    904 
    905 	DPRINTF("our cmd=0x%08lx", cmd, 0, 0, 0);
    906 	//mutex_enter(&tty_lock);
    907 
    908 	switch (cmd) {
    909 	case TIOCSBRK:
    910 		ucom_break(sc, 1);
    911 		break;
    912 
    913 	case TIOCCBRK:
    914 		ucom_break(sc, 0);
    915 		break;
    916 
    917 	case TIOCSDTR:
    918 		ucom_dtr(sc, 1);
    919 		break;
    920 
    921 	case TIOCCDTR:
    922 		ucom_dtr(sc, 0);
    923 		break;
    924 
    925 	case TIOCGFLAGS:
    926 		mutex_enter(&sc->sc_lock);
    927 		*(int *)data = sc->sc_swflags;
    928 		mutex_exit(&sc->sc_lock);
    929 		break;
    930 
    931 	case TIOCSFLAGS:
    932 		error = kauth_authorize_device_tty(l->l_cred,
    933 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    934 		if (error)
    935 			break;
    936 		mutex_enter(&sc->sc_lock);
    937 		sc->sc_swflags = *(int *)data;
    938 		mutex_exit(&sc->sc_lock);
    939 		break;
    940 
    941 	case TIOCMSET:
    942 	case TIOCMBIS:
    943 	case TIOCMBIC:
    944 		tiocm_to_ucom(sc, cmd, *(int *)data);
    945 		break;
    946 
    947 	case TIOCMGET:
    948 		*(int *)data = ucom_to_tiocm(sc);
    949 		break;
    950 
    951 	case PPS_IOC_CREATE:
    952 	case PPS_IOC_DESTROY:
    953 	case PPS_IOC_GETPARAMS:
    954 	case PPS_IOC_SETPARAMS:
    955 	case PPS_IOC_GETCAP:
    956 	case PPS_IOC_FETCH:
    957 #ifdef PPS_SYNC
    958 	case PPS_IOC_KCBIND:
    959 #endif
    960 		mutex_spin_enter(&timecounter_lock);
    961 		error = pps_ioctl(cmd, data, &sc->sc_pps_state);
    962 		mutex_spin_exit(&timecounter_lock);
    963 		break;
    964 
    965 	default:
    966 		error = EPASSTHROUGH;
    967 		break;
    968 	}
    969 
    970 	//mutex_exit(&tty_lock);
    971 
    972 	return error;
    973 }
    974 
    975 static void
    976 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
    977 {
    978 	u_char combits;
    979 
    980 	combits = 0;
    981 	if (ISSET(ttybits, TIOCM_DTR))
    982 		SET(combits, UMCR_DTR);
    983 	if (ISSET(ttybits, TIOCM_RTS))
    984 		SET(combits, UMCR_RTS);
    985 
    986 	mutex_enter(&sc->sc_lock);
    987 	switch (how) {
    988 	case TIOCMBIC:
    989 		CLR(sc->sc_mcr, combits);
    990 		break;
    991 
    992 	case TIOCMBIS:
    993 		SET(sc->sc_mcr, combits);
    994 		break;
    995 
    996 	case TIOCMSET:
    997 		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
    998 		SET(sc->sc_mcr, combits);
    999 		break;
   1000 	}
   1001 	mutex_exit(&sc->sc_lock);
   1002 
   1003 	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
   1004 		ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
   1005 	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
   1006 		ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
   1007 }
   1008 
   1009 static int
   1010 ucom_to_tiocm(struct ucom_softc *sc)
   1011 {
   1012 	u_char combits;
   1013 	int ttybits = 0;
   1014 
   1015 	mutex_enter(&sc->sc_lock);
   1016 	combits = sc->sc_mcr;
   1017 	if (ISSET(combits, UMCR_DTR))
   1018 		SET(ttybits, TIOCM_DTR);
   1019 	if (ISSET(combits, UMCR_RTS))
   1020 		SET(ttybits, TIOCM_RTS);
   1021 
   1022 	combits = sc->sc_msr;
   1023 	if (ISSET(combits, UMSR_DCD))
   1024 		SET(ttybits, TIOCM_CD);
   1025 	if (ISSET(combits, UMSR_CTS))
   1026 		SET(ttybits, TIOCM_CTS);
   1027 	if (ISSET(combits, UMSR_DSR))
   1028 		SET(ttybits, TIOCM_DSR);
   1029 	if (ISSET(combits, UMSR_RI | UMSR_TERI))
   1030 		SET(ttybits, TIOCM_RI);
   1031 
   1032 #if 0
   1033 XXX;
   1034 	if (sc->sc_ier != 0)
   1035 		SET(ttybits, TIOCM_LE);
   1036 #endif
   1037 	mutex_exit(&sc->sc_lock);
   1038 
   1039 	return ttybits;
   1040 }
   1041 
   1042 static void
   1043 ucom_break(struct ucom_softc *sc, int onoff)
   1044 {
   1045 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1046 
   1047 	DPRINTF("onoff=%d", onoff, 0, 0, 0);
   1048 
   1049 	if (sc->sc_methods->ucom_set != NULL)
   1050 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1051 		    UCOM_SET_BREAK, onoff);
   1052 }
   1053 
   1054 static void
   1055 ucom_dtr(struct ucom_softc *sc, int onoff)
   1056 {
   1057 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1058 
   1059 	DPRINTF("onoff=%d", onoff, 0, 0, 0);
   1060 
   1061 	if (sc->sc_methods->ucom_set != NULL)
   1062 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1063 		    UCOM_SET_DTR, onoff);
   1064 }
   1065 
   1066 static void
   1067 ucom_rts(struct ucom_softc *sc, int onoff)
   1068 {
   1069 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1070 
   1071 	DPRINTF("onoff=%d", onoff, 0, 0, 0);
   1072 
   1073 	if (sc->sc_methods->ucom_set != NULL)
   1074 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1075 		    UCOM_SET_RTS, onoff);
   1076 }
   1077 
   1078 void
   1079 ucom_status_change(struct ucom_softc *sc)
   1080 {
   1081 	struct tty *tp = sc->sc_tty;
   1082 	u_char old_msr;
   1083 
   1084 	if (sc->sc_methods->ucom_get_status != NULL) {
   1085 		old_msr = sc->sc_msr;
   1086 		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
   1087 		    &sc->sc_lsr, &sc->sc_msr);
   1088 		if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD)) {
   1089 			mutex_spin_enter(&timecounter_lock);
   1090 			pps_capture(&sc->sc_pps_state);
   1091 			pps_event(&sc->sc_pps_state,
   1092 			    (sc->sc_msr & UMSR_DCD) ?
   1093 			    PPS_CAPTUREASSERT :
   1094 			    PPS_CAPTURECLEAR);
   1095 			mutex_spin_exit(&timecounter_lock);
   1096 
   1097 			(*tp->t_linesw->l_modem)(tp,
   1098 			    ISSET(sc->sc_msr, UMSR_DCD));
   1099 		}
   1100 	} else {
   1101 		sc->sc_lsr = 0;
   1102 		/* Assume DCD is present, if we have no chance to check it. */
   1103 		sc->sc_msr = UMSR_DCD;
   1104 	}
   1105 }
   1106 
   1107 static int
   1108 ucomparam(struct tty *tp, struct termios *t)
   1109 {
   1110 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
   1111 	    UCOMUNIT(tp->t_dev));
   1112 	int error;
   1113 
   1114 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1115 
   1116 	if (sc == NULL || sc->sc_dying)
   1117 		return EIO;
   1118 
   1119 	/* Check requested parameters. */
   1120 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
   1121 		return EINVAL;
   1122 
   1123 	/*
   1124 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1125 	 * is always active.
   1126 	 */
   1127 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
   1128 		SET(t->c_cflag, CLOCAL);
   1129 		CLR(t->c_cflag, HUPCL);
   1130 	}
   1131 
   1132 	/*
   1133 	 * If there were no changes, don't do anything.  This avoids dropping
   1134 	 * input and improves performance when all we did was frob things like
   1135 	 * VMIN and VTIME.
   1136 	 */
   1137 	if (tp->t_ospeed == t->c_ospeed &&
   1138 	    tp->t_cflag == t->c_cflag)
   1139 		return 0;
   1140 
   1141 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
   1142 
   1143 	/* And copy to tty. */
   1144 	tp->t_ispeed = 0;
   1145 	tp->t_ospeed = t->c_ospeed;
   1146 	tp->t_cflag = t->c_cflag;
   1147 
   1148 	if (sc->sc_methods->ucom_param != NULL) {
   1149 		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
   1150 			    t);
   1151 		if (error)
   1152 			return error;
   1153 	}
   1154 
   1155 	/* XXX worry about CHWFLOW */
   1156 
   1157 	/*
   1158 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1159 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1160 	 * explicit request.
   1161 	 */
   1162 	DPRINTF("l_modem", 0, 0, 0, 0);
   1163 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
   1164 
   1165 #if 0
   1166 XXX what if the hardware is not open
   1167 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1168 		if (sc->sc_tx_stopped) {
   1169 			sc->sc_tx_stopped = 0;
   1170 			ucomstart(tp);
   1171 		}
   1172 	}
   1173 #endif
   1174 
   1175 	return 0;
   1176 }
   1177 
   1178 static int
   1179 ucomhwiflow(struct tty *tp, int block)
   1180 {
   1181 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
   1182 	    UCOMUNIT(tp->t_dev));
   1183 	int old;
   1184 
   1185 	if (sc == NULL)
   1186 		return 0;
   1187 
   1188 	mutex_enter(&sc->sc_lock);
   1189 	old = sc->sc_rx_stopped;
   1190 	sc->sc_rx_stopped = (u_char)block;
   1191 
   1192 	if (old && !block) {
   1193 		sc->sc_rx_unblock = 1;
   1194 		softint_schedule(sc->sc_si);
   1195 	}
   1196 	mutex_exit(&sc->sc_lock);
   1197 
   1198 	return 1;
   1199 }
   1200 
   1201 static void
   1202 ucomstart(struct tty *tp)
   1203 {
   1204 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
   1205 	    UCOMUNIT(tp->t_dev));
   1206 	struct ucom_buffer *ub;
   1207 	u_char *data;
   1208 	int cnt;
   1209 
   1210 	if (sc == NULL)
   1211 		return;
   1212 
   1213 	KASSERT(&sc->sc_lock);
   1214 	KASSERT(mutex_owned(&tty_lock));
   1215 	if (sc->sc_dying) {
   1216 		return;
   1217 	}
   1218 
   1219 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1220 		goto out;
   1221 	if (sc->sc_tx_stopped)
   1222 		goto out;
   1223 
   1224 	if (!ttypull(tp))
   1225 		goto out;
   1226 
   1227 	/* Grab the first contiguous region of buffer space. */
   1228 	data = tp->t_outq.c_cf;
   1229 	cnt = ndqb(&tp->t_outq, 0);
   1230 
   1231 	if (cnt == 0)
   1232 		goto out;
   1233 
   1234 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_free);
   1235 	if (ub == NULL) {
   1236 		SET(tp->t_state, TS_BUSY);
   1237 		goto out;
   1238 	}
   1239 
   1240 	SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_free, ub_link);
   1241 
   1242 	if (SIMPLEQ_FIRST(&sc->sc_obuff_free) == NULL)
   1243 		SET(tp->t_state, TS_BUSY);
   1244 
   1245 	if (cnt > sc->sc_obufsize)
   1246 		cnt = sc->sc_obufsize;
   1247 
   1248 	if (sc->sc_methods->ucom_write != NULL)
   1249 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
   1250 					   ub->ub_data, data, &cnt);
   1251 	else
   1252 		memcpy(ub->ub_data, data, cnt);
   1253 
   1254 	ub->ub_len = cnt;
   1255 	ub->ub_index = 0;
   1256 
   1257 	SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_full, ub, ub_link);
   1258 
   1259 	softint_schedule(sc->sc_si);
   1260 
   1261  out:
   1262 	return;
   1263 }
   1264 
   1265 void
   1266 ucomstop(struct tty *tp, int flag)
   1267 {
   1268 #if 0
   1269 	struct ucom_softc *sc =
   1270 	    device_lookup_private(&ucom_cd, UCOMUNIT(tp->t_dev));
   1271 
   1272 	mutex_enter(&sc->sc_lock);
   1273 	mutex_spin_enter(&tty_lock);
   1274 	if (ISSET(tp->t_state, TS_BUSY)) {
   1275 		/* obuff_full -> obuff_free? */
   1276 		/* sc->sc_tx_stopped = 1; */
   1277 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1278 			SET(tp->t_state, TS_FLUSH);
   1279 	}
   1280 	mutex_spin_exit(&tty_lock);
   1281 	mutex_exit(&sc->sc_lock);
   1282 #endif
   1283 }
   1284 
   1285 static void
   1286 ucom_write_status(struct ucom_softc *sc, struct ucom_buffer *ub,
   1287     usbd_status err)
   1288 {
   1289 	struct tty *tp = sc->sc_tty;
   1290 	uint32_t cc = ub->ub_len;
   1291 
   1292 	KASSERT(mutex_owned(&sc->sc_lock));
   1293 
   1294 	switch (err) {
   1295 	case USBD_IN_PROGRESS:
   1296 		ub->ub_index = ub->ub_len;
   1297 		break;
   1298 	case USBD_STALLED:
   1299 		ub->ub_index = 0;
   1300 		softint_schedule(sc->sc_si);
   1301 		break;
   1302 	case USBD_NORMAL_COMPLETION:
   1303 		usbd_get_xfer_status(ub->ub_xfer, NULL, NULL, &cc, NULL);
   1304 		rnd_add_uint32(&sc->sc_rndsource, cc);
   1305 		/*FALLTHROUGH*/
   1306 	default:
   1307 		SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_full, ub_link);
   1308 		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
   1309 		cc -= sc->sc_opkthdrlen;
   1310 
   1311 		mutex_spin_enter(&tty_lock);
   1312 		CLR(tp->t_state, TS_BUSY);
   1313 		if (ISSET(tp->t_state, TS_FLUSH))
   1314 			CLR(tp->t_state, TS_FLUSH);
   1315 		else
   1316 			ndflush(&tp->t_outq, cc);
   1317 		mutex_spin_exit(&tty_lock);
   1318 
   1319 		if (err != USBD_CANCELLED && err != USBD_IOERROR &&
   1320 		    !sc->sc_dying) {
   1321 			if ((ub = SIMPLEQ_FIRST(&sc->sc_obuff_full)) != NULL)
   1322 				ucom_submit_write(sc, ub);
   1323 
   1324 			mutex_spin_enter(&tty_lock);
   1325 			(*tp->t_linesw->l_start)(tp);
   1326 			mutex_spin_exit(&tty_lock);
   1327 		}
   1328 		break;
   1329 	}
   1330 }
   1331 
   1332 static void
   1333 ucom_submit_write(struct ucom_softc *sc, struct ucom_buffer *ub)
   1334 {
   1335 
   1336 	KASSERT(mutex_owned(&sc->sc_lock));
   1337 
   1338 	usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, ub->ub_len,
   1339 	    0, USBD_NO_TIMEOUT, ucomwritecb);
   1340 
   1341 	ucom_write_status(sc, ub, usbd_transfer(ub->ub_xfer));
   1342 }
   1343 
   1344 static void
   1345 ucomwritecb(struct usbd_xfer *xfer, void *p, usbd_status status)
   1346 {
   1347 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1348 
   1349 	mutex_enter(&sc->sc_lock);
   1350 	ucom_write_status(sc, SIMPLEQ_FIRST(&sc->sc_obuff_full), status);
   1351 	mutex_exit(&sc->sc_lock);
   1352 
   1353 }
   1354 
   1355 static void
   1356 ucom_softintr(void *arg)
   1357 {
   1358 	struct ucom_softc *sc = arg;
   1359 	struct tty *tp = sc->sc_tty;
   1360 	struct ucom_buffer *ub;
   1361 
   1362 	mutex_enter(&sc->sc_lock);
   1363 	mutex_enter(&tty_lock);
   1364 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
   1365 		mutex_exit(&tty_lock);
   1366 		mutex_exit(&sc->sc_lock);
   1367 		return;
   1368 	}
   1369 	mutex_exit(&tty_lock);
   1370 
   1371 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_full);
   1372 
   1373 	if (ub != NULL && ub->ub_index == 0)
   1374 		ucom_submit_write(sc, ub);
   1375 
   1376 	if (sc->sc_rx_unblock)
   1377 		ucom_read_complete(sc);
   1378 
   1379 	mutex_exit(&sc->sc_lock);
   1380 }
   1381 
   1382 static void
   1383 ucom_read_complete(struct ucom_softc *sc)
   1384 {
   1385 	int (*rint)(int, struct tty *);
   1386 	struct ucom_buffer *ub;
   1387 	struct tty *tp;
   1388 
   1389 	KASSERT(mutex_owned(&sc->sc_lock));
   1390 
   1391 	tp = sc->sc_tty;
   1392 	rint = tp->t_linesw->l_rint;
   1393 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1394 
   1395 	while (ub != NULL && !sc->sc_rx_stopped) {
   1396 
   1397 		/* XXX ttyinput takes tty_lock */
   1398 		while (ub->ub_index < ub->ub_len && !sc->sc_rx_stopped) {
   1399 			/* Give characters to tty layer. */
   1400 			if ((*rint)(ub->ub_data[ub->ub_index], tp) == -1) {
   1401 				/* Overflow: drop remainder */
   1402 				ub->ub_index = ub->ub_len;
   1403 			} else
   1404 				ub->ub_index++;
   1405 		}
   1406 
   1407 		if (ub->ub_index == ub->ub_len) {
   1408 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
   1409 
   1410 			ucomsubmitread(sc, ub);
   1411 
   1412 			ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1413 		}
   1414 	}
   1415 
   1416 	sc->sc_rx_unblock = (ub != NULL);
   1417 }
   1418 
   1419 static usbd_status
   1420 ucomsubmitread(struct ucom_softc *sc, struct ucom_buffer *ub)
   1421 {
   1422 	usbd_status err;
   1423 
   1424 	if (sc->sc_bulkin_no != -1) {
   1425 		usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, sc->sc_ibufsize,
   1426 		    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ucomreadcb);
   1427 
   1428 		if ((err = usbd_transfer(ub->ub_xfer)) != USBD_IN_PROGRESS) {
   1429 			/* XXX: Recover from this, please! */
   1430 			printf("%s: err=%s\n", __func__, usbd_errstr(err));
   1431 			return err;
   1432 		}
   1433 	}
   1434 
   1435 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_empty, ub, ub_link);
   1436 
   1437 	return USBD_NORMAL_COMPLETION;
   1438 }
   1439 
   1440 void
   1441 ucomreadcb(struct usbd_xfer *xfer, void *p, usbd_status status)
   1442 {
   1443 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1444 	struct tty *tp = sc->sc_tty;
   1445 	struct ucom_buffer *ub;
   1446 	uint32_t cc;
   1447 	u_char *cp;
   1448 
   1449 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1450 
   1451 	if (status == USBD_CANCELLED)
   1452 		return;
   1453 
   1454 	mutex_enter(&sc->sc_lock);
   1455 	if (status == USBD_IOERROR ||
   1456 	    sc->sc_dying) {
   1457 		DPRINTF("dying", 0, 0, 0, 0);
   1458 		/* Send something to wake upper layer */
   1459 		(tp->t_linesw->l_rint)('\n', tp);
   1460 		mutex_spin_enter(&tty_lock);	/* XXX */
   1461 		ttwakeup(tp);
   1462 		mutex_spin_exit(&tty_lock);	/* XXX */
   1463 		mutex_exit(&sc->sc_lock);
   1464 		return;
   1465 	}
   1466 
   1467 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty);
   1468 	SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_empty, ub_link);
   1469 
   1470 	if (status == USBD_STALLED) {
   1471 		if (sc->sc_bulkin_no != -1)
   1472 			usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
   1473 		if (sc->sc_ipipe)
   1474 			usbd_clear_endpoint_stall_async(sc->sc_ipipe);
   1475 		ucomsubmitread(sc, ub);
   1476 		mutex_exit(&sc->sc_lock);
   1477 		return;
   1478 	}
   1479 
   1480 	if (status != USBD_NORMAL_COMPLETION) {
   1481 		printf("ucomreadcb: wonky status=%s\n", usbd_errstr(status));
   1482 		mutex_exit(&sc->sc_lock);
   1483 		return;
   1484 	}
   1485 
   1486 	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
   1487 
   1488 #ifdef UCOM_DEBUG
   1489 	/* This is triggered by uslsa(4) occasionally. */
   1490 	if ((ucomdebug > 0) && (cc == 0)) {
   1491 		device_printf(sc->sc_dev, "ucomreadcb: zero length xfer!\n");
   1492 	}
   1493 #endif
   1494 
   1495 	KDASSERT(cp == ub->ub_data);
   1496 
   1497 	rnd_add_uint32(&sc->sc_rndsource, cc);
   1498 
   1499 	if (sc->sc_opening) {
   1500 		ucomsubmitread(sc, ub);
   1501 		mutex_exit(&sc->sc_lock);
   1502 		return;
   1503 	}
   1504 
   1505 	ub->ub_data = usbd_get_buffer(xfer);
   1506 	if (sc->sc_methods->ucom_read != NULL) {
   1507 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
   1508 		    &cp, &cc);
   1509 		ub->ub_index = (u_int)(cp - ub->ub_data);
   1510 	} else
   1511 		ub->ub_index = 0;
   1512 
   1513 	ub->ub_len = cc;
   1514 
   1515 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_full, ub, ub_link);
   1516 
   1517 	ucom_read_complete(sc);
   1518 	mutex_exit(&sc->sc_lock);
   1519 }
   1520 
   1521 static void
   1522 ucom_cleanup(struct ucom_softc *sc)
   1523 {
   1524 
   1525 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1526 
   1527 	DPRINTF("aborting pipes", 0, 0, 0, 0);
   1528 
   1529 	KASSERT(mutex_owned(&sc->sc_lock));
   1530 
   1531 	ucom_shutdown(sc);
   1532 	if (sc->sc_bulkin_pipe != NULL) {
   1533 		usbd_abort_pipe(sc->sc_bulkin_pipe);
   1534 	}
   1535 	if (sc->sc_bulkout_pipe != NULL) {
   1536 		usbd_abort_pipe(sc->sc_bulkout_pipe);
   1537 	}
   1538 }
   1539 
   1540 #endif /* NUCOM > 0 */
   1541 
   1542 int
   1543 ucomprint(void *aux, const char *pnp)
   1544 {
   1545 	struct ucom_attach_args *ucaa = aux;
   1546 
   1547 	if (pnp)
   1548 		aprint_normal("ucom at %s", pnp);
   1549 	if (ucaa->ucaa_portno != UCOM_UNK_PORTNO)
   1550 		aprint_normal(" portno %d", ucaa->ucaa_portno);
   1551 	return UNCONF;
   1552 }
   1553 
   1554 int
   1555 ucomsubmatch(device_t parent, cfdata_t cf,
   1556 	     const int *ldesc, void *aux)
   1557 {
   1558 	struct ucom_attach_args *ucaa = aux;
   1559 
   1560 	if (ucaa->ucaa_portno != UCOM_UNK_PORTNO &&
   1561 	    cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
   1562 	    cf->cf_loc[UCOMBUSCF_PORTNO] != ucaa->ucaa_portno)
   1563 		return 0;
   1564 	return config_match(parent, cf, aux);
   1565 }
   1566