xref: /freebsd/sys/dev/usb/serial/ugensa.c (revision 6416c259d4ee0adf7a11d83de2b876d240051bc3)
102ac6454SAndrew Thompson /* $FreeBSD$ */
202ac6454SAndrew Thompson /*	$NetBSD: ugensa.c,v 1.9.2.1 2007/03/24 14:55:50 yamt Exp $	*/
302ac6454SAndrew Thompson 
402ac6454SAndrew Thompson /*
502ac6454SAndrew Thompson  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
602ac6454SAndrew Thompson  * All rights reserved.
702ac6454SAndrew Thompson  *
802ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
902ac6454SAndrew Thompson  * by Roland C. Dowdeswell <elric@netbsd.org>.
1002ac6454SAndrew Thompson  *
1102ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1202ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1302ac6454SAndrew Thompson  * are met:
1402ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1502ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1602ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1702ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1802ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
1902ac6454SAndrew Thompson  *
2002ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2102ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2202ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2302ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2402ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2502ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2602ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2702ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2802ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2902ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3002ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
3102ac6454SAndrew Thompson  */
3202ac6454SAndrew Thompson 
3302ac6454SAndrew Thompson /*
3402ac6454SAndrew Thompson  * NOTE: all function names beginning like "ugensa_cfg_" can only
3502ac6454SAndrew Thompson  * be called from within the config thread function !
3602ac6454SAndrew Thompson  */
3702ac6454SAndrew Thompson 
38ed6d949aSAndrew Thompson #include <sys/stdint.h>
39ed6d949aSAndrew Thompson #include <sys/stddef.h>
40ed6d949aSAndrew Thompson #include <sys/param.h>
41ed6d949aSAndrew Thompson #include <sys/queue.h>
42ed6d949aSAndrew Thompson #include <sys/types.h>
43ed6d949aSAndrew Thompson #include <sys/systm.h>
44ed6d949aSAndrew Thompson #include <sys/kernel.h>
45ed6d949aSAndrew Thompson #include <sys/bus.h>
46ed6d949aSAndrew Thompson #include <sys/linker_set.h>
47ed6d949aSAndrew Thompson #include <sys/module.h>
48ed6d949aSAndrew Thompson #include <sys/lock.h>
49ed6d949aSAndrew Thompson #include <sys/mutex.h>
50ed6d949aSAndrew Thompson #include <sys/condvar.h>
51ed6d949aSAndrew Thompson #include <sys/sysctl.h>
52ed6d949aSAndrew Thompson #include <sys/sx.h>
53ed6d949aSAndrew Thompson #include <sys/unistd.h>
54ed6d949aSAndrew Thompson #include <sys/callout.h>
55ed6d949aSAndrew Thompson #include <sys/malloc.h>
56ed6d949aSAndrew Thompson #include <sys/priv.h>
57ed6d949aSAndrew Thompson 
5802ac6454SAndrew Thompson #include <dev/usb/usb.h>
59ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
60ed6d949aSAndrew Thompson #include "usbdevs.h"
6102ac6454SAndrew Thompson 
62a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_debug
6302ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
6402ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6502ac6454SAndrew Thompson 
6602ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
6702ac6454SAndrew Thompson 
6802ac6454SAndrew Thompson #define	UGENSA_BUF_SIZE		2048	/* bytes */
6902ac6454SAndrew Thompson #define	UGENSA_CONFIG_INDEX	0
7002ac6454SAndrew Thompson #define	UGENSA_IFACE_INDEX	0
7102ac6454SAndrew Thompson #define	UGENSA_IFACE_MAX	8	/* exclusivly */
7202ac6454SAndrew Thompson 
7302ac6454SAndrew Thompson enum {
7402ac6454SAndrew Thompson 	UGENSA_BULK_DT_WR,
7502ac6454SAndrew Thompson 	UGENSA_BULK_DT_RD,
7602ac6454SAndrew Thompson 	UGENSA_N_TRANSFER,
7702ac6454SAndrew Thompson };
7802ac6454SAndrew Thompson 
7902ac6454SAndrew Thompson struct ugensa_sub_softc {
80a593f6b8SAndrew Thompson 	struct ucom_softc *sc_ucom_ptr;
81760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER];
8202ac6454SAndrew Thompson };
8302ac6454SAndrew Thompson 
8402ac6454SAndrew Thompson struct ugensa_softc {
85760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
86760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom[UGENSA_IFACE_MAX];
8702ac6454SAndrew Thompson 	struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX];
8802ac6454SAndrew Thompson 
8902ac6454SAndrew Thompson 	struct mtx sc_mtx;
9002ac6454SAndrew Thompson 	uint8_t	sc_niface;
9102ac6454SAndrew Thompson };
9202ac6454SAndrew Thompson 
9302ac6454SAndrew Thompson /* prototypes */
9402ac6454SAndrew Thompson 
9502ac6454SAndrew Thompson static device_probe_t ugensa_probe;
9602ac6454SAndrew Thompson static device_attach_t ugensa_attach;
9702ac6454SAndrew Thompson static device_detach_t ugensa_detach;
9802ac6454SAndrew Thompson 
99e0a69b51SAndrew Thompson static usb_callback_t ugensa_bulk_write_callback;
100e0a69b51SAndrew Thompson static usb_callback_t ugensa_bulk_read_callback;
10102ac6454SAndrew Thompson 
102760bc48eSAndrew Thompson static void	ugensa_start_read(struct ucom_softc *);
103760bc48eSAndrew Thompson static void	ugensa_stop_read(struct ucom_softc *);
104760bc48eSAndrew Thompson static void	ugensa_start_write(struct ucom_softc *);
105760bc48eSAndrew Thompson static void	ugensa_stop_write(struct ucom_softc *);
106655dc9d0SAndrew Thompson static void	ugensa_poll(struct ucom_softc *ucom);
10702ac6454SAndrew Thompson 
108655dc9d0SAndrew Thompson static const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = {
10902ac6454SAndrew Thompson 
11002ac6454SAndrew Thompson 	[UGENSA_BULK_DT_WR] = {
11102ac6454SAndrew Thompson 		.type = UE_BULK,
11202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
11302ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
1144eae601eSAndrew Thompson 		.bufsize = UGENSA_BUF_SIZE,
1154eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
1164eae601eSAndrew Thompson 		.callback = &ugensa_bulk_write_callback,
11702ac6454SAndrew Thompson 	},
11802ac6454SAndrew Thompson 
11902ac6454SAndrew Thompson 	[UGENSA_BULK_DT_RD] = {
12002ac6454SAndrew Thompson 		.type = UE_BULK,
12102ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
12202ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
1234eae601eSAndrew Thompson 		.bufsize = UGENSA_BUF_SIZE,
1244eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1254eae601eSAndrew Thompson 		.callback = &ugensa_bulk_read_callback,
12602ac6454SAndrew Thompson 	},
12702ac6454SAndrew Thompson };
12802ac6454SAndrew Thompson 
129760bc48eSAndrew Thompson static const struct ucom_callback ugensa_callback = {
130a593f6b8SAndrew Thompson 	.ucom_start_read = &ugensa_start_read,
131a593f6b8SAndrew Thompson 	.ucom_stop_read = &ugensa_stop_read,
132a593f6b8SAndrew Thompson 	.ucom_start_write = &ugensa_start_write,
133a593f6b8SAndrew Thompson 	.ucom_stop_write = &ugensa_stop_write,
134655dc9d0SAndrew Thompson 	.ucom_poll = &ugensa_poll,
13502ac6454SAndrew Thompson };
13602ac6454SAndrew Thompson 
13702ac6454SAndrew Thompson static device_method_t ugensa_methods[] = {
13802ac6454SAndrew Thompson 	/* Device methods */
13902ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ugensa_probe),
14002ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ugensa_attach),
14102ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ugensa_detach),
14202ac6454SAndrew Thompson 	{0, 0}
14302ac6454SAndrew Thompson };
14402ac6454SAndrew Thompson 
14502ac6454SAndrew Thompson static devclass_t ugensa_devclass;
14602ac6454SAndrew Thompson 
14702ac6454SAndrew Thompson static driver_t ugensa_driver = {
14802ac6454SAndrew Thompson 	.name = "ugensa",
14902ac6454SAndrew Thompson 	.methods = ugensa_methods,
15002ac6454SAndrew Thompson 	.size = sizeof(struct ugensa_softc),
15102ac6454SAndrew Thompson };
15202ac6454SAndrew Thompson 
1539aef556dSAndrew Thompson DRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0);
15402ac6454SAndrew Thompson MODULE_DEPEND(ugensa, ucom, 1, 1, 1);
15502ac6454SAndrew Thompson MODULE_DEPEND(ugensa, usb, 1, 1, 1);
156910cb8feSAndrew Thompson MODULE_VERSION(ugensa, 1);
15702ac6454SAndrew Thompson 
158760bc48eSAndrew Thompson static const struct usb_device_id ugensa_devs[] = {
15902ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)},
16002ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)},
16102ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)},
16202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)},
16302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)},
16402ac6454SAndrew Thompson };
16502ac6454SAndrew Thompson 
16602ac6454SAndrew Thompson static int
16702ac6454SAndrew Thompson ugensa_probe(device_t dev)
16802ac6454SAndrew Thompson {
169760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
17002ac6454SAndrew Thompson 
171f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
17202ac6454SAndrew Thompson 		return (ENXIO);
17302ac6454SAndrew Thompson 	}
17402ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
17502ac6454SAndrew Thompson 		return (ENXIO);
17602ac6454SAndrew Thompson 	}
17702ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != 0) {
17802ac6454SAndrew Thompson 		return (ENXIO);
17902ac6454SAndrew Thompson 	}
180a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
18102ac6454SAndrew Thompson }
18202ac6454SAndrew Thompson 
18302ac6454SAndrew Thompson static int
18402ac6454SAndrew Thompson ugensa_attach(device_t dev)
18502ac6454SAndrew Thompson {
186760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
18702ac6454SAndrew Thompson 	struct ugensa_softc *sc = device_get_softc(dev);
18802ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc;
189760bc48eSAndrew Thompson 	struct usb_interface *iface;
19002ac6454SAndrew Thompson 	int32_t error;
19102ac6454SAndrew Thompson 	uint8_t iface_index;
19202ac6454SAndrew Thompson 	int x, cnt;
19302ac6454SAndrew Thompson 
194a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
19502ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
19602ac6454SAndrew Thompson 
19702ac6454SAndrew Thompson 	/* Figure out how many interfaces this device has got */
19802ac6454SAndrew Thompson 	for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) {
199a593f6b8SAndrew Thompson 		if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) ||
200a593f6b8SAndrew Thompson 		    (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) {
20102ac6454SAndrew Thompson 			/* we have reached the end */
20202ac6454SAndrew Thompson 			break;
20302ac6454SAndrew Thompson 		}
20402ac6454SAndrew Thompson 	}
20502ac6454SAndrew Thompson 
20602ac6454SAndrew Thompson 	if (cnt == 0) {
207767cb2e2SAndrew Thompson 		device_printf(dev, "No interfaces\n");
20802ac6454SAndrew Thompson 		goto detach;
20902ac6454SAndrew Thompson 	}
21002ac6454SAndrew Thompson 	for (x = 0; x < cnt; x++) {
211a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, x);
21202ac6454SAndrew Thompson 		if (iface->idesc->bInterfaceClass != UICLASS_VENDOR)
21302ac6454SAndrew Thompson 			/* Not a serial port, most likely a SD reader */
21402ac6454SAndrew Thompson 			continue;
21502ac6454SAndrew Thompson 
21602ac6454SAndrew Thompson 		ssc = sc->sc_sub + sc->sc_niface;
217a593f6b8SAndrew Thompson 		ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface;
21802ac6454SAndrew Thompson 
21902ac6454SAndrew Thompson 		iface_index = (UGENSA_IFACE_INDEX + x);
220a593f6b8SAndrew Thompson 		error = usbd_transfer_setup(uaa->device,
22102ac6454SAndrew Thompson 		    &iface_index, ssc->sc_xfer, ugensa_xfer_config,
22202ac6454SAndrew Thompson 		    UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
22302ac6454SAndrew Thompson 
22402ac6454SAndrew Thompson 		if (error) {
22502ac6454SAndrew Thompson 			device_printf(dev, "allocating USB "
226767cb2e2SAndrew Thompson 			    "transfers failed\n");
22702ac6454SAndrew Thompson 			goto detach;
22802ac6454SAndrew Thompson 		}
22902ac6454SAndrew Thompson 		/* clear stall at first run */
230deefe583SAndrew Thompson 		mtx_lock(&sc->sc_mtx);
231ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
232ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
233deefe583SAndrew Thompson 		mtx_unlock(&sc->sc_mtx);
23402ac6454SAndrew Thompson 
23502ac6454SAndrew Thompson 		/* initialize port number */
236a593f6b8SAndrew Thompson 		ssc->sc_ucom_ptr->sc_portno = sc->sc_niface;
23702ac6454SAndrew Thompson 		sc->sc_niface++;
23802ac6454SAndrew Thompson 		if (x != uaa->info.bIfaceIndex)
239a593f6b8SAndrew Thompson 			usbd_set_parent_iface(uaa->device, x,
24002ac6454SAndrew Thompson 			    uaa->info.bIfaceIndex);
24102ac6454SAndrew Thompson 	}
24202ac6454SAndrew Thompson 	device_printf(dev, "Found %d interfaces.\n", sc->sc_niface);
24302ac6454SAndrew Thompson 
244a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc,
24502ac6454SAndrew Thompson 	    &ugensa_callback, &sc->sc_mtx);
24602ac6454SAndrew Thompson 	if (error) {
24702ac6454SAndrew Thompson 		DPRINTF("attach failed\n");
24802ac6454SAndrew Thompson 		goto detach;
24902ac6454SAndrew Thompson 	}
250*6416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
251*6416c259SNick Hibma 
25202ac6454SAndrew Thompson 	return (0);			/* success */
25302ac6454SAndrew Thompson 
25402ac6454SAndrew Thompson detach:
25502ac6454SAndrew Thompson 	ugensa_detach(dev);
25602ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
25702ac6454SAndrew Thompson }
25802ac6454SAndrew Thompson 
25902ac6454SAndrew Thompson static int
26002ac6454SAndrew Thompson ugensa_detach(device_t dev)
26102ac6454SAndrew Thompson {
26202ac6454SAndrew Thompson 	struct ugensa_softc *sc = device_get_softc(dev);
26302ac6454SAndrew Thompson 	uint8_t x;
26402ac6454SAndrew Thompson 
265015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
26602ac6454SAndrew Thompson 
26702ac6454SAndrew Thompson 	for (x = 0; x < sc->sc_niface; x++) {
268a593f6b8SAndrew Thompson 		usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
26902ac6454SAndrew Thompson 	}
27002ac6454SAndrew Thompson 	mtx_destroy(&sc->sc_mtx);
27102ac6454SAndrew Thompson 
27202ac6454SAndrew Thompson 	return (0);
27302ac6454SAndrew Thompson }
27402ac6454SAndrew Thompson 
27502ac6454SAndrew Thompson static void
276ed6d949aSAndrew Thompson ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
27702ac6454SAndrew Thompson {
278ed6d949aSAndrew Thompson 	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
279ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
28002ac6454SAndrew Thompson 	uint32_t actlen;
28102ac6454SAndrew Thompson 
28202ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
28302ac6454SAndrew Thompson 	case USB_ST_SETUP:
28402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
28502ac6454SAndrew Thompson tr_setup:
286ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
287ed6d949aSAndrew Thompson 		if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
28802ac6454SAndrew Thompson 		    UGENSA_BUF_SIZE, &actlen)) {
289ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
290a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
29102ac6454SAndrew Thompson 		}
29202ac6454SAndrew Thompson 		return;
29302ac6454SAndrew Thompson 
29402ac6454SAndrew Thompson 	default:			/* Error */
295ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
29602ac6454SAndrew Thompson 			/* try to clear stall first */
297ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
29802ac6454SAndrew Thompson 			goto tr_setup;
29902ac6454SAndrew Thompson 		}
30002ac6454SAndrew Thompson 		return;
30102ac6454SAndrew Thompson 	}
30202ac6454SAndrew Thompson }
30302ac6454SAndrew Thompson 
30402ac6454SAndrew Thompson static void
305ed6d949aSAndrew Thompson ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
30602ac6454SAndrew Thompson {
307ed6d949aSAndrew Thompson 	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
308ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
309ed6d949aSAndrew Thompson 	int actlen;
310ed6d949aSAndrew Thompson 
311ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
31202ac6454SAndrew Thompson 
31302ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
31402ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
315ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
316ed6d949aSAndrew Thompson 		ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
31702ac6454SAndrew Thompson 
31802ac6454SAndrew Thompson 	case USB_ST_SETUP:
31902ac6454SAndrew Thompson tr_setup:
320ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
321a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
32202ac6454SAndrew Thompson 		return;
32302ac6454SAndrew Thompson 
32402ac6454SAndrew Thompson 	default:			/* Error */
325ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
32602ac6454SAndrew Thompson 			/* try to clear stall first */
327ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
32802ac6454SAndrew Thompson 			goto tr_setup;
32902ac6454SAndrew Thompson 		}
33002ac6454SAndrew Thompson 		return;
33102ac6454SAndrew Thompson 	}
33202ac6454SAndrew Thompson }
33302ac6454SAndrew Thompson 
33402ac6454SAndrew Thompson static void
335760bc48eSAndrew Thompson ugensa_start_read(struct ucom_softc *ucom)
33602ac6454SAndrew Thompson {
33702ac6454SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
33802ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
33902ac6454SAndrew Thompson 
340a593f6b8SAndrew Thompson 	usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
34102ac6454SAndrew Thompson }
34202ac6454SAndrew Thompson 
34302ac6454SAndrew Thompson static void
344760bc48eSAndrew Thompson ugensa_stop_read(struct ucom_softc *ucom)
34502ac6454SAndrew Thompson {
34602ac6454SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
34702ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
34802ac6454SAndrew Thompson 
349a593f6b8SAndrew Thompson 	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
35002ac6454SAndrew Thompson }
35102ac6454SAndrew Thompson 
35202ac6454SAndrew Thompson static void
353760bc48eSAndrew Thompson ugensa_start_write(struct ucom_softc *ucom)
35402ac6454SAndrew Thompson {
35502ac6454SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
35602ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
35702ac6454SAndrew Thompson 
358a593f6b8SAndrew Thompson 	usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
35902ac6454SAndrew Thompson }
36002ac6454SAndrew Thompson 
36102ac6454SAndrew Thompson static void
362760bc48eSAndrew Thompson ugensa_stop_write(struct ucom_softc *ucom)
36302ac6454SAndrew Thompson {
36402ac6454SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
36502ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
36602ac6454SAndrew Thompson 
367a593f6b8SAndrew Thompson 	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
36802ac6454SAndrew Thompson }
369655dc9d0SAndrew Thompson 
370655dc9d0SAndrew Thompson static void
371655dc9d0SAndrew Thompson ugensa_poll(struct ucom_softc *ucom)
372655dc9d0SAndrew Thompson {
373655dc9d0SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
374655dc9d0SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
375655dc9d0SAndrew Thompson 
376655dc9d0SAndrew Thompson 	usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER);
377655dc9d0SAndrew Thompson }
378