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