Home | History | Annotate | Line # | Download | only in usb
ucycom.c revision 1.46.4.2
      1  1.46.4.2    martin /*	$NetBSD: ucycom.c,v 1.46.4.2 2020/04/08 14:08:13 martin Exp $	*/
      2       1.1     skrll 
      3       1.1     skrll /*
      4       1.1     skrll  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5       1.1     skrll  * All rights reserved.
      6       1.1     skrll  *
      7       1.1     skrll  * This code is derived from software contributed to The NetBSD Foundation
      8      1.26     skrll  * by Nick Hudson
      9       1.1     skrll  *
     10       1.1     skrll  * Redistribution and use in source and binary forms, with or without
     11       1.1     skrll  * modification, are permitted provided that the following conditions
     12       1.1     skrll  * are met:
     13       1.1     skrll  * 1. Redistributions of source code must retain the above copyright
     14       1.1     skrll  *    notice, this list of conditions and the following disclaimer.
     15       1.1     skrll  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1     skrll  *    notice, this list of conditions and the following disclaimer in the
     17       1.1     skrll  *    documentation and/or other materials provided with the distribution.
     18       1.1     skrll  *
     19       1.1     skrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1     skrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1     skrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1     skrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1     skrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1     skrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1     skrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1     skrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1     skrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1     skrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1     skrll  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1     skrll  */
     31       1.1     skrll /*
     32       1.1     skrll  * This code is based on the ucom driver.
     33       1.1     skrll  */
     34       1.1     skrll 
     35       1.1     skrll /*
     36       1.1     skrll  * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
     37       1.1     skrll  * RS232 bridges.
     38       1.1     skrll  */
     39       1.1     skrll 
     40       1.1     skrll #include <sys/cdefs.h>
     41  1.46.4.2    martin __KERNEL_RCSID(0, "$NetBSD: ucycom.c,v 1.46.4.2 2020/04/08 14:08:13 martin Exp $");
     42      1.45     skrll 
     43      1.45     skrll #ifdef _KERNEL_OPT
     44      1.45     skrll #include "opt_usb.h"
     45      1.45     skrll #endif
     46       1.1     skrll 
     47       1.1     skrll #include <sys/param.h>
     48       1.1     skrll #include <sys/systm.h>
     49       1.1     skrll #include <sys/conf.h>
     50       1.1     skrll #include <sys/kernel.h>
     51      1.43     skrll #include <sys/kmem.h>
     52       1.3     skrll #include <sys/device.h>
     53       1.1     skrll #include <sys/sysctl.h>
     54       1.1     skrll #include <sys/tty.h>
     55       1.1     skrll #include <sys/file.h>
     56       1.1     skrll #include <sys/vnode.h>
     57      1.10      elad #include <sys/kauth.h>
     58      1.32  uebayasi #include <sys/lwp.h>
     59       1.1     skrll 
     60       1.1     skrll #include <dev/usb/usb.h>
     61       1.1     skrll #include <dev/usb/usbhid.h>
     62       1.1     skrll 
     63       1.1     skrll #include <dev/usb/usbdi.h>
     64       1.1     skrll #include <dev/usb/usbdi_util.h>
     65       1.1     skrll #include <dev/usb/usbdevs.h>
     66       1.1     skrll #include <dev/usb/uhidev.h>
     67      1.46    bouyer #include <dev/hid/hid.h>
     68       1.1     skrll 
     69       1.1     skrll #include "ioconf.h"
     70       1.1     skrll 
     71       1.1     skrll #ifdef UCYCOM_DEBUG
     72      1.31    dyoung #define DPRINTF(x)	if (ucycomdebug) printf x
     73      1.31    dyoung #define DPRINTFN(n, x)	if (ucycomdebug > (n)) printf x
     74      1.29     skrll int	ucycomdebug = 20;
     75       1.1     skrll #else
     76       1.1     skrll #define DPRINTF(x)
     77       1.1     skrll #define DPRINTFN(n,x)
     78       1.1     skrll #endif
     79       1.1     skrll 
     80       1.1     skrll 
     81      1.41  christos #define	UCYCOMCALLUNIT_MASK	TTCALLUNIT_MASK
     82      1.41  christos #define	UCYCOMUNIT_MASK		TTUNIT_MASK
     83      1.41  christos #define	UCYCOMDIALOUT_MASK	TTDIALOUT_MASK
     84      1.41  christos 
     85      1.41  christos #define	UCYCOMCALLUNIT(x)	TTCALLUNIT(x)
     86      1.41  christos #define	UCYCOMUNIT(x)		TTUNIT(x)
     87      1.41  christos #define	UCYCOMDIALOUT(x)	TTDIALOUT(x)
     88       1.1     skrll 
     89       1.1     skrll /* Configuration Byte */
     90       1.1     skrll #define UCYCOM_RESET		0x80
     91       1.1     skrll #define UCYCOM_PARITY_TYPE_MASK	0x20
     92       1.1     skrll #define  UCYCOM_PARITY_ODD	 0x20
     93       1.1     skrll #define  UCYCOM_PARITY_EVEN	 0x00
     94       1.1     skrll #define UCYCOM_PARITY_MASK	0x10
     95       1.1     skrll #define  UCYCOM_PARITY_ON	 0x10
     96       1.1     skrll #define  UCYCOM_PARITY_OFF	 0x00
     97       1.1     skrll #define UCYCOM_STOP_MASK	0x08
     98       1.1     skrll #define  UCYCOM_STOP_BITS_2	 0x08
     99       1.1     skrll #define  UCYCOM_STOP_BITS_1	 0x00
    100       1.1     skrll #define UCYCOM_DATA_MASK	0x03
    101       1.1     skrll #define  UCYCOM_DATA_BITS_8	 0x03
    102       1.1     skrll #define  UCYCOM_DATA_BITS_7	 0x02
    103       1.1     skrll #define  UCYCOM_DATA_BITS_6	 0x01
    104       1.1     skrll #define  UCYCOM_DATA_BITS_5	 0x00
    105       1.1     skrll 
    106       1.1     skrll /* Modem (Input) status byte */
    107       1.1     skrll #define UCYCOM_RI	0x80
    108       1.1     skrll #define UCYCOM_DCD	0x40
    109       1.1     skrll #define UCYCOM_DSR	0x20
    110       1.1     skrll #define UCYCOM_CTS	0x10
    111       1.1     skrll #define UCYCOM_ERROR	0x08
    112       1.1     skrll #define UCYCOM_LMASK	0x07
    113       1.1     skrll 
    114       1.1     skrll /* Modem (Output) control byte */
    115       1.1     skrll #define UCYCOM_DTR	0x20
    116       1.1     skrll #define UCYCOM_RTS	0x10
    117       1.1     skrll #define UCYCOM_ORESET	0x08
    118       1.1     skrll 
    119       1.1     skrll struct ucycom_softc {
    120       1.1     skrll 	struct uhidev		sc_hdev;
    121       1.1     skrll 
    122       1.1     skrll 	struct tty		*sc_tty;
    123       1.1     skrll 
    124  1.46.4.2    martin 	enum {
    125  1.46.4.2    martin 		UCYCOM_INIT_NONE,
    126  1.46.4.2    martin 		UCYCOM_INIT_INITED
    127  1.46.4.2    martin 	} sc_init_state;
    128  1.46.4.2    martin 
    129      1.35     skrll 	kmutex_t sc_lock;	/* protects refcnt, others */
    130      1.35     skrll 
    131       1.1     skrll 	/* uhidev parameters */
    132       1.1     skrll 	size_t			sc_flen; /* feature report length */
    133       1.1     skrll 	size_t			sc_olen; /* output report length */
    134       1.1     skrll 
    135       1.1     skrll 	uint8_t			*sc_obuf;
    136      1.29     skrll 	int			sc_wlen;
    137       1.1     skrll 
    138       1.1     skrll 	/* settings */
    139       1.1     skrll 	uint32_t		sc_baud;
    140       1.1     skrll 	uint8_t			sc_cfg;	/* Data format */
    141       1.1     skrll 	uint8_t			sc_mcr;	/* Modem control */
    142       1.1     skrll 	uint8_t			sc_msr;	/* Modem status */
    143       1.1     skrll 	int			sc_swflags;
    144       1.1     skrll 
    145       1.1     skrll 	/* flags */
    146       1.1     skrll 	char			sc_dying;
    147       1.1     skrll };
    148       1.1     skrll 
    149  1.46.4.2    martin static dev_type_open(ucycomopen);
    150  1.46.4.2    martin static dev_type_close(ucycomclose);
    151  1.46.4.2    martin static dev_type_read(ucycomread);
    152  1.46.4.2    martin static dev_type_write(ucycomwrite);
    153  1.46.4.2    martin static dev_type_ioctl(ucycomioctl);
    154  1.46.4.2    martin static dev_type_stop(ucycomstop);
    155  1.46.4.2    martin static dev_type_tty(ucycomtty);
    156  1.46.4.2    martin static dev_type_poll(ucycompoll);
    157       1.1     skrll 
    158       1.1     skrll const struct cdevsw ucycom_cdevsw = {
    159      1.38  dholland 	.d_open = ucycomopen,
    160      1.38  dholland 	.d_close = ucycomclose,
    161      1.38  dholland 	.d_read = ucycomread,
    162      1.38  dholland 	.d_write = ucycomwrite,
    163      1.38  dholland 	.d_ioctl = ucycomioctl,
    164      1.38  dholland 	.d_stop = ucycomstop,
    165      1.38  dholland 	.d_tty = ucycomtty,
    166      1.38  dholland 	.d_poll = ucycompoll,
    167      1.38  dholland 	.d_mmap = nommap,
    168      1.38  dholland 	.d_kqfilter = ttykqfilter,
    169      1.39  dholland 	.d_discard = nodiscard,
    170      1.38  dholland 	.d_flag = D_TTY
    171       1.1     skrll };
    172       1.1     skrll 
    173       1.1     skrll Static int ucycomparam(struct tty *, struct termios *);
    174       1.1     skrll Static void ucycomstart(struct tty *);
    175      1.43     skrll Static void ucycomwritecb(struct usbd_xfer *, void *, usbd_status);
    176       1.1     skrll Static void ucycom_intr(struct uhidev *, void *, u_int);
    177       1.1     skrll Static int ucycom_configure(struct ucycom_softc *, uint32_t, uint8_t);
    178       1.1     skrll Static void tiocm_to_ucycom(struct ucycom_softc *, u_long, int);
    179       1.1     skrll Static int ucycom_to_tiocm(struct ucycom_softc *);
    180       1.1     skrll Static void ucycom_set_status(struct ucycom_softc *);
    181       1.1     skrll Static void ucycom_dtr(struct ucycom_softc *, int);
    182       1.1     skrll #if 0
    183       1.1     skrll Static void ucycom_rts(struct ucycom_softc *, int);
    184       1.1     skrll #endif
    185      1.43     skrll Static void ucycom_cleanup(struct ucycom_softc *);
    186       1.1     skrll 
    187       1.1     skrll #ifdef UCYCOM_DEBUG
    188       1.1     skrll Static void ucycom_get_cfg(struct ucycom_softc *);
    189       1.1     skrll #endif
    190       1.1     skrll 
    191       1.1     skrll Static const struct usb_devno ucycom_devs[] = {
    192       1.1     skrll 	{ USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 },
    193       1.1     skrll 	{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE },
    194       1.1     skrll };
    195       1.1     skrll #define ucycom_lookup(v, p) usb_lookup(ucycom_devs, v, p)
    196       1.1     skrll 
    197  1.46.4.2    martin static int	ucycom_match(device_t, cfdata_t, void *);
    198  1.46.4.2    martin static void	ucycom_attach(device_t, device_t, void *);
    199  1.46.4.2    martin static int	ucycom_detach(device_t, int);
    200  1.46.4.2    martin static int	ucycom_activate(device_t, enum devact);
    201  1.46.4.1  christos 
    202      1.44   msaitoh CFATTACH_DECL_NEW(ucycom, sizeof(struct ucycom_softc), ucycom_match,
    203      1.44   msaitoh     ucycom_attach, ucycom_detach, ucycom_activate);
    204       1.1     skrll 
    205  1.46.4.2    martin static int
    206      1.23      cube ucycom_match(device_t parent, cfdata_t match, void *aux)
    207       1.1     skrll {
    208       1.1     skrll 	struct uhidev_attach_arg *uha = aux;
    209       1.1     skrll 
    210      1.44   msaitoh 	return ucycom_lookup(uha->uiaa->uiaa_vendor, uha->uiaa->uiaa_product)
    211      1.44   msaitoh 	    != NULL ? UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    212       1.1     skrll }
    213       1.1     skrll 
    214  1.46.4.2    martin static void
    215      1.23      cube ucycom_attach(device_t parent, device_t self, void *aux)
    216       1.1     skrll {
    217      1.23      cube 	struct ucycom_softc *sc = device_private(self);
    218       1.1     skrll 	struct uhidev_attach_arg *uha = aux;
    219       1.1     skrll 	int size, repid;
    220       1.1     skrll 	void *desc;
    221       1.1     skrll 
    222      1.23      cube 	sc->sc_hdev.sc_dev = self;
    223       1.1     skrll 	sc->sc_hdev.sc_intr = ucycom_intr;
    224       1.1     skrll 	sc->sc_hdev.sc_parent = uha->parent;
    225       1.1     skrll 	sc->sc_hdev.sc_report_id = uha->reportid;
    226  1.46.4.2    martin 	sc->sc_init_state = UCYCOM_INIT_NONE;
    227       1.1     skrll 
    228       1.1     skrll 	uhidev_get_report_desc(uha->parent, &desc, &size);
    229       1.1     skrll 	repid = uha->reportid;
    230       1.1     skrll 	sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
    231       1.1     skrll 	sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);
    232       1.1     skrll 
    233  1.46.4.2    martin 	if (sc->sc_olen != 8 && sc->sc_olen != 32)
    234  1.46.4.2    martin 		return;
    235  1.46.4.2    martin 	if (sc->sc_flen != 5)
    236  1.46.4.2    martin 		return;
    237  1.46.4.2    martin 
    238       1.1     skrll 	sc->sc_msr = sc->sc_mcr = 0;
    239       1.1     skrll 
    240       1.1     skrll 	/* set up tty */
    241      1.33     rmind 	sc->sc_tty = tty_alloc();
    242       1.1     skrll 	sc->sc_tty->t_sc = sc;
    243       1.1     skrll 	sc->sc_tty->t_oproc = ucycomstart;
    244       1.1     skrll 	sc->sc_tty->t_param = ucycomparam;
    245       1.1     skrll 
    246       1.1     skrll 	tty_attach(sc->sc_tty);
    247       1.1     skrll 
    248       1.4     skrll 	/* Nothing interesting to report */
    249      1.23      cube 	aprint_normal("\n");
    250  1.46.4.2    martin 
    251  1.46.4.2    martin 	sc->sc_init_state = UCYCOM_INIT_INITED;
    252       1.1     skrll }
    253       1.1     skrll 
    254       1.1     skrll 
    255  1.46.4.2    martin static int
    256      1.23      cube ucycom_detach(device_t self, int flags)
    257       1.1     skrll {
    258      1.23      cube 	struct ucycom_softc *sc = device_private(self);
    259       1.1     skrll 	struct tty *tp = sc->sc_tty;
    260       1.1     skrll 	int maj, mn;
    261       1.1     skrll 	int s;
    262       1.1     skrll 
    263       1.1     skrll 	DPRINTF(("ucycom_detach: sc=%p flags=%d tp=%p\n", sc, flags, tp));
    264       1.1     skrll 
    265       1.1     skrll 	sc->sc_dying = 1;
    266       1.1     skrll 
    267       1.1     skrll 	s = splusb();
    268       1.1     skrll 	if (tp != NULL) {
    269      1.18        ad 		mutex_spin_enter(&tty_lock);
    270       1.1     skrll 		CLR(tp->t_state, TS_CARR_ON);
    271       1.1     skrll 		CLR(tp->t_cflag, CLOCAL | MDMBUF);
    272       1.1     skrll 		ttyflush(tp, FREAD|FWRITE);
    273      1.18        ad 		mutex_spin_exit(&tty_lock);
    274       1.1     skrll 	}
    275       1.1     skrll 	/* Wait for processes to go away. */
    276      1.34       mrg 	usb_detach_waitold(sc->sc_hdev.sc_dev);
    277       1.1     skrll 	splx(s);
    278       1.1     skrll 
    279       1.1     skrll 	/* locate the major number */
    280       1.1     skrll 	maj = cdevsw_lookup_major(&ucycom_cdevsw);
    281       1.1     skrll 
    282       1.1     skrll 	/* Nuke the vnodes for any open instances. */
    283       1.9   thorpej 	mn = device_unit(self);
    284       1.1     skrll 
    285       1.1     skrll 	DPRINTFN(2, ("ucycom_detach: maj=%d mn=%d\n", maj, mn));
    286       1.1     skrll 	vdevgone(maj, mn, mn, VCHR);
    287       1.1     skrll 	vdevgone(maj, mn | UCYCOMDIALOUT_MASK, mn | UCYCOMDIALOUT_MASK, VCHR);
    288       1.1     skrll 	vdevgone(maj, mn | UCYCOMCALLUNIT_MASK, mn | UCYCOMCALLUNIT_MASK, VCHR);
    289       1.1     skrll 
    290       1.1     skrll 	/* Detach and free the tty. */
    291       1.1     skrll 	if (tp != NULL) {
    292       1.1     skrll 		DPRINTF(("ucycom_detach: tty_detach %p\n", tp));
    293       1.1     skrll 		tty_detach(tp);
    294      1.33     rmind 		tty_free(tp);
    295       1.1     skrll 		sc->sc_tty = NULL;
    296       1.1     skrll 	}
    297       1.1     skrll 
    298       1.1     skrll 	return 0;
    299       1.1     skrll }
    300       1.1     skrll 
    301  1.46.4.2    martin static int
    302      1.31    dyoung ucycom_activate(device_t self, enum devact act)
    303       1.1     skrll {
    304      1.23      cube 	struct ucycom_softc *sc = device_private(self);
    305       1.1     skrll 
    306       1.1     skrll 	DPRINTFN(5,("ucycom_activate: %d\n", act));
    307       1.1     skrll 
    308       1.1     skrll 	switch (act) {
    309       1.1     skrll 	case DVACT_DEACTIVATE:
    310       1.1     skrll 		sc->sc_dying = 1;
    311      1.30    dyoung 		return 0;
    312      1.30    dyoung 	default:
    313      1.30    dyoung 		return EOPNOTSUPP;
    314       1.1     skrll 	}
    315       1.1     skrll }
    316       1.1     skrll 
    317       1.1     skrll #if 0
    318       1.1     skrll void
    319       1.1     skrll ucycom_shutdown(struct ucycom_softc *sc)
    320       1.1     skrll {
    321       1.1     skrll 	struct tty *tp = sc->sc_tty;
    322       1.1     skrll 
    323       1.1     skrll 	DPRINTF(("ucycom_shutdown\n"));
    324       1.1     skrll 	/*
    325       1.1     skrll 	 * Hang up if necessary.  Wait a bit, so the other side has time to
    326       1.1     skrll 	 * notice even if we immediately open the port again.
    327       1.1     skrll 	 */
    328       1.1     skrll 	if (ISSET(tp->t_cflag, HUPCL)) {
    329       1.1     skrll 		ucycom_dtr(sc, 0);
    330       1.1     skrll 		(void)tsleep(sc, TTIPRI, ttclos, hz);
    331       1.1     skrll 	}
    332       1.1     skrll }
    333       1.1     skrll #endif
    334       1.1     skrll 
    335  1.46.4.2    martin static int
    336      1.16  christos ucycomopen(dev_t dev, int flag, int mode, struct lwp *l)
    337       1.1     skrll {
    338      1.23      cube 	struct ucycom_softc *sc =
    339      1.23      cube 	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
    340       1.1     skrll 	struct tty *tp;
    341       1.1     skrll 	int s, err;
    342       1.1     skrll 
    343      1.25  drochner 	DPRINTF(("ucycomopen: unit=%d\n", UCYCOMUNIT(dev)));
    344       1.1     skrll 	DPRINTF(("ucycomopen: sc=%p\n", sc));
    345      1.26     skrll 
    346       1.1     skrll 	if (sc == NULL)
    347      1.43     skrll 		return ENXIO;
    348       1.1     skrll 	if (sc->sc_dying)
    349      1.43     skrll 		return EIO;
    350  1.46.4.2    martin 	if (sc->sc_init_state != UCYCOM_INIT_INITED)
    351  1.46.4.2    martin 		return ENXIO;
    352      1.23      cube 	if (!device_is_active(sc->sc_hdev.sc_dev))
    353      1.43     skrll 		return ENXIO;
    354       1.1     skrll 
    355       1.1     skrll 	tp = sc->sc_tty;
    356       1.1     skrll 
    357       1.1     skrll 	DPRINTF(("ucycomopen: tp=%p\n", tp));
    358       1.1     skrll 
    359      1.12      elad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    360      1.43     skrll 		return EBUSY;
    361       1.1     skrll 
    362       1.1     skrll 	s = spltty();
    363       1.1     skrll 
    364       1.1     skrll 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    365       1.1     skrll 		struct termios t;
    366       1.1     skrll 
    367       1.1     skrll 		tp->t_dev = dev;
    368       1.1     skrll 
    369       1.1     skrll 		err = uhidev_open(&sc->sc_hdev);
    370       1.1     skrll 		if (err) {
    371       1.1     skrll 			/* Any cleanup? */
    372       1.1     skrll 			splx(s);
    373      1.43     skrll 			return err;
    374       1.1     skrll 		}
    375       1.1     skrll 
    376       1.1     skrll 		/*
    377       1.1     skrll 		 * Initialize the termios status to the defaults.  Add in the
    378       1.1     skrll 		 * sticky bits from TIOCSFLAGS.
    379       1.1     skrll 		 */
    380       1.1     skrll 		t.c_ispeed = 0;
    381       1.1     skrll 		t.c_ospeed = TTYDEF_SPEED;
    382       1.1     skrll 		t.c_cflag = TTYDEF_CFLAG;
    383       1.1     skrll 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
    384       1.1     skrll 			SET(t.c_cflag, CLOCAL);
    385       1.1     skrll 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
    386       1.1     skrll 			SET(t.c_cflag, CRTSCTS);
    387       1.1     skrll 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
    388       1.1     skrll 			SET(t.c_cflag, MDMBUF);
    389      1.27     skrll 
    390       1.1     skrll 		tp->t_ospeed = 0;
    391       1.1     skrll 		(void) ucycomparam(tp, &t);
    392       1.1     skrll 		tp->t_iflag = TTYDEF_IFLAG;
    393       1.1     skrll 		tp->t_oflag = TTYDEF_OFLAG;
    394       1.1     skrll 		tp->t_lflag = TTYDEF_LFLAG;
    395       1.1     skrll 		ttychars(tp);
    396       1.1     skrll 		ttsetwater(tp);
    397       1.1     skrll 
    398       1.1     skrll 		/* Allocate an output report buffer */
    399      1.43     skrll 		sc->sc_obuf = kmem_alloc(sc->sc_olen, KM_SLEEP);
    400      1.27     skrll 
    401       1.1     skrll 		DPRINTF(("ucycomopen: sc->sc_obuf=%p\n", sc->sc_obuf));
    402       1.1     skrll 
    403       1.1     skrll #if 0
    404      1.26     skrll 		/* XXX Don't do this as for some reason trying to do an
    405      1.26     skrll 		 * XXX interrupt out transfer at this point means everything
    406      1.26     skrll 		 * XXX gets stuck!?!
    407       1.1     skrll 		 */
    408       1.1     skrll 		/*
    409       1.1     skrll 		 * Turn on DTR.  We must always do this, even if carrier is not
    410       1.1     skrll 		 * present, because otherwise we'd have to use TIOCSDTR
    411       1.1     skrll 		 * immediately after setting CLOCAL, which applications do not
    412       1.1     skrll 		 * expect.  We always assert DTR while the device is open
    413       1.1     skrll 		 * unless explicitly requested to deassert it.
    414       1.1     skrll 		 */
    415       1.1     skrll 		ucycom_dtr(sc, 1);
    416       1.1     skrll #endif
    417       1.1     skrll 
    418       1.1     skrll #if 0
    419       1.1     skrll 		/* XXX CLR(sc->sc_rx_flags, RX_ANY_BLOCK);*/
    420       1.1     skrll 		ucycom_hwiflow(sc);
    421       1.1     skrll #endif
    422       1.1     skrll 
    423       1.1     skrll 	}
    424       1.1     skrll 	splx(s);
    425       1.1     skrll 
    426       1.1     skrll 	err = ttyopen(tp, UCYCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
    427       1.1     skrll 	if (err)
    428       1.1     skrll 		goto bad;
    429       1.1     skrll 
    430       1.1     skrll 	err = (*tp->t_linesw->l_open)(dev, tp);
    431       1.1     skrll 	if (err)
    432       1.1     skrll 		goto bad;
    433       1.1     skrll 
    434      1.43     skrll 	return 0;
    435       1.1     skrll 
    436       1.1     skrll bad:
    437       1.1     skrll 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    438       1.1     skrll 		/*
    439       1.1     skrll 		 * We failed to open the device, and nobody else had it opened.
    440       1.1     skrll 		 * Clean up the state as appropriate.
    441       1.1     skrll 		 */
    442       1.1     skrll 		ucycom_cleanup(sc);
    443       1.1     skrll 	}
    444       1.1     skrll 
    445      1.43     skrll 	return err;
    446       1.1     skrll 
    447       1.1     skrll }
    448       1.1     skrll 
    449       1.1     skrll 
    450  1.46.4.2    martin static int
    451      1.16  christos ucycomclose(dev_t dev, int flag, int mode, struct lwp *l)
    452       1.1     skrll {
    453      1.23      cube 	struct ucycom_softc *sc =
    454      1.23      cube 	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
    455       1.1     skrll 	struct tty *tp = sc->sc_tty;
    456       1.1     skrll 
    457      1.25  drochner 	DPRINTF(("ucycomclose: unit=%d\n", UCYCOMUNIT(dev)));
    458       1.1     skrll 	if (!ISSET(tp->t_state, TS_ISOPEN))
    459      1.43     skrll 		return 0;
    460       1.1     skrll 
    461       1.1     skrll 	(*tp->t_linesw->l_close)(tp, flag);
    462       1.1     skrll 	ttyclose(tp);
    463       1.1     skrll 
    464       1.1     skrll 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    465       1.1     skrll 		/*
    466       1.1     skrll 		 * Although we got a last close, the device may still be in
    467       1.1     skrll 		 * use; e.g. if this was the dialout node, and there are still
    468       1.1     skrll 		 * processes waiting for carrier on the non-dialout node.
    469       1.1     skrll 		 */
    470       1.1     skrll 		ucycom_cleanup(sc);
    471       1.1     skrll 	}
    472       1.1     skrll 
    473      1.43     skrll 	return 0;
    474       1.1     skrll }
    475       1.1     skrll 
    476       1.1     skrll Static void
    477       1.1     skrll ucycomstart(struct tty *tp)
    478       1.1     skrll {
    479      1.23      cube 	struct ucycom_softc *sc =
    480      1.23      cube 	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(tp->t_dev));
    481      1.36    martin 	usbd_status err __unused;
    482       1.1     skrll 	u_char *data;
    483      1.29     skrll 	int cnt, len, s;
    484       1.1     skrll 
    485       1.1     skrll 	if (sc->sc_dying)
    486       1.1     skrll 		return;
    487       1.1     skrll 
    488       1.1     skrll 	s = spltty();
    489       1.1     skrll 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) {
    490  1.46.4.2    martin 		DPRINTFN(4,("ucycomstart: no go, state=%#x\n", tp->t_state));
    491       1.1     skrll 		goto out;
    492       1.1     skrll 	}
    493       1.1     skrll 
    494       1.1     skrll #if 0
    495       1.1     skrll 	/* HW FLOW CTL */
    496       1.1     skrll 	if (sc->sc_tx_stopped)
    497       1.1     skrll 		goto out;
    498       1.1     skrll #endif
    499       1.1     skrll 
    500      1.19        ad 	if (ttypull(tp) == 0)
    501      1.19        ad 		goto out;
    502       1.1     skrll 
    503       1.1     skrll 	/* Grab the first contiguous region of buffer space. */
    504       1.1     skrll 	data = tp->t_outq.c_cf;
    505       1.1     skrll 	cnt = ndqb(&tp->t_outq, 0);
    506       1.1     skrll 
    507       1.1     skrll 	if (cnt == 0) {
    508       1.1     skrll 		DPRINTF(("ucycomstart: cnt == 0\n"));
    509       1.1     skrll 		goto out;
    510       1.1     skrll 	}
    511       1.1     skrll 
    512       1.1     skrll 	SET(tp->t_state, TS_BUSY);
    513      1.27     skrll 
    514       1.1     skrll 	/*
    515       1.1     skrll 	 * The 8 byte output report uses byte 0 for control and byte
    516       1.1     skrll 	 * count.
    517       1.1     skrll 	 *
    518       1.1     skrll 	 * The 32 byte output report uses byte 0 for control. Byte 1
    519       1.1     skrll 	 * is used for byte count.
    520       1.1     skrll 	 */
    521      1.27     skrll 	memset(sc->sc_obuf, 0, sc->sc_olen);
    522       1.1     skrll 	len = cnt;
    523       1.1     skrll 	switch (sc->sc_olen) {
    524       1.1     skrll 	case 8:
    525       1.1     skrll 		if (cnt > sc->sc_olen - 1) {
    526       1.1     skrll 			DPRINTF(("ucycomstart(8): big buffer %d chars\n", len));
    527       1.1     skrll 			len = sc->sc_olen - 1;
    528       1.1     skrll 		}
    529      1.27     skrll 
    530       1.1     skrll 		memcpy(sc->sc_obuf + 1, data, len);
    531       1.1     skrll 		sc->sc_obuf[0] = len | sc->sc_mcr;
    532       1.1     skrll 
    533      1.44   msaitoh 		DPRINTF(("ucycomstart(8): sc->sc_obuf[0] = %d | %d = %d\n",
    534      1.44   msaitoh 		    len, sc->sc_mcr, sc->sc_obuf[0]));
    535       1.2     skrll #ifdef UCYCOM_DEBUG
    536       1.1     skrll 		if (ucycomdebug > 10) {
    537      1.43     skrll 			uint32_t i;
    538      1.43     skrll 			uint8_t *d = data;
    539       1.1     skrll 
    540       1.1     skrll 			DPRINTF(("ucycomstart(8): data ="));
    541       1.1     skrll 			for (i = 0; i < len; i++)
    542       1.1     skrll 				DPRINTF((" %02x", d[i]));
    543       1.1     skrll 			DPRINTF(("\n"));
    544       1.1     skrll 		}
    545       1.2     skrll #endif
    546       1.1     skrll 		break;
    547      1.27     skrll 
    548       1.1     skrll 	case 32:
    549       1.1     skrll 		if (cnt > sc->sc_olen - 2) {
    550      1.26     skrll 			DPRINTF(("ucycomstart(32): big buffer %d chars\n",
    551      1.26     skrll 			    len));
    552       1.1     skrll 			len = sc->sc_olen - 2;
    553       1.1     skrll 		}
    554      1.27     skrll 
    555       1.1     skrll 		memcpy(sc->sc_obuf + 2, data, len);
    556       1.1     skrll 		sc->sc_obuf[0] = sc->sc_mcr;
    557       1.1     skrll 		sc->sc_obuf[1] = len;
    558      1.26     skrll 		DPRINTF(("ucycomstart(32): sc->sc_obuf[0] = %d\n"
    559      1.26     skrll 		    "sc->sc_obuf[1] = %d\n", sc->sc_obuf[0], sc->sc_obuf[1]));
    560       1.2     skrll #ifdef UCYCOM_DEBUG
    561       1.1     skrll 		if (ucycomdebug > 10) {
    562      1.43     skrll 			uint32_t i;
    563      1.43     skrll 			uint8_t *d = data;
    564       1.1     skrll 
    565       1.1     skrll 			DPRINTF(("ucycomstart(32): data ="));
    566       1.1     skrll 			for (i = 0; i < len; i++)
    567       1.1     skrll 				DPRINTF((" %02x", d[i]));
    568       1.1     skrll 			DPRINTF(("\n"));
    569       1.1     skrll 		}
    570       1.2     skrll #endif
    571       1.1     skrll 		break;
    572      1.27     skrll 
    573       1.1     skrll 	default:
    574      1.28     skrll 		DPRINTFN(2,("ucycomstart: unknown output report size (%zd)\n",
    575       1.1     skrll 		    sc->sc_olen));
    576       1.1     skrll 		goto out;
    577       1.1     skrll 	}
    578       1.1     skrll 	splx(s);
    579      1.29     skrll 	sc->sc_wlen = len;
    580       1.1     skrll 
    581       1.1     skrll #ifdef UCYCOM_DEBUG
    582       1.1     skrll 	if (ucycomdebug > 5) {
    583       1.1     skrll 		int i;
    584       1.1     skrll 
    585       1.1     skrll 		if (len != 0) {
    586      1.26     skrll 			DPRINTF(("ucycomstart: sc->sc_obuf[0..%zd) =",
    587      1.26     skrll 			    sc->sc_olen));
    588       1.1     skrll 			for (i = 0; i < sc->sc_olen; i++)
    589       1.1     skrll 				DPRINTF((" %02x", sc->sc_obuf[i]));
    590       1.1     skrll 			DPRINTF(("\n"));
    591       1.1     skrll 		}
    592       1.1     skrll 	}
    593       1.1     skrll #endif
    594      1.29     skrll 	DPRINTFN(4,("ucycomstart: %d chars\n", len));
    595      1.43     skrll 	usbd_setup_xfer(sc->sc_hdev.sc_parent->sc_oxfer, sc, sc->sc_obuf,
    596      1.43     skrll 	    sc->sc_olen, 0, USBD_NO_TIMEOUT, ucycomwritecb);
    597       1.1     skrll 
    598      1.29     skrll 	/* What can we do on error? */
    599      1.29     skrll 	err = usbd_transfer(sc->sc_hdev.sc_parent->sc_oxfer);
    600       1.1     skrll 
    601       1.1     skrll #ifdef UCYCOM_DEBUG
    602      1.29     skrll 	if (err != USBD_IN_PROGRESS)
    603      1.29     skrll 		DPRINTF(("ucycomstart: err=%s\n", usbd_errstr(err)));
    604       1.1     skrll #endif
    605      1.29     skrll 	return;
    606      1.29     skrll 
    607      1.29     skrll out:
    608      1.29     skrll 	splx(s);
    609      1.29     skrll }
    610      1.29     skrll 
    611      1.29     skrll Static void
    612      1.43     skrll ucycomwritecb(struct usbd_xfer *xfer, void *p, usbd_status status)
    613      1.29     skrll {
    614      1.29     skrll 	struct ucycom_softc *sc = (struct ucycom_softc *)p;
    615      1.29     skrll 	struct tty *tp = sc->sc_tty;
    616      1.29     skrll 	usbd_status stat;
    617      1.29     skrll 	int len, s;
    618      1.29     skrll 
    619      1.29     skrll 	if (status == USBD_CANCELLED || sc->sc_dying)
    620      1.29     skrll 		goto error;
    621      1.29     skrll 
    622      1.29     skrll 	if (status) {
    623      1.29     skrll 		DPRINTF(("ucycomwritecb: status=%d\n", status));
    624      1.29     skrll 		usbd_clear_endpoint_stall(sc->sc_hdev.sc_parent->sc_opipe);
    625      1.29     skrll 		/* XXX we should restart after some delay. */
    626      1.29     skrll 		goto error;
    627      1.29     skrll 	}
    628       1.1     skrll 
    629      1.29     skrll 	usbd_get_xfer_status(xfer, NULL, NULL, &len, &stat);
    630      1.29     skrll 
    631      1.29     skrll 	if (status != USBD_NORMAL_COMPLETION) {
    632      1.29     skrll 		DPRINTFN(4,("ucycomwritecb: status = %d\n", status));
    633      1.29     skrll 		goto error;
    634      1.29     skrll 	}
    635      1.29     skrll 
    636      1.29     skrll 	DPRINTFN(4,("ucycomwritecb: did %d/%d chars\n", sc->sc_wlen, len));
    637      1.29     skrll 
    638      1.29     skrll 	s = spltty();
    639       1.1     skrll 	CLR(tp->t_state, TS_BUSY);
    640       1.1     skrll 	if (ISSET(tp->t_state, TS_FLUSH))
    641       1.1     skrll 		CLR(tp->t_state, TS_FLUSH);
    642       1.1     skrll 	else
    643      1.29     skrll 		ndflush(&tp->t_outq, sc->sc_wlen);
    644       1.1     skrll 	(*tp->t_linesw->l_start)(tp);
    645      1.29     skrll 	splx(s);
    646      1.29     skrll 	return;
    647       1.1     skrll 
    648      1.29     skrll error:
    649      1.29     skrll 	s = spltty();
    650      1.29     skrll 	CLR(tp->t_state, TS_BUSY);
    651       1.1     skrll 	splx(s);
    652       1.1     skrll }
    653       1.1     skrll 
    654       1.1     skrll Static int
    655       1.1     skrll ucycomparam(struct tty *tp, struct termios *t)
    656       1.1     skrll {
    657       1.1     skrll 	struct ucycom_softc *sc = tp->t_sc;
    658       1.1     skrll 	uint32_t baud;
    659       1.1     skrll 	uint8_t cfg;
    660       1.1     skrll 	int err;
    661       1.1     skrll 
    662       1.1     skrll 	if (t->c_ospeed < 0) {
    663       1.1     skrll 		DPRINTF(("ucycomparam: c_ospeed < 0\n"));
    664      1.43     skrll 		return EINVAL;
    665       1.1     skrll 	}
    666       1.1     skrll 
    667       1.1     skrll 	/* Check requested parameters. */
    668       1.1     skrll 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
    669      1.43     skrll 		return EINVAL;
    670       1.1     skrll 
    671       1.1     skrll 	/*
    672       1.1     skrll 	 * For the console, always force CLOCAL and !HUPCL, so that the port
    673       1.1     skrll 	 * is always active.
    674       1.1     skrll 	 */
    675       1.1     skrll 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR)) {
    676       1.1     skrll 		SET(t->c_cflag, CLOCAL);
    677       1.1     skrll 		CLR(t->c_cflag, HUPCL);
    678       1.1     skrll 	}
    679       1.1     skrll 
    680       1.1     skrll 	/*
    681       1.1     skrll 	 * If there were no changes, don't do anything.  This avoids dropping
    682       1.1     skrll 	 * input and improves performance when all we did was frob things like
    683       1.1     skrll 	 * VMIN and VTIME.
    684       1.1     skrll 	 */
    685       1.1     skrll 	if (tp->t_ospeed == t->c_ospeed &&
    686       1.1     skrll 	    tp->t_cflag == t->c_cflag)
    687      1.43     skrll 		return 0;
    688       1.1     skrll 
    689       1.1     skrll 	/* XXX lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); */
    690       1.1     skrll 
    691       1.1     skrll 	/* And copy to tty. */
    692       1.1     skrll 	tp->t_ispeed = 0;
    693       1.1     skrll 	tp->t_ospeed = t->c_ospeed;
    694       1.1     skrll 	tp->t_cflag = t->c_cflag;
    695       1.1     skrll 
    696       1.1     skrll 	baud = t->c_ispeed;
    697       1.1     skrll 	DPRINTF(("ucycomparam: baud=%d\n", baud));
    698       1.1     skrll 
    699       1.1     skrll 	if (t->c_cflag & CIGNORE) {
    700       1.1     skrll 		cfg = sc->sc_cfg;
    701       1.1     skrll 	} else {
    702       1.1     skrll 		cfg = 0;
    703       1.1     skrll 		switch (t->c_cflag & CSIZE) {
    704       1.1     skrll 		case CS8:
    705       1.1     skrll 			cfg |= UCYCOM_DATA_BITS_8;
    706       1.1     skrll 			break;
    707       1.1     skrll 		case CS7:
    708       1.1     skrll 			cfg |= UCYCOM_DATA_BITS_7;
    709       1.1     skrll 			break;
    710       1.1     skrll 		case CS6:
    711       1.1     skrll 			cfg |= UCYCOM_DATA_BITS_6;
    712       1.1     skrll 			break;
    713       1.1     skrll 		case CS5:
    714       1.1     skrll 			cfg |= UCYCOM_DATA_BITS_5;
    715       1.1     skrll 			break;
    716       1.1     skrll 		default:
    717      1.43     skrll 			return EINVAL;
    718       1.1     skrll 		}
    719       1.1     skrll 		cfg |= ISSET(t->c_cflag, CSTOPB) ?
    720       1.1     skrll 		    UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1;
    721       1.1     skrll 		cfg |= ISSET(t->c_cflag, PARENB) ?
    722       1.1     skrll 		    UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF;
    723       1.1     skrll 		cfg |= ISSET(t->c_cflag, PARODD) ?
    724       1.1     skrll 		    UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN;
    725       1.1     skrll 	}
    726       1.1     skrll 
    727       1.1     skrll 	/*
    728       1.1     skrll 	 * Update the tty layer's idea of the carrier bit, in case we changed
    729       1.1     skrll 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
    730       1.1     skrll 	 * explicit request.
    731       1.1     skrll 	 */
    732       1.1     skrll 	DPRINTF(("ucycomparam: l_modem\n"));
    733       1.1     skrll 	(void) (*tp->t_linesw->l_modem)(tp, 1 /* XXX carrier */ );
    734       1.1     skrll 
    735       1.1     skrll 	err = ucycom_configure(sc, baud, cfg);
    736      1.43     skrll 	return err;
    737       1.1     skrll }
    738       1.1     skrll 
    739  1.46.4.2    martin static void
    740      1.16  christos ucycomstop(struct tty *tp, int flag)
    741       1.1     skrll {
    742       1.1     skrll 	DPRINTF(("ucycomstop: flag=%d\n", flag));
    743       1.1     skrll }
    744       1.1     skrll 
    745  1.46.4.2    martin static int
    746       1.1     skrll ucycomread(dev_t dev, struct uio *uio, int flag)
    747       1.1     skrll {
    748      1.23      cube 	struct ucycom_softc *sc =
    749      1.23      cube 	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
    750       1.1     skrll 	struct tty *tp = sc->sc_tty;
    751       1.1     skrll 	int err;
    752       1.1     skrll 
    753      1.26     skrll 	DPRINTF(("ucycomread: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio,
    754      1.26     skrll 	    flag));
    755       1.1     skrll 	if (sc->sc_dying)
    756      1.43     skrll 		return EIO;
    757       1.1     skrll 
    758       1.1     skrll 	err = ((*tp->t_linesw->l_read)(tp, uio, flag));
    759      1.43     skrll 	return err;
    760       1.1     skrll }
    761       1.1     skrll 
    762       1.1     skrll 
    763  1.46.4.2    martin static int
    764       1.1     skrll ucycomwrite(dev_t dev, struct uio *uio, int flag)
    765       1.1     skrll {
    766      1.23      cube 	struct ucycom_softc *sc =
    767      1.23      cube 	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
    768       1.1     skrll 	struct tty *tp = sc->sc_tty;
    769       1.1     skrll 	int err;
    770       1.1     skrll 
    771      1.26     skrll 	DPRINTF(("ucycomwrite: sc=%p, tp=%p, uio=%p, flag=%d\n", sc, tp, uio,
    772      1.26     skrll 	    flag));
    773       1.1     skrll 	if (sc->sc_dying)
    774      1.43     skrll 		return EIO;
    775       1.1     skrll 
    776       1.1     skrll 	err = ((*tp->t_linesw->l_write)(tp, uio, flag));
    777      1.43     skrll 	return err;
    778       1.1     skrll }
    779       1.1     skrll 
    780  1.46.4.2    martin static struct tty *
    781       1.1     skrll ucycomtty(dev_t dev)
    782       1.1     skrll {
    783      1.23      cube 	struct ucycom_softc *sc =
    784      1.23      cube 	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
    785       1.1     skrll 	struct tty *tp = sc->sc_tty;
    786       1.1     skrll 
    787       1.1     skrll 	DPRINTF(("ucycomtty: sc=%p, tp=%p\n", sc, tp));
    788       1.1     skrll 
    789      1.43     skrll 	return tp;
    790       1.1     skrll }
    791       1.1     skrll 
    792  1.46.4.2    martin static int
    793      1.17  christos ucycomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    794       1.1     skrll {
    795      1.23      cube 	struct ucycom_softc *sc =
    796      1.23      cube 	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
    797       1.1     skrll 	struct tty *tp = sc->sc_tty;
    798       1.1     skrll 	int err;
    799       1.1     skrll 	int s;
    800       1.1     skrll 
    801       1.1     skrll 	if (sc->sc_dying)
    802      1.43     skrll 		return EIO;
    803       1.1     skrll 
    804       1.1     skrll 	DPRINTF(("ucycomioctl: sc=%p, tp=%p, data=%p\n", sc, tp, data));
    805       1.1     skrll 
    806       1.6  christos 	err = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    807       1.1     skrll 	if (err != EPASSTHROUGH)
    808      1.43     skrll 		return err;
    809       1.1     skrll 
    810       1.6  christos 	err = ttioctl(tp, cmd, data, flag, l);
    811       1.1     skrll 	if (err != EPASSTHROUGH)
    812      1.43     skrll 		return err;
    813       1.1     skrll 
    814       1.1     skrll 	err = 0;
    815       1.1     skrll 
    816       1.1     skrll 	DPRINTF(("ucycomioctl: our cmd=0x%08lx\n", cmd));
    817       1.1     skrll 	s = spltty();
    818       1.1     skrll 
    819       1.1     skrll 	switch (cmd) {
    820       1.1     skrll /*	case TIOCSBRK:
    821       1.1     skrll 		ucycom_break(sc, 1);
    822       1.1     skrll 		break;
    823       1.1     skrll 
    824       1.1     skrll 	case TIOCCBRK:
    825       1.1     skrll 		ucycom_break(sc, 0);
    826       1.1     skrll 		break;
    827       1.1     skrll */
    828       1.1     skrll 	case TIOCSDTR:
    829       1.1     skrll 		ucycom_dtr(sc, 1);
    830       1.1     skrll 		break;
    831       1.1     skrll 
    832       1.1     skrll 	case TIOCCDTR:
    833       1.1     skrll 		ucycom_dtr(sc, 0);
    834       1.1     skrll 		break;
    835       1.1     skrll 
    836       1.1     skrll 	case TIOCGFLAGS:
    837       1.1     skrll 		*(int *)data = sc->sc_swflags;
    838       1.1     skrll 		break;
    839       1.1     skrll 
    840       1.1     skrll 	case TIOCSFLAGS:
    841      1.13      elad 		err = kauth_authorize_device_tty(l->l_cred,
    842      1.13      elad 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
    843       1.1     skrll 		if (err)
    844       1.1     skrll 			break;
    845       1.1     skrll 		sc->sc_swflags = *(int *)data;
    846       1.1     skrll 		break;
    847       1.1     skrll 
    848       1.1     skrll 	case TIOCMSET:
    849       1.1     skrll 	case TIOCMBIS:
    850       1.1     skrll 	case TIOCMBIC:
    851       1.1     skrll 		tiocm_to_ucycom(sc, cmd, *(int *)data);
    852       1.1     skrll 		break;
    853       1.1     skrll 
    854       1.1     skrll 	case TIOCMGET:
    855       1.1     skrll 		*(int *)data = ucycom_to_tiocm(sc);
    856       1.1     skrll 		break;
    857       1.1     skrll 
    858       1.1     skrll 	default:
    859       1.1     skrll 		err = EPASSTHROUGH;
    860       1.1     skrll 		break;
    861       1.1     skrll 	}
    862       1.1     skrll 
    863       1.1     skrll 	splx(s);
    864       1.1     skrll 
    865      1.43     skrll 	return err;
    866       1.1     skrll }
    867       1.1     skrll 
    868  1.46.4.2    martin static int
    869       1.6  christos ucycompoll(dev_t dev, int events, struct lwp *l)
    870       1.1     skrll {
    871      1.23      cube 	struct ucycom_softc *sc =
    872      1.23      cube 	    device_lookup_private(&ucycom_cd, UCYCOMUNIT(dev));
    873       1.1     skrll 	struct tty *tp = sc->sc_tty;
    874       1.1     skrll 	int err;
    875      1.27     skrll 
    876      1.26     skrll 	DPRINTF(("ucycompoll: sc=%p, tp=%p, events=%d, lwp=%p\n", sc, tp,
    877      1.26     skrll 	    events, l));
    878       1.1     skrll 
    879       1.1     skrll 	if (sc->sc_dying)
    880      1.43     skrll 		return EIO;
    881       1.1     skrll 
    882       1.6  christos 	err = ((*tp->t_linesw->l_poll)(tp, events, l));
    883      1.43     skrll 	return err;
    884       1.1     skrll }
    885       1.1     skrll 
    886       1.1     skrll Static int
    887       1.1     skrll ucycom_configure(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
    888       1.1     skrll {
    889       1.1     skrll 	uint8_t report[5];
    890       1.1     skrll 	int err;
    891       1.1     skrll 
    892       1.1     skrll 	switch (baud) {
    893       1.1     skrll 	case 600:
    894       1.1     skrll 	case 1200:
    895       1.1     skrll 	case 2400:
    896       1.1     skrll 	case 4800:
    897       1.1     skrll 	case 9600:
    898       1.1     skrll 	case 19200:
    899       1.1     skrll 	case 38400:
    900       1.1     skrll 	case 57600:
    901       1.1     skrll #if 0
    902       1.1     skrll 	/*
    903       1.1     skrll 	 * Stock chips only support standard baud rates in the 600 - 57600
    904       1.1     skrll 	 * range, but higher rates can be achieved using custom firmware.
    905       1.1     skrll 	 */
    906       1.1     skrll 	case 115200:
    907       1.1     skrll 	case 153600:
    908       1.1     skrll 	case 192000:
    909       1.1     skrll #endif
    910       1.1     skrll 		break;
    911       1.1     skrll 	default:
    912      1.43     skrll 		return EINVAL;
    913       1.1     skrll 	}
    914       1.1     skrll 
    915       1.1     skrll 	DPRINTF(("ucycom_configure: setting %d baud, %d-%c-%d (%d)\n", baud,
    916       1.1     skrll 	    5 + (cfg & UCYCOM_DATA_MASK),
    917       1.1     skrll 	    (cfg & UCYCOM_PARITY_MASK) ?
    918       1.1     skrll 		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
    919       1.1     skrll 	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
    920       1.1     skrll 
    921       1.1     skrll 	report[0] = baud & 0xff;
    922       1.1     skrll 	report[1] = (baud >> 8) & 0xff;
    923       1.1     skrll 	report[2] = (baud >> 16) & 0xff;
    924       1.1     skrll 	report[3] = (baud >> 24) & 0xff;
    925       1.1     skrll 	report[4] = cfg;
    926       1.1     skrll 	err = uhidev_set_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
    927       1.1     skrll 	    report, sc->sc_flen);
    928       1.1     skrll 	if (err != 0) {
    929       1.1     skrll 		DPRINTF(("%s\n", usbd_errstr(err)));
    930       1.1     skrll 		return EIO;
    931       1.1     skrll 	}
    932       1.1     skrll 	sc->sc_baud = baud;
    933       1.1     skrll 	sc->sc_cfg = cfg;
    934       1.1     skrll 
    935      1.29     skrll #ifdef UCYCOM_DEBUG
    936      1.29     skrll 	ucycom_get_cfg(sc);
    937      1.29     skrll #endif
    938      1.29     skrll 
    939       1.1     skrll 	return 0;
    940       1.1     skrll }
    941       1.1     skrll 
    942       1.1     skrll Static void
    943       1.1     skrll ucycom_intr(struct uhidev *addr, void *ibuf, u_int len)
    944       1.1     skrll {
    945       1.1     skrll 	struct ucycom_softc *sc = (struct ucycom_softc *)addr;
    946       1.1     skrll 	struct tty *tp = sc->sc_tty;
    947       1.1     skrll 	int (*rint)(int , struct tty *) = tp->t_linesw->l_rint;
    948       1.1     skrll 	uint8_t *cp = ibuf;
    949       1.1     skrll 	int s, n, st, chg;
    950      1.27     skrll 
    951       1.1     skrll 	/* We understand 8 byte and 32 byte input records */
    952       1.1     skrll 	switch (len) {
    953       1.1     skrll 	case 8:
    954       1.1     skrll 		n = cp[0] & UCYCOM_LMASK;
    955       1.1     skrll 		st = cp[0] & ~UCYCOM_LMASK;
    956       1.1     skrll 		cp++;
    957       1.1     skrll 		break;
    958       1.1     skrll 
    959       1.1     skrll 	case 32:
    960       1.1     skrll 		st = cp[0];
    961       1.1     skrll 		n = cp[1];
    962       1.1     skrll 		cp += 2;
    963       1.1     skrll 		break;
    964       1.1     skrll 
    965       1.1     skrll 	default:
    966       1.1     skrll 		DPRINTFN(3,("ucycom_intr: Unknown input report length\n"));
    967       1.1     skrll 		return;
    968       1.1     skrll 	}
    969       1.1     skrll 
    970       1.1     skrll #ifdef UCYCOM_DEBUG
    971       1.1     skrll 	if (ucycomdebug > 5) {
    972      1.43     skrll 		uint32_t i;
    973       1.1     skrll 
    974       1.1     skrll 		if (n != 0) {
    975       1.1     skrll 			DPRINTF(("ucycom_intr: ibuf[0..%d) =", n));
    976       1.1     skrll 			for (i = 0; i < n; i++)
    977       1.1     skrll 				DPRINTF((" %02x", cp[i]));
    978       1.1     skrll 			DPRINTF(("\n"));
    979       1.1     skrll 		}
    980       1.1     skrll 	}
    981       1.1     skrll #endif
    982       1.1     skrll 
    983       1.1     skrll 	/* Give characters to tty layer. */
    984      1.29     skrll 	s = spltty();
    985       1.1     skrll 	while (n-- > 0) {
    986       1.1     skrll 		DPRINTFN(7,("ucycom_intr: char=0x%02x\n", *cp));
    987       1.1     skrll 		if ((*rint)(*cp++, tp) == -1) {
    988       1.1     skrll 			/* XXX what should we do? */
    989      1.23      cube 			aprint_error_dev(sc->sc_hdev.sc_dev,
    990      1.23      cube 			    "lost a character\n");
    991       1.1     skrll 			break;
    992       1.1     skrll 		}
    993       1.1     skrll 	}
    994       1.1     skrll 	splx(s);
    995       1.1     skrll 	chg = st ^ sc->sc_msr;
    996       1.1     skrll 	sc->sc_msr = st;
    997       1.1     skrll 	if (ISSET(chg, UCYCOM_DCD))
    998       1.1     skrll 		(*tp->t_linesw->l_modem)(tp,
    999       1.1     skrll 		    ISSET(sc->sc_msr, UCYCOM_DCD));
   1000       1.1     skrll 
   1001       1.1     skrll }
   1002       1.1     skrll 
   1003       1.1     skrll Static void
   1004       1.1     skrll tiocm_to_ucycom(struct ucycom_softc *sc, u_long how, int ttybits)
   1005       1.1     skrll {
   1006       1.1     skrll 	u_char combits;
   1007       1.1     skrll 	u_char before = sc->sc_mcr;
   1008       1.1     skrll 
   1009       1.1     skrll 	combits = 0;
   1010       1.1     skrll 	if (ISSET(ttybits, TIOCM_DTR))
   1011       1.1     skrll 		SET(combits, UCYCOM_DTR);
   1012       1.1     skrll 	if (ISSET(ttybits, TIOCM_RTS))
   1013       1.1     skrll 		SET(combits, UCYCOM_RTS);
   1014       1.1     skrll 
   1015       1.1     skrll 	switch (how) {
   1016       1.1     skrll 	case TIOCMBIC:
   1017       1.1     skrll 		CLR(sc->sc_mcr, combits);
   1018       1.1     skrll 		break;
   1019       1.1     skrll 
   1020       1.1     skrll 	case TIOCMBIS:
   1021       1.1     skrll 		SET(sc->sc_mcr, combits);
   1022       1.1     skrll 		break;
   1023       1.1     skrll 
   1024       1.1     skrll 	case TIOCMSET:
   1025       1.1     skrll 		CLR(sc->sc_mcr, UCYCOM_DTR | UCYCOM_RTS);
   1026       1.1     skrll 		SET(sc->sc_mcr, combits);
   1027       1.1     skrll 		break;
   1028       1.1     skrll 	}
   1029       1.1     skrll 	if (before ^ sc->sc_mcr) {
   1030       1.1     skrll 		DPRINTF(("tiocm_to_ucycom: something has changed\n"));
   1031       1.1     skrll 		ucycom_set_status(sc);
   1032       1.1     skrll 	}
   1033       1.1     skrll }
   1034       1.1     skrll 
   1035       1.1     skrll Static int
   1036       1.1     skrll ucycom_to_tiocm(struct ucycom_softc *sc)
   1037       1.1     skrll {
   1038       1.1     skrll 	u_char combits;
   1039       1.1     skrll 	int ttybits = 0;
   1040       1.1     skrll 
   1041       1.1     skrll 	combits = sc->sc_mcr;
   1042       1.1     skrll 	if (ISSET(combits, UCYCOM_DTR))
   1043       1.1     skrll 		SET(ttybits, TIOCM_DTR);
   1044       1.1     skrll 	if (ISSET(combits, UCYCOM_RTS))
   1045       1.1     skrll 		SET(ttybits, TIOCM_RTS);
   1046       1.1     skrll 
   1047       1.1     skrll 	combits = sc->sc_msr;
   1048       1.1     skrll 	if (ISSET(combits, UCYCOM_DCD))
   1049       1.1     skrll 		SET(ttybits, TIOCM_CD);
   1050       1.1     skrll 	if (ISSET(combits, UCYCOM_CTS))
   1051       1.1     skrll 		SET(ttybits, TIOCM_CTS);
   1052       1.1     skrll 	if (ISSET(combits, UCYCOM_DSR))
   1053       1.1     skrll 		SET(ttybits, TIOCM_DSR);
   1054       1.1     skrll 	if (ISSET(combits, UCYCOM_RI))
   1055       1.1     skrll 		SET(ttybits, TIOCM_RI);
   1056       1.1     skrll 
   1057      1.43     skrll 	return ttybits;
   1058       1.1     skrll }
   1059       1.1     skrll 
   1060       1.1     skrll Static void
   1061       1.1     skrll ucycom_dtr(struct ucycom_softc *sc, int set)
   1062       1.1     skrll {
   1063       1.1     skrll 	uint8_t old;
   1064      1.27     skrll 
   1065       1.1     skrll 	old = sc->sc_mcr;
   1066       1.1     skrll 	if (set)
   1067       1.1     skrll 		SET(sc->sc_mcr, UCYCOM_DTR);
   1068       1.1     skrll 	else
   1069       1.1     skrll 		CLR(sc->sc_mcr, UCYCOM_DTR);
   1070      1.27     skrll 
   1071       1.1     skrll 	if (old ^ sc->sc_mcr)
   1072       1.1     skrll 		ucycom_set_status(sc);
   1073       1.1     skrll }
   1074       1.1     skrll 
   1075       1.1     skrll #if 0
   1076       1.1     skrll Static void
   1077       1.1     skrll ucycom_rts(struct ucycom_softc *sc, int set)
   1078       1.1     skrll {
   1079       1.1     skrll 	uint8_t old;
   1080      1.27     skrll 
   1081       1.1     skrll 	old = sc->sc_msr;
   1082       1.1     skrll 	if (set)
   1083       1.1     skrll 		SET(sc->sc_mcr, UCYCOM_RTS);
   1084       1.1     skrll 	else
   1085       1.1     skrll 		CLR(sc->sc_mcr, UCYCOM_RTS);
   1086      1.27     skrll 
   1087       1.1     skrll 	if (old ^ sc->sc_mcr)
   1088       1.1     skrll 		ucycom_set_status(sc);
   1089       1.1     skrll }
   1090       1.1     skrll #endif
   1091       1.1     skrll 
   1092       1.1     skrll Static void
   1093       1.1     skrll ucycom_set_status(struct ucycom_softc *sc)
   1094       1.1     skrll {
   1095       1.1     skrll 	int err;
   1096       1.1     skrll 
   1097       1.1     skrll 	if (sc->sc_olen != 8 && sc->sc_olen != 32) {
   1098      1.26     skrll 		DPRINTFN(2,("ucycom_set_status: unknown output report "
   1099      1.26     skrll 		    "size (%zd)\n", sc->sc_olen));
   1100       1.1     skrll 		return;
   1101       1.1     skrll 	}
   1102      1.27     skrll 
   1103       1.1     skrll 	DPRINTF(("ucycom_set_status: %d\n", sc->sc_mcr));
   1104       1.1     skrll 
   1105       1.1     skrll 	memset(sc->sc_obuf, 0, sc->sc_olen);
   1106       1.1     skrll 	sc->sc_obuf[0] = sc->sc_mcr;
   1107       1.1     skrll 
   1108       1.1     skrll 	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
   1109      1.14  christos 	if (err) {
   1110       1.1     skrll 		DPRINTF(("ucycom_set_status: err=%d\n", err));
   1111      1.14  christos 	}
   1112       1.1     skrll }
   1113       1.1     skrll 
   1114       1.1     skrll #ifdef UCYCOM_DEBUG
   1115       1.1     skrll Static void
   1116       1.1     skrll ucycom_get_cfg(struct ucycom_softc *sc)
   1117       1.1     skrll {
   1118       1.1     skrll 	int err, cfg, baud;
   1119       1.1     skrll 	uint8_t report[5];
   1120      1.27     skrll 
   1121       1.1     skrll 	err = uhidev_get_report(&sc->sc_hdev, UHID_FEATURE_REPORT,
   1122       1.1     skrll 	    report, sc->sc_flen);
   1123      1.37     skrll 	if (err) {
   1124      1.37     skrll 		DPRINTF(("%s: failed\n", __func__));
   1125      1.37     skrll 		return;
   1126      1.37     skrll 	}
   1127       1.1     skrll 	cfg = report[4];
   1128      1.26     skrll 	baud = (report[3] << 24) + (report[2] << 16) + (report[1] << 8) +
   1129      1.26     skrll 	    report[0];
   1130      1.29     skrll 	DPRINTF(("ucycom_get_cfg: device reports %d baud, %d-%c-%d (%d)\n",
   1131      1.26     skrll 	    baud, 5 + (cfg & UCYCOM_DATA_MASK),
   1132       1.1     skrll 	    (cfg & UCYCOM_PARITY_MASK) ?
   1133       1.1     skrll 		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
   1134       1.1     skrll 	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
   1135       1.1     skrll }
   1136       1.1     skrll #endif
   1137       1.1     skrll 
   1138       1.1     skrll Static void
   1139       1.1     skrll ucycom_cleanup(struct ucycom_softc *sc)
   1140       1.1     skrll {
   1141      1.42       mrg 	uint8_t	*obuf;
   1142      1.42       mrg 
   1143       1.1     skrll 	DPRINTF(("ucycom_cleanup: closing uhidev\n"));
   1144       1.1     skrll 
   1145      1.42       mrg 	obuf = sc->sc_obuf;
   1146      1.42       mrg 	sc->sc_obuf = NULL;
   1147       1.1     skrll 	uhidev_close(&sc->sc_hdev);
   1148      1.42       mrg 
   1149      1.42       mrg 	if (obuf != NULL)
   1150      1.43     skrll 		kmem_free(obuf, sc->sc_olen);
   1151       1.1     skrll }
   1152