xref: /openbsd-src/sys/dev/usb/ucycom.c (revision 81508fe356eb7772a68118f65f91723ce5261d7d)
1*81508fe3Sjsg /*	$OpenBSD: ucycom.c,v 1.42 2024/05/23 03:21:09 jsg Exp $	*/
235717820Sjason /*	$NetBSD: ucycom.c,v 1.3 2005/08/05 07:27:47 skrll Exp $	*/
335717820Sjason 
435717820Sjason /*
535717820Sjason  * Copyright (c) 2005 The NetBSD Foundation, Inc.
635717820Sjason  * All rights reserved.
735717820Sjason  *
835717820Sjason  * This code is derived from software contributed to The NetBSD Foundation
935717820Sjason  * by Nick Hudson
1035717820Sjason  *
1135717820Sjason  * Redistribution and use in source and binary forms, with or without
1235717820Sjason  * modification, are permitted provided that the following conditions
1335717820Sjason  * are met:
1435717820Sjason  * 1. Redistributions of source code must retain the above copyright
1535717820Sjason  *    notice, this list of conditions and the following disclaimer.
1635717820Sjason  * 2. Redistributions in binary form must reproduce the above copyright
1735717820Sjason  *    notice, this list of conditions and the following disclaimer in the
1835717820Sjason  *    documentation and/or other materials provided with the distribution.
1935717820Sjason  *
2035717820Sjason  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2135717820Sjason  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2235717820Sjason  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2335717820Sjason  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2435717820Sjason  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2535717820Sjason  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2635717820Sjason  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2735717820Sjason  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2835717820Sjason  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2935717820Sjason  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3035717820Sjason  * POSSIBILITY OF SUCH DAMAGE.
3135717820Sjason  */
3235717820Sjason /*
3335717820Sjason  * This code is based on the ucom driver.
3435717820Sjason  */
3535717820Sjason 
3635717820Sjason /*
3735717820Sjason  * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
3835717820Sjason  * RS232 bridges.
3935717820Sjason  */
4035717820Sjason 
4135717820Sjason #include <sys/param.h>
4235717820Sjason #include <sys/systm.h>
4335717820Sjason #include <sys/malloc.h>
4435717820Sjason #include <sys/device.h>
4535717820Sjason #include <sys/tty.h>
4635717820Sjason 
4735717820Sjason #include <dev/usb/usb.h>
4835717820Sjason #include <dev/usb/usbhid.h>
4935717820Sjason 
5035717820Sjason #include <dev/usb/usbdi.h>
5135717820Sjason #include <dev/usb/usbdevs.h>
5235717820Sjason #include <dev/usb/uhidev.h>
5335717820Sjason 
5435717820Sjason #include <dev/usb/ucomvar.h>
5535717820Sjason 
5635717820Sjason #ifdef UCYCOM_DEBUG
57695146ceSjsg #define DPRINTF(x)	if (ucycomdebug) printf x
58695146ceSjsg #define DPRINTFN(n, x)	if (ucycomdebug > (n)) printf x
5935717820Sjason int	ucycomdebug = 200;
6035717820Sjason #else
6135717820Sjason #define DPRINTF(x)
6235717820Sjason #define DPRINTFN(n,x)
6335717820Sjason #endif
6435717820Sjason 
6535717820Sjason /* Configuration Byte */
6635717820Sjason #define UCYCOM_RESET		0x80
6735717820Sjason #define UCYCOM_PARITY_TYPE_MASK	0x20
6835717820Sjason #define  UCYCOM_PARITY_ODD	 0x20
6935717820Sjason #define  UCYCOM_PARITY_EVEN	 0x00
7035717820Sjason #define UCYCOM_PARITY_MASK	0x10
7135717820Sjason #define  UCYCOM_PARITY_ON	 0x10
7235717820Sjason #define  UCYCOM_PARITY_OFF	 0x00
7335717820Sjason #define UCYCOM_STOP_MASK	0x08
7435717820Sjason #define  UCYCOM_STOP_BITS_2	 0x08
7535717820Sjason #define  UCYCOM_STOP_BITS_1	 0x00
7635717820Sjason #define UCYCOM_DATA_MASK	0x03
7735717820Sjason #define  UCYCOM_DATA_BITS_8	 0x03
7835717820Sjason #define  UCYCOM_DATA_BITS_7	 0x02
7935717820Sjason #define  UCYCOM_DATA_BITS_6	 0x01
8035717820Sjason #define  UCYCOM_DATA_BITS_5	 0x00
8135717820Sjason 
8235717820Sjason /* Modem (Input) status byte */
8335717820Sjason #define UCYCOM_RI	0x80
8435717820Sjason #define UCYCOM_DCD	0x40
8535717820Sjason #define UCYCOM_DSR	0x20
8635717820Sjason #define UCYCOM_CTS	0x10
8735717820Sjason #define UCYCOM_ERROR	0x08
8835717820Sjason #define UCYCOM_LMASK	0x07
8935717820Sjason 
9035717820Sjason /* Modem (Output) control byte */
9135717820Sjason #define UCYCOM_DTR	0x20
9235717820Sjason #define UCYCOM_RTS	0x10
9335717820Sjason #define UCYCOM_ORESET	0x08
9435717820Sjason 
9535717820Sjason struct ucycom_softc {
9635717820Sjason 	struct uhidev		 sc_hdev;
97ab0b1be7Smglocker 	struct usbd_device	*sc_udev;
9835717820Sjason 
9935717820Sjason 	/* uhidev parameters */
10035717820Sjason 	size_t			 sc_flen;	/* feature report length */
10135717820Sjason 	size_t			 sc_ilen;	/* input report length */
10235717820Sjason 	size_t			 sc_olen;	/* output report length */
10335717820Sjason 
10435717820Sjason 	uint8_t			*sc_obuf;
10535717820Sjason 
10635717820Sjason 	uint8_t			*sc_ibuf;
10735717820Sjason 	uint32_t		 sc_icnt;
10835717820Sjason 
10935717820Sjason 	/* settings */
11035717820Sjason 	uint32_t		 sc_baud;
11135717820Sjason 	uint8_t			 sc_cfg;	/* Data format */
11235717820Sjason 	uint8_t			 sc_mcr;	/* Modem control */
11335717820Sjason 	uint8_t			 sc_msr;	/* Modem status */
11435717820Sjason 	uint8_t			 sc_newmsr;	/* from HID intr */
11535717820Sjason 	int			 sc_swflags;
11635717820Sjason 
11773030186Smbalmer 	struct device		*sc_subdev;
11835717820Sjason };
11935717820Sjason 
12035717820Sjason /* Callback routines */
12178315254Smbalmer void	ucycom_set(void *, int, int, int);
12278315254Smbalmer int	ucycom_param(void *, int, struct termios *);
12378315254Smbalmer void	ucycom_get_status(void *, int, u_char *, u_char *);
12478315254Smbalmer int	ucycom_open(void *, int);
12578315254Smbalmer void	ucycom_close(void *, int);
12678315254Smbalmer void	ucycom_write(void *, int, u_char *, u_char *, u_int32_t *);
12778315254Smbalmer void	ucycom_read(void *, int, u_char **, u_int32_t *);
12835717820Sjason 
129c520a48cSnaddy const struct ucom_methods ucycom_methods = {
13035717820Sjason 	NULL, /* ucycom_get_status, */
13135717820Sjason 	ucycom_set,
13235717820Sjason 	ucycom_param,
13335717820Sjason 	NULL,
13435717820Sjason 	ucycom_open,
13535717820Sjason 	ucycom_close,
13635717820Sjason 	ucycom_read,
13735717820Sjason 	ucycom_write,
13835717820Sjason };
13935717820Sjason 
14078315254Smbalmer void ucycom_intr(struct uhidev *, void *, u_int);
14135717820Sjason 
14278315254Smbalmer const struct usb_devno ucycom_devs[] = {
14335717820Sjason 	{ USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_USBRS232 },
14435717820Sjason 	{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMUSB },
14535717820Sjason 	{ USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EMLT20 },
14635717820Sjason };
14735717820Sjason 
1489f5f6d50Smbalmer int ucycom_match(struct device *, void *, void *);
1499f5f6d50Smbalmer void ucycom_attach(struct device *, struct device *, void *);
1509f5f6d50Smbalmer int ucycom_detach(struct device *, int);
1519f5f6d50Smbalmer 
1529f5f6d50Smbalmer struct cfdriver ucycom_cd = {
1539f5f6d50Smbalmer 	NULL, "ucycom", DV_DULL
1549f5f6d50Smbalmer };
1559f5f6d50Smbalmer 
1569f5f6d50Smbalmer const struct cfattach ucycom_ca = {
157cf8c8cdaSmpi 	sizeof(struct ucycom_softc), ucycom_match, ucycom_attach, ucycom_detach
1589f5f6d50Smbalmer };
15935717820Sjason 
160de5d9ff0Sjsg int
ucycom_match(struct device * parent,void * match,void * aux)161de5d9ff0Sjsg ucycom_match(struct device *parent, void *match, void *aux)
16235717820Sjason {
16335717820Sjason 	struct uhidev_attach_arg *uha = aux;
16435717820Sjason 
165faac88c0Santon 	if (UHIDEV_CLAIM_MULTIPLE_REPORTID(uha))
166f964a65cSmpi 		return (UMATCH_NONE);
167f964a65cSmpi 
168230ab8cdSjasper 	return (usb_lookup(ucycom_devs, uha->uaa->vendor, uha->uaa->product) != NULL ?
16935717820Sjason 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
17035717820Sjason }
17135717820Sjason 
172de5d9ff0Sjsg void
ucycom_attach(struct device * parent,struct device * self,void * aux)173de5d9ff0Sjsg ucycom_attach(struct device *parent, struct device *self, void *aux)
17435717820Sjason {
175de5d9ff0Sjsg 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
176de5d9ff0Sjsg 	struct usb_attach_arg *uaa = aux;
17745578972Sjason 	struct uhidev_attach_arg *uha = (struct uhidev_attach_arg *)uaa;
178ab0b1be7Smglocker 	struct usbd_device *dev = uha->parent->sc_udev;
17935717820Sjason 	struct ucom_attach_args uca;
18035717820Sjason 	int size, repid, err;
18135717820Sjason 	void *desc;
18235717820Sjason 
18335717820Sjason 	sc->sc_hdev.sc_intr = ucycom_intr;
18435717820Sjason 	sc->sc_hdev.sc_parent = uha->parent;
18535717820Sjason 	sc->sc_hdev.sc_report_id = uha->reportid;
18635717820Sjason 
18735717820Sjason 	uhidev_get_report_desc(uha->parent, &desc, &size);
18835717820Sjason 	repid = uha->reportid;
18935717820Sjason 	sc->sc_ilen = hid_report_size(desc, size, hid_input, repid);
19035717820Sjason 	sc->sc_olen = hid_report_size(desc, size, hid_output, repid);
19135717820Sjason 	sc->sc_flen = hid_report_size(desc, size, hid_feature, repid);
19235717820Sjason 
19335717820Sjason 	DPRINTF(("ucycom_open: olen %d ilen %d flen %d\n", sc->sc_ilen,
19435717820Sjason 	    sc->sc_olen, sc->sc_flen));
19535717820Sjason 
19635717820Sjason 	printf("\n");
19735717820Sjason 
19835717820Sjason 	sc->sc_udev = dev;
19935717820Sjason 
20035717820Sjason 	err = uhidev_open(&sc->sc_hdev);
20135717820Sjason 	if (err) {
20235717820Sjason 		DPRINTF(("ucycom_open: uhidev_open %d\n", err));
20335717820Sjason 		return;
20435717820Sjason 	}
20535717820Sjason 
20635717820Sjason 	DPRINTF(("ucycom attach: sc %p opipe %p ipipe %p report_id %d\n",
20735717820Sjason 	    sc, sc->sc_hdev.sc_parent->sc_opipe, sc->sc_hdev.sc_parent->sc_ipipe,
20835717820Sjason 	    uha->reportid));
20935717820Sjason 
21035717820Sjason 	/* bulkin, bulkout set above */
21135717820Sjason 	bzero(&uca, sizeof uca);
21235717820Sjason 	uca.bulkin = uca.bulkout = -1;
21335717820Sjason 	uca.ibufsize = sc->sc_ilen - 1;
21435717820Sjason 	uca.obufsize = sc->sc_olen - 1;
21535717820Sjason 	uca.ibufsizepad = 1;
21635717820Sjason 	uca.opkthdrlen = 0;
2172c2d5c4aSderaadt 	uca.uhidev = sc->sc_hdev.sc_parent;
21835717820Sjason 	uca.device = uaa->device;
21935717820Sjason 	uca.iface = uaa->iface;
22035717820Sjason 	uca.methods = &ucycom_methods;
22135717820Sjason 	uca.arg = sc;
22235717820Sjason 	uca.info = NULL;
22335717820Sjason 
22435717820Sjason 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
22535717820Sjason 	DPRINTF(("ucycom_attach: complete %p\n", sc->sc_subdev));
22635717820Sjason }
22735717820Sjason 
22835717820Sjason void
ucycom_get_status(void * addr,int portno,u_char * lsr,u_char * msr)22935717820Sjason ucycom_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
23035717820Sjason {
23135717820Sjason 	struct ucycom_softc *sc = addr;
23235717820Sjason 
23335717820Sjason 	DPRINTF(("ucycom_get_status:\n"));
23435717820Sjason 
23535717820Sjason #if 0
23635717820Sjason 	if (lsr != NULL)
23735717820Sjason 		*lsr = sc->sc_lsr;
23835717820Sjason #endif
23935717820Sjason 	if (msr != NULL)
24035717820Sjason 		*msr = sc->sc_msr;
24135717820Sjason }
24235717820Sjason 
24335717820Sjason int
ucycom_open(void * addr,int portno)24435717820Sjason ucycom_open(void *addr, int portno)
24535717820Sjason {
24635717820Sjason 	struct ucycom_softc *sc = addr;
24735717820Sjason 	struct termios t;
24835717820Sjason 	int err;
24935717820Sjason 
25035717820Sjason 	DPRINTF(("ucycom_open: complete\n"));
25135717820Sjason 
2529f2e1236Spirofti 	if (usbd_is_dying(sc->sc_udev))
25335717820Sjason 		return (EIO);
25435717820Sjason 
25535717820Sjason 	/* Allocate an output report buffer */
25645235b13Sfgsch 	sc->sc_obuf = malloc(sc->sc_olen, M_USBDEV, M_WAITOK | M_ZERO);
25735717820Sjason 
25835717820Sjason 	/* Allocate an input report buffer */
25935717820Sjason 	sc->sc_ibuf = malloc(sc->sc_ilen, M_USBDEV, M_WAITOK);
26035717820Sjason 
26135717820Sjason 	DPRINTF(("ucycom_open: sc->sc_ibuf=%p sc->sc_obuf=%p \n",
26235717820Sjason 	    sc->sc_ibuf, sc->sc_obuf));
26335717820Sjason 
26435717820Sjason 	t.c_ospeed = 9600;
26535717820Sjason 	t.c_cflag = CSTOPB | CS8;
26635717820Sjason 	(void)ucycom_param(sc, portno, &t);
26735717820Sjason 
26835717820Sjason 	sc->sc_mcr = UCYCOM_DTR | UCYCOM_RTS;
26935717820Sjason 	sc->sc_obuf[0] = sc->sc_mcr;
27035717820Sjason 	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
27135717820Sjason 	if (err) {
27235717820Sjason 		DPRINTF(("ucycom_open: set RTS err=%d\n", err));
27335717820Sjason 		return (EIO);
27435717820Sjason 	}
27535717820Sjason 
27635717820Sjason 	return (0);
27735717820Sjason }
27835717820Sjason 
27935717820Sjason void
ucycom_close(void * addr,int portno)28035717820Sjason ucycom_close(void *addr, int portno)
28135717820Sjason {
28235717820Sjason 	struct ucycom_softc *sc = addr;
283f5582d7fSjason 	int s;
28435717820Sjason 
2859f2e1236Spirofti 	if (usbd_is_dying(sc->sc_udev))
28635717820Sjason 		return;
28735717820Sjason 
288f5582d7fSjason 	s = splusb();
28935717820Sjason 	if (sc->sc_obuf != NULL) {
290234dfda1Sderaadt 		free(sc->sc_obuf, M_USBDEV, sc->sc_olen);
29135717820Sjason 		sc->sc_obuf = NULL;
29235717820Sjason 	}
29335717820Sjason 	if (sc->sc_ibuf != NULL) {
294234dfda1Sderaadt 		free(sc->sc_ibuf, M_USBDEV, sc->sc_ilen);
29535717820Sjason 		sc->sc_ibuf = NULL;
29635717820Sjason 	}
297f5582d7fSjason 	splx(s);
29835717820Sjason }
29935717820Sjason 
30078315254Smbalmer void
ucycom_read(void * addr,int portno,u_char ** ptr,u_int32_t * count)30135717820Sjason ucycom_read(void *addr, int portno, u_char **ptr, u_int32_t *count)
30235717820Sjason {
30335717820Sjason 	struct ucycom_softc *sc = addr;
30435717820Sjason 
30535717820Sjason 	if (sc->sc_newmsr ^ sc->sc_msr) {
30635717820Sjason 		DPRINTF(("ucycom_read: msr %d new %d\n",
30735717820Sjason 		    sc->sc_msr, sc->sc_newmsr));
30835717820Sjason 		sc->sc_msr = sc->sc_newmsr;
30935717820Sjason 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
31035717820Sjason 	}
31135717820Sjason 
31235717820Sjason 	DPRINTF(("ucycom_read: buf %p chars %d\n", sc->sc_ibuf, sc->sc_icnt));
31335717820Sjason 	*ptr = sc->sc_ibuf;
31435717820Sjason 	*count = sc->sc_icnt;
31535717820Sjason }
31635717820Sjason 
31778315254Smbalmer void
ucycom_write(void * addr,int portno,u_char * to,u_char * data,u_int32_t * cnt)31835717820Sjason ucycom_write(void *addr, int portno, u_char *to, u_char *data, u_int32_t *cnt)
31935717820Sjason {
32035717820Sjason 	struct ucycom_softc *sc = addr;
32135717820Sjason 	u_int32_t len;
32235717820Sjason #ifdef UCYCOM_DEBUG
32335717820Sjason 	u_int32_t want = *cnt;
32435717820Sjason #endif
32535717820Sjason 
32635717820Sjason 	/*
32735717820Sjason 	 * The 8 byte output report uses byte 0 for control and byte
32835717820Sjason 	 * count.
32935717820Sjason 	 *
33035717820Sjason 	 * The 32 byte output report uses byte 0 for control. Byte 1
33135717820Sjason 	 * is used for byte count.
33235717820Sjason 	 */
33335717820Sjason 	len = sc->sc_olen;
33435717820Sjason 	memset(to, 0, len);
33535717820Sjason 	switch (sc->sc_olen) {
33635717820Sjason 	case 8:
33735717820Sjason 		to[0] = *cnt | sc->sc_mcr;
33835717820Sjason 		memcpy(&to[1], data, *cnt);
33935717820Sjason 		DPRINTF(("ucycomstart(8): to[0] = %d | %d = %d\n",
34035717820Sjason 		    *cnt, sc->sc_mcr, to[0]));
34135717820Sjason 		break;
34235717820Sjason 
34335717820Sjason 	case 32:
34435717820Sjason 		to[0] = sc->sc_mcr;
34535717820Sjason 		to[1] = *cnt;
34635717820Sjason 		memcpy(&to[2], data, *cnt);
34735717820Sjason 		DPRINTF(("ucycomstart(32): to[0] = %d\nto[1] = %d\n",
34835717820Sjason 		    to[0], to[1]));
34935717820Sjason 		break;
35035717820Sjason 	}
35135717820Sjason 
35235717820Sjason #ifdef UCYCOM_DEBUG
35335717820Sjason 	if (ucycomdebug > 5) {
35435717820Sjason 		int i;
35535717820Sjason 
35635717820Sjason 		if (len != 0) {
35735717820Sjason 			DPRINTF(("ucycomstart: to[0..%d) =", len-1));
35835717820Sjason 			for (i = 0; i < len; i++)
35935717820Sjason 				DPRINTF((" %02x", to[i]));
36035717820Sjason 			DPRINTF(("\n"));
36135717820Sjason 		}
36235717820Sjason 	}
36335717820Sjason #endif
36435717820Sjason 	*cnt = len;
36535717820Sjason 
36635717820Sjason 	DPRINTFN(4,("ucycomstart: req %d chars did %d chars\n", want, len));
36735717820Sjason }
36835717820Sjason 
36978315254Smbalmer int
ucycom_param(void * addr,int portno,struct termios * t)37035717820Sjason ucycom_param(void *addr, int portno, struct termios *t)
37135717820Sjason {
37235717820Sjason 	struct ucycom_softc *sc = addr;
37335717820Sjason 	uint8_t report[5];
374cc1678f7Smpi 	size_t rlen;
37535717820Sjason 	uint32_t baud = 0;
37635717820Sjason 	uint8_t cfg;
37735717820Sjason 
3789f2e1236Spirofti 	if (usbd_is_dying(sc->sc_udev))
37935717820Sjason 		return (EIO);
38035717820Sjason 
38135717820Sjason 	switch (t->c_ospeed) {
38235717820Sjason 	case 600:
38335717820Sjason 	case 1200:
38435717820Sjason 	case 2400:
38535717820Sjason 	case 4800:
38635717820Sjason 	case 9600:
38735717820Sjason 	case 19200:
38835717820Sjason 	case 38400:
38935717820Sjason 	case 57600:
39035717820Sjason #if 0
39135717820Sjason 	/*
39235717820Sjason 	 * Stock chips only support standard baud rates in the 600 - 57600
39335717820Sjason 	 * range, but higher rates can be achieved using custom firmware.
39435717820Sjason 	 */
39535717820Sjason 	case 115200:
39635717820Sjason 	case 153600:
39735717820Sjason 	case 192000:
39835717820Sjason #endif
39935717820Sjason 		baud = t->c_ospeed;
40035717820Sjason 		break;
40135717820Sjason 	default:
40235717820Sjason 		return (EINVAL);
40335717820Sjason 	}
40435717820Sjason 
40535717820Sjason 	if (t->c_cflag & CIGNORE) {
40635717820Sjason 		cfg = sc->sc_cfg;
40735717820Sjason 	} else {
40835717820Sjason 		cfg = 0;
40935717820Sjason 		switch (t->c_cflag & CSIZE) {
41035717820Sjason 		case CS8:
41135717820Sjason 			cfg |= UCYCOM_DATA_BITS_8;
41235717820Sjason 			break;
41335717820Sjason 		case CS7:
41435717820Sjason 			cfg |= UCYCOM_DATA_BITS_7;
41535717820Sjason 			break;
41635717820Sjason 		case CS6:
41735717820Sjason 			cfg |= UCYCOM_DATA_BITS_6;
41835717820Sjason 			break;
41935717820Sjason 		case CS5:
42035717820Sjason 			cfg |= UCYCOM_DATA_BITS_5;
42135717820Sjason 			break;
42235717820Sjason 		default:
42335717820Sjason 			return (EINVAL);
42435717820Sjason 		}
42535717820Sjason 		cfg |= ISSET(t->c_cflag, CSTOPB) ?
42635717820Sjason 		    UCYCOM_STOP_BITS_2 : UCYCOM_STOP_BITS_1;
42735717820Sjason 		cfg |= ISSET(t->c_cflag, PARENB) ?
42835717820Sjason 		    UCYCOM_PARITY_ON : UCYCOM_PARITY_OFF;
42935717820Sjason 		cfg |= ISSET(t->c_cflag, PARODD) ?
43035717820Sjason 		    UCYCOM_PARITY_ODD : UCYCOM_PARITY_EVEN;
43135717820Sjason 	}
43235717820Sjason 
43335717820Sjason 	DPRINTF(("ucycom_param: setting %d baud, %d-%c-%d (%d)\n", baud,
43435717820Sjason 	    5 + (cfg & UCYCOM_DATA_MASK),
43535717820Sjason 	    (cfg & UCYCOM_PARITY_MASK) ?
43635717820Sjason 		((cfg & UCYCOM_PARITY_TYPE_MASK) ? 'O' : 'E') : 'N',
43735717820Sjason 	    (cfg & UCYCOM_STOP_MASK) ? 2 : 1, cfg));
43835717820Sjason 
43935717820Sjason 	report[0] = baud & 0xff;
44035717820Sjason 	report[1] = (baud >> 8) & 0xff;
44135717820Sjason 	report[2] = (baud >> 16) & 0xff;
44235717820Sjason 	report[3] = (baud >> 24) & 0xff;
44335717820Sjason 	report[4] = cfg;
444cc1678f7Smpi 	rlen = MIN(sc->sc_flen, sizeof(report));
445e6a02383Smpi 	if (uhidev_set_report(sc->sc_hdev.sc_parent, UHID_FEATURE_REPORT,
446cc1678f7Smpi 	    sc->sc_hdev.sc_report_id, report, rlen) != rlen)
44735717820Sjason 		return EIO;
44835717820Sjason 	sc->sc_baud = baud;
449832e293aSjsg 	return (0);
45035717820Sjason }
45135717820Sjason 
45278315254Smbalmer void
ucycom_intr(struct uhidev * addr,void * ibuf,u_int len)45335717820Sjason ucycom_intr(struct uhidev *addr, void *ibuf, u_int len)
45435717820Sjason {
455ab0b1be7Smglocker 	extern void ucomreadcb(struct usbd_xfer *, void *, usbd_status);
45635717820Sjason 	struct ucycom_softc *sc = (struct ucycom_softc *)addr;
45735717820Sjason 	uint8_t *cp = ibuf;
45835717820Sjason 	int n, st, s;
45935717820Sjason 
46035717820Sjason 	/* not accepting data anymore.. */
46135717820Sjason 	if (sc->sc_ibuf == NULL)
46235717820Sjason 		return;
46335717820Sjason 
46435717820Sjason 	/* We understand 8 byte and 32 byte input records */
46535717820Sjason 	switch (len) {
46635717820Sjason 	case 8:
46735717820Sjason 		n = cp[0] & UCYCOM_LMASK;
46835717820Sjason 		st = cp[0] & ~UCYCOM_LMASK;
46935717820Sjason 		cp++;
47035717820Sjason 		break;
47135717820Sjason 
47235717820Sjason 	case 32:
47335717820Sjason 		st = cp[0];
47435717820Sjason 		n = cp[1];
47535717820Sjason 		cp += 2;
47635717820Sjason 		break;
47735717820Sjason 
47835717820Sjason 	default:
47935717820Sjason 		DPRINTFN(3,("ucycom_intr: Unknown input report length\n"));
48035717820Sjason 		return;
48135717820Sjason 	}
48235717820Sjason 
48335717820Sjason #ifdef UCYCOM_DEBUG
48435717820Sjason 	if (ucycomdebug > 5) {
48535717820Sjason 		u_int32_t i;
48635717820Sjason 
48735717820Sjason 		if (n != 0) {
48835717820Sjason 			DPRINTF(("ucycom_intr: ibuf[0..%d) =", n));
48935717820Sjason 			for (i = 0; i < n; i++)
49035717820Sjason 				DPRINTF((" %02x", cp[i]));
49135717820Sjason 			DPRINTF(("\n"));
49235717820Sjason 		}
49335717820Sjason 	}
49435717820Sjason #endif
49535717820Sjason 
49635717820Sjason 	if (n > 0 || st != sc->sc_msr) {
49735717820Sjason 		s = spltty();
49835717820Sjason 		sc->sc_newmsr = st;
49935717820Sjason 		bcopy(cp, sc->sc_ibuf, n);
50035717820Sjason 		sc->sc_icnt = n;
50135717820Sjason 		ucomreadcb(addr->sc_parent->sc_ixfer, sc->sc_subdev,
50235717820Sjason 		    USBD_NORMAL_COMPLETION);
50335717820Sjason 		splx(s);
50435717820Sjason 	}
50535717820Sjason }
50635717820Sjason 
50778315254Smbalmer void
ucycom_set(void * addr,int portno,int reg,int onoff)50835717820Sjason ucycom_set(void *addr, int portno, int reg, int onoff)
50935717820Sjason {
51035717820Sjason 	struct ucycom_softc *sc = addr;
51135717820Sjason 	int err;
51235717820Sjason 
51335717820Sjason 	switch (reg) {
51435717820Sjason 	case UCOM_SET_DTR:
51535717820Sjason 		if (onoff)
51635717820Sjason 			SET(sc->sc_mcr, UCYCOM_DTR);
51735717820Sjason 		else
51835717820Sjason 			CLR(sc->sc_mcr, UCYCOM_DTR);
51935717820Sjason 		break;
52035717820Sjason 	case UCOM_SET_RTS:
52135717820Sjason 		if (onoff)
52235717820Sjason 			SET(sc->sc_mcr, UCYCOM_RTS);
52335717820Sjason 		else
52435717820Sjason 			CLR(sc->sc_mcr, UCYCOM_RTS);
52535717820Sjason 		break;
52635717820Sjason 	case UCOM_SET_BREAK:
52735717820Sjason 		break;
52835717820Sjason 	}
52935717820Sjason 
53035717820Sjason 	memset(sc->sc_obuf, 0, sc->sc_olen);
53135717820Sjason 	sc->sc_obuf[0] = sc->sc_mcr;
53235717820Sjason 
53335717820Sjason 	err = uhidev_write(sc->sc_hdev.sc_parent, sc->sc_obuf, sc->sc_olen);
53435717820Sjason 	if (err)
53535717820Sjason 		DPRINTF(("ucycom_set_status: err=%d\n", err));
53635717820Sjason }
53735717820Sjason 
53835717820Sjason int
ucycom_detach(struct device * self,int flags)53935717820Sjason ucycom_detach(struct device *self, int flags)
54035717820Sjason {
54135717820Sjason 	struct ucycom_softc *sc = (struct ucycom_softc *)self;
54235717820Sjason 
54335717820Sjason 	DPRINTF(("ucycom_detach: sc=%p flags=%d\n", sc, flags));
54435717820Sjason 	if (sc->sc_subdev != NULL) {
54535717820Sjason 		config_detach(sc->sc_subdev, flags);
54635717820Sjason 		sc->sc_subdev = NULL;
54735717820Sjason 	}
5489dcdfc61Smpi 
5499dcdfc61Smpi 	if (sc->sc_hdev.sc_state & UHIDEV_OPEN)
5509dcdfc61Smpi 		uhidev_close(&sc->sc_hdev);
5519dcdfc61Smpi 
55235717820Sjason 	return (0);
55335717820Sjason }
554