u3g.c revision 187259
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/usb2/serial/u3g2.c 187259 2009-01-15 02:35:40Z 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 * NOTE:
32 *
33 * - The device ID's are stored in "core/usb2_msctest.c"
34 */
35
36#include <dev/usb2/include/usb2_devid.h>
37#include <dev/usb2/include/usb2_standard.h>
38#include <dev/usb2/include/usb2_mfunc.h>
39#include <dev/usb2/include/usb2_error.h>
40#include <dev/usb2/include/usb2_defs.h>
41
42#define	USB_DEBUG_VAR u3g_debug
43
44#include <dev/usb2/core/usb2_core.h>
45#include <dev/usb2/core/usb2_debug.h>
46#include <dev/usb2/core/usb2_process.h>
47#include <dev/usb2/core/usb2_request.h>
48#include <dev/usb2/core/usb2_lookup.h>
49#include <dev/usb2/core/usb2_util.h>
50#include <dev/usb2/core/usb2_busdma.h>
51#include <dev/usb2/core/usb2_msctest.h>
52#include <dev/usb2/core/usb2_dynamic.h>
53#include <dev/usb2/core/usb2_device.h>
54
55#include <dev/usb2/serial/usb2_serial.h>
56
57#if USB_DEBUG
58static int u3g_debug = 0;
59
60SYSCTL_NODE(_hw_usb2, OID_AUTO, u3g, CTLFLAG_RW, 0, "USB u3g");
61SYSCTL_INT(_hw_usb2_u3g, OID_AUTO, debug, CTLFLAG_RW,
62    &u3g_debug, 0, "u3g debug level");
63#endif
64
65#define	U3G_MAXPORTS		4
66#define	U3G_CONFIG_INDEX	0
67#define	U3G_BSIZE		2048
68
69#define	U3GSP_GPRS		0
70#define	U3GSP_EDGE		1
71#define	U3GSP_CDMA		2
72#define	U3GSP_UMTS		3
73#define	U3GSP_HSDPA		4
74#define	U3GSP_HSUPA		5
75#define	U3GSP_HSPA		6
76#define	U3GSP_MAX		7
77
78#define	U3GFL_NONE		0x00	/* No flags */
79#define	U3GFL_HUAWEI_INIT	0x01	/* Init command required */
80#define	U3GFL_SCSI_EJECT	0x02	/* SCSI eject command required */
81#define	U3GFL_SIERRA_INIT	0x04	/* Init command required */
82
83struct u3g_speeds_s {
84	uint32_t ispeed;
85	uint32_t ospeed;
86};
87
88enum {
89	U3G_BULK_WR,
90	U3G_BULK_RD,
91	U3G_N_TRANSFER = 2,
92};
93
94struct u3g_softc {
95	struct usb2_com_super_softc sc_super_ucom;
96	struct usb2_com_softc sc_ucom;
97
98	struct usb2_xfer *sc_xfer[U3G_N_TRANSFER];
99	struct usb2_device *sc_udev;
100
101	uint8_t	sc_iface_no;		/* interface number */
102	uint8_t	sc_iface_index;		/* interface index */
103	uint8_t	sc_lsr;			/* local status register */
104	uint8_t	sc_msr;			/* U3G status register */
105	struct u3g_speeds_s sc_speed;
106	uint8_t	sc_numports;
107};
108
109static device_probe_t u3g_probe;
110static device_attach_t u3g_attach;
111static device_detach_t u3g_detach;
112
113static usb2_callback_t u3g_write_callback;
114static usb2_callback_t u3g_read_callback;
115
116static void u3g_start_read(struct usb2_com_softc *ucom);
117static void u3g_stop_read(struct usb2_com_softc *ucom);
118static void u3g_start_write(struct usb2_com_softc *ucom);
119static void u3g_stop_write(struct usb2_com_softc *ucom);
120
121static int u3g_driver_loaded(struct module *mod, int what, void *arg);
122
123static const struct usb2_config u3g_config[U3G_N_TRANSFER] = {
124
125	[U3G_BULK_WR] = {
126		.type = UE_BULK,
127		.endpoint = UE_ADDR_ANY,
128		.direction = UE_DIR_OUT,
129		.mh.bufsize = U3G_BSIZE,/* bytes */
130		.mh.flags = {.pipe_bof = 1,.force_short_xfer = 1,},
131		.mh.callback = &u3g_write_callback,
132	},
133
134	[U3G_BULK_RD] = {
135		.type = UE_BULK,
136		.endpoint = UE_ADDR_ANY,
137		.direction = UE_DIR_IN,
138		.mh.bufsize = U3G_BSIZE,/* bytes */
139		.mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
140		.mh.callback = &u3g_read_callback,
141	},
142};
143
144static const struct usb2_com_callback u3g_callback = {
145	.usb2_com_start_read = &u3g_start_read,
146	.usb2_com_stop_read = &u3g_stop_read,
147	.usb2_com_start_write = &u3g_start_write,
148	.usb2_com_stop_write = &u3g_stop_write,
149};
150
151static const struct u3g_speeds_s u3g_speeds[U3GSP_MAX] = {
152	[U3GSP_GPRS] = {64000, 64000},
153	[U3GSP_EDGE] = {384000, 64000},
154	[U3GSP_CDMA] = {384000, 64000},
155	[U3GSP_UMTS] = {384000, 64000},
156	[U3GSP_HSDPA] = {1200000, 384000},
157	[U3GSP_HSUPA] = {1200000, 384000},
158	[U3GSP_HSPA] = {7200000, 384000},
159};
160
161static device_method_t u3g_methods[] = {
162	DEVMETHOD(device_probe, u3g_probe),
163	DEVMETHOD(device_attach, u3g_attach),
164	DEVMETHOD(device_detach, u3g_detach),
165	{0, 0}
166};
167
168static devclass_t u3g_devclass;
169
170static driver_t u3g_driver = {
171	.name = "u3g",
172	.methods = u3g_methods,
173	.size = sizeof(struct u3g_softc),
174};
175
176DRIVER_MODULE(u3g, ushub, u3g_driver, u3g_devclass, u3g_driver_loaded, 0);
177MODULE_DEPEND(u3g, usb2_serial, 1, 1, 1);
178MODULE_DEPEND(u3g, usb2_core, 1, 1, 1);
179
180/* Huawei specific defines */
181
182#define	U3GINFO(flag,speed) ((flag)|((speed) * 256))
183#define	U3G_GET_SPEED(uaa) (USB_GET_DRIVER_INFO(uaa) / 256)
184
185/*
186 * NOTE: The entries marked with XXX should be checked for the correct
187 * speed indication to set the buffer sizes.
188 */
189static const struct usb2_device_id u3g_devs[] = {
190	/* OEM: Option */
191	{USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3G, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},
192	{USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GQUAD, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},
193	{USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GT3GPLUS, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},
194	{USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GTMAX36, U3GINFO(U3GSP_HSDPA, U3GFL_NONE))},
195	{USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_GTMAXHSUPA, U3GINFO(U3GSP_HSDPA, U3GFL_NONE))},
196	{USB_VPI(USB_VENDOR_OPTION, USB_PRODUCT_OPTION_VODAFONEMC3G, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},
197	/* OEM: Qualcomm, Inc. */
198	{USB_VPI(USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_ZTE_STOR, U3GINFO(U3GSP_CDMA, U3GFL_SCSI_EJECT))},
199	{USB_VPI(USB_VENDOR_QUALCOMMINC, USB_PRODUCT_QUALCOMMINC_CDMA_MSM, U3GINFO(U3GSP_CDMA, U3GFL_SCSI_EJECT))},
200	/* OEM: Huawei */
201	{USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_MOBILE, U3GINFO(U3GSP_HSDPA, U3GFL_HUAWEI_INIT))},
202	{USB_VPI(USB_VENDOR_HUAWEI, USB_PRODUCT_HUAWEI_E220, U3GINFO(U3GSP_HSPA, U3GFL_HUAWEI_INIT))},
203	/* OEM: Novatel */
204	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_CDMA_MODEM, U3GINFO(U3GSP_CDMA, U3GFL_SCSI_EJECT))},
205	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_ES620, U3GINFO(U3GSP_UMTS, U3GFL_SCSI_EJECT))},	/* XXX */
206	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_MC950D, U3GINFO(U3GSP_HSUPA, U3GFL_SCSI_EJECT))},
207	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U720, U3GINFO(U3GSP_UMTS, U3GFL_SCSI_EJECT))},	/* XXX */
208	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U727, U3GINFO(U3GSP_UMTS, U3GFL_SCSI_EJECT))},	/* XXX */
209	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U740, U3GINFO(U3GSP_HSDPA, U3GFL_SCSI_EJECT))},
210	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U740_2, U3GINFO(U3GSP_HSDPA, U3GFL_SCSI_EJECT))},
211	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_U870, U3GINFO(U3GSP_UMTS, U3GFL_SCSI_EJECT))},	/* XXX */
212	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V620, U3GINFO(U3GSP_UMTS, U3GFL_SCSI_EJECT))},	/* XXX */
213	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V640, U3GINFO(U3GSP_UMTS, U3GFL_SCSI_EJECT))},	/* XXX */
214	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V720, U3GINFO(U3GSP_UMTS, U3GFL_SCSI_EJECT))},	/* XXX */
215	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_V740, U3GINFO(U3GSP_HSDPA, U3GFL_SCSI_EJECT))},
216	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_X950D, U3GINFO(U3GSP_HSUPA, U3GFL_SCSI_EJECT))},
217	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_XU870, U3GINFO(U3GSP_HSDPA, U3GFL_SCSI_EJECT))},
218	{USB_VPI(USB_VENDOR_NOVATEL, USB_PRODUCT_NOVATEL_ZEROCD, U3GINFO(U3GSP_HSUPA, U3GFL_SCSI_EJECT))},
219	{USB_VPI(USB_VENDOR_DELL, USB_PRODUCT_DELL_U740, U3GINFO(U3GSP_HSDPA, U3GFL_SCSI_EJECT))},
220	/* OEM: Merlin */
221	{USB_VPI(USB_VENDOR_MERLIN, USB_PRODUCT_MERLIN_V620, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
222	/* OEM: Sierra Wireless: */
223	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD580, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
224	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD595, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
225	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC595U, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
226	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC597E, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
227	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_C597, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
228	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
229	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880E, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
230	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC880U, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
231	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
232	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881E, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
233	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC881U, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
234	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_EM5625, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
235	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
236	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5720_2, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
237	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC5725, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
238	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MINI5725, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
239	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AIRCARD875, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
240	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
241	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_2, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
242	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8755_3, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
243	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8765, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
244	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_AC875U, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
245	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8775_2, U3GINFO(U3GSP_HSDPA, U3GFL_NONE))},	/* XXX */
246	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_HS2300, U3GINFO(U3GSP_HSDPA, U3GFL_NONE))},
247	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8780, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
248	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_MC8781, U3GINFO(U3GSP_UMTS, U3GFL_NONE))},	/* XXX */
249	{USB_VPI(USB_VENDOR_HP, USB_PRODUCT_HP_HS2300, U3GINFO(U3GSP_HSPA, U3GFL_NONE))},	/* XXX */
250	/* Sierra TruInstaller device ID */
251	{USB_VPI(USB_VENDOR_SIERRA, USB_PRODUCT_SIERRA_TRUINSTALL, U3GINFO(U3GSP_UMTS, U3GFL_SIERRA_INIT))},
252};
253
254static void
255u3g_sierra_init(struct usb2_device *udev)
256{
257	struct usb2_device_request req;
258
259	DPRINTFN(0, "\n");
260
261	req.bmRequestType = UT_VENDOR;
262	req.bRequest = UR_SET_INTERFACE;
263	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
264	USETW(req.wIndex, UHF_PORT_CONNECTION);
265	USETW(req.wLength, 0);
266
267	if (usb2_do_request_flags(udev, NULL, &req,
268	    NULL, 0, NULL, USB_MS_HZ)) {
269		/* ignore any errors */
270	}
271	return;
272}
273
274static void
275u3g_huawei_init(struct usb2_device *udev)
276{
277	struct usb2_device_request req;
278
279	DPRINTFN(0, "\n");
280
281	req.bmRequestType = UT_WRITE_DEVICE;
282	req.bRequest = UR_SET_FEATURE;
283	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
284	USETW(req.wIndex, UHF_PORT_SUSPEND);
285	USETW(req.wLength, 0);
286
287	if (usb2_do_request_flags(udev, NULL, &req,
288	    NULL, 0, NULL, USB_MS_HZ)) {
289		/* ignore any errors */
290	}
291	return;
292}
293
294static int
295u3g_lookup_huawei(struct usb2_attach_arg *uaa)
296{
297	/* Calling the lookup function will also set the driver info! */
298	return (usb2_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa));
299}
300
301/*
302 * The following function handles 3G modem devices (E220, Mobile,
303 * etc.) with auto-install flash disks for Windows/MacOSX on the first
304 * interface.  After some command or some delay they change appearance
305 * to a modem.
306 */
307static usb2_error_t
308u3g_test_huawei_autoinst(struct usb2_device *udev,
309    struct usb2_attach_arg *uaa)
310{
311	struct usb2_interface *iface;
312	struct usb2_interface_descriptor *id;
313	uint32_t flags;
314
315	if (udev == NULL) {
316		return (USB_ERR_INVAL);
317	}
318	iface = usb2_get_iface(udev, 0);
319	if (iface == NULL) {
320		return (USB_ERR_INVAL);
321	}
322	id = iface->idesc;
323	if (id == NULL) {
324		return (USB_ERR_INVAL);
325	}
326	if (id->bInterfaceClass != UICLASS_MASS) {
327		return (USB_ERR_INVAL);
328	}
329	if (u3g_lookup_huawei(uaa)) {
330		/* no device match */
331		return (USB_ERR_INVAL);
332	}
333	flags = USB_GET_DRIVER_INFO(uaa);
334
335	if (flags & U3GFL_HUAWEI_INIT) {
336		u3g_huawei_init(udev);
337	} else if (flags & U3GFL_SCSI_EJECT) {
338		return (usb2_test_autoinstall(udev, 0, 1));
339	} else if (flags & U3GFL_SIERRA_INIT) {
340		u3g_sierra_init(udev);
341	} else {
342		/* no quirks */
343		return (USB_ERR_INVAL);
344	}
345	return (0);			/* success */
346}
347
348static int
349u3g_driver_loaded(struct module *mod, int what, void *arg)
350{
351	switch (what) {
352	case MOD_LOAD:
353		/* register our autoinstall handler */
354		usb2_test_huawei_autoinst_p = &u3g_test_huawei_autoinst;
355		break;
356	case MOD_UNLOAD:
357		usb2_test_huawei_unload(NULL);
358		break;
359	default:
360		return (EOPNOTSUPP);
361	}
362 	return (0);
363}
364
365static int
366u3g_probe(device_t self)
367{
368	struct usb2_attach_arg *uaa = device_get_ivars(self);
369
370	if (uaa->usb2_mode != USB_MODE_HOST) {
371		return (ENXIO);
372	}
373	if (uaa->info.bConfigIndex != U3G_CONFIG_INDEX) {
374		return (ENXIO);
375	}
376	if (uaa->info.bInterfaceClass != UICLASS_VENDOR) {
377		return (ENXIO);
378	}
379	return (u3g_lookup_huawei(uaa));
380}
381
382static int
383u3g_attach(device_t dev)
384{
385	struct usb2_attach_arg *uaa = device_get_ivars(dev);
386	struct u3g_softc *sc = device_get_softc(dev);
387	int error;
388
389	DPRINTF("sc=%p\n", sc);
390
391	if (sc == NULL) {
392		return (ENOMEM);
393	}
394	device_set_usb2_desc(dev);
395
396	sc->sc_udev = uaa->device;
397	sc->sc_iface_no = uaa->info.bIfaceNum;
398	sc->sc_iface_index = uaa->info.bIfaceIndex;
399	sc->sc_speed = u3g_speeds[U3G_GET_SPEED(uaa)];
400
401	error = usb2_transfer_setup(uaa->device, &sc->sc_iface_index,
402	    sc->sc_xfer, u3g_config, U3G_N_TRANSFER, sc, &Giant);
403
404	if (error) {
405		DPRINTF("could not allocate all pipes\n");
406		goto detach;
407	}
408	/* set stall by default */
409	usb2_transfer_set_stall(sc->sc_xfer[U3G_BULK_WR]);
410	usb2_transfer_set_stall(sc->sc_xfer[U3G_BULK_RD]);
411
412	error = usb2_com_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
413	    &u3g_callback, &Giant);
414	if (error) {
415		DPRINTF("usb2_com_attach failed\n");
416		goto detach;
417	}
418	return (0);
419
420detach:
421	u3g_detach(dev);
422	return (ENXIO);
423}
424
425static int
426u3g_detach(device_t dev)
427{
428	struct u3g_softc *sc = device_get_softc(dev);
429
430	DPRINTF("sc=%p\n", sc);
431
432	usb2_com_detach(&sc->sc_super_ucom, &sc->sc_ucom, 1);
433
434	usb2_transfer_unsetup(sc->sc_xfer, U3G_N_TRANSFER);
435
436	return (0);
437}
438
439static void
440u3g_start_read(struct usb2_com_softc *ucom)
441{
442	struct u3g_softc *sc = ucom->sc_parent;
443
444	/* start read endpoint */
445	usb2_transfer_start(sc->sc_xfer[U3G_BULK_RD]);
446	return;
447}
448
449static void
450u3g_stop_read(struct usb2_com_softc *ucom)
451{
452	struct u3g_softc *sc = ucom->sc_parent;
453
454	/* stop read endpoint */
455	usb2_transfer_stop(sc->sc_xfer[U3G_BULK_RD]);
456	return;
457}
458
459static void
460u3g_start_write(struct usb2_com_softc *ucom)
461{
462	struct u3g_softc *sc = ucom->sc_parent;
463
464	usb2_transfer_start(sc->sc_xfer[U3G_BULK_WR]);
465	return;
466}
467
468static void
469u3g_stop_write(struct usb2_com_softc *ucom)
470{
471	struct u3g_softc *sc = ucom->sc_parent;
472
473	usb2_transfer_stop(sc->sc_xfer[U3G_BULK_WR]);
474	return;
475}
476
477static void
478u3g_write_callback(struct usb2_xfer *xfer)
479{
480	struct u3g_softc *sc = xfer->priv_sc;
481	uint32_t actlen;
482
483	switch (USB_GET_STATE(xfer)) {
484	case USB_ST_TRANSFERRED:
485	case USB_ST_SETUP:
486tr_setup:
487		if (usb2_com_get_data(&sc->sc_ucom, xfer->frbuffers, 0,
488		    U3G_BSIZE, &actlen)) {
489			xfer->frlengths[0] = actlen;
490			usb2_start_hardware(xfer);
491		}
492		break;
493
494	default:			/* Error */
495		if (xfer->error != USB_ERR_CANCELLED) {
496			/* do a builtin clear-stall */
497			xfer->flags.stall_pipe = 1;
498			goto tr_setup;
499		}
500		break;
501	}
502	return;
503}
504
505static void
506u3g_read_callback(struct usb2_xfer *xfer)
507{
508	struct u3g_softc *sc = xfer->priv_sc;
509
510	switch (USB_GET_STATE(xfer)) {
511	case USB_ST_TRANSFERRED:
512		usb2_com_put_data(&sc->sc_ucom, xfer->frbuffers, 0, xfer->actlen);
513
514	case USB_ST_SETUP:
515tr_setup:
516		xfer->frlengths[0] = xfer->max_data_length;
517		usb2_start_hardware(xfer);
518		break;
519
520	default:			/* Error */
521		if (xfer->error != USB_ERR_CANCELLED) {
522			/* do a builtin clear-stall */
523			xfer->flags.stall_pipe = 1;
524			goto tr_setup;
525		}
526		break;
527	}
528	return;
529}
530