Home | History | Annotate | Line # | Download | only in usb
ucom.c revision 1.74.6.2
      1 /*	$NetBSD: ucom.c,v 1.74.6.2 2009/01/17 13:29:09 mjf 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.74.6.2 2009/01/17 13:29:09 mjf 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/kauth.h>
     52 #if defined(__NetBSD__)
     53 #include "rnd.h"
     54 #if NRND > 0
     55 #include <sys/rnd.h>
     56 #endif
     57 #endif
     58 
     59 #include <dev/usb/usb.h>
     60 
     61 #include <dev/usb/usbdi.h>
     62 #include <dev/usb/usbdi_util.h>
     63 #include <dev/usb/usbdevs.h>
     64 #include <dev/usb/usb_quirks.h>
     65 
     66 #include <dev/usb/ucomvar.h>
     67 
     68 #include "ucom.h"
     69 
     70 #include "locators.h"
     71 
     72 #if NUCOM > 0
     73 
     74 #ifdef UCOM_DEBUG
     75 #define DPRINTFN(n, x)	if (ucomdebug > (n)) logprintf x
     76 int ucomdebug = 0;
     77 #else
     78 #define DPRINTFN(n, x)
     79 #endif
     80 #define DPRINTF(x) DPRINTFN(0, x)
     81 
     82 #define	UCOMUNIT_MASK		0x3ffff
     83 #define	UCOMDIALOUT_MASK	0x80000
     84 #define	UCOMCALLUNIT_MASK	0x40000
     85 
     86 #define	UCOMUNIT(x)		(minor(x) & UCOMUNIT_MASK)
     87 #define	UCOMDIALOUT(x)		(minor(x) & UCOMDIALOUT_MASK)
     88 #define	UCOMCALLUNIT(x)		(minor(x) & UCOMCALLUNIT_MASK)
     89 
     90 struct ucom_softc {
     91 	USBBASEDEVICE		sc_dev;		/* base device */
     92 
     93 	usbd_device_handle	sc_udev;	/* USB device */
     94 
     95 	usbd_interface_handle	sc_iface;	/* data interface */
     96 
     97 	int			sc_bulkin_no;	/* bulk in endpoint address */
     98 	usbd_pipe_handle	sc_bulkin_pipe;	/* bulk in pipe */
     99 	usbd_xfer_handle	sc_ixfer;	/* read request */
    100 	u_char			*sc_ibuf;	/* read buffer */
    101 	u_int			sc_ibufsize;	/* read buffer size */
    102 	u_int			sc_ibufsizepad;	/* read buffer size padded */
    103 
    104 	int			sc_bulkout_no;	/* bulk out endpoint address */
    105 	usbd_pipe_handle	sc_bulkout_pipe;/* bulk out pipe */
    106 	usbd_xfer_handle	sc_oxfer;	/* write request */
    107 	u_char			*sc_obuf;	/* write buffer */
    108 	u_int			sc_obufsize;	/* write buffer size */
    109 	u_int			sc_opkthdrlen;	/* header length of
    110 						 * output packet */
    111 
    112 	struct ucom_methods     *sc_methods;
    113 	void                    *sc_parent;
    114 	int			sc_portno;
    115 
    116 	struct tty		*sc_tty;	/* our tty */
    117 	u_char			sc_lsr;
    118 	u_char			sc_msr;
    119 	u_char			sc_mcr;
    120 	u_char			sc_tx_stopped;
    121 	int			sc_swflags;
    122 
    123 	u_char			sc_opening;	/* lock during open */
    124 	int			sc_refcnt;
    125 	u_char			sc_dying;	/* disconnecting */
    126 
    127 #if defined(__NetBSD__) && NRND > 0
    128 	rndsource_element_t	sc_rndsource;	/* random source */
    129 #endif
    130 };
    131 
    132 dev_type_open(ucomopen);
    133 dev_type_close(ucomclose);
    134 dev_type_read(ucomread);
    135 dev_type_write(ucomwrite);
    136 dev_type_ioctl(ucomioctl);
    137 dev_type_stop(ucomstop);
    138 dev_type_tty(ucomtty);
    139 dev_type_poll(ucompoll);
    140 
    141 const struct cdevsw ucom_cdevsw = {
    142 	ucomopen, ucomclose, ucomread, ucomwrite, ucomioctl,
    143 	ucomstop, ucomtty, ucompoll, nommap, ttykqfilter, D_TTY
    144 };
    145 
    146 Static void	ucom_cleanup(struct ucom_softc *);
    147 Static void	ucom_hwiflow(struct ucom_softc *);
    148 Static int	ucomparam(struct tty *, struct termios *);
    149 Static void	ucomstart(struct tty *);
    150 Static void	ucom_shutdown(struct ucom_softc *);
    151 Static int	ucom_do_ioctl(struct ucom_softc *, u_long, void *,
    152 			      int, struct lwp *);
    153 Static void	ucom_dtr(struct ucom_softc *, int);
    154 Static void	ucom_rts(struct ucom_softc *, int);
    155 Static void	ucom_break(struct ucom_softc *, int);
    156 Static usbd_status ucomstartread(struct ucom_softc *);
    157 Static void	ucomreadcb(usbd_xfer_handle, usbd_private_handle, usbd_status);
    158 Static void	ucomwritecb(usbd_xfer_handle, usbd_private_handle, usbd_status);
    159 Static void	tiocm_to_ucom(struct ucom_softc *, u_long, int);
    160 Static int	ucom_to_tiocm(struct ucom_softc *);
    161 
    162 USB_DECLARE_DRIVER(ucom);
    163 
    164 USB_MATCH(ucom)
    165 {
    166 	return (1);
    167 }
    168 
    169 USB_ATTACH(ucom)
    170 {
    171 	struct ucom_softc *sc = device_private(self);
    172 	struct ucom_attach_args *uca = aux;
    173 	struct tty *tp;
    174 
    175 	if (uca->info != NULL)
    176 		printf(": %s", uca->info);
    177 	printf("\n");
    178 
    179 	sc->sc_dev = self;
    180 	sc->sc_udev = uca->device;
    181 	sc->sc_iface = uca->iface;
    182 	sc->sc_bulkout_no = uca->bulkout;
    183 	sc->sc_bulkin_no = uca->bulkin;
    184 	sc->sc_ibufsize = uca->ibufsize;
    185 	sc->sc_ibufsizepad = uca->ibufsizepad;
    186 	sc->sc_obufsize = uca->obufsize;
    187 	sc->sc_opkthdrlen = uca->opkthdrlen;
    188 	sc->sc_methods = uca->methods;
    189 	sc->sc_parent = uca->arg;
    190 	sc->sc_portno = uca->portno;
    191 
    192 	tp = ttymalloc();
    193 	tp->t_oproc = ucomstart;
    194 	tp->t_param = ucomparam;
    195 	sc->sc_tty = tp;
    196 
    197 	DPRINTF(("ucom_attach: tty_attach %p\n", tp));
    198 	tty_attach(tp);
    199 
    200 #if defined(__NetBSD__) && NRND > 0
    201 	rnd_attach_source(&sc->sc_rndsource, USBDEVNAME(sc->sc_dev),
    202 			  RND_TYPE_TTY, 0);
    203 #endif
    204 
    205 	if (!pmf_device_register(self, NULL, NULL))
    206 		aprint_error_dev(self, "couldn't establish power handler\n");
    207 	USB_ATTACH_SUCCESS_RETURN;
    208 }
    209 
    210 USB_DETACH(ucom)
    211 {
    212 	struct ucom_softc *sc = device_private(self);
    213 	struct tty *tp = sc->sc_tty;
    214 	int maj, mn;
    215 	int s;
    216 
    217 	DPRINTF(("ucom_detach: sc=%p flags=%d tp=%p, pipe=%d,%d\n",
    218 		 sc, flags, tp, sc->sc_bulkin_no, sc->sc_bulkout_no));
    219 
    220 	sc->sc_dying = 1;
    221 	pmf_device_deregister(self);
    222 
    223 	if (sc->sc_bulkin_pipe != NULL)
    224 		usbd_abort_pipe(sc->sc_bulkin_pipe);
    225 	if (sc->sc_bulkout_pipe != NULL)
    226 		usbd_abort_pipe(sc->sc_bulkout_pipe);
    227 
    228 	s = splusb();
    229 	if (--sc->sc_refcnt >= 0) {
    230 		/* Wake up anyone waiting */
    231 		if (tp != NULL) {
    232 			mutex_spin_enter(&tty_lock);
    233 			CLR(tp->t_state, TS_CARR_ON);
    234 			CLR(tp->t_cflag, CLOCAL | MDMBUF);
    235 			ttyflush(tp, FREAD|FWRITE);
    236 			mutex_spin_exit(&tty_lock);
    237 		}
    238 		/* Wait for processes to go away. */
    239 		usb_detach_wait(USBDEV(sc->sc_dev));
    240 	}
    241 	splx(s);
    242 
    243 	/* locate the major number */
    244 	maj = cdevsw_lookup_major(&ucom_cdevsw);
    245 
    246 	/* Nuke the vnodes for any open instances. */
    247 	mn = device_unit(self);
    248 	DPRINTF(("ucom_detach: maj=%d mn=%d\n", maj, mn));
    249 	vdevgone(maj, mn, mn, VCHR);
    250 	vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR);
    251 	vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR);
    252 
    253 	/* Detach and free the tty. */
    254 	if (tp != NULL) {
    255 		tty_detach(tp);
    256 		ttyfree(tp);
    257 		sc->sc_tty = NULL;
    258 	}
    259 
    260 	/* Detach the random source */
    261 #if defined(__NetBSD__) && NRND > 0
    262 	rnd_detach_source(&sc->sc_rndsource);
    263 #endif
    264 
    265 	return (0);
    266 }
    267 
    268 int
    269 ucom_activate(device_ptr_t self, enum devact act)
    270 {
    271 	struct ucom_softc *sc = device_private(self);
    272 
    273 	DPRINTFN(5,("ucom_activate: %d\n", act));
    274 
    275 	switch (act) {
    276 	case DVACT_ACTIVATE:
    277 		return (EOPNOTSUPP);
    278 
    279 	case DVACT_DEACTIVATE:
    280 		sc->sc_dying = 1;
    281 		break;
    282 	}
    283 	return (0);
    284 }
    285 
    286 void
    287 ucom_shutdown(struct ucom_softc *sc)
    288 {
    289 	struct tty *tp = sc->sc_tty;
    290 
    291 	DPRINTF(("ucom_shutdown\n"));
    292 	/*
    293 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    294 	 * notice even if we immediately open the port again.
    295 	 */
    296 	if (ISSET(tp->t_cflag, HUPCL)) {
    297 		ucom_dtr(sc, 0);
    298 		(void)tsleep(sc, TTIPRI, ttclos, hz);
    299 	}
    300 }
    301 
    302 int
    303 ucomopen(dev_t dev, int flag, int mode, struct lwp *l)
    304 {
    305 	int unit = UCOMUNIT(dev);
    306 	usbd_status err;
    307 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit);
    308 	struct tty *tp;
    309 	int s;
    310 	int error;
    311 
    312 	if (sc == NULL)
    313 		return (ENXIO);
    314 
    315 	if (sc->sc_dying)
    316 		return (EIO);
    317 
    318 	if (!device_is_active(sc->sc_dev))
    319 		return (ENXIO);
    320 
    321 	tp = sc->sc_tty;
    322 
    323 	DPRINTF(("ucomopen: unit=%d, tp=%p\n", unit, tp));
    324 
    325 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    326 		return (EBUSY);
    327 
    328 	s = spltty();
    329 
    330 	/*
    331 	 * Do the following iff this is a first open.
    332 	 */
    333 	while (sc->sc_opening)
    334 		tsleep(&sc->sc_opening, PRIBIO, "ucomop", 0);
    335 
    336 	if (sc->sc_dying) {
    337 		splx(s);
    338 		return (EIO);
    339 	}
    340 	sc->sc_opening = 1;
    341 
    342 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    343 		struct termios t;
    344 
    345 		tp->t_dev = dev;
    346 
    347 		if (sc->sc_methods->ucom_open != NULL) {
    348 			error = sc->sc_methods->ucom_open(sc->sc_parent,
    349 							  sc->sc_portno);
    350 			if (error) {
    351 				ucom_cleanup(sc);
    352 				sc->sc_opening = 0;
    353 				wakeup(&sc->sc_opening);
    354 				splx(s);
    355 				return (error);
    356 			}
    357 		}
    358 
    359 		ucom_status_change(sc);
    360 
    361 		/*
    362 		 * Initialize the termios status to the defaults.  Add in the
    363 		 * sticky bits from TIOCSFLAGS.
    364 		 */
    365 		t.c_ispeed = 0;
    366 		t.c_ospeed = TTYDEF_SPEED;
    367 		t.c_cflag = TTYDEF_CFLAG;
    368 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    369 			SET(t.c_cflag, CLOCAL);
    370 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    371 			SET(t.c_cflag, CRTSCTS);
    372 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    373 			SET(t.c_cflag, MDMBUF);
    374 		/* Make sure ucomparam() will do something. */
    375 		tp->t_ospeed = 0;
    376 		(void) ucomparam(tp, &t);
    377 		tp->t_iflag = TTYDEF_IFLAG;
    378 		tp->t_oflag = TTYDEF_OFLAG;
    379 		tp->t_lflag = TTYDEF_LFLAG;
    380 		ttychars(tp);
    381 		ttsetwater(tp);
    382 
    383 		/*
    384 		 * Turn on DTR.  We must always do this, even if carrier is not
    385 		 * present, because otherwise we'd have to use TIOCSDTR
    386 		 * immediately after setting CLOCAL, which applications do not
    387 		 * expect.  We always assert DTR while the device is open
    388 		 * unless explicitly requested to deassert it.  Ditto RTS.
    389 		 */
    390 		ucom_dtr(sc, 1);
    391 		ucom_rts(sc, 1);
    392 
    393 		/* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/
    394 		ucom_hwiflow(sc);
    395 
    396 		DPRINTF(("ucomopen: open pipes in=%d out=%d\n",
    397 			 sc->sc_bulkin_no, sc->sc_bulkout_no));
    398 
    399 		/* Open the bulk pipes */
    400 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 0,
    401 				     &sc->sc_bulkin_pipe);
    402 		if (err) {
    403 			DPRINTF(("%s: open bulk in error (addr %d), err=%s\n",
    404 				 USBDEVNAME(sc->sc_dev), sc->sc_bulkin_no,
    405 				 usbd_errstr(err)));
    406 			error = EIO;
    407 			goto fail_0;
    408 		}
    409 		err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
    410 				     USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
    411 		if (err) {
    412 			DPRINTF(("%s: open bulk out error (addr %d), err=%s\n",
    413 				 USBDEVNAME(sc->sc_dev), sc->sc_bulkout_no,
    414 				 usbd_errstr(err)));
    415 			error = EIO;
    416 			goto fail_1;
    417 		}
    418 
    419 		/* Allocate a request and an input buffer and start reading. */
    420 		sc->sc_ixfer = usbd_alloc_xfer(sc->sc_udev);
    421 		if (sc->sc_ixfer == NULL) {
    422 			error = ENOMEM;
    423 			goto fail_2;
    424 		}
    425 
    426 		sc->sc_ibuf = usbd_alloc_buffer(sc->sc_ixfer,
    427 						sc->sc_ibufsizepad);
    428 		if (sc->sc_ibuf == NULL) {
    429 			error = ENOMEM;
    430 			goto fail_3;
    431 		}
    432 
    433 		sc->sc_oxfer = usbd_alloc_xfer(sc->sc_udev);
    434 		if (sc->sc_oxfer == NULL) {
    435 			error = ENOMEM;
    436 			goto fail_3;
    437 		}
    438 
    439 		sc->sc_obuf = usbd_alloc_buffer(sc->sc_oxfer,
    440 						sc->sc_obufsize +
    441 						sc->sc_opkthdrlen);
    442 		if (sc->sc_obuf == NULL) {
    443 			error = ENOMEM;
    444 			goto fail_4;
    445 		}
    446 
    447 		ucomstartread(sc);
    448 	}
    449 	sc->sc_opening = 0;
    450 	wakeup(&sc->sc_opening);
    451 	splx(s);
    452 
    453 	error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    454 	if (error)
    455 		goto bad;
    456 
    457 	error = (*tp->t_linesw->l_open)(dev, tp);
    458 	if (error)
    459 		goto bad;
    460 
    461 	return (0);
    462 
    463 fail_4:
    464 	usbd_free_xfer(sc->sc_oxfer);
    465 	sc->sc_oxfer = NULL;
    466 fail_3:
    467 	usbd_free_xfer(sc->sc_ixfer);
    468 	sc->sc_ixfer = NULL;
    469 fail_2:
    470 	usbd_close_pipe(sc->sc_bulkout_pipe);
    471 	sc->sc_bulkout_pipe = NULL;
    472 fail_1:
    473 	usbd_close_pipe(sc->sc_bulkin_pipe);
    474 	sc->sc_bulkin_pipe = NULL;
    475 fail_0:
    476 	sc->sc_opening = 0;
    477 	wakeup(&sc->sc_opening);
    478 	splx(s);
    479 	return (error);
    480 
    481 bad:
    482 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    483 		/*
    484 		 * We failed to open the device, and nobody else had it opened.
    485 		 * Clean up the state as appropriate.
    486 		 */
    487 		ucom_cleanup(sc);
    488 	}
    489 
    490 	return (error);
    491 }
    492 
    493 int
    494 ucomclose(dev_t dev, int flag, int mode, struct lwp *l)
    495 {
    496 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    497 	struct tty *tp = sc->sc_tty;
    498 
    499 	DPRINTF(("ucomclose: unit=%"PRId64"\n", UCOMUNIT(dev)));
    500 	if (!ISSET(tp->t_state, TS_ISOPEN))
    501 		return (0);
    502 
    503 	sc->sc_refcnt++;
    504 
    505 	(*tp->t_linesw->l_close)(tp, flag);
    506 	ttyclose(tp);
    507 
    508 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    509 		/*
    510 		 * Although we got a last close, the device may still be in
    511 		 * use; e.g. if this was the dialout node, and there are still
    512 		 * processes waiting for carrier on the non-dialout node.
    513 		 */
    514 		ucom_cleanup(sc);
    515 	}
    516 
    517 	if (sc->sc_methods->ucom_close != NULL)
    518 		sc->sc_methods->ucom_close(sc->sc_parent, sc->sc_portno);
    519 
    520 	if (--sc->sc_refcnt < 0)
    521 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    522 
    523 	return (0);
    524 }
    525 
    526 int
    527 ucomread(dev_t dev, struct uio *uio, int flag)
    528 {
    529 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    530 	struct tty *tp = sc->sc_tty;
    531 	int error;
    532 
    533 	if (sc->sc_dying)
    534 		return (EIO);
    535 
    536 	sc->sc_refcnt++;
    537 	error = ((*tp->t_linesw->l_read)(tp, uio, flag));
    538 	if (--sc->sc_refcnt < 0)
    539 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    540 	return (error);
    541 }
    542 
    543 int
    544 ucomwrite(dev_t dev, struct uio *uio, int flag)
    545 {
    546 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    547 	struct tty *tp = sc->sc_tty;
    548 	int error;
    549 
    550 	if (sc->sc_dying)
    551 		return (EIO);
    552 
    553 	sc->sc_refcnt++;
    554 	error = ((*tp->t_linesw->l_write)(tp, uio, flag));
    555 	if (--sc->sc_refcnt < 0)
    556 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    557 	return (error);
    558 }
    559 
    560 int
    561 ucompoll(dev_t dev, int events, struct lwp *l)
    562 {
    563 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    564 	struct tty *tp = sc->sc_tty;
    565 	int revents;
    566 
    567 	if (sc->sc_dying)
    568 		return (POLLHUP);
    569 
    570 	sc->sc_refcnt++;
    571 	revents = ((*tp->t_linesw->l_poll)(tp, events, l));
    572 	if (--sc->sc_refcnt < 0)
    573 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    574 	return (revents);
    575 }
    576 
    577 struct tty *
    578 ucomtty(dev_t dev)
    579 {
    580 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    581 	struct tty *tp = sc->sc_tty;
    582 
    583 	return (tp);
    584 }
    585 
    586 int
    587 ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    588 {
    589 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, UCOMUNIT(dev));
    590 	int error;
    591 
    592 	sc->sc_refcnt++;
    593 	error = ucom_do_ioctl(sc, cmd, data, flag, l);
    594 	if (--sc->sc_refcnt < 0)
    595 		usb_detach_wakeup(USBDEV(sc->sc_dev));
    596 	return (error);
    597 }
    598 
    599 Static int
    600 ucom_do_ioctl(struct ucom_softc *sc, u_long cmd, void *data,
    601 	      int flag, struct lwp *l)
    602 {
    603 	struct tty *tp = sc->sc_tty;
    604 	int error;
    605 	int s;
    606 
    607 	if (sc->sc_dying)
    608 		return (EIO);
    609 
    610 	DPRINTF(("ucomioctl: cmd=0x%08lx\n", cmd));
    611 
    612 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    613 	if (error != EPASSTHROUGH)
    614 		return (error);
    615 
    616 	error = ttioctl(tp, cmd, data, flag, l);
    617 	if (error != EPASSTHROUGH)
    618 		return (error);
    619 
    620 	if (sc->sc_methods->ucom_ioctl != NULL) {
    621 		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
    622 			    sc->sc_portno, cmd, data, flag, l->l_proc);
    623 		if (error != EPASSTHROUGH)
    624 			return (error);
    625 	}
    626 
    627 	error = 0;
    628 
    629 	DPRINTF(("ucomioctl: our cmd=0x%08lx\n", cmd));
    630 	s = spltty();
    631 
    632 	switch (cmd) {
    633 	case TIOCSBRK:
    634 		ucom_break(sc, 1);
    635 		break;
    636 
    637 	case TIOCCBRK:
    638 		ucom_break(sc, 0);
    639 		break;
    640 
    641 	case TIOCSDTR:
    642 		ucom_dtr(sc, 1);
    643 		break;
    644 
    645 	case TIOCCDTR:
    646 		ucom_dtr(sc, 0);
    647 		break;
    648 
    649 	case TIOCGFLAGS:
    650 		*(int *)data = sc->sc_swflags;
    651 		break;
    652 
    653 	case TIOCSFLAGS:
    654 		error = kauth_authorize_device_tty(l->l_cred,
    655 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    656 		if (error)
    657 			break;
    658 		sc->sc_swflags = *(int *)data;
    659 		break;
    660 
    661 	case TIOCMSET:
    662 	case TIOCMBIS:
    663 	case TIOCMBIC:
    664 		tiocm_to_ucom(sc, cmd, *(int *)data);
    665 		break;
    666 
    667 	case TIOCMGET:
    668 		*(int *)data = ucom_to_tiocm(sc);
    669 		break;
    670 
    671 	default:
    672 		error = EPASSTHROUGH;
    673 		break;
    674 	}
    675 
    676 	splx(s);
    677 
    678 	return (error);
    679 }
    680 
    681 Static void
    682 tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
    683 {
    684 	u_char combits;
    685 
    686 	combits = 0;
    687 	if (ISSET(ttybits, TIOCM_DTR))
    688 		SET(combits, UMCR_DTR);
    689 	if (ISSET(ttybits, TIOCM_RTS))
    690 		SET(combits, UMCR_RTS);
    691 
    692 	switch (how) {
    693 	case TIOCMBIC:
    694 		CLR(sc->sc_mcr, combits);
    695 		break;
    696 
    697 	case TIOCMBIS:
    698 		SET(sc->sc_mcr, combits);
    699 		break;
    700 
    701 	case TIOCMSET:
    702 		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
    703 		SET(sc->sc_mcr, combits);
    704 		break;
    705 	}
    706 
    707 	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
    708 		ucom_dtr(sc, (sc->sc_mcr & UMCR_DTR) != 0);
    709 	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
    710 		ucom_rts(sc, (sc->sc_mcr & UMCR_RTS) != 0);
    711 }
    712 
    713 Static int
    714 ucom_to_tiocm(struct ucom_softc *sc)
    715 {
    716 	u_char combits;
    717 	int ttybits = 0;
    718 
    719 	combits = sc->sc_mcr;
    720 	if (ISSET(combits, UMCR_DTR))
    721 		SET(ttybits, TIOCM_DTR);
    722 	if (ISSET(combits, UMCR_RTS))
    723 		SET(ttybits, TIOCM_RTS);
    724 
    725 	combits = sc->sc_msr;
    726 	if (ISSET(combits, UMSR_DCD))
    727 		SET(ttybits, TIOCM_CD);
    728 	if (ISSET(combits, UMSR_CTS))
    729 		SET(ttybits, TIOCM_CTS);
    730 	if (ISSET(combits, UMSR_DSR))
    731 		SET(ttybits, TIOCM_DSR);
    732 	if (ISSET(combits, UMSR_RI | UMSR_TERI))
    733 		SET(ttybits, TIOCM_RI);
    734 
    735 #if 0
    736 XXX;
    737 	if (sc->sc_ier != 0)
    738 		SET(ttybits, TIOCM_LE);
    739 #endif
    740 
    741 	return (ttybits);
    742 }
    743 
    744 Static void
    745 ucom_break(struct ucom_softc *sc, int onoff)
    746 {
    747 	DPRINTF(("ucom_break: onoff=%d\n", onoff));
    748 
    749 	if (sc->sc_methods->ucom_set != NULL)
    750 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    751 		    UCOM_SET_BREAK, onoff);
    752 }
    753 
    754 Static void
    755 ucom_dtr(struct ucom_softc *sc, int onoff)
    756 {
    757 	DPRINTF(("ucom_dtr: onoff=%d\n", onoff));
    758 
    759 	if (sc->sc_methods->ucom_set != NULL)
    760 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    761 		    UCOM_SET_DTR, onoff);
    762 }
    763 
    764 Static void
    765 ucom_rts(struct ucom_softc *sc, int onoff)
    766 {
    767 	DPRINTF(("ucom_rts: onoff=%d\n", onoff));
    768 
    769 	if (sc->sc_methods->ucom_set != NULL)
    770 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
    771 		    UCOM_SET_RTS, onoff);
    772 }
    773 
    774 void
    775 ucom_status_change(struct ucom_softc *sc)
    776 {
    777 	struct tty *tp = sc->sc_tty;
    778 	u_char old_msr;
    779 
    780 	if (sc->sc_methods->ucom_get_status != NULL) {
    781 		old_msr = sc->sc_msr;
    782 		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
    783 		    &sc->sc_lsr, &sc->sc_msr);
    784 		if (ISSET((sc->sc_msr ^ old_msr), UMSR_DCD))
    785 			(*tp->t_linesw->l_modem)(tp,
    786 			    ISSET(sc->sc_msr, UMSR_DCD));
    787 	} else {
    788 		sc->sc_lsr = 0;
    789 		/* Assume DCD is present, if we have no chance to check it. */
    790 		sc->sc_msr = UMSR_DCD;
    791 	}
    792 }
    793 
    794 Static int
    795 ucomparam(struct tty *tp, struct termios *t)
    796 {
    797 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
    798 	    UCOMUNIT(tp->t_dev));
    799 	int error;
    800 
    801 	if (sc->sc_dying)
    802 		return (EIO);
    803 
    804 	/* Check requested parameters. */
    805 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    806 		return (EINVAL);
    807 
    808 	/*
    809 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    810 	 * is always active.
    811 	 */
    812 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
    813 		SET(t->c_cflag, CLOCAL);
    814 		CLR(t->c_cflag, HUPCL);
    815 	}
    816 
    817 	/*
    818 	 * If there were no changes, don't do anything.  This avoids dropping
    819 	 * input and improves performance when all we did was frob things like
    820 	 * VMIN and VTIME.
    821 	 */
    822 	if (tp->t_ospeed == t->c_ospeed &&
    823 	    tp->t_cflag == t->c_cflag)
    824 		return (0);
    825 
    826 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
    827 
    828 	/* And copy to tty. */
    829 	tp->t_ispeed = 0;
    830 	tp->t_ospeed = t->c_ospeed;
    831 	tp->t_cflag = t->c_cflag;
    832 
    833 	if (sc->sc_methods->ucom_param != NULL) {
    834 		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
    835 			    t);
    836 		if (error)
    837 			return (error);
    838 	}
    839 
    840 	/* XXX worry about CHWFLOW */
    841 
    842 	/*
    843 	 * Update the tty layer's idea of the carrier bit, in case we changed
    844 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
    845 	 * explicit request.
    846 	 */
    847 	DPRINTF(("ucomparam: l_modem\n"));
    848 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
    849 
    850 #if 0
    851 XXX what if the hardware is not open
    852 	if (!ISSET(t->c_cflag, CHWFLOW)) {
    853 		if (sc->sc_tx_stopped) {
    854 			sc->sc_tx_stopped = 0;
    855 			ucomstart(tp);
    856 		}
    857 	}
    858 #endif
    859 
    860 	return (0);
    861 }
    862 
    863 /*
    864  * (un)block input via hw flowcontrol
    865  */
    866 Static void
    867 ucom_hwiflow(struct ucom_softc *sc)
    868 {
    869 	DPRINTF(("ucom_hwiflow:\n"));
    870 #if 0
    871 XXX
    872 	bus_space_tag_t iot = sc->sc_iot;
    873 	bus_space_handle_t ioh = sc->sc_ioh;
    874 
    875 	if (sc->sc_mcr_rts == 0)
    876 		return;
    877 
    878 	if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
    879 		CLR(sc->sc_mcr, sc->sc_mcr_rts);
    880 		CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
    881 	} else {
    882 		SET(sc->sc_mcr, sc->sc_mcr_rts);
    883 		SET(sc->sc_mcr_active, sc->sc_mcr_rts);
    884 	}
    885 	bus_space_write_1(iot, ioh, com_mcr, sc->sc_mcr_active);
    886 #endif
    887 }
    888 
    889 Static void
    890 ucomstart(struct tty *tp)
    891 {
    892 	struct ucom_softc *sc = device_lookup_private(&ucom_cd,
    893 	    UCOMUNIT(tp->t_dev));
    894 	usbd_status err;
    895 	int s;
    896 	u_char *data;
    897 	int cnt;
    898 
    899 	if (sc->sc_dying)
    900 		return;
    901 
    902 	s = spltty();
    903 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
    904 		DPRINTFN(4,("ucomstart: no go, state=0x%x\n", tp->t_state));
    905 		goto out;
    906 	}
    907 	if (sc->sc_tx_stopped)
    908 		goto out;
    909 
    910 	if (!ttypull(tp))
    911 		goto out;
    912 
    913 	/* Grab the first contiguous region of buffer space. */
    914 	data = tp->t_outq.c_cf;
    915 	cnt = ndqb(&tp->t_outq, 0);
    916 
    917 	if (cnt == 0) {
    918 		DPRINTF(("ucomstart: cnt==0\n"));
    919 		goto out;
    920 	}
    921 
    922 	SET(tp->t_state, TS_BUSY);
    923 
    924 	if (cnt > sc->sc_obufsize) {
    925 		DPRINTF(("ucomstart: big buffer %d chars\n", cnt));
    926 		cnt = sc->sc_obufsize;
    927 	}
    928 	if (sc->sc_methods->ucom_write != NULL)
    929 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
    930 					   sc->sc_obuf, data, &cnt);
    931 	else
    932 		memcpy(sc->sc_obuf, data, cnt);
    933 
    934 	DPRINTFN(4,("ucomstart: %d chars\n", cnt));
    935 	usbd_setup_xfer(sc->sc_oxfer, sc->sc_bulkout_pipe,
    936 			(usbd_private_handle)sc, sc->sc_obuf, cnt,
    937 			USBD_NO_COPY, USBD_NO_TIMEOUT, ucomwritecb);
    938 	/* What can we do on error? */
    939 	err = usbd_transfer(sc->sc_oxfer);
    940 #ifdef DIAGNOSTIC
    941 	if (err != USBD_IN_PROGRESS)
    942 		printf("ucomstart: err=%s\n", usbd_errstr(err));
    943 #endif
    944 
    945 out:
    946 	splx(s);
    947 }
    948 
    949 void
    950 ucomstop(struct tty *tp, int flag)
    951 {
    952 	DPRINTF(("ucomstop: flag=%d\n", flag));
    953 #if 0
    954 	/*struct ucom_softc *sc =
    955 	    device_lookup_private(&ucom_cd, UCOMUNIT(dev));*/
    956 	int s;
    957 
    958 	s = spltty();
    959 	if (ISSET(tp->t_state, TS_BUSY)) {
    960 		DPRINTF(("ucomstop: XXX\n"));
    961 		/* sc->sc_tx_stopped = 1; */
    962 		if (!ISSET(tp->t_state, TS_TTSTOP))
    963 			SET(tp->t_state, TS_FLUSH);
    964 	}
    965 	splx(s);
    966 #endif
    967 }
    968 
    969 Static void
    970 ucomwritecb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
    971 {
    972 	struct ucom_softc *sc = (struct ucom_softc *)p;
    973 	struct tty *tp = sc->sc_tty;
    974 	u_int32_t cc;
    975 	int s;
    976 
    977 	DPRINTFN(5,("ucomwritecb: status=%d\n", status));
    978 
    979 	if (status == USBD_CANCELLED || sc->sc_dying)
    980 		goto error;
    981 
    982 	if (status) {
    983 		DPRINTF(("ucomwritecb: status=%d\n", status));
    984 		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
    985 		/* XXX we should restart after some delay. */
    986 		goto error;
    987 	}
    988 
    989 	usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
    990 #if defined(__NetBSD__) && NRND > 0
    991 	rnd_add_uint32(&sc->sc_rndsource, cc);
    992 #endif
    993 	DPRINTFN(5,("ucomwritecb: cc=%d\n", cc));
    994 	/* convert from USB bytes to tty bytes */
    995 	cc -= sc->sc_opkthdrlen;
    996 
    997 	s = spltty();
    998 	CLR(tp->t_state, TS_BUSY);
    999 	if (ISSET(tp->t_state, TS_FLUSH))
   1000 		CLR(tp->t_state, TS_FLUSH);
   1001 	else
   1002 		ndflush(&tp->t_outq, cc);
   1003 	(*tp->t_linesw->l_start)(tp);
   1004 	splx(s);
   1005 	return;
   1006 
   1007 error:
   1008 	s = spltty();
   1009 	CLR(tp->t_state, TS_BUSY);
   1010 	splx(s);
   1011 }
   1012 
   1013 Static usbd_status
   1014 ucomstartread(struct ucom_softc *sc)
   1015 {
   1016 	usbd_status err;
   1017 
   1018 	DPRINTFN(5,("ucomstartread: start\n"));
   1019 	usbd_setup_xfer(sc->sc_ixfer, sc->sc_bulkin_pipe,
   1020 			(usbd_private_handle)sc,
   1021 			sc->sc_ibuf, sc->sc_ibufsize,
   1022 			USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1023 			USBD_NO_TIMEOUT, ucomreadcb);
   1024 	err = usbd_transfer(sc->sc_ixfer);
   1025 	if (err != USBD_IN_PROGRESS) {
   1026 		DPRINTF(("ucomstartread: err=%s\n", usbd_errstr(err)));
   1027 		return (err);
   1028 	}
   1029 	return (USBD_NORMAL_COMPLETION);
   1030 }
   1031 
   1032 Static void
   1033 ucomreadcb(usbd_xfer_handle xfer, usbd_private_handle p, usbd_status status)
   1034 {
   1035 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1036 	struct tty *tp = sc->sc_tty;
   1037 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
   1038 	usbd_status err;
   1039 	u_int32_t cc;
   1040 	u_char *cp;
   1041 	int s;
   1042 
   1043 	DPRINTFN(5,("ucomreadcb: status=%d\n", status));
   1044 
   1045 	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
   1046 	    sc->sc_dying) {
   1047 		DPRINTF(("ucomreadcb: dying\n"));
   1048 		/* Send something to wake upper layer */
   1049 		s = spltty();
   1050 		(*rint)('\n', tp);
   1051 		mutex_spin_enter(&tty_lock);	/* XXX */
   1052 		ttwakeup(tp);
   1053 		mutex_spin_exit(&tty_lock);	/* XXX */
   1054 		splx(s);
   1055 		return;
   1056 	}
   1057 
   1058 	if (status) {
   1059 		usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
   1060 		/* XXX we should restart after some delay. */
   1061 		return;
   1062 	}
   1063 
   1064 	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
   1065 #if defined(__NetBSD__) && NRND > 0
   1066 	rnd_add_uint32(&sc->sc_rndsource, cc);
   1067 #endif
   1068 	DPRINTFN(5,("ucomreadcb: got %d chars, tp=%p\n", cc, tp));
   1069 	if (sc->sc_methods->ucom_read != NULL)
   1070 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
   1071 					  &cp, &cc);
   1072 
   1073 	s = spltty();
   1074 	/* Give characters to tty layer. */
   1075 	while (cc-- > 0) {
   1076 		DPRINTFN(7,("ucomreadcb: char=0x%02x\n", *cp));
   1077 		if ((*rint)(*cp++, tp) == -1) {
   1078 			/* XXX what should we do? */
   1079 			aprint_error_dev(sc->sc_dev, "lost %d chars\n",
   1080 			       cc);
   1081 			break;
   1082 		}
   1083 	}
   1084 	splx(s);
   1085 
   1086 	err = ucomstartread(sc);
   1087 	if (err) {
   1088 		aprint_error_dev(sc->sc_dev, "read start failed\n");
   1089 		/* XXX what should we dow now? */
   1090 	}
   1091 }
   1092 
   1093 Static void
   1094 ucom_cleanup(struct ucom_softc *sc)
   1095 {
   1096 	DPRINTF(("ucom_cleanup: closing pipes\n"));
   1097 
   1098 	ucom_shutdown(sc);
   1099 	if (sc->sc_bulkin_pipe != NULL) {
   1100 		usbd_abort_pipe(sc->sc_bulkin_pipe);
   1101 		usbd_close_pipe(sc->sc_bulkin_pipe);
   1102 		sc->sc_bulkin_pipe = NULL;
   1103 	}
   1104 	if (sc->sc_bulkout_pipe != NULL) {
   1105 		usbd_abort_pipe(sc->sc_bulkout_pipe);
   1106 		usbd_close_pipe(sc->sc_bulkout_pipe);
   1107 		sc->sc_bulkout_pipe = NULL;
   1108 	}
   1109 	if (sc->sc_ixfer != NULL) {
   1110 		usbd_free_xfer(sc->sc_ixfer);
   1111 		sc->sc_ixfer = NULL;
   1112 	}
   1113 	if (sc->sc_oxfer != NULL) {
   1114 		usbd_free_xfer(sc->sc_oxfer);
   1115 		sc->sc_oxfer = NULL;
   1116 	}
   1117 }
   1118 
   1119 #endif /* NUCOM > 0 */
   1120 
   1121 int
   1122 ucomprint(void *aux, const char *pnp)
   1123 {
   1124 	struct ucom_attach_args *uca = aux;
   1125 
   1126 	if (pnp)
   1127 		aprint_normal("ucom at %s", pnp);
   1128 	if (uca->portno != UCOM_UNK_PORTNO)
   1129 		aprint_normal(" portno %d", uca->portno);
   1130 	return (UNCONF);
   1131 }
   1132 
   1133 int
   1134 ucomsubmatch(device_t parent, cfdata_t cf,
   1135 	     const int *ldesc, void *aux)
   1136 {
   1137 	struct ucom_attach_args *uca = aux;
   1138 
   1139 	if (uca->portno != UCOM_UNK_PORTNO &&
   1140 	    cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
   1141 	    cf->cf_loc[UCOMBUSCF_PORTNO] != uca->portno)
   1142 		return (0);
   1143 	return (config_match(parent, cf, aux));
   1144 }
   1145