u3g.c revision 191868
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 191868 2009-05-07 02:13:56Z 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#include "usbdevs.h"
34#include <dev/usb/usb.h>
35#include <dev/usb/usb_mfunc.h>
36#include <dev/usb/usb_error.h>
37
38#define	USB_DEBUG_VAR u3g_debug
39
40#include <dev/usb/usb_core.h>
41#include <dev/usb/usb_debug.h>
42#include <dev/usb/usb_process.h>
43#include <dev/usb/usb_request.h>
44#include <dev/usb/usb_lookup.h>
45#include <dev/usb/usb_util.h>
46#include <dev/usb/usb_busdma.h>
47#include <dev/usb/usb_msctest.h>
48#include <dev/usb/usb_dynamic.h>
49#include <dev/usb/usb_device.h>
50
51#include <dev/usb/serial/usb_serial.h>
52
53#if USB_DEBUG
54static int u3g_debug = 0;
55
56SYSCTL_NODE(_hw_usb2, OID_AUTO, u3g, CTLFLAG_RW, 0, "USB 3g");
57SYSCTL_INT(_hw_usb2_u3g, OID_AUTO, debug, CTLFLAG_RW,
58    &u3g_debug, 0, "Debug level");
59#endif
60
61#define	U3G_MAXPORTS		4
62#define	U3G_CONFIG_INDEX	0
63#define	U3G_BSIZE		2048
64
65#define	U3GSP_GPRS		0
66#define	U3GSP_EDGE		1
67#define	U3GSP_CDMA		2
68#define	U3GSP_UMTS		3
69#define	U3GSP_HSDPA		4
70#define	U3GSP_HSUPA		5
71#define	U3GSP_HSPA		6
72#define	U3GSP_MAX		7
73
74#define	U3GFL_HUAWEI_INIT	0x0001	/* Init command required */
75#define	U3GFL_SCSI_EJECT	0x0002	/* SCSI eject command required */
76#define	U3GFL_SIERRA_INIT	0x0004	/* Init command required */
77#define	U3GFL_SAEL_M460_INIT	0x0008	/* Init device */
78
79enum {
80	U3G_BULK_WR,
81	U3G_BULK_RD,
82	U3G_N_TRANSFER,
83};
84
85struct u3g_softc {
86	struct usb2_com_super_softc sc_super_ucom;
87	struct usb2_com_softc sc_ucom[U3G_MAXPORTS];
88
89	struct usb2_xfer *sc_xfer[U3G_MAXPORTS][U3G_N_TRANSFER];
90	struct usb2_device *sc_udev;
91	struct mtx sc_mtx;
92
93	uint8_t	sc_lsr;			/* local status register */
94	uint8_t	sc_msr;			/* U3G status register */
95	uint8_t	sc_numports;
96};
97
98static device_probe_t u3g_probe;
99static device_attach_t u3g_attach;
100static device_detach_t u3g_detach;
101
102static usb2_callback_t u3g_write_callback;
103static usb2_callback_t u3g_read_callback;
104
105static void u3g_start_read(struct usb2_com_softc *ucom);
106static void u3g_stop_read(struct usb2_com_softc *ucom);
107static void u3g_start_write(struct usb2_com_softc *ucom);
108static void u3g_stop_write(struct usb2_com_softc *ucom);
109
110static int u3g_driver_loaded(struct module *mod, int what, void *arg);
111
112static const struct usb2_config u3g_config[U3G_N_TRANSFER] = {
113
114	[U3G_BULK_WR] = {
115		.type = UE_BULK,
116		.endpoint = UE_ADDR_ANY,
117		.direction = UE_DIR_OUT,
118		.bufsize = U3G_BSIZE,/* bytes */
119		.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
120		.callback = &u3g_write_callback,
121	},
122
123	[U3G_BULK_RD] = {
124		.type = UE_BULK,
125		.endpoint = UE_ADDR_ANY,
126		.direction = UE_DIR_IN,
127		.bufsize = U3G_BSIZE,/* bytes */
128		.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
129		.callback = &u3g_read_callback,
130	},
131};
132
133static const struct usb2_com_callback u3g_callback = {
134	.usb2_com_start_read = &u3g_start_read,
135	.usb2_com_stop_read = &u3g_stop_read,
136	.usb2_com_start_write = &u3g_start_write,
137	.usb2_com_stop_write = &u3g_stop_write,
138};
139
140static device_method_t u3g_methods[] = {
141	DEVMETHOD(device_probe, u3g_probe),
142	DEVMETHOD(device_attach, u3g_attach),
143	DEVMETHOD(device_detach, u3g_detach),
144	{0, 0}
145};
146
147static devclass_t u3g_devclass;
148
149static driver_t u3g_driver = {
150	.name = "u3g",
151	.methods = u3g_methods,
152	.size = sizeof(struct u3g_softc),
153};
154
155DRIVER_MODULE(u3g, uhub, u3g_driver, u3g_devclass, u3g_driver_loaded, 0);
156MODULE_DEPEND(u3g, ucom, 1, 1, 1);
157MODULE_DEPEND(u3g, usb, 1, 1, 1);
158
159static const struct usb2_device_id u3g_devs[] = {
160#define	U3G_DEV(v,p,i) { USB_VPI(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, i) }
161	/* OEM: Option */
162	U3G_DEV(OPTION, GT3G, 0),
163	U3G_DEV(OPTION, GT3GQUAD, 0),
164	U3G_DEV(OPTION, GT3GPLUS, 0),
165	U3G_DEV(OPTION, GTMAX36, 0),
166	U3G_DEV(OPTION, GTMAXHSUPA, 0),
167	U3G_DEV(OPTION, VODAFONEMC3G, 0),
168	/* OEM: Qualcomm, Inc. */
169	U3G_DEV(QUALCOMMINC, ZTE_STOR, U3GFL_SCSI_EJECT),
170	U3G_DEV(QUALCOMMINC, CDMA_MSM, U3GFL_SCSI_EJECT),
171	/* OEM: Huawei */
172	U3G_DEV(HUAWEI, MOBILE, U3GFL_HUAWEI_INIT),
173	U3G_DEV(HUAWEI, E220, U3GFL_HUAWEI_INIT),
174	/* OEM: Novatel */
175	U3G_DEV(NOVATEL, CDMA_MODEM, 0),
176	U3G_DEV(NOVATEL, ES620, 0),
177	U3G_DEV(NOVATEL, MC950D, 0),
178	U3G_DEV(NOVATEL, U720, 0),
179	U3G_DEV(NOVATEL, U727, 0),
180	U3G_DEV(NOVATEL, U740, 0),
181	U3G_DEV(NOVATEL, U740_2, 0),
182	U3G_DEV(NOVATEL, U870, 0),
183	U3G_DEV(NOVATEL, V620, 0),
184	U3G_DEV(NOVATEL, V640, 0),
185	U3G_DEV(NOVATEL, V720, 0),
186	U3G_DEV(NOVATEL, V740, 0),
187	U3G_DEV(NOVATEL, X950D, 0),
188	U3G_DEV(NOVATEL, XU870, 0),
189	U3G_DEV(NOVATEL, ZEROCD, U3GFL_SCSI_EJECT),
190	U3G_DEV(DELL, U740, 0),
191	/* OEM: Merlin */
192	U3G_DEV(MERLIN, V620, 0),
193	/* OEM: Sierra Wireless: */
194	U3G_DEV(SIERRA, AIRCARD580, 0),
195	U3G_DEV(SIERRA, AIRCARD595, 0),
196	U3G_DEV(SIERRA, AC595U, 0),
197	U3G_DEV(SIERRA, AC597E, 0),
198	U3G_DEV(SIERRA, C597, 0),
199	U3G_DEV(SIERRA, AC880, 0),
200	U3G_DEV(SIERRA, AC880E, 0),
201	U3G_DEV(SIERRA, AC880U, 0),
202	U3G_DEV(SIERRA, AC881, 0),
203	U3G_DEV(SIERRA, AC881E, 0),
204	U3G_DEV(SIERRA, AC881U, 0),
205	U3G_DEV(SIERRA, EM5625, 0),
206	U3G_DEV(SIERRA, MC5720, 0),
207	U3G_DEV(SIERRA, MC5720_2, 0),
208	U3G_DEV(SIERRA, MC5725, 0),
209	U3G_DEV(SIERRA, MINI5725, 0),
210	U3G_DEV(SIERRA, AIRCARD875, 0),
211	U3G_DEV(SIERRA, MC8755, 0),
212	U3G_DEV(SIERRA, MC8755_2, 0),
213	U3G_DEV(SIERRA, MC8755_3, 0),
214	U3G_DEV(SIERRA, MC8765, 0),
215	U3G_DEV(SIERRA, AC875U, 0),
216	U3G_DEV(SIERRA, MC8775_2, 0),
217	U3G_DEV(SIERRA, MC8780, 0),
218	U3G_DEV(SIERRA, MC8781, 0),
219	U3G_DEV(HP, HS2300, 0),
220	/* Sierra TruInstaller device ID */
221	U3G_DEV(SIERRA, TRUINSTALL, U3GFL_SIERRA_INIT),
222	/* PRUEBA SILABS */
223	U3G_DEV(SILABS, SAEL, U3GFL_SAEL_M460_INIT),
224};
225
226static void
227u3g_sierra_init(struct usb2_device *udev)
228{
229	struct usb2_device_request req;
230
231	DPRINTFN(0, "\n");
232
233	req.bmRequestType = UT_VENDOR;
234	req.bRequest = UR_SET_INTERFACE;
235	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
236	USETW(req.wIndex, UHF_PORT_CONNECTION);
237	USETW(req.wLength, 0);
238
239	if (usb2_do_request_flags(udev, NULL, &req,
240	    NULL, 0, NULL, USB_MS_HZ)) {
241		/* ignore any errors */
242	}
243	return;
244}
245
246static void
247u3g_huawei_init(struct usb2_device *udev)
248{
249	struct usb2_device_request req;
250
251	DPRINTFN(0, "\n");
252
253	req.bmRequestType = UT_WRITE_DEVICE;
254	req.bRequest = UR_SET_FEATURE;
255	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
256	USETW(req.wIndex, UHF_PORT_SUSPEND);
257	USETW(req.wLength, 0);
258
259	if (usb2_do_request_flags(udev, NULL, &req,
260	    NULL, 0, NULL, USB_MS_HZ)) {
261		/* ignore any errors */
262	}
263	return;
264}
265
266static void
267u3g_sael_m460_init(struct usb2_device *udev)
268{
269	static const uint8_t setup[][24] = {
270	     { 0x41, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
271	     { 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 },
272	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
273	       0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
274	       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
275	     { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02 },
276	     { 0xc1, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 },
277	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
278	     { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 },
279	     { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
280	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
281	     { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
282	     { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
283	       0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
284	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
285	       0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
286	       0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
287	     { 0x41, 0x12, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 },
288	     { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
289	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
290	     { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
291	     { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
292	       0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
293	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
294	       0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
295	       0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
296	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
297	};
298
299	struct usb2_device_request req;
300	usb2_error_t err;
301	uint16_t len;
302	uint8_t buf[0x300];
303	uint8_t n;
304
305	DPRINTFN(1, "\n");
306
307	if (usb2_req_set_alt_interface_no(udev, NULL, 0, 0)) {
308		DPRINTFN(0, "Alt setting 0 failed\n");
309		return;
310	}
311
312	for (n = 0; n != (sizeof(setup)/sizeof(setup[0])); n++) {
313
314		memcpy(&req, setup[n], sizeof(req));
315
316		len = UGETW(req.wLength);
317		if (req.bmRequestType & UE_DIR_IN) {
318			if (len > sizeof(buf)) {
319				DPRINTFN(0, "too small buffer\n");
320				continue;
321			}
322			err = usb2_do_request(udev, NULL, &req, buf);
323		} else {
324			if (len > (sizeof(setup[0]) - 8)) {
325				DPRINTFN(0, "too small buffer\n");
326				continue;
327			}
328			err = usb2_do_request(udev, NULL, &req,
329			    __DECONST(uint8_t *, &setup[n][8]));
330		}
331		if (err) {
332			DPRINTFN(1, "request %u failed\n",
333			    (unsigned int)n);
334			/*
335			 * Some of the requests will fail. Stop doing
336			 * requests when we are getting timeouts so
337			 * that we don't block the explore/attach
338			 * thread forever.
339			 */
340			if (err == USB_ERR_TIMEOUT)
341				break;
342		}
343	}
344}
345
346static int
347u3g_lookup_huawei(struct usb2_attach_arg *uaa)
348{
349	/* Calling the lookup function will also set the driver info! */
350	return (usb2_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa));
351}
352
353/*
354 * The following function handles 3G modem devices (E220, Mobile,
355 * etc.) with auto-install flash disks for Windows/MacOSX on the first
356 * interface.  After some command or some delay they change appearance
357 * to a modem.
358 */
359static usb2_error_t
360u3g_test_huawei_autoinst(struct usb2_device *udev,
361    struct usb2_attach_arg *uaa)
362{
363	struct usb2_interface *iface;
364	struct usb2_interface_descriptor *id;
365	uint32_t flags;
366
367	if (udev == NULL) {
368		return (USB_ERR_INVAL);
369	}
370	iface = usb2_get_iface(udev, 0);
371	if (iface == NULL) {
372		return (USB_ERR_INVAL);
373	}
374	id = iface->idesc;
375	if (id == NULL) {
376		return (USB_ERR_INVAL);
377	}
378	if (id->bInterfaceClass != UICLASS_MASS) {
379		return (USB_ERR_INVAL);
380	}
381	if (u3g_lookup_huawei(uaa)) {
382		/* no device match */
383		return (USB_ERR_INVAL);
384	}
385	flags = USB_GET_DRIVER_INFO(uaa);
386
387	if (flags & U3GFL_HUAWEI_INIT) {
388		u3g_huawei_init(udev);
389	} else if (flags & U3GFL_SCSI_EJECT) {
390		return (usb2_test_autoinstall(udev, 0, 1));
391	} else if (flags & U3GFL_SIERRA_INIT) {
392		u3g_sierra_init(udev);
393	} else {
394		/* no quirks */
395		return (USB_ERR_INVAL);
396	}
397	return (0);			/* success */
398}
399
400static int
401u3g_driver_loaded(struct module *mod, int what, void *arg)
402{
403	switch (what) {
404	case MOD_LOAD:
405		/* register our autoinstall handler */
406		usb2_test_huawei_autoinst_p = &u3g_test_huawei_autoinst;
407		break;
408	case MOD_UNLOAD:
409		usb2_test_huawei_unload(NULL);
410		break;
411	default:
412		return (EOPNOTSUPP);
413	}
414 	return (0);
415}
416
417static int
418u3g_probe(device_t self)
419{
420	struct usb2_attach_arg *uaa = device_get_ivars(self);
421
422	if (uaa->usb2_mode != USB_MODE_HOST) {
423		return (ENXIO);
424	}
425	if (uaa->info.bConfigIndex != U3G_CONFIG_INDEX) {
426		return (ENXIO);
427	}
428	if (uaa->info.bInterfaceClass != UICLASS_VENDOR) {
429		return (ENXIO);
430	}
431	return (u3g_lookup_huawei(uaa));
432}
433
434static int
435u3g_attach(device_t dev)
436{
437	struct usb2_config u3g_config_tmp[U3G_N_TRANSFER];
438	struct usb2_attach_arg *uaa = device_get_ivars(dev);
439	struct u3g_softc *sc = device_get_softc(dev);
440	struct usb2_interface *iface;
441	struct usb2_interface_descriptor *id;
442	uint32_t flags;
443	uint8_t m;
444	uint8_t n;
445	uint8_t i;
446	uint8_t x;
447	int error;
448
449	DPRINTF("sc=%p\n", sc);
450
451	flags = USB_GET_DRIVER_INFO(uaa);
452
453	if (flags & U3GFL_SAEL_M460_INIT)
454		u3g_sael_m460_init(uaa->device);
455
456	/* copy in USB config */
457	for (n = 0; n != U3G_N_TRANSFER; n++)
458		u3g_config_tmp[n] = u3g_config[n];
459
460	device_set_usb2_desc(dev);
461	mtx_init(&sc->sc_mtx, "u3g", NULL, MTX_DEF);
462
463	sc->sc_udev = uaa->device;
464
465	x = 0;		/* interface index */
466	i = 0;		/* endpoint index */
467	m = 0;		/* number of ports */
468
469	while (m != U3G_MAXPORTS) {
470
471		/* update BULK endpoint index */
472		for (n = 0; n != U3G_N_TRANSFER; n++)
473			u3g_config_tmp[n].ep_index = i;
474
475		iface = usb2_get_iface(uaa->device, x);
476		if (iface == NULL) {
477			if (m != 0)
478				break;	/* end of interfaces */
479			DPRINTF("did not find any modem endpoints\n");
480			goto detach;
481		}
482
483		id = usb2_get_interface_descriptor(iface);
484		if ((id == NULL) ||
485		    (id->bInterfaceClass != UICLASS_VENDOR)) {
486			/* next interface */
487			x++;
488			i = 0;
489			continue;
490		}
491
492		/* try to allocate a set of BULK endpoints */
493		error = usb2_transfer_setup(uaa->device, &x,
494		    sc->sc_xfer[m], u3g_config_tmp, U3G_N_TRANSFER,
495		    &sc->sc_ucom[m], &sc->sc_mtx);
496		if (error) {
497			/* next interface */
498			x++;
499			i = 0;
500			continue;
501		}
502
503		/* grab other interface, if any */
504                if (x != uaa->info.bIfaceIndex)
505                        usb2_set_parent_iface(uaa->device, x,
506                            uaa->info.bIfaceIndex);
507
508		/* set stall by default */
509		mtx_lock(&sc->sc_mtx);
510		usb2_transfer_set_stall(sc->sc_xfer[m][U3G_BULK_WR]);
511		usb2_transfer_set_stall(sc->sc_xfer[m][U3G_BULK_RD]);
512		mtx_unlock(&sc->sc_mtx);
513
514		m++;	/* found one port */
515		i++;	/* next endpoint index */
516	}
517
518	sc->sc_numports = m;
519
520	error = usb2_com_attach(&sc->sc_super_ucom, sc->sc_ucom,
521	    sc->sc_numports, sc, &u3g_callback, &sc->sc_mtx);
522	if (error) {
523		DPRINTF("usb2_com_attach failed\n");
524		goto detach;
525	}
526	if (sc->sc_numports != 1) {
527		/* be verbose */
528		device_printf(dev, "Found %u ports.\n",
529		    (unsigned int)sc->sc_numports);
530	}
531	return (0);
532
533detach:
534	u3g_detach(dev);
535	return (ENXIO);
536}
537
538static int
539u3g_detach(device_t dev)
540{
541	struct u3g_softc *sc = device_get_softc(dev);
542	uint8_t m;
543
544	DPRINTF("sc=%p\n", sc);
545
546	/* NOTE: It is not dangerous to detach more ports than attached! */
547	usb2_com_detach(&sc->sc_super_ucom, sc->sc_ucom, U3G_MAXPORTS);
548
549	for (m = 0; m != U3G_MAXPORTS; m++)
550		usb2_transfer_unsetup(sc->sc_xfer[m], U3G_N_TRANSFER);
551	mtx_destroy(&sc->sc_mtx);
552
553	return (0);
554}
555
556static void
557u3g_start_read(struct usb2_com_softc *ucom)
558{
559	struct u3g_softc *sc = ucom->sc_parent;
560
561	/* start read endpoint */
562	usb2_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
563	return;
564}
565
566static void
567u3g_stop_read(struct usb2_com_softc *ucom)
568{
569	struct u3g_softc *sc = ucom->sc_parent;
570
571	/* stop read endpoint */
572	usb2_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
573	return;
574}
575
576static void
577u3g_start_write(struct usb2_com_softc *ucom)
578{
579	struct u3g_softc *sc = ucom->sc_parent;
580
581	usb2_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
582	return;
583}
584
585static void
586u3g_stop_write(struct usb2_com_softc *ucom)
587{
588	struct u3g_softc *sc = ucom->sc_parent;
589
590	usb2_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
591	return;
592}
593
594static void
595u3g_write_callback(struct usb2_xfer *xfer)
596{
597	struct usb2_com_softc *ucom = xfer->priv_sc;
598	uint32_t actlen;
599
600	switch (USB_GET_STATE(xfer)) {
601	case USB_ST_TRANSFERRED:
602	case USB_ST_SETUP:
603tr_setup:
604		if (usb2_com_get_data(ucom, xfer->frbuffers, 0,
605		    U3G_BSIZE, &actlen)) {
606			xfer->frlengths[0] = actlen;
607			usb2_start_hardware(xfer);
608		}
609		break;
610
611	default:			/* Error */
612		if (xfer->error != USB_ERR_CANCELLED) {
613			/* do a builtin clear-stall */
614			xfer->flags.stall_pipe = 1;
615			goto tr_setup;
616		}
617		break;
618	}
619	return;
620}
621
622static void
623u3g_read_callback(struct usb2_xfer *xfer)
624{
625	struct usb2_com_softc *ucom = xfer->priv_sc;
626
627	switch (USB_GET_STATE(xfer)) {
628	case USB_ST_TRANSFERRED:
629		usb2_com_put_data(ucom, xfer->frbuffers, 0, xfer->actlen);
630
631	case USB_ST_SETUP:
632tr_setup:
633		xfer->frlengths[0] = xfer->max_data_length;
634		usb2_start_hardware(xfer);
635		break;
636
637	default:			/* Error */
638		if (xfer->error != USB_ERR_CANCELLED) {
639			/* do a builtin clear-stall */
640			xfer->flags.stall_pipe = 1;
641			goto tr_setup;
642		}
643		break;
644	}
645	return;
646}
647