Home | History | Annotate | Line # | Download | only in usb
ucom.c revision 1.141
      1  1.141     skrll /*	$NetBSD: ucom.c,v 1.141 2025/09/30 19:10:17 skrll Exp $	*/
      2    1.1  augustss 
      3    1.1  augustss /*
      4   1.22  augustss  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5    1.1  augustss  * All rights reserved.
      6    1.1  augustss  *
      7    1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8   1.23  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9    1.1  augustss  * Carlstedt Research & Technology.
     10    1.1  augustss  *
     11    1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12    1.1  augustss  * modification, are permitted provided that the following conditions
     13    1.1  augustss  * are met:
     14    1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15    1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16    1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17    1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18    1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19    1.1  augustss  *
     20    1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21    1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22    1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23    1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24    1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25    1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26    1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27    1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28    1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29    1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30    1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31    1.1  augustss  */
     32   1.22  augustss /*
     33   1.22  augustss  * This code is very heavily based on the 16550 driver, com.c.
     34   1.22  augustss  */
     35   1.40     lukem 
     36   1.40     lukem #include <sys/cdefs.h>
     37  1.141     skrll __KERNEL_RCSID(0, "$NetBSD: ucom.c,v 1.141 2025/09/30 19:10:17 skrll Exp $");
     38  1.115     skrll 
     39  1.115     skrll #ifdef _KERNEL_OPT
     40  1.139  jakllsch #include "opt_ntp.h"
     41  1.115     skrll #include "opt_usb.h"
     42  1.115     skrll #endif
     43    1.1  augustss 
     44    1.1  augustss #include <sys/param.h>
     45    1.1  augustss #include <sys/systm.h>
     46    1.1  augustss #include <sys/kernel.h>
     47    1.1  augustss #include <sys/ioctl.h>
     48    1.3  augustss #include <sys/conf.h>
     49    1.1  augustss #include <sys/tty.h>
     50    1.1  augustss #include <sys/file.h>
     51    1.1  augustss #include <sys/select.h>
     52    1.1  augustss #include <sys/proc.h>
     53    1.1  augustss #include <sys/vnode.h>
     54   1.12  augustss #include <sys/device.h>
     55    1.1  augustss #include <sys/poll.h>
     56   1.82    martin #include <sys/queue.h>
     57   1.63      elad #include <sys/kauth.h>
     58  1.111     skrll #include <sys/sysctl.h>
     59  1.106       gdt #include <sys/timepps.h>
     60  1.109  riastrad #include <sys/rndsource.h>
     61    1.1  augustss 
     62    1.1  augustss #include <dev/usb/usb.h>
     63    1.1  augustss 
     64    1.1  augustss #include <dev/usb/usbdi.h>
     65    1.1  augustss #include <dev/usb/usbdi_util.h>
     66    1.1  augustss #include <dev/usb/usbdevs.h>
     67    1.1  augustss #include <dev/usb/usb_quirks.h>
     68  1.111     skrll #include <dev/usb/usbhist.h>
     69    1.1  augustss 
     70   1.12  augustss #include <dev/usb/ucomvar.h>
     71   1.12  augustss 
     72   1.13  augustss #include "ucom.h"
     73   1.53  drochner #include "locators.h"
     74  1.124       mrg #include "ioconf.h"
     75   1.53  drochner 
     76   1.13  augustss #if NUCOM > 0
     77   1.13  augustss 
     78  1.111     skrll #ifdef USB_DEBUG
     79  1.111     skrll #ifndef UCOM_DEBUG
     80  1.111     skrll #define ucomdebug 0
     81  1.111     skrll #else
     82   1.12  augustss int ucomdebug = 0;
     83  1.111     skrll 
     84  1.111     skrll SYSCTL_SETUP(sysctl_hw_ucom_setup, "sysctl hw.ucom setup")
     85  1.111     skrll {
     86  1.117     skrll 	int err;
     87  1.117     skrll 	const struct sysctlnode *rnode;
     88  1.117     skrll 	const struct sysctlnode *cnode;
     89  1.117     skrll 
     90  1.117     skrll 	err = sysctl_createv(clog, 0, NULL, &rnode,
     91  1.117     skrll 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "ucom",
     92  1.117     skrll 	    SYSCTL_DESCR("ucom global controls"),
     93  1.117     skrll 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
     94  1.117     skrll 
     95  1.117     skrll 	if (err)
     96  1.117     skrll 		goto fail;
     97  1.117     skrll 
     98  1.117     skrll 	/* control debugging printfs */
     99  1.117     skrll 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    100  1.117     skrll 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    101  1.117     skrll 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    102  1.117     skrll 	    NULL, 0, &ucomdebug, sizeof(ucomdebug), CTL_CREATE, CTL_EOL);
    103  1.117     skrll 	if (err)
    104  1.117     skrll 		goto fail;
    105  1.111     skrll 
    106  1.117     skrll 	return;
    107  1.111     skrll fail:
    108  1.117     skrll 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    109  1.111     skrll }
    110  1.111     skrll 
    111  1.111     skrll #endif /* UCOM_DEBUG */
    112  1.111     skrll #endif /* USB_DEBUG */
    113  1.111     skrll 
    114  1.141     skrll #define DPRINTF(FMT,A,B,C,D)	USBHIST_LOGN(ucomdebug,1,FMT,A,B,C,D)
    115  1.141     skrll #define DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(ucomdebug,N,FMT,A,B,C,D)
    116  1.141     skrll #define UCOMHIST_FUNC()		USBHIST_FUNC()
    117  1.141     skrll #define UCOMHIST_CALLED(name)	USBHIST_CALLED(ucomdebug)
    118   1.12  augustss 
    119  1.108  christos #define	UCOMCALLUNIT_MASK	TTCALLUNIT_MASK
    120  1.108  christos #define	UCOMUNIT_MASK		TTUNIT_MASK
    121  1.108  christos #define	UCOMDIALOUT_MASK	TTDIALOUT_MASK
    122  1.108  christos 
    123  1.108  christos #define	UCOMCALLUNIT(x)		TTCALLUNIT(x)
    124  1.108  christos #define	UCOMUNIT(x)		TTUNIT(x)
    125  1.108  christos #define	UCOMDIALOUT(x)		TTDIALOUT(x)
    126  1.132  riastrad #define	ucom_unit		tty_unit
    127   1.12  augustss 
    128   1.82    martin /*
    129   1.82    martin  * XXX: We can submit multiple input/output buffers to the usb stack
    130   1.82    martin  * to improve throughput, but the usb stack is too lame to deal with this
    131   1.82    martin  * in a number of places.
    132   1.82    martin  */
    133   1.82    martin #define	UCOM_IN_BUFFS	1
    134   1.82    martin #define	UCOM_OUT_BUFFS	1
    135   1.82    martin 
    136   1.82    martin struct ucom_buffer {
    137   1.82    martin 	SIMPLEQ_ENTRY(ucom_buffer) ub_link;
    138  1.111     skrll 	struct usbd_xfer *ub_xfer;
    139   1.82    martin 	u_char *ub_data;
    140   1.82    martin 	u_int ub_len;
    141   1.82    martin 	u_int ub_index;
    142   1.82    martin };
    143   1.82    martin 
    144    1.1  augustss struct ucom_softc {
    145   1.85    dyoung 	device_t		sc_dev;		/* base device */
    146   1.12  augustss 
    147  1.111     skrll 	struct usbd_device *	sc_udev;	/* USB device */
    148  1.111     skrll 	struct usbd_interface *	sc_iface;	/* data interface */
    149   1.12  augustss 
    150   1.12  augustss 	int			sc_bulkin_no;	/* bulk in endpoint address */
    151  1.115     skrll 	struct usbd_pipe *	sc_bulkin_pipe;/* bulk in pipe */
    152   1.19  augustss 	u_int			sc_ibufsize;	/* read buffer size */
    153   1.22  augustss 	u_int			sc_ibufsizepad;	/* read buffer size padded */
    154   1.82    martin 	struct ucom_buffer	sc_ibuff[UCOM_IN_BUFFS];
    155   1.82    martin 	SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_empty;
    156   1.82    martin 	SIMPLEQ_HEAD(, ucom_buffer) sc_ibuff_full;
    157   1.12  augustss 
    158   1.12  augustss 	int			sc_bulkout_no;	/* bulk out endpoint address */
    159  1.111     skrll 	struct usbd_pipe *	sc_bulkout_pipe;/* bulk out pipe */
    160   1.19  augustss 	u_int			sc_obufsize;	/* write buffer size */
    161   1.82    martin 	u_int			sc_opkthdrlen;	/* header length of */
    162   1.82    martin 	struct ucom_buffer	sc_obuff[UCOM_OUT_BUFFS];
    163   1.82    martin 	SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_free;
    164   1.82    martin 	SIMPLEQ_HEAD(, ucom_buffer) sc_obuff_full;
    165   1.82    martin 
    166   1.82    martin 	void			*sc_si;
    167   1.12  augustss 
    168   1.90  jakllsch 	const struct ucom_methods *sc_methods;
    169   1.12  augustss 	void                    *sc_parent;
    170   1.12  augustss 	int			sc_portno;
    171   1.12  augustss 
    172   1.12  augustss 	struct tty		*sc_tty;	/* our tty */
    173   1.12  augustss 	u_char			sc_lsr;
    174   1.12  augustss 	u_char			sc_msr;
    175   1.12  augustss 	u_char			sc_mcr;
    176   1.82    martin 	volatile u_char		sc_rx_stopped;
    177   1.82    martin 	u_char			sc_rx_unblock;
    178   1.12  augustss 	u_char			sc_tx_stopped;
    179   1.12  augustss 	int			sc_swflags;
    180   1.12  augustss 
    181  1.115     skrll 	enum ucom_state {
    182  1.115     skrll 	    UCOM_DEAD,
    183  1.115     skrll 	    UCOM_ATTACHED,
    184  1.115     skrll 	    UCOM_OPENING,
    185  1.115     skrll 	    UCOM_OPEN
    186  1.115     skrll 	}			sc_state;
    187  1.130  riastrad 	bool			sc_closing;	/* software is closing */
    188  1.130  riastrad 	bool			sc_dying;	/* hardware is gone */
    189   1.31  explorer 
    190  1.106       gdt 	struct pps_state	sc_pps_state;	/* pps state */
    191  1.106       gdt 
    192  1.111     skrll 	krndsource_t		sc_rndsource;	/* random source */
    193  1.111     skrll 
    194  1.111     skrll 	kmutex_t		sc_lock;
    195  1.115     skrll 	kcondvar_t		sc_statecv;
    196  1.130  riastrad 
    197  1.130  riastrad 	struct timeval		sc_hup_time;
    198    1.1  augustss };
    199    1.1  augustss 
    200   1.44   gehenna dev_type_open(ucomopen);
    201  1.130  riastrad dev_type_cancel(ucomcancel);
    202   1.44   gehenna dev_type_close(ucomclose);
    203   1.44   gehenna dev_type_read(ucomread);
    204   1.44   gehenna dev_type_write(ucomwrite);
    205   1.44   gehenna dev_type_ioctl(ucomioctl);
    206   1.44   gehenna dev_type_stop(ucomstop);
    207   1.44   gehenna dev_type_tty(ucomtty);
    208   1.44   gehenna dev_type_poll(ucompoll);
    209   1.44   gehenna 
    210   1.44   gehenna const struct cdevsw ucom_cdevsw = {
    211  1.103  dholland 	.d_open = ucomopen,
    212  1.130  riastrad 	.d_cancel = ucomcancel,
    213  1.103  dholland 	.d_close = ucomclose,
    214  1.103  dholland 	.d_read = ucomread,
    215  1.103  dholland 	.d_write = ucomwrite,
    216  1.103  dholland 	.d_ioctl = ucomioctl,
    217  1.103  dholland 	.d_stop = ucomstop,
    218  1.103  dholland 	.d_tty = ucomtty,
    219  1.103  dholland 	.d_poll = ucompoll,
    220  1.103  dholland 	.d_mmap = nommap,
    221  1.103  dholland 	.d_kqfilter = ttykqfilter,
    222  1.105  dholland 	.d_discard = nodiscard,
    223  1.130  riastrad 	.d_cfdriver = &ucom_cd,
    224  1.131  riastrad 	.d_devtounit = ucom_unit,
    225  1.111     skrll 	.d_flag = D_TTY | D_MPSAFE
    226   1.44   gehenna };
    227   1.12  augustss 
    228  1.130  riastrad static void	ucom_cleanup(struct ucom_softc *, int);
    229   1.82    martin static int	ucomparam(struct tty *, struct termios *);
    230   1.82    martin static int	ucomhwiflow(struct tty *, int);
    231   1.82    martin static void	ucomstart(struct tty *);
    232   1.82    martin static void	ucom_shutdown(struct ucom_softc *);
    233   1.82    martin static void	ucom_dtr(struct ucom_softc *, int);
    234   1.82    martin static void	ucom_rts(struct ucom_softc *, int);
    235   1.82    martin static void	ucom_break(struct ucom_softc *, int);
    236   1.82    martin static void	tiocm_to_ucom(struct ucom_softc *, u_long, int);
    237   1.82    martin static int	ucom_to_tiocm(struct ucom_softc *);
    238   1.82    martin 
    239  1.111     skrll static void	ucomreadcb(struct usbd_xfer *, void *, usbd_status);
    240   1.82    martin static void	ucom_submit_write(struct ucom_softc *, struct ucom_buffer *);
    241   1.82    martin static void	ucom_write_status(struct ucom_softc *, struct ucom_buffer *,
    242  1.115     skrll 		    usbd_status);
    243   1.82    martin 
    244  1.111     skrll static void	ucomwritecb(struct usbd_xfer *, void *, usbd_status);
    245   1.82    martin static void	ucom_read_complete(struct ucom_softc *);
    246  1.115     skrll static int	ucomsubmitread(struct ucom_softc *, struct ucom_buffer *);
    247   1.82    martin static void	ucom_softintr(void *);
    248    1.1  augustss 
    249   1.85    dyoung int ucom_match(device_t, cfdata_t, void *);
    250   1.85    dyoung void ucom_attach(device_t, device_t, void *);
    251   1.85    dyoung int ucom_detach(device_t, int);
    252  1.124       mrg 
    253   1.85    dyoung CFATTACH_DECL_NEW(ucom, sizeof(struct ucom_softc), ucom_match, ucom_attach,
    254  1.125       mrg     ucom_detach, NULL);
    255    1.1  augustss 
    256   1.85    dyoung int
    257   1.85    dyoung ucom_match(device_t parent, cfdata_t match, void *aux)
    258    1.1  augustss {
    259  1.111     skrll 	return 1;
    260    1.1  augustss }
    261    1.1  augustss 
    262   1.85    dyoung void
    263   1.85    dyoung ucom_attach(device_t parent, device_t self, void *aux)
    264    1.1  augustss {
    265   1.77      cube 	struct ucom_softc *sc = device_private(self);
    266  1.111     skrll 	struct ucom_attach_args *ucaa = aux;
    267   1.12  augustss 
    268  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    269  1.111     skrll 
    270  1.111     skrll 	if (ucaa->ucaa_info != NULL)
    271  1.111     skrll 		aprint_normal(": %s", ucaa->ucaa_info);
    272   1.83     pooka 	aprint_normal("\n");
    273   1.12  augustss 
    274  1.111     skrll 	prop_dictionary_set_int32(device_properties(self), "port",
    275  1.111     skrll 	    ucaa->ucaa_portno);
    276  1.110     joerg 
    277   1.77      cube 	sc->sc_dev = self;
    278  1.111     skrll 	sc->sc_udev = ucaa->ucaa_device;
    279  1.111     skrll 	sc->sc_iface = ucaa->ucaa_iface;
    280  1.111     skrll 	sc->sc_bulkout_no = ucaa->ucaa_bulkout;
    281  1.111     skrll 	sc->sc_bulkin_no = ucaa->ucaa_bulkin;
    282  1.111     skrll 	sc->sc_ibufsize = ucaa->ucaa_ibufsize;
    283  1.111     skrll 	sc->sc_ibufsizepad = ucaa->ucaa_ibufsizepad;
    284  1.111     skrll 	sc->sc_obufsize = ucaa->ucaa_obufsize;
    285  1.111     skrll 	sc->sc_opkthdrlen = ucaa->ucaa_opkthdrlen;
    286  1.111     skrll 	sc->sc_methods = ucaa->ucaa_methods;
    287  1.111     skrll 	sc->sc_parent = ucaa->ucaa_arg;
    288  1.111     skrll 	sc->sc_portno = ucaa->ucaa_portno;
    289   1.12  augustss 
    290   1.82    martin 	sc->sc_lsr = 0;
    291   1.82    martin 	sc->sc_msr = 0;
    292   1.82    martin 	sc->sc_mcr = 0;
    293   1.82    martin 	sc->sc_tx_stopped = 0;
    294   1.82    martin 	sc->sc_swflags = 0;
    295  1.130  riastrad 	sc->sc_closing = false;
    296  1.115     skrll 	sc->sc_dying = false;
    297  1.115     skrll 	sc->sc_state = UCOM_DEAD;
    298   1.82    martin 
    299  1.111     skrll 	sc->sc_si = softint_establish(SOFTINT_USB, ucom_softintr, sc);
    300  1.111     skrll 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    301  1.115     skrll 	cv_init(&sc->sc_statecv, "ucomstate");
    302  1.111     skrll 
    303  1.133  riastrad 	rnd_attach_source(&sc->sc_rndsource, device_xname(sc->sc_dev),
    304  1.133  riastrad 	    RND_TYPE_TTY, RND_FLAG_DEFAULT);
    305  1.133  riastrad 
    306  1.111     skrll 	SIMPLEQ_INIT(&sc->sc_ibuff_empty);
    307  1.111     skrll 	SIMPLEQ_INIT(&sc->sc_ibuff_full);
    308  1.111     skrll 	SIMPLEQ_INIT(&sc->sc_obuff_free);
    309  1.111     skrll 	SIMPLEQ_INIT(&sc->sc_obuff_full);
    310  1.111     skrll 
    311  1.111     skrll 	memset(sc->sc_ibuff, 0, sizeof(sc->sc_ibuff));
    312  1.111     skrll 	memset(sc->sc_obuff, 0, sizeof(sc->sc_obuff));
    313  1.111     skrll 
    314  1.119  pgoyette 	DPRINTF("open pipes in=%jd out=%jd",
    315  1.119  pgoyette 	    sc->sc_bulkin_no, sc->sc_bulkout_no, 0, 0);
    316  1.111     skrll 
    317  1.111     skrll 	struct ucom_buffer *ub;
    318  1.111     skrll 	usbd_status err;
    319  1.111     skrll 	int error;
    320  1.111     skrll 
    321  1.111     skrll 	/* Open the bulk pipes */
    322  1.111     skrll 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no,
    323  1.111     skrll 	    USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
    324  1.111     skrll 	if (err) {
    325  1.119  pgoyette 		DPRINTF("open bulk in error (addr %jd), err=%jd",
    326  1.111     skrll 		    sc->sc_bulkin_no, err, 0, 0);
    327  1.111     skrll 		error = EIO;
    328  1.111     skrll 		goto fail_0;
    329  1.111     skrll 	}
    330  1.115     skrll 	/* Allocate input buffers */
    331  1.115     skrll 	for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
    332  1.115     skrll 	    ub++) {
    333  1.115     skrll 		error = usbd_create_xfer(sc->sc_bulkin_pipe,
    334  1.120     skrll 		    sc->sc_ibufsizepad, 0, 0,
    335  1.115     skrll 		    &ub->ub_xfer);
    336  1.115     skrll 		if (error)
    337  1.115     skrll 			goto fail_1;
    338  1.115     skrll 		ub->ub_data = usbd_get_buffer(ub->ub_xfer);
    339  1.115     skrll 	}
    340  1.115     skrll 
    341  1.111     skrll 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no,
    342  1.111     skrll 	    USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
    343  1.111     skrll 	if (err) {
    344  1.119  pgoyette 		DPRINTF("open bulk out error (addr %jd), err=%jd",
    345  1.111     skrll 		    sc->sc_bulkout_no, err, 0, 0);
    346  1.111     skrll 		error = EIO;
    347  1.111     skrll 		goto fail_1;
    348  1.111     skrll 	}
    349  1.111     skrll 	for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
    350  1.111     skrll 	    ub++) {
    351  1.115     skrll 		error = usbd_create_xfer(sc->sc_bulkout_pipe,
    352  1.115     skrll 		    sc->sc_obufsize, 0, 0, &ub->ub_xfer);
    353  1.111     skrll 		if (error)
    354  1.111     skrll 			goto fail_2;
    355  1.111     skrll 		ub->ub_data = usbd_get_buffer(ub->ub_xfer);
    356  1.111     skrll 		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
    357  1.111     skrll 	}
    358   1.82    martin 
    359  1.115     skrll 	struct tty *tp = tty_alloc();
    360   1.12  augustss 	tp->t_oproc = ucomstart;
    361   1.12  augustss 	tp->t_param = ucomparam;
    362   1.82    martin 	tp->t_hwiflow = ucomhwiflow;
    363   1.12  augustss 	sc->sc_tty = tp;
    364   1.12  augustss 
    365  1.119  pgoyette 	DPRINTF("tty_attach %#jx", (uintptr_t)tp, 0, 0, 0);
    366   1.12  augustss 	tty_attach(tp);
    367   1.12  augustss 
    368   1.74       smb 	if (!pmf_device_register(self, NULL, NULL))
    369   1.74       smb 		aprint_error_dev(self, "couldn't establish power handler\n");
    370  1.115     skrll 
    371  1.115     skrll 	sc->sc_state = UCOM_ATTACHED;
    372   1.85    dyoung 	return;
    373  1.111     skrll 
    374  1.111     skrll fail_2:
    375  1.115     skrll 	for (ub = &sc->sc_obuff[0]; ub != &sc->sc_obuff[UCOM_OUT_BUFFS];
    376  1.111     skrll 	    ub++) {
    377  1.111     skrll 		if (ub->ub_xfer)
    378  1.111     skrll 			usbd_destroy_xfer(ub->ub_xfer);
    379  1.111     skrll 	}
    380  1.115     skrll 	usbd_close_pipe(sc->sc_bulkout_pipe);
    381  1.130  riastrad 	sc->sc_bulkout_pipe = NULL;
    382  1.115     skrll 
    383  1.115     skrll fail_1:
    384  1.115     skrll 	for (ub = &sc->sc_ibuff[0]; ub != &sc->sc_ibuff[UCOM_IN_BUFFS];
    385  1.111     skrll 	    ub++) {
    386  1.111     skrll 		if (ub->ub_xfer)
    387  1.111     skrll 			usbd_destroy_xfer(ub->ub_xfer);
    388  1.111     skrll 	}
    389  1.111     skrll 	usbd_close_pipe(sc->sc_bulkin_pipe);
    390  1.130  riastrad 	sc->sc_bulkin_pipe = NULL;
    391  1.111     skrll 
    392  1.111     skrll fail_0:
    393  1.111     skrll 	aprint_error_dev(self, "attach failed, error=%d\n", error);
    394   1.12  augustss }
    395   1.12  augustss 
    396   1.85    dyoung int
    397   1.85    dyoung ucom_detach(device_t self, int flags)
    398   1.12  augustss {
    399   1.77      cube 	struct ucom_softc *sc = device_private(self);
    400   1.36  augustss 	struct tty *tp = sc->sc_tty;
    401   1.12  augustss 	int maj, mn;
    402  1.111     skrll 	int i;
    403  1.111     skrll 
    404  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    405   1.12  augustss 
    406  1.119  pgoyette 	DPRINTF("sc=%#jx flags=%jd tp=%#jx", (uintptr_t)sc, flags,
    407  1.119  pgoyette 	    (uintptr_t)tp, 0);
    408  1.119  pgoyette 	DPRINTF("... pipe=%jd,%jd", sc->sc_bulkin_no, sc->sc_bulkout_no, 0, 0);
    409   1.12  augustss 
    410  1.130  riastrad 	/* Prevent new opens from hanging.  */
    411  1.111     skrll 	mutex_enter(&sc->sc_lock);
    412  1.115     skrll 	sc->sc_dying = true;
    413  1.111     skrll 	mutex_exit(&sc->sc_lock);
    414  1.111     skrll 
    415   1.74       smb 	pmf_device_deregister(self);
    416   1.12  augustss 
    417  1.130  riastrad 	/* tty is now off.  */
    418  1.130  riastrad 	if (tp != NULL) {
    419  1.134  riastrad 		ttylock(tp);
    420  1.130  riastrad 		CLR(tp->t_state, TS_CARR_ON);
    421  1.130  riastrad 		CLR(tp->t_cflag, CLOCAL | MDMBUF);
    422  1.134  riastrad 		ttyunlock(tp);
    423  1.115     skrll 	}
    424  1.115     skrll 
    425   1.12  augustss 	/* locate the major number */
    426   1.44   gehenna 	maj = cdevsw_lookup_major(&ucom_cdevsw);
    427   1.12  augustss 
    428   1.12  augustss 	/* Nuke the vnodes for any open instances. */
    429   1.62   thorpej 	mn = device_unit(self);
    430  1.119  pgoyette 	DPRINTF("maj=%jd mn=%jd", maj, mn, 0, 0);
    431   1.12  augustss 	vdevgone(maj, mn, mn, VCHR);
    432   1.22  augustss 	vdevgone(maj, mn | UCOMDIALOUT_MASK, mn | UCOMDIALOUT_MASK, VCHR);
    433   1.22  augustss 	vdevgone(maj, mn | UCOMCALLUNIT_MASK, mn | UCOMCALLUNIT_MASK, VCHR);
    434   1.12  augustss 
    435  1.130  riastrad 	softint_disestablish(sc->sc_si);
    436  1.130  riastrad 
    437   1.12  augustss 	/* Detach and free the tty. */
    438   1.36  augustss 	if (tp != NULL) {
    439   1.36  augustss 		tty_detach(tp);
    440   1.87     rmind 		tty_free(tp);
    441   1.33  augustss 		sc->sc_tty = NULL;
    442   1.33  augustss 	}
    443   1.12  augustss 
    444   1.82    martin 	for (i = 0; i < UCOM_IN_BUFFS; i++) {
    445   1.82    martin 		if (sc->sc_ibuff[i].ub_xfer != NULL)
    446  1.111     skrll 			usbd_destroy_xfer(sc->sc_ibuff[i].ub_xfer);
    447   1.82    martin 	}
    448   1.82    martin 
    449   1.82    martin 	for (i = 0; i < UCOM_OUT_BUFFS; i++) {
    450   1.82    martin 		if (sc->sc_obuff[i].ub_xfer != NULL)
    451  1.111     skrll 			usbd_destroy_xfer(sc->sc_obuff[i].ub_xfer);
    452  1.111     skrll 	}
    453  1.111     skrll 
    454  1.111     skrll 	if (sc->sc_bulkin_pipe != NULL) {
    455  1.111     skrll 		usbd_close_pipe(sc->sc_bulkin_pipe);
    456  1.111     skrll 		sc->sc_bulkin_pipe = NULL;
    457  1.111     skrll 	}
    458  1.111     skrll 
    459  1.111     skrll 	if (sc->sc_bulkout_pipe != NULL) {
    460  1.111     skrll 		usbd_close_pipe(sc->sc_bulkout_pipe);
    461  1.111     skrll 		sc->sc_bulkout_pipe = NULL;
    462   1.82    martin 	}
    463   1.82    martin 
    464   1.31  explorer 	/* Detach the random source */
    465   1.31  explorer 	rnd_detach_source(&sc->sc_rndsource);
    466   1.31  explorer 
    467  1.111     skrll 	mutex_destroy(&sc->sc_lock);
    468  1.115     skrll 	cv_destroy(&sc->sc_statecv);
    469  1.111     skrll 
    470  1.111     skrll 	return 0;
    471   1.12  augustss }
    472   1.12  augustss 
    473   1.12  augustss void
    474   1.24  augustss ucom_shutdown(struct ucom_softc *sc)
    475   1.12  augustss {
    476   1.12  augustss 	struct tty *tp = sc->sc_tty;
    477   1.12  augustss 
    478  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    479  1.111     skrll 
    480   1.12  augustss 	/*
    481   1.12  augustss 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    482   1.12  augustss 	 * notice even if we immediately open the port again.
    483   1.12  augustss 	 */
    484   1.12  augustss 	if (ISSET(tp->t_cflag, HUPCL)) {
    485   1.12  augustss 		ucom_dtr(sc, 0);
    486  1.130  riastrad 		mutex_enter(&sc->sc_lock);
    487  1.130  riastrad 		microuptime(&sc->sc_hup_time);
    488  1.130  riastrad 		sc->sc_hup_time.tv_sec++;
    489  1.130  riastrad 		mutex_exit(&sc->sc_lock);
    490   1.12  augustss 	}
    491   1.12  augustss }
    492   1.12  augustss 
    493  1.130  riastrad /*
    494  1.130  riastrad  * ucomopen(dev, flag, mode, l)
    495  1.130  riastrad  *
    496  1.130  riastrad  *	Called when anyone tries to open /dev/ttyU? for an existing
    497  1.130  riastrad  *	ucom? instance that has completed attach.  The attach may have
    498  1.130  riastrad  *	failed, though, or there may be concurrent detach or close in
    499  1.130  riastrad  *	progress, so fail if attach failed (no sc_tty) or detach has
    500  1.130  riastrad  *	begun (sc_dying), or wait if there's a concurrent close in
    501  1.130  riastrad  *	progress before reopening.
    502  1.130  riastrad  */
    503   1.12  augustss int
    504   1.69  christos ucomopen(dev_t dev, int flag, int mode, struct lwp *l)
    505   1.12  augustss {
    506  1.115     skrll 	const int unit = UCOMUNIT(dev);
    507  1.115     skrll 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    508  1.115     skrll 	int error = 0;
    509   1.43  augustss 
    510  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    511  1.111     skrll 
    512  1.111     skrll 	mutex_enter(&sc->sc_lock);
    513  1.111     skrll 	if (sc->sc_dying) {
    514  1.115     skrll 		DPRINTF("... dying", 0, 0, 0, 0);
    515  1.111     skrll 		mutex_exit(&sc->sc_lock);
    516  1.130  riastrad 		return ENXIO;
    517  1.111     skrll 	}
    518   1.12  augustss 
    519  1.111     skrll 	if (!device_is_active(sc->sc_dev)) {
    520  1.111     skrll 		mutex_exit(&sc->sc_lock);
    521  1.111     skrll 		return ENXIO;
    522  1.111     skrll 	}
    523   1.12  augustss 
    524  1.115     skrll 	struct tty *tp = sc->sc_tty;
    525  1.130  riastrad 	if (tp == NULL) {
    526  1.130  riastrad 		DPRINTF("... not attached", 0, 0, 0, 0);
    527  1.130  riastrad 		mutex_exit(&sc->sc_lock);
    528  1.130  riastrad 		return ENXIO;
    529  1.130  riastrad 	}
    530   1.12  augustss 
    531  1.119  pgoyette 	DPRINTF("unit=%jd, tp=%#jx", unit, (uintptr_t)tp, 0, 0);
    532   1.12  augustss 
    533  1.111     skrll 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) {
    534  1.111     skrll 		mutex_exit(&sc->sc_lock);
    535  1.111     skrll 		return EBUSY;
    536  1.111     skrll 	}
    537   1.12  augustss 
    538   1.12  augustss 	/*
    539  1.130  riastrad 	 * If the previous use had set DTR on close, wait up to one
    540  1.130  riastrad 	 * second for the other side to notice we hung up.  After
    541  1.130  riastrad 	 * sleeping, the tty may have been revoked, so restart the
    542  1.130  riastrad 	 * whole operation.
    543  1.130  riastrad 	 *
    544  1.130  riastrad 	 * XXX The wchan is not ttclose but maybe should be.
    545   1.12  augustss 	 */
    546  1.130  riastrad 	if (timerisset(&sc->sc_hup_time)) {
    547  1.130  riastrad 		struct timeval now, delta;
    548  1.130  riastrad 		int ms, ticks;
    549  1.130  riastrad 
    550  1.130  riastrad 		microuptime(&now);
    551  1.130  riastrad 		if (timercmp(&now, &sc->sc_hup_time, <)) {
    552  1.130  riastrad 			timersub(&sc->sc_hup_time, &now, &delta);
    553  1.130  riastrad 			ms = MIN(INT_MAX - 1000, delta.tv_sec*1000);
    554  1.130  riastrad 			ms += howmany(delta.tv_usec, 1000);
    555  1.130  riastrad 			ticks = MAX(1, MIN(INT_MAX, mstohz(ms)));
    556  1.138  riastrad 			(void)cv_timedwait(&sc->sc_statecv, &sc->sc_lock,
    557  1.130  riastrad 			    ticks);
    558  1.111     skrll 			mutex_exit(&sc->sc_lock);
    559  1.138  riastrad 			return ERESTART;
    560  1.111     skrll 		}
    561  1.137   thorpej 		timerclear(&sc->sc_hup_time);
    562   1.17  augustss 	}
    563  1.130  riastrad 
    564  1.130  riastrad 	/*
    565  1.130  riastrad 	 * Wait while the device is initialized by the
    566  1.130  riastrad 	 * first opener or cleaned up by the last closer.
    567  1.130  riastrad 	 */
    568  1.115     skrll 	enum ucom_state state = sc->sc_state;
    569  1.130  riastrad 	if (state == UCOM_OPENING || sc->sc_closing) {
    570  1.130  riastrad 		if (flag & FNONBLOCK)
    571  1.130  riastrad 			error = EWOULDBLOCK;
    572  1.130  riastrad 		else
    573  1.130  riastrad 			error = cv_wait_sig(&sc->sc_statecv, &sc->sc_lock);
    574  1.130  riastrad 		mutex_exit(&sc->sc_lock);
    575  1.130  riastrad 		return error ? error : ERESTART;
    576  1.130  riastrad 	}
    577  1.115     skrll 	KASSERTMSG(state == UCOM_OPEN || state == UCOM_ATTACHED,
    578  1.115     skrll 	    "state is %d", state);
    579   1.43  augustss 
    580  1.130  riastrad 	/*
    581  1.130  riastrad 	 * If this is the first open, then make sure the pipes are
    582  1.130  riastrad 	 * running and perform any initialization needed.
    583  1.130  riastrad 	 */
    584  1.130  riastrad 	bool firstopen = (state == UCOM_ATTACHED);
    585  1.130  riastrad 	if (firstopen) {
    586  1.130  riastrad 		KASSERT(!ISSET(tp->t_state, TS_ISOPEN));
    587  1.130  riastrad 		KASSERT(tp->t_wopen == 0);
    588  1.130  riastrad 
    589   1.12  augustss 		tp->t_dev = dev;
    590  1.115     skrll 		sc->sc_state = UCOM_OPENING;
    591  1.130  riastrad 		mutex_exit(&sc->sc_lock);
    592   1.12  augustss 
    593   1.22  augustss 		if (sc->sc_methods->ucom_open != NULL) {
    594   1.22  augustss 			error = sc->sc_methods->ucom_open(sc->sc_parent,
    595  1.115     skrll 			    sc->sc_portno);
    596  1.130  riastrad 			if (error)
    597  1.130  riastrad 				goto bad;
    598   1.22  augustss 		}
    599   1.22  augustss 
    600   1.12  augustss 		ucom_status_change(sc);
    601   1.12  augustss 
    602  1.106       gdt 		/* Clear PPS capture state on first open. */
    603  1.106       gdt 		mutex_spin_enter(&timecounter_lock);
    604  1.106       gdt 		memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state));
    605  1.106       gdt 		sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
    606  1.106       gdt 		pps_init(&sc->sc_pps_state);
    607  1.106       gdt 		mutex_spin_exit(&timecounter_lock);
    608  1.106       gdt 
    609   1.12  augustss 		/*
    610   1.12  augustss 		 * Initialize the termios status to the defaults.  Add in the
    611   1.12  augustss 		 * sticky bits from TIOCSFLAGS.
    612   1.12  augustss 		 */
    613  1.115     skrll 		struct termios t;
    614  1.115     skrll 
    615   1.12  augustss 		t.c_ispeed = 0;
    616   1.12  augustss 		t.c_ospeed = TTYDEF_SPEED;
    617   1.12  augustss 		t.c_cflag = TTYDEF_CFLAG;
    618   1.12  augustss 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    619   1.12  augustss 			SET(t.c_cflag, CLOCAL);
    620   1.12  augustss 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    621   1.12  augustss 			SET(t.c_cflag, CRTSCTS);
    622   1.12  augustss 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    623   1.12  augustss 			SET(t.c_cflag, MDMBUF);
    624   1.12  augustss 		/* Make sure ucomparam() will do something. */
    625   1.12  augustss 		tp->t_ospeed = 0;
    626   1.12  augustss 		(void) ucomparam(tp, &t);
    627   1.12  augustss 		tp->t_iflag = TTYDEF_IFLAG;
    628   1.12  augustss 		tp->t_oflag = TTYDEF_OFLAG;
    629   1.12  augustss 		tp->t_lflag = TTYDEF_LFLAG;
    630   1.12  augustss 		ttychars(tp);
    631   1.12  augustss 		ttsetwater(tp);
    632   1.12  augustss 
    633   1.12  augustss 		/*
    634   1.12  augustss 		 * Turn on DTR.  We must always do this, even if carrier is not
    635   1.12  augustss 		 * present, because otherwise we'd have to use TIOCSDTR
    636   1.12  augustss 		 * immediately after setting CLOCAL, which applications do not
    637   1.12  augustss 		 * expect.  We always assert DTR while the device is open
    638   1.64      gson 		 * unless explicitly requested to deassert it.  Ditto RTS.
    639   1.12  augustss 		 */
    640   1.12  augustss 		ucom_dtr(sc, 1);
    641  1.102  jakllsch 		ucom_rts(sc, 1);
    642   1.12  augustss 
    643  1.115     skrll 		mutex_enter(&sc->sc_lock);
    644   1.82    martin 		sc->sc_rx_unblock = 0;
    645   1.82    martin 		sc->sc_rx_stopped = 0;
    646   1.82    martin 		sc->sc_tx_stopped = 0;
    647   1.82    martin 
    648  1.115     skrll 		for (size_t i = 0; i < UCOM_IN_BUFFS; i++) {
    649  1.115     skrll 			struct ucom_buffer *ub = &sc->sc_ibuff[i];
    650  1.115     skrll 			error = ucomsubmitread(sc, ub);
    651  1.115     skrll 			if (error) {
    652  1.115     skrll 		    		mutex_exit(&sc->sc_lock);
    653  1.130  riastrad 				goto bad;
    654   1.82    martin 			}
    655   1.28    toshii 		}
    656   1.12  augustss 	}
    657  1.111     skrll 	mutex_exit(&sc->sc_lock);
    658   1.12  augustss 
    659  1.119  pgoyette 	DPRINTF("unit=%jd, tp=%#jx dialout %jd nonblock %jd", unit,
    660  1.119  pgoyette 	    (uintptr_t)tp, !!UCOMDIALOUT(dev), !!ISSET(flag, O_NONBLOCK));
    661   1.12  augustss 	error = ttyopen(tp, UCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    662   1.12  augustss 	if (error)
    663   1.12  augustss 		goto bad;
    664   1.12  augustss 
    665   1.32       eeh 	error = (*tp->t_linesw->l_open)(dev, tp);
    666   1.12  augustss 	if (error)
    667   1.12  augustss 		goto bad;
    668   1.12  augustss 
    669  1.130  riastrad 	/*
    670  1.130  riastrad 	 * Success!  If this was the first open, notify waiters that
    671  1.130  riastrad 	 * the tty is open for business.
    672  1.130  riastrad 	 */
    673  1.130  riastrad 	if (firstopen) {
    674  1.130  riastrad 		mutex_enter(&sc->sc_lock);
    675  1.130  riastrad 		KASSERT(sc->sc_state == UCOM_OPENING);
    676  1.130  riastrad 		sc->sc_state = UCOM_OPEN;
    677  1.130  riastrad 		cv_broadcast(&sc->sc_statecv);
    678  1.130  riastrad 		mutex_exit(&sc->sc_lock);
    679  1.130  riastrad 	}
    680  1.111     skrll 	return 0;
    681   1.26    toshii 
    682  1.115     skrll bad:
    683  1.130  riastrad 	/*
    684  1.130  riastrad 	 * Failure!  If this was the first open, hang up, abort pipes,
    685  1.130  riastrad 	 * and notify waiters that we're not opening after all.
    686  1.130  riastrad 	 */
    687  1.130  riastrad 	if (firstopen) {
    688  1.130  riastrad 		ucom_cleanup(sc, flag);
    689  1.115     skrll 
    690  1.130  riastrad 		mutex_enter(&sc->sc_lock);
    691  1.130  riastrad 		KASSERT(sc->sc_state == UCOM_OPENING);
    692  1.130  riastrad 		sc->sc_state = UCOM_ATTACHED;
    693  1.130  riastrad 		cv_broadcast(&sc->sc_statecv);
    694  1.130  riastrad 		mutex_exit(&sc->sc_lock);
    695   1.12  augustss 	}
    696  1.111     skrll 	return error;
    697   1.12  augustss }
    698   1.12  augustss 
    699  1.130  riastrad /*
    700  1.130  riastrad  * ucomcancel(dev, flag, mode, l)
    701  1.130  riastrad  *
    702  1.130  riastrad  *	Called on revoke or last close.  Must interrupt any pending I/O
    703  1.130  riastrad  *	operations and make them fail promptly; once they have all
    704  1.130  riastrad  *	finished (except possibly new opens), ucomclose will be called.
    705  1.130  riastrad  *	We set sc_closing to block new opens until ucomclose runs.
    706  1.130  riastrad  */
    707   1.12  augustss int
    708  1.130  riastrad ucomcancel(dev_t dev, int flag, int mode, struct lwp *l)
    709   1.12  augustss {
    710  1.115     skrll 	const int unit = UCOMUNIT(dev);
    711  1.115     skrll 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit);
    712   1.12  augustss 
    713  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    714  1.111     skrll 
    715  1.119  pgoyette 	DPRINTF("unit=%jd", UCOMUNIT(dev), 0, 0, 0);
    716  1.101  jakllsch 
    717  1.130  riastrad 	/*
    718  1.130  riastrad 	 * This can run at any time before ucomclose on any device
    719  1.130  riastrad 	 * node, even if never attached or if attach failed, so we may
    720  1.130  riastrad 	 * not have a softc or a tty.
    721  1.130  riastrad 	 */
    722  1.101  jakllsch 	if (sc == NULL)
    723  1.101  jakllsch 		return 0;
    724  1.130  riastrad 	struct tty *tp = sc->sc_tty;
    725  1.130  riastrad 	if (tp == NULL)
    726  1.130  riastrad 		return 0;
    727  1.101  jakllsch 
    728  1.130  riastrad 	/*
    729  1.130  riastrad 	 * Mark the device closing so opens block until we're done
    730  1.130  riastrad 	 * closing.  Wake them up so they start over at the top -- if
    731  1.130  riastrad 	 * we're closing because we're detaching, they need to wake up
    732  1.130  riastrad 	 * and notice it's time to fail.
    733  1.130  riastrad 	 */
    734  1.111     skrll 	mutex_enter(&sc->sc_lock);
    735  1.130  riastrad 	sc->sc_closing = true;
    736  1.130  riastrad 	cv_broadcast(&sc->sc_statecv);
    737  1.130  riastrad 	mutex_exit(&sc->sc_lock);
    738  1.115     skrll 
    739  1.115     skrll 	/*
    740  1.130  riastrad 	 * Cancel any pending tty I/O operations, causing them to wake
    741  1.130  riastrad 	 * up and fail promptly, and preventing any new ones from
    742  1.130  riastrad 	 * starting to wait until we have finished closing.
    743  1.115     skrll 	 */
    744  1.130  riastrad 	ttycancel(tp);
    745  1.130  riastrad 
    746  1.130  riastrad 	return 0;
    747  1.130  riastrad }
    748  1.130  riastrad 
    749  1.130  riastrad /*
    750  1.130  riastrad  * ucomclose(dev, flag, mode, l)
    751  1.130  riastrad  *
    752  1.130  riastrad  *	Called after ucomcancel, when all prior operations on the /dev
    753  1.130  riastrad  *	node have completed.  Only new opens may be in progress at this
    754  1.130  riastrad  *	point, but they will block until sc_closing is set to false.
    755  1.130  riastrad  */
    756  1.130  riastrad int
    757  1.130  riastrad ucomclose(dev_t dev, int flag, int mode, struct lwp *l)
    758  1.130  riastrad {
    759  1.130  riastrad 	const int unit = UCOMUNIT(dev);
    760  1.130  riastrad 	struct ucom_softc *sc = device_lookup_private(&ucom_cd, unit);
    761  1.130  riastrad 	int error = 0;
    762  1.115     skrll 
    763  1.130  riastrad 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    764  1.115     skrll 
    765  1.130  riastrad 	DPRINTF("unit=%jd", UCOMUNIT(dev), 0, 0, 0);
    766  1.101  jakllsch 
    767  1.130  riastrad 	/*
    768  1.130  riastrad 	 * This can run at any time after ucomcancel on any device
    769  1.130  riastrad 	 * node, even if never attached or if attach failed, so we may
    770  1.130  riastrad 	 * not have a softc or a tty.
    771  1.130  riastrad 	 */
    772  1.130  riastrad 	if (sc == NULL)
    773  1.130  riastrad 		return 0;
    774  1.115     skrll 	struct tty *tp = sc->sc_tty;
    775  1.130  riastrad 	if (tp == NULL)
    776  1.115     skrll 		return 0;
    777   1.12  augustss 
    778  1.130  riastrad 	/*
    779  1.130  riastrad 	 * Close the tty, causing anyone waiting for it to wake, and
    780  1.130  riastrad 	 * hang up the phone.
    781  1.130  riastrad 	 */
    782  1.130  riastrad 	ucom_cleanup(sc, flag);
    783   1.17  augustss 
    784  1.130  riastrad 	/*
    785  1.130  riastrad 	 * ttyclose should have cleared TS_ISOPEN and interrupted all
    786  1.130  riastrad 	 * pending opens, which should have completed by now.
    787  1.130  riastrad 	 */
    788  1.134  riastrad 	ttylock(tp);
    789  1.130  riastrad 	KASSERT(!ISSET(tp->t_state, TS_ISOPEN));
    790  1.130  riastrad 	KASSERT(tp->t_wopen == 0);
    791  1.134  riastrad 	ttyunlock(tp);
    792   1.12  augustss 
    793  1.130  riastrad 	/*
    794  1.130  riastrad 	 * Close any device-specific state.
    795  1.130  riastrad 	 */
    796  1.130  riastrad 	if (sc->sc_methods->ucom_close != NULL) {
    797  1.130  riastrad 		sc->sc_methods->ucom_close(sc->sc_parent,
    798  1.130  riastrad 		    sc->sc_portno);
    799   1.12  augustss 	}
    800   1.12  augustss 
    801  1.130  riastrad 	/*
    802  1.130  riastrad 	 * We're now closed.  Can reopen after this point, so resume
    803  1.130  riastrad 	 * transfers, mark us no longer closing, and notify anyone
    804  1.130  riastrad 	 * waiting in open.  The state may be OPEN or ATTACHED at this
    805  1.130  riastrad 	 * point -- OPEN if the device was already open when we closed
    806  1.130  riastrad 	 * it, ATTACHED if we interrupted it in the process of opening.
    807  1.130  riastrad 	 */
    808  1.115     skrll 	mutex_enter(&sc->sc_lock);
    809  1.130  riastrad 	KASSERTMSG(sc->sc_state == UCOM_ATTACHED || sc->sc_state == UCOM_OPEN,
    810  1.130  riastrad 	    "%s sc=%p state=%d", device_xname(sc->sc_dev), sc, sc->sc_state);
    811  1.130  riastrad 	KASSERT(sc->sc_closing);
    812  1.130  riastrad 	sc->sc_state = UCOM_ATTACHED;
    813  1.130  riastrad 	sc->sc_closing = false;
    814  1.130  riastrad 	cv_broadcast(&sc->sc_statecv);
    815  1.111     skrll 	mutex_exit(&sc->sc_lock);
    816  1.111     skrll 
    817  1.115     skrll 	return error;
    818   1.12  augustss }
    819   1.43  augustss 
    820   1.12  augustss int
    821   1.24  augustss ucomread(dev_t dev, struct uio *uio, int flag)
    822   1.12  augustss {
    823  1.115     skrll 	const int unit = UCOMUNIT(dev);
    824  1.115     skrll 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    825  1.130  riastrad 	struct tty *tp = sc->sc_tty;
    826   1.12  augustss 
    827  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    828  1.111     skrll 
    829  1.130  riastrad 	return (*tp->t_linesw->l_read)(tp, uio, flag);
    830   1.12  augustss }
    831   1.43  augustss 
    832   1.12  augustss int
    833   1.24  augustss ucomwrite(dev_t dev, struct uio *uio, int flag)
    834   1.12  augustss {
    835  1.115     skrll 	const int unit = UCOMUNIT(dev);
    836  1.115     skrll 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    837  1.130  riastrad 	struct tty *tp = sc->sc_tty;
    838   1.12  augustss 
    839  1.115     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    840  1.115     skrll 
    841  1.130  riastrad 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    842   1.38       scw }
    843   1.38       scw 
    844   1.38       scw int
    845   1.60  christos ucompoll(dev_t dev, int events, struct lwp *l)
    846   1.38       scw {
    847  1.115     skrll 	const int unit = UCOMUNIT(dev);
    848  1.115     skrll 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    849  1.130  riastrad 	struct tty *tp = sc->sc_tty;
    850  1.115     skrll 
    851  1.115     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    852   1.38       scw 
    853  1.130  riastrad 	return (*tp->t_linesw->l_poll)(tp, events, l);
    854   1.12  augustss }
    855   1.12  augustss 
    856   1.12  augustss struct tty *
    857   1.24  augustss ucomtty(dev_t dev)
    858   1.12  augustss {
    859  1.115     skrll 	const int unit = UCOMUNIT(dev);
    860  1.115     skrll 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    861   1.12  augustss 
    862  1.130  riastrad 	return sc->sc_tty;
    863   1.12  augustss }
    864   1.12  augustss 
    865   1.12  augustss int
    866   1.70  christos ucomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    867   1.12  augustss {
    868  1.115     skrll 	const int unit = UCOMUNIT(dev);
    869  1.115     skrll 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
    870   1.12  augustss 	struct tty *tp = sc->sc_tty;
    871   1.12  augustss 	int error;
    872   1.12  augustss 
    873  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
    874  1.111     skrll 
    875  1.127  christos 	DPRINTF("cmd=0x%08jx", cmd, 0, 0, 0);
    876   1.12  augustss 
    877   1.60  christos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    878   1.42    atatat 	if (error != EPASSTHROUGH)
    879  1.111     skrll 		return error;
    880   1.12  augustss 
    881   1.60  christos 	error = ttioctl(tp, cmd, data, flag, l);
    882   1.42    atatat 	if (error != EPASSTHROUGH)
    883  1.111     skrll 		return error;
    884   1.12  augustss 
    885   1.12  augustss 	if (sc->sc_methods->ucom_ioctl != NULL) {
    886   1.12  augustss 		error = sc->sc_methods->ucom_ioctl(sc->sc_parent,
    887  1.115     skrll 		    sc->sc_portno, cmd, data, flag, l->l_proc);
    888   1.42    atatat 		if (error != EPASSTHROUGH)
    889  1.111     skrll 			return error;
    890   1.12  augustss 	}
    891   1.12  augustss 
    892   1.12  augustss 	error = 0;
    893   1.12  augustss 
    894  1.127  christos 	DPRINTF("our cmd=0x%08jx", cmd, 0, 0, 0);
    895   1.12  augustss 
    896   1.12  augustss 	switch (cmd) {
    897   1.12  augustss 	case TIOCSBRK:
    898   1.12  augustss 		ucom_break(sc, 1);
    899   1.12  augustss 		break;
    900   1.12  augustss 
    901   1.12  augustss 	case TIOCCBRK:
    902   1.12  augustss 		ucom_break(sc, 0);
    903   1.12  augustss 		break;
    904   1.12  augustss 
    905   1.12  augustss 	case TIOCSDTR:
    906   1.12  augustss 		ucom_dtr(sc, 1);
    907   1.12  augustss 		break;
    908   1.12  augustss 
    909   1.12  augustss 	case TIOCCDTR:
    910   1.12  augustss 		ucom_dtr(sc, 0);
    911   1.12  augustss 		break;
    912   1.12  augustss 
    913   1.12  augustss 	case TIOCGFLAGS:
    914  1.114     skrll 		mutex_enter(&sc->sc_lock);
    915   1.12  augustss 		*(int *)data = sc->sc_swflags;
    916  1.114     skrll 		mutex_exit(&sc->sc_lock);
    917   1.12  augustss 		break;
    918   1.12  augustss 
    919   1.12  augustss 	case TIOCSFLAGS:
    920   1.67      elad 		error = kauth_authorize_device_tty(l->l_cred,
    921   1.67      elad 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    922   1.12  augustss 		if (error)
    923   1.12  augustss 			break;
    924  1.114     skrll 		mutex_enter(&sc->sc_lock);
    925   1.12  augustss 		sc->sc_swflags = *(int *)data;
    926  1.114     skrll 		mutex_exit(&sc->sc_lock);
    927   1.12  augustss 		break;
    928   1.12  augustss 
    929   1.12  augustss 	case TIOCMSET:
    930   1.12  augustss 	case TIOCMBIS:
    931   1.12  augustss 	case TIOCMBIC:
    932   1.12  augustss 		tiocm_to_ucom(sc, cmd, *(int *)data);
    933   1.12  augustss 		break;
    934   1.12  augustss 
    935   1.12  augustss 	case TIOCMGET:
    936   1.12  augustss 		*(int *)data = ucom_to_tiocm(sc);
    937   1.12  augustss 		break;
    938   1.12  augustss 
    939  1.106       gdt 	case PPS_IOC_CREATE:
    940  1.106       gdt 	case PPS_IOC_DESTROY:
    941  1.106       gdt 	case PPS_IOC_GETPARAMS:
    942  1.106       gdt 	case PPS_IOC_SETPARAMS:
    943  1.106       gdt 	case PPS_IOC_GETCAP:
    944  1.106       gdt 	case PPS_IOC_FETCH:
    945  1.106       gdt #ifdef PPS_SYNC
    946  1.106       gdt 	case PPS_IOC_KCBIND:
    947  1.106       gdt #endif
    948  1.106       gdt 		mutex_spin_enter(&timecounter_lock);
    949  1.106       gdt 		error = pps_ioctl(cmd, data, &sc->sc_pps_state);
    950  1.106       gdt 		mutex_spin_exit(&timecounter_lock);
    951  1.106       gdt 		break;
    952  1.106       gdt 
    953   1.12  augustss 	default:
    954   1.42    atatat 		error = EPASSTHROUGH;
    955   1.12  augustss 		break;
    956   1.12  augustss 	}
    957   1.12  augustss 
    958  1.111     skrll 	return error;
    959   1.12  augustss }
    960   1.12  augustss 
    961   1.82    martin static void
    962   1.29    toshii tiocm_to_ucom(struct ucom_softc *sc, u_long how, int ttybits)
    963   1.12  augustss {
    964   1.12  augustss 	u_char combits;
    965    1.3  augustss 
    966   1.12  augustss 	combits = 0;
    967   1.12  augustss 	if (ISSET(ttybits, TIOCM_DTR))
    968   1.12  augustss 		SET(combits, UMCR_DTR);
    969   1.12  augustss 	if (ISSET(ttybits, TIOCM_RTS))
    970   1.12  augustss 		SET(combits, UMCR_RTS);
    971   1.43  augustss 
    972  1.114     skrll 	mutex_enter(&sc->sc_lock);
    973   1.12  augustss 	switch (how) {
    974   1.12  augustss 	case TIOCMBIC:
    975   1.12  augustss 		CLR(sc->sc_mcr, combits);
    976   1.12  augustss 		break;
    977   1.12  augustss 
    978   1.12  augustss 	case TIOCMBIS:
    979   1.12  augustss 		SET(sc->sc_mcr, combits);
    980   1.12  augustss 		break;
    981   1.12  augustss 
    982   1.12  augustss 	case TIOCMSET:
    983   1.12  augustss 		CLR(sc->sc_mcr, UMCR_DTR | UMCR_RTS);
    984   1.12  augustss 		SET(sc->sc_mcr, combits);
    985   1.12  augustss 		break;
    986   1.12  augustss 	}
    987  1.115     skrll 	u_char mcr = sc->sc_mcr;
    988  1.114     skrll 	mutex_exit(&sc->sc_lock);
    989   1.12  augustss 
    990   1.27    toshii 	if (how == TIOCMSET || ISSET(combits, UMCR_DTR))
    991  1.115     skrll 		ucom_dtr(sc, (mcr & UMCR_DTR) != 0);
    992   1.27    toshii 	if (how == TIOCMSET || ISSET(combits, UMCR_RTS))
    993  1.115     skrll 		ucom_rts(sc, (mcr & UMCR_RTS) != 0);
    994   1.12  augustss }
    995   1.12  augustss 
    996   1.82    martin static int
    997   1.24  augustss ucom_to_tiocm(struct ucom_softc *sc)
    998   1.12  augustss {
    999   1.12  augustss 	u_char combits;
   1000   1.12  augustss 	int ttybits = 0;
   1001   1.12  augustss 
   1002  1.114     skrll 	mutex_enter(&sc->sc_lock);
   1003   1.12  augustss 	combits = sc->sc_mcr;
   1004   1.12  augustss 	if (ISSET(combits, UMCR_DTR))
   1005   1.12  augustss 		SET(ttybits, TIOCM_DTR);
   1006   1.12  augustss 	if (ISSET(combits, UMCR_RTS))
   1007   1.12  augustss 		SET(ttybits, TIOCM_RTS);
   1008   1.12  augustss 
   1009   1.12  augustss 	combits = sc->sc_msr;
   1010   1.12  augustss 	if (ISSET(combits, UMSR_DCD))
   1011   1.12  augustss 		SET(ttybits, TIOCM_CD);
   1012   1.12  augustss 	if (ISSET(combits, UMSR_CTS))
   1013   1.12  augustss 		SET(ttybits, TIOCM_CTS);
   1014   1.12  augustss 	if (ISSET(combits, UMSR_DSR))
   1015   1.12  augustss 		SET(ttybits, TIOCM_DSR);
   1016   1.12  augustss 	if (ISSET(combits, UMSR_RI | UMSR_TERI))
   1017   1.12  augustss 		SET(ttybits, TIOCM_RI);
   1018   1.12  augustss 
   1019   1.12  augustss #if 0
   1020   1.12  augustss XXX;
   1021   1.12  augustss 	if (sc->sc_ier != 0)
   1022   1.12  augustss 		SET(ttybits, TIOCM_LE);
   1023   1.12  augustss #endif
   1024  1.114     skrll 	mutex_exit(&sc->sc_lock);
   1025   1.12  augustss 
   1026  1.111     skrll 	return ttybits;
   1027   1.12  augustss }
   1028   1.12  augustss 
   1029   1.82    martin static void
   1030   1.77      cube ucom_break(struct ucom_softc *sc, int onoff)
   1031   1.12  augustss {
   1032  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1033  1.111     skrll 
   1034  1.119  pgoyette 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
   1035   1.12  augustss 
   1036   1.14  augustss 	if (sc->sc_methods->ucom_set != NULL)
   1037   1.14  augustss 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1038   1.14  augustss 		    UCOM_SET_BREAK, onoff);
   1039   1.12  augustss }
   1040   1.12  augustss 
   1041   1.82    martin static void
   1042   1.24  augustss ucom_dtr(struct ucom_softc *sc, int onoff)
   1043   1.12  augustss {
   1044  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1045  1.111     skrll 
   1046  1.119  pgoyette 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
   1047   1.12  augustss 
   1048   1.14  augustss 	if (sc->sc_methods->ucom_set != NULL)
   1049   1.43  augustss 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1050   1.14  augustss 		    UCOM_SET_DTR, onoff);
   1051   1.12  augustss }
   1052   1.12  augustss 
   1053   1.82    martin static void
   1054   1.24  augustss ucom_rts(struct ucom_softc *sc, int onoff)
   1055   1.12  augustss {
   1056  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1057  1.111     skrll 
   1058  1.119  pgoyette 	DPRINTF("onoff=%jd", onoff, 0, 0, 0);
   1059   1.12  augustss 
   1060   1.14  augustss 	if (sc->sc_methods->ucom_set != NULL)
   1061   1.43  augustss 		sc->sc_methods->ucom_set(sc->sc_parent, sc->sc_portno,
   1062   1.14  augustss 		    UCOM_SET_RTS, onoff);
   1063   1.12  augustss }
   1064   1.12  augustss 
   1065   1.22  augustss void
   1066   1.24  augustss ucom_status_change(struct ucom_softc *sc)
   1067   1.12  augustss {
   1068   1.27    toshii 	struct tty *tp = sc->sc_tty;
   1069   1.27    toshii 
   1070   1.14  augustss 	if (sc->sc_methods->ucom_get_status != NULL) {
   1071  1.117     skrll 		u_char msr, lsr;
   1072  1.115     skrll 
   1073   1.14  augustss 		sc->sc_methods->ucom_get_status(sc->sc_parent, sc->sc_portno,
   1074  1.115     skrll 		    &lsr, &msr);
   1075  1.115     skrll 		mutex_enter(&sc->sc_lock);
   1076  1.115     skrll 		u_char old_msr = sc->sc_msr;
   1077  1.115     skrll 
   1078  1.115     skrll 		sc->sc_lsr = lsr;
   1079  1.115     skrll 		sc->sc_msr = msr;
   1080  1.115     skrll 		mutex_exit(&sc->sc_lock);
   1081  1.115     skrll 
   1082  1.115     skrll 		if (ISSET((msr ^ old_msr), UMSR_DCD)) {
   1083  1.106       gdt 			mutex_spin_enter(&timecounter_lock);
   1084  1.106       gdt 			pps_capture(&sc->sc_pps_state);
   1085  1.106       gdt 			pps_event(&sc->sc_pps_state,
   1086  1.106       gdt 			    (sc->sc_msr & UMSR_DCD) ?
   1087  1.106       gdt 			    PPS_CAPTUREASSERT :
   1088  1.106       gdt 			    PPS_CAPTURECLEAR);
   1089  1.106       gdt 			mutex_spin_exit(&timecounter_lock);
   1090  1.106       gdt 
   1091  1.115     skrll 			(*tp->t_linesw->l_modem)(tp, ISSET(msr, UMSR_DCD));
   1092  1.106       gdt 		}
   1093   1.14  augustss 	} else {
   1094  1.115     skrll 		mutex_enter(&sc->sc_lock);
   1095   1.14  augustss 		sc->sc_lsr = 0;
   1096   1.54  augustss 		/* Assume DCD is present, if we have no chance to check it. */
   1097   1.54  augustss 		sc->sc_msr = UMSR_DCD;
   1098  1.115     skrll 		mutex_exit(&sc->sc_lock);
   1099   1.14  augustss 	}
   1100   1.12  augustss }
   1101   1.12  augustss 
   1102   1.82    martin static int
   1103   1.24  augustss ucomparam(struct tty *tp, struct termios *t)
   1104   1.12  augustss {
   1105  1.115     skrll 	const int unit = UCOMUNIT(tp->t_dev);
   1106  1.115     skrll 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1107  1.115     skrll 	int error = 0;
   1108   1.12  augustss 
   1109  1.130  riastrad 	/* XXX should take tty lock around touching tp */
   1110  1.130  riastrad 
   1111  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1112  1.111     skrll 
   1113   1.12  augustss 	/* Check requested parameters. */
   1114  1.115     skrll 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed) {
   1115  1.115     skrll 		error = EINVAL;
   1116  1.115     skrll 		goto out;
   1117  1.115     skrll 	}
   1118   1.12  augustss 
   1119   1.12  augustss 	/*
   1120   1.12  augustss 	 * For the console, always force CLOCAL and !HUPCL, so that the port
   1121   1.12  augustss 	 * is always active.
   1122   1.12  augustss 	 */
   1123   1.12  augustss 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
   1124   1.12  augustss 		SET(t->c_cflag, CLOCAL);
   1125   1.12  augustss 		CLR(t->c_cflag, HUPCL);
   1126   1.12  augustss 	}
   1127   1.12  augustss 
   1128   1.12  augustss 	/*
   1129   1.12  augustss 	 * If there were no changes, don't do anything.  This avoids dropping
   1130   1.12  augustss 	 * input and improves performance when all we did was frob things like
   1131   1.12  augustss 	 * VMIN and VTIME.
   1132   1.12  augustss 	 */
   1133   1.12  augustss 	if (tp->t_ospeed == t->c_ospeed &&
   1134  1.115     skrll 	    tp->t_cflag == t->c_cflag) {
   1135  1.115     skrll 		goto out;
   1136  1.115     skrll 	}
   1137   1.12  augustss 
   1138   1.30  augustss 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
   1139   1.12  augustss 
   1140   1.12  augustss 	/* And copy to tty. */
   1141   1.12  augustss 	tp->t_ispeed = 0;
   1142   1.12  augustss 	tp->t_ospeed = t->c_ospeed;
   1143   1.12  augustss 	tp->t_cflag = t->c_cflag;
   1144   1.12  augustss 
   1145   1.14  augustss 	if (sc->sc_methods->ucom_param != NULL) {
   1146   1.14  augustss 		error = sc->sc_methods->ucom_param(sc->sc_parent, sc->sc_portno,
   1147  1.115     skrll 		    t);
   1148   1.14  augustss 		if (error)
   1149  1.115     skrll 			goto out;
   1150   1.14  augustss 	}
   1151   1.12  augustss 
   1152   1.30  augustss 	/* XXX worry about CHWFLOW */
   1153   1.12  augustss 
   1154   1.12  augustss 	/*
   1155   1.12  augustss 	 * Update the tty layer's idea of the carrier bit, in case we changed
   1156   1.12  augustss 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
   1157   1.12  augustss 	 * explicit request.
   1158   1.12  augustss 	 */
   1159  1.111     skrll 	DPRINTF("l_modem", 0, 0, 0, 0);
   1160   1.54  augustss 	(void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, UMSR_DCD));
   1161   1.12  augustss 
   1162   1.12  augustss #if 0
   1163   1.12  augustss XXX what if the hardware is not open
   1164   1.12  augustss 	if (!ISSET(t->c_cflag, CHWFLOW)) {
   1165   1.12  augustss 		if (sc->sc_tx_stopped) {
   1166   1.12  augustss 			sc->sc_tx_stopped = 0;
   1167   1.12  augustss 			ucomstart(tp);
   1168   1.12  augustss 		}
   1169   1.12  augustss 	}
   1170   1.12  augustss #endif
   1171  1.115     skrll out:
   1172  1.121  jakllsch 	return error;
   1173   1.12  augustss }
   1174   1.12  augustss 
   1175   1.82    martin static int
   1176   1.82    martin ucomhwiflow(struct tty *tp, int block)
   1177   1.12  augustss {
   1178  1.115     skrll 	const int unit = UCOMUNIT(tp->t_dev);
   1179  1.115     skrll 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1180   1.82    martin 	int old;
   1181   1.12  augustss 
   1182  1.115     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1183  1.115     skrll 
   1184  1.123       mrg 	KASSERT(&sc->sc_lock);
   1185  1.134  riastrad 	KASSERT(ttylocked(tp));
   1186  1.123       mrg 
   1187   1.82    martin 	old = sc->sc_rx_stopped;
   1188   1.82    martin 	sc->sc_rx_stopped = (u_char)block;
   1189   1.12  augustss 
   1190   1.82    martin 	if (old && !block) {
   1191   1.82    martin 		sc->sc_rx_unblock = 1;
   1192  1.122       mrg 		kpreempt_disable();
   1193   1.82    martin 		softint_schedule(sc->sc_si);
   1194  1.122       mrg 		kpreempt_enable();
   1195   1.12  augustss 	}
   1196   1.82    martin 
   1197  1.111     skrll 	return 1;
   1198    1.1  augustss }
   1199    1.3  augustss 
   1200   1.82    martin static void
   1201   1.24  augustss ucomstart(struct tty *tp)
   1202   1.12  augustss {
   1203  1.115     skrll 	const int unit = UCOMUNIT(tp->t_dev);
   1204  1.115     skrll 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1205   1.82    martin 	struct ucom_buffer *ub;
   1206   1.12  augustss 	u_char *data;
   1207   1.12  augustss 	int cnt;
   1208   1.12  augustss 
   1209  1.115     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1210  1.115     skrll 
   1211   1.84  christos 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
   1212   1.12  augustss 		goto out;
   1213   1.12  augustss 	if (sc->sc_tx_stopped)
   1214   1.12  augustss 		goto out;
   1215   1.12  augustss 
   1216   1.82    martin 	if (!ttypull(tp))
   1217   1.73        ad 		goto out;
   1218   1.12  augustss 
   1219   1.12  augustss 	/* Grab the first contiguous region of buffer space. */
   1220   1.12  augustss 	data = tp->t_outq.c_cf;
   1221   1.12  augustss 	cnt = ndqb(&tp->t_outq, 0);
   1222   1.12  augustss 
   1223   1.84  christos 	if (cnt == 0)
   1224   1.12  augustss 		goto out;
   1225   1.12  augustss 
   1226   1.82    martin 	ub = SIMPLEQ_FIRST(&sc->sc_obuff_free);
   1227  1.100   mlelstv 	if (ub == NULL) {
   1228  1.100   mlelstv 		SET(tp->t_state, TS_BUSY);
   1229  1.100   mlelstv 		goto out;
   1230  1.100   mlelstv 	}
   1231  1.111     skrll 
   1232   1.82    martin 	SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_free, ub_link);
   1233   1.12  augustss 
   1234   1.82    martin 	if (SIMPLEQ_FIRST(&sc->sc_obuff_free) == NULL)
   1235   1.82    martin 		SET(tp->t_state, TS_BUSY);
   1236   1.82    martin 
   1237   1.82    martin 	if (cnt > sc->sc_obufsize)
   1238   1.19  augustss 		cnt = sc->sc_obufsize;
   1239   1.82    martin 
   1240   1.22  augustss 	if (sc->sc_methods->ucom_write != NULL)
   1241   1.22  augustss 		sc->sc_methods->ucom_write(sc->sc_parent, sc->sc_portno,
   1242  1.115     skrll 		    ub->ub_data, data, &cnt);
   1243   1.22  augustss 	else
   1244   1.82    martin 		memcpy(ub->ub_data, data, cnt);
   1245   1.82    martin 
   1246   1.82    martin 	ub->ub_len = cnt;
   1247   1.82    martin 	ub->ub_index = 0;
   1248   1.12  augustss 
   1249   1.82    martin 	SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_full, ub, ub_link);
   1250   1.82    martin 
   1251  1.122       mrg 	kpreempt_disable();
   1252   1.82    martin 	softint_schedule(sc->sc_si);
   1253  1.122       mrg 	kpreempt_enable();
   1254    1.3  augustss 
   1255   1.82    martin  out:
   1256  1.115     skrll 	DPRINTF("... done", 0, 0, 0, 0);
   1257  1.111     skrll 	return;
   1258   1.12  augustss }
   1259   1.12  augustss 
   1260   1.21    itojun void
   1261   1.69  christos ucomstop(struct tty *tp, int flag)
   1262   1.12  augustss {
   1263   1.19  augustss #if 0
   1264  1.115     skrll 	const int unit = UCOMUNIT(tp->t_dev);
   1265  1.115     skrll 	struct ucom_softc * const sc = device_lookup_private(&ucom_cd, unit);
   1266   1.12  augustss 
   1267  1.111     skrll 	mutex_enter(&sc->sc_lock);
   1268  1.134  riastrad 	ttylock(tp);
   1269   1.12  augustss 	if (ISSET(tp->t_state, TS_BUSY)) {
   1270  1.111     skrll 		/* obuff_full -> obuff_free? */
   1271   1.19  augustss 		/* sc->sc_tx_stopped = 1; */
   1272   1.12  augustss 		if (!ISSET(tp->t_state, TS_TTSTOP))
   1273   1.12  augustss 			SET(tp->t_state, TS_FLUSH);
   1274   1.12  augustss 	}
   1275  1.134  riastrad 	ttyunlock(tp);
   1276  1.111     skrll 	mutex_exit(&sc->sc_lock);
   1277   1.19  augustss #endif
   1278   1.12  augustss }
   1279   1.12  augustss 
   1280   1.82    martin static void
   1281   1.82    martin ucom_write_status(struct ucom_softc *sc, struct ucom_buffer *ub,
   1282   1.82    martin     usbd_status err)
   1283   1.82    martin {
   1284   1.82    martin 	struct tty *tp = sc->sc_tty;
   1285   1.82    martin 	uint32_t cc = ub->ub_len;
   1286   1.82    martin 
   1287  1.111     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
   1288  1.111     skrll 
   1289   1.82    martin 	switch (err) {
   1290   1.82    martin 	case USBD_IN_PROGRESS:
   1291   1.82    martin 		ub->ub_index = ub->ub_len;
   1292   1.82    martin 		break;
   1293   1.82    martin 	case USBD_STALLED:
   1294   1.82    martin 		ub->ub_index = 0;
   1295  1.122       mrg 		kpreempt_disable();
   1296   1.82    martin 		softint_schedule(sc->sc_si);
   1297  1.122       mrg 		kpreempt_enable();
   1298   1.82    martin 		break;
   1299   1.82    martin 	case USBD_NORMAL_COMPLETION:
   1300   1.82    martin 		usbd_get_xfer_status(ub->ub_xfer, NULL, NULL, &cc, NULL);
   1301   1.82    martin 		rnd_add_uint32(&sc->sc_rndsource, cc);
   1302   1.82    martin 		/*FALLTHROUGH*/
   1303   1.82    martin 	default:
   1304   1.82    martin 		SIMPLEQ_REMOVE_HEAD(&sc->sc_obuff_full, ub_link);
   1305   1.82    martin 		SIMPLEQ_INSERT_TAIL(&sc->sc_obuff_free, ub, ub_link);
   1306   1.82    martin 		cc -= sc->sc_opkthdrlen;
   1307   1.82    martin 
   1308  1.134  riastrad 		ttylock(tp);
   1309   1.82    martin 		CLR(tp->t_state, TS_BUSY);
   1310   1.82    martin 		if (ISSET(tp->t_state, TS_FLUSH))
   1311   1.82    martin 			CLR(tp->t_state, TS_FLUSH);
   1312   1.82    martin 		else
   1313   1.82    martin 			ndflush(&tp->t_outq, cc);
   1314  1.134  riastrad 		ttyunlock(tp);
   1315   1.82    martin 
   1316   1.82    martin 		if (err != USBD_CANCELLED && err != USBD_IOERROR &&
   1317  1.130  riastrad 		    !sc->sc_closing) {
   1318   1.82    martin 			if ((ub = SIMPLEQ_FIRST(&sc->sc_obuff_full)) != NULL)
   1319   1.82    martin 				ucom_submit_write(sc, ub);
   1320   1.82    martin 
   1321  1.134  riastrad 			ttylock(tp);
   1322   1.82    martin 			(*tp->t_linesw->l_start)(tp);
   1323  1.134  riastrad 			ttyunlock(tp);
   1324   1.82    martin 		}
   1325   1.82    martin 		break;
   1326   1.82    martin 	}
   1327   1.82    martin }
   1328   1.82    martin 
   1329   1.82    martin static void
   1330   1.82    martin ucom_submit_write(struct ucom_softc *sc, struct ucom_buffer *ub)
   1331   1.82    martin {
   1332   1.82    martin 
   1333  1.111     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
   1334  1.111     skrll 
   1335  1.111     skrll 	usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, ub->ub_len,
   1336  1.111     skrll 	    0, USBD_NO_TIMEOUT, ucomwritecb);
   1337   1.82    martin 
   1338   1.82    martin 	ucom_write_status(sc, ub, usbd_transfer(ub->ub_xfer));
   1339   1.82    martin }
   1340   1.82    martin 
   1341   1.82    martin static void
   1342  1.111     skrll ucomwritecb(struct usbd_xfer *xfer, void *p, usbd_status status)
   1343   1.12  augustss {
   1344   1.12  augustss 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1345   1.82    martin 
   1346  1.111     skrll 	mutex_enter(&sc->sc_lock);
   1347   1.82    martin 	ucom_write_status(sc, SIMPLEQ_FIRST(&sc->sc_obuff_full), status);
   1348  1.111     skrll 	mutex_exit(&sc->sc_lock);
   1349   1.82    martin 
   1350   1.82    martin }
   1351   1.82    martin 
   1352   1.82    martin static void
   1353   1.82    martin ucom_softintr(void *arg)
   1354   1.82    martin {
   1355  1.115     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1356  1.115     skrll 
   1357   1.82    martin 	struct ucom_softc *sc = arg;
   1358  1.130  riastrad 	struct tty *tp = sc->sc_tty;
   1359   1.12  augustss 
   1360  1.111     skrll 	mutex_enter(&sc->sc_lock);
   1361  1.134  riastrad 	ttylock(tp);
   1362  1.111     skrll 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
   1363  1.134  riastrad 		ttyunlock(tp);
   1364  1.111     skrll 		mutex_exit(&sc->sc_lock);
   1365   1.82    martin 		return;
   1366  1.111     skrll 	}
   1367  1.134  riastrad 	ttyunlock(tp);
   1368   1.12  augustss 
   1369  1.115     skrll 	struct ucom_buffer *ub = SIMPLEQ_FIRST(&sc->sc_obuff_full);
   1370   1.82    martin 
   1371   1.82    martin 	if (ub != NULL && ub->ub_index == 0)
   1372   1.82    martin 		ucom_submit_write(sc, ub);
   1373   1.12  augustss 
   1374   1.82    martin 	if (sc->sc_rx_unblock)
   1375   1.82    martin 		ucom_read_complete(sc);
   1376   1.12  augustss 
   1377  1.111     skrll 	mutex_exit(&sc->sc_lock);
   1378   1.82    martin }
   1379   1.82    martin 
   1380   1.82    martin static void
   1381   1.82    martin ucom_read_complete(struct ucom_softc *sc)
   1382   1.82    martin {
   1383   1.82    martin 	int (*rint)(int, struct tty *);
   1384   1.82    martin 	struct ucom_buffer *ub;
   1385   1.82    martin 	struct tty *tp;
   1386  1.111     skrll 
   1387  1.111     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
   1388   1.82    martin 
   1389   1.82    martin 	tp = sc->sc_tty;
   1390   1.82    martin 	rint = tp->t_linesw->l_rint;
   1391   1.82    martin 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1392   1.39  augustss 
   1393   1.82    martin 	while (ub != NULL && !sc->sc_rx_stopped) {
   1394   1.82    martin 
   1395  1.134  riastrad 		/* XXX ttyinput takes ttylock */
   1396   1.82    martin 		while (ub->ub_index < ub->ub_len && !sc->sc_rx_stopped) {
   1397   1.82    martin 			/* Give characters to tty layer. */
   1398   1.82    martin 			if ((*rint)(ub->ub_data[ub->ub_index], tp) == -1) {
   1399   1.82    martin 				/* Overflow: drop remainder */
   1400   1.82    martin 				ub->ub_index = ub->ub_len;
   1401   1.82    martin 			} else
   1402   1.82    martin 				ub->ub_index++;
   1403   1.82    martin 		}
   1404   1.82    martin 
   1405   1.82    martin 		if (ub->ub_index == ub->ub_len) {
   1406   1.82    martin 			SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_full, ub_link);
   1407   1.82    martin 			ucomsubmitread(sc, ub);
   1408   1.82    martin 			ub = SIMPLEQ_FIRST(&sc->sc_ibuff_full);
   1409   1.82    martin 		}
   1410   1.82    martin 	}
   1411   1.82    martin 
   1412   1.82    martin 	sc->sc_rx_unblock = (ub != NULL);
   1413   1.12  augustss }
   1414   1.12  augustss 
   1415  1.115     skrll static int
   1416   1.82    martin ucomsubmitread(struct ucom_softc *sc, struct ucom_buffer *ub)
   1417   1.12  augustss {
   1418   1.12  augustss 	usbd_status err;
   1419   1.12  augustss 
   1420  1.115     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
   1421  1.115     skrll 
   1422  1.111     skrll 	usbd_setup_xfer(ub->ub_xfer, sc, ub->ub_data, sc->sc_ibufsize,
   1423  1.111     skrll 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, ucomreadcb);
   1424   1.82    martin 
   1425   1.82    martin 	if ((err = usbd_transfer(ub->ub_xfer)) != USBD_IN_PROGRESS) {
   1426   1.82    martin 		/* XXX: Recover from this, please! */
   1427  1.140       bad 		device_printf(sc->sc_dev, "%s: err=%s\n",
   1428  1.140       bad 		    __func__, usbd_errstr(err));
   1429  1.115     skrll 		return EIO;
   1430   1.12  augustss 	}
   1431   1.82    martin 
   1432   1.82    martin 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_empty, ub, ub_link);
   1433   1.82    martin 
   1434  1.115     skrll 	return 0;
   1435   1.12  augustss }
   1436   1.43  augustss 
   1437   1.82    martin static void
   1438  1.111     skrll ucomreadcb(struct usbd_xfer *xfer, void *p, usbd_status status)
   1439   1.12  augustss {
   1440   1.12  augustss 	struct ucom_softc *sc = (struct ucom_softc *)p;
   1441   1.82    martin 	struct ucom_buffer *ub;
   1442  1.111     skrll 	uint32_t cc;
   1443   1.12  augustss 	u_char *cp;
   1444   1.12  augustss 
   1445  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1446  1.111     skrll 
   1447  1.115     skrll 	mutex_enter(&sc->sc_lock);
   1448  1.115     skrll 
   1449  1.115     skrll 	if (status == USBD_CANCELLED || status == USBD_IOERROR ||
   1450  1.130  riastrad 	    sc->sc_closing) {
   1451  1.130  riastrad 		DPRINTF("... done (status %jd closing %jd)",
   1452  1.130  riastrad 		    status, sc->sc_closing, 0, 0);
   1453  1.111     skrll 		mutex_exit(&sc->sc_lock);
   1454   1.12  augustss 		return;
   1455   1.34  augustss 	}
   1456   1.12  augustss 
   1457  1.112     skrll 	ub = SIMPLEQ_FIRST(&sc->sc_ibuff_empty);
   1458  1.112     skrll 	SIMPLEQ_REMOVE_HEAD(&sc->sc_ibuff_empty, ub_link);
   1459  1.112     skrll 
   1460  1.115     skrll 	if (status != USBD_NORMAL_COMPLETION) {
   1461  1.115     skrll 		if (status == USBD_STALLED) {
   1462  1.115     skrll 			usbd_clear_endpoint_stall_async(sc->sc_bulkin_pipe);
   1463  1.115     skrll 		} else {
   1464  1.140       bad 			device_printf(sc->sc_dev,
   1465  1.140       bad 			    "ucomreadcb: wonky status=%s\n",
   1466  1.115     skrll 			    usbd_errstr(status));
   1467  1.115     skrll 		}
   1468  1.115     skrll 
   1469  1.119  pgoyette 		DPRINTF("... done (status %jd)", status, 0, 0, 0);
   1470  1.130  riastrad 		/* re-adds ub to sc_ibuff_empty */
   1471   1.82    martin 		ucomsubmitread(sc, ub);
   1472  1.111     skrll 		mutex_exit(&sc->sc_lock);
   1473   1.82    martin 		return;
   1474   1.82    martin 	}
   1475   1.82    martin 
   1476   1.48   thorpej 	usbd_get_xfer_status(xfer, NULL, (void *)&cp, &cc, NULL);
   1477   1.82    martin 
   1478   1.95  jakllsch #ifdef UCOM_DEBUG
   1479   1.95  jakllsch 	/* This is triggered by uslsa(4) occasionally. */
   1480   1.95  jakllsch 	if ((ucomdebug > 0) && (cc == 0)) {
   1481   1.95  jakllsch 		device_printf(sc->sc_dev, "ucomreadcb: zero length xfer!\n");
   1482   1.82    martin 	}
   1483   1.95  jakllsch #endif
   1484   1.82    martin 	KDASSERT(cp == ub->ub_data);
   1485   1.82    martin 
   1486   1.31  explorer 	rnd_add_uint32(&sc->sc_rndsource, cc);
   1487   1.82    martin 
   1488  1.115     skrll 	if (sc->sc_state != UCOM_OPEN) {
   1489  1.115     skrll 		/* Go around again - we're not quite ready */
   1490  1.130  riastrad 		/* re-adds ub to sc_ibuff_empty */
   1491   1.82    martin 		ucomsubmitread(sc, ub);
   1492  1.111     skrll 		mutex_exit(&sc->sc_lock);
   1493  1.115     skrll 		DPRINTF("... done (not open)", 0, 0, 0, 0);
   1494   1.82    martin 		return;
   1495   1.82    martin 	}
   1496   1.82    martin 
   1497  1.118     skrll 	mutex_exit(&sc->sc_lock);
   1498   1.82    martin 	if (sc->sc_methods->ucom_read != NULL) {
   1499   1.22  augustss 		sc->sc_methods->ucom_read(sc->sc_parent, sc->sc_portno,
   1500   1.82    martin 		    &cp, &cc);
   1501   1.82    martin 		ub->ub_index = (u_int)(cp - ub->ub_data);
   1502   1.82    martin 	} else
   1503   1.82    martin 		ub->ub_index = 0;
   1504   1.82    martin 
   1505   1.82    martin 	ub->ub_len = cc;
   1506   1.22  augustss 
   1507  1.118     skrll 	mutex_enter(&sc->sc_lock);
   1508   1.82    martin 	SIMPLEQ_INSERT_TAIL(&sc->sc_ibuff_full, ub, ub_link);
   1509   1.82    martin 	ucom_read_complete(sc);
   1510  1.111     skrll 	mutex_exit(&sc->sc_lock);
   1511  1.115     skrll 
   1512  1.115     skrll 	DPRINTF("... done", 0, 0, 0, 0);
   1513   1.12  augustss }
   1514   1.12  augustss 
   1515   1.82    martin static void
   1516  1.130  riastrad ucom_cleanup(struct ucom_softc *sc, int flag)
   1517   1.12  augustss {
   1518  1.130  riastrad 	struct tty *tp = sc->sc_tty;
   1519   1.82    martin 
   1520  1.111     skrll 	UCOMHIST_FUNC(); UCOMHIST_CALLED();
   1521  1.111     skrll 
   1522  1.130  riastrad 	DPRINTF("closing pipes", 0, 0, 0, 0);
   1523  1.111     skrll 
   1524  1.130  riastrad 	/*
   1525  1.130  riastrad 	 * Close the tty and interrupt any pending opens waiting for
   1526  1.130  riastrad 	 * carrier so they restart or give up.  This may flush data.
   1527  1.130  riastrad 	 */
   1528  1.130  riastrad 	(*tp->t_linesw->l_close)(tp, flag);
   1529  1.130  riastrad 	ttyclose(tp);
   1530  1.115     skrll 
   1531  1.130  riastrad 	/*
   1532  1.130  riastrad 	 * Interrupt any pending xfers and cause them to fail promptly.
   1533  1.130  riastrad 	 * New xfers will only be submitted under the lock after
   1534  1.130  riastrad 	 * sc_closing is cleared.
   1535  1.130  riastrad 	 */
   1536  1.130  riastrad 	usbd_abort_pipe(sc->sc_bulkin_pipe);
   1537  1.130  riastrad 	usbd_abort_pipe(sc->sc_bulkout_pipe);
   1538  1.116     skrll 
   1539  1.130  riastrad 	/*
   1540  1.130  riastrad 	 * Hang up the phone and start the timer before we can make a
   1541  1.130  riastrad 	 * call again, if necessary.
   1542  1.130  riastrad 	 */
   1543   1.12  augustss 	ucom_shutdown(sc);
   1544   1.12  augustss }
   1545   1.13  augustss 
   1546   1.13  augustss #endif /* NUCOM > 0 */
   1547   1.12  augustss 
   1548   1.20  augustss int
   1549   1.24  augustss ucomprint(void *aux, const char *pnp)
   1550   1.12  augustss {
   1551  1.111     skrll 	struct ucom_attach_args *ucaa = aux;
   1552   1.12  augustss 
   1553   1.12  augustss 	if (pnp)
   1554   1.49   thorpej 		aprint_normal("ucom at %s", pnp);
   1555  1.111     skrll 	if (ucaa->ucaa_portno != UCOM_UNK_PORTNO)
   1556  1.111     skrll 		aprint_normal(" portno %d", ucaa->ucaa_portno);
   1557  1.111     skrll 	return UNCONF;
   1558   1.12  augustss }
   1559   1.12  augustss 
   1560   1.20  augustss int
   1561   1.77      cube ucomsubmatch(device_t parent, cfdata_t cf,
   1562   1.69  christos 	     const int *ldesc, void *aux)
   1563   1.12  augustss {
   1564  1.111     skrll 	struct ucom_attach_args *ucaa = aux;
   1565   1.12  augustss 
   1566  1.111     skrll 	if (ucaa->ucaa_portno != UCOM_UNK_PORTNO &&
   1567   1.53  drochner 	    cf->cf_loc[UCOMBUSCF_PORTNO] != UCOMBUSCF_PORTNO_DEFAULT &&
   1568  1.111     skrll 	    cf->cf_loc[UCOMBUSCF_PORTNO] != ucaa->ucaa_portno)
   1569  1.111     skrll 		return 0;
   1570  1.111     skrll 	return config_match(parent, cf, aux);
   1571   1.12  augustss }
   1572