u3g.c revision 199331
1/*
2 * Copyright (c) 2008 AnyWi Technologies
3 * Author: Andrea Guzzo <aguzzo@anywi.com>
4 * * based on uark.c 1.1 2006/08/14 08:30:22 jsg *
5 * * parts from ubsa.c 183348 2008-09-25 12:00:56Z phk *
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 * $FreeBSD: head/sys/dev/usb/serial/u3g.c 199331 2009-11-16 20:35:16Z thompsa $
20 */
21
22/*
23 * NOTE:
24 *
25 * - The detour through the tty layer is ridiculously expensive wrt
26 *   buffering due to the high speeds.
27 *
28 *   We should consider adding a simple r/w device which allows
29 *   attaching of PPP in a more efficient way.
30 *
31 */
32
33
34#include <sys/stdint.h>
35#include <sys/stddef.h>
36#include <sys/param.h>
37#include <sys/queue.h>
38#include <sys/types.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/bus.h>
42#include <sys/linker_set.h>
43#include <sys/module.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/condvar.h>
47#include <sys/sysctl.h>
48#include <sys/sx.h>
49#include <sys/unistd.h>
50#include <sys/callout.h>
51#include <sys/malloc.h>
52#include <sys/priv.h>
53
54#include <dev/usb/usb.h>
55#include <dev/usb/usbdi.h>
56#include <dev/usb/usbdi_util.h>
57#include "usbdevs.h"
58
59#define	USB_DEBUG_VAR u3g_debug
60#include <dev/usb/usb_debug.h>
61#include <dev/usb/usb_process.h>
62#include <dev/usb/usb_dynamic.h>
63#include <dev/usb/usb_msctest.h>
64#include <dev/usb/usb_device.h>
65
66#include <dev/usb/serial/usb_serial.h>
67
68#if USB_DEBUG
69static int u3g_debug = 0;
70
71SYSCTL_NODE(_hw_usb, OID_AUTO, u3g, CTLFLAG_RW, 0, "USB 3g");
72SYSCTL_INT(_hw_usb_u3g, OID_AUTO, debug, CTLFLAG_RW,
73    &u3g_debug, 0, "Debug level");
74#endif
75
76#define	U3G_MAXPORTS		8
77#define	U3G_CONFIG_INDEX	0
78#define	U3G_BSIZE		2048
79
80#define	U3GSP_GPRS		0
81#define	U3GSP_EDGE		1
82#define	U3GSP_CDMA		2
83#define	U3GSP_UMTS		3
84#define	U3GSP_HSDPA		4
85#define	U3GSP_HSUPA		5
86#define	U3GSP_HSPA		6
87#define	U3GSP_MAX		7
88
89#define	U3GFL_HUAWEI_INIT	0x0001	/* Init command required */
90#define	U3GFL_SCSI_EJECT	0x0002	/* SCSI eject command required */
91#define	U3GFL_SIERRA_INIT	0x0004	/* Init command required */
92#define	U3GFL_SAEL_M460_INIT	0x0008	/* Init device */
93
94enum {
95	U3G_BULK_WR,
96	U3G_BULK_RD,
97	U3G_N_TRANSFER,
98};
99
100struct u3g_softc {
101	struct ucom_super_softc sc_super_ucom;
102	struct ucom_softc sc_ucom[U3G_MAXPORTS];
103
104	struct usb_xfer *sc_xfer[U3G_MAXPORTS][U3G_N_TRANSFER];
105	struct usb_device *sc_udev;
106	struct mtx sc_mtx;
107
108	uint8_t	sc_lsr;			/* local status register */
109	uint8_t	sc_msr;			/* U3G status register */
110	uint8_t	sc_numports;
111};
112
113static device_probe_t u3g_probe;
114static device_attach_t u3g_attach;
115static device_detach_t u3g_detach;
116
117static usb_callback_t u3g_write_callback;
118static usb_callback_t u3g_read_callback;
119
120static void u3g_start_read(struct ucom_softc *ucom);
121static void u3g_stop_read(struct ucom_softc *ucom);
122static void u3g_start_write(struct ucom_softc *ucom);
123static void u3g_stop_write(struct ucom_softc *ucom);
124
125static int u3g_driver_loaded(struct module *mod, int what, void *arg);
126
127static const struct usb_config u3g_config[U3G_N_TRANSFER] = {
128
129	[U3G_BULK_WR] = {
130		.type = UE_BULK,
131		.endpoint = UE_ADDR_ANY,
132		.direction = UE_DIR_OUT,
133		.bufsize = U3G_BSIZE,/* bytes */
134		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
135		.callback = &u3g_write_callback,
136	},
137
138	[U3G_BULK_RD] = {
139		.type = UE_BULK,
140		.endpoint = UE_ADDR_ANY,
141		.direction = UE_DIR_IN,
142		.bufsize = U3G_BSIZE,/* bytes */
143		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
144		.callback = &u3g_read_callback,
145	},
146};
147
148static const struct ucom_callback u3g_callback = {
149	.ucom_start_read = &u3g_start_read,
150	.ucom_stop_read = &u3g_stop_read,
151	.ucom_start_write = &u3g_start_write,
152	.ucom_stop_write = &u3g_stop_write,
153};
154
155static device_method_t u3g_methods[] = {
156	DEVMETHOD(device_probe, u3g_probe),
157	DEVMETHOD(device_attach, u3g_attach),
158	DEVMETHOD(device_detach, u3g_detach),
159	{0, 0}
160};
161
162static devclass_t u3g_devclass;
163
164static driver_t u3g_driver = {
165	.name = "u3g",
166	.methods = u3g_methods,
167	.size = sizeof(struct u3g_softc),
168};
169
170DRIVER_MODULE(u3g, uhub, u3g_driver, u3g_devclass, u3g_driver_loaded, 0);
171MODULE_DEPEND(u3g, ucom, 1, 1, 1);
172MODULE_DEPEND(u3g, usb, 1, 1, 1);
173
174static const struct usb_device_id u3g_devs[] = {
175#define	U3G_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
176	U3G_DEV(CURITEL, UM175, 0),
177	/* OEM: Option */
178	U3G_DEV(OPTION, GT3G, 0),
179	U3G_DEV(OPTION, GT3GQUAD, 0),
180	U3G_DEV(OPTION, GT3GPLUS, 0),
181	U3G_DEV(OPTION, GTMAX36, 0),
182	U3G_DEV(OPTION, GTHSDPA, 0),
183	U3G_DEV(OPTION, GTMAXHSUPA, 0),
184	U3G_DEV(OPTION, GTMAXHSUPAE, 0),
185	U3G_DEV(OPTION, GTMAX380HSUPAE, 0),
186	U3G_DEV(OPTION, VODAFONEMC3G, 0),
187	/* OEM: Qualcomm, Inc. */
188	U3G_DEV(QUALCOMMINC, ZTE_STOR, U3GFL_SCSI_EJECT),
189	U3G_DEV(QUALCOMMINC, CDMA_MSM, U3GFL_SCSI_EJECT),
190	/* OEM: Huawei */
191	U3G_DEV(HUAWEI, MOBILE, U3GFL_HUAWEI_INIT),
192	U3G_DEV(HUAWEI, E180V, U3GFL_HUAWEI_INIT),
193	U3G_DEV(HUAWEI, E220, U3GFL_HUAWEI_INIT),
194	/* OEM: Novatel */
195	U3G_DEV(NOVATEL, CDMA_MODEM, 0),
196	U3G_DEV(NOVATEL, ES620, 0),
197	U3G_DEV(NOVATEL, MC950D, 0),
198	U3G_DEV(NOVATEL, U720, 0),
199	U3G_DEV(NOVATEL, U727, 0),
200	U3G_DEV(NOVATEL, U740, 0),
201	U3G_DEV(NOVATEL, U740_2, 0),
202	U3G_DEV(NOVATEL, U870, 0),
203	U3G_DEV(NOVATEL, V620, 0),
204	U3G_DEV(NOVATEL, V640, 0),
205	U3G_DEV(NOVATEL, V720, 0),
206	U3G_DEV(NOVATEL, V740, 0),
207	U3G_DEV(NOVATEL, X950D, 0),
208	U3G_DEV(NOVATEL, XU870, 0),
209	U3G_DEV(NOVATEL, ZEROCD, U3GFL_SCSI_EJECT),
210	U3G_DEV(NOVATEL, U760, U3GFL_SCSI_EJECT),
211	U3G_DEV(DELL, U740, 0),
212	/* OEM: Merlin */
213	U3G_DEV(MERLIN, V620, 0),
214	/* OEM: Sierra Wireless: */
215	U3G_DEV(SIERRA, AIRCARD580, 0),
216	U3G_DEV(SIERRA, AIRCARD595, 0),
217	U3G_DEV(SIERRA, AC595U, 0),
218	U3G_DEV(SIERRA, AC597E, 0),
219	U3G_DEV(SIERRA, C597, 0),
220	U3G_DEV(SIERRA, AC880, 0),
221	U3G_DEV(SIERRA, AC880E, 0),
222	U3G_DEV(SIERRA, AC880U, 0),
223	U3G_DEV(SIERRA, AC881, 0),
224	U3G_DEV(SIERRA, AC881E, 0),
225	U3G_DEV(SIERRA, AC881U, 0),
226	U3G_DEV(SIERRA, AC885U, 0),
227	U3G_DEV(SIERRA, EM5625, 0),
228	U3G_DEV(SIERRA, MC5720, 0),
229	U3G_DEV(SIERRA, MC5720_2, 0),
230	U3G_DEV(SIERRA, MC5725, 0),
231	U3G_DEV(SIERRA, MINI5725, 0),
232	U3G_DEV(SIERRA, AIRCARD875, 0),
233	U3G_DEV(SIERRA, MC8755, 0),
234	U3G_DEV(SIERRA, MC8755_2, 0),
235	U3G_DEV(SIERRA, MC8755_3, 0),
236	U3G_DEV(SIERRA, MC8765, 0),
237	U3G_DEV(SIERRA, AC875U, 0),
238	U3G_DEV(SIERRA, MC8775_2, 0),
239	U3G_DEV(SIERRA, MC8780, 0),
240	U3G_DEV(SIERRA, MC8781, 0),
241	U3G_DEV(HP, HS2300, 0),
242	/* Sierra TruInstaller device ID */
243	U3G_DEV(SIERRA, TRUINSTALL, U3GFL_SIERRA_INIT),
244	/* PRUEBA SILABS */
245	U3G_DEV(SILABS, SAEL, U3GFL_SAEL_M460_INIT),
246};
247
248static void
249u3g_sierra_init(struct usb_device *udev)
250{
251	struct usb_device_request req;
252
253	DPRINTFN(0, "\n");
254
255	req.bmRequestType = UT_VENDOR;
256	req.bRequest = UR_SET_INTERFACE;
257	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
258	USETW(req.wIndex, UHF_PORT_CONNECTION);
259	USETW(req.wLength, 0);
260
261	if (usbd_do_request_flags(udev, NULL, &req,
262	    NULL, 0, NULL, USB_MS_HZ)) {
263		/* ignore any errors */
264	}
265	return;
266}
267
268static void
269u3g_huawei_init(struct usb_device *udev)
270{
271	struct usb_device_request req;
272
273	DPRINTFN(0, "\n");
274
275	req.bmRequestType = UT_WRITE_DEVICE;
276	req.bRequest = UR_SET_FEATURE;
277	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
278	USETW(req.wIndex, UHF_PORT_SUSPEND);
279	USETW(req.wLength, 0);
280
281	if (usbd_do_request_flags(udev, NULL, &req,
282	    NULL, 0, NULL, USB_MS_HZ)) {
283		/* ignore any errors */
284	}
285	return;
286}
287
288static void
289u3g_sael_m460_init(struct usb_device *udev)
290{
291	static const uint8_t setup[][24] = {
292	     { 0x41, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
293	     { 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 },
294	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
295	       0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
296	       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
297	     { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02 },
298	     { 0xc1, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 },
299	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
300	     { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 },
301	     { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
302	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
303	     { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
304	     { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
305	       0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
306	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
307	       0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
308	       0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
309	     { 0x41, 0x12, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 },
310	     { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
311	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
312	     { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
313	     { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
314	       0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
315	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
316	       0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
317	       0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
318	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
319	};
320
321	struct usb_device_request req;
322	usb_error_t err;
323	uint16_t len;
324	uint8_t buf[0x300];
325	uint8_t n;
326
327	DPRINTFN(1, "\n");
328
329	if (usbd_req_set_alt_interface_no(udev, NULL, 0, 0)) {
330		DPRINTFN(0, "Alt setting 0 failed\n");
331		return;
332	}
333
334	for (n = 0; n != (sizeof(setup)/sizeof(setup[0])); n++) {
335
336		memcpy(&req, setup[n], sizeof(req));
337
338		len = UGETW(req.wLength);
339		if (req.bmRequestType & UE_DIR_IN) {
340			if (len > sizeof(buf)) {
341				DPRINTFN(0, "too small buffer\n");
342				continue;
343			}
344			err = usbd_do_request(udev, NULL, &req, buf);
345		} else {
346			if (len > (sizeof(setup[0]) - 8)) {
347				DPRINTFN(0, "too small buffer\n");
348				continue;
349			}
350			err = usbd_do_request(udev, NULL, &req,
351			    __DECONST(uint8_t *, &setup[n][8]));
352		}
353		if (err) {
354			DPRINTFN(1, "request %u failed\n",
355			    (unsigned int)n);
356			/*
357			 * Some of the requests will fail. Stop doing
358			 * requests when we are getting timeouts so
359			 * that we don't block the explore/attach
360			 * thread forever.
361			 */
362			if (err == USB_ERR_TIMEOUT)
363				break;
364		}
365	}
366}
367
368static int
369u3g_lookup_huawei(struct usb_attach_arg *uaa)
370{
371	/* Calling the lookup function will also set the driver info! */
372	return (usbd_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa));
373}
374
375/*
376 * The following function handles 3G modem devices (E220, Mobile,
377 * etc.) with auto-install flash disks for Windows/MacOSX on the first
378 * interface.  After some command or some delay they change appearance
379 * to a modem.
380 */
381static usb_error_t
382u3g_test_huawei_autoinst(struct usb_device *udev,
383    struct usb_attach_arg *uaa)
384{
385	struct usb_interface *iface;
386	struct usb_interface_descriptor *id;
387	uint32_t flags;
388
389	if (udev == NULL) {
390		return (USB_ERR_INVAL);
391	}
392	iface = usbd_get_iface(udev, 0);
393	if (iface == NULL) {
394		return (USB_ERR_INVAL);
395	}
396	id = iface->idesc;
397	if (id == NULL) {
398		return (USB_ERR_INVAL);
399	}
400	if (id->bInterfaceClass != UICLASS_MASS) {
401		return (USB_ERR_INVAL);
402	}
403	if (u3g_lookup_huawei(uaa)) {
404		/* no device match */
405		return (USB_ERR_INVAL);
406	}
407	flags = USB_GET_DRIVER_INFO(uaa);
408
409	if (flags & U3GFL_HUAWEI_INIT) {
410		u3g_huawei_init(udev);
411	} else if (flags & U3GFL_SCSI_EJECT) {
412		return (usb_test_autoinstall(udev, 0, 1));
413	} else if (flags & U3GFL_SIERRA_INIT) {
414		u3g_sierra_init(udev);
415	} else {
416		/* no quirks */
417		return (USB_ERR_INVAL);
418	}
419	return (0);			/* success */
420}
421
422static int
423u3g_driver_loaded(struct module *mod, int what, void *arg)
424{
425	switch (what) {
426	case MOD_LOAD:
427		/* register our autoinstall handler */
428		usb_test_huawei_autoinst_p = &u3g_test_huawei_autoinst;
429		break;
430	case MOD_UNLOAD:
431		usb_test_huawei_unload(NULL);
432		break;
433	default:
434		return (EOPNOTSUPP);
435	}
436 	return (0);
437}
438
439static int
440u3g_probe(device_t self)
441{
442	struct usb_attach_arg *uaa = device_get_ivars(self);
443
444	if (uaa->usb_mode != USB_MODE_HOST) {
445		return (ENXIO);
446	}
447	if (uaa->info.bConfigIndex != U3G_CONFIG_INDEX) {
448		return (ENXIO);
449	}
450	if (uaa->info.bInterfaceClass != UICLASS_VENDOR) {
451		return (ENXIO);
452	}
453	return (u3g_lookup_huawei(uaa));
454}
455
456static int
457u3g_attach(device_t dev)
458{
459	struct usb_config u3g_config_tmp[U3G_N_TRANSFER];
460	struct usb_attach_arg *uaa = device_get_ivars(dev);
461	struct u3g_softc *sc = device_get_softc(dev);
462	struct usb_interface *iface;
463	struct usb_interface_descriptor *id;
464	uint32_t iface_valid;
465	int error, flags, nports;
466	int ep, n;
467	uint8_t i;
468
469	DPRINTF("sc=%p\n", sc);
470
471	flags = USB_GET_DRIVER_INFO(uaa);
472
473	if (flags & U3GFL_SAEL_M460_INIT)
474		u3g_sael_m460_init(uaa->device);
475
476	/* copy in USB config */
477	for (n = 0; n != U3G_N_TRANSFER; n++)
478		u3g_config_tmp[n] = u3g_config[n];
479
480	device_set_usb_desc(dev);
481	mtx_init(&sc->sc_mtx, "u3g", NULL, MTX_DEF);
482
483	sc->sc_udev = uaa->device;
484
485	/* Claim all interfaces on the device */
486	iface_valid = 0;
487	for (i = uaa->info.bIfaceIndex; i < USB_IFACE_MAX; i++) {
488		iface = usbd_get_iface(uaa->device, i);
489		if (iface == NULL)
490			break;
491		id = usbd_get_interface_descriptor(iface);
492		if (id == NULL || id->bInterfaceClass != UICLASS_VENDOR)
493			continue;
494		usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
495		iface_valid |= (1<<i);
496	}
497
498	i = 0;		/* interface index */
499	ep = 0;		/* endpoint index */
500	nports = 0;	/* number of ports */
501	while (i < USB_IFACE_MAX) {
502		if ((iface_valid & (1<<i)) == 0) {
503			i++;
504			continue;
505		}
506
507		/* update BULK endpoint index */
508		for (n = 0; n < U3G_N_TRANSFER; n++)
509			u3g_config_tmp[n].ep_index = ep;
510
511		/* try to allocate a set of BULK endpoints */
512		error = usbd_transfer_setup(uaa->device, &i,
513		    sc->sc_xfer[nports], u3g_config_tmp, U3G_N_TRANSFER,
514		    &sc->sc_ucom[nports], &sc->sc_mtx);
515		if (error) {
516			/* next interface */
517			i++;
518			ep = 0;
519			continue;
520		}
521
522		/* set stall by default */
523		mtx_lock(&sc->sc_mtx);
524		usbd_xfer_set_stall(sc->sc_xfer[nports][U3G_BULK_WR]);
525		usbd_xfer_set_stall(sc->sc_xfer[nports][U3G_BULK_RD]);
526		mtx_unlock(&sc->sc_mtx);
527
528		nports++;	/* found one port */
529		ep++;
530		if (nports == U3G_MAXPORTS)
531			break;
532	}
533	if (nports == 0) {
534		device_printf(dev, "no ports found\n");
535		goto detach;
536	}
537	sc->sc_numports = nports;
538
539	error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
540	    sc->sc_numports, sc, &u3g_callback, &sc->sc_mtx);
541	if (error) {
542		DPRINTF("ucom_attach failed\n");
543		goto detach;
544	}
545	if (sc->sc_numports > 1)
546		device_printf(dev, "Found %u ports.\n", sc->sc_numports);
547	return (0);
548
549detach:
550	u3g_detach(dev);
551	return (ENXIO);
552}
553
554static int
555u3g_detach(device_t dev)
556{
557	struct u3g_softc *sc = device_get_softc(dev);
558	uint8_t m;
559
560	DPRINTF("sc=%p\n", sc);
561
562	/* NOTE: It is not dangerous to detach more ports than attached! */
563	ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, U3G_MAXPORTS);
564
565	for (m = 0; m != U3G_MAXPORTS; m++)
566		usbd_transfer_unsetup(sc->sc_xfer[m], U3G_N_TRANSFER);
567	mtx_destroy(&sc->sc_mtx);
568
569	return (0);
570}
571
572static void
573u3g_start_read(struct ucom_softc *ucom)
574{
575	struct u3g_softc *sc = ucom->sc_parent;
576
577	/* start read endpoint */
578	usbd_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
579	return;
580}
581
582static void
583u3g_stop_read(struct ucom_softc *ucom)
584{
585	struct u3g_softc *sc = ucom->sc_parent;
586
587	/* stop read endpoint */
588	usbd_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
589	return;
590}
591
592static void
593u3g_start_write(struct ucom_softc *ucom)
594{
595	struct u3g_softc *sc = ucom->sc_parent;
596
597	usbd_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
598	return;
599}
600
601static void
602u3g_stop_write(struct ucom_softc *ucom)
603{
604	struct u3g_softc *sc = ucom->sc_parent;
605
606	usbd_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
607	return;
608}
609
610static void
611u3g_write_callback(struct usb_xfer *xfer, usb_error_t error)
612{
613	struct ucom_softc *ucom = usbd_xfer_softc(xfer);
614	struct usb_page_cache *pc;
615	uint32_t actlen;
616
617	switch (USB_GET_STATE(xfer)) {
618	case USB_ST_TRANSFERRED:
619	case USB_ST_SETUP:
620tr_setup:
621		pc = usbd_xfer_get_frame(xfer, 0);
622		if (ucom_get_data(ucom, pc, 0, U3G_BSIZE, &actlen)) {
623			usbd_xfer_set_frame_len(xfer, 0, actlen);
624			usbd_transfer_submit(xfer);
625		}
626		break;
627
628	default:			/* Error */
629		if (error != USB_ERR_CANCELLED) {
630			/* do a builtin clear-stall */
631			usbd_xfer_set_stall(xfer);
632			goto tr_setup;
633		}
634		break;
635	}
636	return;
637}
638
639static void
640u3g_read_callback(struct usb_xfer *xfer, usb_error_t error)
641{
642	struct ucom_softc *ucom = usbd_xfer_softc(xfer);
643	struct usb_page_cache *pc;
644	int actlen;
645
646	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
647
648	switch (USB_GET_STATE(xfer)) {
649	case USB_ST_TRANSFERRED:
650		pc = usbd_xfer_get_frame(xfer, 0);
651		ucom_put_data(ucom, pc, 0, actlen);
652
653	case USB_ST_SETUP:
654tr_setup:
655		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
656		usbd_transfer_submit(xfer);
657		break;
658
659	default:			/* Error */
660		if (error != USB_ERR_CANCELLED) {
661			/* do a builtin clear-stall */
662			usbd_xfer_set_stall(xfer);
663			goto tr_setup;
664		}
665		break;
666	}
667	return;
668}
669