xref: /freebsd/sys/dev/usb/serial/uchcom.c (revision a8675cae2b38ae9c7abb981e4a11e21a757680e6)
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  * 3. All advertising materials mentioning features or use of this software
4502ac6454SAndrew Thompson  *    must display the following acknowledgement:
4602ac6454SAndrew Thompson  *        This product includes software developed by the NetBSD
4702ac6454SAndrew Thompson  *        Foundation, Inc. and its contributors.
4802ac6454SAndrew Thompson  * 4. Neither the name of The NetBSD Foundation nor the names of its
4902ac6454SAndrew Thompson  *    contributors may be used to endorse or promote products derived
5002ac6454SAndrew Thompson  *    from this software without specific prior written permission.
5102ac6454SAndrew Thompson  *
5202ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
5302ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
5402ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5502ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
5602ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5702ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5802ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5902ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6002ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6102ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6202ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
6302ac6454SAndrew Thompson  */
6402ac6454SAndrew Thompson 
6502ac6454SAndrew Thompson #include <sys/cdefs.h>
6602ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
6702ac6454SAndrew Thompson 
6802ac6454SAndrew Thompson /*
6902ac6454SAndrew Thompson  * driver for WinChipHead CH341/340, the worst USB-serial chip in the world.
7002ac6454SAndrew Thompson  */
7102ac6454SAndrew Thompson 
7202ac6454SAndrew Thompson #include "usbdevs.h"
7302ac6454SAndrew Thompson #include <dev/usb/usb.h>
7402ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
7502ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
7602ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
7702ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h>
7802ac6454SAndrew Thompson 
7902ac6454SAndrew Thompson #define	USB_DEBUG_VAR uchcom_debug
8002ac6454SAndrew Thompson 
8102ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
8202ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
8302ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
8402ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
8502ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h>
8602ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
8702ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
8802ac6454SAndrew Thompson 
8902ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
9002ac6454SAndrew Thompson 
9102ac6454SAndrew Thompson #if USB_DEBUG
9202ac6454SAndrew Thompson static int uchcom_debug = 0;
9302ac6454SAndrew Thompson 
949360ae40SAndrew Thompson SYSCTL_NODE(_hw_usb, OID_AUTO, uchcom, CTLFLAG_RW, 0, "USB uchcom");
959360ae40SAndrew Thompson SYSCTL_INT(_hw_usb_uchcom, OID_AUTO, debug, CTLFLAG_RW,
9602ac6454SAndrew Thompson     &uchcom_debug, 0, "uchcom debug level");
9702ac6454SAndrew Thompson #endif
9802ac6454SAndrew Thompson 
9902ac6454SAndrew Thompson #define	UCHCOM_IFACE_INDEX	0
10002ac6454SAndrew Thompson #define	UCHCOM_CONFIG_INDEX	0
10102ac6454SAndrew Thompson 
10202ac6454SAndrew Thompson #define	UCHCOM_REV_CH340	0x0250
10302ac6454SAndrew Thompson #define	UCHCOM_INPUT_BUF_SIZE	8
10402ac6454SAndrew Thompson 
10502ac6454SAndrew Thompson #define	UCHCOM_REQ_GET_VERSION	0x5F
10602ac6454SAndrew Thompson #define	UCHCOM_REQ_READ_REG	0x95
10702ac6454SAndrew Thompson #define	UCHCOM_REQ_WRITE_REG	0x9A
10802ac6454SAndrew Thompson #define	UCHCOM_REQ_RESET	0xA1
10902ac6454SAndrew Thompson #define	UCHCOM_REQ_SET_DTRRTS	0xA4
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson #define	UCHCOM_REG_STAT1	0x06
11202ac6454SAndrew Thompson #define	UCHCOM_REG_STAT2	0x07
11302ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_PRE	0x12
11402ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_DIV	0x13
11502ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_MOD	0x14
11602ac6454SAndrew Thompson #define	UCHCOM_REG_BPS_PAD	0x0F
11702ac6454SAndrew Thompson #define	UCHCOM_REG_BREAK1	0x05
11802ac6454SAndrew Thompson #define	UCHCOM_REG_BREAK2	0x18
11902ac6454SAndrew Thompson #define	UCHCOM_REG_LCR1		0x18
12002ac6454SAndrew Thompson #define	UCHCOM_REG_LCR2		0x25
12102ac6454SAndrew Thompson 
12202ac6454SAndrew Thompson #define	UCHCOM_VER_20		0x20
12302ac6454SAndrew Thompson 
12402ac6454SAndrew Thompson #define	UCHCOM_BASE_UNKNOWN	0
12502ac6454SAndrew Thompson #define	UCHCOM_BPS_MOD_BASE	20000000
12602ac6454SAndrew Thompson #define	UCHCOM_BPS_MOD_BASE_OFS	1100
12702ac6454SAndrew Thompson 
12802ac6454SAndrew Thompson #define	UCHCOM_DTR_MASK		0x20
12902ac6454SAndrew Thompson #define	UCHCOM_RTS_MASK		0x40
13002ac6454SAndrew Thompson 
13102ac6454SAndrew Thompson #define	UCHCOM_BRK1_MASK	0x01
13202ac6454SAndrew Thompson #define	UCHCOM_BRK2_MASK	0x40
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson #define	UCHCOM_LCR1_MASK	0xAF
13502ac6454SAndrew Thompson #define	UCHCOM_LCR2_MASK	0x07
13602ac6454SAndrew Thompson #define	UCHCOM_LCR1_PARENB	0x80
13702ac6454SAndrew Thompson #define	UCHCOM_LCR2_PAREVEN	0x07
13802ac6454SAndrew Thompson #define	UCHCOM_LCR2_PARODD	0x06
13902ac6454SAndrew Thompson #define	UCHCOM_LCR2_PARMARK	0x05
14002ac6454SAndrew Thompson #define	UCHCOM_LCR2_PARSPACE	0x04
14102ac6454SAndrew Thompson 
14202ac6454SAndrew Thompson #define	UCHCOM_INTR_STAT1	0x02
14302ac6454SAndrew Thompson #define	UCHCOM_INTR_STAT2	0x03
14402ac6454SAndrew Thompson #define	UCHCOM_INTR_LEAST	4
14502ac6454SAndrew Thompson 
14602ac6454SAndrew Thompson #define	UCHCOM_BULK_BUF_SIZE 1024	/* bytes */
14702ac6454SAndrew Thompson 
14802ac6454SAndrew Thompson enum {
14902ac6454SAndrew Thompson 	UCHCOM_BULK_DT_WR,
15002ac6454SAndrew Thompson 	UCHCOM_BULK_DT_RD,
15102ac6454SAndrew Thompson 	UCHCOM_INTR_DT_RD,
15202ac6454SAndrew Thompson 	UCHCOM_N_TRANSFER,
15302ac6454SAndrew Thompson };
15402ac6454SAndrew Thompson 
15502ac6454SAndrew Thompson struct uchcom_softc {
156760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
157760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
15802ac6454SAndrew Thompson 
159760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UCHCOM_N_TRANSFER];
160760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
161deefe583SAndrew Thompson 	struct mtx sc_mtx;
16202ac6454SAndrew Thompson 
16302ac6454SAndrew Thompson 	uint8_t	sc_dtr;			/* local copy */
16402ac6454SAndrew Thompson 	uint8_t	sc_rts;			/* local copy */
16502ac6454SAndrew Thompson 	uint8_t	sc_version;
16602ac6454SAndrew Thompson 	uint8_t	sc_msr;
16702ac6454SAndrew Thompson 	uint8_t	sc_lsr;			/* local status register */
16802ac6454SAndrew Thompson };
16902ac6454SAndrew Thompson 
17002ac6454SAndrew Thompson struct uchcom_divider {
17102ac6454SAndrew Thompson 	uint8_t	dv_prescaler;
17202ac6454SAndrew Thompson 	uint8_t	dv_div;
17302ac6454SAndrew Thompson 	uint8_t	dv_mod;
17402ac6454SAndrew Thompson };
17502ac6454SAndrew Thompson 
17602ac6454SAndrew Thompson struct uchcom_divider_record {
17702ac6454SAndrew Thompson 	uint32_t dvr_high;
17802ac6454SAndrew Thompson 	uint32_t dvr_low;
17902ac6454SAndrew Thompson 	uint32_t dvr_base_clock;
18002ac6454SAndrew Thompson 	struct uchcom_divider dvr_divider;
18102ac6454SAndrew Thompson };
18202ac6454SAndrew Thompson 
18302ac6454SAndrew Thompson static const struct uchcom_divider_record dividers[] =
18402ac6454SAndrew Thompson {
18502ac6454SAndrew Thompson 	{307200, 307200, UCHCOM_BASE_UNKNOWN, {7, 0xD9, 0}},
18602ac6454SAndrew Thompson 	{921600, 921600, UCHCOM_BASE_UNKNOWN, {7, 0xF3, 0}},
18702ac6454SAndrew Thompson 	{2999999, 23530, 6000000, {3, 0, 0}},
18802ac6454SAndrew Thompson 	{23529, 2942, 750000, {2, 0, 0}},
18902ac6454SAndrew Thompson 	{2941, 368, 93750, {1, 0, 0}},
19002ac6454SAndrew Thompson 	{367, 1, 11719, {0, 0, 0}},
19102ac6454SAndrew Thompson };
19202ac6454SAndrew Thompson 
19302ac6454SAndrew Thompson #define	NUM_DIVIDERS	(sizeof (dividers) / sizeof (dividers[0]))
19402ac6454SAndrew Thompson 
195760bc48eSAndrew Thompson static const struct usb_device_id uchcom_devs[] = {
19602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_WCH, USB_PRODUCT_WCH_CH341SER, 0)},
19702ac6454SAndrew Thompson };
19802ac6454SAndrew Thompson 
19902ac6454SAndrew Thompson /* protypes */
20002ac6454SAndrew Thompson 
201760bc48eSAndrew Thompson static int	uchcom_pre_param(struct ucom_softc *, struct termios *);
202760bc48eSAndrew Thompson static void	uchcom_cfg_get_status(struct ucom_softc *, uint8_t *,
20302ac6454SAndrew Thompson 		    uint8_t *);
204760bc48eSAndrew Thompson static void	uchcom_cfg_param(struct ucom_softc *, struct termios *);
205760bc48eSAndrew Thompson static void	uchcom_cfg_set_break(struct ucom_softc *, uint8_t);
206760bc48eSAndrew Thompson static void	uchcom_cfg_set_dtr(struct ucom_softc *, uint8_t);
207760bc48eSAndrew Thompson static void	uchcom_cfg_set_rts(struct ucom_softc *, uint8_t);
208760bc48eSAndrew Thompson static void	uchcom_start_read(struct ucom_softc *);
209760bc48eSAndrew Thompson static void	uchcom_start_write(struct ucom_softc *);
210760bc48eSAndrew Thompson static void	uchcom_stop_read(struct ucom_softc *);
211760bc48eSAndrew Thompson static void	uchcom_stop_write(struct ucom_softc *);
21202ac6454SAndrew Thompson static void	uchcom_update_version(struct uchcom_softc *);
21302ac6454SAndrew Thompson static void	uchcom_convert_status(struct uchcom_softc *, uint8_t);
21402ac6454SAndrew Thompson static void	uchcom_update_status(struct uchcom_softc *);
21502ac6454SAndrew Thompson static void	uchcom_set_dtrrts(struct uchcom_softc *);
21602ac6454SAndrew Thompson static int	uchcom_calc_divider_settings(struct uchcom_divider *, uint32_t);
21702ac6454SAndrew Thompson static void	uchcom_set_dte_rate(struct uchcom_softc *, uint32_t);
21802ac6454SAndrew Thompson static void	uchcom_set_line_control(struct uchcom_softc *, tcflag_t);
21902ac6454SAndrew Thompson static void	uchcom_clear_chip(struct uchcom_softc *);
22002ac6454SAndrew Thompson static void	uchcom_reset_chip(struct uchcom_softc *);
22102ac6454SAndrew Thompson 
22202ac6454SAndrew Thompson static device_probe_t uchcom_probe;
22302ac6454SAndrew Thompson static device_attach_t uchcom_attach;
22402ac6454SAndrew Thompson static device_detach_t uchcom_detach;
22502ac6454SAndrew Thompson 
226e0a69b51SAndrew Thompson static usb_callback_t uchcom_intr_callback;
227e0a69b51SAndrew Thompson static usb_callback_t uchcom_write_callback;
228e0a69b51SAndrew Thompson static usb_callback_t uchcom_read_callback;
22902ac6454SAndrew Thompson 
230760bc48eSAndrew Thompson static const struct usb_config uchcom_config_data[UCHCOM_N_TRANSFER] = {
23102ac6454SAndrew Thompson 
23202ac6454SAndrew Thompson 	[UCHCOM_BULK_DT_WR] = {
23302ac6454SAndrew Thompson 		.type = UE_BULK,
23402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
23502ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
2364eae601eSAndrew Thompson 		.bufsize = UCHCOM_BULK_BUF_SIZE,
2374eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
2384eae601eSAndrew Thompson 		.callback = &uchcom_write_callback,
23902ac6454SAndrew Thompson 	},
24002ac6454SAndrew Thompson 
24102ac6454SAndrew Thompson 	[UCHCOM_BULK_DT_RD] = {
24202ac6454SAndrew Thompson 		.type = UE_BULK,
24302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
24402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2454eae601eSAndrew Thompson 		.bufsize = UCHCOM_BULK_BUF_SIZE,
2464eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2474eae601eSAndrew Thompson 		.callback = &uchcom_read_callback,
24802ac6454SAndrew Thompson 	},
24902ac6454SAndrew Thompson 
25002ac6454SAndrew Thompson 	[UCHCOM_INTR_DT_RD] = {
25102ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
25202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
25302ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
2544eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
2554eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
2564eae601eSAndrew Thompson 		.callback = &uchcom_intr_callback,
25702ac6454SAndrew Thompson 	},
25802ac6454SAndrew Thompson };
25902ac6454SAndrew Thompson 
260a8675caeSAndrew Thompson static struct ucom_callback uchcom_callback = {
26102ac6454SAndrew Thompson 	.usb2_com_cfg_get_status = &uchcom_cfg_get_status,
26202ac6454SAndrew Thompson 	.usb2_com_cfg_set_dtr = &uchcom_cfg_set_dtr,
26302ac6454SAndrew Thompson 	.usb2_com_cfg_set_rts = &uchcom_cfg_set_rts,
26402ac6454SAndrew Thompson 	.usb2_com_cfg_set_break = &uchcom_cfg_set_break,
26502ac6454SAndrew Thompson 	.usb2_com_cfg_param = &uchcom_cfg_param,
26602ac6454SAndrew Thompson 	.usb2_com_pre_param = &uchcom_pre_param,
26702ac6454SAndrew Thompson 	.usb2_com_start_read = &uchcom_start_read,
26802ac6454SAndrew Thompson 	.usb2_com_stop_read = &uchcom_stop_read,
26902ac6454SAndrew Thompson 	.usb2_com_start_write = &uchcom_start_write,
27002ac6454SAndrew Thompson 	.usb2_com_stop_write = &uchcom_stop_write,
27102ac6454SAndrew Thompson };
27202ac6454SAndrew Thompson 
27302ac6454SAndrew Thompson /* ----------------------------------------------------------------------
27402ac6454SAndrew Thompson  * driver entry points
27502ac6454SAndrew Thompson  */
27602ac6454SAndrew Thompson 
27702ac6454SAndrew Thompson static int
27802ac6454SAndrew Thompson uchcom_probe(device_t dev)
27902ac6454SAndrew Thompson {
280760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
28102ac6454SAndrew Thompson 
28202ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
28302ac6454SAndrew Thompson 
284f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
28502ac6454SAndrew Thompson 		return (ENXIO);
28602ac6454SAndrew Thompson 	}
28702ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UCHCOM_CONFIG_INDEX) {
28802ac6454SAndrew Thompson 		return (ENXIO);
28902ac6454SAndrew Thompson 	}
29002ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UCHCOM_IFACE_INDEX) {
29102ac6454SAndrew Thompson 		return (ENXIO);
29202ac6454SAndrew Thompson 	}
29302ac6454SAndrew Thompson 	return (usb2_lookup_id_by_uaa(uchcom_devs, sizeof(uchcom_devs), uaa));
29402ac6454SAndrew Thompson }
29502ac6454SAndrew Thompson 
29602ac6454SAndrew Thompson static int
29702ac6454SAndrew Thompson uchcom_attach(device_t dev)
29802ac6454SAndrew Thompson {
29902ac6454SAndrew Thompson 	struct uchcom_softc *sc = device_get_softc(dev);
300760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
30102ac6454SAndrew Thompson 	int error;
30202ac6454SAndrew Thompson 	uint8_t iface_index;
30302ac6454SAndrew Thompson 
30402ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
30502ac6454SAndrew Thompson 
30602ac6454SAndrew Thompson 	device_set_usb2_desc(dev);
307deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "uchcom", NULL, MTX_DEF);
30802ac6454SAndrew Thompson 
30902ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
31002ac6454SAndrew Thompson 
31102ac6454SAndrew Thompson 	switch (uaa->info.bcdDevice) {
31202ac6454SAndrew Thompson 	case UCHCOM_REV_CH340:
31302ac6454SAndrew Thompson 		device_printf(dev, "CH340 detected\n");
31402ac6454SAndrew Thompson 		break;
31502ac6454SAndrew Thompson 	default:
31602ac6454SAndrew Thompson 		device_printf(dev, "CH341 detected\n");
31702ac6454SAndrew Thompson 		break;
31802ac6454SAndrew Thompson 	}
31902ac6454SAndrew Thompson 
32002ac6454SAndrew Thompson 	iface_index = UCHCOM_IFACE_INDEX;
32102ac6454SAndrew Thompson 	error = usb2_transfer_setup(uaa->device,
32202ac6454SAndrew Thompson 	    &iface_index, sc->sc_xfer, uchcom_config_data,
323deefe583SAndrew Thompson 	    UCHCOM_N_TRANSFER, sc, &sc->sc_mtx);
32402ac6454SAndrew Thompson 
32502ac6454SAndrew Thompson 	if (error) {
32602ac6454SAndrew Thompson 		DPRINTF("one or more missing USB endpoints, "
32702ac6454SAndrew Thompson 		    "error=%s\n", usb2_errstr(error));
32802ac6454SAndrew Thompson 		goto detach;
32902ac6454SAndrew Thompson 	}
33002ac6454SAndrew Thompson 	/*
33102ac6454SAndrew Thompson 	 * Do the initialization during attach so that the system does not
33202ac6454SAndrew Thompson 	 * sleep during open:
33302ac6454SAndrew Thompson 	 */
33402ac6454SAndrew Thompson 	uchcom_update_version(sc);
33502ac6454SAndrew Thompson 	uchcom_clear_chip(sc);
33602ac6454SAndrew Thompson 	uchcom_reset_chip(sc);
33702ac6454SAndrew Thompson 	uchcom_update_status(sc);
33802ac6454SAndrew Thompson 
33902ac6454SAndrew Thompson 	sc->sc_dtr = 1;
34002ac6454SAndrew Thompson 	sc->sc_rts = 1;
34102ac6454SAndrew Thompson 
34202ac6454SAndrew Thompson 	/* clear stall at first run */
343deefe583SAndrew Thompson 	mtx_lock(&sc->sc_mtx);
34402ac6454SAndrew Thompson 	usb2_transfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
34502ac6454SAndrew Thompson 	usb2_transfer_set_stall(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
346deefe583SAndrew Thompson 	mtx_unlock(&sc->sc_mtx);
34702ac6454SAndrew Thompson 
34802ac6454SAndrew Thompson 	error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
349deefe583SAndrew Thompson 	    &uchcom_callback, &sc->sc_mtx);
35002ac6454SAndrew Thompson 	if (error) {
35102ac6454SAndrew Thompson 		goto detach;
35202ac6454SAndrew Thompson 	}
35302ac6454SAndrew Thompson 	return (0);
35402ac6454SAndrew Thompson 
35502ac6454SAndrew Thompson detach:
35602ac6454SAndrew Thompson 	uchcom_detach(dev);
35702ac6454SAndrew Thompson 	return (ENXIO);
35802ac6454SAndrew Thompson }
35902ac6454SAndrew Thompson 
36002ac6454SAndrew Thompson static int
36102ac6454SAndrew Thompson uchcom_detach(device_t dev)
36202ac6454SAndrew Thompson {
36302ac6454SAndrew Thompson 	struct uchcom_softc *sc = device_get_softc(dev);
36402ac6454SAndrew Thompson 
36502ac6454SAndrew Thompson 	DPRINTFN(11, "\n");
36602ac6454SAndrew Thompson 
36702ac6454SAndrew Thompson 	usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
36802ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_xfer, UCHCOM_N_TRANSFER);
369deefe583SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson 	return (0);
37202ac6454SAndrew Thompson }
37302ac6454SAndrew Thompson 
37402ac6454SAndrew Thompson /* ----------------------------------------------------------------------
37502ac6454SAndrew Thompson  * low level i/o
37602ac6454SAndrew Thompson  */
37702ac6454SAndrew Thompson 
37802ac6454SAndrew Thompson static void
37902ac6454SAndrew Thompson uchcom_ctrl_write(struct uchcom_softc *sc, uint8_t reqno,
38002ac6454SAndrew Thompson     uint16_t value, uint16_t index)
38102ac6454SAndrew Thompson {
382760bc48eSAndrew Thompson 	struct usb_device_request req;
38302ac6454SAndrew Thompson 
38402ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
38502ac6454SAndrew Thompson 	req.bRequest = reqno;
38602ac6454SAndrew Thompson 	USETW(req.wValue, value);
38702ac6454SAndrew Thompson 	USETW(req.wIndex, index);
38802ac6454SAndrew Thompson 	USETW(req.wLength, 0);
38902ac6454SAndrew Thompson 
39002ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev,
39102ac6454SAndrew Thompson 	    &sc->sc_ucom, &req, NULL, 0, 1000);
39202ac6454SAndrew Thompson }
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson static void
39502ac6454SAndrew Thompson uchcom_ctrl_read(struct uchcom_softc *sc, uint8_t reqno,
39602ac6454SAndrew Thompson     uint16_t value, uint16_t index, void *buf, uint16_t buflen)
39702ac6454SAndrew Thompson {
398760bc48eSAndrew Thompson 	struct usb_device_request req;
39902ac6454SAndrew Thompson 
40002ac6454SAndrew Thompson 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
40102ac6454SAndrew Thompson 	req.bRequest = reqno;
40202ac6454SAndrew Thompson 	USETW(req.wValue, value);
40302ac6454SAndrew Thompson 	USETW(req.wIndex, index);
40402ac6454SAndrew Thompson 	USETW(req.wLength, buflen);
40502ac6454SAndrew Thompson 
40602ac6454SAndrew Thompson 	usb2_com_cfg_do_request(sc->sc_udev,
40702ac6454SAndrew Thompson 	    &sc->sc_ucom, &req, buf, USB_SHORT_XFER_OK, 1000);
40802ac6454SAndrew Thompson }
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson static void
41102ac6454SAndrew Thompson uchcom_write_reg(struct uchcom_softc *sc,
41202ac6454SAndrew Thompson     uint8_t reg1, uint8_t val1, uint8_t reg2, uint8_t val2)
41302ac6454SAndrew Thompson {
41402ac6454SAndrew Thompson 	DPRINTF("0x%02X<-0x%02X, 0x%02X<-0x%02X\n",
41502ac6454SAndrew Thompson 	    (unsigned)reg1, (unsigned)val1,
41602ac6454SAndrew Thompson 	    (unsigned)reg2, (unsigned)val2);
41702ac6454SAndrew Thompson 	uchcom_ctrl_write(
41802ac6454SAndrew Thompson 	    sc, UCHCOM_REQ_WRITE_REG,
41902ac6454SAndrew Thompson 	    reg1 | ((uint16_t)reg2 << 8), val1 | ((uint16_t)val2 << 8));
42002ac6454SAndrew Thompson }
42102ac6454SAndrew Thompson 
42202ac6454SAndrew Thompson static void
42302ac6454SAndrew Thompson uchcom_read_reg(struct uchcom_softc *sc,
42402ac6454SAndrew Thompson     uint8_t reg1, uint8_t *rval1, uint8_t reg2, uint8_t *rval2)
42502ac6454SAndrew Thompson {
42602ac6454SAndrew Thompson 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
42702ac6454SAndrew Thompson 
42802ac6454SAndrew Thompson 	uchcom_ctrl_read(
42902ac6454SAndrew Thompson 	    sc, UCHCOM_REQ_READ_REG,
43002ac6454SAndrew Thompson 	    reg1 | ((uint16_t)reg2 << 8), 0, buf, sizeof(buf));
43102ac6454SAndrew Thompson 
43202ac6454SAndrew Thompson 	DPRINTF("0x%02X->0x%02X, 0x%02X->0x%02X\n",
43302ac6454SAndrew Thompson 	    (unsigned)reg1, (unsigned)buf[0],
43402ac6454SAndrew Thompson 	    (unsigned)reg2, (unsigned)buf[1]);
43502ac6454SAndrew Thompson 
43602ac6454SAndrew Thompson 	if (rval1)
43702ac6454SAndrew Thompson 		*rval1 = buf[0];
43802ac6454SAndrew Thompson 	if (rval2)
43902ac6454SAndrew Thompson 		*rval2 = buf[1];
44002ac6454SAndrew Thompson }
44102ac6454SAndrew Thompson 
44202ac6454SAndrew Thompson static void
44302ac6454SAndrew Thompson uchcom_get_version(struct uchcom_softc *sc, uint8_t *rver)
44402ac6454SAndrew Thompson {
44502ac6454SAndrew Thompson 	uint8_t buf[UCHCOM_INPUT_BUF_SIZE];
44602ac6454SAndrew Thompson 
44702ac6454SAndrew Thompson 	uchcom_ctrl_read(
44802ac6454SAndrew Thompson 	    sc, UCHCOM_REQ_GET_VERSION, 0, 0, buf, sizeof(buf));
44902ac6454SAndrew Thompson 
45002ac6454SAndrew Thompson 	if (rver)
45102ac6454SAndrew Thompson 		*rver = buf[0];
45202ac6454SAndrew Thompson }
45302ac6454SAndrew Thompson 
45402ac6454SAndrew Thompson static void
45502ac6454SAndrew Thompson uchcom_get_status(struct uchcom_softc *sc, uint8_t *rval)
45602ac6454SAndrew Thompson {
45702ac6454SAndrew Thompson 	uchcom_read_reg(sc, UCHCOM_REG_STAT1, rval, UCHCOM_REG_STAT2, NULL);
45802ac6454SAndrew Thompson }
45902ac6454SAndrew Thompson 
46002ac6454SAndrew Thompson static void
46102ac6454SAndrew Thompson uchcom_set_dtrrts_10(struct uchcom_softc *sc, uint8_t val)
46202ac6454SAndrew Thompson {
46302ac6454SAndrew Thompson 	uchcom_write_reg(sc, UCHCOM_REG_STAT1, val, UCHCOM_REG_STAT1, val);
46402ac6454SAndrew Thompson }
46502ac6454SAndrew Thompson 
46602ac6454SAndrew Thompson static void
46702ac6454SAndrew Thompson uchcom_set_dtrrts_20(struct uchcom_softc *sc, uint8_t val)
46802ac6454SAndrew Thompson {
46902ac6454SAndrew Thompson 	uchcom_ctrl_write(sc, UCHCOM_REQ_SET_DTRRTS, val, 0);
47002ac6454SAndrew Thompson }
47102ac6454SAndrew Thompson 
47202ac6454SAndrew Thompson 
47302ac6454SAndrew Thompson /* ----------------------------------------------------------------------
47402ac6454SAndrew Thompson  * middle layer
47502ac6454SAndrew Thompson  */
47602ac6454SAndrew Thompson 
47702ac6454SAndrew Thompson static void
47802ac6454SAndrew Thompson uchcom_update_version(struct uchcom_softc *sc)
47902ac6454SAndrew Thompson {
48002ac6454SAndrew Thompson 	uchcom_get_version(sc, &sc->sc_version);
48102ac6454SAndrew Thompson }
48202ac6454SAndrew Thompson 
48302ac6454SAndrew Thompson static void
48402ac6454SAndrew Thompson uchcom_convert_status(struct uchcom_softc *sc, uint8_t cur)
48502ac6454SAndrew Thompson {
48602ac6454SAndrew Thompson 	sc->sc_dtr = !(cur & UCHCOM_DTR_MASK);
48702ac6454SAndrew Thompson 	sc->sc_rts = !(cur & UCHCOM_RTS_MASK);
48802ac6454SAndrew Thompson 
48902ac6454SAndrew Thompson 	cur = ~cur & 0x0F;
49002ac6454SAndrew Thompson 	sc->sc_msr = (cur << 4) | ((sc->sc_msr >> 4) ^ cur);
49102ac6454SAndrew Thompson }
49202ac6454SAndrew Thompson 
49302ac6454SAndrew Thompson static void
49402ac6454SAndrew Thompson uchcom_update_status(struct uchcom_softc *sc)
49502ac6454SAndrew Thompson {
49602ac6454SAndrew Thompson 	uint8_t cur;
49702ac6454SAndrew Thompson 
49802ac6454SAndrew Thompson 	uchcom_get_status(sc, &cur);
49902ac6454SAndrew Thompson 	uchcom_convert_status(sc, cur);
50002ac6454SAndrew Thompson }
50102ac6454SAndrew Thompson 
50202ac6454SAndrew Thompson 
50302ac6454SAndrew Thompson static void
50402ac6454SAndrew Thompson uchcom_set_dtrrts(struct uchcom_softc *sc)
50502ac6454SAndrew Thompson {
50602ac6454SAndrew Thompson 	uint8_t val = 0;
50702ac6454SAndrew Thompson 
50802ac6454SAndrew Thompson 	if (sc->sc_dtr)
50902ac6454SAndrew Thompson 		val |= UCHCOM_DTR_MASK;
51002ac6454SAndrew Thompson 	if (sc->sc_rts)
51102ac6454SAndrew Thompson 		val |= UCHCOM_RTS_MASK;
51202ac6454SAndrew Thompson 
51302ac6454SAndrew Thompson 	if (sc->sc_version < UCHCOM_VER_20)
51402ac6454SAndrew Thompson 		uchcom_set_dtrrts_10(sc, ~val);
51502ac6454SAndrew Thompson 	else
51602ac6454SAndrew Thompson 		uchcom_set_dtrrts_20(sc, ~val);
51702ac6454SAndrew Thompson }
51802ac6454SAndrew Thompson 
51902ac6454SAndrew Thompson static void
520760bc48eSAndrew Thompson uchcom_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
52102ac6454SAndrew Thompson {
52202ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
52302ac6454SAndrew Thompson 	uint8_t brk1;
52402ac6454SAndrew Thompson 	uint8_t brk2;
52502ac6454SAndrew Thompson 
52602ac6454SAndrew Thompson 	uchcom_read_reg(sc, UCHCOM_REG_BREAK1, &brk1, UCHCOM_REG_BREAK2, &brk2);
52702ac6454SAndrew Thompson 	if (onoff) {
52802ac6454SAndrew Thompson 		/* on - clear bits */
52902ac6454SAndrew Thompson 		brk1 &= ~UCHCOM_BRK1_MASK;
53002ac6454SAndrew Thompson 		brk2 &= ~UCHCOM_BRK2_MASK;
53102ac6454SAndrew Thompson 	} else {
53202ac6454SAndrew Thompson 		/* off - set bits */
53302ac6454SAndrew Thompson 		brk1 |= UCHCOM_BRK1_MASK;
53402ac6454SAndrew Thompson 		brk2 |= UCHCOM_BRK2_MASK;
53502ac6454SAndrew Thompson 	}
53602ac6454SAndrew Thompson 	uchcom_write_reg(sc, UCHCOM_REG_BREAK1, brk1, UCHCOM_REG_BREAK2, brk2);
53702ac6454SAndrew Thompson }
53802ac6454SAndrew Thompson 
53902ac6454SAndrew Thompson static int
54002ac6454SAndrew Thompson uchcom_calc_divider_settings(struct uchcom_divider *dp, uint32_t rate)
54102ac6454SAndrew Thompson {
54202ac6454SAndrew Thompson 	const struct uchcom_divider_record *rp;
54302ac6454SAndrew Thompson 	uint32_t div;
54402ac6454SAndrew Thompson 	uint32_t rem;
54502ac6454SAndrew Thompson 	uint32_t mod;
54602ac6454SAndrew Thompson 	uint8_t i;
54702ac6454SAndrew Thompson 
54802ac6454SAndrew Thompson 	/* find record */
54902ac6454SAndrew Thompson 	for (i = 0; i != NUM_DIVIDERS; i++) {
55002ac6454SAndrew Thompson 		if (dividers[i].dvr_high >= rate &&
55102ac6454SAndrew Thompson 		    dividers[i].dvr_low <= rate) {
55202ac6454SAndrew Thompson 			rp = &dividers[i];
55302ac6454SAndrew Thompson 			goto found;
55402ac6454SAndrew Thompson 		}
55502ac6454SAndrew Thompson 	}
55602ac6454SAndrew Thompson 	return (-1);
55702ac6454SAndrew Thompson 
55802ac6454SAndrew Thompson found:
55902ac6454SAndrew Thompson 	dp->dv_prescaler = rp->dvr_divider.dv_prescaler;
56002ac6454SAndrew Thompson 	if (rp->dvr_base_clock == UCHCOM_BASE_UNKNOWN)
56102ac6454SAndrew Thompson 		dp->dv_div = rp->dvr_divider.dv_div;
56202ac6454SAndrew Thompson 	else {
56302ac6454SAndrew Thompson 		div = rp->dvr_base_clock / rate;
56402ac6454SAndrew Thompson 		rem = rp->dvr_base_clock % rate;
56502ac6454SAndrew Thompson 		if (div == 0 || div >= 0xFF)
56602ac6454SAndrew Thompson 			return (-1);
56702ac6454SAndrew Thompson 		if ((rem << 1) >= rate)
56802ac6454SAndrew Thompson 			div += 1;
56902ac6454SAndrew Thompson 		dp->dv_div = (uint8_t)-div;
57002ac6454SAndrew Thompson 	}
57102ac6454SAndrew Thompson 
57202ac6454SAndrew Thompson 	mod = UCHCOM_BPS_MOD_BASE / rate + UCHCOM_BPS_MOD_BASE_OFS;
57302ac6454SAndrew Thompson 	mod = mod + mod / 2;
57402ac6454SAndrew Thompson 
57502ac6454SAndrew Thompson 	dp->dv_mod = mod / 0x100;
57602ac6454SAndrew Thompson 
57702ac6454SAndrew Thompson 	return (0);
57802ac6454SAndrew Thompson }
57902ac6454SAndrew Thompson 
58002ac6454SAndrew Thompson static void
58102ac6454SAndrew Thompson uchcom_set_dte_rate(struct uchcom_softc *sc, uint32_t rate)
58202ac6454SAndrew Thompson {
58302ac6454SAndrew Thompson 	struct uchcom_divider dv;
58402ac6454SAndrew Thompson 
58502ac6454SAndrew Thompson 	if (uchcom_calc_divider_settings(&dv, rate))
58602ac6454SAndrew Thompson 		return;
58702ac6454SAndrew Thompson 
58802ac6454SAndrew Thompson 	uchcom_write_reg(sc,
58902ac6454SAndrew Thompson 	    UCHCOM_REG_BPS_PRE, dv.dv_prescaler,
59002ac6454SAndrew Thompson 	    UCHCOM_REG_BPS_DIV, dv.dv_div);
59102ac6454SAndrew Thompson 	uchcom_write_reg(sc,
59202ac6454SAndrew Thompson 	    UCHCOM_REG_BPS_MOD, dv.dv_mod,
59302ac6454SAndrew Thompson 	    UCHCOM_REG_BPS_PAD, 0);
59402ac6454SAndrew Thompson }
59502ac6454SAndrew Thompson 
59602ac6454SAndrew Thompson static void
59702ac6454SAndrew Thompson uchcom_set_line_control(struct uchcom_softc *sc, tcflag_t cflag)
59802ac6454SAndrew Thompson {
59902ac6454SAndrew Thompson 	uint8_t lcr1 = 0;
60002ac6454SAndrew Thompson 	uint8_t lcr2 = 0;
60102ac6454SAndrew Thompson 
60202ac6454SAndrew Thompson 	uchcom_read_reg(sc, UCHCOM_REG_LCR1, &lcr1, UCHCOM_REG_LCR2, &lcr2);
60302ac6454SAndrew Thompson 
60402ac6454SAndrew Thompson 	lcr1 &= ~UCHCOM_LCR1_MASK;
60502ac6454SAndrew Thompson 	lcr2 &= ~UCHCOM_LCR2_MASK;
60602ac6454SAndrew Thompson 
60702ac6454SAndrew Thompson 	/*
60802ac6454SAndrew Thompson 	 * XXX: it is difficult to handle the line control appropriately:
60902ac6454SAndrew Thompson 	 *   - CS8, !CSTOPB and any parity mode seems ok, but
61002ac6454SAndrew Thompson 	 *   - the chip doesn't have the function to calculate parity
61102ac6454SAndrew Thompson 	 *     in !CS8 mode.
61202ac6454SAndrew Thompson 	 *   - it is unclear that the chip supports CS5,6 mode.
61302ac6454SAndrew Thompson 	 *   - it is unclear how to handle stop bits.
61402ac6454SAndrew Thompson 	 */
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson 	if (cflag & PARENB) {
61702ac6454SAndrew Thompson 		lcr1 |= UCHCOM_LCR1_PARENB;
61802ac6454SAndrew Thompson 		if (cflag & PARODD)
61902ac6454SAndrew Thompson 			lcr2 |= UCHCOM_LCR2_PARODD;
62002ac6454SAndrew Thompson 		else
62102ac6454SAndrew Thompson 			lcr2 |= UCHCOM_LCR2_PAREVEN;
62202ac6454SAndrew Thompson 	}
62302ac6454SAndrew Thompson 	uchcom_write_reg(sc, UCHCOM_REG_LCR1, lcr1, UCHCOM_REG_LCR2, lcr2);
62402ac6454SAndrew Thompson }
62502ac6454SAndrew Thompson 
62602ac6454SAndrew Thompson static void
62702ac6454SAndrew Thompson uchcom_clear_chip(struct uchcom_softc *sc)
62802ac6454SAndrew Thompson {
62902ac6454SAndrew Thompson 	DPRINTF("\n");
63002ac6454SAndrew Thompson 	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, 0, 0);
63102ac6454SAndrew Thompson }
63202ac6454SAndrew Thompson 
63302ac6454SAndrew Thompson static void
63402ac6454SAndrew Thompson uchcom_reset_chip(struct uchcom_softc *sc)
63502ac6454SAndrew Thompson {
63602ac6454SAndrew Thompson 	uint16_t val;
63702ac6454SAndrew Thompson 	uint16_t idx;
63802ac6454SAndrew Thompson 	uint8_t lcr1;
63902ac6454SAndrew Thompson 	uint8_t lcr2;
64002ac6454SAndrew Thompson 	uint8_t pre;
64102ac6454SAndrew Thompson 	uint8_t div;
64202ac6454SAndrew Thompson 	uint8_t mod;
64302ac6454SAndrew Thompson 
64402ac6454SAndrew Thompson 	uchcom_read_reg(sc, UCHCOM_REG_LCR1, &lcr1, UCHCOM_REG_LCR2, &lcr2);
64502ac6454SAndrew Thompson 	uchcom_read_reg(sc, UCHCOM_REG_BPS_PRE, &pre, UCHCOM_REG_BPS_DIV, &div);
64602ac6454SAndrew Thompson 	uchcom_read_reg(sc, UCHCOM_REG_BPS_MOD, &mod, UCHCOM_REG_BPS_PAD, NULL);
64702ac6454SAndrew Thompson 
64802ac6454SAndrew Thompson 	val = 0;
64902ac6454SAndrew Thompson 	idx = 0;
65002ac6454SAndrew Thompson 	val |= (uint16_t)(lcr1 & 0xF0) << 8;
65102ac6454SAndrew Thompson 	val |= 0x01;
65202ac6454SAndrew Thompson 	val |= (uint16_t)(lcr2 & 0x0F) << 8;
65302ac6454SAndrew Thompson 	val |= 0x02;
65402ac6454SAndrew Thompson 	idx |= pre & 0x07;
65502ac6454SAndrew Thompson 	val |= 0x04;
65602ac6454SAndrew Thompson 	idx |= (uint16_t)div << 8;
65702ac6454SAndrew Thompson 	val |= 0x08;
65802ac6454SAndrew Thompson 	idx |= mod & 0xF8;
65902ac6454SAndrew Thompson 	val |= 0x10;
66002ac6454SAndrew Thompson 
66102ac6454SAndrew Thompson 	DPRINTF("reset v=0x%04X, i=0x%04X\n", val, idx);
66202ac6454SAndrew Thompson 
66302ac6454SAndrew Thompson 	uchcom_ctrl_write(sc, UCHCOM_REQ_RESET, val, idx);
66402ac6454SAndrew Thompson }
66502ac6454SAndrew Thompson 
66602ac6454SAndrew Thompson /* ----------------------------------------------------------------------
66702ac6454SAndrew Thompson  * methods for ucom
66802ac6454SAndrew Thompson  */
66902ac6454SAndrew Thompson static void
670760bc48eSAndrew Thompson uchcom_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
67102ac6454SAndrew Thompson {
67202ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
67302ac6454SAndrew Thompson 
67402ac6454SAndrew Thompson 	DPRINTF("\n");
67502ac6454SAndrew Thompson 
67602ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
67702ac6454SAndrew Thompson 	*msr = sc->sc_msr;
67802ac6454SAndrew Thompson }
67902ac6454SAndrew Thompson 
68002ac6454SAndrew Thompson static void
681760bc48eSAndrew Thompson uchcom_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
68202ac6454SAndrew Thompson {
68302ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
68402ac6454SAndrew Thompson 
68502ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
68602ac6454SAndrew Thompson 
68702ac6454SAndrew Thompson 	sc->sc_dtr = onoff;
68802ac6454SAndrew Thompson 	uchcom_set_dtrrts(sc);
68902ac6454SAndrew Thompson }
69002ac6454SAndrew Thompson 
69102ac6454SAndrew Thompson static void
692760bc48eSAndrew Thompson uchcom_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
69302ac6454SAndrew Thompson {
69402ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
69502ac6454SAndrew Thompson 
69602ac6454SAndrew Thompson 	DPRINTF("onoff = %d\n", onoff);
69702ac6454SAndrew Thompson 
69802ac6454SAndrew Thompson 	sc->sc_rts = onoff;
69902ac6454SAndrew Thompson 	uchcom_set_dtrrts(sc);
70002ac6454SAndrew Thompson }
70102ac6454SAndrew Thompson 
70202ac6454SAndrew Thompson static int
703760bc48eSAndrew Thompson uchcom_pre_param(struct ucom_softc *ucom, struct termios *t)
70402ac6454SAndrew Thompson {
70502ac6454SAndrew Thompson 	struct uchcom_divider dv;
70602ac6454SAndrew Thompson 
70702ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
70802ac6454SAndrew Thompson 	case CS5:
70902ac6454SAndrew Thompson 	case CS6:
71002ac6454SAndrew Thompson 	case CS7:
71102ac6454SAndrew Thompson 		return (EIO);
71202ac6454SAndrew Thompson 	default:
71302ac6454SAndrew Thompson 		break;
71402ac6454SAndrew Thompson 	}
71502ac6454SAndrew Thompson 
71602ac6454SAndrew Thompson 	if (uchcom_calc_divider_settings(&dv, t->c_ospeed)) {
71702ac6454SAndrew Thompson 		return (EIO);
71802ac6454SAndrew Thompson 	}
71902ac6454SAndrew Thompson 	return (0);			/* success */
72002ac6454SAndrew Thompson }
72102ac6454SAndrew Thompson 
72202ac6454SAndrew Thompson static void
723760bc48eSAndrew Thompson uchcom_cfg_param(struct ucom_softc *ucom, struct termios *t)
72402ac6454SAndrew Thompson {
72502ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
72602ac6454SAndrew Thompson 
72702ac6454SAndrew Thompson 	uchcom_set_line_control(sc, t->c_cflag);
72802ac6454SAndrew Thompson 	uchcom_set_dte_rate(sc, t->c_ospeed);
72902ac6454SAndrew Thompson }
73002ac6454SAndrew Thompson 
73102ac6454SAndrew Thompson static void
732760bc48eSAndrew Thompson uchcom_start_read(struct ucom_softc *ucom)
73302ac6454SAndrew Thompson {
73402ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
73502ac6454SAndrew Thompson 
73602ac6454SAndrew Thompson 	/* start interrupt endpoint */
73702ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
73802ac6454SAndrew Thompson 
73902ac6454SAndrew Thompson 	/* start read endpoint */
74002ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
74102ac6454SAndrew Thompson }
74202ac6454SAndrew Thompson 
74302ac6454SAndrew Thompson static void
744760bc48eSAndrew Thompson uchcom_stop_read(struct ucom_softc *ucom)
74502ac6454SAndrew Thompson {
74602ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
74702ac6454SAndrew Thompson 
74802ac6454SAndrew Thompson 	/* stop interrupt endpoint */
74902ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[UCHCOM_INTR_DT_RD]);
75002ac6454SAndrew Thompson 
75102ac6454SAndrew Thompson 	/* stop read endpoint */
75202ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_RD]);
75302ac6454SAndrew Thompson }
75402ac6454SAndrew Thompson 
75502ac6454SAndrew Thompson static void
756760bc48eSAndrew Thompson uchcom_start_write(struct ucom_softc *ucom)
75702ac6454SAndrew Thompson {
75802ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
75902ac6454SAndrew Thompson 
76002ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
76102ac6454SAndrew Thompson }
76202ac6454SAndrew Thompson 
76302ac6454SAndrew Thompson static void
764760bc48eSAndrew Thompson uchcom_stop_write(struct ucom_softc *ucom)
76502ac6454SAndrew Thompson {
76602ac6454SAndrew Thompson 	struct uchcom_softc *sc = ucom->sc_parent;
76702ac6454SAndrew Thompson 
76802ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[UCHCOM_BULK_DT_WR]);
76902ac6454SAndrew Thompson }
77002ac6454SAndrew Thompson 
77102ac6454SAndrew Thompson /* ----------------------------------------------------------------------
77202ac6454SAndrew Thompson  * callback when the modem status is changed.
77302ac6454SAndrew Thompson  */
77402ac6454SAndrew Thompson static void
775760bc48eSAndrew Thompson uchcom_intr_callback(struct usb_xfer *xfer)
77602ac6454SAndrew Thompson {
77702ac6454SAndrew Thompson 	struct uchcom_softc *sc = xfer->priv_sc;
77802ac6454SAndrew Thompson 	uint8_t buf[UCHCOM_INTR_LEAST];
77902ac6454SAndrew Thompson 
78002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
78102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
78202ac6454SAndrew Thompson 
78302ac6454SAndrew Thompson 		DPRINTF("actlen = %u\n", xfer->actlen);
78402ac6454SAndrew Thompson 
78502ac6454SAndrew Thompson 		if (xfer->actlen >= UCHCOM_INTR_LEAST) {
78602ac6454SAndrew Thompson 			usb2_copy_out(xfer->frbuffers, 0, buf,
78702ac6454SAndrew Thompson 			    UCHCOM_INTR_LEAST);
78802ac6454SAndrew Thompson 
78902ac6454SAndrew Thompson 			DPRINTF("data = 0x%02X 0x%02X 0x%02X 0x%02X\n",
79002ac6454SAndrew Thompson 			    (unsigned)buf[0], (unsigned)buf[1],
79102ac6454SAndrew Thompson 			    (unsigned)buf[2], (unsigned)buf[3]);
79202ac6454SAndrew Thompson 
79302ac6454SAndrew Thompson 			uchcom_convert_status(sc, buf[UCHCOM_INTR_STAT1]);
79402ac6454SAndrew Thompson 			usb2_com_status_change(&sc->sc_ucom);
79502ac6454SAndrew Thompson 		}
79602ac6454SAndrew Thompson 	case USB_ST_SETUP:
79702ac6454SAndrew Thompson tr_setup:
79802ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
79902ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
80002ac6454SAndrew Thompson 		break;
80102ac6454SAndrew Thompson 
80202ac6454SAndrew Thompson 	default:			/* Error */
80302ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
80402ac6454SAndrew Thompson 			/* try to clear stall first */
80502ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
80602ac6454SAndrew Thompson 			goto tr_setup;
80702ac6454SAndrew Thompson 		}
80802ac6454SAndrew Thompson 		break;
80902ac6454SAndrew Thompson 	}
81002ac6454SAndrew Thompson }
81102ac6454SAndrew Thompson 
81202ac6454SAndrew Thompson static void
813760bc48eSAndrew Thompson uchcom_write_callback(struct usb_xfer *xfer)
81402ac6454SAndrew Thompson {
81502ac6454SAndrew Thompson 	struct uchcom_softc *sc = xfer->priv_sc;
81602ac6454SAndrew Thompson 	uint32_t actlen;
81702ac6454SAndrew Thompson 
81802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
81902ac6454SAndrew Thompson 	case USB_ST_SETUP:
82002ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
82102ac6454SAndrew Thompson tr_setup:
82202ac6454SAndrew Thompson 		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
82302ac6454SAndrew Thompson 		    UCHCOM_BULK_BUF_SIZE, &actlen)) {
82402ac6454SAndrew Thompson 
82502ac6454SAndrew Thompson 			DPRINTF("actlen = %d\n", actlen);
82602ac6454SAndrew Thompson 
82702ac6454SAndrew Thompson 			xfer->frlengths[0] = actlen;
82802ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
82902ac6454SAndrew Thompson 		}
83002ac6454SAndrew Thompson 		return;
83102ac6454SAndrew Thompson 
83202ac6454SAndrew Thompson 	default:			/* Error */
83302ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
83402ac6454SAndrew Thompson 			/* try to clear stall first */
83502ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
83602ac6454SAndrew Thompson 			goto tr_setup;
83702ac6454SAndrew Thompson 		}
83802ac6454SAndrew Thompson 		return;
83902ac6454SAndrew Thompson 
84002ac6454SAndrew Thompson 	}
84102ac6454SAndrew Thompson }
84202ac6454SAndrew Thompson 
84302ac6454SAndrew Thompson static void
844760bc48eSAndrew Thompson uchcom_read_callback(struct usb_xfer *xfer)
84502ac6454SAndrew Thompson {
84602ac6454SAndrew Thompson 	struct uchcom_softc *sc = xfer->priv_sc;
84702ac6454SAndrew Thompson 
84802ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
84902ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
85002ac6454SAndrew Thompson 		usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen);
85102ac6454SAndrew Thompson 
85202ac6454SAndrew Thompson 	case USB_ST_SETUP:
85302ac6454SAndrew Thompson tr_setup:
85402ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
85502ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
85602ac6454SAndrew Thompson 		return;
85702ac6454SAndrew Thompson 
85802ac6454SAndrew Thompson 	default:			/* Error */
85902ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
86002ac6454SAndrew Thompson 			/* try to clear stall first */
86102ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
86202ac6454SAndrew Thompson 			goto tr_setup;
86302ac6454SAndrew Thompson 		}
86402ac6454SAndrew Thompson 		return;
86502ac6454SAndrew Thompson 	}
86602ac6454SAndrew Thompson }
86702ac6454SAndrew Thompson 
86802ac6454SAndrew Thompson static device_method_t uchcom_methods[] = {
86902ac6454SAndrew Thompson 	/* Device interface */
87002ac6454SAndrew Thompson 	DEVMETHOD(device_probe, uchcom_probe),
87102ac6454SAndrew Thompson 	DEVMETHOD(device_attach, uchcom_attach),
87202ac6454SAndrew Thompson 	DEVMETHOD(device_detach, uchcom_detach),
87302ac6454SAndrew Thompson 
87402ac6454SAndrew Thompson 	{0, 0}
87502ac6454SAndrew Thompson };
87602ac6454SAndrew Thompson 
87702ac6454SAndrew Thompson static driver_t uchcom_driver = {
87802ac6454SAndrew Thompson 	"ucom",
87902ac6454SAndrew Thompson 	uchcom_methods,
88002ac6454SAndrew Thompson 	sizeof(struct uchcom_softc)
88102ac6454SAndrew Thompson };
88202ac6454SAndrew Thompson 
88302ac6454SAndrew Thompson static devclass_t uchcom_devclass;
88402ac6454SAndrew Thompson 
8859aef556dSAndrew Thompson DRIVER_MODULE(uchcom, uhub, uchcom_driver, uchcom_devclass, NULL, 0);
88602ac6454SAndrew Thompson MODULE_DEPEND(uchcom, ucom, 1, 1, 1);
88702ac6454SAndrew Thompson MODULE_DEPEND(uchcom, usb, 1, 1, 1);
888