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