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