xref: /freebsd/sys/dev/usb/serial/uchcom.c (revision ece4b0bd43bfb226fac07e61c933bb3d3d2c2c36)
102ac6454SAndrew Thompson /*	$NetBSD: uchcom.c,v 1.1 2007/09/03 17:57:37 tshiozak Exp $	*/
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson /*-
402ac6454SAndrew Thompson  * Copyright (c) 2007, Takanori Watanabe
502ac6454SAndrew Thompson  * All rights reserved.
602ac6454SAndrew Thompson  *
702ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
802ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
902ac6454SAndrew Thompson  * are met:
1002ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1102ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1202ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1402ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1502ac6454SAndrew Thompson  *
1602ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1702ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1802ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1902ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2002ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2102ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2202ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2302ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2402ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2502ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2602ac6454SAndrew Thompson  * SUCH DAMAGE.
2702ac6454SAndrew Thompson  */
2802ac6454SAndrew Thompson 
2902ac6454SAndrew Thompson /*
3002ac6454SAndrew Thompson  * Copyright (c) 2007 The NetBSD Foundation, Inc.
3102ac6454SAndrew Thompson  * All rights reserved.
3202ac6454SAndrew Thompson  *
3302ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
3402ac6454SAndrew Thompson  * by Takuya SHIOZAKI (tshiozak@netbsd.org).
3502ac6454SAndrew Thompson  *
3602ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
3702ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
3802ac6454SAndrew Thompson  * are met:
3902ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
4002ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
4102ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
4202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
4302ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
4402ac6454SAndrew Thompson  *
4502ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
4602ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4702ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4802ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
4902ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5002ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5102ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5202ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5302ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5402ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
5502ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
5602ac6454SAndrew Thompson  */
5702ac6454SAndrew Thompson 
5802ac6454SAndrew Thompson #include <sys/cdefs.h>
5902ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
6002ac6454SAndrew Thompson 
6102ac6454SAndrew Thompson /*
6211f35cfaSAndrew Thompson  * Driver for WinChipHead CH341/340, the worst USB-serial chip in the
6311f35cfaSAndrew Thompson  * world.
6402ac6454SAndrew Thompson  */
6502ac6454SAndrew Thompson 
66ed6d949aSAndrew Thompson #include <sys/stdint.h>
67ed6d949aSAndrew Thompson #include <sys/stddef.h>
68ed6d949aSAndrew Thompson #include <sys/param.h>
69ed6d949aSAndrew Thompson #include <sys/queue.h>
70ed6d949aSAndrew Thompson #include <sys/types.h>
71ed6d949aSAndrew Thompson #include <sys/systm.h>
72ed6d949aSAndrew Thompson #include <sys/kernel.h>
73ed6d949aSAndrew Thompson #include <sys/bus.h>
74ed6d949aSAndrew Thompson #include <sys/module.h>
75ed6d949aSAndrew Thompson #include <sys/lock.h>
76ed6d949aSAndrew Thompson #include <sys/mutex.h>
77ed6d949aSAndrew Thompson #include <sys/condvar.h>
78ed6d949aSAndrew Thompson #include <sys/sysctl.h>
79ed6d949aSAndrew Thompson #include <sys/sx.h>
80ed6d949aSAndrew Thompson #include <sys/unistd.h>
81ed6d949aSAndrew Thompson #include <sys/callout.h>
82ed6d949aSAndrew Thompson #include <sys/malloc.h>
83ed6d949aSAndrew Thompson #include <sys/priv.h>
84ed6d949aSAndrew Thompson 
8502ac6454SAndrew Thompson #include <dev/usb/usb.h>
86ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
87ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
88ed6d949aSAndrew Thompson #include "usbdevs.h"
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson #define	USB_DEBUG_VAR uchcom_debug
9102ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
9202ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
9502ac6454SAndrew Thompson 
96b850ecc1SAndrew Thompson #ifdef USB_DEBUG
9702ac6454SAndrew Thompson static int uchcom_debug = 0;
9802ac6454SAndrew Thompson 
996472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, uchcom, CTLFLAG_RW, 0, "USB uchcom");
100*ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uchcom, OID_AUTO, debug, CTLFLAG_RWTUN,
10102ac6454SAndrew Thompson     &uchcom_debug, 0, "uchcom debug level");
10202ac6454SAndrew Thompson #endif
10302ac6454SAndrew Thompson 
10402ac6454SAndrew Thompson #define	UCHCOM_IFACE_INDEX	0
10502ac6454SAndrew Thompson #define	UCHCOM_CONFIG_INDEX	0
10602ac6454SAndrew Thompson 
10702ac6454SAndrew Thompson #define	UCHCOM_REV_CH340	0x0250
10802ac6454SAndrew Thompson #define	UCHCOM_INPUT_BUF_SIZE	8
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson #define	UCHCOM_REQ_GET_VERSION	0x5F
11102ac6454SAndrew Thompson #define	UCHCOM_REQ_READ_REG	0x95
11202ac6454SAndrew Thompson #define	UCHCOM_REQ_WRITE_REG	0x9A
11302ac6454SAndrew Thompson #define	UCHCOM_REQ_RESET	0xA1
11402ac6454SAndrew Thompson #define	UCHCOM_REQ_SET_DTRRTS	0xA4
11502ac6454SAndrew Thompson 
11602ac6454SAndrew Thompson #define	UCHCOM_REG_STAT1	0x06
11702ac6454SAndrew Thompson #define	UCHCOM_REG_STAT2	0x07
11802ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_PRE	0x12
11902ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_DIV	0x13
12002ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_MOD	0x14
12102ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_PAD	0x0F
12202ac6454SAndrew Thompson #define	UCHCOM_REG_BREAK1	0x05
12302ac6454SAndrew Thompson #define	UCHCOM_REG_BREAK2	0x18
12402ac6454SAndrew Thompson #define	UCHCOM_REG_LCR1		0x18
12502ac6454SAndrew Thompson #define	UCHCOM_REG_LCR2		0x25
12602ac6454SAndrew Thompson 
12702ac6454SAndrew Thompson #define	UCHCOM_VER_20		0x20
12802ac6454SAndrew Thompson 
12902ac6454SAndrew Thompson #define	UCHCOM_BASE_UNKNOWN	0
13002ac6454SAndrew Thompson #define	UCHCOM_BPS_MOD_BASE	20000000
13102ac6454SAndrew Thompson #define	UCHCOM_BPS_MOD_BASE_OFS	1100
13202ac6454SAndrew Thompson 
13302ac6454SAndrew Thompson #define	UCHCOM_DTR_MASK		0x20
13402ac6454SAndrew Thompson #define	UCHCOM_RTS_MASK		0x40
13502ac6454SAndrew Thompson 
13602ac6454SAndrew Thompson #define	UCHCOM_BRK1_MASK	0x01
13702ac6454SAndrew Thompson #define	UCHCOM_BRK2_MASK	0x40
13802ac6454SAndrew Thompson 
13902ac6454SAndrew Thompson #define	UCHCOM_LCR1_MASK	0xAF
14002ac6454SAndrew Thompson #define	UCHCOM_LCR2_MASK	0x07
14102ac6454SAndrew Thompson #define	UCHCOM_LCR1_PARENB	0x80
14202ac6454SAndrew Thompson #define	UCHCOM_LCR2_PAREVEN	0x07
14302ac6454SAndrew Thompson #define	UCHCOM_LCR2_PARODD	0x06
14402ac6454SAndrew Thompson #define	UCHCOM_LCR2_PARMARK	0x05
14502ac6454SAndrew Thompson #define	UCHCOM_LCR2_PARSPACE	0x04
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson #define	UCHCOM_INTR_STAT1	0x02
14802ac6454SAndrew Thompson #define	UCHCOM_INTR_STAT2	0x03
14902ac6454SAndrew Thompson #define	UCHCOM_INTR_LEAST	4
15002ac6454SAndrew Thompson 
15102ac6454SAndrew Thompson #define	UCHCOM_BULK_BUF_SIZE 1024	/* bytes */
15202ac6454SAndrew Thompson 
15302ac6454SAndrew Thompson enum {
15402ac6454SAndrew Thompson 	UCHCOM_BULK_DT_WR,
15502ac6454SAndrew Thompson 	UCHCOM_BULK_DT_RD,
15602ac6454SAndrew Thompson 	UCHCOM_INTR_DT_RD,
15702ac6454SAndrew Thompson 	UCHCOM_N_TRANSFER,
15802ac6454SAndrew Thompson };
15902ac6454SAndrew Thompson 
16002ac6454SAndrew Thompson struct uchcom_softc {
161760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
162760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
16302ac6454SAndrew Thompson 
164760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UCHCOM_N_TRANSFER];
165760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
166deefe583SAndrew Thompson 	struct mtx sc_mtx;
16702ac6454SAndrew Thompson 
16802ac6454SAndrew Thompson 	uint8_t	sc_dtr;			/* local copy */
16902ac6454SAndrew Thompson 	uint8_t	sc_rts;			/* local copy */
17002ac6454SAndrew Thompson 	uint8_t	sc_version;
17102ac6454SAndrew Thompson 	uint8_t	sc_msr;
17202ac6454SAndrew Thompson 	uint8_t	sc_lsr;			/* local status register */
17302ac6454SAndrew Thompson };
17402ac6454SAndrew Thompson 
17502ac6454SAndrew Thompson struct uchcom_divider {
17602ac6454SAndrew Thompson 	uint8_t	dv_prescaler;
17702ac6454SAndrew Thompson 	uint8_t	dv_div;
17802ac6454SAndrew Thompson 	uint8_t	dv_mod;
17902ac6454SAndrew Thompson };
18002ac6454SAndrew Thompson 
18102ac6454SAndrew Thompson struct uchcom_divider_record {
18202ac6454SAndrew Thompson 	uint32_t dvr_high;
18302ac6454SAndrew Thompson 	uint32_t dvr_low;
18402ac6454SAndrew Thompson 	uint32_t dvr_base_clock;
18502ac6454SAndrew Thompson 	struct uchcom_divider dvr_divider;
18602ac6454SAndrew Thompson };
18702ac6454SAndrew Thompson 
18802ac6454SAndrew Thompson static const struct uchcom_divider_record dividers[] =
18902ac6454SAndrew Thompson {
19002ac6454SAndrew Thompson 	{307200, 307200, UCHCOM_BASE_UNKNOWN, {7, 0xD9, 0}},
19102ac6454SAndrew Thompson 	{921600, 921600, UCHCOM_BASE_UNKNOWN, {7, 0xF3, 0}},
19202ac6454SAndrew Thompson 	{2999999, 23530, 6000000, {3, 0, 0}},
19302ac6454SAndrew Thompson 	{23529, 2942, 750000, {2, 0, 0}},
19402ac6454SAndrew Thompson 	{2941, 368, 93750, {1, 0, 0}},
19502ac6454SAndrew Thompson 	{367, 1, 11719, {0, 0, 0}},
19602ac6454SAndrew Thompson };
19702ac6454SAndrew Thompson 
19802ac6454SAndrew Thompson #define	NUM_DIVIDERS	(sizeof (dividers) / sizeof (dividers[0]))
19902ac6454SAndrew Thompson 
200f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uchcom_devs[] = {
20102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_WCH, USB_PRODUCT_WCH_CH341SER, 0)},
20211f35cfaSAndrew Thompson 	{USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER, 0)},
20351638137SGavin Atkinson 	{USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER_2, 0)},
20402ac6454SAndrew Thompson };
20502ac6454SAndrew Thompson 
20602ac6454SAndrew Thompson /* protypes */
20702ac6454SAndrew Thompson 
2085805d178SHans Petter Selasky static void	uchcom_free(struct ucom_softc *);
209760bc48eSAndrew Thompson static int	uchcom_pre_param(struct ucom_softc *, struct termios *);
210760bc48eSAndrew Thompson static void	uchcom_cfg_get_status(struct ucom_softc *, uint8_t *,
21102ac6454SAndrew Thompson 		    uint8_t *);
21211f35cfaSAndrew Thompson static void	uchcom_cfg_open(struct ucom_softc *ucom);
213760bc48eSAndrew Thompson static void	uchcom_cfg_param(struct ucom_softc *, struct termios *);
214760bc48eSAndrew Thompson static void	uchcom_cfg_set_break(struct ucom_softc *, uint8_t);
215760bc48eSAndrew Thompson static void	uchcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
216760bc48eSAndrew Thompson static void	uchcom_cfg_set_rts(struct ucom_softc *, uint8_t);
217760bc48eSAndrew Thompson static void	uchcom_start_read(struct ucom_softc *);
218760bc48eSAndrew Thompson static void	uchcom_start_write(struct ucom_softc *);
219760bc48eSAndrew Thompson static void	uchcom_stop_read(struct ucom_softc *);
220760bc48eSAndrew Thompson static void	uchcom_stop_write(struct ucom_softc *);
22102ac6454SAndrew Thompson static void	uchcom_update_version(struct uchcom_softc *);
22202ac6454SAndrew Thompson static void	uchcom_convert_status(struct uchcom_softc *, uint8_t);
22302ac6454SAndrew Thompson static void	uchcom_update_status(struct uchcom_softc *);
22411f35cfaSAndrew Thompson static void	uchcom_set_dtr_rts(struct uchcom_softc *);
22502ac6454SAndrew Thompson static int	uchcom_calc_divider_settings(struct uchcom_divider *, uint32_t);
22611f35cfaSAndrew Thompson static void	uchcom_set_baudrate(struct uchcom_softc *, uint32_t);
227655dc9d0SAndrew Thompson static void	uchcom_poll(struct ucom_softc *ucom);
22802ac6454SAndrew Thompson 
22902ac6454SAndrew Thompson static device_probe_t uchcom_probe;
23002ac6454SAndrew Thompson static device_attach_t uchcom_attach;
23102ac6454SAndrew Thompson static device_detach_t uchcom_detach;
232c01fc06eSHans Petter Selasky static void uchcom_free_softc(struct uchcom_softc *);
23302ac6454SAndrew Thompson 
234e0a69b51SAndrew Thompson static usb_callback_t uchcom_intr_callback;
235e0a69b51SAndrew Thompson static usb_callback_t uchcom_write_callback;
236e0a69b51SAndrew Thompson static usb_callback_t uchcom_read_callback;
23702ac6454SAndrew Thompson 
238760bc48eSAndrew Thompson static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson 	[UCHCOM_BULK_DT_WR] = {
24102ac6454SAndrew Thompson 		.type = UE_BULK,
24202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
24302ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2444eae601eSAndrew Thompson 		.bufsize = UCHCOM_BULK_BUF_SIZE,
2454eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2464eae601eSAndrew Thompson 		.callback = &uchcom_write_callback,
24702ac6454SAndrew Thompson 	},
24802ac6454SAndrew Thompson 
24902ac6454SAndrew Thompson 	[UCHCOM_BULK_DT_RD] = {
25002ac6454SAndrew Thompson 		.type = UE_BULK,
25102ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
25202ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2534eae601eSAndrew Thompson 		.bufsize = UCHCOM_BULK_BUF_SIZE,
2544eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2554eae601eSAndrew Thompson 		.callback = &uchcom_read_callback,
25602ac6454SAndrew Thompson 	},
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson 	[UCHCOM_INTR_DT_RD] = {
25902ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
26002ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
26102ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2624eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2634eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
2644eae601eSAndrew Thompson 		.callback = &uchcom_intr_callback,
26502ac6454SAndrew Thompson 	},
26602ac6454SAndrew Thompson };
26702ac6454SAndrew Thompson 
268a8675caeSAndrew Thompson static struct ucom_callback uchcom_callback = {
269a593f6b8SAndrew Thompson 	.ucom_cfg_get_status = &uchcom_cfg_get_status,
270a593f6b8SAndrew Thompson 	.ucom_cfg_set_dtr = &uchcom_cfg_set_dtr,
271a593f6b8SAndrew Thompson 	.ucom_cfg_set_rts = &uchcom_cfg_set_rts,
272a593f6b8SAndrew Thompson 	.ucom_cfg_set_break = &uchcom_cfg_set_break,
27311f35cfaSAndrew Thompson 	.ucom_cfg_open = &uchcom_cfg_open,
274a593f6b8SAndrew Thompson 	.ucom_cfg_param = &uchcom_cfg_param,
275a593f6b8SAndrew Thompson 	.ucom_pre_param = &uchcom_pre_param,
276a593f6b8SAndrew Thompson 	.ucom_start_read = &uchcom_start_read,
277a593f6b8SAndrew Thompson 	.ucom_stop_read = &uchcom_stop_read,
278a593f6b8SAndrew Thompson 	.ucom_start_write = &uchcom_start_write,
279a593f6b8SAndrew Thompson 	.ucom_stop_write = &uchcom_stop_write,
280655dc9d0SAndrew Thompson 	.ucom_poll = &uchcom_poll,
2815805d178SHans Petter Selasky 	.ucom_free = &uchcom_free,
28202ac6454SAndrew Thompson };
28302ac6454SAndrew Thompson 
28402ac6454SAndrew Thompson /* ----------------------------------------------------------------------
28502ac6454SAndrew Thompson  * driver entry points
28602ac6454SAndrew Thompson  */
28702ac6454SAndrew Thompson 
28802ac6454SAndrew Thompson static int
28902ac6454SAndrew Thompson uchcom_probe(device_t dev)
29002ac6454SAndrew Thompson {
291760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
29202ac6454SAndrew Thompson 
29302ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
29402ac6454SAndrew Thompson 
295f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
29602ac6454SAndrew Thompson 		return (ENXIO);
29702ac6454SAndrew Thompson 	}
29802ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
29902ac6454SAndrew Thompson 		return (ENXIO);
30002ac6454SAndrew Thompson 	}
30102ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
30202ac6454SAndrew Thompson 		return (ENXIO);
30302ac6454SAndrew Thompson 	}
304a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
30502ac6454SAndrew Thompson }
30602ac6454SAndrew Thompson 
30702ac6454SAndrew Thompson static int
30802ac6454SAndrew Thompson uchcom_attach(device_t dev)
30902ac6454SAndrew Thompson {
31002ac6454SAndrew Thompson 	struct uchcom_softc *sc = device_get_softc(dev);
311760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
31202ac6454SAndrew Thompson 	int error;
31302ac6454SAndrew Thompson 	uint8_t iface_index;
31402ac6454SAndrew Thompson 
31502ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
31602ac6454SAndrew Thompson 
317a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
318deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uchcom", NULL, MTX_DEF);
3195805d178SHans Petter Selasky 	ucom_ref(&sc->sc_super_ucom);
32002ac6454SAndrew Thompson 
32102ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
32202ac6454SAndrew Thompson 
32302ac6454SAndrew Thompson 	switch (uaa->info.bcdDevice) {
32402ac6454SAndrew Thompson 	case UCHCOM_REV_CH340:
32502ac6454SAndrew Thompson 		device_printf(dev, "CH340 detected\n");
32602ac6454SAndrew Thompson 		break;
32702ac6454SAndrew Thompson 	default:
32802ac6454SAndrew Thompson 		device_printf(dev, "CH341 detected\n");
32902ac6454SAndrew Thompson 		break;
33002ac6454SAndrew Thompson 	}
33102ac6454SAndrew Thompson 
33202ac6454SAndrew Thompson 	iface_index = UCHCOM_IFACE_INDEX;
333a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
33402ac6454SAndrew Thompson 	    &iface_index, sc->sc_xfer, uchcom_config_data,
335deefe583SAndrew Thompson 	    UCHCOM_N_TRANSFER, sc, &sc->sc_mtx);
33602ac6454SAndrew Thompson 
33702ac6454SAndrew Thompson 	if (error) {
33802ac6454SAndrew Thompson 		DPRINTF("one or more missing USB endpoints, "
339a593f6b8SAndrew Thompson 		    "error=%s\n", usbd_errstr(error));
34002ac6454SAndrew Thompson 		goto detach;
34102ac6454SAndrew Thompson 	}
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson 	/* clear stall at first run */
344deefe583SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
345ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
346ed6d949aSAndrew Thompson 	usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
347deefe583SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
34802ac6454SAndrew Thompson 
349a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
350deefe583SAndrew Thompson 	    &uchcom_callback, &sc->sc_mtx);
35102ac6454SAndrew Thompson 	if (error) {
35202ac6454SAndrew Thompson 		goto detach;
35302ac6454SAndrew Thompson 	}
3546416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
3556416c259SNick Hibma 
35602ac6454SAndrew Thompson 	return (0);
35702ac6454SAndrew Thompson 
35802ac6454SAndrew Thompson detach:
35902ac6454SAndrew Thompson 	uchcom_detach(dev);
36002ac6454SAndrew Thompson 	return (ENXIO);
36102ac6454SAndrew Thompson }
36202ac6454SAndrew Thompson 
36302ac6454SAndrew Thompson static int
36402ac6454SAndrew Thompson uchcom_detach(device_t dev)
36502ac6454SAndrew Thompson {
36602ac6454SAndrew Thompson 	struct uchcom_softc *sc = device_get_softc(dev);
36702ac6454SAndrew Thompson 
36802ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
36902ac6454SAndrew Thompson 
370015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
371a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER);
37202ac6454SAndrew Thompson 
373c01fc06eSHans Petter Selasky 	device_claim_softc(dev);
374c01fc06eSHans Petter Selasky 
375c01fc06eSHans Petter Selasky 	uchcom_free_softc(sc);
376c01fc06eSHans Petter Selasky 
37702ac6454SAndrew Thompson 	return (0);
37802ac6454SAndrew Thompson }
37902ac6454SAndrew Thompson 
3805805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(uchcom);
3815805d178SHans Petter Selasky 
3825805d178SHans Petter Selasky static void
383c01fc06eSHans Petter Selasky uchcom_free_softc(struct uchcom_softc *sc)
3845805d178SHans Petter Selasky {
3855805d178SHans Petter Selasky 	if (ucom_unref(&sc->sc_super_ucom)) {
3865805d178SHans Petter Selasky 		mtx_destroy(&sc->sc_mtx);
387c01fc06eSHans Petter Selasky 		device_free_softc(sc);
3885805d178SHans Petter Selasky 	}
3895805d178SHans Petter Selasky }
3905805d178SHans Petter Selasky 
3915805d178SHans Petter Selasky static void
3925805d178SHans Petter Selasky uchcom_free(struct ucom_softc *ucom)
3935805d178SHans Petter Selasky {
394c01fc06eSHans Petter Selasky 	uchcom_free_softc(ucom->sc_parent);
3955805d178SHans Petter Selasky }
3965805d178SHans Petter Selasky 
39702ac6454SAndrew Thompson /* ----------------------------------------------------------------------
39802ac6454SAndrew Thompson  * low level i/o
39902ac6454SAndrew Thompson  */
40002ac6454SAndrew Thompson 
40102ac6454SAndrew Thompson static void
40202ac6454SAndrew Thompson uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
40302ac6454SAndrew Thompson     uint16_t value, uint16_t index)
40402ac6454SAndrew Thompson {
405760bc48eSAndrew Thompson 	struct usb_device_request req;
40602ac6454SAndrew Thompson 
40702ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
40802ac6454SAndrew Thompson 	req.bRequest = reqno;
40902ac6454SAndrew Thompson 	USETW(req.wValue, value);
41002ac6454SAndrew Thompson 	USETW(req.wIndex, index);
41102ac6454SAndrew Thompson 	USETW(req.wLength, 0);
41202ac6454SAndrew Thompson 
413a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev,
41402ac6454SAndrew Thompson 	    &sc->sc_ucom, &req, NULL, 0, 1000);
41502ac6454SAndrew Thompson }
41602ac6454SAndrew Thompson 
41702ac6454SAndrew Thompson static void
41802ac6454SAndrew Thompson uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
41902ac6454SAndrew Thompson     uint16_t value, uint16_t index, void *buf, uint16_t buflen)
42002ac6454SAndrew Thompson {
421760bc48eSAndrew Thompson 	struct usb_device_request req;
42202ac6454SAndrew Thompson 
42302ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
42402ac6454SAndrew Thompson 	req.bRequest = reqno;
42502ac6454SAndrew Thompson 	USETW(req.wValue, value);
42602ac6454SAndrew Thompson 	USETW(req.wIndex, index);
42702ac6454SAndrew Thompson 	USETW(req.wLength, buflen);
42802ac6454SAndrew Thompson 
429a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev,
43002ac6454SAndrew Thompson 	    &sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000);
43102ac6454SAndrew Thompson }
43202ac6454SAndrew Thompson 
43302ac6454SAndrew Thompson static void
43402ac6454SAndrew Thompson uchcom_write_reg(struct uchcom_softc *sc,
43502ac6454SAndrew Thompson     uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
43602ac6454SAndrew Thompson {
43702ac6454SAndrew Thompson 	DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
43802ac6454SAndrew Thompson 	    (unsigned)reg1, (unsigned)val1,
43902ac6454SAndrew Thompson 	    (unsigned)reg2, (unsigned)val2);
44002ac6454SAndrew Thompson 	uchcom_ctrl_write(
44102ac6454SAndrew Thompson 	    sc, UCHCOM_REQ_WRITE_REG,
44202ac6454SAndrew Thompson 	    reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
44302ac6454SAndrew Thompson }
44402ac6454SAndrew Thompson 
44502ac6454SAndrew Thompson static void
44602ac6454SAndrew Thompson uchcom_read_reg(struct uchcom_softc *sc,
44702ac6454SAndrew Thompson     uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
44802ac6454SAndrew Thompson {
44902ac6454SAndrew Thompson 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
45002ac6454SAndrew Thompson 
45102ac6454SAndrew Thompson 	uchcom_ctrl_read(
45202ac6454SAndrew Thompson 	    sc, UCHCOM_REQ_READ_REG,
45302ac6454SAndrew Thompson 	    reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
45402ac6454SAndrew Thompson 
45502ac6454SAndrew Thompson 	DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
45602ac6454SAndrew Thompson 	    (unsigned)reg1, (unsigned)buf[0],
45702ac6454SAndrew Thompson 	    (unsigned)reg2, (unsigned)buf[1]);
45802ac6454SAndrew Thompson 
45902ac6454SAndrew Thompson 	if (rval1)
46002ac6454SAndrew Thompson 		*rval1 = buf[0];
46102ac6454SAndrew Thompson 	if (rval2)
46202ac6454SAndrew Thompson 		*rval2 = buf[1];
46302ac6454SAndrew Thompson }
46402ac6454SAndrew Thompson 
46502ac6454SAndrew Thompson static void
46602ac6454SAndrew Thompson uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
46702ac6454SAndrew Thompson {
46802ac6454SAndrew Thompson 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
46902ac6454SAndrew Thompson 
47011f35cfaSAndrew Thompson 	uchcom_ctrl_read(sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson 	if (rver)
47302ac6454SAndrew Thompson 		*rver = buf[0];
47402ac6454SAndrew Thompson }
47502ac6454SAndrew Thompson 
47602ac6454SAndrew Thompson static void
47702ac6454SAndrew Thompson uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
47802ac6454SAndrew Thompson {
47902ac6454SAndrew Thompson 	uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
48002ac6454SAndrew Thompson }
48102ac6454SAndrew Thompson 
48202ac6454SAndrew Thompson static void
48311f35cfaSAndrew Thompson uchcom_set_dtr_rts_10(struct uchcom_softc *sc, uint8_t val)
48402ac6454SAndrew Thompson {
48502ac6454SAndrew Thompson 	uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
48602ac6454SAndrew Thompson }
48702ac6454SAndrew Thompson 
48802ac6454SAndrew Thompson static void
48911f35cfaSAndrew Thompson uchcom_set_dtr_rts_20(struct uchcom_softc *sc, uint8_t val)
49002ac6454SAndrew Thompson {
49102ac6454SAndrew Thompson 	uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
49202ac6454SAndrew Thompson }
49302ac6454SAndrew Thompson 
49402ac6454SAndrew Thompson 
49502ac6454SAndrew Thompson /* ----------------------------------------------------------------------
49602ac6454SAndrew Thompson  * middle layer
49702ac6454SAndrew Thompson  */
49802ac6454SAndrew Thompson 
49902ac6454SAndrew Thompson static void
50002ac6454SAndrew Thompson uchcom_update_version(struct uchcom_softc *sc)
50102ac6454SAndrew Thompson {
50202ac6454SAndrew Thompson 	uchcom_get_version(sc, &sc->sc_version);
50302ac6454SAndrew Thompson }
50402ac6454SAndrew Thompson 
50502ac6454SAndrew Thompson static void
50602ac6454SAndrew Thompson uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
50702ac6454SAndrew Thompson {
50802ac6454SAndrew Thompson 	sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
50902ac6454SAndrew Thompson 	sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
51002ac6454SAndrew Thompson 
51102ac6454SAndrew Thompson 	cur = ~cur & 0x0F;
51202ac6454SAndrew Thompson 	sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
51302ac6454SAndrew Thompson }
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson static void
51602ac6454SAndrew Thompson uchcom_update_status(struct uchcom_softc *sc)
51702ac6454SAndrew Thompson {
51802ac6454SAndrew Thompson 	uint8_t cur;
51902ac6454SAndrew Thompson 
52002ac6454SAndrew Thompson 	uchcom_get_status(sc, &cur);
52102ac6454SAndrew Thompson 	uchcom_convert_status(sc, cur);
52202ac6454SAndrew Thompson }
52302ac6454SAndrew Thompson 
52402ac6454SAndrew Thompson 
52502ac6454SAndrew Thompson static void
52611f35cfaSAndrew Thompson uchcom_set_dtr_rts(struct uchcom_softc *sc)
52702ac6454SAndrew Thompson {
52802ac6454SAndrew Thompson 	uint8_t val = 0;
52902ac6454SAndrew Thompson 
53002ac6454SAndrew Thompson 	if (sc->sc_dtr)
53102ac6454SAndrew Thompson 		val |= UCHCOM_DTR_MASK;
53202ac6454SAndrew Thompson 	if (sc->sc_rts)
53302ac6454SAndrew Thompson 		val |= UCHCOM_RTS_MASK;
53402ac6454SAndrew Thompson 
53502ac6454SAndrew Thompson 	if (sc->sc_version < UCHCOM_VER_20)
53611f35cfaSAndrew Thompson 		uchcom_set_dtr_rts_10(sc, ~val);
53702ac6454SAndrew Thompson 	else
53811f35cfaSAndrew Thompson 		uchcom_set_dtr_rts_20(sc, ~val);
53902ac6454SAndrew Thompson }
54002ac6454SAndrew Thompson 
54102ac6454SAndrew Thompson static void
542760bc48eSAndrew Thompson uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
54302ac6454SAndrew Thompson {
54402ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
54502ac6454SAndrew Thompson 	uint8_t brk1;
54602ac6454SAndrew Thompson 	uint8_t brk2;
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
54902ac6454SAndrew Thompson 	if (onoff) {
55002ac6454SAndrew Thompson 		/* on - clear bits */
55102ac6454SAndrew Thompson 		brk1 &= ~UCHCOM_BRK1_MASK;
55202ac6454SAndrew Thompson 		brk2 &= ~UCHCOM_BRK2_MASK;
55302ac6454SAndrew Thompson 	} else {
55402ac6454SAndrew Thompson 		/* off - set bits */
55502ac6454SAndrew Thompson 		brk1 |= UCHCOM_BRK1_MASK;
55602ac6454SAndrew Thompson 		brk2 |= UCHCOM_BRK2_MASK;
55702ac6454SAndrew Thompson 	}
55802ac6454SAndrew Thompson 	uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
55902ac6454SAndrew Thompson }
56002ac6454SAndrew Thompson 
56102ac6454SAndrew Thompson static int
56202ac6454SAndrew Thompson uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
56302ac6454SAndrew Thompson {
56402ac6454SAndrew Thompson 	const struct uchcom_divider_record *rp;
56502ac6454SAndrew Thompson 	uint32_t div;
56602ac6454SAndrew Thompson 	uint32_t rem;
56702ac6454SAndrew Thompson 	uint32_t mod;
56802ac6454SAndrew Thompson 	uint8_t i;
56902ac6454SAndrew Thompson 
57002ac6454SAndrew Thompson 	/* find record */
57102ac6454SAndrew Thompson 	for (i = 0; i != NUM_DIVIDERS; i++) {
57202ac6454SAndrew Thompson 		if (dividers[i].dvr_high >= rate &&
57302ac6454SAndrew Thompson 		    dividers[i].dvr_low <= rate) {
57402ac6454SAndrew Thompson 			rp = &dividers[i];
57502ac6454SAndrew Thompson 			goto found;
57602ac6454SAndrew Thompson 		}
57702ac6454SAndrew Thompson 	}
57802ac6454SAndrew Thompson 	return (-1);
57902ac6454SAndrew Thompson 
58002ac6454SAndrew Thompson found:
58102ac6454SAndrew Thompson 	dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
58202ac6454SAndrew Thompson 	if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
58302ac6454SAndrew Thompson 		dp->dv_div = rp->dvr_divider.dv_div;
58402ac6454SAndrew Thompson 	else {
58502ac6454SAndrew Thompson 		div = rp->dvr_base_clock / rate;
58602ac6454SAndrew Thompson 		rem = rp->dvr_base_clock % rate;
58702ac6454SAndrew Thompson 		if (div == 0 || div >= 0xFF)
58802ac6454SAndrew Thompson 			return (-1);
58902ac6454SAndrew Thompson 		if ((rem << 1) >= rate)
59002ac6454SAndrew Thompson 			div += 1;
59102ac6454SAndrew Thompson 		dp->dv_div = (uint8_t)-div;
59202ac6454SAndrew Thompson 	}
59302ac6454SAndrew Thompson 
59411f35cfaSAndrew Thompson 	mod = (UCHCOM_BPS_MOD_BASE / rate) + UCHCOM_BPS_MOD_BASE_OFS;
59511f35cfaSAndrew Thompson 	mod = mod + (mod / 2);
59602ac6454SAndrew Thompson 
59711f35cfaSAndrew Thompson 	dp->dv_mod = (mod + 0xFF) / 0x100;
59802ac6454SAndrew Thompson 
59902ac6454SAndrew Thompson 	return (0);
60002ac6454SAndrew Thompson }
60102ac6454SAndrew Thompson 
60202ac6454SAndrew Thompson static void
60311f35cfaSAndrew Thompson uchcom_set_baudrate(struct uchcom_softc *sc, uint32_t rate)
60402ac6454SAndrew Thompson {
60502ac6454SAndrew Thompson 	struct uchcom_divider dv;
60602ac6454SAndrew Thompson 
60702ac6454SAndrew Thompson 	if (uchcom_calc_divider_settings(&dv, rate))
60802ac6454SAndrew Thompson 		return;
60902ac6454SAndrew Thompson 
61002ac6454SAndrew Thompson 	uchcom_write_reg(sc,
61102ac6454SAndrew Thompson 	    UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
61202ac6454SAndrew Thompson 	    UCHCOM_REG_BPS_DIV, dv.dv_div);
61302ac6454SAndrew Thompson 	uchcom_write_reg(sc,
61402ac6454SAndrew Thompson 	    UCHCOM_REG_BPS_MOD, dv.dv_mod,
61502ac6454SAndrew Thompson 	    UCHCOM_REG_BPS_PAD, 0);
61602ac6454SAndrew Thompson }
61702ac6454SAndrew Thompson 
61802ac6454SAndrew Thompson /* ----------------------------------------------------------------------
61902ac6454SAndrew Thompson  * methods for ucom
62002ac6454SAndrew Thompson  */
62102ac6454SAndrew Thompson static void
622760bc48eSAndrew Thompson uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
62302ac6454SAndrew Thompson {
62402ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
62502ac6454SAndrew Thompson 
62602ac6454SAndrew Thompson 	DPRINTF("\n");
62702ac6454SAndrew Thompson 
62802ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
62902ac6454SAndrew Thompson 	*msr = sc->sc_msr;
63002ac6454SAndrew Thompson }
63102ac6454SAndrew Thompson 
63202ac6454SAndrew Thompson static void
633760bc48eSAndrew Thompson uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
63402ac6454SAndrew Thompson {
63502ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
63602ac6454SAndrew Thompson 
63702ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
63802ac6454SAndrew Thompson 
63902ac6454SAndrew Thompson 	sc->sc_dtr = onoff;
64011f35cfaSAndrew Thompson 	uchcom_set_dtr_rts(sc);
64102ac6454SAndrew Thompson }
64202ac6454SAndrew Thompson 
64302ac6454SAndrew Thompson static void
644760bc48eSAndrew Thompson uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
64502ac6454SAndrew Thompson {
64602ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
64702ac6454SAndrew Thompson 
64802ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
64902ac6454SAndrew Thompson 
65002ac6454SAndrew Thompson 	sc->sc_rts = onoff;
65111f35cfaSAndrew Thompson 	uchcom_set_dtr_rts(sc);
65211f35cfaSAndrew Thompson }
65311f35cfaSAndrew Thompson 
65411f35cfaSAndrew Thompson static void
65511f35cfaSAndrew Thompson uchcom_cfg_open(struct ucom_softc *ucom)
65611f35cfaSAndrew Thompson {
65711f35cfaSAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
65811f35cfaSAndrew Thompson 
65911f35cfaSAndrew Thompson 	DPRINTF("\n");
66011f35cfaSAndrew Thompson 
66111f35cfaSAndrew Thompson 	uchcom_update_version(sc);
66211f35cfaSAndrew Thompson 	uchcom_update_status(sc);
66302ac6454SAndrew Thompson }
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson static int
666760bc48eSAndrew Thompson uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
66702ac6454SAndrew Thompson {
66802ac6454SAndrew Thompson 	struct uchcom_divider dv;
66902ac6454SAndrew Thompson 
67002ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
67111f35cfaSAndrew Thompson 	case CS8:
67202ac6454SAndrew Thompson 		break;
67311f35cfaSAndrew Thompson 	default:
67411f35cfaSAndrew Thompson 		return (EIO);
67502ac6454SAndrew Thompson 	}
67602ac6454SAndrew Thompson 
67702ac6454SAndrew Thompson 	if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
67802ac6454SAndrew Thompson 		return (EIO);
67902ac6454SAndrew Thompson 	}
68002ac6454SAndrew Thompson 	return (0);			/* success */
68102ac6454SAndrew Thompson }
68202ac6454SAndrew Thompson 
68302ac6454SAndrew Thompson static void
684760bc48eSAndrew Thompson uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
68502ac6454SAndrew Thompson {
68602ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
68702ac6454SAndrew Thompson 
68811f35cfaSAndrew Thompson 	uchcom_get_version(sc, 0);
68911f35cfaSAndrew Thompson 	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
69011f35cfaSAndrew Thompson 	uchcom_set_baudrate(sc, t->c_ospeed);
69111f35cfaSAndrew Thompson 	uchcom_read_reg(sc, 0x18, 0, 0x25, 0);
69211f35cfaSAndrew Thompson 	uchcom_write_reg(sc, 0x18, 0x50, 0x25, 0x00);
69311f35cfaSAndrew Thompson 	uchcom_update_status(sc);
69411f35cfaSAndrew Thompson 	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0x501f, 0xd90a);
69511f35cfaSAndrew Thompson 	uchcom_set_baudrate(sc, t->c_ospeed);
69611f35cfaSAndrew Thompson 	uchcom_set_dtr_rts(sc);
69711f35cfaSAndrew Thompson 	uchcom_update_status(sc);
69802ac6454SAndrew Thompson }
69902ac6454SAndrew Thompson 
70002ac6454SAndrew Thompson static void
701760bc48eSAndrew Thompson uchcom_start_read(struct ucom_softc *ucom)
70202ac6454SAndrew Thompson {
70302ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
70402ac6454SAndrew Thompson 
70502ac6454SAndrew Thompson 	/* start interrupt endpoint */
706a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
70702ac6454SAndrew Thompson 
70802ac6454SAndrew Thompson 	/* start read endpoint */
709a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
71002ac6454SAndrew Thompson }
71102ac6454SAndrew Thompson 
71202ac6454SAndrew Thompson static void
713760bc48eSAndrew Thompson uchcom_stop_read(struct ucom_softc *ucom)
71402ac6454SAndrew Thompson {
71502ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
71602ac6454SAndrew Thompson 
71702ac6454SAndrew Thompson 	/* stop interrupt endpoint */
718a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
71902ac6454SAndrew Thompson 
72002ac6454SAndrew Thompson 	/* stop read endpoint */
721a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
72202ac6454SAndrew Thompson }
72302ac6454SAndrew Thompson 
72402ac6454SAndrew Thompson static void
725760bc48eSAndrew Thompson uchcom_start_write(struct ucom_softc *ucom)
72602ac6454SAndrew Thompson {
72702ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
72802ac6454SAndrew Thompson 
729a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
73002ac6454SAndrew Thompson }
73102ac6454SAndrew Thompson 
73202ac6454SAndrew Thompson static void
733760bc48eSAndrew Thompson uchcom_stop_write(struct ucom_softc *ucom)
73402ac6454SAndrew Thompson {
73502ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
73602ac6454SAndrew Thompson 
737a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
73802ac6454SAndrew Thompson }
73902ac6454SAndrew Thompson 
74002ac6454SAndrew Thompson /* ----------------------------------------------------------------------
74102ac6454SAndrew Thompson  * callback when the modem status is changed.
74202ac6454SAndrew Thompson  */
74302ac6454SAndrew Thompson static void
744ed6d949aSAndrew Thompson uchcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
74502ac6454SAndrew Thompson {
746ed6d949aSAndrew Thompson 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
747ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
74802ac6454SAndrew Thompson 	uint8_t buf[UCHCOM_INTR_LEAST];
749ed6d949aSAndrew Thompson 	int actlen;
750ed6d949aSAndrew Thompson 
751ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
75202ac6454SAndrew Thompson 
75302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
75402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
75502ac6454SAndrew Thompson 
756ed6d949aSAndrew Thompson 		DPRINTF("actlen = %u\n", actlen);
75702ac6454SAndrew Thompson 
758ed6d949aSAndrew Thompson 		if (actlen >= UCHCOM_INTR_LEAST) {
759ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
760ed6d949aSAndrew Thompson 			usbd_copy_out(pc, 0, buf, UCHCOM_INTR_LEAST);
76102ac6454SAndrew Thompson 
76202ac6454SAndrew Thompson 			DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
76302ac6454SAndrew Thompson 			    (unsigned)buf[0], (unsigned)buf[1],
76402ac6454SAndrew Thompson 			    (unsigned)buf[2], (unsigned)buf[3]);
76502ac6454SAndrew Thompson 
76602ac6454SAndrew Thompson 			uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
767a593f6b8SAndrew Thompson 			ucom_status_change(&sc->sc_ucom);
76802ac6454SAndrew Thompson 		}
76902ac6454SAndrew Thompson 	case USB_ST_SETUP:
77002ac6454SAndrew Thompson tr_setup:
771ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
772a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
77302ac6454SAndrew Thompson 		break;
77402ac6454SAndrew Thompson 
77502ac6454SAndrew Thompson 	default:			/* Error */
776ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
77702ac6454SAndrew Thompson 			/* try to clear stall first */
778ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
77902ac6454SAndrew Thompson 			goto tr_setup;
78002ac6454SAndrew Thompson 		}
78102ac6454SAndrew Thompson 		break;
78202ac6454SAndrew Thompson 	}
78302ac6454SAndrew Thompson }
78402ac6454SAndrew Thompson 
78502ac6454SAndrew Thompson static void
786ed6d949aSAndrew Thompson uchcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
78702ac6454SAndrew Thompson {
788ed6d949aSAndrew Thompson 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
789ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
79002ac6454SAndrew Thompson 	uint32_t actlen;
79102ac6454SAndrew Thompson 
79202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
79302ac6454SAndrew Thompson 	case USB_ST_SETUP:
79402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
79502ac6454SAndrew Thompson tr_setup:
796ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
797ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
79811f35cfaSAndrew Thompson 		    usbd_xfer_max_len(xfer), &actlen)) {
79902ac6454SAndrew Thompson 
80002ac6454SAndrew Thompson 			DPRINTF("actlen = %d\n", actlen);
80102ac6454SAndrew Thompson 
802ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
803a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
80402ac6454SAndrew Thompson 		}
80511f35cfaSAndrew Thompson 		break;
80602ac6454SAndrew Thompson 
80702ac6454SAndrew Thompson 	default:			/* Error */
808ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
80902ac6454SAndrew Thompson 			/* try to clear stall first */
810ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
81102ac6454SAndrew Thompson 			goto tr_setup;
81202ac6454SAndrew Thompson 		}
81311f35cfaSAndrew Thompson 		break;
81402ac6454SAndrew Thompson 	}
81502ac6454SAndrew Thompson }
81602ac6454SAndrew Thompson 
81702ac6454SAndrew Thompson static void
818ed6d949aSAndrew Thompson uchcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
81902ac6454SAndrew Thompson {
820ed6d949aSAndrew Thompson 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
821ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
822ed6d949aSAndrew Thompson 	int actlen;
823ed6d949aSAndrew Thompson 
824ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
82502ac6454SAndrew Thompson 
82602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
82702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
82811f35cfaSAndrew Thompson 
82911f35cfaSAndrew Thompson 		if (actlen > 0) {
830ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
831ed6d949aSAndrew Thompson 			ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
83211f35cfaSAndrew Thompson 		}
83302ac6454SAndrew Thompson 
83402ac6454SAndrew Thompson 	case USB_ST_SETUP:
83502ac6454SAndrew Thompson tr_setup:
836ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
837a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
83811f35cfaSAndrew Thompson 		break;
83902ac6454SAndrew Thompson 
84002ac6454SAndrew Thompson 	default:			/* Error */
841ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
84202ac6454SAndrew Thompson 			/* try to clear stall first */
843ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
84402ac6454SAndrew Thompson 			goto tr_setup;
84502ac6454SAndrew Thompson 		}
84611f35cfaSAndrew Thompson 		break;
84702ac6454SAndrew Thompson 	}
84802ac6454SAndrew Thompson }
84902ac6454SAndrew Thompson 
850655dc9d0SAndrew Thompson static void
851655dc9d0SAndrew Thompson uchcom_poll(struct ucom_softc *ucom)
852655dc9d0SAndrew Thompson {
853655dc9d0SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
854655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_xfer, UCHCOM_N_TRANSFER);
855655dc9d0SAndrew Thompson }
856655dc9d0SAndrew Thompson 
85702ac6454SAndrew Thompson static device_method_t uchcom_methods[] = {
85802ac6454SAndrew Thompson 	/* Device interface */
85902ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uchcom_probe),
86002ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uchcom_attach),
86102ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uchcom_detach),
8625805d178SHans Petter Selasky 	DEVMETHOD_END
86302ac6454SAndrew Thompson };
86402ac6454SAndrew Thompson 
86502ac6454SAndrew Thompson static driver_t uchcom_driver = {
8663c12706cSHans Petter Selasky 	.name = "uchcom",
8676d917491SHans Petter Selasky 	.methods = uchcom_methods,
8686d917491SHans Petter Selasky 	.size = sizeof(struct uchcom_softc)
86902ac6454SAndrew Thompson };
87002ac6454SAndrew Thompson 
87102ac6454SAndrew Thompson static devclass_t uchcom_devclass;
87202ac6454SAndrew Thompson 
8739aef556dSAndrew Thompson DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0);
87402ac6454SAndrew Thompson MODULE_DEPEND(uchcom, ucom, 1, 1, 1);
87502ac6454SAndrew Thompson MODULE_DEPEND(uchcom, usb, 1, 1, 1);
876910cb8feSAndrew Thompson MODULE_VERSION(uchcom, 1);
877