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