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