LCOV - code coverage report
Current view: top level - dev/usb - ucom.c (source / functions) Hit Total Coverage
Test: 6.4 Lines: 0 570 0.0 %
Date: 2018-10-19 03:25:38 Functions: 0 31 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13