xref: /freebsd/sys/dev/usb/serial/ugensa.c (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
102ac6454SAndrew Thompson /*	$NetBSD: ugensa.c,v 1.9.2.1 2007/03/24 14:55:50 yamt Exp $	*/
202ac6454SAndrew Thompson 
3718cf2ccSPedro F. Giffuni /*-
4*b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5718cf2ccSPedro F. Giffuni  *
602ac6454SAndrew Thompson  * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
702ac6454SAndrew Thompson  * All rights reserved.
802ac6454SAndrew Thompson  *
902ac6454SAndrew Thompson  * This code is derived from software contributed to The NetBSD Foundation
1002ac6454SAndrew Thompson  * by Roland C. Dowdeswell <elric@netbsd.org>.
1102ac6454SAndrew Thompson  *
1202ac6454SAndrew Thompson  * Redistribution and use in source and binary forms, with or without
1302ac6454SAndrew Thompson  * modification, are permitted provided that the following conditions
1402ac6454SAndrew Thompson  * are met:
1502ac6454SAndrew Thompson  * 1. Redistributions of source code must retain the above copyright
1602ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer.
1702ac6454SAndrew Thompson  * 2. Redistributions in binary form must reproduce the above copyright
1802ac6454SAndrew Thompson  *    notice, this list of conditions and the following disclaimer in the
1902ac6454SAndrew Thompson  *    documentation and/or other materials provided with the distribution.
2002ac6454SAndrew Thompson  *
2102ac6454SAndrew Thompson  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2202ac6454SAndrew Thompson  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2302ac6454SAndrew Thompson  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2402ac6454SAndrew Thompson  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2502ac6454SAndrew Thompson  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2602ac6454SAndrew Thompson  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2702ac6454SAndrew Thompson  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2802ac6454SAndrew Thompson  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2902ac6454SAndrew Thompson  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3002ac6454SAndrew Thompson  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3102ac6454SAndrew Thompson  * POSSIBILITY OF SUCH DAMAGE.
3202ac6454SAndrew Thompson  */
3302ac6454SAndrew Thompson 
3402ac6454SAndrew Thompson /*
3502ac6454SAndrew Thompson  * NOTE: all function names beginning like "ugensa_cfg_" can only
3602ac6454SAndrew Thompson  * be called from within the config thread function !
3702ac6454SAndrew Thompson  */
3802ac6454SAndrew Thompson 
39ed6d949aSAndrew Thompson #include <sys/stdint.h>
40ed6d949aSAndrew Thompson #include <sys/stddef.h>
41ed6d949aSAndrew Thompson #include <sys/param.h>
42ed6d949aSAndrew Thompson #include <sys/queue.h>
43ed6d949aSAndrew Thompson #include <sys/types.h>
44ed6d949aSAndrew Thompson #include <sys/systm.h>
45ed6d949aSAndrew Thompson #include <sys/kernel.h>
46ed6d949aSAndrew Thompson #include <sys/bus.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 */
72ccbb3559SPoul-Henning Kamp #define	UGENSA_PORT_MAX		8	/* exclusivly */
7302ac6454SAndrew Thompson 
7402ac6454SAndrew Thompson enum {
7502ac6454SAndrew Thompson 	UGENSA_BULK_DT_WR,
7602ac6454SAndrew Thompson 	UGENSA_BULK_DT_RD,
7702ac6454SAndrew Thompson 	UGENSA_N_TRANSFER,
7802ac6454SAndrew Thompson };
7902ac6454SAndrew Thompson 
8002ac6454SAndrew Thompson struct ugensa_sub_softc {
81a593f6b8SAndrew Thompson 	struct ucom_softc *sc_ucom_ptr;
82760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER];
8302ac6454SAndrew Thompson };
8402ac6454SAndrew Thompson 
8502ac6454SAndrew Thompson struct ugensa_softc {
86760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
87ccbb3559SPoul-Henning Kamp 	struct ucom_softc sc_ucom[UGENSA_PORT_MAX];
88ccbb3559SPoul-Henning Kamp 	struct ugensa_sub_softc sc_sub[UGENSA_PORT_MAX];
8902ac6454SAndrew Thompson 
9002ac6454SAndrew Thompson 	struct mtx sc_mtx;
91ccbb3559SPoul-Henning Kamp 	uint8_t	sc_nports;
9202ac6454SAndrew Thompson };
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson /* prototypes */
9502ac6454SAndrew Thompson 
9602ac6454SAndrew Thompson static device_probe_t ugensa_probe;
9702ac6454SAndrew Thompson static device_attach_t ugensa_attach;
9802ac6454SAndrew Thompson static device_detach_t ugensa_detach;
99c01fc06eSHans Petter Selasky static void ugensa_free_softc(struct ugensa_softc *);
10002ac6454SAndrew Thompson 
101e0a69b51SAndrew Thompson static usb_callback_t ugensa_bulk_write_callback;
102e0a69b51SAndrew Thompson static usb_callback_t ugensa_bulk_read_callback;
10302ac6454SAndrew Thompson 
1045805d178SHans Petter Selasky static void	ugensa_free(struct ucom_softc *);
105760bc48eSAndrew Thompson static void	ugensa_start_read(struct ucom_softc *);
106760bc48eSAndrew Thompson static void	ugensa_stop_read(struct ucom_softc *);
107760bc48eSAndrew Thompson static void	ugensa_start_write(struct ucom_softc *);
108760bc48eSAndrew Thompson static void	ugensa_stop_write(struct ucom_softc *);
109655dc9d0SAndrew Thompson static void	ugensa_poll(struct ucom_softc *ucom);
11002ac6454SAndrew Thompson 
111655dc9d0SAndrew Thompson static const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = {
11202ac6454SAndrew Thompson 	[UGENSA_BULK_DT_WR] = {
11302ac6454SAndrew Thompson 		.type = UE_BULK,
11402ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
11502ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
1164eae601eSAndrew Thompson 		.bufsize = UGENSA_BUF_SIZE,
1174eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
1184eae601eSAndrew Thompson 		.callback = &ugensa_bulk_write_callback,
11902ac6454SAndrew Thompson 	},
12002ac6454SAndrew Thompson 
12102ac6454SAndrew Thompson 	[UGENSA_BULK_DT_RD] = {
12202ac6454SAndrew Thompson 		.type = UE_BULK,
12302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
12402ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
1254eae601eSAndrew Thompson 		.bufsize = UGENSA_BUF_SIZE,
1264eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1274eae601eSAndrew Thompson 		.callback = &ugensa_bulk_read_callback,
12802ac6454SAndrew Thompson 	},
12902ac6454SAndrew Thompson };
13002ac6454SAndrew Thompson 
131760bc48eSAndrew Thompson static const struct ucom_callback ugensa_callback = {
132a593f6b8SAndrew Thompson 	.ucom_start_read = &ugensa_start_read,
133a593f6b8SAndrew Thompson 	.ucom_stop_read = &ugensa_stop_read,
134a593f6b8SAndrew Thompson 	.ucom_start_write = &ugensa_start_write,
135a593f6b8SAndrew Thompson 	.ucom_stop_write = &ugensa_stop_write,
136655dc9d0SAndrew Thompson 	.ucom_poll = &ugensa_poll,
1375805d178SHans Petter Selasky 	.ucom_free = &ugensa_free,
13802ac6454SAndrew Thompson };
13902ac6454SAndrew Thompson 
14002ac6454SAndrew Thompson static device_method_t ugensa_methods[] = {
14102ac6454SAndrew Thompson 	/* Device methods */
14202ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ugensa_probe),
14302ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ugensa_attach),
14402ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ugensa_detach),
1455805d178SHans Petter Selasky 	DEVMETHOD_END
14602ac6454SAndrew Thompson };
14702ac6454SAndrew Thompson 
14802ac6454SAndrew Thompson static driver_t ugensa_driver = {
14902ac6454SAndrew Thompson 	.name = "ugensa",
15002ac6454SAndrew Thompson 	.methods = ugensa_methods,
15102ac6454SAndrew Thompson 	.size = sizeof(struct ugensa_softc),
15202ac6454SAndrew Thompson };
15302ac6454SAndrew Thompson 
154ccbb3559SPoul-Henning Kamp /* Driver-info is max number of serial ports per interface */
155f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID ugensa_devs[] = {
156ccbb3559SPoul-Henning Kamp 	{USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 1)},
157ccbb3559SPoul-Henning Kamp 	{USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 1)},
158ccbb3559SPoul-Henning Kamp 	{USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 1)},
159ccbb3559SPoul-Henning Kamp 	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 1)},
160ccbb3559SPoul-Henning Kamp 	{USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 3)},
16188535229SHans Petter Selasky 	{USB_VENDOR(USB_VENDOR_GOOGLE), USB_IFACE_CLASS(UICLASS_VENDOR),
16288535229SHans Petter Selasky 		USB_IFACE_SUBCLASS(0x50), USB_IFACE_PROTOCOL(0x01), USB_DRIVER_INFO(10)},
16302ac6454SAndrew Thompson };
16402ac6454SAndrew Thompson 
165bc9372d7SJohn Baldwin DRIVER_MODULE(ugensa, uhub, ugensa_driver, NULL, NULL);
166f809f280SWarner Losh MODULE_DEPEND(ugensa, ucom, 1, 1, 1);
167f809f280SWarner Losh MODULE_DEPEND(ugensa, usb, 1, 1, 1);
168f809f280SWarner Losh MODULE_VERSION(ugensa, 1);
169f809f280SWarner Losh USB_PNP_HOST_INFO(ugensa_devs);
170f809f280SWarner Losh 
17102ac6454SAndrew Thompson static int
ugensa_probe(device_t dev)17202ac6454SAndrew Thompson ugensa_probe(device_t dev)
17302ac6454SAndrew Thompson {
174760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
17502ac6454SAndrew Thompson 
176f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
17702ac6454SAndrew Thompson 		return (ENXIO);
17802ac6454SAndrew Thompson 	}
17902ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
18002ac6454SAndrew Thompson 		return (ENXIO);
18102ac6454SAndrew Thompson 	}
18202ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != 0) {
18302ac6454SAndrew Thompson 		return (ENXIO);
18402ac6454SAndrew Thompson 	}
185a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
18602ac6454SAndrew Thompson }
18702ac6454SAndrew Thompson 
18802ac6454SAndrew Thompson static int
ugensa_attach(device_t dev)18902ac6454SAndrew Thompson ugensa_attach(device_t dev)
19002ac6454SAndrew Thompson {
191760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
19202ac6454SAndrew Thompson 	struct ugensa_softc *sc = device_get_softc(dev);
19302ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc;
194760bc48eSAndrew Thompson 	struct usb_interface *iface;
195ccbb3559SPoul-Henning Kamp 	struct usb_config xfer_config[UGENSA_N_TRANSFER];
19602ac6454SAndrew Thompson 	int32_t error;
19702ac6454SAndrew Thompson 	uint8_t iface_index;
198ccbb3559SPoul-Henning Kamp 	int x, maxports;
19902ac6454SAndrew Thompson 
200ccbb3559SPoul-Henning Kamp 	maxports = USB_GET_DRIVER_INFO(uaa);
201a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
20202ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
2035805d178SHans Petter Selasky 	ucom_ref(&sc->sc_super_ucom);
20402ac6454SAndrew Thompson 
205ccbb3559SPoul-Henning Kamp 	for (iface_index = UGENSA_IFACE_INDEX; iface_index < UGENSA_IFACE_MAX; iface_index++) {
206ccbb3559SPoul-Henning Kamp 		iface = usbd_get_iface(uaa->device, iface_index);
207ccbb3559SPoul-Henning Kamp 		if (iface == NULL || iface->idesc->bInterfaceClass != UICLASS_VENDOR)
20802ac6454SAndrew Thompson 			/* Not a serial port, most likely a SD reader */
20902ac6454SAndrew Thompson 			continue;
21002ac6454SAndrew Thompson 
211ccbb3559SPoul-Henning Kamp 		/* Loop over all endpoints pairwise */
212ccbb3559SPoul-Henning Kamp 		for (x = 0; x < maxports && sc->sc_nports < UGENSA_PORT_MAX; x++) {
213ccbb3559SPoul-Henning Kamp 			ssc = sc->sc_sub + sc->sc_nports;
214ccbb3559SPoul-Henning Kamp 			ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_nports;
215ccbb3559SPoul-Henning Kamp 
216ccbb3559SPoul-Henning Kamp 			memcpy(xfer_config, ugensa_xfer_config, sizeof ugensa_xfer_config);
217ccbb3559SPoul-Henning Kamp 			xfer_config[UGENSA_BULK_DT_RD].ep_index = x;
218ccbb3559SPoul-Henning Kamp 			xfer_config[UGENSA_BULK_DT_WR].ep_index = x;
219ccbb3559SPoul-Henning Kamp 
220a593f6b8SAndrew Thompson 			error = usbd_transfer_setup(uaa->device,
221ccbb3559SPoul-Henning Kamp 			    &iface_index, ssc->sc_xfer, xfer_config,
22202ac6454SAndrew Thompson 			    UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
22302ac6454SAndrew Thompson 
22402ac6454SAndrew Thompson 			if (error) {
225ccbb3559SPoul-Henning Kamp 				if (x == 0) {
22602ac6454SAndrew Thompson 					device_printf(dev, "allocating USB "
227ccbb3559SPoul-Henning Kamp 					    "transfers failed (%d)\n", error);
22802ac6454SAndrew Thompson 					goto detach;
22902ac6454SAndrew Thompson 				}
230ccbb3559SPoul-Henning Kamp 				break;
231ccbb3559SPoul-Henning Kamp 			}
232ccbb3559SPoul-Henning Kamp 
233ec97e9caSHans Petter Selasky 			/* clear stall at first run */
234deefe583SAndrew Thompson 			mtx_lock(&sc->sc_mtx);
235ec97e9caSHans Petter Selasky 			usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
236ec97e9caSHans Petter Selasky 			usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
237deefe583SAndrew Thompson 			mtx_unlock(&sc->sc_mtx);
23802ac6454SAndrew Thompson 
23902ac6454SAndrew Thompson 			/* initialize port number */
240ccbb3559SPoul-Henning Kamp 			ssc->sc_ucom_ptr->sc_portno = sc->sc_nports;
241ccbb3559SPoul-Henning Kamp 			if (iface_index != uaa->info.bIfaceIndex) {
242ccbb3559SPoul-Henning Kamp 				usbd_set_parent_iface(uaa->device, iface_index,
24302ac6454SAndrew Thompson 				    uaa->info.bIfaceIndex);
24402ac6454SAndrew Thompson 			}
245ccbb3559SPoul-Henning Kamp 			sc->sc_nports++;
246ccbb3559SPoul-Henning Kamp 		}
247ccbb3559SPoul-Henning Kamp 	}
248ccbb3559SPoul-Henning Kamp 	device_printf(dev, "Found %d serial ports.\n", sc->sc_nports);
24902ac6454SAndrew Thompson 
250ccbb3559SPoul-Henning Kamp 	error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_nports, sc,
25102ac6454SAndrew Thompson 	    &ugensa_callback, &sc->sc_mtx);
252ccbb3559SPoul-Henning Kamp 
25302ac6454SAndrew Thompson 	if (error) {
254ccbb3559SPoul-Henning Kamp 		DPRINTF("ucom attach failed\n");
25502ac6454SAndrew Thompson 		goto detach;
25602ac6454SAndrew Thompson 	}
2576416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
2586416c259SNick Hibma 
25902ac6454SAndrew Thompson 	return (0);			/* success */
26002ac6454SAndrew Thompson 
26102ac6454SAndrew Thompson detach:
26202ac6454SAndrew Thompson 	ugensa_detach(dev);
26302ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
26402ac6454SAndrew Thompson }
26502ac6454SAndrew Thompson 
26602ac6454SAndrew Thompson static int
ugensa_detach(device_t dev)26702ac6454SAndrew Thompson ugensa_detach(device_t dev)
26802ac6454SAndrew Thompson {
26902ac6454SAndrew Thompson 	struct ugensa_softc *sc = device_get_softc(dev);
27002ac6454SAndrew Thompson 	uint8_t x;
27102ac6454SAndrew Thompson 
272015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
27302ac6454SAndrew Thompson 
274ccbb3559SPoul-Henning Kamp 	for (x = 0; x < sc->sc_nports; x++) {
275a593f6b8SAndrew Thompson 		usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
27602ac6454SAndrew Thompson 	}
27702ac6454SAndrew Thompson 
278c01fc06eSHans Petter Selasky 	device_claim_softc(dev);
279c01fc06eSHans Petter Selasky 
280c01fc06eSHans Petter Selasky 	ugensa_free_softc(sc);
281c01fc06eSHans Petter Selasky 
28202ac6454SAndrew Thompson 	return (0);
28302ac6454SAndrew Thompson }
28402ac6454SAndrew Thompson 
2855805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(ugensa);
2865805d178SHans Petter Selasky 
2875805d178SHans Petter Selasky static void
ugensa_free_softc(struct ugensa_softc * sc)288c01fc06eSHans Petter Selasky ugensa_free_softc(struct ugensa_softc *sc)
2895805d178SHans Petter Selasky {
2905805d178SHans Petter Selasky 	if (ucom_unref(&sc->sc_super_ucom)) {
2915805d178SHans Petter Selasky 		mtx_destroy(&sc->sc_mtx);
292c01fc06eSHans Petter Selasky 		device_free_softc(sc);
2935805d178SHans Petter Selasky 	}
2945805d178SHans Petter Selasky }
2955805d178SHans Petter Selasky 
2965805d178SHans Petter Selasky static void
ugensa_free(struct ucom_softc * ucom)2975805d178SHans Petter Selasky ugensa_free(struct ucom_softc *ucom)
2985805d178SHans Petter Selasky {
299c01fc06eSHans Petter Selasky 	ugensa_free_softc(ucom->sc_parent);
3005805d178SHans Petter Selasky }
3015805d178SHans Petter Selasky 
30202ac6454SAndrew Thompson static void
ugensa_bulk_write_callback(struct usb_xfer * xfer,usb_error_t error)303ed6d949aSAndrew Thompson ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
30402ac6454SAndrew Thompson {
305ed6d949aSAndrew Thompson 	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
306ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
30702ac6454SAndrew Thompson 	uint32_t actlen;
30802ac6454SAndrew Thompson 
30902ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
31002ac6454SAndrew Thompson 	case USB_ST_SETUP:
31102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
31202ac6454SAndrew Thompson tr_setup:
313ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
314ed6d949aSAndrew Thompson 		if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
31502ac6454SAndrew Thompson 		    UGENSA_BUF_SIZE, &actlen)) {
316ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
317a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
31802ac6454SAndrew Thompson 		}
319ec97e9caSHans Petter Selasky 		return;
32002ac6454SAndrew Thompson 
32102ac6454SAndrew Thompson 	default:			/* Error */
322ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
32302ac6454SAndrew Thompson 			/* try to clear stall first */
324ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
32502ac6454SAndrew Thompson 			goto tr_setup;
32602ac6454SAndrew Thompson 		}
327ec97e9caSHans Petter Selasky 		return;
32802ac6454SAndrew Thompson 	}
32902ac6454SAndrew Thompson }
33002ac6454SAndrew Thompson 
33102ac6454SAndrew Thompson static void
ugensa_bulk_read_callback(struct usb_xfer * xfer,usb_error_t error)332ed6d949aSAndrew Thompson ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
33302ac6454SAndrew Thompson {
334ed6d949aSAndrew Thompson 	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
335ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
336ed6d949aSAndrew Thompson 	int actlen;
337ed6d949aSAndrew Thompson 
338ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
33902ac6454SAndrew Thompson 
34002ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
34102ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
342ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
343ed6d949aSAndrew Thompson 		ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
34402ac6454SAndrew Thompson 
34502ac6454SAndrew Thompson 	case USB_ST_SETUP:
34602ac6454SAndrew Thompson tr_setup:
347ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
348a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
34902ac6454SAndrew Thompson 		return;
35002ac6454SAndrew Thompson 
35102ac6454SAndrew Thompson 	default:			/* Error */
352ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
35302ac6454SAndrew Thompson 			/* try to clear stall first */
354ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
35502ac6454SAndrew Thompson 			goto tr_setup;
35602ac6454SAndrew Thompson 		}
35702ac6454SAndrew Thompson 		return;
35802ac6454SAndrew Thompson 	}
35902ac6454SAndrew Thompson }
36002ac6454SAndrew Thompson 
36102ac6454SAndrew Thompson static void
ugensa_start_read(struct ucom_softc * ucom)362760bc48eSAndrew Thompson ugensa_start_read(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_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
36802ac6454SAndrew Thompson }
36902ac6454SAndrew Thompson 
37002ac6454SAndrew Thompson static void
ugensa_stop_read(struct ucom_softc * ucom)371760bc48eSAndrew Thompson ugensa_stop_read(struct ucom_softc *ucom)
37202ac6454SAndrew Thompson {
37302ac6454SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
37402ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
37502ac6454SAndrew Thompson 
376a593f6b8SAndrew Thompson 	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
37702ac6454SAndrew Thompson }
37802ac6454SAndrew Thompson 
37902ac6454SAndrew Thompson static void
ugensa_start_write(struct ucom_softc * ucom)380760bc48eSAndrew Thompson ugensa_start_write(struct ucom_softc *ucom)
38102ac6454SAndrew Thompson {
38202ac6454SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
38302ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
38402ac6454SAndrew Thompson 
385a593f6b8SAndrew Thompson 	usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
38602ac6454SAndrew Thompson }
38702ac6454SAndrew Thompson 
38802ac6454SAndrew Thompson static void
ugensa_stop_write(struct ucom_softc * ucom)389760bc48eSAndrew Thompson ugensa_stop_write(struct ucom_softc *ucom)
39002ac6454SAndrew Thompson {
39102ac6454SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
39202ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
39302ac6454SAndrew Thompson 
394a593f6b8SAndrew Thompson 	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
39502ac6454SAndrew Thompson }
396655dc9d0SAndrew Thompson 
397655dc9d0SAndrew Thompson static void
ugensa_poll(struct ucom_softc * ucom)398655dc9d0SAndrew Thompson ugensa_poll(struct ucom_softc *ucom)
399655dc9d0SAndrew Thompson {
400655dc9d0SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
401655dc9d0SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
402655dc9d0SAndrew Thompson 
403655dc9d0SAndrew Thompson 	usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER);
404655dc9d0SAndrew Thompson }
405