xref: /freebsd/sys/dev/usb/serial/u3g.c (revision 566a751354a439384a12dd3f4b43ff3b55ddf9a8)
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$
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
69 static int u3g_debug = 0;
70 
71 SYSCTL_NODE(_hw_usb, OID_AUTO, u3g, CTLFLAG_RW, 0, "USB 3g");
72 SYSCTL_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 
94 enum {
95 	U3G_BULK_WR,
96 	U3G_BULK_RD,
97 	U3G_N_TRANSFER,
98 };
99 
100 struct 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 
113 static device_probe_t u3g_probe;
114 static device_attach_t u3g_attach;
115 static device_detach_t u3g_detach;
116 
117 static usb_callback_t u3g_write_callback;
118 static usb_callback_t u3g_read_callback;
119 
120 static void u3g_start_read(struct ucom_softc *ucom);
121 static void u3g_stop_read(struct ucom_softc *ucom);
122 static void u3g_start_write(struct ucom_softc *ucom);
123 static void u3g_stop_write(struct ucom_softc *ucom);
124 
125 static int u3g_driver_loaded(struct module *mod, int what, void *arg);
126 
127 static 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 
148 static 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 
155 static 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 
162 static devclass_t u3g_devclass;
163 
164 static driver_t u3g_driver = {
165 	.name = "u3g",
166 	.methods = u3g_methods,
167 	.size = sizeof(struct u3g_softc),
168 };
169 
170 DRIVER_MODULE(u3g, uhub, u3g_driver, u3g_devclass, u3g_driver_loaded, 0);
171 MODULE_DEPEND(u3g, ucom, 1, 1, 1);
172 MODULE_DEPEND(u3g, usb, 1, 1, 1);
173 
174 static 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 	/* OEM: Huawei */
177 	U3G_DEV(HUAWEI, MOBILE, U3GFL_HUAWEI_INIT),
178 	U3G_DEV(HUAWEI, E180V, U3GFL_HUAWEI_INIT),
179 	U3G_DEV(HUAWEI, E220, U3GFL_HUAWEI_INIT),
180 	/* OEM: Option */
181 	U3G_DEV(OPTION, GT3G, 0),
182 	U3G_DEV(OPTION, GT3GQUAD, 0),
183 	U3G_DEV(OPTION, GT3GPLUS, 0),
184 	U3G_DEV(OPTION, GTMAX36, 0),
185 	U3G_DEV(OPTION, GTHSDPA, 0),
186 	U3G_DEV(OPTION, GTMAXHSUPA, 0),
187 	U3G_DEV(OPTION, GTMAXHSUPAE, 0),
188 	U3G_DEV(OPTION, GTMAX380HSUPAE, 0),
189 	U3G_DEV(OPTION, VODAFONEMC3G, 0),
190 	/* OEM: Qualcomm, Inc. */
191 	U3G_DEV(QUALCOMMINC, ZTE_STOR, U3GFL_SCSI_EJECT),
192 	U3G_DEV(QUALCOMMINC, CDMA_MSM, U3GFL_SCSI_EJECT),
193 	/* OEM: Merlin */
194 	U3G_DEV(MERLIN, V620, 0),
195 	/* OEM: Novatel */
196 	U3G_DEV(NOVATEL, CDMA_MODEM, 0),
197 	U3G_DEV(NOVATEL, ES620, 0),
198 	U3G_DEV(NOVATEL, MC950D, 0),
199 	U3G_DEV(NOVATEL, U720, 0),
200 	U3G_DEV(NOVATEL, U727, 0),
201 	U3G_DEV(NOVATEL, U740, 0),
202 	U3G_DEV(NOVATEL, U740_2, 0),
203 	U3G_DEV(NOVATEL, U870, 0),
204 	U3G_DEV(NOVATEL, V620, 0),
205 	U3G_DEV(NOVATEL, V640, 0),
206 	U3G_DEV(NOVATEL, V720, 0),
207 	U3G_DEV(NOVATEL, V740, 0),
208 	U3G_DEV(NOVATEL, X950D, 0),
209 	U3G_DEV(NOVATEL, XU870, 0),
210 	U3G_DEV(NOVATEL, ZEROCD, U3GFL_SCSI_EJECT),
211 	U3G_DEV(NOVATEL, U760, U3GFL_SCSI_EJECT),
212 	U3G_DEV(DELL, U740, 0),
213 	/* OEM: Sierra Wireless: */
214 	U3G_DEV(SIERRA, AIRCARD580, 0),
215 	U3G_DEV(SIERRA, AIRCARD595, 0),
216 	U3G_DEV(SIERRA, AC595U, 0),
217 	U3G_DEV(SIERRA, AC597E, 0),
218 	U3G_DEV(SIERRA, C597, 0),
219 	U3G_DEV(SIERRA, AC880, 0),
220 	U3G_DEV(SIERRA, AC880E, 0),
221 	U3G_DEV(SIERRA, AC880U, 0),
222 	U3G_DEV(SIERRA, AC881, 0),
223 	U3G_DEV(SIERRA, AC881E, 0),
224 	U3G_DEV(SIERRA, AC881U, 0),
225 	U3G_DEV(SIERRA, AC885U, 0),
226 	U3G_DEV(SIERRA, EM5625, 0),
227 	U3G_DEV(SIERRA, MC5720, 0),
228 	U3G_DEV(SIERRA, MC5720_2, 0),
229 	U3G_DEV(SIERRA, MC5725, 0),
230 	U3G_DEV(SIERRA, MINI5725, 0),
231 	U3G_DEV(SIERRA, AIRCARD875, 0),
232 	U3G_DEV(SIERRA, MC8755, 0),
233 	U3G_DEV(SIERRA, MC8755_2, 0),
234 	U3G_DEV(SIERRA, MC8755_3, 0),
235 	U3G_DEV(SIERRA, MC8765, 0),
236 	U3G_DEV(SIERRA, AC875U, 0),
237 	U3G_DEV(SIERRA, MC8775_2, 0),
238 	U3G_DEV(SIERRA, MC8780, 0),
239 	U3G_DEV(SIERRA, MC8781, 0),
240 	U3G_DEV(HP, HS2300, 0),
241 	/* Sierra TruInstaller device ID */
242 	U3G_DEV(SIERRA, TRUINSTALL, U3GFL_SIERRA_INIT),
243 	/* PRUEBA SILABS */
244 	U3G_DEV(SILABS, SAEL, U3GFL_SAEL_M460_INIT),
245 };
246 
247 static void
248 u3g_sierra_init(struct usb_device *udev)
249 {
250 	struct usb_device_request req;
251 
252 	req.bmRequestType = UT_VENDOR;
253 	req.bRequest = UR_SET_INTERFACE;
254 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
255 	USETW(req.wIndex, UHF_PORT_CONNECTION);
256 	USETW(req.wLength, 0);
257 
258 	if (usbd_do_request_flags(udev, NULL, &req,
259 	    NULL, 0, NULL, USB_MS_HZ)) {
260 		/* ignore any errors */
261 	}
262 	return;
263 }
264 
265 static void
266 u3g_huawei_init(struct usb_device *udev)
267 {
268 	struct usb_device_request req;
269 
270 	req.bmRequestType = UT_WRITE_DEVICE;
271 	req.bRequest = UR_SET_FEATURE;
272 	USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
273 	USETW(req.wIndex, UHF_PORT_SUSPEND);
274 	USETW(req.wLength, 0);
275 
276 	if (usbd_do_request_flags(udev, NULL, &req,
277 	    NULL, 0, NULL, USB_MS_HZ)) {
278 		/* ignore any errors */
279 	}
280 	return;
281 }
282 
283 static void
284 u3g_sael_m460_init(struct usb_device *udev)
285 {
286 	static const uint8_t setup[][24] = {
287 	     { 0x41, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
288 	     { 0x41, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 },
289 	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
290 	       0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
291 	       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
292 	     { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02 },
293 	     { 0xc1, 0x08, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00 },
294 	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
295 	     { 0xc1, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 },
296 	     { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
297 	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
298 	     { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
299 	     { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
300 	       0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
301 	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
302 	       0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
303 	       0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
304 	     { 0x41, 0x12, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00 },
305 	     { 0x41, 0x01, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00 },
306 	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
307 	     { 0x41, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00 },
308 	     { 0x41, 0x19, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,
309 	       0x00, 0x00, 0x00, 0x00, 0x11, 0x13 },
310 	     { 0x41, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
311 	       0x09, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00,
312 	       0x0a, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00 },
313 	     { 0x41, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 },
314 	};
315 
316 	struct usb_device_request req;
317 	usb_error_t err;
318 	uint16_t len;
319 	uint8_t buf[0x300];
320 	uint8_t n;
321 
322 	DPRINTFN(1, "\n");
323 
324 	if (usbd_req_set_alt_interface_no(udev, NULL, 0, 0)) {
325 		DPRINTFN(0, "Alt setting 0 failed\n");
326 		return;
327 	}
328 
329 	for (n = 0; n != (sizeof(setup)/sizeof(setup[0])); n++) {
330 
331 		memcpy(&req, setup[n], sizeof(req));
332 
333 		len = UGETW(req.wLength);
334 		if (req.bmRequestType & UE_DIR_IN) {
335 			if (len > sizeof(buf)) {
336 				DPRINTFN(0, "too small buffer\n");
337 				continue;
338 			}
339 			err = usbd_do_request(udev, NULL, &req, buf);
340 		} else {
341 			if (len > (sizeof(setup[0]) - 8)) {
342 				DPRINTFN(0, "too small buffer\n");
343 				continue;
344 			}
345 			err = usbd_do_request(udev, NULL, &req,
346 			    __DECONST(uint8_t *, &setup[n][8]));
347 		}
348 		if (err) {
349 			DPRINTFN(1, "request %u failed\n",
350 			    (unsigned int)n);
351 			/*
352 			 * Some of the requests will fail. Stop doing
353 			 * requests when we are getting timeouts so
354 			 * that we don't block the explore/attach
355 			 * thread forever.
356 			 */
357 			if (err == USB_ERR_TIMEOUT)
358 				break;
359 		}
360 	}
361 }
362 
363 static int
364 u3g_lookup_huawei(struct usb_attach_arg *uaa)
365 {
366 	/* Calling the lookup function will also set the driver info! */
367 	return (usbd_lookup_id_by_uaa(u3g_devs, sizeof(u3g_devs), uaa));
368 }
369 
370 /*
371  * The following function handles 3G modem devices (E220, Mobile,
372  * etc.) with auto-install flash disks for Windows/MacOSX on the first
373  * interface.  After some command or some delay they change appearance
374  * to a modem.
375  */
376 static usb_error_t
377 u3g_test_huawei_autoinst(struct usb_device *udev,
378     struct usb_attach_arg *uaa)
379 {
380 	struct usb_interface *iface;
381 	struct usb_interface_descriptor *id;
382 	uint32_t flags;
383 
384 	if (udev == NULL) {
385 		return (USB_ERR_INVAL);
386 	}
387 	iface = usbd_get_iface(udev, 0);
388 	if (iface == NULL) {
389 		return (USB_ERR_INVAL);
390 	}
391 	id = iface->idesc;
392 	if (id == NULL) {
393 		return (USB_ERR_INVAL);
394 	}
395 	if (id->bInterfaceClass != UICLASS_MASS) {
396 		return (USB_ERR_INVAL);
397 	}
398 	if (u3g_lookup_huawei(uaa)) {
399 		/* no device match */
400 		return (USB_ERR_INVAL);
401 	}
402 	flags = USB_GET_DRIVER_INFO(uaa);
403 
404 	if (flags & U3GFL_HUAWEI_INIT) {
405 		u3g_huawei_init(udev);
406 	} else if (flags & U3GFL_SCSI_EJECT) {
407 		return (usb_test_autoinstall(udev, 0, 1));
408 	} else if (flags & U3GFL_SIERRA_INIT) {
409 		u3g_sierra_init(udev);
410 	} else {
411 		/* no quirks */
412 		return (USB_ERR_INVAL);
413 	}
414 	return (0);			/* success */
415 }
416 
417 static int
418 u3g_driver_loaded(struct module *mod, int what, void *arg)
419 {
420 	switch (what) {
421 	case MOD_LOAD:
422 		/* register our autoinstall handler */
423 		usb_test_huawei_autoinst_p = &u3g_test_huawei_autoinst;
424 		break;
425 	case MOD_UNLOAD:
426 		usb_test_huawei_unload(NULL);
427 		break;
428 	default:
429 		return (EOPNOTSUPP);
430 	}
431  	return (0);
432 }
433 
434 static int
435 u3g_probe(device_t self)
436 {
437 	struct usb_attach_arg *uaa = device_get_ivars(self);
438 
439 	if (uaa->usb_mode != USB_MODE_HOST) {
440 		return (ENXIO);
441 	}
442 	if (uaa->info.bConfigIndex != U3G_CONFIG_INDEX) {
443 		return (ENXIO);
444 	}
445 	if (uaa->info.bInterfaceClass != UICLASS_VENDOR) {
446 		return (ENXIO);
447 	}
448 	return (u3g_lookup_huawei(uaa));
449 }
450 
451 static int
452 u3g_attach(device_t dev)
453 {
454 	struct usb_config u3g_config_tmp[U3G_N_TRANSFER];
455 	struct usb_attach_arg *uaa = device_get_ivars(dev);
456 	struct u3g_softc *sc = device_get_softc(dev);
457 	struct usb_interface *iface;
458 	struct usb_interface_descriptor *id;
459 	uint32_t iface_valid;
460 	int error, flags, nports;
461 	int ep, n;
462 	uint8_t i;
463 
464 	DPRINTF("sc=%p\n", sc);
465 
466 	flags = USB_GET_DRIVER_INFO(uaa);
467 
468 	if (flags & U3GFL_SAEL_M460_INIT)
469 		u3g_sael_m460_init(uaa->device);
470 
471 	/* copy in USB config */
472 	for (n = 0; n != U3G_N_TRANSFER; n++)
473 		u3g_config_tmp[n] = u3g_config[n];
474 
475 	device_set_usb_desc(dev);
476 	mtx_init(&sc->sc_mtx, "u3g", NULL, MTX_DEF);
477 
478 	sc->sc_udev = uaa->device;
479 
480 	/* Claim all interfaces on the device */
481 	iface_valid = 0;
482 	for (i = uaa->info.bIfaceIndex; i < USB_IFACE_MAX; i++) {
483 		iface = usbd_get_iface(uaa->device, i);
484 		if (iface == NULL)
485 			break;
486 		id = usbd_get_interface_descriptor(iface);
487 		if (id == NULL || id->bInterfaceClass != UICLASS_VENDOR)
488 			continue;
489 		usbd_set_parent_iface(uaa->device, i, uaa->info.bIfaceIndex);
490 		iface_valid |= (1<<i);
491 	}
492 
493 	i = 0;		/* interface index */
494 	ep = 0;		/* endpoint index */
495 	nports = 0;	/* number of ports */
496 	while (i < USB_IFACE_MAX) {
497 		if ((iface_valid & (1<<i)) == 0) {
498 			i++;
499 			continue;
500 		}
501 
502 		/* update BULK endpoint index */
503 		for (n = 0; n < U3G_N_TRANSFER; n++)
504 			u3g_config_tmp[n].ep_index = ep;
505 
506 		/* try to allocate a set of BULK endpoints */
507 		error = usbd_transfer_setup(uaa->device, &i,
508 		    sc->sc_xfer[nports], u3g_config_tmp, U3G_N_TRANSFER,
509 		    &sc->sc_ucom[nports], &sc->sc_mtx);
510 		if (error) {
511 			/* next interface */
512 			i++;
513 			ep = 0;
514 			continue;
515 		}
516 
517 		/* set stall by default */
518 		mtx_lock(&sc->sc_mtx);
519 		usbd_xfer_set_stall(sc->sc_xfer[nports][U3G_BULK_WR]);
520 		usbd_xfer_set_stall(sc->sc_xfer[nports][U3G_BULK_RD]);
521 		mtx_unlock(&sc->sc_mtx);
522 
523 		nports++;	/* found one port */
524 		ep++;
525 		if (nports == U3G_MAXPORTS)
526 			break;
527 	}
528 	if (nports == 0) {
529 		device_printf(dev, "no ports found\n");
530 		goto detach;
531 	}
532 	sc->sc_numports = nports;
533 
534 	error = ucom_attach(&sc->sc_super_ucom, sc->sc_ucom,
535 	    sc->sc_numports, sc, &u3g_callback, &sc->sc_mtx);
536 	if (error) {
537 		DPRINTF("ucom_attach failed\n");
538 		goto detach;
539 	}
540 	if (sc->sc_numports > 1)
541 		device_printf(dev, "Found %u ports.\n", sc->sc_numports);
542 	return (0);
543 
544 detach:
545 	u3g_detach(dev);
546 	return (ENXIO);
547 }
548 
549 static int
550 u3g_detach(device_t dev)
551 {
552 	struct u3g_softc *sc = device_get_softc(dev);
553 	uint8_t m;
554 
555 	DPRINTF("sc=%p\n", sc);
556 
557 	/* NOTE: It is not dangerous to detach more ports than attached! */
558 	ucom_detach(&sc->sc_super_ucom, sc->sc_ucom, U3G_MAXPORTS);
559 
560 	for (m = 0; m != U3G_MAXPORTS; m++)
561 		usbd_transfer_unsetup(sc->sc_xfer[m], U3G_N_TRANSFER);
562 	mtx_destroy(&sc->sc_mtx);
563 
564 	return (0);
565 }
566 
567 static void
568 u3g_start_read(struct ucom_softc *ucom)
569 {
570 	struct u3g_softc *sc = ucom->sc_parent;
571 
572 	/* start read endpoint */
573 	usbd_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
574 	return;
575 }
576 
577 static void
578 u3g_stop_read(struct ucom_softc *ucom)
579 {
580 	struct u3g_softc *sc = ucom->sc_parent;
581 
582 	/* stop read endpoint */
583 	usbd_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_RD]);
584 	return;
585 }
586 
587 static void
588 u3g_start_write(struct ucom_softc *ucom)
589 {
590 	struct u3g_softc *sc = ucom->sc_parent;
591 
592 	usbd_transfer_start(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
593 	return;
594 }
595 
596 static void
597 u3g_stop_write(struct ucom_softc *ucom)
598 {
599 	struct u3g_softc *sc = ucom->sc_parent;
600 
601 	usbd_transfer_stop(sc->sc_xfer[ucom->sc_local_unit][U3G_BULK_WR]);
602 	return;
603 }
604 
605 static void
606 u3g_write_callback(struct usb_xfer *xfer, usb_error_t error)
607 {
608 	struct ucom_softc *ucom = usbd_xfer_softc(xfer);
609 	struct usb_page_cache *pc;
610 	uint32_t actlen;
611 
612 	switch (USB_GET_STATE(xfer)) {
613 	case USB_ST_TRANSFERRED:
614 	case USB_ST_SETUP:
615 tr_setup:
616 		pc = usbd_xfer_get_frame(xfer, 0);
617 		if (ucom_get_data(ucom, pc, 0, U3G_BSIZE, &actlen)) {
618 			usbd_xfer_set_frame_len(xfer, 0, actlen);
619 			usbd_transfer_submit(xfer);
620 		}
621 		break;
622 
623 	default:			/* Error */
624 		if (error != USB_ERR_CANCELLED) {
625 			/* do a builtin clear-stall */
626 			usbd_xfer_set_stall(xfer);
627 			goto tr_setup;
628 		}
629 		break;
630 	}
631 	return;
632 }
633 
634 static void
635 u3g_read_callback(struct usb_xfer *xfer, usb_error_t error)
636 {
637 	struct ucom_softc *ucom = usbd_xfer_softc(xfer);
638 	struct usb_page_cache *pc;
639 	int actlen;
640 
641 	usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
642 
643 	switch (USB_GET_STATE(xfer)) {
644 	case USB_ST_TRANSFERRED:
645 		pc = usbd_xfer_get_frame(xfer, 0);
646 		ucom_put_data(ucom, pc, 0, actlen);
647 
648 	case USB_ST_SETUP:
649 tr_setup:
650 		usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
651 		usbd_transfer_submit(xfer);
652 		break;
653 
654 	default:			/* Error */
655 		if (error != USB_ERR_CANCELLED) {
656 			/* do a builtin clear-stall */
657 			usbd_xfer_set_stall(xfer);
658 			goto tr_setup;
659 		}
660 		break;
661 	}
662 	return;
663 }
664