xref: /freebsd/sys/dev/usb/serial/umct.c (revision 5d38a4d4f7a511cc431d45ea7d7692d44f9fae00)
102ac6454SAndrew Thompson #include <sys/cdefs.h>
202ac6454SAndrew Thompson __FBSDID("$FreeBSD$");
302ac6454SAndrew Thompson 
402ac6454SAndrew Thompson /*-
502ac6454SAndrew Thompson  * Copyright (c) 2003 Scott Long
602ac6454SAndrew Thompson  * All rights reserved.
702ac6454SAndrew Thompson  *
802ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
902ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1002ac6454SAndrew Thompson  * are met:
1102ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1202ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1302ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1402ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1502ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1602ac6454SAndrew Thompson  *
1702ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1802ac6454SAndrew Thompson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1902ac6454SAndrew Thompson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2002ac6454SAndrew Thompson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2102ac6454SAndrew Thompson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2202ac6454SAndrew Thompson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2302ac6454SAndrew Thompson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2402ac6454SAndrew Thompson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2502ac6454SAndrew Thompson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2602ac6454SAndrew Thompson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2702ac6454SAndrew Thompson  * SUCH DAMAGE.
2802ac6454SAndrew Thompson  *
2902ac6454SAndrew Thompson  */
3002ac6454SAndrew Thompson 
3102ac6454SAndrew Thompson /*
3202ac6454SAndrew Thompson  * Driver for the MCT (Magic Control Technology) USB-RS232 Converter.
3302ac6454SAndrew Thompson  * Based on the superb documentation from the linux mct_u232 driver by
3402ac6454SAndrew Thompson  * Wolfgang Grandeggar <wolfgang@cec.ch>.
3502ac6454SAndrew Thompson  * This device smells a lot like the Belkin F5U103, except that it has
3602ac6454SAndrew Thompson  * suffered some mild brain-damage.  This driver is based off of the ubsa.c
375d38a4d4SEd Schouten  * driver from Alexander Kabaev <kan@FreeBSD.org>.  Merging the two together
3802ac6454SAndrew Thompson  * might be useful, though the subtle differences might lead to lots of
3902ac6454SAndrew Thompson  * #ifdef's.
4002ac6454SAndrew Thompson  */
4102ac6454SAndrew Thompson 
4202ac6454SAndrew Thompson /*
4302ac6454SAndrew Thompson  * NOTE: all function names beginning like "umct_cfg_" can only
4402ac6454SAndrew Thompson  * be called from within the config thread function !
4502ac6454SAndrew Thompson  */
4602ac6454SAndrew Thompson 
4702ac6454SAndrew Thompson #include "usbdevs.h"
4802ac6454SAndrew Thompson #include <dev/usb/usb.h>
4902ac6454SAndrew Thompson #include <dev/usb/usb_mfunc.h>
5002ac6454SAndrew Thompson #include <dev/usb/usb_error.h>
5102ac6454SAndrew Thompson #include <dev/usb/usb_cdc.h>
5202ac6454SAndrew Thompson #include <dev/usb/usb_defs.h>
5302ac6454SAndrew Thompson 
5402ac6454SAndrew Thompson #define	USB_DEBUG_VAR usb2_debug
5502ac6454SAndrew Thompson 
5602ac6454SAndrew Thompson #include <dev/usb/usb_core.h>
5702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
5802ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
5902ac6454SAndrew Thompson #include <dev/usb/usb_request.h>
6002ac6454SAndrew Thompson #include <dev/usb/usb_lookup.h>
6102ac6454SAndrew Thompson #include <dev/usb/usb_util.h>
6202ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h>
6302ac6454SAndrew Thompson #include <dev/usb/usb_device.h>
6402ac6454SAndrew Thompson 
6502ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
6602ac6454SAndrew Thompson 
6702ac6454SAndrew Thompson /* The UMCT advertises the standard 8250 UART registers */
6802ac6454SAndrew Thompson #define	UMCT_GET_MSR		2	/* Get Modem Status Register */
6902ac6454SAndrew Thompson #define	UMCT_GET_MSR_SIZE	1
7002ac6454SAndrew Thompson #define	UMCT_GET_LCR		6	/* Get Line Control Register */
7102ac6454SAndrew Thompson #define	UMCT_GET_LCR_SIZE	1
7202ac6454SAndrew Thompson #define	UMCT_SET_BAUD		5	/* Set the Baud Rate Divisor */
7302ac6454SAndrew Thompson #define	UMCT_SET_BAUD_SIZE	4
7402ac6454SAndrew Thompson #define	UMCT_SET_LCR		7	/* Set Line Control Register */
7502ac6454SAndrew Thompson #define	UMCT_SET_LCR_SIZE	1
7602ac6454SAndrew Thompson #define	UMCT_SET_MCR		10	/* Set Modem Control Register */
7702ac6454SAndrew Thompson #define	UMCT_SET_MCR_SIZE	1
7802ac6454SAndrew Thompson 
7902ac6454SAndrew Thompson #define	UMCT_INTR_INTERVAL	100
8002ac6454SAndrew Thompson #define	UMCT_IFACE_INDEX	0
8102ac6454SAndrew Thompson #define	UMCT_CONFIG_INDEX	1
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson enum {
8402ac6454SAndrew Thompson 	UMCT_BULK_DT_WR,
8502ac6454SAndrew Thompson 	UMCT_BULK_DT_RD,
8602ac6454SAndrew Thompson 	UMCT_INTR_DT_RD,
8702ac6454SAndrew Thompson 	UMCT_N_TRANSFER,
8802ac6454SAndrew Thompson };
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson struct umct_softc {
9102ac6454SAndrew Thompson 	struct usb2_com_super_softc sc_super_ucom;
9202ac6454SAndrew Thompson 	struct usb2_com_softc sc_ucom;
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson 	struct usb2_device *sc_udev;
9502ac6454SAndrew Thompson 	struct usb2_xfer *sc_xfer[UMCT_N_TRANSFER];
9602ac6454SAndrew Thompson 
9702ac6454SAndrew Thompson 	uint32_t sc_unit;
9802ac6454SAndrew Thompson 
9902ac6454SAndrew Thompson 	uint16_t sc_obufsize;
10002ac6454SAndrew Thompson 
10102ac6454SAndrew Thompson 	uint8_t	sc_lsr;
10202ac6454SAndrew Thompson 	uint8_t	sc_msr;
10302ac6454SAndrew Thompson 	uint8_t	sc_lcr;
10402ac6454SAndrew Thompson 	uint8_t	sc_mcr;
10502ac6454SAndrew Thompson 	uint8_t	sc_iface_no;
10602ac6454SAndrew Thompson 	uint8_t	sc_name[16];
10702ac6454SAndrew Thompson };
10802ac6454SAndrew Thompson 
10902ac6454SAndrew Thompson /* prototypes */
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson static device_probe_t umct_probe;
11202ac6454SAndrew Thompson static device_attach_t umct_attach;
11302ac6454SAndrew Thompson static device_detach_t umct_detach;
11402ac6454SAndrew Thompson 
11502ac6454SAndrew Thompson static usb2_callback_t umct_intr_callback;
11602ac6454SAndrew Thompson static usb2_callback_t umct_write_callback;
11702ac6454SAndrew Thompson static usb2_callback_t umct_read_callback;
11802ac6454SAndrew Thompson 
11902ac6454SAndrew Thompson static void	umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
12002ac6454SAndrew Thompson 		    uint16_t len, uint32_t value);
12102ac6454SAndrew Thompson static void	umct_cfg_get_status(struct usb2_com_softc *, uint8_t *,
12202ac6454SAndrew Thompson 		    uint8_t *);
12302ac6454SAndrew Thompson static void	umct_cfg_set_break(struct usb2_com_softc *, uint8_t);
12402ac6454SAndrew Thompson static void	umct_cfg_set_dtr(struct usb2_com_softc *, uint8_t);
12502ac6454SAndrew Thompson static void	umct_cfg_set_rts(struct usb2_com_softc *, uint8_t);
12602ac6454SAndrew Thompson static uint8_t	umct_calc_baud(uint32_t);
12702ac6454SAndrew Thompson static int	umct_pre_param(struct usb2_com_softc *, struct termios *);
12802ac6454SAndrew Thompson static void	umct_cfg_param(struct usb2_com_softc *, struct termios *);
12902ac6454SAndrew Thompson static void	umct_start_read(struct usb2_com_softc *);
13002ac6454SAndrew Thompson static void	umct_stop_read(struct usb2_com_softc *);
13102ac6454SAndrew Thompson static void	umct_start_write(struct usb2_com_softc *);
13202ac6454SAndrew Thompson static void	umct_stop_write(struct usb2_com_softc *);
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson static const struct usb2_config umct_config[UMCT_N_TRANSFER] = {
13502ac6454SAndrew Thompson 
13602ac6454SAndrew Thompson 	[UMCT_BULK_DT_WR] = {
13702ac6454SAndrew Thompson 		.type = UE_BULK,
13802ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
13902ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
14002ac6454SAndrew Thompson 		.mh.bufsize = 0,	/* use wMaxPacketSize */
14102ac6454SAndrew Thompson 		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
14202ac6454SAndrew Thompson 		.mh.callback = &umct_write_callback,
14302ac6454SAndrew Thompson 	},
14402ac6454SAndrew Thompson 
14502ac6454SAndrew Thompson 	[UMCT_BULK_DT_RD] = {
14602ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
14702ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
14802ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
14902ac6454SAndrew Thompson 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
15002ac6454SAndrew Thompson 		.mh.bufsize = 0,	/* use wMaxPacketSize */
15102ac6454SAndrew Thompson 		.mh.callback = &umct_read_callback,
15202ac6454SAndrew Thompson 		.ep_index = 0,		/* first interrupt endpoint */
15302ac6454SAndrew Thompson 	},
15402ac6454SAndrew Thompson 
15502ac6454SAndrew Thompson 	[UMCT_INTR_DT_RD] = {
15602ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
15702ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
15802ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
15902ac6454SAndrew Thompson 		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
16002ac6454SAndrew Thompson 		.mh.bufsize = 0,	/* use wMaxPacketSize */
16102ac6454SAndrew Thompson 		.mh.callback = &umct_intr_callback,
16202ac6454SAndrew Thompson 		.ep_index = 1,		/* second interrupt endpoint */
16302ac6454SAndrew Thompson 	},
16402ac6454SAndrew Thompson };
16502ac6454SAndrew Thompson 
16602ac6454SAndrew Thompson static const struct usb2_com_callback umct_callback = {
16702ac6454SAndrew Thompson 	.usb2_com_cfg_get_status = &umct_cfg_get_status,
16802ac6454SAndrew Thompson 	.usb2_com_cfg_set_dtr = &umct_cfg_set_dtr,
16902ac6454SAndrew Thompson 	.usb2_com_cfg_set_rts = &umct_cfg_set_rts,
17002ac6454SAndrew Thompson 	.usb2_com_cfg_set_break = &umct_cfg_set_break,
17102ac6454SAndrew Thompson 	.usb2_com_cfg_param = &umct_cfg_param,
17202ac6454SAndrew Thompson 	.usb2_com_pre_param = &umct_pre_param,
17302ac6454SAndrew Thompson 	.usb2_com_start_read = &umct_start_read,
17402ac6454SAndrew Thompson 	.usb2_com_stop_read = &umct_stop_read,
17502ac6454SAndrew Thompson 	.usb2_com_start_write = &umct_start_write,
17602ac6454SAndrew Thompson 	.usb2_com_stop_write = &umct_stop_write,
17702ac6454SAndrew Thompson };
17802ac6454SAndrew Thompson 
17902ac6454SAndrew Thompson static const struct usb2_device_id umct_devs[] = {
18002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
18102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
18202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
18302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
18402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
18502ac6454SAndrew Thompson };
18602ac6454SAndrew Thompson 
18702ac6454SAndrew Thompson static device_method_t umct_methods[] = {
18802ac6454SAndrew Thompson 	DEVMETHOD(device_probe, umct_probe),
18902ac6454SAndrew Thompson 	DEVMETHOD(device_attach, umct_attach),
19002ac6454SAndrew Thompson 	DEVMETHOD(device_detach, umct_detach),
19102ac6454SAndrew Thompson 	{0, 0}
19202ac6454SAndrew Thompson };
19302ac6454SAndrew Thompson 
19402ac6454SAndrew Thompson static devclass_t umct_devclass;
19502ac6454SAndrew Thompson 
19602ac6454SAndrew Thompson static driver_t umct_driver = {
19702ac6454SAndrew Thompson 	.name = "umct",
19802ac6454SAndrew Thompson 	.methods = umct_methods,
19902ac6454SAndrew Thompson 	.size = sizeof(struct umct_softc),
20002ac6454SAndrew Thompson };
20102ac6454SAndrew Thompson 
20202ac6454SAndrew Thompson DRIVER_MODULE(umct, ushub, umct_driver, umct_devclass, NULL, 0);
20302ac6454SAndrew Thompson MODULE_DEPEND(umct, ucom, 1, 1, 1);
20402ac6454SAndrew Thompson MODULE_DEPEND(umct, usb, 1, 1, 1);
20502ac6454SAndrew Thompson 
20602ac6454SAndrew Thompson static int
20702ac6454SAndrew Thompson umct_probe(device_t dev)
20802ac6454SAndrew Thompson {
20902ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson 	if (uaa->usb2_mode != USB_MODE_HOST) {
21202ac6454SAndrew Thompson 		return (ENXIO);
21302ac6454SAndrew Thompson 	}
21402ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) {
21502ac6454SAndrew Thompson 		return (ENXIO);
21602ac6454SAndrew Thompson 	}
21702ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
21802ac6454SAndrew Thompson 		return (ENXIO);
21902ac6454SAndrew Thompson 	}
22002ac6454SAndrew Thompson 	return (usb2_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
22102ac6454SAndrew Thompson }
22202ac6454SAndrew Thompson 
22302ac6454SAndrew Thompson static int
22402ac6454SAndrew Thompson umct_attach(device_t dev)
22502ac6454SAndrew Thompson {
22602ac6454SAndrew Thompson 	struct usb2_attach_arg *uaa = device_get_ivars(dev);
22702ac6454SAndrew Thompson 	struct umct_softc *sc = device_get_softc(dev);
22802ac6454SAndrew Thompson 	int32_t error;
22902ac6454SAndrew Thompson 	uint16_t maxp;
23002ac6454SAndrew Thompson 	uint8_t iface_index;
23102ac6454SAndrew Thompson 
23202ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
23302ac6454SAndrew Thompson 	sc->sc_unit = device_get_unit(dev);
23402ac6454SAndrew Thompson 
23502ac6454SAndrew Thompson 	device_set_usb2_desc(dev);
23602ac6454SAndrew Thompson 
23702ac6454SAndrew Thompson 	snprintf(sc->sc_name, sizeof(sc->sc_name),
23802ac6454SAndrew Thompson 	    "%s", device_get_nameunit(dev));
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson 	sc->sc_iface_no = uaa->info.bIfaceNum;
24102ac6454SAndrew Thompson 
24202ac6454SAndrew Thompson 	iface_index = UMCT_IFACE_INDEX;
24302ac6454SAndrew Thompson 	error = usb2_transfer_setup(uaa->device, &iface_index,
24402ac6454SAndrew Thompson 	    sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &Giant);
24502ac6454SAndrew Thompson 
24602ac6454SAndrew Thompson 	if (error) {
24702ac6454SAndrew Thompson 		device_printf(dev, "allocating USB "
24802ac6454SAndrew Thompson 		    "transfers failed!\n");
24902ac6454SAndrew Thompson 		goto detach;
25002ac6454SAndrew Thompson 	}
25102ac6454SAndrew Thompson 	/*
25202ac6454SAndrew Thompson 	 * The real bulk-in endpoint is also marked as an interrupt.
25302ac6454SAndrew Thompson 	 * The only way to differentiate it from the real interrupt
25402ac6454SAndrew Thompson 	 * endpoint is to look at the wMaxPacketSize field.
25502ac6454SAndrew Thompson 	 */
25602ac6454SAndrew Thompson 	maxp = UGETW(sc->sc_xfer[UMCT_BULK_DT_RD]->pipe->edesc->wMaxPacketSize);
25702ac6454SAndrew Thompson 	if (maxp == 0x2) {
25802ac6454SAndrew Thompson 
25902ac6454SAndrew Thompson 		/* guessed wrong - switch around endpoints */
26002ac6454SAndrew Thompson 
26102ac6454SAndrew Thompson 		struct usb2_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
26202ac6454SAndrew Thompson 
26302ac6454SAndrew Thompson 		sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
26402ac6454SAndrew Thompson 		sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
26502ac6454SAndrew Thompson 
26602ac6454SAndrew Thompson 		sc->sc_xfer[UMCT_BULK_DT_RD]->callback = &umct_read_callback;
26702ac6454SAndrew Thompson 		sc->sc_xfer[UMCT_INTR_DT_RD]->callback = &umct_intr_callback;
26802ac6454SAndrew Thompson 	}
26902ac6454SAndrew Thompson 	sc->sc_obufsize = sc->sc_xfer[UMCT_BULK_DT_WR]->max_data_length;
27002ac6454SAndrew Thompson 
27102ac6454SAndrew Thompson 	if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
27202ac6454SAndrew Thompson 		if (sc->sc_obufsize > 16) {
27302ac6454SAndrew Thompson 			sc->sc_obufsize = 16;
27402ac6454SAndrew Thompson 		}
27502ac6454SAndrew Thompson 	}
27602ac6454SAndrew Thompson 	error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
27702ac6454SAndrew Thompson 	    &umct_callback, &Giant);
27802ac6454SAndrew Thompson 	if (error) {
27902ac6454SAndrew Thompson 		goto detach;
28002ac6454SAndrew Thompson 	}
28102ac6454SAndrew Thompson 	return (0);			/* success */
28202ac6454SAndrew Thompson 
28302ac6454SAndrew Thompson detach:
28402ac6454SAndrew Thompson 	umct_detach(dev);
28502ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
28602ac6454SAndrew Thompson }
28702ac6454SAndrew Thompson 
28802ac6454SAndrew Thompson static int
28902ac6454SAndrew Thompson umct_detach(device_t dev)
29002ac6454SAndrew Thompson {
29102ac6454SAndrew Thompson 	struct umct_softc *sc = device_get_softc(dev);
29202ac6454SAndrew Thompson 
29302ac6454SAndrew Thompson 	usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
29402ac6454SAndrew Thompson 
29502ac6454SAndrew Thompson 	usb2_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
29602ac6454SAndrew Thompson 
29702ac6454SAndrew Thompson 	return (0);
29802ac6454SAndrew Thompson }
29902ac6454SAndrew Thompson 
30002ac6454SAndrew Thompson static void
30102ac6454SAndrew Thompson umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
30202ac6454SAndrew Thompson     uint16_t len, uint32_t value)
30302ac6454SAndrew Thompson {
30402ac6454SAndrew Thompson 	struct usb2_device_request req;
30502ac6454SAndrew Thompson 	usb2_error_t err;
30602ac6454SAndrew Thompson 	uint8_t temp[4];
30702ac6454SAndrew Thompson 
30802ac6454SAndrew Thompson 	if (len > 4)
30902ac6454SAndrew Thompson 		len = 4;
31002ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
31102ac6454SAndrew Thompson 	req.bRequest = request;
31202ac6454SAndrew Thompson 	USETW(req.wValue, 0);
31302ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_iface_no;
31402ac6454SAndrew Thompson 	req.wIndex[1] = 0;
31502ac6454SAndrew Thompson 	USETW(req.wLength, len);
31602ac6454SAndrew Thompson 	USETDW(temp, value);
31702ac6454SAndrew Thompson 
31802ac6454SAndrew Thompson 	err = usb2_com_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
31902ac6454SAndrew Thompson 	    &req, temp, 0, 1000);
32002ac6454SAndrew Thompson 	if (err) {
32102ac6454SAndrew Thompson 		DPRINTFN(0, "device request failed, err=%s "
32202ac6454SAndrew Thompson 		    "(ignored)\n", usb2_errstr(err));
32302ac6454SAndrew Thompson 	}
32402ac6454SAndrew Thompson 	return;
32502ac6454SAndrew Thompson }
32602ac6454SAndrew Thompson 
32702ac6454SAndrew Thompson static void
32802ac6454SAndrew Thompson umct_intr_callback(struct usb2_xfer *xfer)
32902ac6454SAndrew Thompson {
33002ac6454SAndrew Thompson 	struct umct_softc *sc = xfer->priv_sc;
33102ac6454SAndrew Thompson 	uint8_t buf[2];
33202ac6454SAndrew Thompson 
33302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
33402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
33502ac6454SAndrew Thompson 		if (xfer->actlen < 2) {
33602ac6454SAndrew Thompson 			DPRINTF("too short message\n");
33702ac6454SAndrew Thompson 			goto tr_setup;
33802ac6454SAndrew Thompson 		}
33902ac6454SAndrew Thompson 		usb2_copy_out(xfer->frbuffers, 0, buf, sizeof(buf));
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 		sc->sc_msr = buf[0];
34202ac6454SAndrew Thompson 		sc->sc_lsr = buf[1];
34302ac6454SAndrew Thompson 
34402ac6454SAndrew Thompson 		usb2_com_status_change(&sc->sc_ucom);
34502ac6454SAndrew Thompson 
34602ac6454SAndrew Thompson 	case USB_ST_SETUP:
34702ac6454SAndrew Thompson tr_setup:
34802ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
34902ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
35002ac6454SAndrew Thompson 		return;
35102ac6454SAndrew Thompson 
35202ac6454SAndrew Thompson 	default:			/* Error */
35302ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
35402ac6454SAndrew Thompson 			/* try to clear stall first */
35502ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
35602ac6454SAndrew Thompson 			goto tr_setup;
35702ac6454SAndrew Thompson 		}
35802ac6454SAndrew Thompson 		return;
35902ac6454SAndrew Thompson 	}
36002ac6454SAndrew Thompson }
36102ac6454SAndrew Thompson 
36202ac6454SAndrew Thompson static void
36302ac6454SAndrew Thompson umct_cfg_get_status(struct usb2_com_softc *ucom, uint8_t *lsr, uint8_t *msr)
36402ac6454SAndrew Thompson {
36502ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
36602ac6454SAndrew Thompson 
36702ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
36802ac6454SAndrew Thompson 	*msr = sc->sc_msr;
36902ac6454SAndrew Thompson }
37002ac6454SAndrew Thompson 
37102ac6454SAndrew Thompson static void
37202ac6454SAndrew Thompson umct_cfg_set_break(struct usb2_com_softc *ucom, uint8_t onoff)
37302ac6454SAndrew Thompson {
37402ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
37502ac6454SAndrew Thompson 
37602ac6454SAndrew Thompson 	if (onoff)
37702ac6454SAndrew Thompson 		sc->sc_lcr |= 0x40;
37802ac6454SAndrew Thompson 	else
37902ac6454SAndrew Thompson 		sc->sc_lcr &= ~0x40;
38002ac6454SAndrew Thompson 
38102ac6454SAndrew Thompson 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
38202ac6454SAndrew Thompson }
38302ac6454SAndrew Thompson 
38402ac6454SAndrew Thompson static void
38502ac6454SAndrew Thompson umct_cfg_set_dtr(struct usb2_com_softc *ucom, uint8_t onoff)
38602ac6454SAndrew Thompson {
38702ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
38802ac6454SAndrew Thompson 
38902ac6454SAndrew Thompson 	if (onoff)
39002ac6454SAndrew Thompson 		sc->sc_mcr |= 0x01;
39102ac6454SAndrew Thompson 	else
39202ac6454SAndrew Thompson 		sc->sc_mcr &= ~0x01;
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
39502ac6454SAndrew Thompson }
39602ac6454SAndrew Thompson 
39702ac6454SAndrew Thompson static void
39802ac6454SAndrew Thompson umct_cfg_set_rts(struct usb2_com_softc *ucom, uint8_t onoff)
39902ac6454SAndrew Thompson {
40002ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
40102ac6454SAndrew Thompson 
40202ac6454SAndrew Thompson 	if (onoff)
40302ac6454SAndrew Thompson 		sc->sc_mcr |= 0x02;
40402ac6454SAndrew Thompson 	else
40502ac6454SAndrew Thompson 		sc->sc_mcr &= ~0x02;
40602ac6454SAndrew Thompson 
40702ac6454SAndrew Thompson 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
40802ac6454SAndrew Thompson }
40902ac6454SAndrew Thompson 
41002ac6454SAndrew Thompson static uint8_t
41102ac6454SAndrew Thompson umct_calc_baud(uint32_t baud)
41202ac6454SAndrew Thompson {
41302ac6454SAndrew Thompson 	switch (baud) {
41402ac6454SAndrew Thompson 		case B300:return (0x1);
41502ac6454SAndrew Thompson 	case B600:
41602ac6454SAndrew Thompson 		return (0x2);
41702ac6454SAndrew Thompson 	case B1200:
41802ac6454SAndrew Thompson 		return (0x3);
41902ac6454SAndrew Thompson 	case B2400:
42002ac6454SAndrew Thompson 		return (0x4);
42102ac6454SAndrew Thompson 	case B4800:
42202ac6454SAndrew Thompson 		return (0x6);
42302ac6454SAndrew Thompson 	case B9600:
42402ac6454SAndrew Thompson 		return (0x8);
42502ac6454SAndrew Thompson 	case B19200:
42602ac6454SAndrew Thompson 		return (0x9);
42702ac6454SAndrew Thompson 	case B38400:
42802ac6454SAndrew Thompson 		return (0xa);
42902ac6454SAndrew Thompson 	case B57600:
43002ac6454SAndrew Thompson 		return (0xb);
43102ac6454SAndrew Thompson 	case 115200:
43202ac6454SAndrew Thompson 		return (0xc);
43302ac6454SAndrew Thompson 	case B0:
43402ac6454SAndrew Thompson 	default:
43502ac6454SAndrew Thompson 		break;
43602ac6454SAndrew Thompson 	}
43702ac6454SAndrew Thompson 	return (0x0);
43802ac6454SAndrew Thompson }
43902ac6454SAndrew Thompson 
44002ac6454SAndrew Thompson static int
44102ac6454SAndrew Thompson umct_pre_param(struct usb2_com_softc *ucom, struct termios *t)
44202ac6454SAndrew Thompson {
44302ac6454SAndrew Thompson 	return (0);			/* we accept anything */
44402ac6454SAndrew Thompson }
44502ac6454SAndrew Thompson 
44602ac6454SAndrew Thompson static void
44702ac6454SAndrew Thompson umct_cfg_param(struct usb2_com_softc *ucom, struct termios *t)
44802ac6454SAndrew Thompson {
44902ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
45002ac6454SAndrew Thompson 	uint32_t value;
45102ac6454SAndrew Thompson 
45202ac6454SAndrew Thompson 	value = umct_calc_baud(t->c_ospeed);
45302ac6454SAndrew Thompson 	umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
45402ac6454SAndrew Thompson 
45502ac6454SAndrew Thompson 	value = (sc->sc_lcr & 0x40);
45602ac6454SAndrew Thompson 
45702ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
45802ac6454SAndrew Thompson 	case CS5:
45902ac6454SAndrew Thompson 		value |= 0x0;
46002ac6454SAndrew Thompson 		break;
46102ac6454SAndrew Thompson 	case CS6:
46202ac6454SAndrew Thompson 		value |= 0x1;
46302ac6454SAndrew Thompson 		break;
46402ac6454SAndrew Thompson 	case CS7:
46502ac6454SAndrew Thompson 		value |= 0x2;
46602ac6454SAndrew Thompson 		break;
46702ac6454SAndrew Thompson 	default:
46802ac6454SAndrew Thompson 	case CS8:
46902ac6454SAndrew Thompson 		value |= 0x3;
47002ac6454SAndrew Thompson 		break;
47102ac6454SAndrew Thompson 	}
47202ac6454SAndrew Thompson 
47302ac6454SAndrew Thompson 	value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
47402ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
47502ac6454SAndrew Thompson 		value |= 0x8;
47602ac6454SAndrew Thompson 		value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
47702ac6454SAndrew Thompson 	}
47802ac6454SAndrew Thompson 	/*
47902ac6454SAndrew Thompson 	 * XXX There doesn't seem to be a way to tell the device
48002ac6454SAndrew Thompson 	 * to use flow control.
48102ac6454SAndrew Thompson 	 */
48202ac6454SAndrew Thompson 
48302ac6454SAndrew Thompson 	sc->sc_lcr = value;
48402ac6454SAndrew Thompson 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
48502ac6454SAndrew Thompson }
48602ac6454SAndrew Thompson 
48702ac6454SAndrew Thompson static void
48802ac6454SAndrew Thompson umct_start_read(struct usb2_com_softc *ucom)
48902ac6454SAndrew Thompson {
49002ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
49102ac6454SAndrew Thompson 
49202ac6454SAndrew Thompson 	/* start interrupt endpoint */
49302ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
49402ac6454SAndrew Thompson 
49502ac6454SAndrew Thompson 	/* start read endpoint */
49602ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
49702ac6454SAndrew Thompson }
49802ac6454SAndrew Thompson 
49902ac6454SAndrew Thompson static void
50002ac6454SAndrew Thompson umct_stop_read(struct usb2_com_softc *ucom)
50102ac6454SAndrew Thompson {
50202ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
50302ac6454SAndrew Thompson 
50402ac6454SAndrew Thompson 	/* stop interrupt endpoint */
50502ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
50602ac6454SAndrew Thompson 
50702ac6454SAndrew Thompson 	/* stop read endpoint */
50802ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
50902ac6454SAndrew Thompson }
51002ac6454SAndrew Thompson 
51102ac6454SAndrew Thompson static void
51202ac6454SAndrew Thompson umct_start_write(struct usb2_com_softc *ucom)
51302ac6454SAndrew Thompson {
51402ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
51502ac6454SAndrew Thompson 
51602ac6454SAndrew Thompson 	usb2_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
51702ac6454SAndrew Thompson }
51802ac6454SAndrew Thompson 
51902ac6454SAndrew Thompson static void
52002ac6454SAndrew Thompson umct_stop_write(struct usb2_com_softc *ucom)
52102ac6454SAndrew Thompson {
52202ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
52302ac6454SAndrew Thompson 
52402ac6454SAndrew Thompson 	usb2_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
52502ac6454SAndrew Thompson }
52602ac6454SAndrew Thompson 
52702ac6454SAndrew Thompson static void
52802ac6454SAndrew Thompson umct_write_callback(struct usb2_xfer *xfer)
52902ac6454SAndrew Thompson {
53002ac6454SAndrew Thompson 	struct umct_softc *sc = xfer->priv_sc;
53102ac6454SAndrew Thompson 	uint32_t actlen;
53202ac6454SAndrew Thompson 
53302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
53402ac6454SAndrew Thompson 	case USB_ST_SETUP:
53502ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
53602ac6454SAndrew Thompson tr_setup:
53702ac6454SAndrew Thompson 		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
53802ac6454SAndrew Thompson 		    sc->sc_obufsize, &actlen)) {
53902ac6454SAndrew Thompson 
54002ac6454SAndrew Thompson 			xfer->frlengths[0] = actlen;
54102ac6454SAndrew Thompson 			usb2_start_hardware(xfer);
54202ac6454SAndrew Thompson 		}
54302ac6454SAndrew Thompson 		return;
54402ac6454SAndrew Thompson 
54502ac6454SAndrew Thompson 	default:			/* Error */
54602ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
54702ac6454SAndrew Thompson 			/* try to clear stall first */
54802ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
54902ac6454SAndrew Thompson 			goto tr_setup;
55002ac6454SAndrew Thompson 		}
55102ac6454SAndrew Thompson 		return;
55202ac6454SAndrew Thompson 	}
55302ac6454SAndrew Thompson }
55402ac6454SAndrew Thompson 
55502ac6454SAndrew Thompson static void
55602ac6454SAndrew Thompson umct_read_callback(struct usb2_xfer *xfer)
55702ac6454SAndrew Thompson {
55802ac6454SAndrew Thompson 	struct umct_softc *sc = xfer->priv_sc;
55902ac6454SAndrew Thompson 
56002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
56102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
56202ac6454SAndrew Thompson 		usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers,
56302ac6454SAndrew Thompson 		    0, xfer->actlen);
56402ac6454SAndrew Thompson 
56502ac6454SAndrew Thompson 	case USB_ST_SETUP:
56602ac6454SAndrew Thompson tr_setup:
56702ac6454SAndrew Thompson 		xfer->frlengths[0] = xfer->max_data_length;
56802ac6454SAndrew Thompson 		usb2_start_hardware(xfer);
56902ac6454SAndrew Thompson 		return;
57002ac6454SAndrew Thompson 
57102ac6454SAndrew Thompson 	default:			/* Error */
57202ac6454SAndrew Thompson 		if (xfer->error != USB_ERR_CANCELLED) {
57302ac6454SAndrew Thompson 			/* try to clear stall first */
57402ac6454SAndrew Thompson 			xfer->flags.stall_pipe = 1;
57502ac6454SAndrew Thompson 			goto tr_setup;
57602ac6454SAndrew Thompson 		}
57702ac6454SAndrew Thompson 		return;
57802ac6454SAndrew Thompson 	}
57902ac6454SAndrew Thompson }
580