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