xref: /freebsd/sys/dev/usb/serial/uchcom.c (revision 34bedcee3c5a8640f3e344218b47c2d970df78ed)
102ac6454SAndrew Thompson /*	$NetBSD: uchcom.c,v 1.1 2007/09/03 17:57:37 tshiozak Exp $	*/
202ac6454SAndrew Thompson 
302ac6454SAndrew Thompson /*-
4718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD AND BSD-2-Clause-NetBSD
5718cf2ccSPedro F. Giffuni  *
602ac6454SAndrew Thompson  * Copyright (c) 2007, Takanori Watanabe
702ac6454SAndrew Thompson  * All rights reserved.
802ac6454SAndrew Thompson  *
902ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1002ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1102ac6454SAndrew Thompson  * are met:
1202ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1302ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1402ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1602ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1702ac6454SAndrew Thompson  *
1802ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1902ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2002ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2102ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2202ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2302ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2402ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2502ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2602ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2702ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2802ac6454SAndrew Thompson  * SUCH DAMAGE.
2902ac6454SAndrew Thompson  */
3002ac6454SAndrew Thompson 
3102ac6454SAndrew Thompson /*
3202ac6454SAndrew Thompson  * Copyright (c) 2007 The NetBSD Foundation, Inc.
3302ac6454SAndrew Thompson  * All rights reserved.
3402ac6454SAndrew Thompson  *
3502ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
3602ac6454SAndrew Thompson  * by Takuya SHIOZAKI (tshiozak@netbsd.org).
3702ac6454SAndrew Thompson  *
3802ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
3902ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
4002ac6454SAndrew Thompson  * are met:
4102ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
4202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
4302ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
4402ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
4502ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
4602ac6454SAndrew Thompson  *
4702ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
4802ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4902ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5002ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5102ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5202ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5302ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5402ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5502ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5602ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
5702ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
5802ac6454SAndrew Thompson  */
5902ac6454SAndrew Thompson 
6002ac6454SAndrew Thompson #include <sys/cdefs.h>
6102ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
6202ac6454SAndrew Thompson 
6302ac6454SAndrew Thompson /*
6411f35cfaSAndrew Thompson  * Driver for WinChipHead CH341/340, the worst USB-serial chip in the
6511f35cfaSAndrew Thompson  * world.
6602ac6454SAndrew Thompson  */
6702ac6454SAndrew Thompson 
68ed6d949aSAndrew Thompson #include <sys/stdint.h>
69ed6d949aSAndrew Thompson #include <sys/stddef.h>
70ed6d949aSAndrew Thompson #include <sys/param.h>
71ed6d949aSAndrew Thompson #include <sys/queue.h>
72ed6d949aSAndrew Thompson #include <sys/types.h>
73ed6d949aSAndrew Thompson #include <sys/systm.h>
74ed6d949aSAndrew Thompson #include <sys/kernel.h>
75ed6d949aSAndrew Thompson #include <sys/bus.h>
76ed6d949aSAndrew Thompson #include <sys/module.h>
77ed6d949aSAndrew Thompson #include <sys/lock.h>
78ed6d949aSAndrew Thompson #include <sys/mutex.h>
79ed6d949aSAndrew Thompson #include <sys/condvar.h>
80ed6d949aSAndrew Thompson #include <sys/sysctl.h>
81ed6d949aSAndrew Thompson #include <sys/sx.h>
82ed6d949aSAndrew Thompson #include <sys/unistd.h>
83ed6d949aSAndrew Thompson #include <sys/callout.h>
84ed6d949aSAndrew Thompson #include <sys/malloc.h>
85ed6d949aSAndrew Thompson #include <sys/priv.h>
86ed6d949aSAndrew Thompson 
8702ac6454SAndrew Thompson #include <dev/usb/usb.h>
88ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
89ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
90ed6d949aSAndrew Thompson #include "usbdevs.h"
9102ac6454SAndrew Thompson 
9202ac6454SAndrew Thompson #define	USB_DEBUG_VAR uchcom_debug
9302ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
9402ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
9502ac6454SAndrew Thompson 
9602ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
9702ac6454SAndrew Thompson 
98b850ecc1SAndrew Thompson #ifdef USB_DEBUG
9902ac6454SAndrew Thompson static int uchcom_debug = 0;
10002ac6454SAndrew Thompson 
101f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, uchcom, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
102f8d2b1f3SPawel Biernacki     "USB uchcom");
103ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_uchcom, OID_AUTO, debug, CTLFLAG_RWTUN,
10402ac6454SAndrew Thompson     &uchcom_debug, 0, "uchcom debug level");
10502ac6454SAndrew Thompson #endif
10602ac6454SAndrew Thompson 
10702ac6454SAndrew Thompson #define	UCHCOM_IFACE_INDEX	0
10802ac6454SAndrew Thompson #define	UCHCOM_CONFIG_INDEX	0
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson #define	UCHCOM_REV_CH340	0x0250
11102ac6454SAndrew Thompson #define	UCHCOM_INPUT_BUF_SIZE	8
11202ac6454SAndrew Thompson 
11302ac6454SAndrew Thompson #define	UCHCOM_REQ_GET_VERSION	0x5F
11402ac6454SAndrew Thompson #define	UCHCOM_REQ_READ_REG	0x95
11502ac6454SAndrew Thompson #define	UCHCOM_REQ_WRITE_REG	0x9A
11602ac6454SAndrew Thompson #define	UCHCOM_REQ_RESET	0xA1
11702ac6454SAndrew Thompson #define	UCHCOM_REQ_SET_DTRRTS	0xA4
11802ac6454SAndrew Thompson 
11902ac6454SAndrew Thompson #define	UCHCOM_REG_STAT1	0x06
12002ac6454SAndrew Thompson #define	UCHCOM_REG_STAT2	0x07
12102ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_PRE	0x12
12202ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_DIV	0x13
12302ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_MOD	0x14
12402ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_PAD	0x0F
12502ac6454SAndrew Thompson #define	UCHCOM_REG_BREAK1	0x05
12602ac6454SAndrew Thompson #define	UCHCOM_REG_LCR1		0x18
12702ac6454SAndrew Thompson #define	UCHCOM_REG_LCR2		0x25
12802ac6454SAndrew Thompson 
12902ac6454SAndrew Thompson #define	UCHCOM_VER_20		0x20
130a23a4e68SAndriy Gapon #define	UCHCOM_VER_30		0x30
13102ac6454SAndrew Thompson 
13202ac6454SAndrew Thompson #define	UCHCOM_BASE_UNKNOWN	0
13302ac6454SAndrew Thompson #define	UCHCOM_BPS_MOD_BASE	20000000
13402ac6454SAndrew Thompson #define	UCHCOM_BPS_MOD_BASE_OFS	1100
13502ac6454SAndrew Thompson 
13602ac6454SAndrew Thompson #define	UCHCOM_DTR_MASK		0x20
13702ac6454SAndrew Thompson #define	UCHCOM_RTS_MASK		0x40
13802ac6454SAndrew Thompson 
139dad3e656SAndriy Gapon #define	UCHCOM_BRK_MASK		0x01
14002ac6454SAndrew Thompson 
14102ac6454SAndrew Thompson #define	UCHCOM_LCR1_MASK	0xAF
14202ac6454SAndrew Thompson #define	UCHCOM_LCR2_MASK	0x07
143dad3e656SAndriy Gapon #define	UCHCOM_LCR1_RX		0x80
144dad3e656SAndriy Gapon #define	UCHCOM_LCR1_TX		0x40
145dad3e656SAndriy Gapon #define	UCHCOM_LCR1_PARENB	0x08
146dad3e656SAndriy Gapon #define	UCHCOM_LCR1_CS8		0x03
14702ac6454SAndrew Thompson #define	UCHCOM_LCR2_PAREVEN	0x07
14802ac6454SAndrew Thompson #define	UCHCOM_LCR2_PARODD	0x06
14902ac6454SAndrew Thompson #define	UCHCOM_LCR2_PARMARK	0x05
15002ac6454SAndrew Thompson #define	UCHCOM_LCR2_PARSPACE	0x04
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson #define	UCHCOM_INTR_STAT1	0x02
15302ac6454SAndrew Thompson #define	UCHCOM_INTR_STAT2	0x03
15402ac6454SAndrew Thompson #define	UCHCOM_INTR_LEAST	4
15502ac6454SAndrew Thompson 
15602ac6454SAndrew Thompson #define	UCHCOM_BULK_BUF_SIZE 1024	/* bytes */
15702ac6454SAndrew Thompson 
15802ac6454SAndrew Thompson enum {
15902ac6454SAndrew Thompson 	UCHCOM_BULK_DT_WR,
16002ac6454SAndrew Thompson 	UCHCOM_BULK_DT_RD,
16102ac6454SAndrew Thompson 	UCHCOM_INTR_DT_RD,
16202ac6454SAndrew Thompson 	UCHCOM_N_TRANSFER,
16302ac6454SAndrew Thompson };
16402ac6454SAndrew Thompson 
16502ac6454SAndrew Thompson struct uchcom_softc {
166760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
167760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
16802ac6454SAndrew Thompson 
169760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UCHCOM_N_TRANSFER];
170760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
171deefe583SAndrew Thompson 	struct mtx sc_mtx;
17202ac6454SAndrew Thompson 
17302ac6454SAndrew Thompson 	uint8_t	sc_dtr;			/* local copy */
17402ac6454SAndrew Thompson 	uint8_t	sc_rts;			/* local copy */
17502ac6454SAndrew Thompson 	uint8_t	sc_version;
17602ac6454SAndrew Thompson 	uint8_t	sc_msr;
17702ac6454SAndrew Thompson 	uint8_t	sc_lsr;			/* local status register */
17802ac6454SAndrew Thompson };
17902ac6454SAndrew Thompson 
18002ac6454SAndrew Thompson struct uchcom_divider {
18102ac6454SAndrew Thompson 	uint8_t	dv_prescaler;
18202ac6454SAndrew Thompson 	uint8_t	dv_div;
18302ac6454SAndrew Thompson 	uint8_t	dv_mod;
18402ac6454SAndrew Thompson };
18502ac6454SAndrew Thompson 
18602ac6454SAndrew Thompson struct uchcom_divider_record {
18702ac6454SAndrew Thompson 	uint32_t dvr_high;
18802ac6454SAndrew Thompson 	uint32_t dvr_low;
18902ac6454SAndrew Thompson 	uint32_t dvr_base_clock;
19002ac6454SAndrew Thompson 	struct uchcom_divider dvr_divider;
19102ac6454SAndrew Thompson };
19202ac6454SAndrew Thompson 
19302ac6454SAndrew Thompson static const struct uchcom_divider_record dividers[] =
19402ac6454SAndrew Thompson {
19502ac6454SAndrew Thompson 	{307200, 307200, UCHCOM_BASE_UNKNOWN, {7, 0xD9, 0}},
19602ac6454SAndrew Thompson 	{921600, 921600, UCHCOM_BASE_UNKNOWN, {7, 0xF3, 0}},
19702ac6454SAndrew Thompson 	{2999999, 23530, 6000000, {3, 0, 0}},
19802ac6454SAndrew Thompson 	{23529, 2942, 750000, {2, 0, 0}},
19902ac6454SAndrew Thompson 	{2941, 368, 93750, {1, 0, 0}},
20002ac6454SAndrew Thompson 	{367, 1, 11719, {0, 0, 0}},
20102ac6454SAndrew Thompson };
20202ac6454SAndrew Thompson 
203432157dcSPedro F. Giffuni #define	NUM_DIVIDERS	nitems(dividers)
20402ac6454SAndrew Thompson 
205f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID uchcom_devs[] = {
20602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_WCH, USB_PRODUCT_WCH_CH341SER, 0)},
20711f35cfaSAndrew Thompson 	{USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER, 0)},
20851638137SGavin Atkinson 	{USB_VPI(USB_VENDOR_WCH2, USB_PRODUCT_WCH2_CH341SER_2, 0)},
20902ac6454SAndrew Thompson };
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson /* protypes */
21202ac6454SAndrew Thompson 
2135805d178SHans Petter Selasky static void	uchcom_free(struct ucom_softc *);
214760bc48eSAndrew Thompson static int	uchcom_pre_param(struct ucom_softc *, struct termios *);
215760bc48eSAndrew Thompson static void	uchcom_cfg_get_status(struct ucom_softc *, uint8_t *,
21602ac6454SAndrew Thompson 		    uint8_t *);
21711f35cfaSAndrew Thompson static void	uchcom_cfg_open(struct ucom_softc *ucom);
218760bc48eSAndrew Thompson static void	uchcom_cfg_param(struct ucom_softc *, struct termios *);
219760bc48eSAndrew Thompson static void	uchcom_cfg_set_break(struct ucom_softc *, uint8_t);
220760bc48eSAndrew Thompson static void	uchcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
221760bc48eSAndrew Thompson static void	uchcom_cfg_set_rts(struct ucom_softc *, uint8_t);
222760bc48eSAndrew Thompson static void	uchcom_start_read(struct ucom_softc *);
223760bc48eSAndrew Thompson static void	uchcom_start_write(struct ucom_softc *);
224760bc48eSAndrew Thompson static void	uchcom_stop_read(struct ucom_softc *);
225760bc48eSAndrew Thompson static void	uchcom_stop_write(struct ucom_softc *);
22602ac6454SAndrew Thompson static void	uchcom_update_version(struct uchcom_softc *);
22702ac6454SAndrew Thompson static void	uchcom_convert_status(struct uchcom_softc *, uint8_t);
22802ac6454SAndrew Thompson static void	uchcom_update_status(struct uchcom_softc *);
22911f35cfaSAndrew Thompson static void	uchcom_set_dtr_rts(struct uchcom_softc *);
23002ac6454SAndrew Thompson static int	uchcom_calc_divider_settings(struct uchcom_divider *, uint32_t);
23111f35cfaSAndrew Thompson static void	uchcom_set_baudrate(struct uchcom_softc *, uint32_t);
232655dc9d0SAndrew Thompson static void	uchcom_poll(struct ucom_softc *ucom);
23302ac6454SAndrew Thompson 
23402ac6454SAndrew Thompson static device_probe_t uchcom_probe;
23502ac6454SAndrew Thompson static device_attach_t uchcom_attach;
23602ac6454SAndrew Thompson static device_detach_t uchcom_detach;
237c01fc06eSHans Petter Selasky static void uchcom_free_softc(struct uchcom_softc *);
23802ac6454SAndrew Thompson 
239e0a69b51SAndrew Thompson static usb_callback_t uchcom_intr_callback;
240e0a69b51SAndrew Thompson static usb_callback_t uchcom_write_callback;
241e0a69b51SAndrew Thompson static usb_callback_t uchcom_read_callback;
24202ac6454SAndrew Thompson 
243760bc48eSAndrew Thompson static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
24402ac6454SAndrew Thompson 	[UCHCOM_BULK_DT_WR] = {
24502ac6454SAndrew Thompson 		.type = UE_BULK,
24602ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
24702ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2484eae601eSAndrew Thompson 		.bufsize = UCHCOM_BULK_BUF_SIZE,
2494eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2504eae601eSAndrew Thompson 		.callback = &uchcom_write_callback,
25102ac6454SAndrew Thompson 	},
25202ac6454SAndrew Thompson 
25302ac6454SAndrew Thompson 	[UCHCOM_BULK_DT_RD] = {
25402ac6454SAndrew Thompson 		.type = UE_BULK,
25502ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
25602ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2574eae601eSAndrew Thompson 		.bufsize = UCHCOM_BULK_BUF_SIZE,
2584eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2594eae601eSAndrew Thompson 		.callback = &uchcom_read_callback,
26002ac6454SAndrew Thompson 	},
26102ac6454SAndrew Thompson 
26202ac6454SAndrew Thompson 	[UCHCOM_INTR_DT_RD] = {
26302ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
26402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
26502ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2664eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2674eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
2684eae601eSAndrew Thompson 		.callback = &uchcom_intr_callback,
26902ac6454SAndrew Thompson 	},
27002ac6454SAndrew Thompson };
27102ac6454SAndrew Thompson 
272a8675caeSAndrew Thompson static struct ucom_callback uchcom_callback = {
273a593f6b8SAndrew Thompson 	.ucom_cfg_get_status = &uchcom_cfg_get_status,
274a593f6b8SAndrew Thompson 	.ucom_cfg_set_dtr = &uchcom_cfg_set_dtr,
275a593f6b8SAndrew Thompson 	.ucom_cfg_set_rts = &uchcom_cfg_set_rts,
276a593f6b8SAndrew Thompson 	.ucom_cfg_set_break = &uchcom_cfg_set_break,
27711f35cfaSAndrew Thompson 	.ucom_cfg_open = &uchcom_cfg_open,
278a593f6b8SAndrew Thompson 	.ucom_cfg_param = &uchcom_cfg_param,
279a593f6b8SAndrew Thompson 	.ucom_pre_param = &uchcom_pre_param,
280a593f6b8SAndrew Thompson 	.ucom_start_read = &uchcom_start_read,
281a593f6b8SAndrew Thompson 	.ucom_stop_read = &uchcom_stop_read,
282a593f6b8SAndrew Thompson 	.ucom_start_write = &uchcom_start_write,
283a593f6b8SAndrew Thompson 	.ucom_stop_write = &uchcom_stop_write,
284655dc9d0SAndrew Thompson 	.ucom_poll = &uchcom_poll,
2855805d178SHans Petter Selasky 	.ucom_free = &uchcom_free,
28602ac6454SAndrew Thompson };
28702ac6454SAndrew Thompson 
28802ac6454SAndrew Thompson /* ----------------------------------------------------------------------
28902ac6454SAndrew Thompson  * driver entry points
29002ac6454SAndrew Thompson  */
29102ac6454SAndrew Thompson 
29202ac6454SAndrew Thompson static int
29302ac6454SAndrew Thompson uchcom_probe(device_t dev)
29402ac6454SAndrew Thompson {
295760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
29602ac6454SAndrew Thompson 
29702ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
29802ac6454SAndrew Thompson 
299f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
30002ac6454SAndrew Thompson 		return (ENXIO);
30102ac6454SAndrew Thompson 	}
30202ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
30302ac6454SAndrew Thompson 		return (ENXIO);
30402ac6454SAndrew Thompson 	}
30502ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
30602ac6454SAndrew Thompson 		return (ENXIO);
30702ac6454SAndrew Thompson 	}
308a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
30902ac6454SAndrew Thompson }
31002ac6454SAndrew Thompson 
31102ac6454SAndrew Thompson static int
31202ac6454SAndrew Thompson uchcom_attach(device_t dev)
31302ac6454SAndrew Thompson {
31402ac6454SAndrew Thompson 	struct uchcom_softc *sc = device_get_softc(dev);
315760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
31602ac6454SAndrew Thompson 	int error;
31702ac6454SAndrew Thompson 	uint8_t iface_index;
31802ac6454SAndrew Thompson 
31902ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
32002ac6454SAndrew Thompson 
321a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
322deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uchcom", NULL, MTX_DEF);
3235805d178SHans Petter Selasky 	ucom_ref(&sc->sc_super_ucom);
32402ac6454SAndrew Thompson 
32502ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
32602ac6454SAndrew Thompson 
327d759c295SAndriy Gapon 	switch (uaa->info.idProduct) {
328d759c295SAndriy Gapon 	case USB_PRODUCT_WCH2_CH341SER:
32902ac6454SAndrew Thompson 		device_printf(dev, "CH340 detected\n");
33002ac6454SAndrew Thompson 		break;
331d759c295SAndriy Gapon 	case USB_PRODUCT_WCH2_CH341SER_2:
332*34bedceeSHans Petter Selasky 	case USB_PRODUCT_WCH2_CH341SER_3:
33302ac6454SAndrew Thompson 		device_printf(dev, "CH341 detected\n");
33402ac6454SAndrew Thompson 		break;
335d759c295SAndriy Gapon 	default:
336d759c295SAndriy Gapon 		device_printf(dev, "New CH340/CH341 product 0x%04x detected\n",
337d759c295SAndriy Gapon 		    uaa->info.idProduct);
338d759c295SAndriy Gapon 		break;
33902ac6454SAndrew Thompson 	}
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 	iface_index = UCHCOM_IFACE_INDEX;
342a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device,
34302ac6454SAndrew Thompson 	    &iface_index, sc->sc_xfer, uchcom_config_data,
344deefe583SAndrew Thompson 	    UCHCOM_N_TRANSFER, sc, &sc->sc_mtx);
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson 	if (error) {
34702ac6454SAndrew Thompson 		DPRINTF("one or more missing USB endpoints, "
348a593f6b8SAndrew Thompson 		    "error=%s\n", usbd_errstr(error));
34902ac6454SAndrew Thompson 		goto detach;
35002ac6454SAndrew Thompson 	}
35102ac6454SAndrew Thompson 
35202ac6454SAndrew Thompson 	/* clear stall at first run */
353deefe583SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
354ec97e9caSHans Petter Selasky 	usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
355ec97e9caSHans Petter Selasky 	usbd_xfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
356deefe583SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
35702ac6454SAndrew Thompson 
358a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
359deefe583SAndrew Thompson 	    &uchcom_callback, &sc->sc_mtx);
36002ac6454SAndrew Thompson 	if (error) {
36102ac6454SAndrew Thompson 		goto detach;
36202ac6454SAndrew Thompson 	}
3636416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
3646416c259SNick Hibma 
36502ac6454SAndrew Thompson 	return (0);
36602ac6454SAndrew Thompson 
36702ac6454SAndrew Thompson detach:
36802ac6454SAndrew Thompson 	uchcom_detach(dev);
36902ac6454SAndrew Thompson 	return (ENXIO);
37002ac6454SAndrew Thompson }
37102ac6454SAndrew Thompson 
37202ac6454SAndrew Thompson static int
37302ac6454SAndrew Thompson uchcom_detach(device_t dev)
37402ac6454SAndrew Thompson {
37502ac6454SAndrew Thompson 	struct uchcom_softc *sc = device_get_softc(dev);
37602ac6454SAndrew Thompson 
37702ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
37802ac6454SAndrew Thompson 
379015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
380a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER);
38102ac6454SAndrew Thompson 
382c01fc06eSHans Petter Selasky 	device_claim_softc(dev);
383c01fc06eSHans Petter Selasky 
384c01fc06eSHans Petter Selasky 	uchcom_free_softc(sc);
385c01fc06eSHans Petter Selasky 
38602ac6454SAndrew Thompson 	return (0);
38702ac6454SAndrew Thompson }
38802ac6454SAndrew Thompson 
3895805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(uchcom);
3905805d178SHans Petter Selasky 
3915805d178SHans Petter Selasky static void
392c01fc06eSHans Petter Selasky uchcom_free_softc(struct uchcom_softc *sc)
3935805d178SHans Petter Selasky {
3945805d178SHans Petter Selasky 	if (ucom_unref(&sc->sc_super_ucom)) {
3955805d178SHans Petter Selasky 		mtx_destroy(&sc->sc_mtx);
396c01fc06eSHans Petter Selasky 		device_free_softc(sc);
3975805d178SHans Petter Selasky 	}
3985805d178SHans Petter Selasky }
3995805d178SHans Petter Selasky 
4005805d178SHans Petter Selasky static void
4015805d178SHans Petter Selasky uchcom_free(struct ucom_softc *ucom)
4025805d178SHans Petter Selasky {
403c01fc06eSHans Petter Selasky 	uchcom_free_softc(ucom->sc_parent);
4045805d178SHans Petter Selasky }
4055805d178SHans Petter Selasky 
40602ac6454SAndrew Thompson /* ----------------------------------------------------------------------
40702ac6454SAndrew Thompson  * low level i/o
40802ac6454SAndrew Thompson  */
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson static void
41102ac6454SAndrew Thompson uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
41202ac6454SAndrew Thompson     uint16_t value, uint16_t index)
41302ac6454SAndrew Thompson {
414760bc48eSAndrew Thompson 	struct usb_device_request req;
41502ac6454SAndrew Thompson 
41602ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
41702ac6454SAndrew Thompson 	req.bRequest = reqno;
41802ac6454SAndrew Thompson 	USETW(req.wValue, value);
41902ac6454SAndrew Thompson 	USETW(req.wIndex, index);
42002ac6454SAndrew Thompson 	USETW(req.wLength, 0);
42102ac6454SAndrew Thompson 
4221d33c9a5SAndriy Gapon 	DPRINTF("WR REQ 0x%02X VAL 0x%04X IDX 0x%04X\n",
4231d33c9a5SAndriy Gapon 	    reqno, value, index);
424a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev,
42502ac6454SAndrew Thompson 	    &sc->sc_ucom, &req, NULL, 0, 1000);
42602ac6454SAndrew Thompson }
42702ac6454SAndrew Thompson 
42802ac6454SAndrew Thompson static void
42902ac6454SAndrew Thompson uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
43002ac6454SAndrew Thompson     uint16_t value, uint16_t index, void *buf, uint16_t buflen)
43102ac6454SAndrew Thompson {
432760bc48eSAndrew Thompson 	struct usb_device_request req;
43302ac6454SAndrew Thompson 
43402ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
43502ac6454SAndrew Thompson 	req.bRequest = reqno;
43602ac6454SAndrew Thompson 	USETW(req.wValue, value);
43702ac6454SAndrew Thompson 	USETW(req.wIndex, index);
43802ac6454SAndrew Thompson 	USETW(req.wLength, buflen);
43902ac6454SAndrew Thompson 
4401d33c9a5SAndriy Gapon 	DPRINTF("RD REQ 0x%02X VAL 0x%04X IDX 0x%04X LEN %d\n",
4411d33c9a5SAndriy Gapon 	    reqno, value, index, buflen);
442a593f6b8SAndrew Thompson 	ucom_cfg_do_request(sc->sc_udev,
44302ac6454SAndrew Thompson 	    &sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000);
44402ac6454SAndrew Thompson }
44502ac6454SAndrew Thompson 
44602ac6454SAndrew Thompson static void
44702ac6454SAndrew Thompson uchcom_write_reg(struct uchcom_softc *sc,
44802ac6454SAndrew Thompson     uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
44902ac6454SAndrew Thompson {
45002ac6454SAndrew Thompson 	DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
45102ac6454SAndrew Thompson 	    (unsigned)reg1, (unsigned)val1,
45202ac6454SAndrew Thompson 	    (unsigned)reg2, (unsigned)val2);
45302ac6454SAndrew Thompson 	uchcom_ctrl_write(
45402ac6454SAndrew Thompson 	    sc, UCHCOM_REQ_WRITE_REG,
45502ac6454SAndrew Thompson 	    reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
45602ac6454SAndrew Thompson }
45702ac6454SAndrew Thompson 
45802ac6454SAndrew Thompson static void
45902ac6454SAndrew Thompson uchcom_read_reg(struct uchcom_softc *sc,
46002ac6454SAndrew Thompson     uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
46102ac6454SAndrew Thompson {
46202ac6454SAndrew Thompson 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
46302ac6454SAndrew Thompson 
46402ac6454SAndrew Thompson 	uchcom_ctrl_read(
46502ac6454SAndrew Thompson 	    sc, UCHCOM_REQ_READ_REG,
46602ac6454SAndrew Thompson 	    reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
46702ac6454SAndrew Thompson 
46802ac6454SAndrew Thompson 	DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
46902ac6454SAndrew Thompson 	    (unsigned)reg1, (unsigned)buf[0],
47002ac6454SAndrew Thompson 	    (unsigned)reg2, (unsigned)buf[1]);
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson 	if (rval1)
47302ac6454SAndrew Thompson 		*rval1 = buf[0];
47402ac6454SAndrew Thompson 	if (rval2)
47502ac6454SAndrew Thompson 		*rval2 = buf[1];
47602ac6454SAndrew Thompson }
47702ac6454SAndrew Thompson 
47802ac6454SAndrew Thompson static void
47902ac6454SAndrew Thompson uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
48002ac6454SAndrew Thompson {
48102ac6454SAndrew Thompson 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
48202ac6454SAndrew Thompson 
48311f35cfaSAndrew Thompson 	uchcom_ctrl_read(sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
48402ac6454SAndrew Thompson 
48502ac6454SAndrew Thompson 	if (rver)
48602ac6454SAndrew Thompson 		*rver = buf[0];
48702ac6454SAndrew Thompson }
48802ac6454SAndrew Thompson 
48902ac6454SAndrew Thompson static void
49002ac6454SAndrew Thompson uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
49102ac6454SAndrew Thompson {
49202ac6454SAndrew Thompson 	uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
49302ac6454SAndrew Thompson }
49402ac6454SAndrew Thompson 
49502ac6454SAndrew Thompson static void
49611f35cfaSAndrew Thompson uchcom_set_dtr_rts_10(struct uchcom_softc *sc, uint8_t val)
49702ac6454SAndrew Thompson {
49802ac6454SAndrew Thompson 	uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
49902ac6454SAndrew Thompson }
50002ac6454SAndrew Thompson 
50102ac6454SAndrew Thompson static void
50211f35cfaSAndrew Thompson uchcom_set_dtr_rts_20(struct uchcom_softc *sc, uint8_t val)
50302ac6454SAndrew Thompson {
50402ac6454SAndrew Thompson 	uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
50502ac6454SAndrew Thompson }
50602ac6454SAndrew Thompson 
50702ac6454SAndrew Thompson /* ----------------------------------------------------------------------
50802ac6454SAndrew Thompson  * middle layer
50902ac6454SAndrew Thompson  */
51002ac6454SAndrew Thompson 
51102ac6454SAndrew Thompson static void
51202ac6454SAndrew Thompson uchcom_update_version(struct uchcom_softc *sc)
51302ac6454SAndrew Thompson {
51402ac6454SAndrew Thompson 	uchcom_get_version(sc, &sc->sc_version);
5151d33c9a5SAndriy Gapon 	DPRINTF("Chip version: 0x%02x\n", sc->sc_version);
51602ac6454SAndrew Thompson }
51702ac6454SAndrew Thompson 
51802ac6454SAndrew Thompson static void
51902ac6454SAndrew Thompson uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
52002ac6454SAndrew Thompson {
52102ac6454SAndrew Thompson 	sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
52202ac6454SAndrew Thompson 	sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
52302ac6454SAndrew Thompson 
52402ac6454SAndrew Thompson 	cur = ~cur & 0x0F;
52502ac6454SAndrew Thompson 	sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
52602ac6454SAndrew Thompson }
52702ac6454SAndrew Thompson 
52802ac6454SAndrew Thompson static void
52902ac6454SAndrew Thompson uchcom_update_status(struct uchcom_softc *sc)
53002ac6454SAndrew Thompson {
53102ac6454SAndrew Thompson 	uint8_t cur;
53202ac6454SAndrew Thompson 
53302ac6454SAndrew Thompson 	uchcom_get_status(sc, &cur);
53402ac6454SAndrew Thompson 	uchcom_convert_status(sc, cur);
53502ac6454SAndrew Thompson }
53602ac6454SAndrew Thompson 
53702ac6454SAndrew Thompson static void
53811f35cfaSAndrew Thompson uchcom_set_dtr_rts(struct uchcom_softc *sc)
53902ac6454SAndrew Thompson {
54002ac6454SAndrew Thompson 	uint8_t val = 0;
54102ac6454SAndrew Thompson 
54202ac6454SAndrew Thompson 	if (sc->sc_dtr)
54302ac6454SAndrew Thompson 		val |= UCHCOM_DTR_MASK;
54402ac6454SAndrew Thompson 	if (sc->sc_rts)
54502ac6454SAndrew Thompson 		val |= UCHCOM_RTS_MASK;
54602ac6454SAndrew Thompson 
54702ac6454SAndrew Thompson 	if (sc->sc_version < UCHCOM_VER_20)
54811f35cfaSAndrew Thompson 		uchcom_set_dtr_rts_10(sc, ~val);
54902ac6454SAndrew Thompson 	else
55011f35cfaSAndrew Thompson 		uchcom_set_dtr_rts_20(sc, ~val);
55102ac6454SAndrew Thompson }
55202ac6454SAndrew Thompson 
55302ac6454SAndrew Thompson static void
554760bc48eSAndrew Thompson uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
55502ac6454SAndrew Thompson {
55602ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
55702ac6454SAndrew Thompson 	uint8_t brk1;
55802ac6454SAndrew Thompson 	uint8_t brk2;
55902ac6454SAndrew Thompson 
560dad3e656SAndriy Gapon 	uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_LCR1, &brk2);
56102ac6454SAndrew Thompson 	if (onoff) {
56202ac6454SAndrew Thompson 		/* on - clear bits */
563dad3e656SAndriy Gapon 		brk1 &= ~UCHCOM_BRK_MASK;
564dad3e656SAndriy Gapon 		brk2 &= ~UCHCOM_LCR1_TX;
56502ac6454SAndrew Thompson 	} else {
56602ac6454SAndrew Thompson 		/* off - set bits */
567dad3e656SAndriy Gapon 		brk1 |= UCHCOM_BRK_MASK;
568dad3e656SAndriy Gapon 		brk2 |= UCHCOM_LCR1_TX;
56902ac6454SAndrew Thompson 	}
570dad3e656SAndriy Gapon 	uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_LCR1, brk2);
57102ac6454SAndrew Thompson }
57202ac6454SAndrew Thompson 
57302ac6454SAndrew Thompson static int
57402ac6454SAndrew Thompson uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
57502ac6454SAndrew Thompson {
57602ac6454SAndrew Thompson 	const struct uchcom_divider_record *rp;
57702ac6454SAndrew Thompson 	uint32_t div;
57802ac6454SAndrew Thompson 	uint32_t rem;
57902ac6454SAndrew Thompson 	uint32_t mod;
58002ac6454SAndrew Thompson 	uint8_t i;
58102ac6454SAndrew Thompson 
58202ac6454SAndrew Thompson 	/* find record */
58302ac6454SAndrew Thompson 	for (i = 0; i != NUM_DIVIDERS; i++) {
58402ac6454SAndrew Thompson 		if (dividers[i].dvr_high >= rate &&
58502ac6454SAndrew Thompson 		    dividers[i].dvr_low <= rate) {
58602ac6454SAndrew Thompson 			rp = &dividers[i];
58702ac6454SAndrew Thompson 			goto found;
58802ac6454SAndrew Thompson 		}
58902ac6454SAndrew Thompson 	}
59002ac6454SAndrew Thompson 	return (-1);
59102ac6454SAndrew Thompson 
59202ac6454SAndrew Thompson found:
59302ac6454SAndrew Thompson 	dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
59402ac6454SAndrew Thompson 	if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
59502ac6454SAndrew Thompson 		dp->dv_div = rp->dvr_divider.dv_div;
59602ac6454SAndrew Thompson 	else {
59702ac6454SAndrew Thompson 		div = rp->dvr_base_clock / rate;
59802ac6454SAndrew Thompson 		rem = rp->dvr_base_clock % rate;
59902ac6454SAndrew Thompson 		if (div == 0 || div >= 0xFF)
60002ac6454SAndrew Thompson 			return (-1);
60102ac6454SAndrew Thompson 		if ((rem << 1) >= rate)
60202ac6454SAndrew Thompson 			div += 1;
60302ac6454SAndrew Thompson 		dp->dv_div = (uint8_t)-div;
60402ac6454SAndrew Thompson 	}
60502ac6454SAndrew Thompson 
60611f35cfaSAndrew Thompson 	mod = (UCHCOM_BPS_MOD_BASE / rate) + UCHCOM_BPS_MOD_BASE_OFS;
60711f35cfaSAndrew Thompson 	mod = mod + (mod / 2);
60802ac6454SAndrew Thompson 
60911f35cfaSAndrew Thompson 	dp->dv_mod = (mod + 0xFF) / 0x100;
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson 	return (0);
61202ac6454SAndrew Thompson }
61302ac6454SAndrew Thompson 
61402ac6454SAndrew Thompson static void
61511f35cfaSAndrew Thompson uchcom_set_baudrate(struct uchcom_softc *sc, uint32_t rate)
61602ac6454SAndrew Thompson {
61702ac6454SAndrew Thompson 	struct uchcom_divider dv;
61802ac6454SAndrew Thompson 
61902ac6454SAndrew Thompson 	if (uchcom_calc_divider_settings(&dv, rate))
62002ac6454SAndrew Thompson 		return;
62102ac6454SAndrew Thompson 
6227acd73fdSAndriy Gapon 	/*
6237acd73fdSAndriy Gapon 	 * According to linux code we need to set bit 7 of UCHCOM_REG_BPS_PRE,
6247acd73fdSAndriy Gapon 	 * otherwise the chip will buffer data.
6257acd73fdSAndriy Gapon 	 */
62602ac6454SAndrew Thompson 	uchcom_write_reg(sc,
6277acd73fdSAndriy Gapon 	    UCHCOM_REG_BPS_PRE, dv.dv_prescaler | 0x80,
62802ac6454SAndrew Thompson 	    UCHCOM_REG_BPS_DIV, dv.dv_div);
62902ac6454SAndrew Thompson 	uchcom_write_reg(sc,
63002ac6454SAndrew Thompson 	    UCHCOM_REG_BPS_MOD, dv.dv_mod,
63102ac6454SAndrew Thompson 	    UCHCOM_REG_BPS_PAD, 0);
63202ac6454SAndrew Thompson }
63302ac6454SAndrew Thompson 
63402ac6454SAndrew Thompson /* ----------------------------------------------------------------------
63502ac6454SAndrew Thompson  * methods for ucom
63602ac6454SAndrew Thompson  */
63702ac6454SAndrew Thompson static void
638760bc48eSAndrew Thompson uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
63902ac6454SAndrew Thompson {
64002ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
64102ac6454SAndrew Thompson 
64202ac6454SAndrew Thompson 	DPRINTF("\n");
64302ac6454SAndrew Thompson 
644fe67c846SIan Lepore 	/* XXX Note: sc_lsr is always zero */
64502ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
64602ac6454SAndrew Thompson 	*msr = sc->sc_msr;
64702ac6454SAndrew Thompson }
64802ac6454SAndrew Thompson 
64902ac6454SAndrew Thompson static void
650760bc48eSAndrew Thompson uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
65102ac6454SAndrew Thompson {
65202ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
65302ac6454SAndrew Thompson 
65402ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
65502ac6454SAndrew Thompson 
65602ac6454SAndrew Thompson 	sc->sc_dtr = onoff;
65711f35cfaSAndrew Thompson 	uchcom_set_dtr_rts(sc);
65802ac6454SAndrew Thompson }
65902ac6454SAndrew Thompson 
66002ac6454SAndrew Thompson static void
661760bc48eSAndrew Thompson uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
66202ac6454SAndrew Thompson {
66302ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
66402ac6454SAndrew Thompson 
66502ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
66602ac6454SAndrew Thompson 
66702ac6454SAndrew Thompson 	sc->sc_rts = onoff;
66811f35cfaSAndrew Thompson 	uchcom_set_dtr_rts(sc);
66911f35cfaSAndrew Thompson }
67011f35cfaSAndrew Thompson 
67111f35cfaSAndrew Thompson static void
67211f35cfaSAndrew Thompson uchcom_cfg_open(struct ucom_softc *ucom)
67311f35cfaSAndrew Thompson {
67411f35cfaSAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
67511f35cfaSAndrew Thompson 
67611f35cfaSAndrew Thompson 	DPRINTF("\n");
67711f35cfaSAndrew Thompson 
67811f35cfaSAndrew Thompson 	uchcom_update_version(sc);
67911f35cfaSAndrew Thompson 	uchcom_update_status(sc);
68002ac6454SAndrew Thompson }
68102ac6454SAndrew Thompson 
68202ac6454SAndrew Thompson static int
683760bc48eSAndrew Thompson uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
68402ac6454SAndrew Thompson {
68502ac6454SAndrew Thompson 	struct uchcom_divider dv;
68602ac6454SAndrew Thompson 
68702ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
68811f35cfaSAndrew Thompson 	case CS8:
68902ac6454SAndrew Thompson 		break;
69011f35cfaSAndrew Thompson 	default:
69111f35cfaSAndrew Thompson 		return (EIO);
69202ac6454SAndrew Thompson 	}
69340e7b064SAndriy Gapon 	if ((t->c_cflag & CSTOPB) != 0)
69440e7b064SAndriy Gapon 		return (EIO);
69540e7b064SAndriy Gapon 	if ((t->c_cflag & PARENB) != 0)
69640e7b064SAndriy Gapon 		return (EIO);
69702ac6454SAndrew Thompson 
69802ac6454SAndrew Thompson 	if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
69902ac6454SAndrew Thompson 		return (EIO);
70002ac6454SAndrew Thompson 	}
70102ac6454SAndrew Thompson 	return (0);			/* success */
70202ac6454SAndrew Thompson }
70302ac6454SAndrew Thompson 
70402ac6454SAndrew Thompson static void
705760bc48eSAndrew Thompson uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
70602ac6454SAndrew Thompson {
70702ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
70802ac6454SAndrew Thompson 
709a23a4e68SAndriy Gapon 	uchcom_get_version(sc, NULL);
71011f35cfaSAndrew Thompson 	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
71111f35cfaSAndrew Thompson 	uchcom_set_baudrate(sc, t->c_ospeed);
712a23a4e68SAndriy Gapon 	if (sc->sc_version < UCHCOM_VER_30) {
713a23a4e68SAndriy Gapon 		uchcom_read_reg(sc, UCHCOM_REG_LCR1, NULL,
714a23a4e68SAndriy Gapon 		    UCHCOM_REG_LCR2, NULL);
715a23a4e68SAndriy Gapon 		uchcom_write_reg(sc, UCHCOM_REG_LCR1, 0x50,
716a23a4e68SAndriy Gapon 		    UCHCOM_REG_LCR2, 0x00);
717a23a4e68SAndriy Gapon 	} else {
718a23a4e68SAndriy Gapon 		/*
719a23a4e68SAndriy Gapon 		 * Set up line control:
720a23a4e68SAndriy Gapon 		 * - enable transmit and receive
721a23a4e68SAndriy Gapon 		 * - set 8n1 mode
722a23a4e68SAndriy Gapon 		 * To do: support other sizes, parity, stop bits.
723a23a4e68SAndriy Gapon 		 */
724a23a4e68SAndriy Gapon 		uchcom_write_reg(sc,
725a23a4e68SAndriy Gapon 		    UCHCOM_REG_LCR1,
726a23a4e68SAndriy Gapon 		    UCHCOM_LCR1_RX | UCHCOM_LCR1_TX | UCHCOM_LCR1_CS8,
727a23a4e68SAndriy Gapon 		    UCHCOM_REG_LCR2, 0x00);
728a23a4e68SAndriy Gapon 	}
72911f35cfaSAndrew Thompson 	uchcom_update_status(sc);
73011f35cfaSAndrew Thompson 	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0x501f, 0xd90a);
73111f35cfaSAndrew Thompson 	uchcom_set_baudrate(sc, t->c_ospeed);
73211f35cfaSAndrew Thompson 	uchcom_set_dtr_rts(sc);
73311f35cfaSAndrew Thompson 	uchcom_update_status(sc);
73402ac6454SAndrew Thompson }
73502ac6454SAndrew Thompson 
73602ac6454SAndrew Thompson static void
737760bc48eSAndrew Thompson uchcom_start_read(struct ucom_softc *ucom)
73802ac6454SAndrew Thompson {
73902ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
74002ac6454SAndrew Thompson 
74102ac6454SAndrew Thompson 	/* start interrupt endpoint */
742a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
74302ac6454SAndrew Thompson 
74402ac6454SAndrew Thompson 	/* start read endpoint */
745a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
74602ac6454SAndrew Thompson }
74702ac6454SAndrew Thompson 
74802ac6454SAndrew Thompson static void
749760bc48eSAndrew Thompson uchcom_stop_read(struct ucom_softc *ucom)
75002ac6454SAndrew Thompson {
75102ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
75202ac6454SAndrew Thompson 
75302ac6454SAndrew Thompson 	/* stop interrupt endpoint */
754a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
75502ac6454SAndrew Thompson 
75602ac6454SAndrew Thompson 	/* stop read endpoint */
757a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
75802ac6454SAndrew Thompson }
75902ac6454SAndrew Thompson 
76002ac6454SAndrew Thompson static void
761760bc48eSAndrew Thompson uchcom_start_write(struct ucom_softc *ucom)
76202ac6454SAndrew Thompson {
76302ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
76402ac6454SAndrew Thompson 
765a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
76602ac6454SAndrew Thompson }
76702ac6454SAndrew Thompson 
76802ac6454SAndrew Thompson static void
769760bc48eSAndrew Thompson uchcom_stop_write(struct ucom_softc *ucom)
77002ac6454SAndrew Thompson {
77102ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
77202ac6454SAndrew Thompson 
773a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
77402ac6454SAndrew Thompson }
77502ac6454SAndrew Thompson 
77602ac6454SAndrew Thompson /* ----------------------------------------------------------------------
77702ac6454SAndrew Thompson  * callback when the modem status is changed.
77802ac6454SAndrew Thompson  */
77902ac6454SAndrew Thompson static void
780ed6d949aSAndrew Thompson uchcom_intr_callback(struct usb_xfer *xfer, usb_error_t error)
78102ac6454SAndrew Thompson {
782ed6d949aSAndrew Thompson 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
783ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
78402ac6454SAndrew Thompson 	uint8_t buf[UCHCOM_INTR_LEAST];
785ed6d949aSAndrew Thompson 	int actlen;
786ed6d949aSAndrew Thompson 
787ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
78802ac6454SAndrew Thompson 
78902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
79002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
79102ac6454SAndrew Thompson 
792ed6d949aSAndrew Thompson 		DPRINTF("actlen = %u\n", actlen);
79302ac6454SAndrew Thompson 
794ed6d949aSAndrew Thompson 		if (actlen >= UCHCOM_INTR_LEAST) {
795ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
796ed6d949aSAndrew Thompson 			usbd_copy_out(pc, 0, buf, UCHCOM_INTR_LEAST);
79702ac6454SAndrew Thompson 
79802ac6454SAndrew Thompson 			DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
79902ac6454SAndrew Thompson 			    (unsigned)buf[0], (unsigned)buf[1],
80002ac6454SAndrew Thompson 			    (unsigned)buf[2], (unsigned)buf[3]);
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson 			uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
803a593f6b8SAndrew Thompson 			ucom_status_change(&sc->sc_ucom);
80402ac6454SAndrew Thompson 		}
80502ac6454SAndrew Thompson 	case USB_ST_SETUP:
80602ac6454SAndrew Thompson tr_setup:
807ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
808a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
80902ac6454SAndrew Thompson 		break;
81002ac6454SAndrew Thompson 
81102ac6454SAndrew Thompson 	default:			/* Error */
812ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
81302ac6454SAndrew Thompson 			/* try to clear stall first */
814ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
81502ac6454SAndrew Thompson 			goto tr_setup;
81602ac6454SAndrew Thompson 		}
81702ac6454SAndrew Thompson 		break;
81802ac6454SAndrew Thompson 	}
81902ac6454SAndrew Thompson }
82002ac6454SAndrew Thompson 
82102ac6454SAndrew Thompson static void
822ed6d949aSAndrew Thompson uchcom_write_callback(struct usb_xfer *xfer, usb_error_t error)
82302ac6454SAndrew Thompson {
824ed6d949aSAndrew Thompson 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
825ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
82602ac6454SAndrew Thompson 	uint32_t actlen;
82702ac6454SAndrew Thompson 
82802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
82902ac6454SAndrew Thompson 	case USB_ST_SETUP:
83002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
83102ac6454SAndrew Thompson tr_setup:
832ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
833ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
83411f35cfaSAndrew Thompson 		    usbd_xfer_max_len(xfer), &actlen)) {
83502ac6454SAndrew Thompson 			DPRINTF("actlen = %d\n", actlen);
83602ac6454SAndrew Thompson 
837ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
838a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
83902ac6454SAndrew Thompson 		}
84011f35cfaSAndrew Thompson 		break;
84102ac6454SAndrew Thompson 
84202ac6454SAndrew Thompson 	default:			/* Error */
843ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
84402ac6454SAndrew Thompson 			/* try to clear stall first */
845ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
84602ac6454SAndrew Thompson 			goto tr_setup;
84702ac6454SAndrew Thompson 		}
84811f35cfaSAndrew Thompson 		break;
84902ac6454SAndrew Thompson 	}
85002ac6454SAndrew Thompson }
85102ac6454SAndrew Thompson 
85202ac6454SAndrew Thompson static void
853ed6d949aSAndrew Thompson uchcom_read_callback(struct usb_xfer *xfer, usb_error_t error)
85402ac6454SAndrew Thompson {
855ed6d949aSAndrew Thompson 	struct uchcom_softc *sc = usbd_xfer_softc(xfer);
856ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
857ed6d949aSAndrew Thompson 	int actlen;
858ed6d949aSAndrew Thompson 
859ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
86002ac6454SAndrew Thompson 
86102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
86202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
86311f35cfaSAndrew Thompson 
86411f35cfaSAndrew Thompson 		if (actlen > 0) {
865ed6d949aSAndrew Thompson 			pc = usbd_xfer_get_frame(xfer, 0);
866ed6d949aSAndrew Thompson 			ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
86711f35cfaSAndrew Thompson 		}
86802ac6454SAndrew Thompson 
86902ac6454SAndrew Thompson 	case USB_ST_SETUP:
87002ac6454SAndrew Thompson tr_setup:
871ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
872a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
87311f35cfaSAndrew Thompson 		break;
87402ac6454SAndrew Thompson 
87502ac6454SAndrew Thompson 	default:			/* Error */
876ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
87702ac6454SAndrew Thompson 			/* try to clear stall first */
878ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
87902ac6454SAndrew Thompson 			goto tr_setup;
88002ac6454SAndrew Thompson 		}
88111f35cfaSAndrew Thompson 		break;
88202ac6454SAndrew Thompson 	}
88302ac6454SAndrew Thompson }
88402ac6454SAndrew Thompson 
885655dc9d0SAndrew Thompson static void
886655dc9d0SAndrew Thompson uchcom_poll(struct ucom_softc *ucom)
887655dc9d0SAndrew Thompson {
888655dc9d0SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
889655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_xfer, UCHCOM_N_TRANSFER);
890655dc9d0SAndrew Thompson }
891655dc9d0SAndrew Thompson 
89202ac6454SAndrew Thompson static device_method_t uchcom_methods[] = {
89302ac6454SAndrew Thompson 	/* Device interface */
89402ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uchcom_probe),
89502ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uchcom_attach),
89602ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uchcom_detach),
8975805d178SHans Petter Selasky 	DEVMETHOD_END
89802ac6454SAndrew Thompson };
89902ac6454SAndrew Thompson 
90002ac6454SAndrew Thompson static driver_t uchcom_driver = {
9013c12706cSHans Petter Selasky 	.name = "uchcom",
9026d917491SHans Petter Selasky 	.methods = uchcom_methods,
9036d917491SHans Petter Selasky 	.size = sizeof(struct uchcom_softc)
90402ac6454SAndrew Thompson };
90502ac6454SAndrew Thompson 
90602ac6454SAndrew Thompson static devclass_t uchcom_devclass;
90702ac6454SAndrew Thompson 
9089aef556dSAndrew Thompson DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0);
90902ac6454SAndrew Thompson MODULE_DEPEND(uchcom, ucom, 1, 1, 1);
91002ac6454SAndrew Thompson MODULE_DEPEND(uchcom, usb, 1, 1, 1);
911910cb8feSAndrew Thompson MODULE_VERSION(uchcom, 1);
912f809f280SWarner Losh USB_PNP_HOST_INFO(uchcom_devs);
913