xref: /freebsd/sys/dev/usb/serial/ugensa.c (revision 5805d1782dc26efb42fa24df0dbada6dbe1dd566)
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/module.h>
47ed6d949aSAndrew Thompson #include <sys/lock.h>
48ed6d949aSAndrew Thompson #include <sys/mutex.h>
49ed6d949aSAndrew Thompson #include <sys/condvar.h>
50ed6d949aSAndrew Thompson #include <sys/sysctl.h>
51ed6d949aSAndrew Thompson #include <sys/sx.h>
52ed6d949aSAndrew Thompson #include <sys/unistd.h>
53ed6d949aSAndrew Thompson #include <sys/callout.h>
54ed6d949aSAndrew Thompson #include <sys/malloc.h>
55ed6d949aSAndrew Thompson #include <sys/priv.h>
56ed6d949aSAndrew Thompson 
5702ac6454SAndrew Thompson #include <dev/usb/usb.h>
58ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h>
59ed6d949aSAndrew Thompson #include "usbdevs.h"
6002ac6454SAndrew Thompson 
61a593f6b8SAndrew Thompson #define	USB_DEBUG_VAR usb_debug
6202ac6454SAndrew Thompson #include <dev/usb/usb_debug.h>
6302ac6454SAndrew Thompson #include <dev/usb/usb_process.h>
6402ac6454SAndrew Thompson 
6502ac6454SAndrew Thompson #include <dev/usb/serial/usb_serial.h>
6602ac6454SAndrew Thompson 
6702ac6454SAndrew Thompson #define	UGENSA_BUF_SIZE		2048	/* bytes */
6802ac6454SAndrew Thompson #define	UGENSA_CONFIG_INDEX	0
6902ac6454SAndrew Thompson #define	UGENSA_IFACE_INDEX	0
7002ac6454SAndrew Thompson #define	UGENSA_IFACE_MAX	8	/* exclusivly */
7102ac6454SAndrew Thompson 
7202ac6454SAndrew Thompson enum {
7302ac6454SAndrew Thompson 	UGENSA_BULK_DT_WR,
7402ac6454SAndrew Thompson 	UGENSA_BULK_DT_RD,
7502ac6454SAndrew Thompson 	UGENSA_N_TRANSFER,
7602ac6454SAndrew Thompson };
7702ac6454SAndrew Thompson 
7802ac6454SAndrew Thompson struct ugensa_sub_softc {
79a593f6b8SAndrew Thompson 	struct ucom_softc *sc_ucom_ptr;
80760bc48eSAndrew Thompson 	struct usb_xfer *sc_xfer[UGENSA_N_TRANSFER];
8102ac6454SAndrew Thompson };
8202ac6454SAndrew Thompson 
8302ac6454SAndrew Thompson struct ugensa_softc {
84760bc48eSAndrew Thompson 	struct ucom_super_softc sc_super_ucom;
85760bc48eSAndrew Thompson 	struct ucom_softc sc_ucom[UGENSA_IFACE_MAX];
8602ac6454SAndrew Thompson 	struct ugensa_sub_softc sc_sub[UGENSA_IFACE_MAX];
8702ac6454SAndrew Thompson 
8802ac6454SAndrew Thompson 	struct mtx sc_mtx;
8902ac6454SAndrew Thompson 	uint8_t	sc_niface;
9002ac6454SAndrew Thompson };
9102ac6454SAndrew Thompson 
9202ac6454SAndrew Thompson /* prototypes */
9302ac6454SAndrew Thompson 
9402ac6454SAndrew Thompson static device_probe_t ugensa_probe;
9502ac6454SAndrew Thompson static device_attach_t ugensa_attach;
9602ac6454SAndrew Thompson static device_detach_t ugensa_detach;
97*5805d178SHans Petter Selasky static device_free_softc_t ugensa_free_softc;
9802ac6454SAndrew Thompson 
99e0a69b51SAndrew Thompson static usb_callback_t ugensa_bulk_write_callback;
100e0a69b51SAndrew Thompson static usb_callback_t ugensa_bulk_read_callback;
10102ac6454SAndrew Thompson 
102*5805d178SHans Petter Selasky static void	ugensa_free(struct ucom_softc *);
103760bc48eSAndrew Thompson static void	ugensa_start_read(struct ucom_softc *);
104760bc48eSAndrew Thompson static void	ugensa_stop_read(struct ucom_softc *);
105760bc48eSAndrew Thompson static void	ugensa_start_write(struct ucom_softc *);
106760bc48eSAndrew Thompson static void	ugensa_stop_write(struct ucom_softc *);
107655dc9d0SAndrew Thompson static void	ugensa_poll(struct ucom_softc *ucom);
10802ac6454SAndrew Thompson 
109655dc9d0SAndrew Thompson static const struct usb_config ugensa_xfer_config[UGENSA_N_TRANSFER] = {
11002ac6454SAndrew Thompson 
11102ac6454SAndrew Thompson 	[UGENSA_BULK_DT_WR] = {
11202ac6454SAndrew Thompson 		.type = UE_BULK,
11302ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
11402ac6454SAndrew Thompson 		.direction = UE_DIR_OUT,
1154eae601eSAndrew Thompson 		.bufsize = UGENSA_BUF_SIZE,
1164eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
1174eae601eSAndrew Thompson 		.callback = &ugensa_bulk_write_callback,
11802ac6454SAndrew Thompson 	},
11902ac6454SAndrew Thompson 
12002ac6454SAndrew Thompson 	[UGENSA_BULK_DT_RD] = {
12102ac6454SAndrew Thompson 		.type = UE_BULK,
12202ac6454SAndrew Thompson 		.endpoint = UE_ADDR_ANY,
12302ac6454SAndrew Thompson 		.direction = UE_DIR_IN,
1244eae601eSAndrew Thompson 		.bufsize = UGENSA_BUF_SIZE,
1254eae601eSAndrew Thompson 		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
1264eae601eSAndrew Thompson 		.callback = &ugensa_bulk_read_callback,
12702ac6454SAndrew Thompson 	},
12802ac6454SAndrew Thompson };
12902ac6454SAndrew Thompson 
130760bc48eSAndrew Thompson static const struct ucom_callback ugensa_callback = {
131a593f6b8SAndrew Thompson 	.ucom_start_read = &ugensa_start_read,
132a593f6b8SAndrew Thompson 	.ucom_stop_read = &ugensa_stop_read,
133a593f6b8SAndrew Thompson 	.ucom_start_write = &ugensa_start_write,
134a593f6b8SAndrew Thompson 	.ucom_stop_write = &ugensa_stop_write,
135655dc9d0SAndrew Thompson 	.ucom_poll = &ugensa_poll,
136*5805d178SHans Petter Selasky 	.ucom_free = &ugensa_free,
13702ac6454SAndrew Thompson };
13802ac6454SAndrew Thompson 
13902ac6454SAndrew Thompson static device_method_t ugensa_methods[] = {
14002ac6454SAndrew Thompson 	/* Device methods */
14102ac6454SAndrew Thompson 	DEVMETHOD(device_probe, ugensa_probe),
14202ac6454SAndrew Thompson 	DEVMETHOD(device_attach, ugensa_attach),
14302ac6454SAndrew Thompson 	DEVMETHOD(device_detach, ugensa_detach),
144*5805d178SHans Petter Selasky 	DEVMETHOD(device_free_softc, ugensa_free_softc),
145*5805d178SHans Petter Selasky 	DEVMETHOD_END
14602ac6454SAndrew Thompson };
14702ac6454SAndrew Thompson 
14802ac6454SAndrew Thompson static devclass_t ugensa_devclass;
14902ac6454SAndrew Thompson 
15002ac6454SAndrew Thompson static driver_t ugensa_driver = {
15102ac6454SAndrew Thompson 	.name = "ugensa",
15202ac6454SAndrew Thompson 	.methods = ugensa_methods,
15302ac6454SAndrew Thompson 	.size = sizeof(struct ugensa_softc),
15402ac6454SAndrew Thompson };
15502ac6454SAndrew Thompson 
1569aef556dSAndrew Thompson DRIVER_MODULE(ugensa, uhub, ugensa_driver, ugensa_devclass, NULL, 0);
15702ac6454SAndrew Thompson MODULE_DEPEND(ugensa, ucom, 1, 1, 1);
15802ac6454SAndrew Thompson MODULE_DEPEND(ugensa, usb, 1, 1, 1);
159910cb8feSAndrew Thompson MODULE_VERSION(ugensa, 1);
16002ac6454SAndrew Thompson 
161f1a16106SHans Petter Selasky static const STRUCT_USB_HOST_ID ugensa_devs[] = {
16202ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_AIRPRIME, USB_PRODUCT_AIRPRIME_PC5220, 0)},
16302ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_CMOTECH, USB_PRODUCT_CMOTECH_CDMA_MODEM1, 0)},
16402ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_KYOCERA2, USB_PRODUCT_KYOCERA2_CDMA_MSM_K, 0)},
16502ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_49GPLUS, 0)},
16602ac6454SAndrew Thompson 	{USB_VPI(USB_VENDOR_NOVATEL2, USB_PRODUCT_NOVATEL2_FLEXPACKGPS, 0)},
16702ac6454SAndrew Thompson };
16802ac6454SAndrew Thompson 
16902ac6454SAndrew Thompson static int
17002ac6454SAndrew Thompson ugensa_probe(device_t dev)
17102ac6454SAndrew Thompson {
172760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
17302ac6454SAndrew Thompson 
174f29a0724SAndrew Thompson 	if (uaa->usb_mode != USB_MODE_HOST) {
17502ac6454SAndrew Thompson 		return (ENXIO);
17602ac6454SAndrew Thompson 	}
17702ac6454SAndrew Thompson 	if (uaa->info.bConfigIndex != UGENSA_CONFIG_INDEX) {
17802ac6454SAndrew Thompson 		return (ENXIO);
17902ac6454SAndrew Thompson 	}
18002ac6454SAndrew Thompson 	if (uaa->info.bIfaceIndex != 0) {
18102ac6454SAndrew Thompson 		return (ENXIO);
18202ac6454SAndrew Thompson 	}
183a593f6b8SAndrew Thompson 	return (usbd_lookup_id_by_uaa(ugensa_devs, sizeof(ugensa_devs), uaa));
18402ac6454SAndrew Thompson }
18502ac6454SAndrew Thompson 
18602ac6454SAndrew Thompson static int
18702ac6454SAndrew Thompson ugensa_attach(device_t dev)
18802ac6454SAndrew Thompson {
189760bc48eSAndrew Thompson 	struct usb_attach_arg *uaa = device_get_ivars(dev);
19002ac6454SAndrew Thompson 	struct ugensa_softc *sc = device_get_softc(dev);
19102ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc;
192760bc48eSAndrew Thompson 	struct usb_interface *iface;
19302ac6454SAndrew Thompson 	int32_t error;
19402ac6454SAndrew Thompson 	uint8_t iface_index;
19502ac6454SAndrew Thompson 	int x, cnt;
19602ac6454SAndrew Thompson 
197a593f6b8SAndrew Thompson 	device_set_usb_desc(dev);
19802ac6454SAndrew Thompson 	mtx_init(&sc->sc_mtx, "ugensa", NULL, MTX_DEF);
199*5805d178SHans Petter Selasky 	ucom_ref(&sc->sc_super_ucom);
20002ac6454SAndrew Thompson 
20102ac6454SAndrew Thompson 	/* Figure out how many interfaces this device has got */
20202ac6454SAndrew Thompson 	for (cnt = 0; cnt < UGENSA_IFACE_MAX; cnt++) {
203a593f6b8SAndrew Thompson 		if ((usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 0) == NULL) ||
204a593f6b8SAndrew Thompson 		    (usbd_get_endpoint(uaa->device, cnt, ugensa_xfer_config + 1) == NULL)) {
20502ac6454SAndrew Thompson 			/* we have reached the end */
20602ac6454SAndrew Thompson 			break;
20702ac6454SAndrew Thompson 		}
20802ac6454SAndrew Thompson 	}
20902ac6454SAndrew Thompson 
21002ac6454SAndrew Thompson 	if (cnt == 0) {
211767cb2e2SAndrew Thompson 		device_printf(dev, "No interfaces\n");
21202ac6454SAndrew Thompson 		goto detach;
21302ac6454SAndrew Thompson 	}
21402ac6454SAndrew Thompson 	for (x = 0; x < cnt; x++) {
215a593f6b8SAndrew Thompson 		iface = usbd_get_iface(uaa->device, x);
21602ac6454SAndrew Thompson 		if (iface->idesc->bInterfaceClass != UICLASS_VENDOR)
21702ac6454SAndrew Thompson 			/* Not a serial port, most likely a SD reader */
21802ac6454SAndrew Thompson 			continue;
21902ac6454SAndrew Thompson 
22002ac6454SAndrew Thompson 		ssc = sc->sc_sub + sc->sc_niface;
221a593f6b8SAndrew Thompson 		ssc->sc_ucom_ptr = sc->sc_ucom + sc->sc_niface;
22202ac6454SAndrew Thompson 
22302ac6454SAndrew Thompson 		iface_index = (UGENSA_IFACE_INDEX + x);
224a593f6b8SAndrew Thompson 		error = usbd_transfer_setup(uaa->device,
22502ac6454SAndrew Thompson 		    &iface_index, ssc->sc_xfer, ugensa_xfer_config,
22602ac6454SAndrew Thompson 		    UGENSA_N_TRANSFER, ssc, &sc->sc_mtx);
22702ac6454SAndrew Thompson 
22802ac6454SAndrew Thompson 		if (error) {
22902ac6454SAndrew Thompson 			device_printf(dev, "allocating USB "
230767cb2e2SAndrew Thompson 			    "transfers failed\n");
23102ac6454SAndrew Thompson 			goto detach;
23202ac6454SAndrew Thompson 		}
23302ac6454SAndrew Thompson 		/* clear stall at first run */
234deefe583SAndrew Thompson 		mtx_lock(&sc->sc_mtx);
235ed6d949aSAndrew Thompson 		usbd_xfer_set_stall(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
236ed6d949aSAndrew Thompson 		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 */
240a593f6b8SAndrew Thompson 		ssc->sc_ucom_ptr->sc_portno = sc->sc_niface;
24102ac6454SAndrew Thompson 		sc->sc_niface++;
24202ac6454SAndrew Thompson 		if (x != uaa->info.bIfaceIndex)
243a593f6b8SAndrew Thompson 			usbd_set_parent_iface(uaa->device, x,
24402ac6454SAndrew Thompson 			    uaa->info.bIfaceIndex);
24502ac6454SAndrew Thompson 	}
24602ac6454SAndrew Thompson 	device_printf(dev, "Found %d interfaces.\n", sc->sc_niface);
24702ac6454SAndrew Thompson 
248a593f6b8SAndrew Thompson 	error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom, sc->sc_niface, sc,
24902ac6454SAndrew Thompson 	    &ugensa_callback, &sc->sc_mtx);
25002ac6454SAndrew Thompson 	if (error) {
25102ac6454SAndrew Thompson 		DPRINTF("attach failed\n");
25202ac6454SAndrew Thompson 		goto detach;
25302ac6454SAndrew Thompson 	}
2546416c259SNick Hibma 	ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
2556416c259SNick Hibma 
25602ac6454SAndrew Thompson 	return (0);			/* success */
25702ac6454SAndrew Thompson 
25802ac6454SAndrew Thompson detach:
25902ac6454SAndrew Thompson 	ugensa_detach(dev);
26002ac6454SAndrew Thompson 	return (ENXIO);			/* failure */
26102ac6454SAndrew Thompson }
26202ac6454SAndrew Thompson 
26302ac6454SAndrew Thompson static int
26402ac6454SAndrew Thompson ugensa_detach(device_t dev)
26502ac6454SAndrew Thompson {
26602ac6454SAndrew Thompson 	struct ugensa_softc *sc = device_get_softc(dev);
26702ac6454SAndrew Thompson 	uint8_t x;
26802ac6454SAndrew Thompson 
269015bb88fSNick Hibma 	ucom_detach(&sc->sc_super_ucom, sc->sc_ucom);
27002ac6454SAndrew Thompson 
27102ac6454SAndrew Thompson 	for (x = 0; x < sc->sc_niface; x++) {
272a593f6b8SAndrew Thompson 		usbd_transfer_unsetup(sc->sc_sub[x].sc_xfer, UGENSA_N_TRANSFER);
27302ac6454SAndrew Thompson 	}
27402ac6454SAndrew Thompson 
27502ac6454SAndrew Thompson 	return (0);
27602ac6454SAndrew Thompson }
27702ac6454SAndrew Thompson 
278*5805d178SHans Petter Selasky UCOM_UNLOAD_DRAIN(ugensa);
279*5805d178SHans Petter Selasky 
280*5805d178SHans Petter Selasky static void
281*5805d178SHans Petter Selasky ugensa_free_softc(device_t dev, void *arg)
282*5805d178SHans Petter Selasky {
283*5805d178SHans Petter Selasky 	struct ugensa_softc *sc = arg;
284*5805d178SHans Petter Selasky 
285*5805d178SHans Petter Selasky 	if (ucom_unref(&sc->sc_super_ucom)) {
286*5805d178SHans Petter Selasky 		if (mtx_initialized(&sc->sc_mtx))
287*5805d178SHans Petter Selasky 			mtx_destroy(&sc->sc_mtx);
288*5805d178SHans Petter Selasky 		device_free_softc(dev, sc);
289*5805d178SHans Petter Selasky 	}
290*5805d178SHans Petter Selasky }
291*5805d178SHans Petter Selasky 
292*5805d178SHans Petter Selasky static void
293*5805d178SHans Petter Selasky ugensa_free(struct ucom_softc *ucom)
294*5805d178SHans Petter Selasky {
295*5805d178SHans Petter Selasky 	ugensa_free_softc(NULL, ucom->sc_parent);
296*5805d178SHans Petter Selasky }
297*5805d178SHans Petter Selasky 
29802ac6454SAndrew Thompson static void
299ed6d949aSAndrew Thompson ugensa_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
30002ac6454SAndrew Thompson {
301ed6d949aSAndrew Thompson 	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
302ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
30302ac6454SAndrew Thompson 	uint32_t actlen;
30402ac6454SAndrew Thompson 
30502ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
30602ac6454SAndrew Thompson 	case USB_ST_SETUP:
30702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
30802ac6454SAndrew Thompson tr_setup:
309ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
310ed6d949aSAndrew Thompson 		if (ucom_get_data(ssc->sc_ucom_ptr, pc, 0,
31102ac6454SAndrew Thompson 		    UGENSA_BUF_SIZE, &actlen)) {
312ed6d949aSAndrew Thompson 			usbd_xfer_set_frame_len(xfer, 0, actlen);
313a593f6b8SAndrew Thompson 			usbd_transfer_submit(xfer);
31402ac6454SAndrew Thompson 		}
31502ac6454SAndrew Thompson 		return;
31602ac6454SAndrew Thompson 
31702ac6454SAndrew Thompson 	default:			/* Error */
318ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
31902ac6454SAndrew Thompson 			/* try to clear stall first */
320ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
32102ac6454SAndrew Thompson 			goto tr_setup;
32202ac6454SAndrew Thompson 		}
32302ac6454SAndrew Thompson 		return;
32402ac6454SAndrew Thompson 	}
32502ac6454SAndrew Thompson }
32602ac6454SAndrew Thompson 
32702ac6454SAndrew Thompson static void
328ed6d949aSAndrew Thompson ugensa_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
32902ac6454SAndrew Thompson {
330ed6d949aSAndrew Thompson 	struct ugensa_sub_softc *ssc = usbd_xfer_softc(xfer);
331ed6d949aSAndrew Thompson 	struct usb_page_cache *pc;
332ed6d949aSAndrew Thompson 	int actlen;
333ed6d949aSAndrew Thompson 
334ed6d949aSAndrew Thompson 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
33502ac6454SAndrew Thompson 
33602ac6454SAndrew Thompson 	switch (USB_GET_STATE(xfer)) {
33702ac6454SAndrew Thompson 	case USB_ST_TRANSFERRED:
338ed6d949aSAndrew Thompson 		pc = usbd_xfer_get_frame(xfer, 0);
339ed6d949aSAndrew Thompson 		ucom_put_data(ssc->sc_ucom_ptr, pc, 0, actlen);
34002ac6454SAndrew Thompson 
34102ac6454SAndrew Thompson 	case USB_ST_SETUP:
34202ac6454SAndrew Thompson tr_setup:
343ed6d949aSAndrew Thompson 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
344a593f6b8SAndrew Thompson 		usbd_transfer_submit(xfer);
34502ac6454SAndrew Thompson 		return;
34602ac6454SAndrew Thompson 
34702ac6454SAndrew Thompson 	default:			/* Error */
348ed6d949aSAndrew Thompson 		if (error != USB_ERR_CANCELLED) {
34902ac6454SAndrew Thompson 			/* try to clear stall first */
350ed6d949aSAndrew Thompson 			usbd_xfer_set_stall(xfer);
35102ac6454SAndrew Thompson 			goto tr_setup;
35202ac6454SAndrew Thompson 		}
35302ac6454SAndrew Thompson 		return;
35402ac6454SAndrew Thompson 	}
35502ac6454SAndrew Thompson }
35602ac6454SAndrew Thompson 
35702ac6454SAndrew Thompson static void
358760bc48eSAndrew Thompson ugensa_start_read(struct ucom_softc *ucom)
35902ac6454SAndrew Thompson {
36002ac6454SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
36102ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
36202ac6454SAndrew Thompson 
363a593f6b8SAndrew Thompson 	usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
36402ac6454SAndrew Thompson }
36502ac6454SAndrew Thompson 
36602ac6454SAndrew Thompson static void
367760bc48eSAndrew Thompson ugensa_stop_read(struct ucom_softc *ucom)
36802ac6454SAndrew Thompson {
36902ac6454SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
37002ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
37102ac6454SAndrew Thompson 
372a593f6b8SAndrew Thompson 	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_RD]);
37302ac6454SAndrew Thompson }
37402ac6454SAndrew Thompson 
37502ac6454SAndrew Thompson static void
376760bc48eSAndrew Thompson ugensa_start_write(struct ucom_softc *ucom)
37702ac6454SAndrew Thompson {
37802ac6454SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
37902ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
38002ac6454SAndrew Thompson 
381a593f6b8SAndrew Thompson 	usbd_transfer_start(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
38202ac6454SAndrew Thompson }
38302ac6454SAndrew Thompson 
38402ac6454SAndrew Thompson static void
385760bc48eSAndrew Thompson ugensa_stop_write(struct ucom_softc *ucom)
38602ac6454SAndrew Thompson {
38702ac6454SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
38802ac6454SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
38902ac6454SAndrew Thompson 
390a593f6b8SAndrew Thompson 	usbd_transfer_stop(ssc->sc_xfer[UGENSA_BULK_DT_WR]);
39102ac6454SAndrew Thompson }
392655dc9d0SAndrew Thompson 
393655dc9d0SAndrew Thompson static void
394655dc9d0SAndrew Thompson ugensa_poll(struct ucom_softc *ucom)
395655dc9d0SAndrew Thompson {
396655dc9d0SAndrew Thompson 	struct ugensa_softc *sc = ucom->sc_parent;
397655dc9d0SAndrew Thompson 	struct ugensa_sub_softc *ssc = sc->sc_sub + ucom->sc_portno;
398655dc9d0SAndrew Thompson 
399655dc9d0SAndrew Thompson 	usbd_transfer_poll(ssc->sc_xfer, UGENSA_N_TRANSFER);
400655dc9d0SAndrew Thompson }
401