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