xref: /freebsd/sys/dev/usb/serial/umct.c (revision 767cb2e29d4183d9978a5973b5092c6e0079eb6b)
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 
47ed6d949aSAndrew Thompson #include <sys/stdint.h>
48ed6d949aSAndrew Thompson #include <sys/stddef.h>
49ed6d949aSAndrew Thompson #include <sys/param.h>
50ed6d949aSAndrew Thompson #include <sys/queue.h>
51ed6d949aSAndrew Thompson #include <sys/types.h>
52ed6d949aSAndrew Thompson #include <sys/systm.h>
53ed6d949aSAndrew Thompson #include <sys/kernel.h>
54ed6d949aSAndrew Thompson #include <sys/bus.h>
55ed6d949aSAndrew Thompson #include <sys/linker_set.h>
56ed6d949aSAndrew Thompson #include <sys/module.h>
57ed6d949aSAndrew Thompson #include <sys/lock.h>
58ed6d949aSAndrew Thompson #include <sys/mutex.h>
59ed6d949aSAndrew Thompson #include <sys/condvar.h>
60ed6d949aSAndrew Thompson #include <sys/sysctl.h>
61ed6d949aSAndrew Thompson #include <sys/sx.h>
62ed6d949aSAndrew Thompson #include <sys/unistd.h>
63ed6d949aSAndrew Thompson #include <sys/callout.h>
64ed6d949aSAndrew Thompson #include <sys/malloc.h>
65ed6d949aSAndrew Thompson #include <sys/priv.h>
66ed6d949aSAndrew Thompson 
6702ac6454SAndrew Thompson #include <dev/usb/usb.h>
68ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
69ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h>
70ed6d949aSAndrew Thompson #include "usbdevs.h"
7102ac6454SAndrew Thompson 
72a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_debug
7302ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
7402ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
7502ac6454SAndrew Thompson 
7602ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
7702ac6454SAndrew Thompson 
7802ac6454SAndrew Thompson /* The UMCT advertises the standard 8250 UART registers */
7902ac6454SAndrew Thompson #define	UMCT_GET_MSR		2	/* Get Modem Status Register */
8002ac6454SAndrew Thompson #define	UMCT_GET_MSR_SIZE	1
8102ac6454SAndrew Thompson #define	UMCT_GET_LCR		6	/* Get Line Control Register */
8202ac6454SAndrew Thompson #define	UMCT_GET_LCR_SIZE	1
8302ac6454SAndrew Thompson #define	UMCT_SET_BAUD		5	/* Set the Baud Rate Divisor */
8402ac6454SAndrew Thompson #define	UMCT_SET_BAUD_SIZE	4
8502ac6454SAndrew Thompson #define	UMCT_SET_LCR		7	/* Set Line Control Register */
8602ac6454SAndrew Thompson #define	UMCT_SET_LCR_SIZE	1
8702ac6454SAndrew Thompson #define	UMCT_SET_MCR		10	/* Set Modem Control Register */
8802ac6454SAndrew Thompson #define	UMCT_SET_MCR_SIZE	1
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson #define	UMCT_INTR_INTERVAL	100
9102ac6454SAndrew Thompson #define	UMCT_IFACE_INDEX	0
92774651eeSAndrew Thompson #define	UMCT_CONFIG_INDEX	0
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson enum {
9502ac6454SAndrew Thompson 	UMCT_BULK_DT_WR,
9602ac6454SAndrew Thompson 	UMCT_BULK_DT_RD,
9702ac6454SAndrew Thompson 	UMCT_INTR_DT_RD,
9802ac6454SAndrew Thompson 	UMCT_N_TRANSFER,
9902ac6454SAndrew Thompson };
10002ac6454SAndrew Thompson 
10102ac6454SAndrew Thompson struct umct_softc {
102760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
103760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom;
10402ac6454SAndrew Thompson 
105760bc48eSAndrew Thompson 	struct usb_device *sc_udev;
106760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UMCT_N_TRANSFER];
107deefe583SAndrew Thompson 	struct mtx sc_mtx;
10802ac6454SAndrew Thompson 
10902ac6454SAndrew Thompson 	uint32_t sc_unit;
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson 	uint16_t sc_obufsize;
11202ac6454SAndrew Thompson 
11302ac6454SAndrew Thompson 	uint8_t	sc_lsr;
11402ac6454SAndrew Thompson 	uint8_t	sc_msr;
11502ac6454SAndrew Thompson 	uint8_t	sc_lcr;
11602ac6454SAndrew Thompson 	uint8_t	sc_mcr;
11702ac6454SAndrew Thompson 	uint8_t	sc_iface_no;
118585b13f2SAndrew Thompson 	uint8_t sc_swap_cb;
11902ac6454SAndrew Thompson 	uint8_t	sc_name[16];
12002ac6454SAndrew Thompson };
12102ac6454SAndrew Thompson 
12202ac6454SAndrew Thompson /* prototypes */
12302ac6454SAndrew Thompson 
12402ac6454SAndrew Thompson static device_probe_t umct_probe;
12502ac6454SAndrew Thompson static device_attach_t umct_attach;
12602ac6454SAndrew Thompson static device_detach_t umct_detach;
12702ac6454SAndrew Thompson 
128e0a69b51SAndrew Thompson static usb_callback_t umct_intr_callback;
129585b13f2SAndrew Thompson static usb_callback_t umct_intr_callback_sub;
130e0a69b51SAndrew Thompson static usb_callback_t umct_read_callback;
131585b13f2SAndrew Thompson static usb_callback_t umct_read_callback_sub;
132585b13f2SAndrew Thompson static usb_callback_t umct_write_callback;
13302ac6454SAndrew Thompson 
13402ac6454SAndrew Thompson static void	umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
13502ac6454SAndrew Thompson 		    uint16_t len, uint32_t value);
136760bc48eSAndrew Thompson static void	umct_cfg_get_status(struct ucom_softc *, uint8_t *,
13702ac6454SAndrew Thompson 		    uint8_t *);
138760bc48eSAndrew Thompson static void	umct_cfg_set_break(struct ucom_softc *, uint8_t);
139760bc48eSAndrew Thompson static void	umct_cfg_set_dtr(struct ucom_softc *, uint8_t);
140760bc48eSAndrew Thompson static void	umct_cfg_set_rts(struct ucom_softc *, uint8_t);
14102ac6454SAndrew Thompson static uint8_t	umct_calc_baud(uint32_t);
142760bc48eSAndrew Thompson static int	umct_pre_param(struct ucom_softc *, struct termios *);
143760bc48eSAndrew Thompson static void	umct_cfg_param(struct ucom_softc *, struct termios *);
144760bc48eSAndrew Thompson static void	umct_start_read(struct ucom_softc *);
145760bc48eSAndrew Thompson static void	umct_stop_read(struct ucom_softc *);
146760bc48eSAndrew Thompson static void	umct_start_write(struct ucom_softc *);
147760bc48eSAndrew Thompson static void	umct_stop_write(struct ucom_softc *);
148655dc9d0SAndrew Thompson static void	umct_poll(struct ucom_softc *ucom);
14902ac6454SAndrew Thompson 
150760bc48eSAndrew Thompson static const struct usb_config umct_config[UMCT_N_TRANSFER] = {
15102ac6454SAndrew Thompson 
15202ac6454SAndrew Thompson 	[UMCT_BULK_DT_WR] = {
15302ac6454SAndrew Thompson 		.type = UE_BULK,
15402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
15502ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
1564eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
1574eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
1584eae601eSAndrew Thompson 		.callback = &umct_write_callback,
15902ac6454SAndrew Thompson 	},
16002ac6454SAndrew Thompson 
16102ac6454SAndrew Thompson 	[UMCT_BULK_DT_RD] = {
16202ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
16302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
16402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
1654eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1664eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
1674eae601eSAndrew Thompson 		.callback = &umct_read_callback,
16802ac6454SAndrew Thompson 		.ep_index = 0,		/* first interrupt endpoint */
16902ac6454SAndrew Thompson 	},
17002ac6454SAndrew Thompson 
17102ac6454SAndrew Thompson 	[UMCT_INTR_DT_RD] = {
17202ac6454SAndrew Thompson 		.type = UE_INTERRUPT,
17302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
17402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
1754eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1764eae601eSAndrew Thompson 		.bufsize = 0,	/* use wMaxPacketSize */
1774eae601eSAndrew Thompson 		.callback = &umct_intr_callback,
17802ac6454SAndrew Thompson 		.ep_index = 1,		/* second interrupt endpoint */
17902ac6454SAndrew Thompson 	},
18002ac6454SAndrew Thompson };
18102ac6454SAndrew Thompson 
182760bc48eSAndrew Thompson static const struct ucom_callback umct_callback = {
183a593f6b8SAndrew Thompson 	.ucom_cfg_get_status = &umct_cfg_get_status,
184a593f6b8SAndrew Thompson 	.ucom_cfg_set_dtr = &umct_cfg_set_dtr,
185a593f6b8SAndrew Thompson 	.ucom_cfg_set_rts = &umct_cfg_set_rts,
186a593f6b8SAndrew Thompson 	.ucom_cfg_set_break = &umct_cfg_set_break,
187a593f6b8SAndrew Thompson 	.ucom_cfg_param = &umct_cfg_param,
188a593f6b8SAndrew Thompson 	.ucom_pre_param = &umct_pre_param,
189a593f6b8SAndrew Thompson 	.ucom_start_read = &umct_start_read,
190a593f6b8SAndrew Thompson 	.ucom_stop_read = &umct_stop_read,
191a593f6b8SAndrew Thompson 	.ucom_start_write = &umct_start_write,
192a593f6b8SAndrew Thompson 	.ucom_stop_write = &umct_stop_write,
193655dc9d0SAndrew Thompson 	.ucom_poll = &umct_poll,
19402ac6454SAndrew Thompson };
19502ac6454SAndrew Thompson 
196760bc48eSAndrew Thompson static const struct usb_device_id umct_devs[] = {
19702ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0)},
19802ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232, 0)},
19902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232, 0)},
20002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U109, 0)},
20102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U409, 0)},
20202ac6454SAndrew Thompson };
20302ac6454SAndrew Thompson 
20402ac6454SAndrew Thompson static device_method_t umct_methods[] = {
20502ac6454SAndrew Thompson 	DEVMETHOD(device_probe, umct_probe),
20602ac6454SAndrew Thompson 	DEVMETHOD(device_attach, umct_attach),
20702ac6454SAndrew Thompson 	DEVMETHOD(device_detach, umct_detach),
20802ac6454SAndrew Thompson 	{0, 0}
20902ac6454SAndrew Thompson };
21002ac6454SAndrew Thompson 
21102ac6454SAndrew Thompson static devclass_t umct_devclass;
21202ac6454SAndrew Thompson 
21302ac6454SAndrew Thompson static driver_t umct_driver = {
21402ac6454SAndrew Thompson 	.name = "umct",
21502ac6454SAndrew Thompson 	.methods = umct_methods,
21602ac6454SAndrew Thompson 	.size = sizeof(struct umct_softc),
21702ac6454SAndrew Thompson };
21802ac6454SAndrew Thompson 
2199aef556dSAndrew Thompson DRIVER_MODULE(umct, uhub, umct_driver, umct_devclass, NULL, 0);
22002ac6454SAndrew Thompson MODULE_DEPEND(umct, ucom, 1, 1, 1);
22102ac6454SAndrew Thompson MODULE_DEPEND(umct, usb, 1, 1, 1);
22202ac6454SAndrew Thompson 
22302ac6454SAndrew Thompson static int
22402ac6454SAndrew Thompson umct_probe(device_t dev)
22502ac6454SAndrew Thompson {
226760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
22702ac6454SAndrew Thompson 
228f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
22902ac6454SAndrew Thompson 		return (ENXIO);
23002ac6454SAndrew Thompson 	}
23102ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UMCT_CONFIG_INDEX) {
23202ac6454SAndrew Thompson 		return (ENXIO);
23302ac6454SAndrew Thompson 	}
23402ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != UMCT_IFACE_INDEX) {
23502ac6454SAndrew Thompson 		return (ENXIO);
23602ac6454SAndrew Thompson 	}
237a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(umct_devs, sizeof(umct_devs), uaa));
23802ac6454SAndrew Thompson }
23902ac6454SAndrew Thompson 
24002ac6454SAndrew Thompson static int
24102ac6454SAndrew Thompson umct_attach(device_t dev)
24202ac6454SAndrew Thompson {
243760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
24402ac6454SAndrew Thompson 	struct umct_softc *sc = device_get_softc(dev);
24502ac6454SAndrew Thompson 	int32_t error;
246585b13f2SAndrew Thompson 	uint16_t maxp;
24702ac6454SAndrew Thompson 	uint8_t iface_index;
24802ac6454SAndrew Thompson 
24902ac6454SAndrew Thompson 	sc->sc_udev = uaa->device;
25002ac6454SAndrew Thompson 	sc->sc_unit = device_get_unit(dev);
25102ac6454SAndrew Thompson 
252a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
253deefe583SAndrew Thompson 	mtx_init(&sc->sc_mtx, "umct", NULL, MTX_DEF);
25402ac6454SAndrew Thompson 
25502ac6454SAndrew Thompson 	snprintf(sc->sc_name, sizeof(sc->sc_name),
25602ac6454SAndrew Thompson 	    "%s", device_get_nameunit(dev));
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson 	sc->sc_iface_no = uaa->info.bIfaceNum;
25902ac6454SAndrew Thompson 
26002ac6454SAndrew Thompson 	iface_index = UMCT_IFACE_INDEX;
261a593f6b8SAndrew Thompson 	error = usbd_transfer_setup(uaa->device, &iface_index,
262deefe583SAndrew Thompson 	    sc->sc_xfer, umct_config, UMCT_N_TRANSFER, sc, &sc->sc_mtx);
26302ac6454SAndrew Thompson 
26402ac6454SAndrew Thompson 	if (error) {
26502ac6454SAndrew Thompson 		device_printf(dev, "allocating USB "
266767cb2e2SAndrew Thompson 		    "transfers failed\n");
26702ac6454SAndrew Thompson 		goto detach;
26802ac6454SAndrew Thompson 	}
269585b13f2SAndrew Thompson 
27002ac6454SAndrew Thompson 	/*
27102ac6454SAndrew Thompson 	 * The real bulk-in endpoint is also marked as an interrupt.
27202ac6454SAndrew Thompson 	 * The only way to differentiate it from the real interrupt
27302ac6454SAndrew Thompson 	 * endpoint is to look at the wMaxPacketSize field.
27402ac6454SAndrew Thompson 	 */
275585b13f2SAndrew Thompson 	maxp = usbd_xfer_max_framelen(sc->sc_xfer[UMCT_BULK_DT_RD]);
27602ac6454SAndrew Thompson 	if (maxp == 0x2) {
27702ac6454SAndrew Thompson 
27802ac6454SAndrew Thompson 		/* guessed wrong - switch around endpoints */
27902ac6454SAndrew Thompson 
280760bc48eSAndrew Thompson 		struct usb_xfer *temp = sc->sc_xfer[UMCT_INTR_DT_RD];
28102ac6454SAndrew Thompson 
28202ac6454SAndrew Thompson 		sc->sc_xfer[UMCT_INTR_DT_RD] = sc->sc_xfer[UMCT_BULK_DT_RD];
28302ac6454SAndrew Thompson 		sc->sc_xfer[UMCT_BULK_DT_RD] = temp;
284585b13f2SAndrew Thompson 		sc->sc_swap_cb = 1;
28502ac6454SAndrew Thompson 	}
286585b13f2SAndrew Thompson 
287ed6d949aSAndrew Thompson 	sc->sc_obufsize = usbd_xfer_max_len(sc->sc_xfer[UMCT_BULK_DT_WR]);
28802ac6454SAndrew Thompson 
28902ac6454SAndrew Thompson 	if (uaa->info.idProduct == USB_PRODUCT_MCT_SITECOM_USB232) {
29002ac6454SAndrew Thompson 		if (sc->sc_obufsize > 16) {
29102ac6454SAndrew Thompson 			sc->sc_obufsize = 16;
29202ac6454SAndrew Thompson 		}
29302ac6454SAndrew Thompson 	}
294a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
295deefe583SAndrew Thompson 	    &umct_callback, &sc->sc_mtx);
29602ac6454SAndrew Thompson 	if (error) {
29702ac6454SAndrew Thompson 		goto detach;
29802ac6454SAndrew Thompson 	}
29902ac6454SAndrew Thompson 	return (0);			/* success */
30002ac6454SAndrew Thompson 
30102ac6454SAndrew Thompson detach:
30202ac6454SAndrew Thompson 	umct_detach(dev);
30302ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
30402ac6454SAndrew Thompson }
30502ac6454SAndrew Thompson 
30602ac6454SAndrew Thompson static int
30702ac6454SAndrew Thompson umct_detach(device_t dev)
30802ac6454SAndrew Thompson {
30902ac6454SAndrew Thompson 	struct umct_softc *sc = device_get_softc(dev);
31002ac6454SAndrew Thompson 
311a593f6b8SAndrew Thompson 	ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
312a593f6b8SAndrew Thompson 	usbd_transfer_unsetup(sc->sc_xfer, UMCT_N_TRANSFER);
313deefe583SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
31402ac6454SAndrew Thompson 
31502ac6454SAndrew Thompson 	return (0);
31602ac6454SAndrew Thompson }
31702ac6454SAndrew Thompson 
31802ac6454SAndrew Thompson static void
31902ac6454SAndrew Thompson umct_cfg_do_request(struct umct_softc *sc, uint8_t request,
32002ac6454SAndrew Thompson     uint16_t len, uint32_t value)
32102ac6454SAndrew Thompson {
322760bc48eSAndrew Thompson 	struct usb_device_request req;
323e0a69b51SAndrew Thompson 	usb_error_t err;
32402ac6454SAndrew Thompson 	uint8_t temp[4];
32502ac6454SAndrew Thompson 
32602ac6454SAndrew Thompson 	if (len > 4)
32702ac6454SAndrew Thompson 		len = 4;
32802ac6454SAndrew Thompson 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
32902ac6454SAndrew Thompson 	req.bRequest = request;
33002ac6454SAndrew Thompson 	USETW(req.wValue, 0);
33102ac6454SAndrew Thompson 	req.wIndex[0] = sc->sc_iface_no;
33202ac6454SAndrew Thompson 	req.wIndex[1] = 0;
33302ac6454SAndrew Thompson 	USETW(req.wLength, len);
33402ac6454SAndrew Thompson 	USETDW(temp, value);
33502ac6454SAndrew Thompson 
336a593f6b8SAndrew Thompson 	err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
33702ac6454SAndrew Thompson 	    &req, temp, 0, 1000);
33802ac6454SAndrew Thompson 	if (err) {
33902ac6454SAndrew Thompson 		DPRINTFN(0, "device request failed, err=%s "
340a593f6b8SAndrew Thompson 		    "(ignored)\n", usbd_errstr(err));
34102ac6454SAndrew Thompson 	}
34202ac6454SAndrew Thompson 	return;
34302ac6454SAndrew Thompson }
34402ac6454SAndrew Thompson 
34502ac6454SAndrew Thompson static void
346585b13f2SAndrew Thompson umct_intr_callback_sub(struct usb_xfer *xfer, usb_error_t error)
34702ac6454SAndrew Thompson {
348ed6d949aSAndrew Thompson 	struct umct_softc *sc = usbd_xfer_softc(xfer);
349ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
35002ac6454SAndrew Thompson 	uint8_t buf[2];
351ed6d949aSAndrew Thompson 	int actlen;
352ed6d949aSAndrew Thompson 
353ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
35402ac6454SAndrew Thompson 
35502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
35602ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
357ed6d949aSAndrew Thompson 		if (actlen < 2) {
35802ac6454SAndrew Thompson 			DPRINTF("too short message\n");
35902ac6454SAndrew Thompson 			goto tr_setup;
36002ac6454SAndrew Thompson 		}
361ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
362ed6d949aSAndrew Thompson 		usbd_copy_out(pc, 0, buf, sizeof(buf));
36302ac6454SAndrew Thompson 
36402ac6454SAndrew Thompson 		sc->sc_msr = buf[0];
36502ac6454SAndrew Thompson 		sc->sc_lsr = buf[1];
36602ac6454SAndrew Thompson 
367a593f6b8SAndrew Thompson 		ucom_status_change(&sc->sc_ucom);
36802ac6454SAndrew Thompson 
36902ac6454SAndrew Thompson 	case USB_ST_SETUP:
37002ac6454SAndrew Thompson tr_setup:
371ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
372a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
37302ac6454SAndrew Thompson 		return;
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson 	default:			/* Error */
376ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
37702ac6454SAndrew Thompson 			/* try to clear stall first */
378ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
37902ac6454SAndrew Thompson 			goto tr_setup;
38002ac6454SAndrew Thompson 		}
38102ac6454SAndrew Thompson 		return;
38202ac6454SAndrew Thompson 	}
38302ac6454SAndrew Thompson }
38402ac6454SAndrew Thompson 
38502ac6454SAndrew Thompson static void
386760bc48eSAndrew Thompson umct_cfg_get_status(struct ucom_softc *ucom, uint8_t *lsr, uint8_t *msr)
38702ac6454SAndrew Thompson {
38802ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
38902ac6454SAndrew Thompson 
39002ac6454SAndrew Thompson 	*lsr = sc->sc_lsr;
39102ac6454SAndrew Thompson 	*msr = sc->sc_msr;
39202ac6454SAndrew Thompson }
39302ac6454SAndrew Thompson 
39402ac6454SAndrew Thompson static void
395760bc48eSAndrew Thompson umct_cfg_set_break(struct ucom_softc *ucom, uint8_t onoff)
39602ac6454SAndrew Thompson {
39702ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
39802ac6454SAndrew Thompson 
39902ac6454SAndrew Thompson 	if (onoff)
40002ac6454SAndrew Thompson 		sc->sc_lcr |= 0x40;
40102ac6454SAndrew Thompson 	else
40202ac6454SAndrew Thompson 		sc->sc_lcr &= ~0x40;
40302ac6454SAndrew Thompson 
40402ac6454SAndrew Thompson 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, sc->sc_lcr);
40502ac6454SAndrew Thompson }
40602ac6454SAndrew Thompson 
40702ac6454SAndrew Thompson static void
408760bc48eSAndrew Thompson umct_cfg_set_dtr(struct ucom_softc *ucom, uint8_t onoff)
40902ac6454SAndrew Thompson {
41002ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
41102ac6454SAndrew Thompson 
41202ac6454SAndrew Thompson 	if (onoff)
41302ac6454SAndrew Thompson 		sc->sc_mcr |= 0x01;
41402ac6454SAndrew Thompson 	else
41502ac6454SAndrew Thompson 		sc->sc_mcr &= ~0x01;
41602ac6454SAndrew Thompson 
41702ac6454SAndrew Thompson 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
41802ac6454SAndrew Thompson }
41902ac6454SAndrew Thompson 
42002ac6454SAndrew Thompson static void
421760bc48eSAndrew Thompson umct_cfg_set_rts(struct ucom_softc *ucom, uint8_t onoff)
42202ac6454SAndrew Thompson {
42302ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
42402ac6454SAndrew Thompson 
42502ac6454SAndrew Thompson 	if (onoff)
42602ac6454SAndrew Thompson 		sc->sc_mcr |= 0x02;
42702ac6454SAndrew Thompson 	else
42802ac6454SAndrew Thompson 		sc->sc_mcr &= ~0x02;
42902ac6454SAndrew Thompson 
43002ac6454SAndrew Thompson 	umct_cfg_do_request(sc, UMCT_SET_MCR, UMCT_SET_MCR_SIZE, sc->sc_mcr);
43102ac6454SAndrew Thompson }
43202ac6454SAndrew Thompson 
43302ac6454SAndrew Thompson static uint8_t
43402ac6454SAndrew Thompson umct_calc_baud(uint32_t baud)
43502ac6454SAndrew Thompson {
43602ac6454SAndrew Thompson 	switch (baud) {
43702ac6454SAndrew Thompson 		case B300:return (0x1);
43802ac6454SAndrew Thompson 	case B600:
43902ac6454SAndrew Thompson 		return (0x2);
44002ac6454SAndrew Thompson 	case B1200:
44102ac6454SAndrew Thompson 		return (0x3);
44202ac6454SAndrew Thompson 	case B2400:
44302ac6454SAndrew Thompson 		return (0x4);
44402ac6454SAndrew Thompson 	case B4800:
44502ac6454SAndrew Thompson 		return (0x6);
44602ac6454SAndrew Thompson 	case B9600:
44702ac6454SAndrew Thompson 		return (0x8);
44802ac6454SAndrew Thompson 	case B19200:
44902ac6454SAndrew Thompson 		return (0x9);
45002ac6454SAndrew Thompson 	case B38400:
45102ac6454SAndrew Thompson 		return (0xa);
45202ac6454SAndrew Thompson 	case B57600:
45302ac6454SAndrew Thompson 		return (0xb);
45402ac6454SAndrew Thompson 	case 115200:
45502ac6454SAndrew Thompson 		return (0xc);
45602ac6454SAndrew Thompson 	case B0:
45702ac6454SAndrew Thompson 	default:
45802ac6454SAndrew Thompson 		break;
45902ac6454SAndrew Thompson 	}
46002ac6454SAndrew Thompson 	return (0x0);
46102ac6454SAndrew Thompson }
46202ac6454SAndrew Thompson 
46302ac6454SAndrew Thompson static int
464760bc48eSAndrew Thompson umct_pre_param(struct ucom_softc *ucom, struct termios *t)
46502ac6454SAndrew Thompson {
46602ac6454SAndrew Thompson 	return (0);			/* we accept anything */
46702ac6454SAndrew Thompson }
46802ac6454SAndrew Thompson 
46902ac6454SAndrew Thompson static void
470760bc48eSAndrew Thompson umct_cfg_param(struct ucom_softc *ucom, struct termios *t)
47102ac6454SAndrew Thompson {
47202ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
47302ac6454SAndrew Thompson 	uint32_t value;
47402ac6454SAndrew Thompson 
47502ac6454SAndrew Thompson 	value = umct_calc_baud(t->c_ospeed);
47602ac6454SAndrew Thompson 	umct_cfg_do_request(sc, UMCT_SET_BAUD, UMCT_SET_BAUD_SIZE, value);
47702ac6454SAndrew Thompson 
47802ac6454SAndrew Thompson 	value = (sc->sc_lcr & 0x40);
47902ac6454SAndrew Thompson 
48002ac6454SAndrew Thompson 	switch (t->c_cflag & CSIZE) {
48102ac6454SAndrew Thompson 	case CS5:
48202ac6454SAndrew Thompson 		value |= 0x0;
48302ac6454SAndrew Thompson 		break;
48402ac6454SAndrew Thompson 	case CS6:
48502ac6454SAndrew Thompson 		value |= 0x1;
48602ac6454SAndrew Thompson 		break;
48702ac6454SAndrew Thompson 	case CS7:
48802ac6454SAndrew Thompson 		value |= 0x2;
48902ac6454SAndrew Thompson 		break;
49002ac6454SAndrew Thompson 	default:
49102ac6454SAndrew Thompson 	case CS8:
49202ac6454SAndrew Thompson 		value |= 0x3;
49302ac6454SAndrew Thompson 		break;
49402ac6454SAndrew Thompson 	}
49502ac6454SAndrew Thompson 
49602ac6454SAndrew Thompson 	value |= (t->c_cflag & CSTOPB) ? 0x4 : 0;
49702ac6454SAndrew Thompson 	if (t->c_cflag & PARENB) {
49802ac6454SAndrew Thompson 		value |= 0x8;
49902ac6454SAndrew Thompson 		value |= (t->c_cflag & PARODD) ? 0x0 : 0x10;
50002ac6454SAndrew Thompson 	}
50102ac6454SAndrew Thompson 	/*
50202ac6454SAndrew Thompson 	 * XXX There doesn't seem to be a way to tell the device
50302ac6454SAndrew Thompson 	 * to use flow control.
50402ac6454SAndrew Thompson 	 */
50502ac6454SAndrew Thompson 
50602ac6454SAndrew Thompson 	sc->sc_lcr = value;
50702ac6454SAndrew Thompson 	umct_cfg_do_request(sc, UMCT_SET_LCR, UMCT_SET_LCR_SIZE, value);
50802ac6454SAndrew Thompson }
50902ac6454SAndrew Thompson 
51002ac6454SAndrew Thompson static void
511760bc48eSAndrew Thompson umct_start_read(struct ucom_softc *ucom)
51202ac6454SAndrew Thompson {
51302ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
51402ac6454SAndrew Thompson 
51502ac6454SAndrew Thompson 	/* start interrupt endpoint */
516a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UMCT_INTR_DT_RD]);
51702ac6454SAndrew Thompson 
51802ac6454SAndrew Thompson 	/* start read endpoint */
519a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_RD]);
52002ac6454SAndrew Thompson }
52102ac6454SAndrew Thompson 
52202ac6454SAndrew Thompson static void
523760bc48eSAndrew Thompson umct_stop_read(struct ucom_softc *ucom)
52402ac6454SAndrew Thompson {
52502ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
52602ac6454SAndrew Thompson 
52702ac6454SAndrew Thompson 	/* stop interrupt endpoint */
528a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UMCT_INTR_DT_RD]);
52902ac6454SAndrew Thompson 
53002ac6454SAndrew Thompson 	/* stop read endpoint */
531a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_RD]);
53202ac6454SAndrew Thompson }
53302ac6454SAndrew Thompson 
53402ac6454SAndrew Thompson static void
535760bc48eSAndrew Thompson umct_start_write(struct ucom_softc *ucom)
53602ac6454SAndrew Thompson {
53702ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
53802ac6454SAndrew Thompson 
539a593f6b8SAndrew Thompson 	usbd_transfer_start(sc->sc_xfer[UMCT_BULK_DT_WR]);
54002ac6454SAndrew Thompson }
54102ac6454SAndrew Thompson 
54202ac6454SAndrew Thompson static void
543760bc48eSAndrew Thompson umct_stop_write(struct ucom_softc *ucom)
54402ac6454SAndrew Thompson {
54502ac6454SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
54602ac6454SAndrew Thompson 
547a593f6b8SAndrew Thompson 	usbd_transfer_stop(sc->sc_xfer[UMCT_BULK_DT_WR]);
54802ac6454SAndrew Thompson }
54902ac6454SAndrew Thompson 
55002ac6454SAndrew Thompson static void
551585b13f2SAndrew Thompson umct_read_callback(struct usb_xfer *xfer, usb_error_t error)
552585b13f2SAndrew Thompson {
553585b13f2SAndrew Thompson 	struct umct_softc *sc = usbd_xfer_softc(xfer);
554585b13f2SAndrew Thompson 
555585b13f2SAndrew Thompson 	if (sc->sc_swap_cb)
556585b13f2SAndrew Thompson 		umct_intr_callback_sub(xfer, error);
557585b13f2SAndrew Thompson 	else
558585b13f2SAndrew Thompson 		umct_read_callback_sub(xfer, error);
559585b13f2SAndrew Thompson }
560585b13f2SAndrew Thompson 
561585b13f2SAndrew Thompson static void
562585b13f2SAndrew Thompson umct_intr_callback(struct usb_xfer *xfer, usb_error_t error)
563585b13f2SAndrew Thompson {
564585b13f2SAndrew Thompson 	struct umct_softc *sc = usbd_xfer_softc(xfer);
565585b13f2SAndrew Thompson 
566585b13f2SAndrew Thompson 	if (sc->sc_swap_cb)
567585b13f2SAndrew Thompson 		umct_read_callback_sub(xfer, error);
568585b13f2SAndrew Thompson 	else
569585b13f2SAndrew Thompson 		umct_intr_callback_sub(xfer, error);
570585b13f2SAndrew Thompson }
571585b13f2SAndrew Thompson 
572585b13f2SAndrew Thompson static void
573ed6d949aSAndrew Thompson umct_write_callback(struct usb_xfer *xfer, usb_error_t error)
57402ac6454SAndrew Thompson {
575ed6d949aSAndrew Thompson 	struct umct_softc *sc = usbd_xfer_softc(xfer);
576ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
57702ac6454SAndrew Thompson 	uint32_t actlen;
57802ac6454SAndrew Thompson 
57902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
58002ac6454SAndrew Thompson 	case USB_ST_SETUP:
58102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
58202ac6454SAndrew Thompson tr_setup:
583ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
584ed6d949aSAndrew Thompson 		if (ucom_get_data(&sc->sc_ucom, pc, 0,
58502ac6454SAndrew Thompson 		    sc->sc_obufsize, &actlen)) {
58602ac6454SAndrew Thompson 
587ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
588a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
58902ac6454SAndrew Thompson 		}
59002ac6454SAndrew Thompson 		return;
59102ac6454SAndrew Thompson 
59202ac6454SAndrew Thompson 	default:			/* Error */
593ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
59402ac6454SAndrew Thompson 			/* try to clear stall first */
595ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
59602ac6454SAndrew Thompson 			goto tr_setup;
59702ac6454SAndrew Thompson 		}
59802ac6454SAndrew Thompson 		return;
59902ac6454SAndrew Thompson 	}
60002ac6454SAndrew Thompson }
60102ac6454SAndrew Thompson 
60202ac6454SAndrew Thompson static void
603585b13f2SAndrew Thompson umct_read_callback_sub(struct usb_xfer *xfer, usb_error_t error)
60402ac6454SAndrew Thompson {
605ed6d949aSAndrew Thompson 	struct umct_softc *sc = usbd_xfer_softc(xfer);
606ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
607ed6d949aSAndrew Thompson 	int actlen;
608ed6d949aSAndrew Thompson 
609ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
61002ac6454SAndrew Thompson 
61102ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
61202ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
613ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
614ed6d949aSAndrew Thompson 		ucom_put_data(&sc->sc_ucom, pc, 0, actlen);
61502ac6454SAndrew Thompson 
61602ac6454SAndrew Thompson 	case USB_ST_SETUP:
61702ac6454SAndrew Thompson tr_setup:
618ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
619a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
62002ac6454SAndrew Thompson 		return;
62102ac6454SAndrew Thompson 
62202ac6454SAndrew Thompson 	default:			/* Error */
623ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
62402ac6454SAndrew Thompson 			/* try to clear stall first */
625ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
62602ac6454SAndrew Thompson 			goto tr_setup;
62702ac6454SAndrew Thompson 		}
62802ac6454SAndrew Thompson 		return;
62902ac6454SAndrew Thompson 	}
63002ac6454SAndrew Thompson }
631655dc9d0SAndrew Thompson 
632655dc9d0SAndrew Thompson static void
633655dc9d0SAndrew Thompson umct_poll(struct ucom_softc *ucom)
634655dc9d0SAndrew Thompson {
635655dc9d0SAndrew Thompson 	struct umct_softc *sc = ucom->sc_parent;
636655dc9d0SAndrew Thompson 	usbd_transfer_poll(sc->sc_xfer, UMCT_N_TRANSFER);
637655dc9d0SAndrew Thompson }
638